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
24 elseif g:vim_markdown_frontmatter == 1
25 if b:front_matter == 1 && a:lnum > 2
26 let l0 = getline(a:lnum-1)
28 let b:front_matter = 0
32 let b:front_matter = 1
37 if b:fenced_block == 1 || b:front_matter == 1
42 " keep previous foldlevel
47 let l2 = getline(a:lnum+1)
48 if l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
49 " next line is underlined (level 1)
51 elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
52 " next line is underlined (level 2)
56 if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
57 " current line starts with hashes
58 return '>'.(matchend(l1, '^#\+') - 1)
63 " keep previous foldlevel
68 function! Foldtext_markdown()
69 let line = getline(v:foldstart)
70 let has_numbers = &number || &relativenumber
71 let nucolwidth = &fdc + has_numbers * &numberwidth
72 let windowwidth = winwidth(0) - nucolwidth - 6
73 let foldedlinecount = v:foldend - v:foldstart
74 let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
75 let line = substitute(line, '\%("""\|''''''\)', '', '')
76 let fillcharcount = windowwidth - len(line) - len(foldedlinecount) + 1
77 return line . ' ' . repeat("-", fillcharcount) . ' ' . foldedlinecount
80 function! Foldexpr_markdown(lnum)
84 let l0 = getline(a:lnum-1)
87 " keep track of fenced code blocks
88 if l0 =~ '````*' || l0 =~ '\~\~\~\~*'
89 if b:fenced_block == 0
90 let b:fenced_block = 1
91 elseif b:fenced_block == 1
92 let b:fenced_block = 0
94 elseif g:vim_markdown_frontmatter == 1
95 if b:front_matter == 1
97 let b:front_matter = 0
101 let b:front_matter = 1
106 if b:fenced_block == 1 || b:front_matter == 1
107 " keep previous foldlevel
111 let l2 = getline(a:lnum+1)
112 if l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
113 " next line is underlined (level 1)
115 elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
116 " next line is underlined (level 2)
117 if s:vim_markdown_folding_level >= 2
124 let l1 = getline(a:lnum)
125 if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
126 " fold level according to option
127 if s:vim_markdown_folding_level == 1 || matchend(l1, '^#\+') > s:vim_markdown_folding_level
128 if a:lnum == line('$')
129 return matchend(l1, '^#\+') - 1
134 " headers are not folded
139 if l0 =~ '^#' && !s:is_mkdCode(a:lnum-1)
140 " previous line starts with hashes
141 return '>'.matchend(l0, '^#\+')
143 " keep previous foldlevel
150 let b:fenced_block = 0
151 let b:front_matter = 0
152 let s:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1)
154 function! s:MarkdownSetupFolding()
155 if !get(g:, "vim_markdown_folding_disabled", 0)
156 setlocal foldexpr=Foldexpr_markdown(v:lnum)
157 setlocal foldmethod=expr
158 if get(g:, "vim_markdown_folding_style_pythonic", 0) && get(g:, "vim_markdown_override_foldtext", 1)
159 setlocal foldtext=Foldtext_markdown()
163 call s:MarkdownSetupFolding()
165 " These autocmds need to be kept in sync with the autocmds calling
166 " s:MarkdownRefreshSyntax in ftplugin/markdown.vim.
167 autocmd BufWinEnter,BufWritePost <buffer> call s:MarkdownSetupFolding()
168 autocmd InsertEnter,InsertLeave <buffer> call s:MarkdownSetupFolding()
169 autocmd CursorHold,CursorHoldI <buffer> call s:MarkdownSetupFolding()