]> 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 code block syntax of longer backticks, tildes
[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         endif
25
26         let l2 = getline(a:lnum+1)
27         if l2 =~ '^==\+\s*' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum+1)
28             " next line is underlined (level 1)
29             return '>0'
30         elseif l2 =~ '^--\+\s*' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum+1)
31             " next line is underlined (level 2)
32             return '>1'
33         endif
34
35         if l1 =~ '^#' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum)
36             " current line starts with hashes
37             return '>'.(matchend(l1, '^#\+') - 1)
38         elseif a:lnum == 1
39             " fold any 'preamble'
40             return '>1'
41         else
42             " keep previous foldlevel
43             return '='
44         endif
45     endfunction
46
47     function! Foldtext_markdown()
48         let line = getline(v:foldstart)
49         let has_numbers = &number || &relativenumber
50         let nucolwidth = &fdc + has_numbers * &numberwidth
51         let windowwidth = winwidth(0) - nucolwidth - 6
52         let foldedlinecount = v:foldend - v:foldstart
53         let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
54         let line = substitute(line, '\%("""\|''''''\)', '', '')
55         let fillcharcount = windowwidth - len(line) - len(foldedlinecount) + 1
56         return line . ' ' . repeat("-", fillcharcount) . ' ' . foldedlinecount
57     endfunction
58 else
59     function! Foldexpr_markdown(lnum)
60         if (a:lnum == 1)
61             let l0 = ''
62         else
63             let l0 = getline(a:lnum-1)
64         endif
65         " keep track of fenced code blocks
66         if l0 =~ '````*' || l0 =~ '\~\~\~\~*'
67             if b:fenced_block == 0
68                 let b:fenced_block = 1
69             elseif b:fenced_block == 1
70                 let b:fenced_block = 0
71             endif
72         endif
73
74         let l2 = getline(a:lnum+1)
75         if  l2 =~ '^==\+\s*' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum+1)
76             " next line is underlined (level 1)
77             return '>1'
78         elseif l2 =~ '^--\+\s*' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum+1)
79             " next line is underlined (level 2)
80             if g:vim_markdown_folding_level >= 2
81                 return '>1'
82             else
83                 return '>2'
84             endif
85         endif
86
87         let l1 = getline(a:lnum)
88         if l1 =~ '^#' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum)
89             " fold level according to option
90             let l:level = matchend(l1, '^#\+')
91             if g:vim_markdown_folding_level == 1 || l:level > g:vim_markdown_folding_level
92                 return -1
93             else
94                 " headers are not folded
95                 return 0
96             endif
97         endif
98
99         if l0 =~ '^#' && b:fenced_block == 0 && !s:is_mkdCode(a:lnum-1)
100             " current line starts with hashes
101             return '>'.matchend(l0, '^#\+')
102         else
103             " fold here because of setext headers
104             return '='
105         endif
106     endfunction
107 endif
108
109
110 let b:fenced_block = 0
111 let g:vim_markdown_folding_level = get(g:, "vim_markdown_folding_level", 1)
112
113 if !get(g:, "vim_markdown_folding_disabled", 0)
114     setlocal foldexpr=Foldexpr_markdown(v:lnum)
115     setlocal foldmethod=expr
116     if get(g:, "vim_markdown_folding_style_pythonic", 0)
117         setlocal foldtext=Foldtext_markdown()
118     endif
119 endif