]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .vim/bundle/autofolds/plugin/autofolds.vim
.vim: Support folding based on custom section markers
[dotfiles.git] / .vim / bundle / autofolds / plugin / autofolds.vim
1 " autofolds
2 " =========
3 " Automatic folding based on custom section markers
4 " Inspired by: https://vi.stackexchange.com/a/6608
5
6 function! autofolds#foldexpr(lnum, ...)
7 let s:filetype = a:0 > 0 ? a:1 : 'vim' " optional 'filetype' param
8 let s:comment_char = ''
9 if s:filetype == 'vim'
10 let s:comment_char = '"'
11 elseif s:filetype == 'sh'
12 let s:comment_char = '#'
13 endif
14 if s:comment_char == ''
15 return '='
16 endif
17 let s:thisline = getline(a:lnum)
18 let s:two_following_lines = 0
19 if line(a:lnum) + 2 <= line('$')
20 let s:line_1_after = getline(a:lnum+1)
21 let s:line_2_after = getline(a:lnum+2)
22 let s:two_following_lines = 1
23 endif
24 if !s:two_following_lines
25 return '='
26 endif
27 else
28 if (match(s:thisline, '^'.s:comment_char.' ----------') >= 0) &&
29 \ (match(s:line_1_after, '^'.s:comment_char.' ') >= 0) &&
30 \ (match(s:line_2_after, '^'.s:comment_char.' ----------') >= 0)
31 return '>1'
32 else
33 return '='
34 endif
35 endif
36 endfunction
37
38 function! autofolds#foldtext()
39 let s:lines = string(v:foldend-v:foldstart) " # of lines in fold range
40 let s:text = getline(v:foldstart+1)[2:] " extract text after leading comment marker
41 let s:text = substitute(s:text,'^\s*\(.\{-}\)\s*$', '\1', '') " strip leading/trailing whitespace
42 return '+'.repeat('-', 1 + v:foldlevel).repeat(' ', 3-len(s:lines)).s:lines.' lines: '.s:text.' ' " mimic standard foldtext() format
43 endfunction