vim9script SetUndoFtplugin setlocal shiftwidth< SetUndoFtplugin setlocal foldexpr< foldmethod< # The below is already set by textobj-function # SetUndoFtplugin unlet! b:textobj_function_select setlocal shiftwidth=2 setlocal foldmethod=expr setbufvar('%', 'textobj_function_select', function('vimrc#textobj_vim#select')) &l:foldexpr = expand('') .. 'FoldExpr()' def FoldIsBlockOpen(line: string): bool if line =~# '\v^<%(%(export\s+|legacy\s+)?%(fu%[nction]|def)|if|for|while|try)>' || line =~# '\v^augroup\s+%(<\cEND>)@!' || line =~# '\V' .. split(&l:foldmarker, ',')[0] .. '\d\*\s\*\$' return true endif return false enddef def FoldIsBlockClose(line: string): bool if line =~# '\v^' || line =~# '\v^augroup\s+<\cEND>' || line =~# '\V' .. split(&l:foldmarker, ',')[1] .. '\d\*\s\*\$' return true endif return false enddef def FoldExpr(): any var line = getline(v:lnum) if line =~# '^\s' return '=' elseif FoldIsBlockOpen(line) return '>1' elseif v:lnum == 1 || FoldIsBlockClose(getline(prevnonblank(v:lnum - 1))) if getline(v:lnum) ==# '' && (v:lnum - 1) == prevnonblank(v:lnum - 1) && getline(v:lnum - 1) =~# '\v^end%(func%[tion]|def)' return '=' endif return 0 endif return '=' enddef