vim - 自动展开小褶皱

标签 vim

使用foldmethod=syntax时有没有办法自动展开小折叠(<10行)?

我知道会有使用的选项

set foldminlines=10

但是如果我使用此设置,即使我稍后想折叠这些部分也无法折叠。

编辑

感谢Ingo Karkat,我的配置现在可以实现我想要的功能。

au BufReadPre * if !exists('b:folddict') | let b:folddict = {} | endif
function! s:UnfoldSmallFolds( count )
  if empty(b:folddict)
    "fill list
    let l:_none = s:checkInnerFolds(['1',line('$')], a:count)
  else
    "folddict should be filled by now
    " check if lnum is in fold
    let l:index = s:checkFoldIndex(keys(b:folddict), getpos('.')[1])
    if l:index != 0
      "check if open -> close
      if b:folddict[l:index] == 1
        foldclose
        let b:folddict[l:index] = 0
      else
        foldopen
        let b:folddict[l:index] = 1
        let l:_none = s:checkInnerFolds(split(l:index,'-'),a:count)
      endif
    endif
  endif
endfunction

function! s:checkInnerFolds(index,count)
  let l:lnum = a:index[0]
  while l:lnum <= a:index[1]
    if foldclosed(l:lnum) == -1
      let l:lnum += 1
      continue
    endif

    let l:endLnum = foldclosedend(l:lnum)
    let l:innerIndex = l:lnum."-".l:endLnum
    if has_key(b:folddict,l:innerIndex)
      if b:folddict[l:innerIndex] == 1
        foldopen
        let l:_none = s:checkInnerFolds(l:innerIndex,a:count)
      endif
    else                        
      let b:folddict[l:innerIndex] = 0
      if l:endLnum - l:lnum < a:count
        execute printf('%d,%dfoldopen!', l:lnum, l:endLnum)
        let b:folddict[l:innerIndex] = 1
        let l:_none = s:checkInnerFolds(l:innerIndex,a:count)
      endif
    endif
    let l:lnum = l:endLnum + 1
  endwhile
endfunction


function! s:checkFoldIndex(folds, pos)
  let l:retLine = ['0','0']
  for line in a:folds
    let l:splitLine = split(line,'-')
    if a:pos >= l:splitLine[0] && a:pos <= l:splitLine[1]
      if a:pos-l:splitLine[0] < a:pos-l:retLine[0]
        let l:retLine = l:splitLine
      endif
    endif
  endfor
  return join(l:retLine,"-")
endfunction 

最佳答案

这可以通过自定义函数来完成,该函数检查所有关闭的折叠并打开那些只有几行的折叠,如下所示:

" [count]z<r        Open all small folds that contain up to 3 / [count] lines.
function! s:UnfoldSmallFolds( count )
    let l:openCnt = 0
    let l:lnum = 1
    while l:lnum <= line('$')
        if foldclosed(l:lnum) == -1
            let l:lnum += 1
            continue
        endif

        let l:endLnum = foldclosedend(l:lnum)
        if l:endLnum - l:lnum < a:count
            execute printf('%d,%dfoldopen!', l:lnum, l:endLnum)
            let l:openCnt += 1
        endif
        let l:lnum = l:endLnum + 1
    endwhile

    if l:openCnt == 0
        echo printf('No small folds over up to %d lines found', a:count)
    else
        echo printf('%d folds opened', l:openCnt)
    endif
endfunction
nnoremap <silent> z<r :<C-u>call <SID>UnfoldSmallFolds(v:count ? v:count : 3)<CR>

关于vim - 自动展开小褶皱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15774524/

相关文章:

vim - 在终端中禁用 "Press ENTER or type command to continue"

ruby - 如何自动将 "end"附加到 vim 中的 ruby​​ 代码块?

vim - 如何在vim中否定一个glob?

Vim 在特定数量的分隔符后插入

bash - bash/cli 中的视觉选择

vim - 通常,我如何在 VIM 中使用 "go to definition"?那我怎么用golang呢?

python - 如何在 VIM 中设置文件的正确路径?

qt vim 不在 kubuntu 上构建

python - 如何在不锁定 Vim 的情况下运行当前的 python 脚本?

path - 脚本 : switch to buffer by filename path