I want to toggle foldmethod
between expr
and manual
because if we always set foldmethod=expr
, then the cursor tends to move slowly in vim.
I think that foldexpr
is evaluated just after switching foldmethod=expr
, thus I try to toggle at every 5000ms in the following way.
However, foldexpr
can't be evaluated, which implies foldexpr
is not evaluated just after switching foldmethod=expr
.
Then, I have a question: how often or when is foldexpr
evaluated?
To my knowledge, there does not exist any document about timing of evaluation of foldexpr
.
function! s:enable_expr(timer_id) abort
setlocal foldmethod=expr
setlocal foldmethod=manual
endfunction
autocmd CursorMoved * call s:cursor_moved()
let s:timer_id = timer_start(5000, function('s:enable_expr'))
function! s:cursor_moved() abort
setlocal foldmethod=manual
call timer_stop(s:timer_id)
let s:timer_id = timer_start(5000, function('s:enable_expr'))
endfunction
REMARK:
I know a way to toggle them by using InsertEnter
and InsertLeave
like here.
But, if we employ the toggle in this way, then switching between insert mode and normal mode can become slow.
Moreover, I use the command <C-o>
a lot in insert mode, thus we want to avoid this implementation.
Thus, I want to make vim evaluate foldexpr
once in all lines when the cursor holds.
Additional REMARK:
I toggle them with the following implementation for now.
A drawback of this implementation is when we move the cursor after 5000ms under the cursor-hold, the move of the cursor becomes slowly at the beginning due to the switching foldmethod
.
function! s:enable_expr(timer_id) abort
if &l:foldmethod ==# 'manual'
setlocal foldmethod=expr
endif
endfunction
autocmd CursorMoved * call s:cursor_moved()
let s:timer_id = timer_start(5000, function('s:enable_expr'))
function! s:cursor_moved() abort
if &l:foldmethod ==# 'expr'
setlocal foldmethod=manual
endif
call timer_stop(s:timer_id)
let s:timer_id = timer_start(5000, function('s:enable_expr'))
endfunction
A temporary solution
augroup foldmethod-syntax
autocmd InsertEnter,CursorMoved,CursorMovedI * if &l:foldmethod ==# 'expr'
\ | setlocal foldmethod=manual
\ | endif
autocmd BufWrite * if &l:foldmethod ==# 'manual'
\ | setlocal foldmethod=expr
\ | endif
augroup END
InsertEnter
andInsertLeave
. However, if we toggle them in this way, then the command<C-o>
in insert mode tends to be slowly due to the toggle. I want to avoid this situation.:def
function might increase the performance; once fixed, this todo item might help too.