2015-06-02 09:12:01 -04:00
|
|
|
" vim: et sw=2 sts=2
|
|
|
|
|
|
|
|
" Function: SignifyFoldExpr {{{1
|
|
|
|
function! SignifyFoldExpr(lnum)
|
|
|
|
return s:levels[a:lnum]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Function: SignifyFoldText {{{1
|
|
|
|
function! SignifyFoldText()
|
|
|
|
let linelen = &textwidth ? &textwidth : 80
|
|
|
|
let marker = &foldmarker[:stridx(&foldmarker, ',')-1]
|
|
|
|
let range = foldclosedend(v:foldstart) - foldclosed(v:foldstart) + 1
|
|
|
|
|
|
|
|
let left = substitute(getline(v:foldstart), marker, '', '')
|
|
|
|
let leftlen = len(left)
|
|
|
|
|
|
|
|
let right = printf('%d [%d]', range, v:foldlevel)
|
|
|
|
let rightlen = len(right)
|
|
|
|
|
|
|
|
let tmp = strpart(left, 0, linelen - rightlen)
|
|
|
|
let tmplen = len(tmp)
|
|
|
|
|
|
|
|
if leftlen > tmplen
|
|
|
|
let left = strpart(tmp, 0, tmplen - 4) . '... '
|
|
|
|
let leftlen = tmplen
|
|
|
|
endif
|
|
|
|
|
|
|
|
let fill = repeat(' ', linelen - (leftlen + rightlen))
|
|
|
|
|
|
|
|
" return left . fill . right . repeat(' ', 100)
|
|
|
|
return left . fill . right
|
|
|
|
endfunction
|
|
|
|
|
2015-12-08 18:31:59 -05:00
|
|
|
" Function: #dispatch {{{1
|
|
|
|
function! sy#fold#dispatch(do_tab) abort
|
|
|
|
if a:do_tab
|
|
|
|
call sy#fold#enable(1)
|
|
|
|
else
|
|
|
|
call sy#fold#toggle()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2015-12-08 16:19:46 -05:00
|
|
|
" Function: #enable {{{1
|
|
|
|
function! sy#fold#enable(do_tab) abort
|
2015-06-02 09:12:01 -04:00
|
|
|
if !exists('b:sy')
|
|
|
|
echomsg 'signify: I cannot detect any changes!'
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2015-12-08 16:19:46 -05:00
|
|
|
if a:do_tab
|
2015-12-08 07:40:10 -05:00
|
|
|
tabedit %
|
|
|
|
endif
|
|
|
|
|
2015-06-02 09:12:01 -04:00
|
|
|
let [s:context0, s:context1] = get(g:, 'signify_fold_context', [3, 8])
|
|
|
|
let s:levels = s:get_levels(s:get_lines())
|
|
|
|
|
2015-12-08 16:19:46 -05:00
|
|
|
setlocal foldexpr=SignifyFoldExpr(v:lnum)
|
|
|
|
setlocal foldtext=SignifyFoldText()
|
|
|
|
setlocal foldmethod=expr
|
|
|
|
setlocal foldlevel=0
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Function: #disable {{{1
|
|
|
|
function! sy#fold#disable() abort
|
2017-12-23 18:15:15 -05:00
|
|
|
let &l:foldmethod = b:sy_folded.method
|
|
|
|
let &l:foldtext = b:sy_folded.text
|
2015-12-08 17:47:15 -05:00
|
|
|
normal! zv
|
2015-12-08 05:19:08 -05:00
|
|
|
endfunction
|
|
|
|
|
2015-12-08 16:19:46 -05:00
|
|
|
" Function: #toggle {{{1
|
2015-12-08 18:31:59 -05:00
|
|
|
function! sy#fold#toggle() abort
|
2017-12-23 18:15:15 -05:00
|
|
|
if exists('b:sy_folded')
|
2015-12-08 16:19:46 -05:00
|
|
|
call sy#fold#disable()
|
2017-12-23 18:15:15 -05:00
|
|
|
if b:sy_folded.method == 'manual'
|
2015-12-08 17:47:15 -05:00
|
|
|
loadview
|
|
|
|
endif
|
2017-12-23 18:15:15 -05:00
|
|
|
unlet b:sy_folded
|
2015-12-08 07:40:10 -05:00
|
|
|
else
|
2017-12-23 18:15:15 -05:00
|
|
|
let b:sy_folded = { 'method': &foldmethod, 'text': &foldtext }
|
2015-12-08 17:47:15 -05:00
|
|
|
if &foldmethod == 'manual'
|
|
|
|
let old_vop = &viewoptions
|
|
|
|
mkview
|
|
|
|
let &viewoptions = old_vop
|
|
|
|
endif
|
2015-12-08 18:31:59 -05:00
|
|
|
call sy#fold#enable(0)
|
2015-12-08 07:40:10 -05:00
|
|
|
endif
|
2015-12-08 16:19:46 -05:00
|
|
|
|
|
|
|
redraw!
|
|
|
|
call sy#start()
|
2015-06-02 09:12:01 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Function: s:get_lines {{{1
|
|
|
|
function! s:get_lines() abort
|
|
|
|
let lang = v:lang
|
|
|
|
language message C
|
|
|
|
redir => signlist
|
|
|
|
silent! execute 'sign place buffer='. b:sy.buffer
|
|
|
|
redir END
|
|
|
|
silent! execute 'language message' lang
|
|
|
|
|
|
|
|
let lines = []
|
|
|
|
for line in split(signlist, '\n')[2:]
|
|
|
|
call insert(lines, matchlist(line, '\v^\s+line\=(\d+)')[1], 0)
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return reverse(lines)
|
|
|
|
endfunction
|
2015-12-08 05:19:08 -05:00
|
|
|
" }}}
|
|
|
|
|
2015-06-02 09:12:01 -04:00
|
|
|
" Function: s:get_levels {{{1
|
|
|
|
function! s:get_levels(lines) abort
|
|
|
|
let levels = {}
|
|
|
|
|
|
|
|
for line in range(1, line('$'))
|
|
|
|
let levels[line] = 2
|
|
|
|
endfor
|
|
|
|
|
|
|
|
for line in a:lines
|
|
|
|
for l in range(line - s:context1, line + s:context1)
|
|
|
|
if (l < 1) || (l > line('$'))
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
if levels[l] == 2
|
|
|
|
let levels[l] = 1
|
|
|
|
endif
|
|
|
|
for ll in range(line - s:context0, line + s:context0)
|
|
|
|
let levels[ll] = 0
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return levels
|
|
|
|
endfunction
|