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.
1 " folding for Markdown headers, both styles (atx- and setex-)
2 " http://daringfireball.net/projects/markdown/syntax#header
4 " this code can be placed in file
5 " $HOME/.vim/after/ftplugin/markdown.vim
7 " original version from Steve Losh's gist: https://gist.github.com/1038710
9 function! s:is_mkdCode(lnum)
10 let name = synIDattr(synID(a:lnum, 1, 0), 'name')
11 return (name =~ '^mkd\%(Code$\|Snippet\)' || name != '' && name !~ '^\%(mkd\|html\)')
14 if get(g:, "vim_markdown_folding_style_pythonic", 0)
15 function! Foldexpr_markdown(lnum)
16 let l1 = getline(a:lnum)
17 " keep track of fenced code blocks
18 if l1 =~ '````*' || l1 =~ '\~\~\~\~*'
19 if b:fenced_block == 0
20 let b:fenced_block = 1
21 elseif b:fenced_block == 1
22 let b:fenced_block = 0
26 if b:fenced_block == 1
31 " keep previous foldlevel
36 let l2 = getline(a:lnum+1)
37 if l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
38 " next line is underlined (level 1)
40 elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
41 " next line is underlined (level 2)
45 if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
46 " current line starts with hashes
47 return '>'.(matchend(l1, '^#\+') - 1)
52 " keep previous foldlevel
57 function! Foldtext_markdown()
58 let line = getline(v:foldstart)
59 let has_numbers = &number || &relativenumber
60 let nucolwidth = &fdc + has_numbers * &numberwidth
61 let windowwidth = winwidth(0) - nucolwidth - 6
62 let foldedlinecount = v:foldend - v:foldstart
63 let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
64 let line = substitute(line, '\%("""\|''''''\)', '', '')
65 let fillcharcount = windowwidth - len(line) - len(foldedlinecount) + 1
66 return line . ' ' . repeat("-", fillcharcount) . ' ' . foldedlinecount
69 function! Foldexpr_markdown(lnum)
73 let l0 = getline(a:lnum-1)
76 " keep track of fenced code blocks
77 if l0 =~ '````*' || l0 =~ '\~\~\~\~*'
78 if b:fenced_block == 0
79 let b:fenced_block = 1
80 elseif b:fenced_block == 1
81 let b:fenced_block = 0
85 if b:fenced_block == 1
86 " keep previous foldlevel
90 let l2 = getline(a:lnum+1)
91 if l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
92 " next line is underlined (level 1)
94 elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
95 " next line is underlined (level 2)
96 if g:vim_markdown_folding_level >= 2
103 let l1 = getline(a:lnum)
104 if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
105 " fold level according to option
106 let l:level = matchend(l1, '^#\+')
107 if g:vim_markdown_folding_level == 1 || l:level > g:vim_markdown_folding_level
110 " headers are not folded
115 if l0 =~ '^#' && !s:is_mkdCode(a:lnum-1)
116 " current line starts with hashes
117 return '>'.matchend(l0, '^#\+')
119 " fold here because of setext headers
126 let b:fenced_block = 0
127 let g:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1)
129 if !get(g:, "vim_markdown_folding_disabled", 0)
130 setlocal foldexpr=Foldexpr_markdown(v:lnum)
131 setlocal foldmethod=expr
132 if get(g:, "vim_markdown_folding_style_pythonic", 0)
133 setlocal foldtext=Foldtext_markdown()