0
0
mirror of https://github.com/vim/vim.git synced 2025-09-26 04:04:07 -04:00

patch 8.2.2295: incsearch does not detect empty pattern properly

Problem:    Incsearch does not detect empty pattern properly.
Solution:   Return magic state when skipping over a pattern. (Christian
            Brabandt, closes #7612, closes #6420)
This commit is contained in:
Bram Moolenaar
2021-01-04 12:42:13 +01:00
parent 82c38fe508
commit d93a7fc1a9
15 changed files with 125 additions and 40 deletions

View File

@@ -52,6 +52,9 @@ static void restore_cmdline(cmdline_info_T *ccp);
static int cmdline_paste(int regname, int literally, int remcr);
static void redrawcmdprompt(void);
static int ccheck_abbr(int);
#ifdef FEAT_SEARCH_EXTRA
static int empty_pattern_magic(char_u *pat, size_t len, magic_T magic_val);
#endif
#ifdef FEAT_CMDWIN
static int open_cmdwin(void);
@@ -89,15 +92,34 @@ abandon_cmdline(void)
* as a trailing \|, which can happen while typing a pattern.
*/
static int
empty_pattern(char_u *p)
empty_pattern(char_u *p, int delim)
{
size_t n = STRLEN(p);
size_t n = STRLEN(p);
magic_T magic_val = MAGIC_ON;
if (n > 0)
(void) skip_regexp_ex(p, delim, magic_isset(), NULL, NULL, &magic_val);
else
return TRUE;
return empty_pattern_magic(p, n, magic_val);
}
static int
empty_pattern_magic(char_u *p, size_t len, magic_T magic_val)
{
// remove trailing \v and the like
while (n >= 2 && p[n - 2] == '\\'
&& vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
n -= 2;
return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
while (len >= 2 && p[len - 2] == '\\'
&& vim_strchr((char_u *)"mMvVcCZ", p[len - 1]) != NULL)
len -= 2;
// true, if the pattern is empty, or the pattern ends with \| and magic is
// set (or it ends with '|' and very magic is set)
return len == 0 || (len > 1
&& ((p[len - 2] == '\\'
&& p[len - 1] == '|' && magic_val == MAGIC_ON)
|| (p[len - 2] != '\\'
&& p[len - 1] == '|' && magic_val == MAGIC_ALL)));
}
// Struct to store the viewstate during 'incsearch' highlighting.
@@ -149,7 +171,7 @@ typedef struct {
pos_T match_end;
int did_incsearch;
int incsearch_postponed;
magic_T magic_overruled_save;
optmagic_T magic_overruled_save;
} incsearch_state_T;
static void
@@ -207,6 +229,7 @@ do_incsearch_highlighting(
pos_T save_cursor;
int use_last_pat;
int retval = FALSE;
magic_T magic = 0;
*skiplen = 0;
*patlen = ccline.cmdlen;
@@ -252,9 +275,9 @@ do_incsearch_highlighting(
|| STRNCMP(cmd, "vglobal", p - cmd) == 0)
{
if (*cmd == 's' && cmd[1] == 'm')
magic_overruled = MAGIC_ON;
magic_overruled = OPTION_MAGIC_ON;
else if (*cmd == 's' && cmd[1] == 'n')
magic_overruled = MAGIC_OFF;
magic_overruled = OPTION_MAGIC_OFF;
}
else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
{
@@ -288,7 +311,7 @@ do_incsearch_highlighting(
p = skipwhite(p);
delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
*search_delim = delim;
end = skip_regexp(p, delim, magic_isset());
end = skip_regexp_ex(p, delim, magic_isset(), NULL, NULL, &magic);
use_last_pat = end == p && *end == delim;
@@ -302,7 +325,7 @@ do_incsearch_highlighting(
int empty;
*end = NUL;
empty = empty_pattern(p);
empty = empty_pattern_magic(p, STRLEN(p), magic);
*end = c;
if (empty)
goto theend;
@@ -535,7 +558,8 @@ may_do_incsearch_highlighting(
{
next_char = ccline.cmdbuff[skiplen + patlen];
ccline.cmdbuff[skiplen + patlen] = NUL;
if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
if (empty_pattern(ccline.cmdbuff + skiplen, search_delim)
&& !no_hlsearch)
{
redraw_all_later(SOME_VALID);
set_no_hlsearch(TRUE);