0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

patch 8.1.0629: "gn" selects the wrong text with a multi-line match

Problem:    "gn" selects the wrong text with a multi-line match.
Solution:   Get the end position from searchit() directly. (closes #3695)
This commit is contained in:
Bram Moolenaar
2018-12-23 19:10:09 +01:00
parent c33181c44c
commit 5d24a2257e
9 changed files with 60 additions and 54 deletions

View File

@@ -4653,7 +4653,7 @@ ins_compl_get_exp(pos_T *ini)
found_new_match = search_for_exact_line(ins_buf, pos, found_new_match = search_for_exact_line(ins_buf, pos,
compl_direction, compl_pattern); compl_direction, compl_pattern);
else else
found_new_match = searchit(NULL, ins_buf, pos, found_new_match = searchit(NULL, ins_buf, pos, NULL,
compl_direction, compl_direction,
compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
RE_LAST, (linenr_T)0, NULL, NULL); RE_LAST, (linenr_T)0, NULL, NULL);

View File

@@ -10056,7 +10056,7 @@ search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
} }
pos = save_cursor = curwin->w_cursor; pos = save_cursor = curwin->w_cursor;
subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L, subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL); options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
if (subpatnum != FAIL) if (subpatnum != FAIL)
{ {
@@ -10414,7 +10414,7 @@ do_searchpair(
pat = pat3; pat = pat3;
for (;;) for (;;)
{ {
n = searchit(curwin, curbuf, &pos, dir, pat, 1L, n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
options, RE_SEARCH, lnum_stop, &tm, NULL); options, RE_SEARCH, lnum_stop, &tm, NULL);
if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))) if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
/* didn't find it or found the first match again: FAIL */ /* didn't find it or found the first match again: FAIL */

View File

@@ -4669,7 +4669,7 @@ get_address(
#ifdef FEAT_VIRTUALEDIT #ifdef FEAT_VIRTUALEDIT
pos.coladd = 0; pos.coladd = 0;
#endif #endif
if (searchit(curwin, curbuf, &pos, if (searchit(curwin, curbuf, &pos, NULL,
*cmd == '?' ? BACKWARD : FORWARD, *cmd == '?' ? BACKWARD : FORWARD,
(char_u *)"", 1L, SEARCH_MSG, (char_u *)"", 1L, SEARCH_MSG,
i, (linenr_T)0, NULL, NULL) != FAIL) i, (linenr_T)0, NULL, NULL) != FAIL)

View File

@@ -675,7 +675,7 @@ may_adjust_incsearch_highlighting(
++emsg_off; ++emsg_off;
save = pat[patlen]; save = pat[patlen];
pat[patlen] = NUL; pat[patlen] = NUL;
i = searchit(curwin, curbuf, &t, i = searchit(curwin, curbuf, &t, NULL,
c == Ctrl_G ? FORWARD : BACKWARD, c == Ctrl_G ? FORWARD : BACKWARD,
pat, count, search_flags, pat, count, search_flags,
RE_SEARCH, 0, NULL, NULL); RE_SEARCH, 0, NULL, NULL);

View File

@@ -4338,7 +4338,7 @@ find_decl(
for (;;) for (;;)
{ {
valid = FALSE; valid = FALSE;
t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD, t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL); pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
if (curwin->w_cursor.lnum >= old_pos.lnum) if (curwin->w_cursor.lnum >= old_pos.lnum)
t = FAIL; /* match after start is failure too */ t = FAIL; /* match after start is failure too */

View File

@@ -22,7 +22,7 @@ char_u *last_search_pat(void);
void reset_search_dir(void); void reset_search_dir(void);
void set_last_search_pat(char_u *s, int idx, int magic, int setlast); void set_last_search_pat(char_u *s, int idx, int magic, int setlast);
void last_pat_prog(regmmatch_T *regmatch); void last_pat_prog(regmmatch_T *regmatch);
int searchit(win_T *win, buf_T *buf, pos_T *pos, int dir, char_u *pat, long count, int options, int pat_use, linenr_T stop_lnum, proftime_T *tm, int *timed_out); int searchit(win_T *win, buf_T *buf, pos_T *pos, pos_T *end_pos, int dir, char_u *pat, long count, int options, int pat_use, linenr_T stop_lnum, proftime_T *tm, int *timed_out);
void set_search_direction(int cdir); void set_search_direction(int cdir);
int do_search(oparg_T *oap, int dirc, char_u *pat, long count, int options, proftime_T *tm, int *timed_out); int do_search(oparg_T *oap, int dirc, char_u *pat, long count, int options, proftime_T *tm, int *timed_out);
int search_for_exact_line(buf_T *buf, pos_T *pos, int dir, char_u *pat); int search_for_exact_line(buf_T *buf, pos_T *pos, int dir, char_u *pat);

View File

@@ -610,8 +610,8 @@ last_pat_prog(regmmatch_T *regmatch)
/* /*
* Lowest level search function. * Lowest level search function.
* Search for 'count'th occurrence of pattern 'pat' in direction 'dir'. * Search for 'count'th occurrence of pattern "pat" in direction "dir".
* Start at position 'pos' and return the found position in 'pos'. * Start at position "pos" and return the found position in "pos".
* *
* if (options & SEARCH_MSG) == 0 don't give any messages * if (options & SEARCH_MSG) == 0 don't give any messages
* if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
@@ -634,6 +634,7 @@ searchit(
buffer without a window! */ buffer without a window! */
buf_T *buf, buf_T *buf,
pos_T *pos, pos_T *pos,
pos_T *end_pos, // set to end of the match, unless NULL
int dir, int dir,
char_u *pat, char_u *pat,
long count, long count,
@@ -1035,14 +1036,26 @@ searchit(
} }
#endif #endif
} }
if (end_pos != NULL)
{
end_pos->lnum = lnum + matchpos.lnum;
end_pos->col = matchpos.col;
}
} }
else else
{ {
pos->lnum = lnum + matchpos.lnum; pos->lnum = lnum + matchpos.lnum;
pos->col = matchpos.col; pos->col = matchpos.col;
if (end_pos != NULL)
{
end_pos->lnum = lnum + endpos.lnum;
end_pos->col = endpos.col;
}
} }
#ifdef FEAT_VIRTUALEDIT #ifdef FEAT_VIRTUALEDIT
pos->coladd = 0; pos->coladd = 0;
if (end_pos != NULL)
end_pos->coladd = 0;
#endif #endif
found = 1; found = 1;
first_match = FALSE; first_match = FALSE;
@@ -1496,7 +1509,7 @@ do_search(
lrFswap(searchstr,0); lrFswap(searchstr,0);
#endif #endif
c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD, c = searchit(curwin, curbuf, &pos, NULL, dirc == '/' ? FORWARD : BACKWARD,
searchstr, count, spats[0].off.end + (options & searchstr, count, spats[0].off.end + (options &
(SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
+ SEARCH_MSG + SEARCH_START + SEARCH_MSG + SEARCH_START
@@ -4665,20 +4678,19 @@ static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
int int
current_search( current_search(
long count, long count,
int forward) /* move forward or backwards */ int forward) // TRUE for forward, FALSE for backward
{ {
pos_T start_pos; /* position before the pattern */ pos_T start_pos; // start position of the pattern match
pos_T orig_pos; /* position of the cursor at beginning */ pos_T end_pos; // end position of the pattern match
pos_T first_match; /* position of first match */ pos_T orig_pos; // position of the cursor at beginning
pos_T pos; /* position after the pattern */ pos_T pos; // position after the pattern
int i; int i;
int dir; int dir;
int result; /* result of various function calls */ int result; // result of various function calls
char_u old_p_ws = p_ws; char_u old_p_ws = p_ws;
int flags = 0; int flags = 0;
pos_T save_VIsual = VIsual; pos_T save_VIsual = VIsual;
int one_char; int one_char;
int direction = forward ? FORWARD : BACKWARD;
/* wrapping should not occur */ /* wrapping should not occur */
p_ws = FALSE; p_ws = FALSE;
@@ -4730,8 +4742,10 @@ current_search(
flags = 0; flags = 0;
if (!dir && !one_char) if (!dir && !one_char)
flags = SEARCH_END; flags = SEARCH_END;
end_pos = pos;
result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD), result = searchit(curwin, curbuf, &pos, &end_pos,
(dir ? FORWARD : BACKWARD),
spats[last_idx].pat, (long) (i ? count : 1), spats[last_idx].pat, (long) (i ? count : 1),
SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL); SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
@@ -4739,7 +4753,7 @@ current_search(
* beginning of the file (cursor might be on the search match) * beginning of the file (cursor might be on the search match)
* except when Visual mode is active, so that extending the visual * except when Visual mode is active, so that extending the visual
* selection works. */ * selection works. */
if (!result && i) /* not found, abort */ if (i == 1 && !result) /* not found, abort */
{ {
curwin->w_cursor = orig_pos; curwin->w_cursor = orig_pos;
if (VIsual_active) if (VIsual_active)
@@ -4747,7 +4761,7 @@ current_search(
p_ws = old_p_ws; p_ws = old_p_ws;
return FAIL; return FAIL;
} }
else if (!i && !result) else if (i == 0 && !result)
{ {
if (forward) if (forward)
{ {
@@ -4763,48 +4777,19 @@ current_search(
ml_get(curwin->w_buffer->b_ml.ml_line_count)); ml_get(curwin->w_buffer->b_ml.ml_line_count));
} }
} }
if (i == 0)
first_match = pos;
p_ws = old_p_ws; p_ws = old_p_ws;
} }
start_pos = pos; start_pos = pos;
flags = forward ? SEARCH_END : SEARCH_START;
/* Check again from the current cursor position,
* since the next match might actually by only one char wide */
one_char = is_one_char(spats[last_idx].pat, FALSE, &pos, direction);
if (one_char < 0)
/* search failed, abort */
return FAIL;
/* move to match, except for zero-width matches, in which case, we are
* already on the next match */
if (!one_char)
{
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; p_ws = old_p_ws;
/* not found */
if (!result)
return FAIL;
if (!VIsual_active) if (!VIsual_active)
VIsual = start_pos; VIsual = start_pos;
curwin->w_cursor = pos; // put cursor on last character of match
curwin->w_cursor = end_pos;
if (LT_POS(VIsual, end_pos))
dec_cursor();
VIsual_active = TRUE; VIsual_active = TRUE;
VIsual_mode = 'v'; VIsual_mode = 'v';
@@ -4880,7 +4865,7 @@ is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
flag = SEARCH_START; flag = SEARCH_START;
} }
if (searchit(curwin, curbuf, &pos, direction, pattern, 1, if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL) SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
{ {
/* Zero-width pattern should match somewhere, then we can check if /* Zero-width pattern should match somewhere, then we can check if

View File

@@ -131,4 +131,23 @@ func Test_gn_command()
set wrapscan&vim set wrapscan&vim
endfu endfu
func Test_gn_multi_line()
new
call setline(1, [
\ 'func Tm1()',
\ ' echo "one"',
\ 'endfunc',
\ 'func Tm2()',
\ ' echo "two"',
\ 'endfunc',
\ 'func Tm3()',
\ ' echo "three"',
\ 'endfunc',
\])
/\v^func Tm\d\(\)\n.*\zs".*"\ze$
normal jgnrx
call assert_equal(' echo xxxxx', getline(5))
bwipe!
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab

View File

@@ -799,6 +799,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
629,
/**/ /**/
628, 628,
/**/ /**/