3

我想执行MyCommand需要访问b:somevar<afile>. 现在我正在做类似的事情

function F()
    let l:a = bufnr(expand("%"))
    let l:b = bufnr(expand("<afile>"))
    execute "bufdo call G(" . l:b . ")"
    execute "buffer " . a
endfunction

function G(d)
    let l:a = bufnr(expand("%"))
    if l:a == a:d
        execute 'MyCommand'
    endif
endfunction

autocmd BufDelete *.hs :call F()

因此,F()检查每个加载的缓冲区是否是<afile>. 它有效,但感觉相当疯狂,应该有更好的方法。

4

1 回答 1

2

MyCommand只需要访问b:somevar(可能是通过 缓冲区内容getbufline())时,它可以使用getbufvar(expand('<abuf>'), 'somevar').


另一方面,如果它需要直接对缓冲区执行命令,则需要在窗口中临时显示缓冲区,如下所示:

function! ExecuteInVisibleBuffer( bufnr, command )
    let l:winnr = bufwinnr(a:bufnr)
    if l:winnr == -1
        " The buffer is hidden. Make it visible to execute the passed function.
        let l:originalWindowLayout = winrestcmd()
            execute 'noautocmd silent keepalt leftabove sbuffer' a:bufnr
        try
            execute a:command
        finally
            noautocmd silent close
            silent! execute l:originalWindowLayout
        endtry
    else
        " The buffer is visible in at least one window on this tab page.
        let l:currentWinNr = winnr()
        execute l:winnr . 'wincmd w'
        try
            execute a:command
        finally
            execute l:currentWinNr . 'wincmd w'
        endtry
    endif
endfunction
于 2013-01-22T09:06:09.627 回答