vim-patch:8.2.4723: the ModeChanged autocmd event is inefficient

Problem:    The ModeChanged autocmd event is inefficient.
Solution:   Avoid allocating memory. (closes vim/vim#10134)  Rename
            trigger_modechanged() to may_trigger_modechanged().
2bf52dd065

Make v:event readonly for ModeChanged.
This commit is contained in:
zeertzjq
2022-04-10 07:20:35 +08:00
parent ff726cc569
commit 263a7fde35
12 changed files with 77 additions and 71 deletions

View File

@@ -1549,10 +1549,11 @@ Dictionary nvim_get_mode(void)
FUNC_API_SINCE(2) FUNC_API_FAST FUNC_API_SINCE(2) FUNC_API_FAST
{ {
Dictionary rv = ARRAY_DICT_INIT; Dictionary rv = ARRAY_DICT_INIT;
char *modestr = get_mode(); char modestr[MODE_MAX_LENGTH];
get_mode(modestr);
bool blocked = input_blocking(); bool blocked = input_blocking();
PUT(rv, "mode", STRING_OBJ(cstr_as_string(modestr))); PUT(rv, "mode", STRING_OBJ(cstr_to_string(modestr)));
PUT(rv, "blocking", BOOLEAN_OBJ(blocked)); PUT(rv, "blocking", BOOLEAN_OBJ(blocked));
return rv; return rv;

View File

@@ -1083,8 +1083,7 @@ int autocmd_register(int64_t id, event_T event, char_u *pat, int patlen, int gro
// need to initialize last_mode for the first ModeChanged autocmd // need to initialize last_mode for the first ModeChanged autocmd
if (event == EVENT_MODECHANGED && !has_event(EVENT_MODECHANGED)) { if (event == EVENT_MODECHANGED && !has_event(EVENT_MODECHANGED)) {
xfree(last_mode); get_mode(last_mode);
last_mode = get_mode();
} }
// If the event is CursorMoved, update the last cursor position // If the event is CursorMoved, update the last cursor position

View File

@@ -387,7 +387,7 @@ static void insert_enter(InsertState *s)
State = INSERT; State = INSERT;
} }
trigger_modechanged(); may_trigger_modechanged();
stop_insert_mode = false; stop_insert_mode = false;
// need to position cursor again when on a TAB // need to position cursor again when on a TAB
@@ -2097,7 +2097,7 @@ static void ins_ctrl_x(void)
ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X; ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
} }
trigger_modechanged(); may_trigger_modechanged();
} }
// Whether other than default completion has been selected. // Whether other than default completion has been selected.
@@ -2710,7 +2710,7 @@ void set_completion(colnr_T startcol, list_T *list)
show_pum(save_w_wrow, save_w_leftcol); show_pum(save_w_wrow, save_w_leftcol);
} }
trigger_modechanged(); may_trigger_modechanged();
ui_flush(); ui_flush();
} }
@@ -3890,7 +3890,7 @@ static bool ins_compl_prep(int c)
ins_apply_autocmds(EVENT_COMPLETEDONE); ins_apply_autocmds(EVENT_COMPLETEDONE);
} }
trigger_modechanged(); may_trigger_modechanged();
/* reset continue_* if we left expansion-mode, if we stay they'll be /* reset continue_* if we left expansion-mode, if we stay they'll be
* (re)set properly in ins_complete() */ * (re)set properly in ins_complete() */
@@ -4641,7 +4641,7 @@ static int ins_compl_get_exp(pos_T *ini)
compl_curr_match = compl_old_match; compl_curr_match = compl_old_match;
} }
} }
trigger_modechanged(); may_trigger_modechanged();
return i; return i;
} }
@@ -8051,7 +8051,7 @@ static bool ins_esc(long *count, int cmdchar, bool nomove)
State = NORMAL; State = NORMAL;
trigger_modechanged(); may_trigger_modechanged();
// need to position cursor again when on a TAB // need to position cursor again when on a TAB
if (gchar_cursor() == TAB) { if (gchar_cursor() == TAB) {
curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL); curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
@@ -8155,7 +8155,7 @@ static void ins_insert(int replaceState)
} else { } else {
State = replaceState | (State & LANGMAP); State = replaceState | (State & LANGMAP);
} }
trigger_modechanged(); may_trigger_modechanged();
AppendCharToRedobuff(K_INS); AppendCharToRedobuff(K_INS);
showmode(); showmode();
ui_cursor_shape(); // may show different cursor shape ui_cursor_shape(); // may show different cursor shape

View File

@@ -6094,15 +6094,17 @@ static void f_mkdir(typval_T *argvars, typval_T *rettv, FunPtr fptr)
/// "mode()" function /// "mode()" function
static void f_mode(typval_T *argvars, typval_T *rettv, FunPtr fptr) static void f_mode(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{ {
char *mode = get_mode(); char buf[MODE_MAX_LENGTH];
get_mode(buf);
// Clear out the minor mode when the argument is not a non-zero number or // Clear out the minor mode when the argument is not a non-zero number or
// non-empty string. // non-empty string.
if (!non_zero_arg(&argvars[0])) { if (!non_zero_arg(&argvars[0])) {
mode[1] = NUL; buf[1] = NUL;
} }
rettv->vval.v_string = (char_u *)mode; rettv->vval.v_string = vim_strsave((char_u *)buf);
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
} }

View File

@@ -187,7 +187,7 @@ void do_exmode(void)
exmode_active = true; exmode_active = true;
State = NORMAL; State = NORMAL;
trigger_modechanged(); may_trigger_modechanged();
// When using ":global /pat/ visual" and then "Q" we return to continue // When using ":global /pat/ visual" and then "Q" we return to continue
// the :global command. // the :global command.

View File

@@ -921,7 +921,7 @@ static uint8_t *command_line_enter(int firstc, long count, int indent, bool init
} }
tl_ret = true; tl_ret = true;
} }
trigger_modechanged(); may_trigger_modechanged();
state_enter(&s->state); state_enter(&s->state);
@@ -6556,7 +6556,7 @@ static int open_cmdwin(void)
cmdmsg_rl = save_cmdmsg_rl; cmdmsg_rl = save_cmdmsg_rl;
State = save_State; State = save_State;
trigger_modechanged(); may_trigger_modechanged();
setmouse(); setmouse();
return cmdwin_result; return cmdwin_result;

View File

@@ -746,7 +746,11 @@ EXTERN bool listcmd_busy INIT(= false); // set when :argdo, :windo or
// :bufdo is executing // :bufdo is executing
EXTERN bool need_start_insertmode INIT(= false); EXTERN bool need_start_insertmode INIT(= false);
// start insert mode soon // start insert mode soon
EXTERN char *last_mode INIT(= NULL);
#define MODE_MAX_LENGTH 4 // max mode length returned in get_mode()
// including the final NUL character
EXTERN char last_mode[MODE_MAX_LENGTH] INIT(= "n");
EXTERN char_u *last_cmdline INIT(= NULL); // last command line (for ":) EXTERN char_u *last_cmdline INIT(= NULL); // last command line (for ":)
EXTERN char_u *repeat_cmdline INIT(= NULL); // command line for "." EXTERN char_u *repeat_cmdline INIT(= NULL); // command line for "."
EXTERN char_u *new_last_cmdline INIT(= NULL); // new value for last_cmdline EXTERN char_u *new_last_cmdline INIT(= NULL); // new value for last_cmdline

View File

@@ -631,7 +631,6 @@ void free_all_mem(void)
clear_sb_text(true); // free any scrollback text clear_sb_text(true); // free any scrollback text
// Free some global vars. // Free some global vars.
xfree(last_mode);
xfree(last_cmdline); xfree(last_cmdline);
xfree(new_last_cmdline); xfree(new_last_cmdline);
set_keep_msg(NULL, 0); set_keep_msg(NULL, 0);

View File

@@ -481,7 +481,7 @@ static void normal_prepare(NormalState *s)
if (finish_op != c) { if (finish_op != c) {
ui_cursor_shape(); // may show different cursor shape ui_cursor_shape(); // may show different cursor shape
} }
trigger_modechanged(); may_trigger_modechanged();
// When not finishing an operator and no register name typed, reset the count. // When not finishing an operator and no register name typed, reset the count.
if (!finish_op && !s->oa.regname) { if (!finish_op && !s->oa.regname) {
@@ -920,7 +920,7 @@ normal_end:
// Reset finish_op, in case it was set // Reset finish_op, in case it was set
s->c = finish_op; s->c = finish_op;
finish_op = false; finish_op = false;
trigger_modechanged(); may_trigger_modechanged();
// Redraw the cursor with another shape, if we were in Operator-pending // Redraw the cursor with another shape, if we were in Operator-pending
// mode or did a replace command. // mode or did a replace command.
if (s->c || s->ca.cmdchar == 'r') { if (s->c || s->ca.cmdchar == 'r') {
@@ -959,7 +959,7 @@ normal_end:
if (restart_VIsual_select == 1) { if (restart_VIsual_select == 1) {
VIsual_select = true; VIsual_select = true;
VIsual_select_reg = 0; VIsual_select_reg = 0;
trigger_modechanged(); may_trigger_modechanged();
showmode(); showmode();
restart_VIsual_select = 0; restart_VIsual_select = 0;
} }
@@ -2299,7 +2299,7 @@ void end_visual_mode(void)
may_clear_cmdline(); may_clear_cmdline();
adjust_cursor_eol(); adjust_cursor_eol();
trigger_modechanged(); may_trigger_modechanged();
} }
/* /*
@@ -4113,7 +4113,7 @@ static void nv_ctrlg(cmdarg_T *cap)
{ {
if (VIsual_active) { // toggle Selection/Visual mode if (VIsual_active) { // toggle Selection/Visual mode
VIsual_select = !VIsual_select; VIsual_select = !VIsual_select;
trigger_modechanged(); may_trigger_modechanged();
showmode(); showmode();
} else if (!checkclearop(cap->oap)) { } else if (!checkclearop(cap->oap)) {
// print full name if count given or :cd used // print full name if count given or :cd used
@@ -4157,7 +4157,7 @@ static void nv_ctrlo(cmdarg_T *cap)
{ {
if (VIsual_active && VIsual_select) { if (VIsual_active && VIsual_select) {
VIsual_select = false; VIsual_select = false;
trigger_modechanged(); may_trigger_modechanged();
showmode(); showmode();
restart_VIsual_select = 2; // restart Select mode later restart_VIsual_select = 2; // restart Select mode later
} else { } else {
@@ -5945,7 +5945,7 @@ static void nv_visual(cmdarg_T *cap)
// or char/line mode // or char/line mode
VIsual_mode = cap->cmdchar; VIsual_mode = cap->cmdchar;
showmode(); showmode();
trigger_modechanged(); may_trigger_modechanged();
} }
redraw_curbuf_later(INVERTED); // update the inversion redraw_curbuf_later(INVERTED); // update the inversion
} else { // start Visual mode } else { // start Visual mode
@@ -6056,7 +6056,7 @@ static void n_start_visual_mode(int c)
foldAdjustVisual(); foldAdjustVisual();
trigger_modechanged(); may_trigger_modechanged();
setmouse(); setmouse();
// Check for redraw after changing the state. // Check for redraw after changing the state.
conceal_check_cursor_line(); conceal_check_cursor_line();

View File

@@ -156,102 +156,105 @@ int get_real_state(void)
return State; return State;
} }
/// @returns[allocated] mode string /// Returns the current mode as a string in "buf[MODE_MAX_LENGTH]", NUL
char *get_mode(void) /// terminated.
/// The first character represents the major mode, the following ones the minor
/// ones.
void get_mode(char *buf)
{ {
char *buf = xcalloc(MODE_MAX_LENGTH, sizeof(char)); int i = 0;
if (VIsual_active) { if (VIsual_active) {
if (VIsual_select) { if (VIsual_select) {
buf[0] = (char)(VIsual_mode + 's' - 'v'); buf[i++] = (char)(VIsual_mode + 's' - 'v');
} else { } else {
buf[0] = (char)VIsual_mode; buf[i++] = (char)VIsual_mode;
if (restart_VIsual_select) { if (restart_VIsual_select) {
buf[1] = 's'; buf[i++] = 's';
} }
} }
} else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE } else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
|| State == CONFIRM) { || State == CONFIRM) {
buf[0] = 'r'; buf[i++] = 'r';
if (State == ASKMORE) { if (State == ASKMORE) {
buf[1] = 'm'; buf[i++] = 'm';
} else if (State == CONFIRM) { } else if (State == CONFIRM) {
buf[1] = '?'; buf[i++] = '?';
} }
} else if (State == EXTERNCMD) { } else if (State == EXTERNCMD) {
buf[0] = '!'; buf[i++] = '!';
} else if (State & INSERT) { } else if (State & INSERT) {
if (State & VREPLACE_FLAG) { if (State & VREPLACE_FLAG) {
buf[0] = 'R'; buf[i++] = 'R';
buf[1] = 'v'; buf[i++] = 'v';
if (ins_compl_active()) { if (ins_compl_active()) {
buf[2] = 'c'; buf[i++] = 'c';
} else if (ctrl_x_mode_not_defined_yet()) { } else if (ctrl_x_mode_not_defined_yet()) {
buf[2] = 'x'; buf[i++] = 'x';
} }
} else { } else {
if (State & REPLACE_FLAG) { if (State & REPLACE_FLAG) {
buf[0] = 'R'; buf[i++] = 'R';
} else { } else {
buf[0] = 'i'; buf[i++] = 'i';
} }
if (ins_compl_active()) { if (ins_compl_active()) {
buf[1] = 'c'; buf[i++] = 'c';
} else if (ctrl_x_mode_not_defined_yet()) { } else if (ctrl_x_mode_not_defined_yet()) {
buf[1] = 'x'; buf[i++] = 'x';
} }
} }
} else if ((State & CMDLINE) || exmode_active) { } else if ((State & CMDLINE) || exmode_active) {
buf[0] = 'c'; buf[i++] = 'c';
if (exmode_active) { if (exmode_active) {
buf[1] = 'v'; buf[i++] = 'v';
} }
} else if (State & TERM_FOCUS) { } else if (State & TERM_FOCUS) {
buf[0] = 't'; buf[i++] = 't';
} else { } else {
buf[0] = 'n'; buf[i++] = 'n';
if (finish_op) { if (finish_op) {
buf[1] = 'o'; buf[i++] = 'o';
// to be able to detect force-linewise/blockwise/charwise operations // to be able to detect force-linewise/blockwise/charwise operations
buf[2] = (char)motion_force; buf[i++] = (char)motion_force;
} else if (restart_edit == 'I' || restart_edit == 'R' } else if (restart_edit == 'I' || restart_edit == 'R'
|| restart_edit == 'V') { || restart_edit == 'V') {
buf[1] = 'i'; buf[i++] = 'i';
buf[2] = (char)restart_edit; buf[i++] = (char)restart_edit;
} else if (curbuf->terminal) { } else if (curbuf->terminal) {
buf[1] = 't'; buf[i++] = 't';
} }
} }
return buf; buf[i] = NUL;
} }
/// Fires a ModeChanged autocmd. /// Fires a ModeChanged autocmd if appropriate.
void trigger_modechanged(void) void may_trigger_modechanged(void)
{ {
if (!has_event(EVENT_MODECHANGED)) { if (!has_event(EVENT_MODECHANGED)) {
return; return;
} }
char *mode = get_mode(); char curr_mode[MODE_MAX_LENGTH];
if (STRCMP(mode, last_mode) == 0) { char_u pattern_buf[2 * MODE_MAX_LENGTH];
xfree(mode);
get_mode(curr_mode);
if (STRCMP(curr_mode, last_mode) == 0) {
return; return;
} }
save_v_event_T save_v_event; save_v_event_T save_v_event;
dict_T *v_event = get_v_event(&save_v_event); dict_T *v_event = get_v_event(&save_v_event);
tv_dict_add_str(v_event, S_LEN("new_mode"), mode); tv_dict_add_str(v_event, S_LEN("new_mode"), curr_mode);
tv_dict_add_str(v_event, S_LEN("old_mode"), last_mode); tv_dict_add_str(v_event, S_LEN("old_mode"), last_mode);
tv_dict_set_keys_readonly(v_event);
char_u *pat_pre = concat_str((char_u *)last_mode, (char_u *)":"); // concatenate modes in format "old_mode:new_mode"
char_u *pat = concat_str(pat_pre, (char_u *)mode); vim_snprintf((char *)pattern_buf, sizeof(pattern_buf), "%s:%s", last_mode, curr_mode);
xfree(pat_pre);
apply_autocmds(EVENT_MODECHANGED, pat, NULL, false, curbuf); apply_autocmds(EVENT_MODECHANGED, pattern_buf, NULL, false, curbuf);
xfree(last_mode); STRCPY(last_mode, curr_mode);
last_mode = mode;
xfree(pat);
restore_v_event(v_event, &save_v_event); restore_v_event(v_event, &save_v_event);
} }

View File

@@ -422,7 +422,7 @@ void terminal_enter(void)
curwin->w_redr_status = true; // For mode() in statusline. #8323 curwin->w_redr_status = true; // For mode() in statusline. #8323
ui_busy_start(); ui_busy_start();
apply_autocmds(EVENT_TERMENTER, NULL, NULL, false, curbuf); apply_autocmds(EVENT_TERMENTER, NULL, NULL, false, curbuf);
trigger_modechanged(); may_trigger_modechanged();
s->state.execute = terminal_execute; s->state.execute = terminal_execute;
s->state.check = terminal_check; s->state.check = terminal_check;

View File

@@ -72,8 +72,6 @@ enum { NUMBUFLEN = 65, };
#define TERM_FOCUS 0x2000 // Terminal focus mode #define TERM_FOCUS 0x2000 // Terminal focus mode
#define CMDPREVIEW 0x4000 // Showing 'inccommand' command "live" preview. #define CMDPREVIEW 0x4000 // Showing 'inccommand' command "live" preview.
#define MODE_MAX_LENGTH 4 // max mode length returned in mode()
// all mode bits used for mapping // all mode bits used for mapping
#define MAP_ALL_MODES (0x3f | SELECTMODE | TERM_FOCUS) #define MAP_ALL_MODES (0x3f | SELECTMODE | TERM_FOCUS)