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")')
并且在移动光标命令执行之前/之后发生。
有什么我可以做的,以防止这种情况?