Skip to content

Commit add8dce

Browse files
committed
patch 8.0.0627: "gn" selects only one character with 'nowrapscan'
Problem: When 'wrapscan' is off "gn" does not select the whole pattern when it's the last one in the text. (KeyboardFire) Solution: Check if the search fails. (Christian Brabandt, closes #1683)
1 parent e21d69e commit add8dce

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/search.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,7 +4599,7 @@ current_quote(
45994599

46004600
#endif /* FEAT_TEXTOBJ */
46014601

4602-
static int is_one_char(char_u *pattern, int move);
4602+
static int is_one_char(char_u *pattern, int move, pos_T *cur);
46034603

46044604
/*
46054605
* Find next search match under cursor, cursor at end.
@@ -4647,7 +4647,7 @@ current_search(
46474647
orig_pos = pos = curwin->w_cursor;
46484648

46494649
/* Is the pattern is zero-width? */
4650-
one_char = is_one_char(spats[last_idx].pat, TRUE);
4650+
one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor);
46514651
if (one_char == -1)
46524652
{
46534653
p_ws = old_p_ws;
@@ -4710,7 +4710,10 @@ current_search(
47104710

47114711
/* Check again from the current cursor position,
47124712
* since the next match might actually by only one char wide */
4713-
one_char = is_one_char(spats[last_idx].pat, FALSE);
4713+
one_char = is_one_char(spats[last_idx].pat, FALSE, &pos);
4714+
if (one_char < 0)
4715+
/* search failed, abort */
4716+
return FAIL;
47144717

47154718
/* move to match, except for zero-width matches, in which case, we are
47164719
* already on the next match */
@@ -4761,12 +4764,12 @@ current_search(
47614764

47624765
/*
47634766
* Check if the pattern is one character long or zero-width.
4764-
* If move is TRUE, check from the beginning of the buffer, else from the
4765-
* current cursor position.
4767+
* If move is TRUE, check from the beginning of the buffer, else from position
4768+
* "cur".
47664769
* Returns TRUE, FALSE or -1 for failure.
47674770
*/
47684771
static int
4769-
is_one_char(char_u *pattern, int move)
4772+
is_one_char(char_u *pattern, int move, pos_T *cur)
47704773
{
47714774
regmmatch_T regmatch;
47724775
int nmatched = 0;
@@ -4791,7 +4794,7 @@ is_one_char(char_u *pattern, int move)
47914794
}
47924795
else
47934796
{
4794-
pos = curwin->w_cursor;
4797+
pos = *cur;
47954798
/* accept a match at the cursor position */
47964799
flag = SEARCH_START;
47974800
}

src/testdir/test_gn.vim

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
11
" Test for gn command
22

33
func Test_gn_command()
4-
noa new
4+
set belloff=all
5+
noautocmd new
56
" replace a single char by itsself quoted:
67
call setline('.', 'abc x def x ghi x jkl')
78
let @/='x'
89
exe "norm! cgn'x'\<esc>.."
910
call assert_equal("abc 'x' def 'x' ghi 'x' jkl", getline('.'))
1011
sil! %d_
12+
1113
" simple search match
1214
call setline('.', 'foobar')
1315
let @/='foobar'
1416
exe "norm! gncsearchmatch"
1517
call assert_equal('searchmatch', getline('.'))
1618
sil! %d _
19+
1720
" replace a multi-line match
1821
call setline('.', ['', 'one', 'two'])
1922
let @/='one\_s*two\_s'
2023
exe "norm! gnceins\<CR>zwei"
2124
call assert_equal(['','eins','zwei'], getline(1,'$'))
2225
sil! %d _
26+
2327
" test count argument
2428
call setline('.', ['', 'abcdx | abcdx | abcdx'])
2529
let @/='[a]bcdx'
2630
exe "norm! 2gnd"
2731
call assert_equal(['','abcdx | | abcdx'], getline(1,'$'))
2832
sil! %d _
33+
2934
" join lines
3035
call setline('.', ['join ', 'lines'])
3136
let @/='$'
3237
exe "norm! 0gnd"
3338
call assert_equal(['join lines'], getline(1,'$'))
3439
sil! %d _
40+
3541
" zero-width match
3642
call setline('.', ['', 'zero width pattern'])
3743
let @/='\>\zs'
3844
exe "norm! 0gnd"
3945
call assert_equal(['', 'zerowidth pattern'], getline(1,'$'))
4046
sil! %d _
47+
4148
" delete first and last chars
4249
call setline('.', ['delete first and last chars'])
4350
let @/='^'
@@ -46,48 +53,66 @@ func Test_gn_command()
4653
exe "norm! gnd"
4754
call assert_equal(['elete first and last char'], getline(1,'$'))
4855
sil! %d _
56+
4957
" using visual mode
5058
call setline('.', ['', 'uniquepattern uniquepattern'])
5159
exe "norm! /[u]niquepattern/s\<cr>vlgnd"
5260
call assert_equal(['', ' uniquepattern'], getline(1,'$'))
5361
sil! %d _
62+
5463
" backwards search
5564
call setline('.', ['my very excellent mother just served us nachos'])
5665
let @/='mother'
5766
exe "norm! $cgNmongoose"
5867
call assert_equal(['my very excellent mongoose just served us nachos'], getline(1,'$'))
5968
sil! %d _
69+
6070
" search for single char
6171
call setline('.', ['','for (i=0; i<=10; i++)'])
6272
let @/='i'
6373
exe "norm! cgnj"
6474
call assert_equal(['','for (j=0; i<=10; i++)'], getline(1,'$'))
6575
sil! %d _
76+
6677
" search hex char
6778
call setline('.', ['','Y'])
6879
set noignorecase
6980
let @/='\%x59'
7081
exe "norm! gnd"
7182
call assert_equal(['',''], getline(1,'$'))
7283
sil! %d _
84+
7385
" test repeating gdn
7486
call setline('.', ['', '1', 'Johnny', '2', 'Johnny', '3'])
7587
let @/='Johnny'
7688
exe "norm! dgn."
7789
call assert_equal(['','1', '', '2', '', '3'], getline(1,'$'))
7890
sil! %d _
91+
7992
" test repeating gUgn
8093
call setline('.', ['', '1', 'Depp', '2', 'Depp', '3'])
8194
let @/='Depp'
8295
exe "norm! gUgn."
8396
call assert_equal(['', '1', 'DEPP', '2', 'DEPP', '3'], getline(1,'$'))
8497
sil! %d _
98+
8599
" test using look-ahead assertions
86100
call setline('.', ['a:10', '', 'a:1', '', 'a:20'])
87101
let @/='a:0\@!\zs\d\+'
88102
exe "norm! 2nygno\<esc>p"
89103
call assert_equal(['a:10', '', 'a:1', '1', '', 'a:20'], getline(1,'$'))
90104
sil! %d _
105+
106+
" test using nowrapscan
107+
set nowrapscan
108+
call setline(1, 'foo bar baz')
109+
exe "norm! /bar/e\<cr>"
110+
exe "norm! gnd"
111+
call assert_equal(['foo baz'], getline(1,'$'))
112+
sil! %d_
113+
114+
set wrapscan&vim
115+
set belloff&vim
91116
endfu
92117

93118
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
627,
767769
/**/
768770
626,
769771
/**/

0 commit comments

Comments
 (0)