]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1546: some commands for opening a file don't use 'switchbuf' v9.0.1546
authorYegappan Lakshmanan <yegappan@yahoo.com>
Fri, 12 May 2023 16:49:13 +0000 (17:49 +0100)
committerBram Moolenaar <Bram@vim.org>
Fri, 12 May 2023 16:49:13 +0000 (17:49 +0100)
Problem:    Some commands for opening a file don't use 'switchbuf'.
Solution:   Use 'switchbuf' for more commands. (Yegappan Lakshmanan,
            closes #12383, closes #12381)

runtime/doc/options.txt
src/buffer.c
src/testdir/test_gf.vim
src/version.c
src/window.c

index 2a2c7ed5c77568b671060ce63c21bb4f02093fcc..c487f1769b926c3827c035310cb7e913d83868f6 100644 (file)
@@ -7928,16 +7928,18 @@ A jump table for the options with a short description can be found at |Q_op|.
 'switchbuf' 'swb'      string  (default "")
                        global
        This option controls the behavior when switching between buffers.
-       Mostly for |quickfix| commands some values are also used for other
-       commands, as mentioned below.
+       This option is checked, when
+       - jumping to errors with the |quickfix| commands (|:cc|, |:cn|, |:cp|,
+         etc.)
+       - jumping to a tag using the |:stag| command.
+       - opening a file using the |CTRL-W_f| or |CTRL-W_F| command.
+       - jumping to a buffer using a buffer split command (e.g.  |:sbuffer|,
+         |:sbnext|, or |:sbrewind|).
        Possible values (comma-separated list):
-          useopen      If included, jump to the first open window that
-                       contains the specified buffer (if there is one).
-                       Otherwise: Do not examine other windows.
-                       This setting is checked with |quickfix| commands, when
-                       jumping to errors (":cc", ":cn", "cp", etc.).  It is
-                       also used in all buffer related split commands, for
-                       example ":sbuffer", ":sbnext", or ":sbrewind".
+          useopen      If included, jump to the first open window in the
+                       current tab page that contains the specified buffer
+                       (if there is one).  Otherwise: Do not examine other
+                       windows.
           usetab       Like "useopen", but also consider windows in other tab
                        pages.
           split        If included, split the current window before loading
index 174ca1efb86c63abbdd73c9ec6933ff3861cf57b..60b7c80971584477eaa3d98e954a66ac6e37cf2f 100644 (file)
@@ -2560,7 +2560,6 @@ buflist_getfpos(void)
     }
 }
 
-#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
 /*
  * Find file in buffer list by name (it has to be for the current window).
  * Returns NULL if not found.
@@ -2586,7 +2585,6 @@ buflist_findname_exp(char_u *fname)
     }
     return buf;
 }
-#endif
 
 /*
  * Find file in buffer list by name (it has to be for the current window).
index 8317cb5df72f8ffe2a07b293c6b99b043b9dde9a..cc12b3618feb06e70e8fe7bd1a3ad4decab85f25 100644 (file)
@@ -292,4 +292,65 @@ func Test_gf_subdirs_wildcard()
   set path&
 endfunc
 
+" Test for 'switchbuf' with gf and gF commands
+func Test_gf_switchbuf()
+  call writefile(repeat(["aaa"], 10), "Xtest1", 'D')
+  edit Xtest1
+  new
+  call setline(1, ['Xtest1'])
+
+  " Test for 'useopen'
+  set switchbuf=useopen
+  call cursor(1, 1)
+  exe "normal \<C-W>f"
+  call assert_equal([2, 2], [winnr(), winnr('$')])
+  close
+
+  " If the file is opened in another tabpage, then it should not be considered
+  tabedit Xtest1
+  tabfirst
+  exe "normal \<C-W>f"
+  call assert_equal([1, 2], [winnr(), winnr('$')])
+  call assert_equal([1, 2], [tabpagenr(), tabpagenr('$')])
+  close
+
+  " Test for 'usetab'
+  set switchbuf=usetab
+  exe "normal \<C-W>f"
+  call assert_equal([1, 1], [winnr(), winnr('$')])
+  call assert_equal([2, 2], [tabpagenr(), tabpagenr('$')])
+  %bw!
+
+  " Test for CTRL-W_F with 'useopen'
+  set isfname-=:
+  call setline(1, ['Xtest1:5'])
+  set switchbuf=useopen
+  split +1 Xtest1
+  wincmd b
+  exe "normal \<C-W>F"
+  call assert_equal([1, 2], [winnr(), winnr('$')])
+  call assert_equal(5, line('.'))
+  close
+
+  " If the file is opened in another tabpage, then it should not be considered
+  tabedit +1 Xtest1
+  tabfirst
+  exe "normal \<C-W>F"
+  call assert_equal([1, 2], [winnr(), winnr('$')])
+  call assert_equal(5, line('.'))
+  call assert_equal([1, 2], [tabpagenr(), tabpagenr('$')])
+  close
+
+  " Test for CTRL_W_F with 'usetab'
+  set switchbuf=usetab
+  exe "normal \<C-W>F"
+  call assert_equal([2, 2], [tabpagenr(), tabpagenr('$')])
+  call assert_equal([1, 1], [winnr(), winnr('$')])
+  call assert_equal(5, line('.'))
+
+  set switchbuf=
+  set isfname&
+  %bw!
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index f13f8e093e60ccceb905f35d676f5b3f3be24838..a873172226329bcdb5cc3aef2ab3bf8e04a5f131 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1546,
 /**/
     1545,
 /**/
index 27b353e17c53ea9c8f21ec40cb70fa7e0c838dc7..b4b23d3584bf1bb2c52207785b2dd599b1b0a0d9 100644 (file)
@@ -580,7 +580,29 @@ wingotofile:
                    need_mouse_correct = TRUE;
 #endif
                    setpcmark();
-                   if (win_split(0, 0) == OK)
+
+                   // If 'switchbuf' is set to 'useopen' or 'usetab' and the
+                   // file is already opened in a window, then jump to it.
+                   wp = NULL;
+                   if ((swb_flags & (SWB_USEOPEN | SWB_USETAB))
+                                               && cmdmod.cmod_tab == 0)
+                   {
+                       buf_T *existing_buf = buflist_findname_exp(ptr);
+
+                       if (existing_buf != NULL)
+                       {
+                           if (swb_flags & SWB_USEOPEN)
+                               wp = buf_jump_open_win(existing_buf);
+
+                           // If 'switchbuf' contains "usetab": jump to first
+                           // window in any tab page containing "existing_buf"
+                           // if one exists.
+                           if (wp == NULL && (swb_flags & SWB_USETAB))
+                               wp = buf_jump_open_tab(existing_buf);
+                       }
+                   }
+
+                   if (wp == NULL && win_split(0, 0) == OK)
                    {
                        RESET_BINDING(curwin);
                        if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
@@ -591,12 +613,15 @@ wingotofile:
                            win_close(curwin, FALSE);
                            goto_tabpage_win(oldtab, oldwin);
                        }
-                       else if (nchar == 'F' && lnum >= 0)
-                       {
-                           curwin->w_cursor.lnum = lnum;
-                           check_cursor_lnum();
-                           beginline(BL_SOL | BL_FIX);
-                       }
+                       else
+                           wp = curwin;
+                   }
+
+                   if (wp != NULL && nchar == 'F' && lnum >= 0)
+                   {
+                       curwin->w_cursor.lnum = lnum;
+                       check_cursor_lnum();
+                       beginline(BL_SOL | BL_FIX);
                    }
                    vim_free(ptr);
                }