diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index aec0edbbf5..b6ee21b073 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -698,14 +698,11 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i } // Remove the buffer from the list. - if (wipe_buf) { + // Do not wipe out the buffer if it is used in a window. + if (wipe_buf && buf->b_nwindows <= 0) { if (clear_w_buf) { win->w_buffer = NULL; } - // Do not wipe out the buffer if it is used in a window. - if (buf->b_nwindows > 0) { - return true; - } FOR_ALL_TAB_WINDOWS(tp, wp) { mark_forget_file(wp, buf->b_fnum); } diff --git a/test/old/testdir/test_autocmd.vim b/test/old/testdir/test_autocmd.vim index 92f034c31b..5d2d12d2a5 100644 --- a/test/old/testdir/test_autocmd.vim +++ b/test/old/testdir/test_autocmd.vim @@ -3376,7 +3376,7 @@ func Test_BufReadPre_changebuf() close! endfunc -" Test for BufWipeouti autocmd changing the current buffer when reading a file +" Test for BufWipeout autocmd changing the current buffer when reading a file " in an empty buffer with 'f' flag in 'cpo' func Test_BufDelete_changebuf() new @@ -3662,6 +3662,29 @@ func Test_bufwipeout_changes_window() %bwipe! endfunc +func Test_autocmd_prevent_buf_wipe() + " Xa must be the first buffer so that win_close_othertab() puts it in + " another window, which causes wiping the buffer to fail. + %bwipe! + + file Xa + call setline(1, 'foo') + setlocal bufhidden=wipe + tabnew Xb + setlocal bufhidden=wipe + autocmd BufUnload Xa ++once ++nested tabonly + autocmd BufWinLeave Xb ++once tabnext + tabfirst + + edit! Xc + call assert_equal('Xc', bufname('%')) + tabnext + call assert_equal('Xa', bufname('%')) + call assert_equal("\n\"Xa\" --No lines in buffer--", execute('file')) + + %bwipe! +endfunc + func Test_v_event_readonly() autocmd CompleteChanged * let v:event.width = 0 call assert_fails("normal! i\\", 'E46:')