3 " Automatic folding based on custom section markers
4 " Inspired by: https://vi.stackexchange.com/a/6608
6 function! autofolds#foldexpr(lnum, ...)
7 let s:filetype = a:0 > 0 ? a:1 : 'vim' " optional 'filetype' param
8 let s:comment_char = ''
10 let s:comment_char = '"'
11 elseif s:filetype == 'sh'
12 let s:comment_char = '#'
14 if s:comment_char == ''
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
24 if !s:two_following_lines
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)
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