5

わかりました、これはかなり複雑な欲求です。これは、ほとんどのFortran行に「call」ステートメントが含まれているという事実に起因しており、call、call、call、call...と入力するのにうんざりしています。

私が欲しいのは次のとおりです。

  • Enterキーを押すたびに、次の行の前に「call」文字列が自動的に追加されます。
  • Tabキーを押すと、文字列呼び出しの前にタブが追加されます(インデントできるように)
  • 行の先頭にいて、バックスペースを1回押すと、「呼び出し」エントリは削除されますが、タブは残ります。同様に、「if」、「do」、「enddo」、および関連するすべての情報を入力すると、エントリが自動的に削除されると便利です。

このようなものがすでに存在し、可能であるかどうかを知っていますか、そして私が取ることができるヒントや同様のスクリプトがあれば、それは非常にありがたいです。

たとえば、Cスタイルのコメントでは、Enterキーを押すたびに、行の先頭にアスタリスクが自動的に追加されます。それを行うコードはどこにありますか(vimでハードコーディングされていないプラグイン機能だと思います)?

4

2 に答える 2

3

私はCコメントについてのあなたのヒントに従い、これを思いついた:

:set formatoptions+=ro
:set comments+=s:call,m:call,e:call

行が「call」で始まる場合、これは同じテキストで連続する行を自動的に追加する必要があります。前の行に「call」が含まれている場合にのみ機能し、指定したキーワードで「call」は削除されません。これらのオプションを自由に試してみてください。おそらく、youtsrの好みに合わせてカスタマイズできます。

>>通常モードでのインデント使用または挿入モードでのCtrl+の使用。T行の先頭にある「call」を削除するには、。の代わりにCtrl+を使用します。WBackspace

または、略語を使用して入力を高速化することもできます。

:iab ,, call
于 2012-05-10T11:03:03.227 に答える
0

<CR>私の意見では、との動作を変更する必要がある場合<BS>、それは何かが間違っているか、または多くのエッジ ケースがあるため遅かれ早かれ間違っていることを意味します。

私が見つけた最大の問題の 1 つは、カーソルが関数内の最初の列にあるか 2 番目の列にあるかを推測できないことです。これは、タブとバックスペースを正しく処理できるようにするための重要なポイントです。しかし、ここから始めましょう。めちゃくちゃなので、徹底的にテストしていません。お勧めしません。私の意見では、mykiのアプローチの方がはるかに優れています。

このよくコメントされたコードをファイルに追加してvimrcテストします。

"" When pressed 'return' in insert mode:
"" <Esc>: Exit from Insert Mode to Normal Mode.
"" o: Add a new line below the current one and set Insert Mode.
"" call <Esc>: Write literal 'call ' and exit from Insert Mode.
"" A: Set cursor at end of line and enter Insert Mode to begin writting.
inoremap <cr> <Esc>ocall <Esc>A

function! SpecialTab()

    "" Get cursor position.
    let cursor_pos = getpos('.')

    "" If line is empty, insert a tab, update current position and finish
    let line_len = strlen( getline( line('.') ) ) 
    if empty( getline( line('.') ) ) || line_len == 1
        s/\%#/\t/   
        let cursor_pos[2] += 1
        call setpos( '.', cursor_pos )
        return
    endif

    "" Search for a line beginning with 'call', omitting spaces. If found
    "" insert a tab at the beginning of line.
    if match( getline( line('.') ), "\s*call" ) != -1
        s/^/\t/
    else
        "" Insert a normal tab in current cursor position. I cannot use
        "" the regular <Tab> command because function would entry in a 
        "" infinite recursion due to the mapping.
        s/\%#\(.\)/\1\t/
    endif

    "" Set cursor column in the character it was before adding tab character.
    let cursor_pos[2] += 2
    call setpos( '.', cursor_pos )
endfunction

"" Map the tab character.
inoremap <Tab> <Esc>:call SpecialTab()<cr>:startinsert<cr>

function! SpecialBackspace()
    "" Do nothing if line is empty.
    if empty( getline( line('.') ) ) 
        return
    endif

    "" Get cursor position.
    let cursor_pos = getpos( '.' )

    "" If cursor is not in first column press 'delete' button and done.
    if col('.') > 1 
        execute "normal \<Del>"
        return
    endif

    "" Search for 'call' string. If found delete it and set cursor in
    "" previous position.
    if match( getline( line('.') ), "\s*call" ) != -1
        s/call//
        let cursor_pos[2] = 1 
        call setpos( '.', cursor_pos )
        return
    endif

    "" A line with one character is a special case. I delete the complete
    "" line.
    let line_len = strlen( getline( line('.') ) )
    if line_len == 1
        s/^.*$//
        return
    endif

    "" If cursor is in first column, delete character. Messy behavior, I
    "" think :-/
    if col('.') == 1
        s/^.//
    endif
endfunction

"" Map the 'backspace' character.
inoremap <BS> <Esc>:call SpecialBackspace()<cr>:startinsert<cr>
于 2012-05-10T14:19:07.213 に答える