I am writing a filetype plugin that will (among other things) fold LaTeX documents by use of a custom foldlevel function. In order to make the folding efficient, I use
foldmethod=manual during editing and I define a function to refresh the folds, something like:
function! RefreshFolds() setlocal foldmethod=expr normal! zx setlocal foldmethod=manual endfunction
A small catch is that this requires me to refresh the folds manually after I open a buffer. In general, I want to refresh the folds manually, but I want them to be refreshed and defined when the buffer is opened. The first thought was that I could simply call
Refreshfolds() during initialization, but for some reason this didn't work. Instead I have found that I can refresh the folds by utilizing a self destructing autocommand as follows:
augroup init_folds autocmd! autocmd CursorMoved <buffer> call InitFolds() augroup END function! InitFolds() call RefreshFolds() autocmd! init_folds endfunction
This works as desired in most cases, but not if I open several buffers in separate tabs with
vim -p *.tex. In this case, it seems the buffers are opened simultaneously, which means the autocommand is only run for one of the buffers before it self destructs.
What I want is to keep
foldmethod=manual, but at the same time I want to ensure that folds are refreshed through
RefreshFolds() after a buffer is opened. Does anyone know how to either fix the above approach so that it works when buffers are loaded simultaneously, or of a different approach that does not have the same problem?
Note: This problem comes from this Github issue.