diff src/window.c @ 32445:48b95cef8fa2 v9.0.1554

patch 9.0.1554: code for handling 'switchbuf' is repeated Commit: https://github.com/vim/vim/commit/e42c27d9e8a18e3786f13f17663914cdd0f63f9e Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sun May 14 17:24:22 2023 +0100 patch 9.0.1554: code for handling 'switchbuf' is repeated Problem: Code for handling 'switchbuf' is repeated. Solution: Add a function to handle 'switchbuf'. (Yegappan Lakshmanan, closes #12397)
author Bram Moolenaar <Bram@vim.org>
date Sun, 14 May 2023 18:30:05 +0200
parents bfc23ba1bba5
children 87f59a64efab
line wrap: on
line diff
--- a/src/window.c
+++ b/src/window.c
@@ -168,6 +168,32 @@ prevwin_curwin(void)
 }
 
 /*
+ * If the 'switchbuf' option contains "useopen" or "usetab", then try to jump
+ * to a window containing "buf".
+ * Returns the pointer to the window that was jumped to or NULL.
+ */
+    win_T *
+swbuf_goto_win_with_buf(buf_T *buf)
+{
+    win_T   *wp = NULL;
+
+    if (buf == NULL)
+	return wp;
+
+    // If 'switchbuf' contains "useopen": jump to first window in the current
+    // tab page containing "buf" if one exists.
+    if (swb_flags & SWB_USEOPEN)
+	wp = buf_jump_open_win(buf);
+
+    // If 'switchbuf' contains "usetab": jump to first window in any tab page
+    // containing "buf" if one exists.
+    if (wp == NULL && (swb_flags & SWB_USETAB))
+	wp = buf_jump_open_tab(buf);
+
+    return wp;
+}
+
+/*
  * All CTRL-W window commands are handled here, called from normal_cmd().
  */
     void
@@ -586,21 +612,7 @@ wingotofile:
 		    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);
-			}
-		    }
+			wp = swbuf_goto_win_with_buf(buflist_findname_exp(ptr));
 
 		    if (wp == NULL && win_split(0, 0) == OK)
 		    {