2016-09-13 95 views
0

如何展开只包含折叠的折叠以获取我的文档的轮廓? 如果一切都折叠了,我几次按下zr,我会得到与我想要的东西接近的东西,除非如果零件有不同的深度,我要么没有看到一些折叠或看到一些内容。目录在Vim中折叠

在这个例子中:

# 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 
+0

你能举一个我们的例子吗 –

+0

我编辑了我的问题。 – mrtnmgs

回答

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>