开发者

Finding the first line of the current Vim fold

I am writing a Vimscript function in which I need to find the line number of the first line of the current fold. So far I have been using this:

function! GetFoldStart()
    let l:current_line=line('.')
    normal [z
    let l:current_fold_start=line('.')
    execute 'normal ' . l:current_line . 'g'
endfunction

Which works fine, but seems unnecessarily expensive. Is there a cheaper way of achieving it? Basically I need to borrow the functionality of the [z command, without actually moving to the line.

I was hoping that the variable v:foldstart would come to my rescue, but it seems only to 开发者_JAVA百科work correctly for closed folds (for use in foldtext).

Thanks in advance for your Vim wisdom! Jonathan.


First, use normal! (with bang) in scripts, it is safer. Second,

let winview=winsaveview()
try
    " Your code here "
finally
    call winrestview(winview)
endtry

holds more cases then let l:current_line=line('.') ... execute "normal! ".l:current_line."gg".

I don't know a way to obtain information you want without normal! [z, but the following code should not modify jumplist:

function! GetFoldStart()
    let winview=winsaveview()
    try
        keepjumps normal! [z
        return line(".")
    finally
        keepjumps call winrestview(winview)
    endtry
endfunction
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜