Skip to content

Instantly share code, notes, and snippets.

@ypresto
Created December 15, 2012 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypresto/4293828 to your computer and use it in GitHub Desktop.
Save ypresto/4293828 to your computer and use it in GitHub Desktop.
Workaround for foldmethod=expr too heavy problem
" ** FoldRenewer ** {{{2
" Work around for performance problem of expr/syntax foldmethods
" Inspired by: http://vim.wikia.com/wiki/Keep_folds_closed_while_inserting_text
augroup FoldRenewer
autocmd!
" VimEnter: for delayed stashing
" WinEnter: because foldmethod is window-specific
autocmd VimEnter,WinEnter * call FoldRenewerInit()
" open fold under the cursor after re-generating folds
" Recalculate only on saving buffer to reduce freeze
autocmd BufWritePost * call RestoreFoldMethod() | call StashFoldMethod()
" workaround for buffer change from outside of current window,
" like gundo plugin and multi split of single file
autocmd WinLeave * call StashFoldMethod()
augroup END
" Initialize on current window
function! FoldRenewerInit()
if !exists('w:fold_renewer_init_done')
" Set original fdm after splitting window
if exists('b:orig_fdm')
let &l:foldmethod=b:orig_fdm
endif
let w:fold_renewer_init_done = 1
" Delay for permit other plugins to initialize
" FIXME: this autocmd should be WINDOW SPECIFIC
augroup DelayedStashFoldMethod
autocmd!
" CursorMoved: called at start of editting, after ready to edit
autocmd CursorMoved,CursorMovedI * call StashFoldMethod()
augroup END
endif
endfunction
" Let foldmethod calculate folds
function! RestoreFoldMethod()
if &filetype == 'ref-perldoc'
" black list
setlocal foldmethod=manual
return
endif
if exists('w:last_fdm')
if &foldmethod == 'manual'
let &l:foldmethod=w:last_fdm
" open folds under the cursor
execute "normal" "zv"
endif
unlet w:last_fdm
endif
endfunction
" Preserve foldmethod and set it to 'manual'
function! StashFoldMethod()
if !exists('w:last_fdm') && (&foldmethod == 'expr' || &foldmethod == 'syntax')
let b:orig_fdm=&foldmethod
let w:last_fdm=&foldmethod
setlocal foldmethod=manual
endif
autocmd! DelayedStashFoldMethod
endfunction
" ** }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment