2012-01-06 33 views

回答

1

最可能的是,你可以创建一个函数来检查文件是否有任何褶皱,如:

function HasFoldedLine() 
    let lnum=1 
    while lnum <= line("$") 
     if (foldclosed(lnum) > -1) 
      return 1 
     endif 
     let lnum+=1 
    endwhile 
    return 0 
endfu 

现在你可以用一些autocommand使用它,例如:

au CursorHold * if HasFoldedLine() == 1 | set fdc=1 | else |set fdc=0 | endif 

HTH

+0

这是相当缓慢的。 – 2013-11-24 10:53:14

+0

你是对的,现在向我们展示一个更好的解决方案。 – 2013-11-24 14:18:41

+0

我打算,后来:) – 2013-11-24 15:55:15

4

当文件变得足够大时,我的方法比@Zsolt Botykai更快。对于小文件,我可以想象时间差异是微不足道的。该函数不是检查每一行的折叠,而是简单地尝试在折叠之间移动。如果光标从不移动,则没有折叠。

function HasFolds() 
    "Attempt to move between folds, checking line numbers to see if it worked. 
    "If it did, there are folds. 

    function! HasFoldsInner() 
     let origline=line('.') 
     :norm zk 
     if origline==line('.') 
      :norm zj 
      if origline==line('.') 
       return 0 
      else 
       return 1 
      endif 
     else 
      return 1 
     endif 
     return 0 
    endfunction 

    let l:winview=winsaveview() "save window and cursor position 
    let foldsexist=HasFoldsInner() 
    if foldsexist 
     set foldcolumn=1 
    else 
     "Move to the end of the current fold and check again in case the 
     "cursor was on the sole fold in the file when we checked 
     if line('.')!=1 
      :norm [z 
      :norm k 
     else 
      :norm ]z 
      :norm j 
     endif 
     let foldsexist=HasFoldsInner() 
     if foldsexist 
      set foldcolumn=1 
     else 
      set foldcolumn=0 
     endif 
    end 
    call winrestview(l:winview) "restore window/cursor position 
endfunction 

au CursorHold,BufWinEnter ?* call HasFolds() 
0

(无耻的自我插件

我创建了一个插件来做到这一点叫Auto Origami,仿照@ SnoringFrog的answer

删除下面的例子到你的vimrc安装它,看魔术发生后(读:help auto-origami找出如何微调吧):

augroup autofoldcolumn 
    au! 

    " Or whatever autocmd-events you want 
    au CursorHold,BufWinEnter * let &foldcolumn = auto_origami#Foldcolumn() 
augroup END