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

a05bd64c1d

Co-authored-by: Hirohito Higashi <h.east.727@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
zeertzjq
2026-08-08 09:30:03 +08:00
committed by GitHub
parent 318d6a7f59
commit 5d5b8e3e7d
2 changed files with 24 additions and 0 deletions

View File

@@ -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

View File

@@ -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\<Tab>", "xt")
call assert_equal(1, tabpagenr())
tabonly!
endfunc
" Test for tabpage allocation failure
func Test_tabpage_alloc_failure()
CheckFunction test_alloc_fail