]> git.madduck.net Git - etc/vim.git/blob - after/ftplugin/mkd.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:

f04ae2bec4e8e9b3bb407ac53d948317ade502b0
[etc/vim.git] / after / ftplugin / mkd.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! Foldexpr_markdown(lnum)
10     if (a:lnum == 0)
11         let l0 = getline(a:lnum)
12     else
13         let l0 = getline(a:lnum-1)
14     endif
15
16     let l1 = getline(a:lnum)
17
18     let l2 = getline(a:lnum+1)
19
20     if  l2 =~ '^==\+\s*'
21         " next line is underlined (level 1)
22         return '>1'
23     elseif l2 =~ '^--\+\s*'
24         " next line is underlined (level 2)
25         return '>2'
26     elseif l1 =~ '^#'
27         " don't include the section title in the fold
28         return '-1'
29     elseif l0 =~ '^#'
30         " current line starts with hashes
31         return '>'.matchend(l0, '^#\+')
32     elseif a:lnum == 1
33         " fold any 'preamble'
34         return '>1'
35     else
36         " keep previous foldlevel
37         return '='
38     endif
39 endfunc
40
41 setlocal foldexpr=Foldexpr_markdown(v:lnum)
42 setlocal foldmethod=expr
43
44 "---------- everything after this is optional -----------------------
45 " change the following fold options to your liking
46 " see ':help fold-options' for more
47 setlocal foldenable
48 setlocal foldlevel=0
49 setlocal foldcolumn=0
50 set foldmethod=expr
51 set foldopen-=search