]> granicus.if.org Git - vim/commitdiff
patch 8.1.0018: using "gn" may select wrong text when wrapping v8.1.0018
authorBram Moolenaar <Bram@vim.org>
Tue, 22 May 2018 15:50:42 +0000 (17:50 +0200)
committerBram Moolenaar <Bram@vim.org>
Tue, 22 May 2018 15:50:42 +0000 (17:50 +0200)
Problem:    Using "gn" may select wrong text when wrapping.
Solution:   Avoid wrapping when searching forward. (Christian Brabandt)

src/search.c
src/testdir/test_gn.vim
src/version.c

index 726013e6207dee4dde0391834b1a4959197ff8e6..57434ec0b71723353199a9a90f1c82762e3a06ef 100644 (file)
@@ -4665,6 +4665,7 @@ current_search(
 {
     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 */
     int                i;
     int                dir;
@@ -4758,6 +4759,8 @@ current_search(
                                ml_get(curwin->w_buffer->b_ml.ml_line_count));
            }
        }
+       if (i == 0)
+           first_match = pos;
        p_ws = old_p_ws;
     }
 
@@ -4774,9 +4777,25 @@ current_search(
     /* move to match, except for zero-width matches, in which case, we are
      * already on the next match */
     if (!one_char)
-       result = searchit(curwin, curbuf, &pos, direction,
+    {
+       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;
index 7a77d9d88bb2d6a324db780463eac621762f63b1..6d48915c159c3fcedb323d571b4c6b8021a26210 100644 (file)
@@ -4,51 +4,51 @@ func Test_gn_command()
   noautocmd new
   " replace a single char by itsself quoted:
   call setline('.', 'abc x def x ghi x jkl')
-  let @/='x'
+  let @/ = 'x'
   exe "norm! cgn'x'\<esc>.."
   call assert_equal("abc 'x' def 'x' ghi 'x' jkl", getline('.'))
   sil! %d_
 
   " simple search match
   call setline('.', 'foobar')
-  let @/='foobar'
+  let @/ = 'foobar'
   exe "norm! gncsearchmatch"
   call assert_equal('searchmatch', getline('.'))
   sil! %d _
 
   " replace a multi-line match
   call setline('.', ['', 'one', 'two'])
-  let @/='one\_s*two\_s'
+  let @/ = 'one\_s*two\_s'
   exe "norm! gnceins\<CR>zwei"
   call assert_equal(['','eins','zwei'], getline(1,'$'))
   sil! %d _
 
   " test count argument
   call setline('.', ['', 'abcdx | abcdx | abcdx'])
-  let @/='[a]bcdx'
+  let @/ = '[a]bcdx'
   exe "norm! 2gnd"
   call assert_equal(['','abcdx |  | abcdx'], getline(1,'$'))
   sil! %d _
 
   " join lines
   call setline('.', ['join ', 'lines'])
-  let @/='$'
+  let @/ = '$'
   exe "norm! 0gnd"
   call assert_equal(['join lines'], getline(1,'$'))
   sil! %d _
 
   " zero-width match
   call setline('.', ['', 'zero width pattern'])
-  let @/='\>\zs'
+  let @/ = '\>\zs'
   exe "norm! 0gnd"
   call assert_equal(['', 'zerowidth pattern'], getline(1,'$'))
   sil! %d _
 
   " delete first and last chars
   call setline('.', ['delete first and last chars'])
-  let @/='^'
+  let @/ = '^'
   exe "norm! 0gnd$"
-  let @/='\zs'
+  let @/ = '\zs'
   exe "norm! gnd"
   call assert_equal(['elete first and last char'], getline(1,'$'))
   sil! %d _
@@ -61,14 +61,14 @@ func Test_gn_command()
 
   " backwards search
   call setline('.', ['my very excellent mother just served us nachos'])
-  let @/='mother'
+  let @/ = 'mother'
   exe "norm! $cgNmongoose"
   call assert_equal(['my very excellent mongoose just served us nachos'], getline(1,'$'))
   sil! %d _
 
   " search for single char
   call setline('.', ['','for (i=0; i<=10; i++)'])
-  let @/='i'
+  let @/ = 'i'
   exe "norm! cgnj"
   call assert_equal(['','for (j=0; i<=10; i++)'], getline(1,'$'))
   sil! %d _
@@ -76,28 +76,28 @@ func Test_gn_command()
   " search hex char
   call setline('.', ['','Y'])
   set noignorecase
-  let @/='\%x59'
+  let @/ = '\%x59'
   exe "norm! gnd"
   call assert_equal(['',''], getline(1,'$'))
   sil! %d _
 
   " test repeating gdn
   call setline('.', ['', '1', 'Johnny', '2', 'Johnny', '3'])
-  let @/='Johnny'
+  let @/ = 'Johnny'
   exe "norm! dgn."
   call assert_equal(['','1', '', '2', '', '3'], getline(1,'$'))
   sil! %d _
 
   " test repeating gUgn
   call setline('.', ['', '1', 'Depp', '2', 'Depp', '3'])
-  let @/='Depp'
+  let @/ = 'Depp'
   exe "norm! gUgn."
   call assert_equal(['', '1', 'DEPP', '2', 'DEPP', '3'], getline(1,'$'))
   sil! %d _
 
   " test using look-ahead assertions
   call setline('.', ['a:10', '', 'a:1', '', 'a:20'])
-  let @/='a:0\@!\zs\d\+'
+  let @/ = 'a:0\@!\zs\d\+'
   exe "norm! 2nygno\<esc>p"
   call assert_equal(['a:10', '', 'a:1', '1', '', 'a:20'], getline(1,'$'))
   sil! %d _
@@ -113,12 +113,21 @@ func Test_gn_command()
   " search upwards with nowrapscan set
   call setline('.', ['foo', 'bar', 'foo', 'baz'])
   set nowrapscan
-  let @/='foo'
+  let @/ = 'foo'
   $
   norm! dgN
   call assert_equal(['foo', 'bar', '', 'baz'], getline(1,'$'))
   sil! %d_
 
+  " search using the \zs atom
+  call setline(1, [' nnoremap', '' , 'nnoremap'])
+  set wrapscan&vim
+  let @/ = '\_s\zsnnoremap'
+  $
+  norm! cgnmatch
+  call assert_equal([' nnoremap', '', 'match'], getline(1,'$'))
+  sil! %d_
+
   set wrapscan&vim
 endfu
 
index 4e4c1fbee38c1e7d064711ce6659fcb867b2e8f1..1f8cfefb0cc2b65587dc1d8c61db15596a79ff05 100644 (file)
@@ -761,6 +761,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    18,
 /**/
     17,
 /**/