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 l2 = getline(a:lnum+1)
17 if l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
18 " next line is underlined (level 1)
20 elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
21 " next line is underlined (level 2)
25 let l1 = getline(a:lnum)
26 if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
27 " current line starts with hashes
28 return '>'.(matchend(l1, '^#\+') - 1)
33 " keep previous foldlevel
38 function! Foldtext_markdown()
39 let line = getline(v:foldstart)
40 let has_numbers = &number || &relativenumber
41 let nucolwidth = &fdc + has_numbers * &numberwidth
42 let windowwidth = winwidth(0) - nucolwidth - 6
43 let foldedlinecount = v:foldend - v:foldstart
44 let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
45 let line = substitute(line, '\%("""\|''''''\)', '', '')
46 let fillcharcount = windowwidth - len(line) - len(foldedlinecount) + 1
47 return line . ' ' . repeat("-", fillcharcount) . ' ' . foldedlinecount
50 function! Foldexpr_markdown(lnum)
51 let l2 = getline(a:lnum+1)
52 if l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
53 " next line is underlined (level 1)
55 elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
56 " next line is underlined (level 2)
60 let l1 = getline(a:lnum)
61 if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
62 " don't include the section title in the fold
69 let l0 = getline(a:lnum-1)
71 if l0 =~ '^#' && !s:is_mkdCode(a:lnum-1)
72 " current line starts with hashes
73 return '>'.matchend(l0, '^#\+')
75 " keep previous foldlevel
81 if !get(g:, "vim_markdown_folding_disabled", 0)
82 setlocal foldexpr=Foldexpr_markdown(v:lnum)
83 setlocal foldmethod=expr
84 if get(g:, "vim_markdown_folding_style_pythonic", 0)
85 setlocal foldtext=Foldtext_markdown()