From 880d3537d06ef067021c73bc361fa47ab8347992 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 21 Feb 2022 10:55:36 +0000 Subject: [PATCH 1/9] vim-patch:8.2.4428: crash when switching tabpage while in the cmdline window Problem: Crash when switching tabpage while in the cmdline window. Solution: Disallow switching tabpage when in the cmdline window. https://github.com/vim/vim/commit/0f6e28f686dbb59ab3b562408ab9b2234797b9b1 Ensure cmdline window doesn't stop us from closing tabs with EXITFREE. mem_free_all -> win_free_all -> tabpage_close -> ... -> goto_tabpage_tp -> CHECK_CMDWIN can cause an infinite loop if Nvim is exited without using standard methods such as :qa! and friends (e.g: killed via a signal). This issue had caused the ASAN CI's functionaltests to timeout. Cherry-pick Test_cmdwin_tabpage from v8.2.4463. https://github.com/vim/vim/commit/38b85cb4d7216705058708bacbc25ab90cd61595 This bug was already fixed in Nvim. Note that g inside cmdwin is already tested for in tabnewentered_spec.lua anyway. E492 is thrown after E11 when using ":norm" in assert_fails for some reason (except after v8.2.1919, which isn't ported yet). As v8.2.1183 isn't ported yet, so we cannot assert E11 directly. Modify the test to check for E11 and E492 seperately; when v8.2.1183 is ported, the assertion for E492 will fail and the changes can be reverted to match upstream. Remove redundant CHECK_CMDWIN from goto_tabpage; it's handled with text_locked() and text_locked_msg() above: vim-patch:8.2.4434: duplicate check for cmdline window Problem: Duplicate check for cmdline window. Solution: Remove the second check. (Sean Dewar, closes vim/vim#9816) https://github.com/vim/vim/commit/16b51d26fe2cc3afb09afd439069220dea74581d --- src/nvim/eval.c | 7 ++----- src/nvim/ex_docmd.c | 9 +++++---- src/nvim/ex_getln.c | 2 +- src/nvim/memory.c | 3 +++ src/nvim/testdir/test_cmdline.vim | 12 ++++++++++++ src/nvim/window.c | 16 ++++++++++++++++ 6 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 18b9039d60..b2ce15fd87 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -3257,9 +3257,7 @@ char_u *get_user_var_name(expand_T *xp, int idx) } // b: variables - // In cmdwin, the alternative buffer should be used. - hashtab_T *ht - = is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab : &curbuf->b_vars->dv_hashtab; + const hashtab_T *ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab; if (bdone < ht->ht_used) { if (bdone++ == 0) { hi = ht->ht_array; @@ -3273,8 +3271,7 @@ char_u *get_user_var_name(expand_T *xp, int idx) } // w: variables - // In cmdwin, the alternative window should be used. - ht = is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab : &curwin->w_vars->dv_hashtab; + ht = &prevwin_curwin()->w_vars->dv_hashtab; if (wdone < ht->ht_used) { if (wdone++ == 0) { hi = ht->ht_array; diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 63dc1e539e..6bd465e6ee 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -2704,7 +2704,7 @@ static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int * // only full match global is accepted. // Look for buffer-local user commands first, then global ones. - gap = is_in_cmdwin() ? &prevwin->w_buffer->b_ucmds : &curbuf->b_ucmds; + gap = &prevwin_curwin()->w_buffer->b_ucmds; for (;;) { for (j = 0; j < gap->ga_len; j++) { uc = USER_CMD_GA(gap, j); @@ -5378,7 +5378,7 @@ static void uc_list(char_u *name, size_t name_len) uint32_t a; // In cmdwin, the alternative buffer should be used. - garray_T *gap = is_in_cmdwin() ? &prevwin->w_buffer->b_ucmds : &curbuf->b_ucmds; + const garray_T *gap = &prevwin_curwin()->w_buffer->b_ucmds; for (;;) { for (i = 0; i < gap->ga_len; i++) { cmd = USER_CMD_GA(gap, i); @@ -6357,7 +6357,7 @@ char_u *get_user_commands(expand_T *xp FUNC_ATTR_UNUSED, int idx) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { // In cmdwin, the alternative buffer should be used. - const buf_T *const buf = is_in_cmdwin() ? prevwin->w_buffer : curbuf; + const buf_T *const buf = prevwin_curwin()->w_buffer; if (idx < buf->b_ucmds.ga_len) { return USER_CMD_GA(&buf->b_ucmds, idx)->uc_name; @@ -6379,7 +6379,8 @@ static char_u *get_user_command_name(int idx, int cmdidx) } if (cmdidx == CMD_USER_BUF) { // In cmdwin, the alternative buffer should be used. - buf_T *buf = is_in_cmdwin() ? prevwin->w_buffer : curbuf; + const buf_T *const buf = prevwin_curwin()->w_buffer; + if (idx < buf->b_ucmds.ga_len) { return USER_CMD_GA(&buf->b_ucmds, idx)->uc_name; } diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 30287cd6f2..f52f3afe7d 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -6589,7 +6589,7 @@ static int open_cmdwin(void) /// @return true if in the cmdwin, not editing the command line. bool is_in_cmdwin(void) - FUNC_ATTR_PURE + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { return cmdwin_type != 0 && get_cmdline_type() == NUL; } diff --git a/src/nvim/memory.c b/src/nvim/memory.c index d68ca6b62e..373693a6fe 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -575,6 +575,9 @@ void free_all_mem(void) // Don't want to trigger autocommands from here on. block_autocmds(); + // Ensure cmdline window doesn't prevent closing tabs and windows. + cmdwin_type = 0; + // Close all tabs and windows. Reset 'equalalways' to avoid redraws. p_ea = false; if (first_tabpage->tp_next != NULL) { diff --git a/src/nvim/testdir/test_cmdline.vim b/src/nvim/testdir/test_cmdline.vim index ff4cbe544c..c589d941da 100644 --- a/src/nvim/testdir/test_cmdline.vim +++ b/src/nvim/testdir/test_cmdline.vim @@ -1123,6 +1123,18 @@ func Test_cmdlineclear_tabenter() call delete('XtestCmdlineClearTabenter') endfunc +func Test_cmdwin_tabpage() + tabedit + " v8.2.1919 isn't ported yet, so E492 is thrown after E11 here. + " v8.2.1183 also isn't ported yet, so we also can't assert E11 directly. + " For now, assert E11 and E492 seperately. When v8.2.1183 is ported, the + " assert for E492 will fail and this workaround should be removed. + " call assert_fails("silent norm q/g :I\", 'E11:') + call assert_fails("silent norm q/g ", 'E11:') + call assert_fails("silent norm q/g :I\", 'E492:') + tabclose! +endfunc + " test that ";" works to find a match at the start of the first line func Test_zero_line_search() new diff --git a/src/nvim/window.c b/src/nvim/window.c index d659f60e66..5878a6ba0b 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -73,6 +73,15 @@ typedef enum { static char *m_onlyone = N_("Already only one window"); +/// @return the current window, unless in the cmdline window and "prevwin" is +/// set, then return "prevwin". +win_T *prevwin_curwin(void) + FUNC_ATTR_WARN_UNUSED_RESULT +{ + // In cmdwin, the alternative buffer should be used. + return is_in_cmdwin() && prevwin != NULL ? prevwin : curwin; +} + /// all CTRL-W window commands are handled here, called from normal_cmd(). /// /// @param xchar extra char from ":wincmd gx" or NUL @@ -3857,6 +3866,11 @@ int win_new_tabpage(int after, char_u *filename) tabpage_T *newtp; int n; + if (cmdwin_type != 0) { + emsg(_(e_cmdwin)); + return FAIL; + } + newtp = alloc_tabpage(); // Remember the current windows in this Tab page. @@ -4255,6 +4269,8 @@ void goto_tabpage(int n) /// @param trigger_leave_autocmds when true trigger *Leave autocommands. void goto_tabpage_tp(tabpage_T *tp, bool trigger_enter_autocmds, bool trigger_leave_autocmds) { + CHECK_CMDWIN; + // Don't repeat a message in another tab page. set_keep_msg(NULL, 0); From 490874f3da4c39a36438b6cfa6d79e594b82806f Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 21 Feb 2022 16:19:49 +0000 Subject: [PATCH 2/9] vim-patch:8.2.4432: cannot use settabvar() while the cmdline window is open Problem: Cannot use settabvar() while the cmdline window is open. Solution: Only give an error when actually switching tabpage. (closes vim/vim#9813) https://github.com/vim/vim/commit/592f6250017c31c8996325403e511f4502077ba5 --- src/nvim/window.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nvim/window.c b/src/nvim/window.c index 5878a6ba0b..3c34620db4 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -4269,7 +4269,9 @@ void goto_tabpage(int n) /// @param trigger_leave_autocmds when true trigger *Leave autocommands. void goto_tabpage_tp(tabpage_T *tp, bool trigger_enter_autocmds, bool trigger_leave_autocmds) { - CHECK_CMDWIN; + if (trigger_enter_autocmds || trigger_leave_autocmds) { + CHECK_CMDWIN; + } // Don't repeat a message in another tab page. set_keep_msg(NULL, 0); From 7519af4f0fb54f4ef8b48ec4f18036069f18bf62 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Fri, 25 Feb 2022 00:13:25 +0000 Subject: [PATCH 3/9] vim-patch:8.2.4438: crash on exit when using cmdline window Problem: Crash on exit when using cmdline window. Solution: Reset "cmdwin_type" before exiting. (closes vim/vim#9817) https://github.com/vim/vim/commit/ca0c1caa36823ea8e61184268d7337e79995352f Bram also went with the cmdwin_type = 0 solution, but putting it in read_error_exit isn't ideal and only fixes one specific variant of the bug, so don't port that change. Port the test only, but skip it as Nvim does not exit after stdin is exhausted. Using -es instead does exit, but read_error_exit does not run preserve_exit in that case, and does not have issues exiting even without resetting cmdwin_type. Note that the test has problems and is fixed in later patches. --- src/nvim/testdir/test_exit.vim | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/nvim/testdir/test_exit.vim b/src/nvim/testdir/test_exit.vim index bd3e9eb4d4..1d3722c718 100644 --- a/src/nvim/testdir/test_exit.vim +++ b/src/nvim/testdir/test_exit.vim @@ -1,6 +1,7 @@ " Tests for exiting Vim. source shared.vim +source check.vim func Test_exiting() let after =<< trim [CODE] @@ -109,4 +110,21 @@ func Test_exit_code() call delete('Xtestout') endfunc +func Test_exit_error_reading_input() + throw 'Skipped: Nvim does not exit after stdin is read' + + CheckNotGui + + call writefile([":au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout')", ":tabnew\q:"], 'Xscript') + + " Nvim requires "-s -" to read stdin as Normal mode input + " if RunVim([], [], '< Xscript') + if RunVim([], [], '-s - < Xscript') + call assert_equal(['l = 1'], readfile('Xtestout')) + endif + call delete('Xscript') + call delete('Xtestout') +endfun + + " vim: shiftwidth=2 sts=2 expandtab From fe5318293442aabdd4c7fd12ad892adcba1f80fb Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Fri, 25 Feb 2022 00:22:12 +0000 Subject: [PATCH 4/9] vim-patch:8.2.4442: test for error reading input fails on MS-Windows Problem: Test for error reading input fails on MS-Windows. Solution: Don't run the test on MS-Windows. https://github.com/vim/vim/commit/70b9e4f4c3a62e325fd16ac108bd12feb026ede5 --- src/nvim/testdir/test_exit.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nvim/testdir/test_exit.vim b/src/nvim/testdir/test_exit.vim index 1d3722c718..cbbd82d7d3 100644 --- a/src/nvim/testdir/test_exit.vim +++ b/src/nvim/testdir/test_exit.vim @@ -114,6 +114,7 @@ func Test_exit_error_reading_input() throw 'Skipped: Nvim does not exit after stdin is read' CheckNotGui + CheckNotMSWindows call writefile([":au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout')", ":tabnew\q:"], 'Xscript') From 0412dba45622eeb20e83d5458bab8714e2da0d2e Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Fri, 25 Feb 2022 00:20:35 +0000 Subject: [PATCH 5/9] vim-patch:8.2.4444: beep caused by test Problem: Beep caused by test. ASAN reports leaks. Solution: Do not put a NL at the end of the script. Make the text work on MS-Windows. Do not run the test with ASAN. https://github.com/vim/vim/commit/68eab67119734ea1efc7cef1287276d969f2713a The test is skipped, but cherry-pick CheckNotAsan from v8.2.2424 anyway. https://github.com/vim/vim/commit/97202d951685fc4d90085da676a90644cbf72571 --- src/nvim/testdir/check.vim | 8 ++++++++ src/nvim/testdir/test_exit.vim | 9 +++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/nvim/testdir/check.vim b/src/nvim/testdir/check.vim index 883f036fe1..8f97d959ce 100644 --- a/src/nvim/testdir/check.vim +++ b/src/nvim/testdir/check.vim @@ -137,6 +137,14 @@ func CheckNotMSWindows() endif endfunc +" Command to check for not running under ASAN +command CheckNotAsan call CheckNotAsan() +func CheckNotAsan() + if execute('version') =~# '-fsanitize=[a-z,]*\' + throw 'Skipped: does not work with ASAN' + endif +endfunc + " Command to check for satisfying any of the conditions. " e.g. CheckAnyOf Feature:bsd Feature:sun Linux command -nargs=+ CheckAnyOf call CheckAnyOf() diff --git a/src/nvim/testdir/test_exit.vim b/src/nvim/testdir/test_exit.vim index cbbd82d7d3..412802efb2 100644 --- a/src/nvim/testdir/test_exit.vim +++ b/src/nvim/testdir/test_exit.vim @@ -114,13 +114,14 @@ func Test_exit_error_reading_input() throw 'Skipped: Nvim does not exit after stdin is read' CheckNotGui - CheckNotMSWindows + " The early exit causes memory not to be freed somehow + CheckNotAsan - call writefile([":au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout')", ":tabnew\q:"], 'Xscript') + call writefile([":au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout')", ":tabnew", "q:"], 'Xscript', 'b') " Nvim requires "-s -" to read stdin as Normal mode input - " if RunVim([], [], '< Xscript') - if RunVim([], [], '-s - < Xscript') + " if RunVim([], [], ' Date: Fri, 25 Feb 2022 00:22:54 +0000 Subject: [PATCH 6/9] vim-patch:8.2.4445: exit test fails on MS-Windows anyway Problem: Exit test fails on MS-Windows anyway. Solution: Skip the test on MS-Windows. https://github.com/vim/vim/commit/29a9e6971849b4a9eabf14fee1130d51cecfbaa7 --- src/nvim/testdir/test_exit.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nvim/testdir/test_exit.vim b/src/nvim/testdir/test_exit.vim index 412802efb2..4e5da49adb 100644 --- a/src/nvim/testdir/test_exit.vim +++ b/src/nvim/testdir/test_exit.vim @@ -114,6 +114,7 @@ func Test_exit_error_reading_input() throw 'Skipped: Nvim does not exit after stdin is read' CheckNotGui + CheckNotMSWindows " The early exit causes memory not to be freed somehow CheckNotAsan From 7e19c18a544b5f1f15cec0444385ddae80687a26 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Fri, 25 Feb 2022 00:17:17 +0000 Subject: [PATCH 7/9] vim-patch:8.2.4454: resetting cmdwin_type only for one situation Problem: Resetting cmdwin_type only for one situation. Solution: Reset cmdwin_type before closing windows. (closes vim/vim#9822) https://github.com/vim/vim/commit/6a8b13614e5bcb233d20403ae9f008ccba152be3 Move the check to win_free_all to match Vim. --- src/nvim/memory.c | 3 --- src/nvim/testdir/test_exit.vim | 1 + src/nvim/window.c | 3 +++ 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/nvim/memory.c b/src/nvim/memory.c index 373693a6fe..d68ca6b62e 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -575,9 +575,6 @@ void free_all_mem(void) // Don't want to trigger autocommands from here on. block_autocmds(); - // Ensure cmdline window doesn't prevent closing tabs and windows. - cmdwin_type = 0; - // Close all tabs and windows. Reset 'equalalways' to avoid redraws. p_ea = false; if (first_tabpage->tp_next != NULL) { diff --git a/src/nvim/testdir/test_exit.vim b/src/nvim/testdir/test_exit.vim index 4e5da49adb..befcaec2b2 100644 --- a/src/nvim/testdir/test_exit.vim +++ b/src/nvim/testdir/test_exit.vim @@ -123,6 +123,7 @@ func Test_exit_error_reading_input() " Nvim requires "-s -" to read stdin as Normal mode input " if RunVim([], [], 'tp_next != NULL) { tabpage_close(TRUE); } From c5f190e0c21b4e8465502fd9f260c3d49a4102ab Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 14 Mar 2022 11:37:44 +0000 Subject: [PATCH 8/9] vim-patch:8.2.1401: cannot jump to the last used tabpage Problem: Cannot jump to the last used tabpage. Solution: Add g and tabpagnr('#'). (Yegappan Lakshmanan, closes vim/vim#6661, neovim #11626) https://github.com/vim/vim/commit/62a232506d06f6d1b3b7271801c907d6294dfe84 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 to tabpage.txt, and refer to 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. https://github.com/vim/vim/commit/92b83ccfda7a1d654ccaaf161a9c8a8e01fbcf76 --- runtime/doc/builtin.txt | 8 +++--- runtime/doc/index.txt | 3 +- runtime/doc/tabpage.txt | 7 ++--- src/nvim/eval/funcs.c | 4 +-- src/nvim/globals.h | 5 ++-- src/nvim/normal.c | 11 +++++--- src/nvim/testdir/test_tabpage.vim | 46 +++++++++++++++++++++++++++++++ src/nvim/window.c | 24 +++++++++------- 8 files changed, 80 insertions(+), 28 deletions(-) diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index ccc02e83b2..9eec23b7b7 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -8103,15 +8103,15 @@ tabpagebuflist([{arg}]) *tabpagebuflist()* tabpagenr([{arg}]) *tabpagenr()* The result is a Number, which is the number of the current tab page. The first tab page has number 1. + The optional argument {arg} supports the following values: $ the number of the last tab page (the tab page count). - # the number of the last accessed tab page (where - |g| goes to). If there is no previous - tab page, 0 is returned. + # the number of the last accessed tab page + (where |g| goes to). If there is no + previous tab page, 0 is returned. The number can be used with the |:tab| command. - tabpagewinnr({tabarg} [, {arg}]) *tabpagewinnr()* Like |winnr()| but for tab page {tabarg}. {tabarg} specifies the number of tab page to be used. diff --git a/runtime/doc/index.txt b/runtime/doc/index.txt index d02ab1b759..572b4e3f93 100644 --- a/runtime/doc/index.txt +++ b/runtime/doc/index.txt @@ -431,6 +431,7 @@ tag char note action in Normal mode ~ || ":ta" to the keyword at the mouse click || 1 same as "w" || same as "CTRL-T" +|| same as "g" || ["x] 2 same as "x" |N| {count} remove the last digit from {count} || 1 same as "j" @@ -577,7 +578,7 @@ tag command action in Normal mode ~ 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 previous tab page -|CTRL-W_g| CTRL-W g same as `g` : go to last accessed tab +|CTRL-W_g| CTRL-W g same as |g|: go to last accessed tab page |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 diff --git a/runtime/doc/tabpage.txt b/runtime/doc/tabpage.txt index 78b5101da7..f06a6bcc34 100644 --- a/runtime/doc/tabpage.txt +++ b/runtime/doc/tabpage.txt @@ -196,10 +196,6 @@ gt *i_CTRL-* *i_* {count} {count}gt Go to tab page {count}. The first tab page has number one. -CTRL- *CTRL-* -CTRL-W g *g* *CTRL-W_g* -g Go to previous (last accessed) tab page. - :tabp[revious] *:tabp* *:tabprevious* *gT* *:tabN* :tabN[ext] *:tabNext* *CTRL-* ** *i_CTRL-* *i_* @@ -219,6 +215,9 @@ gT Go to the previous tab page. Wraps around from the first one *:tabl* *:tablast* :tabl[ast] Go to the last tab page. + *CTRL-* ** +CTRL-W g *g* *CTRL-W_g* +g Go to the last accessed tab page. Other commands: *:tabs* diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index b249dffe11..b74f9759ac 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -11213,9 +11213,7 @@ static void f_tabpagenr(typval_T *argvars, typval_T *rettv, FunPtr fptr) if (strcmp(arg, "$") == 0) { nr = tabpage_index(NULL) - 1; } else if (strcmp(arg, "#") == 0) { - nr = valid_tabpage(lastused_tabpage) - ? tabpage_index(lastused_tabpage) - : nr; + nr = valid_tabpage(lastused_tabpage) ? tabpage_index(lastused_tabpage) : 0; } else { semsg(_(e_invexpr2), arg); } diff --git a/src/nvim/globals.h b/src/nvim/globals.h index b64ed7c758..cbd67afb09 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -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 // 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 *lastused_tabpage; EXTERN tabpage_T *curtab; +EXTERN tabpage_T *lastused_tabpage; EXTERN bool redraw_tabline INIT(= false); // need to redraw tabline // Iterates over all tabs in the tab list diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 0e5e0ab403..f402865d2d 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -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) { pos_T *pos; @@ -5878,7 +5878,9 @@ static void nv_pcmark(cmdarg_T *cap) if (!checkclearopq(cap->oap)) { if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL) { - goto_tabpage_lastused(); + if (!goto_tabpage_lastused()) { + clearopbeep(cap->oap); + } return; } if (cap->cmdchar == 'g') { @@ -6642,9 +6644,10 @@ static void nv_g_cmd(cmdarg_T *cap) goto_tabpage(-(int)cap->count1); } break; + case TAB: - if (!checkclearop(oap)) { - goto_tabpage_lastused(); + if (!checkclearop(oap) && !goto_tabpage_lastused()) { + clearopbeep(oap); } break; diff --git a/src/nvim/testdir/test_tabpage.vim b/src/nvim/testdir/test_tabpage.vim index 9869dc7590..b458f9a311 100644 --- a/src/nvim/testdir/test_tabpage.vim +++ b/src/nvim/testdir/test_tabpage.vim @@ -128,6 +128,8 @@ function Test_tabpage() 1tabmove 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:') @@ -683,4 +685,48 @@ func Test_tabline_tabmenu() %bw! endfunc +" Test for jumping to last accessed tabpage +func Test_lastused_tabpage() + tabonly! + call assert_equal(0, tabpagenr('#')) + call assert_beeps('call feedkeys("g\", "xt")') + call assert_beeps('call feedkeys("\", "xt")') + call assert_beeps('call feedkeys("\g\", "xt")') + + " open four tab pages + tabnew + tabnew + tabnew + + 2tabnext + + " Test for g + call assert_equal(4, tabpagenr('#')) + call feedkeys("g\", "xt") + call assert_equal(4, tabpagenr()) + call assert_equal(2, tabpagenr('#')) + + " Test for + call feedkeys("\", "xt") + call assert_equal(2, tabpagenr()) + call assert_equal(4, tabpagenr('#')) + + " Test for g + call feedkeys("\g\", "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\", "xt") + call assert_equal(3, tabpagenr()) + call feedkeys("\", "xt") + call assert_equal(3, tabpagenr()) + call feedkeys("\g\", "xt") + call assert_equal(3, tabpagenr()) + + tabclose! +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/window.c b/src/nvim/window.c index f2b84a4124..a235b07b47 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -530,10 +530,6 @@ wingotofile: do_nv_ident('g', xchar); 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 cmdmod.tab = tabpage_index(curtab) + 1; @@ -547,6 +543,12 @@ wingotofile: goto_tabpage(-(int)Prenum1); break; + case TAB: // CTRL-W g: go to last used tab page + if (!goto_tabpage_lastused()) { + beep_flush(); + } + break; + case 'e': if (curwin->w_floating || !ui_has(kUIMultigrid)) { 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; win_T *next_prevwin = tp->tp_prevwin; - tabpage_T *old_curtab = curtab; + curtab = tp; firstwin = tp->tp_firstwin; 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. -void goto_tabpage_lastused(void) +/// Go to the last accessed tab page, if there is one. +/// @return true if the tab page is valid, false otherwise. +bool goto_tabpage_lastused(void) { - int index = tabpage_index(lastused_tabpage); - if (index < tabpage_index(NULL)) { - goto_tabpage(index); + if (valid_tabpage(lastused_tabpage)) { + goto_tabpage_tp(lastused_tabpage, true, true); + return true; } + return false; } /* From 365a9b074f2df3c573ae4a520084818bdd46cd3d Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 14 Mar 2022 12:16:12 +0000 Subject: [PATCH 9/9] vim-patch:8.2.1413: previous tab page not usable from an Ex command Problem: Previous tab page not usable from an Ex command. Solution: Add the "#" argument for :tabnext et al. (Yegappan Lakshmanan, closes vim/vim#6677) https://github.com/vim/vim/commit/94f4ffa7704921a3634e56b878e6dc362bc3d508 Do not rename old_curtab to prev_tp in win_new_tabpage, this can be confused with the previous tabpage (`:tabprevious`). Cherry-pick ex_errmsg from v8.2.1280. https://github.com/vim/vim/commit/8930caaa1a283092aca81fdbc3fcf15c7eadb197 --- src/nvim/ex_docmd.c | 20 +++++++++++++++++- src/nvim/testdir/test_tabpage.vim | 35 ++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 6bd465e6ee..09cf6601ee 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -2036,6 +2036,18 @@ doend: return ea.nextcmd; } +static char ex_error_buf[MSG_BUF_LEN]; + +/// @return an error message with argument included. +/// Uses a static buffer, only the last error will be kept. +/// "msg" will be translated, caller should use N_(). +char *ex_errmsg(const char *const msg, const char_u *const arg) + FUNC_ATTR_NONNULL_ALL +{ + vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg); + return ex_error_buf; +} + // Parse and skip over command modifiers: // - update eap->cmd // - store flags in "cmdmod". @@ -4861,7 +4873,13 @@ static int get_tabpage_arg(exarg_T *eap) if (STRCMP(p, "$") == 0) { tab_number = LAST_TAB_NR; } else if (STRCMP(p, "#") == 0) { - tab_number = tabpage_index(lastused_tabpage); + if (valid_tabpage(lastused_tabpage)) { + tab_number = tabpage_index(lastused_tabpage); + } else { + eap->errmsg = ex_errmsg(e_invargval, eap->arg); + tab_number = 0; + goto theend; + } } else if (p == p_save || *p_save == '-' || *p != NUL || tab_number > LAST_TAB_NR) { // No numbers as argument. diff --git a/src/nvim/testdir/test_tabpage.vim b/src/nvim/testdir/test_tabpage.vim index b458f9a311..51ab5c1022 100644 --- a/src/nvim/testdir/test_tabpage.vim +++ b/src/nvim/testdir/test_tabpage.vim @@ -692,6 +692,7 @@ func Test_lastused_tabpage() call assert_beeps('call feedkeys("g\", "xt")') call assert_beeps('call feedkeys("\", "xt")') call assert_beeps('call feedkeys("\g\", "xt")') + call assert_fails('tabnext #', 'E475:') " open four tab pages tabnew @@ -716,17 +717,41 @@ func Test_lastused_tabpage() call assert_equal(4, tabpagenr()) call assert_equal(2, tabpagenr('#')) + " Test for :tabnext # + tabnext # + call assert_equal(2, tabpagenr()) + call assert_equal(4, tabpagenr('#')) + " Try to jump to a closed tab page - tabclose 2 + tabclose # call assert_equal(0, tabpagenr('#')) call feedkeys("g\", "xt") - call assert_equal(3, tabpagenr()) + call assert_equal(2, tabpagenr()) call feedkeys("\", "xt") - call assert_equal(3, tabpagenr()) + call assert_equal(2, tabpagenr()) call feedkeys("\g\", "xt") - call assert_equal(3, tabpagenr()) + call assert_equal(2, tabpagenr()) + call assert_fails('tabnext #', 'E475:') + call assert_equal(2, tabpagenr()) - tabclose! + " Test for :tabonly # + let wnum = win_getid() + $tabnew + tabonly # + call assert_equal(wnum, win_getid()) + call assert_equal(1, tabpagenr('$')) + + " Test for :tabmove # + tabnew + let wnum = win_getid() + tabnew + tabnew + tabnext 2 + tabmove # + call assert_equal(4, tabpagenr()) + call assert_equal(wnum, win_getid()) + + tabonly! endfunc " vim: shiftwidth=2 sts=2 expandtab