[英]gvim automatic show foldcolumn when there are folds in a file
I know you can use 我知道你可以用
set foldcolumn=1
to enable fold column 启用折叠列
but is there a way to automatic turn it on only when there are folds exist in the file? 但有没有办法只在文件中存在折叠时自动打开它?
My method is faster than @Zsolt Botykai's when files get large enough. 当文件变得足够大时,我的方法比@Zsolt Botykai更快。 For small files I'd imagine the time difference is insignificant.
对于小文件,我认为时差是无关紧要的。 Instead of checking every line for a fold, the function simply tries to move between folds.
该函数不是检查每一行的折叠,而只是尝试在折叠之间移动。 If the cursor never moves, there are no folds.
如果光标从不移动,则没有折叠。
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()
(Shameless self plugin ) (无耻的自我插件 )
I created a plugin to do this called Auto Origami , modeled on @SnoringFrog's answer . 我创建了一个名为Auto Origami的插件,模仿@ SnoringFrog的答案 。
Drop the following example into your vimrc after installing it to see the magic happen (and read :help auto-origami
to find out how to fine-tune it): 安装后将以下示例放入vimrc中以查看魔术发生(并阅读
:help auto-origami
以了解如何对其进行微调):
augroup autofoldcolumn
au!
" Or whatever autocmd-events you want
au CursorHold,BufWinEnter * AutoOrigamiFoldColumn
augroup END
Most probably you can create a function to check if the file has any folds, like: 很可能你可以创建一个函数来检查文件是否有任何折叠,例如:
function HasFoldedLine()
let lnum=1
while lnum <= line("$")
if (foldclosed(lnum) > -1)
return 1
endif
let lnum+=1
endwhile
return 0
endfu
Now you can use it with some autocommand
, eg: 现在您可以将它与一些
autocommand
一起使用,例如:
au CursorHold * if HasFoldedLine() == 1 | set fdc=1 | else |set fdc=0 | endif
HTH HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.