0

I've configured Vim to look like this:

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

Every element is in it's own buffer. Is it possible to delete the REPL buffer only when I quit the code buffer? If I open and close help, for example, the REPL should stay but when I close the code buffer, the REPL buffer should close as well.
This is what I have now:

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

When I use :h, the REPL closes. How can I prevent this?

Loading BG
  • 131
  • 2
  • 9

1 Answers1

0

Turns out <abuf> is the answer, replacing s:last_leaved_buffer_nr:

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

As per :h <abuf>:

When executing autocommands, is replaced with the currently effective buffer number (for ":r file" and ":so file" it is the current buffer, the file being read/sourced is not in a buffer).

Turns out <abuf> gets expanded to the buffer number of the buffer that is currently unloading. When the code buffer is unloading, the buffer number will be in s:code_repl_nrs and s:StopREPL will be able to delete the REPL buffer. When some other buffer is unloading, s:StopREPL will not be able to delete the REPL buffer.

Loading BG
  • 131
  • 2
  • 9