【问题标题】:Table of contents with Fold in VimVim 中的折叠目录
【发布时间】:2016-09-13 21:26:59
【问题描述】:

如何仅展开包含折叠的折叠以获得文档的轮廓? 如果所有东西都折叠了,我按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

【问题讨论】:

  • 你能给我们举一个你的情况的例子
  • 我编辑了我的问题。

标签: vim fold folding


【解决方案1】:

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

" [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>

【讨论】:

    猜你喜欢
    • 2012-12-31
    • 2011-07-01
    • 1970-01-01
    • 2011-03-13
    • 2012-06-13
    • 2013-01-16
    • 2011-01-22
    • 1970-01-01
    • 2011-04-25
    相关资源
    最近更新 更多