]> 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:

1916a730cfe901e6fdca2204d807a7d9f699ad91
[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         if (a:lnum == 1)
51             let l0 = ''
52         else
53             let l0 = getline(a:lnum-1)
54         endif
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
61             endif
62         endif
63
64         let l2 = getline(a:lnum+1)
65         if  l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
66             " next line is underlined (level 1)
67             return 0
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
71                 return 0
72             else
73                 return 1
74             endif
75         endif
76
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
82                 return -1
83             else
84                 " code blocks are always folded
85                 return b:fenced_block
86             endif
87         endif
88
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
92                 return 1
93             endif
94             " current line starts with hashes
95             return '>'.matchend(l0, '^#\+')
96         else
97             " fold here because of setext headers
98             return 1
99         endif
100     endfunction
101 endif
102
103
104 let b:fenced_block = 0
105 let g:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1)
106
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()
112     endif
113 endif