2016-09-13 4 views
0

Как я могу развернуть только складки, содержащие складку, чтобы получить контур моего документа? Если все складывается, и я несколько раз нажимаю zr, я получаю что-то близкое к тому, что хочу, за исключением того, что если части имеют разную глубину, я либо не вижу никаких складок, либо вижу какой-то контент.Содержание с Fold in 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>