如何检测Vim缓冲区是否包含折叠?

时间:2012-03-29 17:08:42

标签: vim folding

寻找建议如何以编程方式检测,如果当前Vim的缓冲区包含至少一个定义的折叠?无论折叠是打开还是关闭。

如果当前缓冲区中定义了折叠,则尝试仅调用mkview

autocmd BufWrite ?* if fold_defined() | mkview | endif

function fold_defined()
  ???
endfunction

3 个答案:

答案 0 :(得分:2)

function! HasFold()
    let v = winsaveview()
    let fold = 0
    for mv in ['zj', 'zk']
        exe 'keepj norm!' mv
        if foldlevel('.') > 0
            let fold = 1
            break
        endif
    endfor
    call winrestview(v)
    return fold
endfunction

答案 1 :(得分:0)

基于perreal's advice,我确实为我的问题写了一个可能的解决方案:

" Detect presence of fold definition in the current buffer
function FoldDefined()
  let result = 0
  let save_cursor = getpos('.')
  call cursor(1,1)
  let scanline = line('.') 
  let lastline = line('$')

  while scanline <= lastline
    if foldlevel(scanline) > 0
      let result = 1
      break
    endif
    let scanline = scanline + 1
  endwhile

  call setpos('.', save_cursor)

  return result
endfunction

答案 2 :(得分:0)

function! FoldDefined()
   return len(filter(range(1, line('$')), 'foldlevel(v:val)>1'))>0
endfunction