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

Fix bug related to folding and hash comments
[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 '>1'
68         elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
69             " next line is underlined (level 2)
70             return '>2'
71         endif
72
73         let l1 = getline(a:lnum)
74         if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
75             " fold level according to option
76             " (in vim -1 is visible, >= 0 is folded)
77             let l:level = matchend(l1, '^#\+')
78             if g:vim_markdown_folding_level == 1 || l:level > g:vim_markdown_folding_level
79                 return -1
80             else
81                 " code blocks are always folded
82                 return b:fenced_block
83             endif
84         endif
85
86         if l0 =~ '^#' && !s:is_mkdCode(a:lnum-1)
87             " collapse comments in fenced code blocks into a single fold
88             if b:fenced_block == 1
89                 return 1
90             endif
91             " current line starts with hashes
92             return '>'.matchend(l0, '^#\+')
93         else
94             " keep previous foldlevel
95             return '='
96         endif
97     endfunction
98 endif
99
100
101 let b:fenced_block = 0
102 let g:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1)
103
104 if !get(g:, "vim_markdown_folding_disabled", 0)
105     setlocal foldexpr=Foldexpr_markdown(v:lnum)
106     setlocal foldmethod=expr
107     if get(g:, "vim_markdown_folding_style_pythonic", 0)
108         setlocal foldtext=Foldtext_markdown()
109     endif
110 endif