vim-patch:8.2.0035: saving and restoring called_emsg is clumsy (#19335)

Problem:    Saving and restoring called_emsg is clumsy.
Solution:   Count the number of error messages.
53989554a4
This commit is contained in:
zeertzjq
2022-07-13 04:08:49 +08:00
committed by GitHub
parent b1e0197a14
commit 39d51c833a
10 changed files with 26 additions and 49 deletions

View File

@@ -3136,18 +3136,16 @@ void maketitle(void)
if (*p_titlestring != NUL) { if (*p_titlestring != NUL) {
if (stl_syntax & STL_IN_TITLE) { if (stl_syntax & STL_IN_TITLE) {
int use_sandbox = false; int use_sandbox = false;
int save_called_emsg = called_emsg; const int called_emsg_before = called_emsg;
use_sandbox = was_set_insecurely(curwin, "titlestring", 0); use_sandbox = was_set_insecurely(curwin, "titlestring", 0);
called_emsg = false;
build_stl_str_hl(curwin, buf, sizeof(buf), build_stl_str_hl(curwin, buf, sizeof(buf),
(char *)p_titlestring, use_sandbox, (char *)p_titlestring, use_sandbox,
0, maxlen, NULL, NULL); 0, maxlen, NULL, NULL);
title_str = buf; title_str = buf;
if (called_emsg) { if (called_emsg > called_emsg_before) {
set_string_option_direct("titlestring", -1, "", OPT_FREE, SID_ERROR); set_string_option_direct("titlestring", -1, "", OPT_FREE, SID_ERROR);
} }
called_emsg |= save_called_emsg;
} else { } else {
title_str = (char *)p_titlestring; title_str = (char *)p_titlestring;
} }
@@ -3252,17 +3250,15 @@ void maketitle(void)
if (*p_iconstring != NUL) { if (*p_iconstring != NUL) {
if (stl_syntax & STL_IN_ICON) { if (stl_syntax & STL_IN_ICON) {
int use_sandbox = false; int use_sandbox = false;
int save_called_emsg = called_emsg; const int called_emsg_before = called_emsg;
use_sandbox = was_set_insecurely(curwin, "iconstring", 0); use_sandbox = was_set_insecurely(curwin, "iconstring", 0);
called_emsg = false;
build_stl_str_hl(curwin, icon_str, sizeof(buf), build_stl_str_hl(curwin, icon_str, sizeof(buf),
(char *)p_iconstring, use_sandbox, (char *)p_iconstring, use_sandbox,
0, 0, NULL, NULL); 0, 0, NULL, NULL);
if (called_emsg) { if (called_emsg > called_emsg_before) {
set_string_option_direct("iconstring", -1, "", OPT_FREE, SID_ERROR); set_string_option_direct("iconstring", -1, "", OPT_FREE, SID_ERROR);
} }
called_emsg |= save_called_emsg;
} else { } else {
icon_str = (char *)p_iconstring; icon_str = (char *)p_iconstring;
} }

View File

@@ -7296,7 +7296,7 @@ void timer_due_cb(TimeWatcher *tw, void *data)
{ {
timer_T *timer = (timer_T *)data; timer_T *timer = (timer_T *)data;
int save_did_emsg = did_emsg; int save_did_emsg = did_emsg;
int save_called_emsg = called_emsg; const int called_emsg_before = called_emsg;
const bool save_ex_pressedreturn = get_pressedreturn(); const bool save_ex_pressedreturn = get_pressedreturn();
if (timer->stopped || timer->paused) { if (timer->stopped || timer->paused) {
@@ -7313,19 +7313,17 @@ void timer_due_cb(TimeWatcher *tw, void *data)
argv[0].v_type = VAR_NUMBER; argv[0].v_type = VAR_NUMBER;
argv[0].vval.v_number = timer->timer_id; argv[0].vval.v_number = timer->timer_id;
typval_T rettv = TV_INITIAL_VALUE; typval_T rettv = TV_INITIAL_VALUE;
called_emsg = false;
callback_call(&timer->callback, 1, argv, &rettv); callback_call(&timer->callback, 1, argv, &rettv);
// Handle error message // Handle error message
if (called_emsg && did_emsg) { if (called_emsg > called_emsg_before && did_emsg) {
timer->emsg_count++; timer->emsg_count++;
if (current_exception != NULL) { if (current_exception != NULL) {
discard_current_exception(); discard_current_exception();
} }
} }
did_emsg = save_did_emsg; did_emsg = save_did_emsg;
called_emsg = save_called_emsg;
set_pressedreturn(save_ex_pressedreturn); set_pressedreturn(save_ex_pressedreturn);
if (timer->emsg_count >= 3) { if (timer->emsg_count >= 3) {

View File

@@ -3902,15 +3902,14 @@ static void f_wait(typval_T *argvars, typval_T *rettv, FunPtr fptr)
typval_T argv = TV_INITIAL_VALUE; typval_T argv = TV_INITIAL_VALUE;
typval_T exprval = TV_INITIAL_VALUE; typval_T exprval = TV_INITIAL_VALUE;
bool error = false; bool error = false;
int save_called_emsg = called_emsg; const int called_emsg_before = called_emsg;
called_emsg = false;
LOOP_PROCESS_EVENTS_UNTIL(&main_loop, main_loop.events, timeout, LOOP_PROCESS_EVENTS_UNTIL(&main_loop, main_loop.events, timeout,
eval_expr_typval(&expr, &argv, 0, &exprval) != OK eval_expr_typval(&expr, &argv, 0, &exprval) != OK
|| tv_get_number_chk(&exprval, &error) || tv_get_number_chk(&exprval, &error)
|| called_emsg || error || got_int); || called_emsg > called_emsg_before || error || got_int);
if (called_emsg || error) { if (called_emsg > called_emsg_before || error) {
rettv->vval.v_number = -3; rettv->vval.v_number = -3;
} else if (got_int) { } else if (got_int) {
got_int = false; got_int = false;
@@ -3920,8 +3919,6 @@ static void f_wait(typval_T *argvars, typval_T *rettv, FunPtr fptr)
rettv->vval.v_number = 0; rettv->vval.v_number = 0;
} }
called_emsg = save_called_emsg;
// Stop dummy timer // Stop dummy timer
time_watcher_stop(tw); time_watcher_stop(tw);
time_watcher_close(tw, dummy_timer_close_cb); time_watcher_close(tw, dummy_timer_close_cb);

View File

@@ -229,7 +229,7 @@ EXTERN bool did_emsg; // set by emsg() when the message
EXTERN bool called_vim_beep; // set if vim_beep() is called EXTERN bool called_vim_beep; // set if vim_beep() is called
EXTERN bool did_emsg_syntax; // did_emsg set because of a EXTERN bool did_emsg_syntax; // did_emsg set because of a
// syntax error // syntax error
EXTERN int called_emsg; // always set by emsg() EXTERN int called_emsg; // always incremented by emsg()
EXTERN int ex_exitval INIT(= 0); // exit value for ex mode EXTERN int ex_exitval INIT(= 0); // exit value for ex mode
EXTERN bool emsg_on_display INIT(= false); // there is an error message EXTERN bool emsg_on_display INIT(= false); // there is an error message
EXTERN bool rc_did_emsg INIT(= false); // vim_regcomp() called emsg() EXTERN bool rc_did_emsg INIT(= false); // vim_regcomp() called emsg()

View File

@@ -398,7 +398,7 @@ static void next_search_hl(win_T *win, match_T *search_hl, match_T *shl, linenr_
linenr_T l; linenr_T l;
colnr_T matchcol; colnr_T matchcol;
long nmatched = 0; long nmatched = 0;
int save_called_emsg = called_emsg; const int called_emsg_before = called_emsg;
// for :{range}s/pat only highlight inside the range // for :{range}s/pat only highlight inside the range
if (lnum < search_first_line || lnum > search_last_line) { if (lnum < search_first_line || lnum > search_last_line) {
@@ -421,7 +421,6 @@ static void next_search_hl(win_T *win, match_T *search_hl, match_T *shl, linenr_
// Repeat searching for a match until one is found that includes "mincol" // Repeat searching for a match until one is found that includes "mincol"
// or none is found in this line. // or none is found in this line.
called_emsg = false;
for (;;) { for (;;) {
// Stop searching after passing the time limit. // Stop searching after passing the time limit.
if (profile_passed_limit(shl->tm)) { if (profile_passed_limit(shl->tm)) {
@@ -468,7 +467,7 @@ static void next_search_hl(win_T *win, match_T *search_hl, match_T *shl, linenr_
if (regprog_is_copy) { if (regprog_is_copy) {
cur->match.regprog = cur->hl.rm.regprog; cur->match.regprog = cur->hl.rm.regprog;
} }
if (called_emsg || got_int || timed_out) { if (called_emsg > called_emsg_before || got_int || timed_out) {
// Error while handling regexp: stop using this regexp. // Error while handling regexp: stop using this regexp.
if (shl == search_hl) { if (shl == search_hl) {
// don't free regprog in the match list, it's a copy // don't free regprog in the match list, it's a copy
@@ -495,9 +494,6 @@ static void next_search_hl(win_T *win, match_T *search_hl, match_T *shl, linenr_
break; // useful match found break; // useful match found
} }
} }
// Restore called_emsg for assert_fails().
called_emsg = save_called_emsg;
} }
/// Advance to the match in window "wp" line "lnum" or past it. /// Advance to the match in window "wp" line "lnum" or past it.

View File

@@ -637,7 +637,7 @@ static bool emsg_multiline(const char *s, bool multiline)
return true; return true;
} }
called_emsg = true; called_emsg++;
// If "emsg_severe" is true: When an error exception is to be thrown, // If "emsg_severe" is true: When an error exception is to be thrown,
// prefer this message over previous messages for the same command. // prefer this message over previous messages for the same command.

View File

@@ -2327,7 +2327,6 @@ regprog_T *vim_regcomp(char *expr_arg, int re_flags)
{ {
regprog_T *prog = NULL; regprog_T *prog = NULL;
char_u *expr = (char_u *)expr_arg; char_u *expr = (char_u *)expr_arg;
int save_called_emsg;
regexp_engine = p_re; regexp_engine = p_re;
@@ -2360,8 +2359,7 @@ regprog_T *vim_regcomp(char *expr_arg, int re_flags)
// //
// First try the NFA engine, unless backtracking was requested. // First try the NFA engine, unless backtracking was requested.
// //
save_called_emsg = called_emsg; const int called_emsg_before = called_emsg;
called_emsg = false;
if (regexp_engine != BACKTRACKING_ENGINE) { if (regexp_engine != BACKTRACKING_ENGINE) {
prog = nfa_regengine.regcomp(expr, prog = nfa_regengine.regcomp(expr,
re_flags + (regexp_engine == AUTOMATIC_ENGINE ? RE_AUTO : 0)); re_flags + (regexp_engine == AUTOMATIC_ENGINE ? RE_AUTO : 0));
@@ -2388,13 +2386,12 @@ regprog_T *vim_regcomp(char *expr_arg, int re_flags)
// also fails for patterns that it can't handle well but are still valid // also fails for patterns that it can't handle well but are still valid
// patterns, thus a retry should work. // patterns, thus a retry should work.
// But don't try if an error message was given. // But don't try if an error message was given.
if (regexp_engine == AUTOMATIC_ENGINE && !called_emsg) { if (regexp_engine == AUTOMATIC_ENGINE && called_emsg == called_emsg_before) {
regexp_engine = BACKTRACKING_ENGINE; regexp_engine = BACKTRACKING_ENGINE;
report_re_switch(expr); report_re_switch(expr);
prog = bt_regengine.regcomp(expr, re_flags); prog = bt_regengine.regcomp(expr, re_flags);
} }
} }
called_emsg |= save_called_emsg;
if (prog != NULL) { if (prog != NULL) {
// Store the info needed to call regcomp() again when the engine turns out // Store the info needed to call regcomp() again when the engine turns out

View File

@@ -6533,13 +6533,11 @@ static void win_redr_ruler(win_T *wp, bool always)
} }
if (*p_ruf && p_ch > 0 && !ui_has(kUIMessages)) { if (*p_ruf && p_ch > 0 && !ui_has(kUIMessages)) {
int save_called_emsg = called_emsg; const int called_emsg_before = called_emsg;
called_emsg = false;
win_redr_custom(wp, false, true); win_redr_custom(wp, false, true);
if (called_emsg) { if (called_emsg > called_emsg_before) {
set_string_option_direct("rulerformat", -1, "", OPT_FREE, SID_ERROR); set_string_option_direct("rulerformat", -1, "", OPT_FREE, SID_ERROR);
} }
called_emsg |= save_called_emsg;
return; return;
} }

View File

@@ -556,7 +556,7 @@ int searchit(win_T *win, buf_T *buf, pos_T *pos, pos_T *end_pos, Direction dir,
long nmatched; long nmatched;
int submatch = 0; int submatch = 0;
bool first_match = true; bool first_match = true;
int save_called_emsg = called_emsg; const int called_emsg_before = called_emsg;
bool break_loop = false; bool break_loop = false;
linenr_T stop_lnum = 0; // stop after this line number when != 0 linenr_T stop_lnum = 0; // stop after this line number when != 0
proftime_T *tm = NULL; // timeout limit or NULL proftime_T *tm = NULL; // timeout limit or NULL
@@ -579,7 +579,6 @@ int searchit(win_T *win, buf_T *buf, pos_T *pos, pos_T *end_pos, Direction dir,
/* /*
* find the string * find the string
*/ */
called_emsg = FALSE;
do { // loop for count do { // loop for count
// When not accepting a match at the start position set "extra_col" to a // When not accepting a match at the start position set "extra_col" to a
// non-zero value. Don't do that when starting at MAXCOL, since MAXCOL + 1 // non-zero value. Don't do that when starting at MAXCOL, since MAXCOL + 1
@@ -651,7 +650,7 @@ int searchit(win_T *win, buf_T *buf, pos_T *pos, pos_T *end_pos, Direction dir,
break; break;
} }
// Abort searching on an error (e.g., out of stack). // Abort searching on an error (e.g., out of stack).
if (called_emsg || (timed_out != NULL && *timed_out)) { if (called_emsg > called_emsg_before || (timed_out != NULL && *timed_out)) {
break; break;
} }
if (nmatched > 0) { if (nmatched > 0) {
@@ -908,7 +907,8 @@ int searchit(win_T *win, buf_T *buf, pos_T *pos, pos_T *end_pos, Direction dir,
// Stop the search if wrapscan isn't set, "stop_lnum" is // Stop the search if wrapscan isn't set, "stop_lnum" is
// specified, after an interrupt, after a match and after looping // specified, after an interrupt, after a match and after looping
// twice. // twice.
if (!p_ws || stop_lnum != 0 || got_int || called_emsg if (!p_ws || stop_lnum != 0 || got_int
|| called_emsg > called_emsg_before
|| (timed_out != NULL && *timed_out) || (timed_out != NULL && *timed_out)
|| break_loop || break_loop
|| found || loop) { || found || loop) {
@@ -933,7 +933,7 @@ int searchit(win_T *win, buf_T *buf, pos_T *pos, pos_T *end_pos, Direction dir,
extra_arg->sa_wrapped = true; extra_arg->sa_wrapped = true;
} }
} }
if (got_int || called_emsg if (got_int || called_emsg > called_emsg_before
|| (timed_out != NULL && *timed_out) || (timed_out != NULL && *timed_out)
|| break_loop) { || break_loop) {
break; break;
@@ -942,8 +942,6 @@ int searchit(win_T *win, buf_T *buf, pos_T *pos, pos_T *end_pos, Direction dir,
vim_regfree(regmatch.regprog); vim_regfree(regmatch.regprog);
called_emsg |= save_called_emsg;
if (!found) { // did not find it if (!found) { // did not find it
if (got_int) { if (got_int) {
emsg(_(e_interr)); emsg(_(e_interr));
@@ -4409,7 +4407,7 @@ static int is_zero_width(char_u *pattern, int move, pos_T *cur, Direction direct
int nmatched = 0; int nmatched = 0;
int result = -1; int result = -1;
pos_T pos; pos_T pos;
int save_called_emsg = called_emsg; const int called_emsg_before = called_emsg;
int flag = 0; int flag = 0;
if (pattern == NULL) { if (pattern == NULL) {
@@ -4435,7 +4433,6 @@ static int is_zero_width(char_u *pattern, int move, pos_T *cur, Direction direct
SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL) { SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL) {
// Zero-width pattern should match somewhere, then we can check if // Zero-width pattern should match somewhere, then we can check if
// start and end are in the same position. // start and end are in the same position.
called_emsg = false;
do { do {
regmatch.startpos[0].col++; regmatch.startpos[0].col++;
nmatched = vim_regexec_multi(&regmatch, curwin, curbuf, nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
@@ -4449,14 +4446,13 @@ static int is_zero_width(char_u *pattern, int move, pos_T *cur, Direction direct
? regmatch.startpos[0].col < pos.col ? regmatch.startpos[0].col < pos.col
: regmatch.startpos[0].col > pos.col); : regmatch.startpos[0].col > pos.col);
if (!called_emsg) { if (called_emsg == called_emsg_before) {
result = (nmatched != 0 result = (nmatched != 0
&& regmatch.startpos[0].lnum == regmatch.endpos[0].lnum && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
&& regmatch.startpos[0].col == regmatch.endpos[0].col); && regmatch.startpos[0].col == regmatch.endpos[0].col);
} }
} }
called_emsg |= save_called_emsg;
vim_regfree(regmatch.regprog); vim_regfree(regmatch.regprog);
return result; return result;
} }

View File

@@ -405,15 +405,15 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr)
const char *const cmd = tv_get_string_chk(&argvars[0]); const char *const cmd = tv_get_string_chk(&argvars[0]);
garray_T ga; garray_T ga;
int save_trylevel = trylevel; int save_trylevel = trylevel;
const int called_emsg_before = called_emsg;
// trylevel must be zero for a ":throw" command to be considered failed // trylevel must be zero for a ":throw" command to be considered failed
trylevel = 0; trylevel = 0;
called_emsg = false;
suppress_errthrow = true; suppress_errthrow = true;
emsg_silent = true; emsg_silent = true;
do_cmdline_cmd(cmd); do_cmdline_cmd(cmd);
if (!called_emsg) { if (called_emsg == called_emsg_before) {
prepare_assert_error(&ga); prepare_assert_error(&ga);
ga_concat(&ga, "command did not fail: "); ga_concat(&ga, "command did not fail: ");
assert_append_cmd_or_arg(&ga, argvars, cmd); assert_append_cmd_or_arg(&ga, argvars, cmd);
@@ -438,7 +438,6 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr)
} }
trylevel = save_trylevel; trylevel = save_trylevel;
called_emsg = false;
suppress_errthrow = false; suppress_errthrow = false;
emsg_silent = false; emsg_silent = false;
emsg_on_display = false; emsg_on_display = false;