Je souhaite fermer par programme un pli dans vim selon qu'il correspond ou non à une expression régulière. J'ai défini une fonction dans mon vimrc de le faire:Vim: Fermer un pli sur une ligne spécifique
" 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
L'idée est de rechercher le motif, si elle existe, puis déplacez à l'endroit où le modèle est, entrez les commandes clés zc
pour fermer le pli , puis revenez à votre position d'origine.
Cependant, cela ne fonctionne pas tout à fait. Si j'appelle cette fonction via :call FoldCopyrightHeader()
, alors il ferme n'importe quel repli du curseur et ne fait rien d'autre.
Ma conjecture est que la commande feedkeys
est asynchrone vim.command('call feedkeys("zc")')
et se passe avant/après l'exécution des commandes du curseur de déplacement.
Y a-t-il quelque chose que je puisse faire pour éviter cela?