开发者

Finding the first line of the current Vim fold

开发者 https://www.devze.com 2023-02-06 12:57 出处:网络
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:

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

精彩评论

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