Skip to content

Commit 22ab547

Browse files
committed
patch 8.0.1148: gN doesn't work on last match with 'wrapscan' off
Problem: "gN" doesn't work on last match with 'wrapscan' off. (fcpg) Solution: Adjust for searching backward. (Christian Brabandt)
1 parent 7c456a4 commit 22ab547

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

src/search.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4611,7 +4611,7 @@ current_quote(
46114611

46124612
#endif /* FEAT_TEXTOBJ */
46134613

4614-
static int is_one_char(char_u *pattern, int move, pos_T *cur);
4614+
static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
46154615

46164616
/*
46174617
* Find next search match under cursor, cursor at end.
@@ -4632,6 +4632,7 @@ current_search(
46324632
int flags = 0;
46334633
pos_T save_VIsual = VIsual;
46344634
int one_char;
4635+
int direction = forward ? FORWARD : BACKWARD;
46354636

46364637
/* wrapping should not occur */
46374638
p_ws = FALSE;
@@ -4658,8 +4659,10 @@ current_search(
46584659
else
46594660
orig_pos = pos = curwin->w_cursor;
46604661

4661-
/* Is the pattern is zero-width? */
4662-
one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor);
4662+
/* Is the pattern is zero-width?, this time, don't care about the direction
4663+
*/
4664+
one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4665+
FORWARD);
46634666
if (one_char == -1)
46644667
{
46654668
p_ws = old_p_ws;
@@ -4718,19 +4721,19 @@ current_search(
47184721
}
47194722

47204723
start_pos = pos;
4721-
flags = forward ? SEARCH_END : 0;
4724+
flags = forward ? SEARCH_END : SEARCH_START;
47224725

47234726
/* Check again from the current cursor position,
47244727
* since the next match might actually by only one char wide */
4725-
one_char = is_one_char(spats[last_idx].pat, FALSE, &pos);
4728+
one_char = is_one_char(spats[last_idx].pat, FALSE, &pos, direction);
47264729
if (one_char < 0)
47274730
/* search failed, abort */
47284731
return FAIL;
47294732

47304733
/* move to match, except for zero-width matches, in which case, we are
47314734
* already on the next match */
47324735
if (!one_char)
4733-
result = searchit(curwin, curbuf, &pos, (forward ? FORWARD : BACKWARD),
4736+
result = searchit(curwin, curbuf, &pos, direction,
47344737
spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0,
47354738
NULL, NULL);
47364739

@@ -4779,10 +4782,11 @@ current_search(
47794782
* Check if the pattern is one character long or zero-width.
47804783
* If move is TRUE, check from the beginning of the buffer, else from position
47814784
* "cur".
4785+
* "direction" is FORWARD or BACKWARD.
47824786
* Returns TRUE, FALSE or -1 for failure.
47834787
*/
47844788
static int
4785-
is_one_char(char_u *pattern, int move, pos_T *cur)
4789+
is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
47864790
{
47874791
regmmatch_T regmatch;
47884792
int nmatched = 0;
@@ -4812,7 +4816,7 @@ is_one_char(char_u *pattern, int move, pos_T *cur)
48124816
flag = SEARCH_START;
48134817
}
48144818

4815-
if (searchit(curwin, curbuf, &pos, FORWARD, pattern, 1,
4819+
if (searchit(curwin, curbuf, &pos, direction, pattern, 1,
48164820
SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
48174821
{
48184822
/* Zero-width pattern should match somewhere, then we can check if
@@ -4825,7 +4829,8 @@ is_one_char(char_u *pattern, int move, pos_T *cur)
48254829
pos.lnum, regmatch.startpos[0].col, NULL, NULL);
48264830
if (!nmatched)
48274831
break;
4828-
} while (regmatch.startpos[0].col < pos.col);
4832+
} while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4833+
: regmatch.startpos[0].col > pos.col);
48294834

48304835
if (!called_emsg)
48314836
{

src/testdir/test_gn.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ func Test_gn_command()
111111
call assert_equal(['foo baz'], getline(1,'$'))
112112
sil! %d_
113113

114+
" search upwards with nowrapscan set
115+
call setline('.', ['foo', 'bar', 'foo', 'baz'])
116+
set nowrapscan
117+
let @/='foo'
118+
$
119+
norm! dgN
120+
call assert_equal(['foo', 'bar', '', 'baz'], getline(1,'$'))
121+
sil! %d_
122+
114123
set wrapscan&vim
115124
set belloff&vim
116125
endfu

src/version.c

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

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
1148,
764766
/**/
765767
1147,
766768
/**/

0 commit comments

Comments
 (0)