]> 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 test failures when the window width is small
[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 l2 = getline(a:lnum+1)
17         if  l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
18             " next line is underlined (level 1)
19             return '>0'
20         elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
21             " next line is underlined (level 2)
22             return '>1'
23         endif
24
25         let l1 = getline(a:lnum)
26         if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
27             " current line starts with hashes
28             return '>'.(matchend(l1, '^#\+') - 1)
29         elseif a:lnum == 1
30             " fold any 'preamble'
31             return '>1'
32         else
33             " keep previous foldlevel
34             return '='
35         endif
36     endfunction
37
38     function! Foldtext_markdown()
39         let line = getline(v:foldstart)
40         let has_numbers = &number || &relativenumber
41         let nucolwidth = &fdc + has_numbers * &numberwidth
42         let windowwidth = winwidth(0) - nucolwidth - 6
43         let foldedlinecount = v:foldend - v:foldstart
44         let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
45         let line = substitute(line, '\%("""\|''''''\)', '', '')
46         let fillcharcount = windowwidth - len(line) - len(foldedlinecount) + 1
47         return line . ' ' . repeat("-", fillcharcount) . ' ' . foldedlinecount
48     endfunction
49 else
50     function! Foldexpr_markdown(lnum)
51         let l2 = getline(a:lnum+1)
52         if  l2 =~ '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
53             " next line is underlined (level 1)
54             return '>1'
55         elseif l2 =~ '^--\+\s*' && !s:is_mkdCode(a:lnum+1)
56             " next line is underlined (level 2)
57             return '>2'
58         endif
59
60         let l1 = getline(a:lnum)
61         if l1 =~ '^#' && !s:is_mkdCode(a:lnum)
62             " don't include the section title in the fold
63             return '-1'
64         endif
65
66         if (a:lnum == 1)
67             let l0 = ''
68         else
69             let l0 = getline(a:lnum-1)
70         endif
71         if l0 =~ '^#' && !s:is_mkdCode(a:lnum-1)
72             " current line starts with hashes
73             return '>'.matchend(l0, '^#\+')
74         else
75             " keep previous foldlevel
76             return '='
77         endif
78     endfunction
79 endif
80
81 if !get(g:, "vim_markdown_folding_disabled", 0)
82     setlocal foldexpr=Foldexpr_markdown(v:lnum)
83     setlocal foldmethod=expr
84     if get(g:, "vim_markdown_folding_style_pythonic", 0)
85         setlocal foldtext=Foldtext_markdown()
86     endif
87 endif