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 return synIDattr(synID(a:lnum, 1, 0), 'name') == 'mkdCode'
13 if get(g:, "vim_markdown_folding_style_pythonic", 0)
14 function! Foldexpr_markdown(lnum)
15 let l2 = getline(a:lnum+1)
16 if l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
17 " next line is underlined (level 1)
19 elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
20 " next line is underlined (level 2)
24 let l1 = getline(a:lnum)
25 if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
26 " current line starts with hashes
27 return '>'.(matchend(l1, '^#\+') - 1)
32 " keep previous foldlevel
37 function! Foldtext_markdown()
38 let line = getline(v:foldstart)
39 let has_numbers = &number || &relativenumber
40 let nucolwidth = &fdc + has_numbers * &numberwidth
41 let windowwidth = winwidth(0) - nucolwidth - 6
42 let foldedlinecount = v:foldend - v:foldstart
43 let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
44 let line = substitute(line, '\%("""\|''''''\)', '', '')
45 let fillcharcount = windowwidth - len(line) - len(foldedlinecount) + 1
46 return line . ' ' . repeat("-", fillcharcount) . ' ' . foldedlinecount
49 function! Foldexpr_markdown(lnum)
53 let l0 = getline(a:lnum-1)
55 " keep track of fenced code blocks
56 if l0 =~ '````*' || l0 =~ '~~~~*'
57 if b:fenced_block == 0
58 let b:fenced_block = 1
59 elseif b:fenced_block == 1
60 let b:fenced_block = 0
64 let l2 = getline(a:lnum+1)
65 if l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
66 " next line is underlined (level 1)
68 elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
69 " next line is underlined (level 2)
70 if g:vim_markdown_folding_level == 2
77 let l1 = getline(a:lnum)
78 if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
79 " fold level according to option
80 let l:level = matchend(l1, '^#\+')
81 if g:vim_markdown_folding_level == 1 || l:level > g:vim_markdown_folding_level
84 " code blocks are always folded
89 if l0 =~ '^#' && !s:is_mkdCode(a:lnum-1)
90 " collapse comments in fenced code blocks into a single fold
91 if b:fenced_block == 1
94 " current line starts with hashes
95 return '>'.matchend(l0, '^#\+')
97 " fold here because of setext headers
104 let b:fenced_block = 0
105 let g:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1)
107 if !get(g:, "vim_markdown_folding_disabled", 0)
108 setlocal foldexpr=Foldexpr_markdown(v:lnum)
109 setlocal foldmethod=expr
110 if get(g:, "vim_markdown_folding_style_pythonic", 0)
111 setlocal foldtext=Foldtext_markdown()