0

我已经将 Vim 配置为如下所示:

+------------------------------------+
| N  |              Code             |
| E  |                               |
| R  |                               |
| D  |                               |
| T  |                               |
| r  |-------------------------------|
| e  |              REPL             |
| e  |                               |
+------------------------------------+

每个元素都在它自己的缓冲区中。只有在我退出代码缓冲区时才可以删除 REPL 缓冲区吗?例如,如果我打开和关闭帮助,REPL 应该保留,但是当我关闭代码缓冲区时,REPL 缓冲区也应该关闭。
这就是我现在所拥有的:

let s:code_repl_nrs = {} " A map of (code buffer number) - (REPL buffer number)
let s:last_leaved_buffer_nr = -1 " The number of the last leaved buffer

autocmd BufLeave,BufDelete * let s:last_leaved_buffer_nr = bufnr('%')
autocmd BufUnload * call <SID>StopREPL(s:last_leaved_buffer_nr) " Stop the REPL when closing the code buffer

"" Stops a REPL given the number of the current buffer
function! s:StopREPL(buffer_nr)
    let l:repl_nr = get(s:code_repl_nrs, a:buffer_nr . '', -1) " Get the number of the REPL buffer
    if l:repl_nr != -1
        exec l:repl_nr . 'bdelete!'
    endif
endfunction

当我使用:h时,REPL 关闭。我怎样才能防止这种情况?

4

1 回答 1

0

原来<abuf>是答案,替换s:last_leaved_buffer_nr

autocmd BufUnload * call <SID>StopRepl(expand('<abuf>'))

根据:h <abuf>

执行自动命令时,用当前有效的缓冲区号替换(对于 ":r file" 和 ":so file",它是当前缓冲区,正在读取/获取的文件不在缓冲区中)。

结果<abuf>被扩展为当前正在卸载的缓冲区的缓冲区号。当代码缓冲区正在卸载时,缓冲区号将在s:code_repl_nrs并且s:StopREPL能够删除 REPL 缓冲区。当其他一些缓冲区正在卸载时,s:StopREPL将无法删除 REPL 缓冲区。

于 2021-08-04T21:39:30.420 回答