~telemachus/vim-dotfiles

vim-dotfiles/plugin/telemachus/neatfold.vim -rw-r--r-- 1.0 KiB
33c5d6b7Peter Aronoff Merge 'refs/remotes/origin/main' 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
" Custom fold expression

" Code from https://superuser.com/a/990547/907
" See also the following
"   * https://bitbucket.org/atimholt/spiffy_foldtext/src/default/
"   * https://gist.github.com/sjl/3360978
"   * http://vimcasts.org/episodes/writing-a-custom-fold-expression/
"   * https://vimrcfu.com/snippet/20
function! NeatFoldText()
  let line = ' ' . substitute(getline(v:foldstart),
        \ '^\s*"\?\s*\|\s*"\?\s*{{' . '{\d*\s*', '', 'g') . ' '
  let lines_count = v:foldend - v:foldstart + 1
  let lines_count_text = '| ' . printf('%10s', lines_count . ' lines') . ' |'
  let foldchar = matchstr(&fillchars, 'fold:\zs.')
  let foldtextstart = strpart('+' . repeat(foldchar, v:foldlevel*2) .
        \ line, 0, (winwidth(0)*2)/3)
  let foldtextend = lines_count_text . repeat(foldchar, 8)
  let foldtextlength = strlen(substitute(foldtextstart . foldtextend, '.',
        \ 'x', 'g')) + &foldcolumn
  return foldtextstart . repeat(foldchar, winwidth(0)-foldtextlength) .
        \ foldtextend
endfunction

set foldtext=NeatFoldText()