vim-patch:8.2.1401: cannot jump to the last used tabpage

Problem:    Cannot jump to the last used tabpage.
Solution:   Add g<Tab> and tabpagnr('#'). (Yegappan Lakshmanan, closes vim/vim#6661,
            neovim #11626)
62a232506d

Nvim implemented this feature before Vim, but Vim made some useful changes (e.g:
beeping on failure). Port the changes to closer match Vim (also makes porting
future patches easier).

Also note that because CHECK_CMDWIN was added to goto_tabpage_tp, there is no
need to do the extra work with tabpage_index and goto_tabpage inside
goto_tabpage_lastused to fix cmdwin issues any more (#11692).
Note that while goto_tabpage_tp doesn't check for textlock like goto_tabpage
does, it shouldn't matter as it is already checked for earlier.

Add tags for <C-Tab> to tabpage.txt, and refer to <C-Tab> over CTRL-Tab to be
consistent with other docs like the patch.
Remove mention of "previous tabpage" (it can be confused with the tabpage to the
left, e.g: `:tabprevious`).
Similarly, don't rename old_curtab to last_tab in enter_tabpage (it might be
confused with the right-most tabpage, e.g: `:tablast`).

Cherry-pick Test_tabpage change from v8.2.0634.
92b83ccfda
This commit is contained in:
Sean Dewar 2022-03-14 11:37:44 +00:00
parent 7e19c18a54
commit c5f190e0c2
No known key found for this signature in database
GPG Key ID: 08CC2C83AD41B581
8 changed files with 80 additions and 28 deletions

View File

@ -8103,15 +8103,15 @@ tabpagebuflist([{arg}]) *tabpagebuflist()*
tabpagenr([{arg}]) *tabpagenr()* tabpagenr([{arg}]) *tabpagenr()*
The result is a Number, which is the number of the current The result is a Number, which is the number of the current
tab page. The first tab page has number 1. tab page. The first tab page has number 1.
The optional argument {arg} supports the following values: The optional argument {arg} supports the following values:
$ the number of the last tab page (the tab page $ the number of the last tab page (the tab page
count). count).
# the number of the last accessed tab page (where # the number of the last accessed tab page
|g<Tab>| goes to). If there is no previous (where |g<Tab>| goes to). If there is no
tab page, 0 is returned. previous tab page, 0 is returned.
The number can be used with the |:tab| command. The number can be used with the |:tab| command.
tabpagewinnr({tabarg} [, {arg}]) *tabpagewinnr()* tabpagewinnr({tabarg} [, {arg}]) *tabpagewinnr()*
Like |winnr()| but for tab page {tabarg}. Like |winnr()| but for tab page {tabarg}.
{tabarg} specifies the number of tab page to be used. {tabarg} specifies the number of tab page to be used.

View File

@ -431,6 +431,7 @@ tag char note action in Normal mode ~
|<C-LeftMouse>| <C-LeftMouse> ":ta" to the keyword at the mouse click |<C-LeftMouse>| <C-LeftMouse> ":ta" to the keyword at the mouse click
|<C-Right>| <C-Right> 1 same as "w" |<C-Right>| <C-Right> 1 same as "w"
|<C-RightMouse>| <C-RightMouse> same as "CTRL-T" |<C-RightMouse>| <C-RightMouse> same as "CTRL-T"
|<C-Tab>| <C-Tab> same as "g<Tab>"
|<Del>| ["x]<Del> 2 same as "x" |<Del>| ["x]<Del> 2 same as "x"
|N<Del>| {count}<Del> remove the last digit from {count} |N<Del>| {count}<Del> remove the last digit from {count}
|<Down>| <Down> 1 same as "j" |<Down>| <Down> 1 same as "j"
@ -577,7 +578,7 @@ tag command action in Normal mode ~
following the file name. following the file name.
|CTRL-W_gt| CTRL-W g t same as `gt`: go to next tab page |CTRL-W_gt| CTRL-W g t same as `gt`: go to next tab page
|CTRL-W_gT| CTRL-W g T same as `gT`: go to previous tab page |CTRL-W_gT| CTRL-W g T same as `gT`: go to previous tab page
|CTRL-W_g<Tab>| CTRL-W g <Tab> same as `g<Tab>` : go to last accessed tab |CTRL-W_g<Tab>| CTRL-W g <Tab> same as |g<Tab>|: go to last accessed tab
page page
|CTRL-W_h| CTRL-W h go to Nth left window (stop at first window) |CTRL-W_h| CTRL-W h go to Nth left window (stop at first window)
|CTRL-W_i| CTRL-W i split window and jump to declaration of |CTRL-W_i| CTRL-W i split window and jump to declaration of

View File

@ -196,10 +196,6 @@ gt *i_CTRL-<PageDown>* *i_<C-PageDown>*
{count}<C-PageDown> {count}<C-PageDown>
{count}gt Go to tab page {count}. The first tab page has number one. {count}gt Go to tab page {count}. The first tab page has number one.
CTRL-<Tab> *CTRL-<Tab>*
CTRL-W g<Tab> *g<Tab>* *CTRL-W_g<Tab>*
g<Tab> Go to previous (last accessed) tab page.
:tabp[revious] *:tabp* *:tabprevious* *gT* *:tabN* :tabp[revious] *:tabp* *:tabprevious* *gT* *:tabN*
:tabN[ext] *:tabNext* *CTRL-<PageUp>* :tabN[ext] *:tabNext* *CTRL-<PageUp>*
<C-PageUp> *<C-PageUp>* *i_CTRL-<PageUp>* *i_<C-PageUp>* <C-PageUp> *<C-PageUp>* *i_CTRL-<PageUp>* *i_<C-PageUp>*
@ -219,6 +215,9 @@ gT Go to the previous tab page. Wraps around from the first one
*:tabl* *:tablast* *:tabl* *:tablast*
:tabl[ast] Go to the last tab page. :tabl[ast] Go to the last tab page.
<C-Tab> *CTRL-<Tab>* *<C-Tab>*
CTRL-W g<Tab> *g<Tab>* *CTRL-W_g<Tab>*
g<Tab> Go to the last accessed tab page.
Other commands: Other commands:
*:tabs* *:tabs*

View File

@ -11213,9 +11213,7 @@ static void f_tabpagenr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
if (strcmp(arg, "$") == 0) { if (strcmp(arg, "$") == 0) {
nr = tabpage_index(NULL) - 1; nr = tabpage_index(NULL) - 1;
} else if (strcmp(arg, "#") == 0) { } else if (strcmp(arg, "#") == 0) {
nr = valid_tabpage(lastused_tabpage) nr = valid_tabpage(lastused_tabpage) ? tabpage_index(lastused_tabpage) : 0;
? tabpage_index(lastused_tabpage)
: nr;
} else { } else {
semsg(_(e_invexpr2), arg); semsg(_(e_invexpr2), arg);
} }

View File

@ -448,10 +448,11 @@ EXTERN int aucmd_win_used INIT(= false); // aucmd_win is being used
EXTERN frame_T *topframe; // top of the window frame tree EXTERN frame_T *topframe; // top of the window frame tree
// Tab pages are alternative topframes. "first_tabpage" points to the first // Tab pages are alternative topframes. "first_tabpage" points to the first
// one in the list, "curtab" is the current one. // one in the list, "curtab" is the current one. "lastused_tabpage" is the
// last used one.
EXTERN tabpage_T *first_tabpage; EXTERN tabpage_T *first_tabpage;
EXTERN tabpage_T *lastused_tabpage;
EXTERN tabpage_T *curtab; EXTERN tabpage_T *curtab;
EXTERN tabpage_T *lastused_tabpage;
EXTERN bool redraw_tabline INIT(= false); // need to redraw tabline EXTERN bool redraw_tabline INIT(= false); // need to redraw tabline
// Iterates over all tabs in the tab list // Iterates over all tabs in the tab list

View File

@ -5869,7 +5869,7 @@ static void nv_gomark(cmdarg_T *cap)
} }
} }
// Handle CTRL-O, CTRL-I, "g;", "g,", and "CTRL-Tab" commands. /// Handle CTRL-O, CTRL-I, "g;", "g,", and "CTRL-Tab" commands.
static void nv_pcmark(cmdarg_T *cap) static void nv_pcmark(cmdarg_T *cap)
{ {
pos_T *pos; pos_T *pos;
@ -5878,7 +5878,9 @@ static void nv_pcmark(cmdarg_T *cap)
if (!checkclearopq(cap->oap)) { if (!checkclearopq(cap->oap)) {
if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL) { if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL) {
goto_tabpage_lastused(); if (!goto_tabpage_lastused()) {
clearopbeep(cap->oap);
}
return; return;
} }
if (cap->cmdchar == 'g') { if (cap->cmdchar == 'g') {
@ -6642,9 +6644,10 @@ static void nv_g_cmd(cmdarg_T *cap)
goto_tabpage(-(int)cap->count1); goto_tabpage(-(int)cap->count1);
} }
break; break;
case TAB: case TAB:
if (!checkclearop(oap)) { if (!checkclearop(oap) && !goto_tabpage_lastused()) {
goto_tabpage_lastused(); clearopbeep(oap);
} }
break; break;

View File

@ -128,6 +128,8 @@ function Test_tabpage()
1tabmove 1tabmove
call assert_equal(2, tabpagenr()) call assert_equal(2, tabpagenr())
call assert_fails('let t = tabpagenr("@")', 'E15:')
call assert_equal(0, tabpagewinnr(-1))
call assert_fails("99tabmove", 'E16:') call assert_fails("99tabmove", 'E16:')
call assert_fails("+99tabmove", 'E16:') call assert_fails("+99tabmove", 'E16:')
call assert_fails("-99tabmove", 'E16:') call assert_fails("-99tabmove", 'E16:')
@ -683,4 +685,48 @@ func Test_tabline_tabmenu()
%bw! %bw!
endfunc endfunc
" Test for jumping to last accessed tabpage
func Test_lastused_tabpage()
tabonly!
call assert_equal(0, tabpagenr('#'))
call assert_beeps('call feedkeys("g\<Tab>", "xt")')
call assert_beeps('call feedkeys("\<C-Tab>", "xt")')
call assert_beeps('call feedkeys("\<C-W>g\<Tab>", "xt")')
" open four tab pages
tabnew
tabnew
tabnew
2tabnext
" Test for g<Tab>
call assert_equal(4, tabpagenr('#'))
call feedkeys("g\<Tab>", "xt")
call assert_equal(4, tabpagenr())
call assert_equal(2, tabpagenr('#'))
" Test for <C-Tab>
call feedkeys("\<C-Tab>", "xt")
call assert_equal(2, tabpagenr())
call assert_equal(4, tabpagenr('#'))
" Test for <C-W>g<Tab>
call feedkeys("\<C-W>g\<Tab>", "xt")
call assert_equal(4, tabpagenr())
call assert_equal(2, tabpagenr('#'))
" Try to jump to a closed tab page
tabclose 2
call assert_equal(0, tabpagenr('#'))
call feedkeys("g\<Tab>", "xt")
call assert_equal(3, tabpagenr())
call feedkeys("\<C-Tab>", "xt")
call assert_equal(3, tabpagenr())
call feedkeys("\<C-W>g\<Tab>", "xt")
call assert_equal(3, tabpagenr())
tabclose!
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab

View File

@ -530,10 +530,6 @@ wingotofile:
do_nv_ident('g', xchar); do_nv_ident('g', xchar);
break; break;
case TAB:
goto_tabpage_lastused();
break;
case 'f': // CTRL-W gf: "gf" in a new tab page case 'f': // CTRL-W gf: "gf" in a new tab page
case 'F': // CTRL-W gF: "gF" in a new tab page case 'F': // CTRL-W gF: "gF" in a new tab page
cmdmod.tab = tabpage_index(curtab) + 1; cmdmod.tab = tabpage_index(curtab) + 1;
@ -547,6 +543,12 @@ wingotofile:
goto_tabpage(-(int)Prenum1); goto_tabpage(-(int)Prenum1);
break; break;
case TAB: // CTRL-W g<Tab>: go to last used tab page
if (!goto_tabpage_lastused()) {
beep_flush();
}
break;
case 'e': case 'e':
if (curwin->w_floating || !ui_has(kUIMultigrid)) { if (curwin->w_floating || !ui_has(kUIMultigrid)) {
beep_flush(); beep_flush();
@ -4119,8 +4121,8 @@ static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, bool trigger_enter_a
{ {
int old_off = tp->tp_firstwin->w_winrow; int old_off = tp->tp_firstwin->w_winrow;
win_T *next_prevwin = tp->tp_prevwin; win_T *next_prevwin = tp->tp_prevwin;
tabpage_T *old_curtab = curtab; tabpage_T *old_curtab = curtab;
curtab = tp; curtab = tp;
firstwin = tp->tp_firstwin; firstwin = tp->tp_firstwin;
lastwin = tp->tp_lastwin; lastwin = tp->tp_lastwin;
@ -4291,13 +4293,15 @@ void goto_tabpage_tp(tabpage_T *tp, bool trigger_enter_autocmds, bool trigger_le
} }
} }
// Go to the last accessed tab page, if there is one. /// Go to the last accessed tab page, if there is one.
void goto_tabpage_lastused(void) /// @return true if the tab page is valid, false otherwise.
bool goto_tabpage_lastused(void)
{ {
int index = tabpage_index(lastused_tabpage); if (valid_tabpage(lastused_tabpage)) {
if (index < tabpage_index(NULL)) { goto_tabpage_tp(lastused_tabpage, true, true);
goto_tabpage(index); return true;
} }
return false;
} }
/* /*