]> git.madduck.net Git - etc/vim.git/blob - after/ftplugin/markdown.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Import tests to syntax.vader from test/header.md
[etc/vim.git] / after / ftplugin / markdown.vim
1 " folding for Markdown headers, both styles (atx- and setex-)
2 " http://daringfireball.net/projects/markdown/syntax#header
3 "
4 " this code can be placed in file
5 "   $HOME/.vim/after/ftplugin/markdown.vim
6 "
7 " original version from Steve Losh's gist: https://gist.github.com/1038710
8
9 function! s:is_mkdCode(lnum)
10     return synIDattr(synID(a:lnum, 1, 0), 'name') == 'mkdCode'
11 endfunction
12
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)
18             return '>0'
19         elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
20             " next line is underlined (level 2)
21             return '>1'
22         endif
23
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)
28         elseif a:lnum == 1
29             " fold any 'preamble'
30             return '>1'
31         else
32             " keep previous foldlevel
33             return '='
34         endif
35     endfunction
36
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
47     endfunction
48 else
49     function! Foldexpr_markdown(lnum)
50         let l2 = getline(a:lnum+1)
51         if  l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
52             " next line is underlined (level 1)
53             return '>1'
54         elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
55             " next line is underlined (level 2)
56             return '>2'
57         endif
58
59         let l1 = getline(a:lnum)
60         if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
61             " don't include the section title in the fold
62             return '-1'
63         endif
64
65         if (a:lnum == 1)
66             let l0 = ''
67         else
68             let l0 = getline(a:lnum-1)
69         endif
70         if l0 =~ '^#' && !s:is_mkdCode(a:lnum-1)
71             " current line starts with hashes
72             return '>'.matchend(l0, '^#\+')
73         else
74             " keep previous foldlevel
75             return '='
76         endif
77     endfunction
78 endif
79
80 if !get(g:, "vim_markdown_folding_disabled", 0)
81     setlocal foldexpr=Foldexpr_markdown(v:lnum)
82     setlocal foldmethod=expr
83     if get(g:, "vim_markdown_folding_style_pythonic", 0)
84         setlocal foldtext=Foldtext_markdown()
85     endif
86 endif