Skip to content

Commit 6da9f75

Browse files
zeertzjqchrisbra
authored andcommitted
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 <zeertzjq@outlook.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 7ccb81b commit 6da9f75

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

runtime/doc/version9.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*version9.txt* For Vim version 9.1. Last change: 2026 Feb 06
1+
*version9.txt* For Vim version 9.1. Last change: 2026 Feb 07
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -52502,4 +52502,9 @@ Problem: search() is used to check for the message from tar that
5250252502
src/testdir/samples/evil.tar.
5250352503
Solution: Use the 'w' flag for search() (Kevin Goodsell)
5250452504

52505+
Patch 9.1.2136
52506+
Problem: :tab sbuffer may close old tabpage if BufLeave autocommand
52507+
splits window (after 9.1.0143).
52508+
Solution: Only close other windows if the buffer will be unloaded (zeertzjq).
52509+
5250552510
vim:tw=78:ts=8:noet:ft=help:norl:fdm=manual:nofoldenable

src/buffer.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,7 @@ set_curbuf(buf_T *buf, int action)
18861886
prevbuf = curbuf;
18871887
set_bufref(&prevbufref, prevbuf);
18881888
set_bufref(&newbufref, buf);
1889+
int prev_nwindows = prevbuf->b_nwindows;
18891890

18901891
// Autocommands may delete the current buffer and/or the buffer we want to
18911892
// go to. In those cases don't close the buffer.
@@ -1904,8 +1905,8 @@ set_curbuf(buf_T *buf, int action)
19041905
// autocommands may have opened a new window
19051906
// with prevbuf, grr
19061907
if (unload ||
1907-
(last_winid != get_last_winid() &&
1908-
strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
1908+
(prev_nwindows <= 1 && last_winid != get_last_winid()
1909+
&& action == DOBUF_GOTO && !buf_hide(prevbuf)))
19091910
close_windows(prevbuf, FALSE);
19101911
#if defined(FEAT_EVAL)
19111912
if (bufref_valid(&prevbufref) && !aborting())

src/testdir/test_autocmd.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4757,7 +4757,7 @@ func Test_autocmd_invalidates_undo_on_textchanged()
47574757
call StopVimInTerminal(buf)
47584758
endfunc
47594759

4760-
func Test_autocmd_creates_new_buffer_on_bufleave()
4760+
func Test_autocmd_creates_new_window_on_bufleave()
47614761
e a.txt
47624762
e b.txt
47634763
setlocal bufhidden=wipe

src/testdir/test_buffer.vim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,4 +880,37 @@ func Test_bdelete_skip_closing_bufs()
880880
%bw!
881881
endfunc
882882

883+
func Test_split_window_in_BufLeave_from_tab_sbuffer()
884+
tabnew Xa
885+
setlocal bufhidden=wipe
886+
let t0 = tabpagenr()
887+
let b0 = bufnr()
888+
let b1 = bufadd('Xb')
889+
autocmd BufLeave Xa ++once split
890+
exe 'tab sbuffer' b1
891+
call assert_equal(t0 + 1, tabpagenr())
892+
call assert_equal([b1, b0], tabpagebuflist())
893+
call assert_equal([b0], tabpagebuflist(t0))
894+
tabclose
895+
call assert_equal(t0, tabpagenr())
896+
call assert_equal([b0], tabpagebuflist())
897+
898+
bwipe! Xa
899+
bwipe! Xb
900+
endfunc
901+
902+
func Test_split_window_in_BufLeave_from_switching_buffer()
903+
tabnew Xa
904+
setlocal bufhidden=wipe
905+
split
906+
let b0 = bufnr()
907+
let b1 = bufadd('Xb')
908+
autocmd BufLeave Xa ++once split
909+
exe 'buffer' b1
910+
call assert_equal([b1, b0, b0], tabpagebuflist())
911+
912+
bwipe! Xa
913+
bwipe! Xb
914+
endfunc
915+
883916
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ static char *(features[]) =
734734

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
2136,
737739
/**/
738740
2135,
739741
/**/

0 commit comments

Comments
 (0)