]> granicus.if.org Git - vim/commitdiff
updated for version 7.3.610 v7.3.610
authorBram Moolenaar <Bram@vim.org>
Wed, 25 Jul 2012 13:06:34 +0000 (15:06 +0200)
committerBram Moolenaar <Bram@vim.org>
Wed, 25 Jul 2012 13:06:34 +0000 (15:06 +0200)
Problem:    Cannot operate on the text that a search pattern matches.
Solution:   Add the "gn" and "gN" commands. (Christian Brabandt)

runtime/doc/index.txt
runtime/doc/visual.txt
src/normal.c
src/proto/search.pro
src/search.c
src/testdir/test53.in
src/testdir/test53.ok
src/version.c

index c01f1d03142f81889e9a724e19180c7b33cb48e8..33806bbdbbadb576c4cf71c07a206c5927f2dc15 100644 (file)
@@ -751,6 +751,8 @@ tag         char          note action in Normal mode        ~
                                   lines down
 |gk|           gk              1  like "k", but when 'wrap' on go N screen
                                   lines up
+|gn|           gn            1,2  find the next match with the last used
+                                  search pattern and Visually select it
 |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
index 4457aeb0187cc05491c3c7a264ba5ee894477c84..74727ec13cf9338627131e5464607576ef0e243f 100644 (file)
@@ -99,6 +99,18 @@ gv                   Start Visual mode with the same area as the previous
                        After using "p" or "P" in Visual mode the text that
                        was put will be selected.
 
+                                                               *gn* *v_gn*
+gn                     Search forward for the last used search pattern, like
+                       with `n`, and start Visual mode to select the match.
+                       If the cursor is on the match, visually selects it.
+                       If an operator is pending, operates on the match.
+                       E.g., "dgn" deletes the text of the next match.
+                       If Visual mode is active, extends the selection
+                       until the end of the next match.
+
+                                                               *gN* *v_gN*
+gN                     Like |gn| but searches backward, like with `N`.
+
                                                        *<LeftMouse>*
 <LeftMouse>            Set the current cursor position.  If Visual mode is
                        active it is stopped.  Only when 'mouse' option is
index 66654f8d793f406973deb44af034b33040ea88c2..0791b1a38db14d2e2a20dd7da09d89074a0c927d 100644 (file)
@@ -1780,10 +1780,18 @@ do_pending_operator(cap, old_col, gui_yank)
            {
                /* 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'))
+                   /* "gn" and "gN" are a bit different */
+                   prep_redo(oap->regname, 0L, NUL, cap->cmdchar, cap->nchar,
+                                       get_op_char(oap->op_type),
+                                       get_extra_op_char(oap->op_type));
+               else
+                   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 (!redo_VIsual_busy)
                {
                    redo_VIsual_mode = resel_VIsual_mode;
@@ -7987,6 +7995,17 @@ nv_g_cmd(cap)
        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 */
 
     /*
index 7f3947ca601f17e651819209f8b0a4fc620f6ff2..9b1fc63962213c29972ce81e9d728001286c68a2 100644 (file)
@@ -27,6 +27,7 @@ int bck_word __ARGS((long count, int bigword, int stop));
 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));
index 9f9f0c1c21e695aa4cc045f0a7ee703b338b3133..5db1d578bc5ca2a15775a4401a96a36a9664d461 100644 (file)
@@ -3397,6 +3397,151 @@ current_word(oap, count, include, bigword)
     return OK;
 }
 
+#if defined(FEAT_VISUAL) || defined(PROTO)
+/*
+ * 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                dir;
+    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, only one character */
+       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)
+           dir = i;
+       else
+           dir = !i;
+       result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
+               spats[last_idx].pat, (long) (i ? count : 1),
+               SEARCH_KEEP | flags | (dir ? 0 : SEARCH_END),
+               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.
@@ -3420,7 +3565,7 @@ current_sent(oap, count, include)
 
 #ifdef FEAT_VISUAL
     /*
-     * When visual area is bigger than one character: Extend it.
+     * When the Visual area is bigger than one character: Extend it.
      */
     if (VIsual_active && !equalpos(start_pos, VIsual))
     {
@@ -3508,8 +3653,8 @@ extend:
 #endif
 
     /*
-     * If cursor started on blank, check if it is just before the start of the
-     * next sentence.
+     * If the cursor started on a blank, check if it is just before the start
+     * of the next sentence.
      */
     while (c = gchar_pos(&pos), vim_iswhite(c))        /* vim_iswhite() is a macro */
        incl(&pos);
@@ -3558,7 +3703,7 @@ extend:
 #ifdef FEAT_VISUAL
     if (VIsual_active)
     {
-       /* avoid getting stuck with "is" on a single space before a sent. */
+       /* Avoid getting stuck with "is" on a single space before a sentence. */
        if (equalpos(start_pos, curwin->w_cursor))
            goto extend;
        if (*p_sel == 'e')
index c2023e97b86c02b37e6b79039a578e142def6421..cc333c7a1f3afbe74026c8bf8e43d475eb429ee1 100644 (file)
@@ -28,6 +28,13 @@ fXdat
 :put =matchstr(\"abcd\", \".\", 0, -1) " a
 :put =match(\"abcd\", \".\", 0, 5) " -1
 :put =match(\"abcd\", \".\", 0, -1) " 0
+/^foobar
+gncsearchmatch\e/one\_s*two\_s
+:1
+gnd
+/[a]bcdx
+:1
+2gnd
 :/^start:/,/^end:/wq! test.out
 ENDTEST
 
@@ -45,4 +52,9 @@ voo "nah" sdf " asdf" sdf " sdf" sd
 -<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:
index c371c6ad898ac41ec9bc5e1a079f94bd3ade3fbf..4f1e5a671e366112ac7d90d8193136a9ba07d02f 100644 (file)
@@ -18,4 +18,7 @@ c
 a
 -1
 0
+SEARCH:
+searchmatch
+abcdx |  | abcdx
 end:
index 397d9b09d3e188bfcdf481787130fadc5a75854e..9ce83e767f2fdeabf6b4a55b8258e99f3fab8a48 100644 (file)
@@ -714,6 +714,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    610,
 /**/
     609,
 /**/