2017-10-17 9 views
1

正規表現にマッチするかどうかに基づいてvimの折り返しをプログラムで閉じたいと思う。私はそうするために私のvimrcで関数を定義した:アイデアは、それが存在する場合は、そのパターンがある場所に移動し、パターンを検索することですVim:特定の行の折り返しを閉じる

" Support python 2 and 3 
if has('python') 
    command! -nargs=1 Python2or3 python <args> 
elseif has('python3') 
    command! -nargs=1 Python2or3 python3 <args> 
else 
    echo "Error: Requires Vim compiled with +python or +python3" 
    finish 
endif 

" Define function 
func! FoldCopyrightHeader() 
Python2or3 << EOF 
import re 
import vim 
# Look at the first 30 lines 
header = '\n'.join(vim.current.buffer[0:30]) 
pattern = 'Copyright .* by .* THE POSSIBILITY OF SUCH DAMAGE' 
match = re.search(pattern, header, flags=re.MULTILINE | re.DOTALL) 
if match: 
    # Find the line number of the block to fold 
    lineno = header[:match.start()].count('\n') 
    # Remember the current position 
    row, col = vim.current.window.cursor 
    # move cursor to the fold block 
    vim.command('cal cursor({},{})'.format(lineno, 0)) 
    # close the fold 
    vim.command('call feedkeys("zc")') 
    # move back to the original position 
    vim.command('cal cursor({},{})'.format(row, col)) 
EOF 
endfunc 

、折り畳みを閉じるには、キーコマンドzcを入力します。元の位置に戻ってください。

しかし、これはうまくいきません。 :call FoldCopyrightHeader()を介してこの関数を呼び出すと、現在カーソルがどのような倍数になっていても閉じます。

私の推測では、feedkeysコマンドは非同期vim.command('call feedkeys("zc")')であり、移動カーソルコマンドの実行の前後に起こっていると思います。

これを防ぐためにできることはありますか?

答えて

1

そして私は質問をタイプしているときにそれを解決しました。

vim.command('call feedkeys("zc")')の代わりにvim.command(':foldclose')を使用すると、このトリックを行うようです。

関連する問題