From 6da9f757c48ce87df381d726b165bed6fa301423 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 7 Feb 2026 10:20:51 +0000 Subject: [PATCH] patch 9.1.2136: :tab sbuffer may close old tabpage Problem: :tab sbuffer may close old tabpage if BufLeave autocommand splits window (after 9.1.0143). Solution: Only close other windows if the buffer will be unloaded (zeertzjq). related: neovim/neovim#37749 closes: #19352 Signed-off-by: zeertzjq Signed-off-by: Christian Brabandt --- runtime/doc/version9.txt | 7 ++++++- src/buffer.c | 5 +++-- src/testdir/test_autocmd.vim | 2 +- src/testdir/test_buffer.vim | 33 +++++++++++++++++++++++++++++++++ src/version.c | 2 ++ 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/runtime/doc/version9.txt b/runtime/doc/version9.txt index 05d4da2be3..3dd3754b18 100644 --- a/runtime/doc/version9.txt +++ b/runtime/doc/version9.txt @@ -1,4 +1,4 @@ -*version9.txt* For Vim version 9.1. Last change: 2026 Feb 06 +*version9.txt* For Vim version 9.1. Last change: 2026 Feb 07 VIM REFERENCE MANUAL by Bram Moolenaar @@ -52502,4 +52502,9 @@ Problem: search() is used to check for the message from tar that src/testdir/samples/evil.tar. Solution: Use the 'w' flag for search() (Kevin Goodsell) +Patch 9.1.2136 +Problem: :tab sbuffer may close old tabpage if BufLeave autocommand + splits window (after 9.1.0143). +Solution: Only close other windows if the buffer will be unloaded (zeertzjq). + vim:tw=78:ts=8:noet:ft=help:norl:fdm=manual:nofoldenable diff --git a/src/buffer.c b/src/buffer.c index 86b7840230..8a5d883e78 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1886,6 +1886,7 @@ set_curbuf(buf_T *buf, int action) prevbuf = curbuf; set_bufref(&prevbufref, prevbuf); set_bufref(&newbufref, buf); + int prev_nwindows = prevbuf->b_nwindows; // Autocommands may delete the current buffer and/or the buffer we want to // go to. In those cases don't close the buffer. @@ -1904,8 +1905,8 @@ set_curbuf(buf_T *buf, int action) // autocommands may have opened a new window // with prevbuf, grr if (unload || - (last_winid != get_last_winid() && - strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL)) + (prev_nwindows <= 1 && last_winid != get_last_winid() + && action == DOBUF_GOTO && !buf_hide(prevbuf))) close_windows(prevbuf, FALSE); #if defined(FEAT_EVAL) if (bufref_valid(&prevbufref) && !aborting()) diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim index 82d62e7636..74b1aabdf3 100644 --- a/src/testdir/test_autocmd.vim +++ b/src/testdir/test_autocmd.vim @@ -4757,7 +4757,7 @@ func Test_autocmd_invalidates_undo_on_textchanged() call StopVimInTerminal(buf) endfunc -func Test_autocmd_creates_new_buffer_on_bufleave() +func Test_autocmd_creates_new_window_on_bufleave() e a.txt e b.txt setlocal bufhidden=wipe diff --git a/src/testdir/test_buffer.vim b/src/testdir/test_buffer.vim index 4ededa15d4..934bc6fe1e 100644 --- a/src/testdir/test_buffer.vim +++ b/src/testdir/test_buffer.vim @@ -880,4 +880,37 @@ func Test_bdelete_skip_closing_bufs() %bw! endfunc +func Test_split_window_in_BufLeave_from_tab_sbuffer() + tabnew Xa + setlocal bufhidden=wipe + let t0 = tabpagenr() + let b0 = bufnr() + let b1 = bufadd('Xb') + autocmd BufLeave Xa ++once split + exe 'tab sbuffer' b1 + call assert_equal(t0 + 1, tabpagenr()) + call assert_equal([b1, b0], tabpagebuflist()) + call assert_equal([b0], tabpagebuflist(t0)) + tabclose + call assert_equal(t0, tabpagenr()) + call assert_equal([b0], tabpagebuflist()) + + bwipe! Xa + bwipe! Xb +endfunc + +func Test_split_window_in_BufLeave_from_switching_buffer() + tabnew Xa + setlocal bufhidden=wipe + split + let b0 = bufnr() + let b1 = bufadd('Xb') + autocmd BufLeave Xa ++once split + exe 'buffer' b1 + call assert_equal([b1, b0, b0], tabpagebuflist()) + + bwipe! Xa + bwipe! Xb +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/version.c b/src/version.c index 0adf730aff..8721de4496 100644 --- a/src/version.c +++ b/src/version.c @@ -734,6 +734,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 2136, /**/ 2135, /**/ -- 2.47.3