Hi Bram!

On Sa, 21 Jul 2012, Bram Moolenaar wrote:

> That sounds good.  I haven't tried the patch yet.  Please also write a
> test for this, then send the patch to the vim-dev list so that others
> can try it out and comment.
> 
> Several users have asked about operating on the search match, I think
> this is the simplest way that allows to do just about everything.  The
> only drawback would be that it uses Visual mode, thus when used in a
> script or mapping it would have to save/restore Visual mode perhaps.
> 
> What I will probably use a lot is "gns" to substitute a match with other
> text.  Then use "n" and "." to repeat (when the size is fixed) or
> "gns^@" (when the size changes).

Attached is an updated patch.

regards,
Christian
-- 
Wir wissen zwar nicht was wir wollen, aber das mit ganzer Kraft.

-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
diff --git a/runtime/doc/index.txt b/runtime/doc/index.txt
--- a/runtime/doc/index.txt
+++ b/runtime/doc/index.txt
@@ -719,6 +719,8 @@
 |gH|		gH		   start Select line mode
 |gI|		gI		2  like "I", but always start in column 1
 |gJ|		gJ		2  join lines without inserting space
+|gN|		gN	      1,2  Visually select the last used search
+				    pattern in reverse direction
 |gP|		["x]gP		2  put the text [from register x] before the
 				   cursor N times, leave the cursor after it
 |gQ|		gQ		    switch to "Ex" mode with Vim editing
@@ -751,6 +753,8 @@
 				   lines down
 |gk|		gk		1  like "k", but when 'wrap' on go N screen
 				   lines up
+|gn|		gn	      1,2  Visually select the last used search
+				    pattern in forward direction
 |gm|		gm		1  go to character at middle of the screenline
 |go|		go		1  cursor to byte N in the buffer
 |gp|		["x]gp		2  put the text [from register x] after the
diff --git a/runtime/doc/visual.txt b/runtime/doc/visual.txt
--- a/runtime/doc/visual.txt
+++ b/runtime/doc/visual.txt
@@ -99,6 +99,16 @@
 			After using "p" or "P" in Visual mode the text that
 			was put will be selected.
 
+								*gn* *v_gn*
+gn			Start Visual mode and select the next search match
+			from the last search pattern in forward direction.
+			If the cursor is on the match, visually selects it.
+			If Visual mode is active, extends the selection
+			until the end of the next match.
+
+								*gN* *v_gN*
+gN			Like |gn| but selects the previous match.
+
 							*<LeftMouse>*
 <LeftMouse>		Set the current cursor position.  If Visual mode is
 			active it is stopped.  Only when 'mouse' option is
diff --git a/src/normal.c b/src/normal.c
--- a/src/normal.c
+++ b/src/normal.c
@@ -1780,10 +1780,15 @@
 	    {
 		/* Prepare for redoing.  Only use the nchar field for "r",
 		 * otherwise it might be the second char of the operator. */
-		prep_redo(oap->regname, 0L, NUL, 'v',
-				get_op_char(oap->op_type),
-				get_extra_op_char(oap->op_type),
-				oap->op_type == OP_REPLACE ? cap->nchar : NUL);
+		if (cap->cmdchar != 'g' && cap->nchar != 'n' && cap->nchar != 'N')
+		    prep_redo(oap->regname, 0L, NUL, 'v',
+					get_op_char(oap->op_type),
+					get_extra_op_char(oap->op_type),
+					oap->op_type == OP_REPLACE ? cap->nchar : NUL);
+		else
+		    prep_redo(oap->regname, 0L, NUL, cap->cmdchar, cap->nchar,
+					get_op_char(oap->op_type),
+					get_extra_op_char(oap->op_type));
 		if (!redo_VIsual_busy)
 		{
 		    redo_VIsual_mode = resel_VIsual_mode;
@@ -7987,6 +7992,17 @@
 	cap->arg = TRUE;
 	nv_visual(cap);
 	break;
+
+    /* "gn", "gN" visually select next/previous search match
+     * "gn" selects next match
+     * "gN" selects previous match
+     */
+    case 'N':
+    case 'n':
+	if (!current_search(cap->count1, cap->nchar == 'n'))
+	    beep_flush();
+
+	break;
 #endif /* FEAT_VISUAL */
 
     /*
@@ -9211,7 +9227,7 @@
 		flag = current_quote(cap->oap, cap->count1, include,
 								  cap->nchar);
 		break;
-#if 0	/* TODO */
+#if 0  /* TODO */
 	case 'S': /* "aS" = a section */
 	case 'f': /* "af" = a filename */
 	case 'u': /* "au" = a URL */
diff --git a/src/proto/search.pro b/src/proto/search.pro
--- a/src/proto/search.pro
+++ b/src/proto/search.pro
@@ -27,6 +27,7 @@
 int end_word __ARGS((long count, int bigword, int stop, int empty));
 int bckend_word __ARGS((long count, int bigword, int eol));
 int current_word __ARGS((oparg_T *oap, long count, int include, int bigword));
+int current_search __ARGS((long count, int forward));
 int current_sent __ARGS((oparg_T *oap, long count, int include));
 int current_block __ARGS((oparg_T *oap, long count, int include, int what, int other));
 int current_tagblock __ARGS((oparg_T *oap, long count_arg, int include));
diff --git a/src/search.c b/src/search.c
--- a/src/search.c
+++ b/src/search.c
@@ -3397,6 +3397,151 @@
     return OK;
 }
 
+#ifdef FEAT_VISUAL
+/*
+ * Find next search match under cursor, cursor at end.
+ * Used while an operator is pending, and in Visual mode.
+ * TODO: redo only works, when used in operator pending mode
+ */
+    int
+current_search(count, forward)
+    long	count;
+    int		forward;	/* move forward or backwards */
+{
+    pos_T	start_pos;      /* position before the pattern */
+    pos_T       orig_pos;       /* position of the cursor at beginning */
+    pos_T       pos;            /* position after the pattern */
+    int         i;
+    int         result;         /* result of various function calls */
+    char_u      old_p_ws = p_ws;
+    int         visual_active = FALSE;
+    int         flags = 0;
+    pos_T	save_VIsual;
+
+
+    /* wrapping should not occur */
+    p_ws = FALSE;
+
+    /* Correct cursor when 'selection' is exclusive */
+    if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
+	dec_cursor();
+
+    if (VIsual_active)
+    {
+	orig_pos = curwin->w_cursor;
+	save_VIsual = VIsual;
+	visual_active = TRUE;
+
+	/* just started visual selection */
+	if (equalpos(VIsual, curwin->w_cursor))
+	    visual_active = FALSE;
+
+	pos = curwin->w_cursor;
+	start_pos = VIsual;
+
+	/* make sure, searching further will extend the match */
+	if (VIsual_active)
+	{
+	    if (forward)
+		incl(&pos);
+	    else
+		decl(&pos);
+	}
+    }
+    else
+	orig_pos = pos = start_pos = curwin->w_cursor;
+
+    /* The trick is to first search backwards and then search forward again,
+     * so that a match at the current cursor position will be correctly
+     * captured.
+     */
+    for (i = 0; i < 2; i++)
+    {
+	if (i && count == 1)
+	    flags = SEARCH_START;
+
+	if (forward)
+	    result = searchit(curwin, curbuf, &pos, (i ? FORWARD : BACKWARD),
+		    spats[last_idx].pat, (long) (i ? count : 1), SEARCH_KEEP |
+		    flags | (i ? 0 : SEARCH_END), RE_SEARCH, 0, NULL);
+	else
+	    result = searchit(curwin, curbuf, &pos, (i ? BACKWARD : FORWARD),
+		    spats[last_idx].pat, (long) (i ? count : 1),
+		    SEARCH_KEEP | flags | (i ? SEARCH_END : 0),
+		    RE_SEARCH, 0, NULL);
+
+	/* first search may fail, but then start searching from the
+	 * 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 */
+	{
+	    curwin->w_cursor = orig_pos;
+	    if (VIsual_active)
+		VIsual = save_VIsual;
+	    p_ws = old_p_ws;
+	    return FAIL;
+	}
+	else if (!i && !result && !visual_active)
+	{
+	    if (forward) /* try again from start of buffer */
+	    {
+		clearpos(&pos);
+	    }
+	    else /* try again from end of buffer */
+	    {
+		/* searching backwards, so set pos to last line and col */
+		pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
+		pos.col  = STRLEN(ml_get(
+			    curwin->w_buffer->b_ml.ml_line_count));
+	    }
+	}
+
+    }
+
+    start_pos = pos;
+    flags = (forward ? SEARCH_END : 0);
+
+    /* move to match */
+    result = searchit(curwin, curbuf, &pos, (forward ? FORWARD : BACKWARD),
+	    spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0, NULL);
+
+    if (!VIsual_active)
+	VIsual = start_pos;
+
+    p_ws = old_p_ws;
+    curwin->w_cursor = pos;
+    VIsual_active = TRUE;
+    VIsual_mode = 'v';
+
+    if (VIsual_active)
+    {
+	redraw_curbuf_later(INVERTED);	/* update the inversion */
+	if (*p_sel == 'e' && ltoreq(VIsual, curwin->w_cursor))
+	    inc_cursor();
+    }
+
+#ifdef FEAT_FOLDING
+    if (fdo_flags & FDO_SEARCH && KeyTyped)
+	foldOpenCursor();
+#endif
+
+    may_start_select('c');
+#ifdef FEAT_MOUSE
+    setmouse();
+#endif
+#ifdef FEAT_CLIPBOARD
+/* Make sure the clipboard gets updated.  Needed because start and
+ * end are still the same, and the selection needs to be owned */
+    clip_star.vmode = NUL;
+#endif
+    redraw_curbuf_later(INVERTED);
+    showmode();
+
+    return OK;
+}
+#endif /* FEAT_VISUAL */
+
 /*
  * Find sentence(s) under the cursor, cursor at end.
  * When Visual active, extend it by one or more sentences.
diff --git a/src/testdir/test53.in b/src/testdir/test53.in
--- a/src/testdir/test53.in
+++ b/src/testdir/test53.in
@@ -28,6 +28,11 @@
 :put =matchstr(\"abcd\", \".\", 0, -1) " a
 :put =match(\"abcd\", \".\", 0, 5) " -1
 :put =match(\"abcd\", \".\", 0, -1) " 0
+/^foobar
+gncsearchmatch/one\_s*two\_s
+:1
gnd
+/[a]bcdx
+:1
2gnd
 :/^start:/,/^end:/wq! test.out
 ENDTEST
 
@@ -45,4 +50,9 @@
 -<b>asdf<i>Xasdf</i>asdf</b>-
 -<b>asdX<i>as<b />df</i>asdf</b>-
 </begin>
+SEARCH:
+foobar
+one
+two
+abcdx | abcdx | abcdx
 end:
diff --git a/src/testdir/test53.ok b/src/testdir/test53.ok
--- a/src/testdir/test53.ok
+++ b/src/testdir/test53.ok
@@ -18,4 +18,7 @@
 a
 -1
 0
+SEARCH:
+searchmatch
+abcdx |  | abcdx
 end:

Raspunde prin e-mail lui