Permalink
Browse files

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)
  • Loading branch information...
brammool committed May 22, 2018
1 parent 62fe66f commit bdb657924d73c98b0ab28411749571e893b699a9
Showing with 46 additions and 16 deletions.
  1. +20 −1 src/search.c
  2. +24 −15 src/testdir/test_gn.vim
  3. +2 −0 src/version.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;
@@ -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,43 +61,43 @@ 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 _
" 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
@@ -761,6 +761,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
18,
/**/
17,
/**/

0 comments on commit bdb6579

Please sign in to comment.