diff src/search.c @ 15239:db5d2429bda3 v8.1.0629

patch 8.1.0629: "gn" selects the wrong text with a multi-line match commit https://github.com/vim/vim/commit/5d24a2257e597fd752e33b2c1e9c19cf9114a517 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Dec 23 19:10:09 2018 +0100 patch 8.1.0629: "gn" selects the wrong text with a multi-line match Problem: "gn" selects the wrong text with a multi-line match. Solution: Get the end position from searchit() directly. (closes https://github.com/vim/vim/issues/3695)
author Bram Moolenaar <Bram@vim.org>
date Sun, 23 Dec 2018 19:15:05 +0100
parents e8fdc71f3ea0
children 55ccc2d353bd
line wrap: on
line diff
--- a/src/search.c
+++ b/src/search.c
@@ -610,8 +610,8 @@ last_pat_prog(regmmatch_T *regmatch)
 
 /*
  * Lowest level search function.
- * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
- * Start at position 'pos' and return the found position in 'pos'.
+ * Search for 'count'th occurrence of pattern "pat" in direction "dir".
+ * Start at position "pos" and return the found position in "pos".
  *
  * if (options & SEARCH_MSG) == 0 don't give any messages
  * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
@@ -634,6 +634,7 @@ searchit(
 				   buffer without a window! */
     buf_T	*buf,
     pos_T	*pos,
+    pos_T	*end_pos,	// set to end of the match, unless NULL
     int		dir,
     char_u	*pat,
     long	count,
@@ -1035,14 +1036,26 @@ searchit(
 			    }
 #endif
 			}
+			if (end_pos != NULL)
+			{
+			    end_pos->lnum = lnum + matchpos.lnum;
+			    end_pos->col = matchpos.col;
+			}
 		    }
 		    else
 		    {
 			pos->lnum = lnum + matchpos.lnum;
 			pos->col = matchpos.col;
+			if (end_pos != NULL)
+			{
+			    end_pos->lnum = lnum + endpos.lnum;
+			    end_pos->col = endpos.col;
+			}
 		    }
 #ifdef FEAT_VIRTUALEDIT
 		    pos->coladd = 0;
+		    if (end_pos != NULL)
+			end_pos->coladd = 0;
 #endif
 		    found = 1;
 		    first_match = FALSE;
@@ -1496,7 +1509,7 @@ do_search(
 	     lrFswap(searchstr,0);
 #endif
 
-	c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
+	c = searchit(curwin, curbuf, &pos, NULL, dirc == '/' ? FORWARD : BACKWARD,
 		searchstr, count, spats[0].off.end + (options &
 		       (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
 			+ SEARCH_MSG + SEARCH_START
@@ -4665,20 +4678,19 @@ static int is_one_char(char_u *pattern, 
     int
 current_search(
     long	count,
-    int		forward)	/* move forward or backwards */
+    int		forward)	// TRUE for forward, FALSE for backward
 {
-    pos_T	start_pos;	/* position before the pattern */
-    pos_T	orig_pos;	/* position of the cursor at beginning */
-    pos_T	first_match;	/* position of first match */
-    pos_T	pos;		/* position after the pattern */
+    pos_T	start_pos;	// start position of the pattern match
+    pos_T	end_pos;	// end position of the pattern match
+    pos_T	orig_pos;	// position of the cursor at beginning
+    pos_T	pos;		// position after the pattern
     int		i;
     int		dir;
-    int		result;		/* result of various function calls */
+    int		result;		// result of various function calls
     char_u	old_p_ws = p_ws;
     int		flags = 0;
     pos_T	save_VIsual = VIsual;
     int		one_char;
-    int		direction = forward ? FORWARD : BACKWARD;
 
     /* wrapping should not occur */
     p_ws = FALSE;
@@ -4730,8 +4742,10 @@ current_search(
 	flags = 0;
 	if (!dir && !one_char)
 	    flags = SEARCH_END;
-
-	result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
+	end_pos = pos;
+
+	result = searchit(curwin, curbuf, &pos, &end_pos,
+		(dir ? FORWARD : BACKWARD),
 		spats[last_idx].pat, (long) (i ? count : 1),
 		SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
 
@@ -4739,7 +4753,7 @@ current_search(
 	 * beginning of the file (cursor might be on the search match)
 	 * except when Visual mode is active, so that extending the visual
 	 * selection works. */
-	if (!result && i) /* not found, abort */
+	if (i == 1 && !result) /* not found, abort */
 	{
 	    curwin->w_cursor = orig_pos;
 	    if (VIsual_active)
@@ -4747,7 +4761,7 @@ current_search(
 	    p_ws = old_p_ws;
 	    return FAIL;
 	}
-	else if (!i && !result)
+	else if (i == 0 && !result)
 	{
 	    if (forward)
 	    {
@@ -4763,48 +4777,19 @@ current_search(
 				ml_get(curwin->w_buffer->b_ml.ml_line_count));
 	    }
 	}
-	if (i == 0)
-	    first_match = pos;
 	p_ws = old_p_ws;
     }
 
     start_pos = pos;
-    flags = forward ? SEARCH_END : SEARCH_START;
-
-    /* Check again from the current cursor position,
-     * since the next match might actually by only one char wide */
-    one_char = is_one_char(spats[last_idx].pat, FALSE, &pos, direction);
-    if (one_char < 0)
-	/* search failed, abort */
-	return FAIL;
-
-    /* move to match, except for zero-width matches, in which case, we are
-     * already on the next match */
-    if (!one_char)
-    {
-	p_ws = FALSE;
-	for (i = 0; i < 2; i++)
-	{
-	    result = searchit(curwin, curbuf, &pos, direction,
-		    spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0,
-								   NULL, NULL);
-	    /* Search successfull, break out from the loop */
-	    if (result)
-		break;
-	    /* search failed, try again from the last search position match */
-	    pos = first_match;
-	}
-    }
-
     p_ws = old_p_ws;
-    /* not found */
-    if (!result)
-	return FAIL;
 
     if (!VIsual_active)
 	VIsual = start_pos;
 
-    curwin->w_cursor = pos;
+    // put cursor on last character of match
+    curwin->w_cursor = end_pos;
+    if (LT_POS(VIsual, end_pos))
+	dec_cursor();
     VIsual_active = TRUE;
     VIsual_mode = 'v';
 
@@ -4880,7 +4865,7 @@ is_one_char(char_u *pattern, int move, p
 	flag = SEARCH_START;
     }
 
-    if (searchit(curwin, curbuf, &pos, direction, pattern, 1,
+    if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
 			 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
     {
 	/* Zero-width pattern should match somewhere, then we can check if