]> git.madduck.net Git - etc/vim.git/blob - after/ftplugin/markdown.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Foldmethod: improve performance
[etc/vim.git] / after / ftplugin / markdown.vim
1 " folding for Markdown headers, both styles (atx- and setex-)
2 " http://daringfireball.net/projects/markdown/syntax#header
3 "
4 " this code can be placed in file
5 "   $HOME/.vim/after/ftplugin/markdown.vim
6 "
7 " original version from Steve Losh's gist: https://gist.github.com/1038710
8
9 func! s:is_mkdCode(lnum)
10     return synIDattr(synID(a:lnum, 1, 0), 'name') == 'mkdCode'
11 endfunc
12
13 func! s:effective_line(lnum)
14     let line = getline(a:lnum)
15     return (line !~ '^=\|^#' || s:is_mkdCode(a:lnum)) ? '' : line
16 endfunc
17
18 func! Foldexpr_markdown(lnum)
19     if (a:lnum == 1)
20         let l0 = ''
21     else
22         let l0 = s:effective_line(a:lnum-1)
23     endif
24
25     let l1 = s:effective_line(a:lnum)
26
27     let l2 = s:effective_line(a:lnum+1)
28
29     if  l2 =~ '^==\+\s*'
30         " next line is underlined (level 1)
31         return '>1'
32     elseif l2 =~ '^--\+\s*'
33         " next line is underlined (level 2)
34         return '>2'
35     elseif l1 =~ '^#'
36         " don't include the section title in the fold
37         return '-1'
38     elseif l0 =~ '^#'
39         " current line starts with hashes
40         return '>'.matchend(l0, '^#\+')
41     else
42         " keep previous foldlevel
43         return '='
44     endif
45 endfunc
46
47
48 if !get(g:, "vim_markdown_folding_disabled", 0)
49   setlocal foldexpr=Foldexpr_markdown(v:lnum)
50   setlocal foldmethod=expr
51 endif