# HG changeset patch # User Christian Brabandt # Date 1527004807 -7200 # Node ID 96933f0ecbd1941b849ff3c048cfed7b63e0ae55 # Parent f4d1227b4b207e42d2420e16eddf5103fcbffa8a patch 8.1.0018: using "gn" may select wrong text when wrapping commit https://github.com/vim/vim/commit/bdb657924d73c98b0ab28411749571e893b699a9 Author: Bram Moolenaar Date: Tue May 22 17:50:42 2018 +0200 patch 8.1.0018: using "gn" may select wrong text when wrapping Problem: Using "gn" may select wrong text when wrapping. Solution: Avoid wrapping when searching forward. (Christian Brabandt) diff --git a/src/search.c b/src/search.c --- a/src/search.c +++ b/src/search.c @@ -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; diff --git a/src/testdir/test_gn.vim b/src/testdir/test_gn.vim --- a/src/testdir/test_gn.vim +++ b/src/testdir/test_gn.vim @@ -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'\.." 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\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\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 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -762,6 +762,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 18, +/**/ 17, /**/ 16,