From: Ben Williams Date: Thu, 15 Nov 2012 20:48:44 +0000 (-0500) Subject: Switch to expression-based folding. X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/20704d86a0ae043664f3facca84a61eded05de6e?ds=inline Switch to expression-based folding. Syntax-based folding was way too slow in very large Markdown files. Add a new file after/ftplugin/mkd.vim with a foldexpr function to determine folds. The function is based on Steve Losh's gist at https://gist.github.com/1038710 --- diff --git a/after/ftplugin/mkd.vim b/after/ftplugin/mkd.vim new file mode 100644 index 0000000..f04ae2b --- /dev/null +++ b/after/ftplugin/mkd.vim @@ -0,0 +1,51 @@ +" folding for Markdown headers, both styles (atx- and setex-) +" http://daringfireball.net/projects/markdown/syntax#header +" +" this code can be placed in file +" $HOME/.vim/after/ftplugin/markdown.vim +" +" original version from Steve Losh's gist: https://gist.github.com/1038710 + +func! Foldexpr_markdown(lnum) + if (a:lnum == 0) + let l0 = getline(a:lnum) + else + let l0 = getline(a:lnum-1) + endif + + let l1 = getline(a:lnum) + + let l2 = getline(a:lnum+1) + + if l2 =~ '^==\+\s*' + " next line is underlined (level 1) + return '>1' + elseif l2 =~ '^--\+\s*' + " next line is underlined (level 2) + return '>2' + elseif l1 =~ '^#' + " don't include the section title in the fold + return '-1' + elseif l0 =~ '^#' + " current line starts with hashes + return '>'.matchend(l0, '^#\+') + elseif a:lnum == 1 + " fold any 'preamble' + return '>1' + else + " keep previous foldlevel + return '=' + endif +endfunc + +setlocal foldexpr=Foldexpr_markdown(v:lnum) +setlocal foldmethod=expr + +"---------- everything after this is optional ----------------------- +" change the following fold options to your liking +" see ':help fold-options' for more +setlocal foldenable +setlocal foldlevel=0 +setlocal foldcolumn=0 +set foldmethod=expr +set foldopen-=search diff --git a/syntax/mkd.vim b/syntax/mkd.vim index 8f3fd86..c99968d 100644 --- a/syntax/mkd.vim +++ b/syntax/mkd.vim @@ -86,27 +86,6 @@ syn region htmlH6 start="^\s*######" end="\($\|#\+\)" contain syn match htmlH1 /^.\+\n=\+$/ contains=@Spell syn match htmlH2 /^.\+\n-\+$/ contains=@Spell - - -" fold region for headings -syn region mkdHeaderFold - \ start="^\s*\z(#\+\)" - \ skip="^\s*\z1#\+" - \ end="^\(\s*#\)\@=" - \ fold contains=TOP - -" fold region for lists -syn region mkdListFold - \ start="^\z(\s*\)\*\z(\s*\)" - \ skip="^\z1 \z2\s*[^#]" - \ end="^\(.\)\@=" - \ fold contains=TOP - -syn sync fromstart -setlocal foldmethod=syntax - - - "highlighting for Markdown groups HtmlHiLink mkdString String HtmlHiLink mkdCode String