vim-patch:8.2.2182: Vim9: value of 'magic' is still relevant

Problem:    Vim9: value of 'magic' is still relevant.
Solution:   Always behave like 'magic' is on in Vim9 script (closes vim/vim#7509)

f4e2099e39

EX_NONWHITE_OK is N/A: only applies to Vim9 script.

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq 2022-12-04 08:38:38 +08:00
parent fba0562723
commit a91ba088ab
14 changed files with 69 additions and 43 deletions

View File

@ -29,7 +29,7 @@
#include "nvim/memline_defs.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/option_defs.h"
#include "nvim/option.h"
#include "nvim/os/input.h"
#include "nvim/path.h"
#include "nvim/pos.h"
@ -380,7 +380,7 @@ static void arglist_del_files(garray_T *alist_ga)
if (p == NULL) {
break;
}
regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
if (regmatch.regprog == NULL) {
xfree(p);
break;

View File

@ -2250,7 +2250,7 @@ int buflist_findpat(const char *pattern, const char *pattern_end, bool unlisted,
}
regmatch_T regmatch;
regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
if (regmatch.regprog == NULL) {
xfree(pat);
return -1;

View File

@ -1505,7 +1505,7 @@ static const char *set_context_by_cmdname(const char *cmd, cmdidx_T cmdidx, cons
arg = (const char *)skipwhite(skiptowhite(arg));
if (*arg != NUL) {
xp->xp_context = EXPAND_NOTHING;
arg = (const char *)skip_regexp((char *)arg + 1, (uint8_t)(*arg), p_magic);
arg = (const char *)skip_regexp((char *)arg + 1, (uint8_t)(*arg), magic_isset());
}
}
return (const char *)find_nextcmd(arg);
@ -1544,7 +1544,7 @@ static const char *set_context_by_cmdname(const char *cmd, cmdidx_T cmdidx, cons
if (delim) {
// Skip "from" part.
arg++;
arg = (const char *)skip_regexp((char *)arg, delim, p_magic);
arg = (const char *)skip_regexp((char *)arg, delim, magic_isset());
}
// Skip "to" part.
while (arg[0] != NUL && (uint8_t)arg[0] != delim) {
@ -2458,7 +2458,7 @@ static int ExpandFromContext(expand_T *xp, char *pat, int *num_file, char ***fil
return nlua_expand_pat(xp, pat, num_file, file);
}
regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
if (regmatch.regprog == NULL) {
return FAIL;
}

View File

@ -3131,7 +3131,7 @@ static bool sub_joining_lines(exarg_T *eap, char *pat, const char *sub, const ch
if (save) {
if ((cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0) {
save_re_pat(RE_SUBST, pat, p_magic);
save_re_pat(RE_SUBST, pat, magic_isset());
}
// put pattern in history
add_to_history(HIST_SEARCH, pat, true, NUL);
@ -3356,7 +3356,7 @@ static int do_sub(exarg_T *eap, proftime_T timeout, long cmdpreview_ns, handle_T
which_pat = RE_LAST; // use last used regexp
delimiter = (char_u)(*cmd++); // remember delimiter character
pat = cmd; // remember start of search pat
cmd = skip_regexp_ex(cmd, delimiter, p_magic, &eap->arg, NULL);
cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), &eap->arg, NULL);
if (cmd[0] == delimiter) { // end delimiter found
*cmd++ = NUL; // replace it with a NUL
has_second_delim = true;
@ -3472,7 +3472,7 @@ static int do_sub(exarg_T *eap, proftime_T timeout, long cmdpreview_ns, handle_T
sub = xstrdup(sub);
sub_copy = sub;
} else {
char *newsub = regtilde(sub, p_magic, cmdpreview);
char *newsub = regtilde(sub, magic_isset(), cmdpreview);
if (newsub != sub) {
// newsub was allocated, free it later.
sub_copy = newsub;
@ -3895,7 +3895,8 @@ static int do_sub(exarg_T *eap, proftime_T timeout, long cmdpreview_ns, handle_T
sublen = vim_regsub_multi(&regmatch,
sub_firstlnum - regmatch.startpos[0].lnum,
(char_u *)sub, (char_u *)sub_firstline, 0,
REGSUB_BACKSLASH | (p_magic ? REGSUB_MAGIC : 0));
REGSUB_BACKSLASH
| (magic_isset() ? REGSUB_MAGIC : 0));
textlock--;
// If getting the substitute string caused an error, don't do
@ -3937,7 +3938,8 @@ static int do_sub(exarg_T *eap, proftime_T timeout, long cmdpreview_ns, handle_T
(void)vim_regsub_multi(&regmatch,
sub_firstlnum - regmatch.startpos[0].lnum,
(char_u *)sub, (char_u *)new_end, sublen,
REGSUB_COPY | REGSUB_BACKSLASH | (p_magic ? REGSUB_MAGIC : 0));
REGSUB_COPY | REGSUB_BACKSLASH
| (magic_isset() ? REGSUB_MAGIC : 0));
textlock--;
sub_nsubs++;
did_sub = true;
@ -4389,7 +4391,7 @@ void ex_global(exarg_T *eap)
delim = *cmd; // get the delimiter
cmd++; // skip delimiter if there is one
pat = cmd; // remember start of pattern
cmd = skip_regexp_ex(cmd, delim, p_magic, &eap->arg, NULL);
cmd = skip_regexp_ex(cmd, delim, magic_isset(), &eap->arg, NULL);
if (cmd[0] == delim) { // end delimiter found
*cmd++ = NUL; // replace it with a NUL
}

View File

@ -3355,7 +3355,7 @@ static linenr_T get_address(exarg_T *eap, char **ptr, cmd_addr_T addr_type, int
goto error;
}
if (skip) { // skip "/pat/"
cmd = skip_regexp(cmd, c, p_magic);
cmd = skip_regexp(cmd, c, magic_isset());
if (*cmd == c) {
cmd++;
}
@ -5828,21 +5828,21 @@ void ex_may_print(exarg_T *eap)
/// ":smagic" and ":snomagic".
static void ex_submagic(exarg_T *eap)
{
int magic_save = p_magic;
const magic_T saved = magic_overruled;
p_magic = (eap->cmdidx == CMD_smagic);
magic_overruled = eap->cmdidx == CMD_smagic ? MAGIC_ON : MAGIC_OFF;
ex_substitute(eap);
p_magic = magic_save;
magic_overruled = saved;
}
/// ":smagic" and ":snomagic" preview callback.
static int ex_submagic_preview(exarg_T *eap, long cmdpreview_ns, handle_T cmdpreview_bufnr)
{
int magic_save = p_magic;
const magic_T saved = magic_overruled;
p_magic = (eap->cmdidx == CMD_smagic);
magic_overruled = eap->cmdidx == CMD_smagic ? MAGIC_ON : MAGIC_OFF;
int retv = ex_substitute_preview(eap, cmdpreview_ns, cmdpreview_bufnr);
p_magic = magic_save;
magic_overruled = saved;
return retv;
}
@ -6503,7 +6503,7 @@ static void ex_findpat(exarg_T *eap)
if (*eap->arg == '/') { // Match regexp, not just whole words
whole = false;
eap->arg++;
char *p = skip_regexp(eap->arg, '/', p_magic);
char *p = skip_regexp(eap->arg, '/', magic_isset());
if (*p) {
*p++ = NUL;
p = skipwhite(p);

View File

@ -98,7 +98,7 @@ typedef struct {
pos_T match_end;
bool did_incsearch;
bool incsearch_postponed;
int magic_save;
magic_T magic_overruled_save;
} incsearch_state_T;
typedef struct command_line_state {
@ -211,7 +211,7 @@ static void init_incsearch_state(incsearch_state_T *s)
s->match_start = curwin->w_cursor;
s->did_incsearch = false;
s->incsearch_postponed = false;
s->magic_save = p_magic;
s->magic_overruled_save = magic_overruled;
clearpos(&s->match_end);
s->save_cursor = curwin->w_cursor; // may be restored later
s->search_start = curwin->w_cursor;
@ -281,9 +281,9 @@ static bool do_incsearch_highlighting(int firstc, int *search_delim, incsearch_s
|| strncmp(cmd, "snomagic", (size_t)MAX(p - cmd, 3)) == 0
|| strncmp(cmd, "vglobal", (size_t)(p - cmd)) == 0) {
if (*cmd == 's' && cmd[1] == 'm') {
p_magic = true;
magic_overruled = MAGIC_ON;
} else if (*cmd == 's' && cmd[1] == 'n') {
p_magic = false;
magic_overruled = MAGIC_OFF;
}
} else if (strncmp(cmd, "sort", (size_t)MAX(p - cmd, 3)) == 0) {
// skip over ! and flags
@ -318,7 +318,7 @@ static bool do_incsearch_highlighting(int firstc, int *search_delim, incsearch_s
p = skipwhite(p);
delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
*search_delim = delim;
end = skip_regexp(p, delim, p_magic);
end = skip_regexp(p, delim, magic_isset());
use_last_pat = end == p && *end == delim;
if (end == p && !use_last_pat) {
@ -556,7 +556,7 @@ static int may_add_char_to_search(int firstc, int *c, incsearch_state_T *s)
*c = mb_tolower(*c);
}
if (*c == search_delim
|| vim_strchr((p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL) {
|| vim_strchr((magic_isset() ? "\\~^$.*[" : "\\^$"), *c) != NULL) {
// put a backslash before special characters
stuffcharReadbuff(*c);
*c = '\\';
@ -588,7 +588,7 @@ static void finish_incsearch_highlighting(int gotesc, incsearch_state_T *s, bool
search_first_line = 0;
search_last_line = MAXLNUM;
p_magic = s->magic_save;
magic_overruled = s->magic_overruled_save;
validate_cursor(); // needed for TAB
redraw_all_later(UPD_SOME_VALID);

View File

@ -1087,6 +1087,10 @@ EXTERN char windowsVersion[20] INIT(= { 0 });
EXTERN int exit_need_delay INIT(= 0);
/// While executing a regexp and set to MAGIC_ON or MAGIC_OFF this overrules
/// p_magic. Otherwise set to MAGIC_NOT_SET.
EXTERN magic_T magic_overruled INIT(= MAGIC_NOT_SET);
/// Skip win_fix_cursor() call for 'splitkeep' when cmdwin is closed.
EXTERN bool skip_win_fix_cursor INIT(= false);
/// Skip win_fix_scroll() call for 'splitkeep' when closing tab page.

View File

@ -1336,7 +1336,7 @@ static void ins_compl_dictionaries(char_u *dict_start, char_u *pat, int flags, i
xfree(pat_esc);
xfree(ptr);
} else {
regmatch.regprog = vim_regcomp((char *)pat, p_magic ? RE_MAGIC : 0);
regmatch.regprog = vim_regcomp((char *)pat, magic_isset() ? RE_MAGIC : 0);
if (regmatch.regprog == NULL) {
goto theend;
}
@ -4430,7 +4430,7 @@ static unsigned quote_meta(char_u *dest, char_u *src, int len)
}
FALLTHROUGH;
case '~':
if (!p_magic) { // quote these only if magic is set
if (!magic_isset()) { // quote these only if magic is set
break;
}
FALLTHROUGH;

View File

@ -3507,9 +3507,9 @@ static void nv_ident(cmdarg_T *cap)
xfree(p);
} else {
if (cmdchar == '*') {
aux_ptr = (p_magic ? "/.*~[^$\\" : "/^$\\");
aux_ptr = (magic_isset() ? "/.*~[^$\\" : "/^$\\");
} else if (cmdchar == '#') {
aux_ptr = (p_magic ? "/?.*~[^$\\" : "/?^$\\");
aux_ptr = (magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
} else if (tag_cmd) {
if (curbuf->b_help) {
// ":help" handles unescaped argument

View File

@ -5147,6 +5147,20 @@ int fill_culopt_flags(char *val, win_T *wp)
return OK;
}
/// Get the value of 'magic' taking "magic_overruled" into account.
bool magic_isset(void)
{
switch (magic_overruled) {
case MAGIC_ON:
return true;
case MAGIC_OFF:
return false;
case MAGIC_NOT_SET:
break;
}
return p_magic;
}
/// Set the callback function value for an option that accepts a function name,
/// lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
/// @return OK if the option is successfully set to a function, otherwise FAIL

View File

@ -15,6 +15,12 @@
#include "nvim/pos.h"
#include "nvim/types.h"
typedef enum {
MAGIC_NOT_SET, ///< p_magic not overruled
MAGIC_ON, ///< magic on inside regexp
MAGIC_OFF, ///< magic off inside regexp
} magic_T;
// The number of sub-matches is limited to 10.
// The first one (index 0) is the whole match, referenced with "\0".
// The second one (index 1) is the first sub-match, referenced with "\1".

View File

@ -138,7 +138,7 @@ int search_regcomp(char_u *pat, int pat_save, int pat_use, int options, regmmatc
int i;
rc_did_emsg = false;
magic = p_magic;
magic = magic_isset();
// If no pattern given, use a previously defined pattern.
if (pat == NULL || *pat == NUL) {
@ -1089,7 +1089,7 @@ int do_search(oparg_T *oap, int dirc, int search_delim, char *pat, long count, i
// Find end of regular expression.
// If there is a matching '/' or '?', toss it.
ps = (char_u *)strcopy;
p = skip_regexp_ex(pat, search_delim, p_magic, &strcopy, NULL);
p = skip_regexp_ex(pat, search_delim, magic_isset(), &strcopy, NULL);
if (strcopy != (char *)ps) {
// made a copy of "pat" to change "\?" to "?"
searchcmdlen += (int)(strlen(pat) - strlen(strcopy));
@ -3468,7 +3468,7 @@ void find_pattern_in_path(char *ptr, Direction dir, size_t len, bool whole, bool
snprintf((char *)pat, patlen, whole ? "\\<%.*s\\>" : "%.*s", (int)len, ptr);
// ignore case according to p_ic, p_scs and pat
regmatch.rm_ic = ignorecase(pat);
regmatch.regprog = vim_regcomp((char *)pat, p_magic ? RE_MAGIC : 0);
regmatch.regprog = vim_regcomp((char *)pat, magic_isset() ? RE_MAGIC : 0);
xfree(pat);
if (regmatch.regprog == NULL) {
goto fpip_end;
@ -3476,7 +3476,7 @@ void find_pattern_in_path(char *ptr, Direction dir, size_t len, bool whole, bool
}
char *inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
if (*inc_opt != NUL) {
incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
incl_regmatch.regprog = vim_regcomp(inc_opt, magic_isset() ? RE_MAGIC : 0);
if (incl_regmatch.regprog == NULL) {
goto fpip_end;
}
@ -3485,7 +3485,7 @@ void find_pattern_in_path(char *ptr, Direction dir, size_t len, bool whole, bool
if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL)) {
def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
? p_def : curbuf->b_p_def,
p_magic ? RE_MAGIC : 0);
magic_isset() ? RE_MAGIC : 0);
if (def_regmatch.regprog == NULL) {
goto fpip_end;
}

View File

@ -1252,7 +1252,7 @@ static void shada_read(ShaDaReadDef *const sd_reader, const int flags)
// string is close to useless: you can only use it with :& or :~ and
// thats all because s//~ is not available until the first call to
// regtilde. Vim was not calling this for some reason.
(void)regtilde(cur_entry.data.sub_string.sub, p_magic, false);
(void)regtilde(cur_entry.data.sub_string.sub, magic_isset(), false);
// Do not free shada entry: its allocated memory was saved above.
break;
case kSDItemHistoryEntry:

View File

@ -1200,7 +1200,8 @@ static void prepare_pats(pat_T *pats, int has_re)
pats->headlen = 0;
} else {
for (pats->headlen = 0; pats->head[pats->headlen] != NUL; pats->headlen++) {
if (vim_strchr((p_magic ? ".[~*\\$" : "\\$"), pats->head[pats->headlen]) != NULL) {
if (vim_strchr(magic_isset() ? ".[~*\\$" : "\\$",
pats->head[pats->headlen]) != NULL) {
break;
}
}
@ -1211,7 +1212,7 @@ static void prepare_pats(pat_T *pats, int has_re)
}
if (has_re) {
pats->regmatch.regprog = vim_regcomp(pats->pat, p_magic ? RE_MAGIC : 0);
pats->regmatch.regprog = vim_regcomp(pats->pat, magic_isset() ? RE_MAGIC : 0);
} else {
pats->regmatch.regprog = NULL;
}
@ -2812,7 +2813,6 @@ static char_u *tag_full_fname(tagptrs_T *tagp)
/// @return OK for success, NOTAGFILE when file not found, FAIL otherwise.
static int jumpto_tag(const char_u *lbuf_arg, int forceit, int keep_help)
{
int save_magic;
bool save_p_ws;
int save_p_scs, save_p_ic;
linenr_T save_lnum;
@ -2955,8 +2955,8 @@ static int jumpto_tag(const char_u *lbuf_arg, int forceit, int keep_help)
curwin->w_set_curswant = true;
postponed_split = 0;
save_magic = p_magic;
p_magic = false; // always execute with 'nomagic'
const magic_T save_magic_overruled = magic_overruled;
magic_overruled = MAGIC_OFF; // always execute with 'nomagic'
// Save value of no_hlsearch, jumping to a tag is not a real search
const bool save_no_hlsearch = no_hlsearch;
@ -3063,7 +3063,7 @@ static int jumpto_tag(const char_u *lbuf_arg, int forceit, int keep_help)
sandbox--;
}
p_magic = save_magic;
magic_overruled = save_magic_overruled;
// restore no_hlsearch when keeping the old search pattern
if (search_options) {
set_no_hlsearch(save_no_hlsearch);