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

Merge pull request #297 from KainokiKaede/master
[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     let name = synIDattr(synID(a:lnum, 1, 0), 'name')
11     return (name =~ '^mkd\%(Code$\|Snippet\)' || name != '' && name !~ '^\%(mkd\|html\)')
12 endfunction
13
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
23             endif
24         elseif g:vim_markdown_frontmatter == 1
25             if b:front_matter == 1 && a:lnum > 2
26                 let l0 = getline(a:lnum-1)
27                 if l0 == '---'
28                     let b:front_matter = 0
29                 endif
30             elseif a:lnum == 1
31                 if l1 == '---'
32                     let b:front_matter = 1
33                 endif
34             endif
35         endif
36
37         if b:fenced_block == 1 || b:front_matter == 1
38             if a:lnum == 1
39                 " fold any 'preamble'
40                 return '>1'
41             else
42                 " keep previous foldlevel
43                 return '='
44             endif
45         endif
46
47         let l2 = getline(a:lnum+1)
48         if l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
49             " next line is underlined (level 1)
50             return '>0'
51         elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
52             " next line is underlined (level 2)
53             return '>1'
54         endif
55
56         if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
57             " current line starts with hashes
58             return '>'.(matchend(l1, '^#\+') - 1)
59         elseif a:lnum == 1
60             " fold any 'preamble'
61             return '>1'
62         else
63             " keep previous foldlevel
64             return '='
65         endif
66     endfunction
67
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
78     endfunction
79 else
80     function! Foldexpr_markdown(lnum)
81         if (a:lnum == 1)
82             let l0 = ''
83         else
84             let l0 = getline(a:lnum-1)
85         endif
86
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
93             endif
94         elseif g:vim_markdown_frontmatter == 1
95             if b:front_matter == 1
96                 if l0 == '---'
97                     let b:front_matter = 0
98                 endif
99             elseif a:lnum == 2
100                 if l0 == '---'
101                     let b:front_matter = 1
102                 endif
103             endif
104         endif
105
106         if b:fenced_block == 1 || b:front_matter == 1
107             " keep previous foldlevel
108             return '='
109         endif
110
111         let l2 = getline(a:lnum+1)
112         if  l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
113             " next line is underlined (level 1)
114             return '>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
118                 return '>1'
119             else
120                 return '>2'
121             endif
122         endif
123
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                 return -1
129             else
130                 " headers are not folded
131                 return 0
132             endif
133         endif
134
135         if l0 =~ '^#' && !s:is_mkdCode(a:lnum-1)
136             " current line starts with hashes
137             return '>'.matchend(l0, '^#\+')
138         else
139             " keep previous foldlevel
140             return '='
141         endif
142     endfunction
143 endif
144
145
146 let b:fenced_block = 0
147 let b:front_matter = 0
148 let s:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1)
149
150 if !get(g:, "vim_markdown_folding_disabled", 0)
151     setlocal foldexpr=Foldexpr_markdown(v:lnum)
152     setlocal foldmethod=expr
153     if get(g:, "vim_markdown_folding_style_pythonic", 0) && get(g:, "vim_markdown_override_foldtext", 1)
154         setlocal foldtext=Foldtext_markdown()
155     endif
156 endif