目录与Vim折叠

时间:2016-09-13 21:27:00

标签: vim fold folding

如何展开包含折叠的折叠,以获得文档的轮廓? 如果所有东西都折叠了,我会按zr几次,我会得到一些接近我想要的东西,除非部件有不同的深度,否则我要么看不到折叠,要么看到一些内容。

在这个例子中:

# Title {{{1
# Subtitle {{{2
some code here
# Another Title {{{1
code here directly under the level 1 title

折叠后我想看到这个:

# Title {{{1
# Subtitle {{{2
# Another Title {{{1

1 个答案:

答案 0 :(得分:0)

这不是微不足道的;我用一个确定嵌套级别的递归函数解决了这个问题,然后关闭了最里面的折叠。

" [count]zy     Unfold all folds containing a fold / containing at least
"           [count] levels of folds. Like |zr|, but counting from
"           the inside-out. Useful to obtain an outline of the Vim
"           buffer that shows the overall structure while hiding the
"           details.
function! s:FoldOutlineRecurse( count, startLnum, endLnum )
    silent! keepjumps normal! zozj

    if line('.') > a:endLnum
        " We've moved out of the current parent fold.
        " Thus, there are no contained folds, and this one should be closed.
        execute a:startLnum . 'foldclose'
        return [0, 1]
    elseif line('.') == a:startLnum && foldclosed('.') == -1
        " We've arrived at the last fold in the buffer.
        execute a:startLnum . 'foldclose'
        return [1, 1]
    else
        let l:nestLevelMax = 0
        let l:isDone = 0
        while ! l:isDone && line('.') <= a:endLnum
            let l:endOfFold = foldclosedend('.')
            let l:endOfFold = (l:endOfFold == -1 ? line('$') : l:endOfFold)
            let [l:isDone, l:nestLevel] = s:FoldOutlineRecurse(a:count, line('.'), l:endOfFold)
            if l:nestLevel > l:nestLevelMax
                let l:nestLevelMax = l:nestLevel
            endif
        endwhile

        if l:nestLevelMax < a:count
            execute a:startLnum . 'foldclose'
        endif

        return [l:isDone, l:nestLevelMax + 1]
    endif
endfunction
function! s:FoldOutline( count )
    let l:save_view = winsaveview()
    try
        call cursor(1, 0)
        keepjumps normal! zM
        call s:FoldOutlineRecurse(a:count, 1, line('$'))
    catch /^Vim\%((\a\+)\)\=:E490:/ " E490: No fold found
        " Ignore, like zr, zm, ...
    finally
        call winrestview(l:save_view)
    endtry
endfunction
nnoremap <silent> zy :<C-u>call <SID>FoldOutline(v:count1)<CR>