From 5d5b8e3e7da3a094bf033ba355dcd54f0cb7cbc0 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 8 Aug 2026 09:30:03 +0800 Subject: [PATCH] vim-patch:9.2.0923: tabpage: closing a tab page loses the alternate tab page (#41229) Problem: Closing the current tab page resets the alternate tab page, even when that is another tab page which still exists, so that CTRL-Tab stops working (igorlfs). Solution: Restore the last used tab page after entering another one to close the current one (Hirohito Higashi). related: vim/vim#20965 closes: vim/vim#20973 https://github.com/vim/vim/commit/a05bd64c1dcde65006997efe0ff5ae6b8a9aa382 Co-authored-by: Hirohito Higashi Co-authored-by: Claude Opus 5 (1M context) --- src/nvim/window.c | 7 +++++++ test/old/testdir/test_tabpage.vim | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/nvim/window.c b/src/nvim/window.c index d0531e6589..c1b137890a 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -2684,6 +2684,7 @@ static bool close_last_window_tabpage(win_T *win, bool free_buf, tabpage_T *prev } buf_T *old_curbuf = curbuf; + tabpage_T *save_lastused = lastused_tabpage; Terminal *term = win->w_buffer ? win->w_buffer->terminal : NULL; if (term) { @@ -2706,6 +2707,12 @@ static bool close_last_window_tabpage(win_T *win, bool free_buf, tabpage_T *prev if (curtab != prev_curtab && valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win) { win_close_othertab(win, free_buf, prev_curtab, false); } + + // Entering the other tab page made the closed one the last used tab page. + // Restore the previous one when it is still there. + if (valid_tabpage(save_lastused) && save_lastused != curtab) { + lastused_tabpage = save_lastused; + } entering_window(curwin); // Since goto_tabpage_tp above did not trigger *Enter autocommands, do diff --git a/test/old/testdir/test_tabpage.vim b/test/old/testdir/test_tabpage.vim index 89da59fe46..3465bc7a35 100644 --- a/test/old/testdir/test_tabpage.vim +++ b/test/old/testdir/test_tabpage.vim @@ -945,6 +945,23 @@ func Test_lastused_tabpage() tabonly! endfunc +" Closing a tab page must keep the last used tab page when it is another one. +func Test_lastused_tabpage_close() + tabonly! + tabnew + tabnew + tabfirst + tablast + call assert_equal(1, tabpagenr('#')) + + tabclose + call assert_equal(1, tabpagenr('#')) + call feedkeys("g\", "xt") + call assert_equal(1, tabpagenr()) + + tabonly! +endfunc + " Test for tabpage allocation failure func Test_tabpage_alloc_failure() CheckFunction test_alloc_fail