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