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 let l2 = getline(a:lnum+1)
27 if l2 =~ '^==\+\s*' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum+1)
28 " next line is underlined (level 1)
30 elseif l2 =~ '^--\+\s*' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum+1)
31 " next line is underlined (level 2)
35 if l1 =~ '^#' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum)
36 " current line starts with hashes
37 return '>'.(matchend(l1, '^#\+') - 1)
42 " keep previous foldlevel
47 function! Foldtext_markdown()
48 let line = getline(v:foldstart)
49 let has_numbers = &number || &relativenumber
50 let nucolwidth = &fdc + has_numbers * &numberwidth
51 let windowwidth = winwidth(0) - nucolwidth - 6
52 let foldedlinecount = v:foldend - v:foldstart
53 let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
54 let line = substitute(line, '\%("""\|''''''\)', '', '')
55 let fillcharcount = windowwidth - len(line) - len(foldedlinecount) + 1
56 return line . ' ' . repeat("-", fillcharcount) . ' ' . foldedlinecount
59 function! Foldexpr_markdown(lnum)
63 let l0 = getline(a:lnum-1)
65 " keep track of fenced code blocks
66 if l0 =~ '````*' || l0 =~ '\~\~\~\~*'
67 if b:fenced_block == 0
68 let b:fenced_block = 1
69 elseif b:fenced_block == 1
70 let b:fenced_block = 0
74 let l2 = getline(a:lnum+1)
75 if l2 =~ '^==\+\s*' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum+1)
76 " next line is underlined (level 1)
78 elseif l2 =~ '^--\+\s*' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum+1)
79 " next line is underlined (level 2)
80 if g:vim_markdown_folding_level >= 2
87 let l1 = getline(a:lnum)
88 if l1 =~ '^#' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum)
89 " fold level according to option
90 let l:level = matchend(l1, '^#\+')
91 if g:vim_markdown_folding_level == 1 || l:level > g:vim_markdown_folding_level
94 " headers are not folded
99 if l0 =~ '^#' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum-1)
100 " current line starts with hashes
101 return '>'.matchend(l0, '^#\+')
103 " fold here because of setext headers
110 let b:fenced_block = 0
111 let g:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1)
113 if !get(g:, "vim_markdown_folding_disabled", 0)
114 setlocal foldexpr=Foldexpr_markdown(v:lnum)
115 setlocal foldmethod=expr
116 if get(g:, "vim_markdown_folding_style_pythonic", 0)
117 setlocal foldtext=Foldtext_markdown()