refactor!: delete insertmode (#18547)

Neovim already removed `evim` (or any similar flags). The 'insertmode'
option is a weird remnant, so get rid of it.

The 'insertmode' option is replaced with a script that closely emulates
the option. This script is documented at :help 'insertmode'
This commit is contained in:
Gregory Anders
2022-05-22 21:20:18 -06:00
committed by GitHub
parent a7e0a02031
commit 9e1ee9fb1d
29 changed files with 62 additions and 270 deletions

View File

@@ -459,9 +459,8 @@ static void insert_enter(InsertState *s)
where_paste_started.lnum = 0;
can_cindent = true;
// The cursor line is not in a closed fold, unless 'insertmode' is set or
// restarting.
if (!p_im && did_restart_edit == 0) {
// The cursor line is not in a closed fold, unless restarting.
if (did_restart_edit == 0) {
foldOpenCursor();
}
@@ -473,7 +472,7 @@ static void insert_enter(InsertState *s)
s->i = showmode();
}
if (!p_im && did_restart_edit == 0) {
if (did_restart_edit == 0) {
change_warning(curbuf, s->i == 0 ? 0 : s->i + 1);
}
@@ -554,7 +553,7 @@ static int insert_check(VimState *state)
}
if (stop_insert_mode && !compl_started) {
// ":stopinsert" used or 'insertmode' reset
// ":stopinsert" used
s->count = 0;
return 0; // exit insert mode
}
@@ -756,7 +755,6 @@ static int insert_execute(VimState *state, int key)
}
// CTRL-\ CTRL-N goes to Normal mode,
// CTRL-\ CTRL-G goes to mode selected with 'insertmode',
// CTRL-\ CTRL-O is like CTRL-O but without moving the cursor
if (s->c == Ctrl_BSL) {
// may need to redraw when no more chars available now
@@ -770,8 +768,6 @@ static int insert_execute(VimState *state, int key)
// it's something else
vungetc(s->c);
s->c = Ctrl_BSL;
} else if (s->c == Ctrl_G && p_im) {
return 1; // continue
} else {
if (s->c == Ctrl_O) {
ins_ctrl_o();
@@ -843,16 +839,6 @@ static int insert_execute(VimState *state, int key)
}
/// Return true when need to go to Insert mode because of 'insertmode'.
///
/// Don't do this when still processing a command or a mapping.
/// Don't do this when inside a ":normal" command.
bool goto_im(void)
FUNC_ATTR_PURE
{
return p_im && stuff_empty() && typebuf_typed();
}
static int insert_handle_key(InsertState *s)
{
// The big switch to handle a character in insert mode.
@@ -884,26 +870,10 @@ static int insert_handle_key(InsertState *s)
}
}
// when 'insertmode' set, and not halfway through a mapping, don't leave
// Insert mode
if (goto_im()) {
if (got_int) {
(void)vgetc(); // flush all buffers
got_int = false;
} else {
vim_beep(BO_IM);
}
break;
}
return 0; // exit insert mode
case Ctrl_Z: // suspend when 'insertmode' set
if (!p_im) {
goto normalchar; // insert CTRL-Z as normal char
}
do_cmdline_cmd("stop");
ui_cursor_shape(); // may need to update cursor shape
break;
case Ctrl_Z:
goto normalchar; // insert CTRL-Z as normal char
case Ctrl_O: // execute one command
if (ctrl_x_mode == CTRL_X_OMNI) {
@@ -939,9 +909,6 @@ static int insert_handle_key(InsertState *s)
case K_F1:
case K_XF1:
stuffcharReadbuff(K_HELP);
if (p_im) {
need_start_insertmode = true;
}
return 0; // exit insert mode
@@ -956,7 +923,7 @@ static int insert_handle_key(InsertState *s)
// For ^@ the trailing ESC will end the insert, unless there is an
// error.
if (stuff_inserted(NUL, 1L, (s->c == Ctrl_A)) == FAIL
&& s->c != Ctrl_A && !p_im) {
&& s->c != Ctrl_A) {
return 0; // exit insert mode
}
s->inserted_space = false;
@@ -1236,7 +1203,7 @@ check_pum:
}
break;
}
if (!ins_eol(s->c) && !p_im) {
if (!ins_eol(s->c)) {
return 0; // out of memory
}
auto_format(false, false);
@@ -1288,13 +1255,6 @@ check_pum:
case Ctrl_L: // Whole line completion after ^X
if (ctrl_x_mode != CTRL_X_WHOLE_LINE) {
// CTRL-L with 'insertmode' set: Leave Insert mode
if (p_im) {
if (echeck_abbr(Ctrl_L + ABBR_OFF)) {
break;
}
return 0; // exit insert mode
}
goto normalchar;
}
FALLTHROUGH;
@@ -7964,10 +7924,8 @@ static bool ins_esc(long *count, int cmdchar, bool nomove)
}
if (!arrow_used) {
// Don't append the ESC for "r<CR>" and "grx".
// When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
// when "count" is non-zero.
if (cmdchar != 'r' && cmdchar != 'v') {
AppendToRedobuff(p_im ? "\014" : ESC_STR);
AppendToRedobuff(ESC_STR);
}
/*

View File

@@ -2880,10 +2880,6 @@ int do_ecmd(int fnum, char *ffname, char *sfname, exarg_T *eap, linenr_T newlnum
redraw_curbuf_later(NOT_VALID); // redraw this buffer later
}
if (p_im && (State & MODE_INSERT) == 0) {
need_start_insertmode = true;
}
// Change directories when the 'acd' option is set.
do_autochdir();
@@ -4909,9 +4905,8 @@ void ex_help(exarg_T *eap)
}
}
if (!p_im) {
restart_edit = 0; // don't want insert mode in help file
}
restart_edit = 0; // don't want insert mode in help file
// Restore KeyTyped, setting 'filetype=help' may reset it.
// It is needed for do_tag top open folds under the cursor.
KeyTyped = old_KeyTyped;

View File

@@ -8979,7 +8979,6 @@ bool save_current_state(save_state_T *sst)
sst->save_restart_edit = restart_edit;
sst->save_msg_didout = msg_didout;
sst->save_State = State;
sst->save_insertmode = p_im;
sst->save_finish_op = finish_op;
sst->save_opcount = opcount;
sst->save_reg_executing = reg_executing;
@@ -8987,7 +8986,6 @@ bool save_current_state(save_state_T *sst)
msg_scroll = false; // no msg scrolling in Normal mode
restart_edit = 0; // don't go to Insert mode
p_im = false; // don't use 'insertmode
// Save the current typeahead. This is required to allow using ":normal"
// from an event handler and makes sure we don't hang when the argument
@@ -9010,7 +9008,6 @@ void restore_current_state(save_state_T *sst)
// override the value of restart_edit anyway.
restart_edit = sst->save_restart_edit;
}
p_im = sst->save_insertmode;
finish_op = sst->save_finish_op;
opcount = sst->save_opcount;
reg_executing = sst->save_reg_executing;

View File

@@ -25,7 +25,6 @@ typedef struct {
int save_restart_edit;
bool save_msg_didout;
int save_State;
int save_insertmode;
bool save_finish_op;
long save_opcount;
int save_reg_executing;

View File

@@ -1336,8 +1336,7 @@ static int command_line_execute(VimState *state, int key)
}
}
// CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
// mode when 'insertmode' is set, CTRL-\ e prompts for an expression.
// CTRL-\ CTRL-N goes to Normal mode, CTRL-\ e prompts for an expression.
if (s->c == Ctrl_BSL) {
no_mapping++;
allow_keys++;
@@ -1399,9 +1398,6 @@ static int command_line_execute(VimState *state, int key)
redrawcmd();
return command_line_not_changed(s);
} else {
if (s->c == Ctrl_G && p_im && restart_edit == 0) {
restart_edit = 'a';
}
s->gotesc = true; // will free ccline.cmdbuff after putting it
// in history
return 0; // back to Normal mode

View File

@@ -1352,14 +1352,12 @@ void openscript(char_u *name, bool directly)
int oldcurscript;
int save_State = State;
int save_restart_edit = restart_edit;
int save_insertmode = p_im;
int save_finish_op = finish_op;
int save_msg_scroll = msg_scroll;
State = MODE_NORMAL;
msg_scroll = false; // no msg scrolling in Normal mode
restart_edit = 0; // don't go to Insert mode
p_im = false; // don't use 'insertmode'
clear_oparg(&oa);
finish_op = false;
@@ -1373,7 +1371,6 @@ void openscript(char_u *name, bool directly)
State = save_State;
msg_scroll = save_msg_scroll;
restart_edit = save_restart_edit;
p_im = save_insertmode;
finish_op = save_finish_op;
}
}
@@ -2513,16 +2510,12 @@ static int vgetorpeek(bool advance)
timedout = true;
continue;
}
// When 'insertmode' is set, ESC just beeps in Insert
// mode. Use CTRL-L to make edit() return.
// In Ex-mode \n is compatible with original Vim behaviour.
// For the command line only CTRL-C always breaks it.
// For the cmdline window: Alternate between ESC and
// CTRL-C: ESC for most situations and CTRL-C to close the
// cmdline window.
if (p_im && (State & MODE_INSERT)) {
c = Ctrl_L;
} else if ((State & MODE_CMDLINE) || (cmdwin_type > 0 && tc == ESC)) {
if ((State & MODE_CMDLINE) || (cmdwin_type > 0 && tc == ESC)) {
c = Ctrl_C;
} else {
c = ESC;

View File

@@ -530,11 +530,6 @@ int main(int argc, char **argv)
// 'autochdir' has been postponed.
do_autochdir();
// start in insert mode
if (p_im) {
need_start_insertmode = true;
}
set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
apply_autocmds(EVENT_VIMENTER, NULL, NULL, false, curbuf);
TIME_MSG("VimEnter autocommands");

View File

@@ -1126,9 +1126,6 @@ static int normal_execute(VimState *state, int key)
if (s->ca.nchar == ESC) {
clearop(&s->oa);
if (restart_edit == 0 && goto_im()) {
restart_edit = 'a';
}
s->command_finished = true;
goto finish;
}
@@ -1178,14 +1175,6 @@ static void normal_check_stuff_buffer(NormalState *s)
// if wait_return still needed call it now
wait_return(false);
}
if (need_start_insertmode && goto_im() && !VIsual_active) {
need_start_insertmode = false;
stuffReadbuff("i"); // start insert mode next
// skip the fileinfo message now, because it would be shown
// after insert mode finishes!
need_fileinfo = false;
}
}
}
@@ -3904,7 +3893,6 @@ static void nv_regreplay(cmdarg_T *cap)
/// Handle a ":" command and <Cmd> or Lua keymaps.
static void nv_colon(cmdarg_T *cap)
{
int old_p_im;
bool cmd_result;
bool is_cmdkey = cap->cmdchar == K_COMMAND;
bool is_lua = cap->cmdchar == K_LUA;
@@ -3930,8 +3918,6 @@ static void nv_colon(cmdarg_T *cap)
compute_cmdrow();
}
old_p_im = p_im;
if (is_lua) {
cmd_result = map_execute_lua();
} else {
@@ -3940,15 +3926,6 @@ static void nv_colon(cmdarg_T *cap)
cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
}
// If 'insertmode' changed, enter or exit Insert mode
if (p_im != old_p_im) {
if (p_im) {
restart_edit = 'i';
} else {
restart_edit = 0;
}
}
if (cmd_result == false) {
// The Ex command failed, do not execute the operator.
clearop(cap->oap);
@@ -6735,10 +6712,6 @@ static void nv_normal(cmdarg_T *cap)
end_visual_mode(); // stop Visual
redraw_curbuf_later(INVERTED);
}
// CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
if (cap->nchar == Ctrl_G && p_im) {
restart_edit = 'a';
}
} else {
clearopbeep(cap->oap);
}
@@ -6753,8 +6726,7 @@ static void nv_esc(cmdarg_T *cap)
no_reason = (cap->oap->op_type == OP_NOP
&& cap->opcount == 0
&& cap->count0 == 0
&& cap->oap->regname == 0
&& !p_im);
&& cap->oap->regname == 0);
if (cap->arg) { // true for CTRL-C
if (restart_edit == 0
@@ -6771,9 +6743,8 @@ static void nv_esc(cmdarg_T *cap)
// Don't reset "restart_edit" when 'insertmode' is set, it won't be
// set again below when halfway through a mapping.
if (!p_im) {
restart_edit = 0;
}
restart_edit = 0;
if (cmdwin_type != 0) {
cmdwin_result = K_IGNORE;
got_int = false; // don't stop executing autocommands et al.
@@ -6796,13 +6767,6 @@ static void nv_esc(cmdarg_T *cap)
vim_beep(BO_ESC);
}
clearop(cap->oap);
// A CTRL-C is often used at the start of a menu. When 'insertmode' is
// set return to Insert mode afterwards.
if (restart_edit == 0 && goto_im()
&& ex_normal_busy == 0) {
restart_edit = 'a';
}
}
// Move the cursor for the "A" command.
@@ -6837,8 +6801,7 @@ static void nv_edit(cmdarg_T *cap)
} else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
&& (cap->oap->op_type != OP_NOP || VIsual_active)) {
nv_object(cap);
} else if (!curbuf->b_p_ma && !p_im && !curbuf->terminal) {
// Only give this error when 'insertmode' is off.
} else if (!curbuf->b_p_ma && !curbuf->terminal) {
emsg(_(e_modifiable));
clearop(cap->oap);
} else if (!checkclearopq(cap->oap)) {

View File

@@ -6719,7 +6719,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank)
// remember it to make 'insertmode' work with mappings for
// Visual mode. But do this only once and not when typed and
// 'insertmode' isn't set.
if (p_im || !KeyTyped) {
if (!KeyTyped) {
restart_edit_save = restart_edit;
} else {
restart_edit_save = 0;

View File

@@ -4052,21 +4052,6 @@ static char *set_bool_option(const int opt_idx, char_u *const varp, const int va
} else if ((int *)varp == &p_paste) {
// when 'paste' is set or reset also change other options
paste_option_changed();
} else if ((int *)varp == &p_im) {
// when 'insertmode' is set from an autocommand need to do work here
if (p_im) {
if ((State & MODE_INSERT) == 0) {
need_start_insertmode = true;
}
stop_insert_mode = false;
} else if (old_value) { // only reset if it was set previously
need_start_insertmode = false;
stop_insert_mode = true;
if (restart_edit != 0 && mode_displayed) {
clear_cmdline = true; // remove "(insert)"
}
restart_edit = 0;
}
} else if ((int *)varp == &p_ic && p_hls) {
// when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
redraw_all_later(SOME_VALID);

View File

@@ -334,9 +334,9 @@ EXTERN unsigned bo_flags;
#ifdef IN_OPTION_C
static char *(p_bo_values[]) = { "all", "backspace", "cursor", "complete",
"copy", "ctrlg", "error", "esc", "ex",
"hangul", "insertmode", "lang", "mess",
"showmatch", "operator", "register", "shell",
"spell", "wildmode", NULL };
"hangul", "lang", "mess", "showmatch",
"operator", "register", "shell", "spell",
"wildmode", NULL };
#endif
// values for the 'belloff' option
@@ -485,7 +485,6 @@ EXTERN char_u *p_iconstring; // 'iconstring'
EXTERN int p_ic; // 'ignorecase'
EXTERN int p_is; // 'incsearch'
EXTERN char_u *p_icm; // 'inccommand'
EXTERN int p_im; // 'insertmode'
EXTERN char_u *p_isf; // 'isfname'
EXTERN char_u *p_isi; // 'isident'
EXTERN char_u *p_isp; // 'isprint'

View File

@@ -1250,9 +1250,9 @@ return {
},
{
full_name='insertmode', abbreviation='im',
short_desc=N_("start the edit of a file in Insert mode"),
short_desc=N_("No description"),
type='bool', scope={'global'},
varname='p_im',
varname='p_force_off',
defaults={if_true=false}
},
{

View File

@@ -2540,9 +2540,7 @@ static int jump_to_help_window(qf_info_T *qi, bool newwin, int *opened_window)
}
}
if (!p_im) {
restart_edit = 0; // don't want insert mode in help file
}
restart_edit = 0; // don't want insert mode in help file
return OK;
}

View File

@@ -16,8 +16,9 @@ func Test_edit_00b()
call setline(1, ['abc '])
inoreabbr <buffer> h here some more
call cursor(1, 4)
" <c-l> expands the abbreviation and ends insertmode
call feedkeys(":set im\<cr> h\<c-l>:set noim\<cr>", 'tix')
" <esc> expands the abbreviation and ends insert mode
" call feedkeys(":set im\<cr> h\<c-l>:set noim\<cr>", 'tix')
call feedkeys("i h\<esc>", 'tix')
call assert_equal(['abc here some more '], getline(1,'$'))
iunabbr <buffer> h
bw!
@@ -234,15 +235,18 @@ func Test_edit_09()
call setline(1, ['abc', 'def', 'ghi'])
call cursor(1, 1)
" 1) CTRL-\ CTLR-N
call feedkeys(":set im\<cr>\<c-\>\<c-n>ccABC\<c-l>", 'txin')
" call feedkeys(":set im\<cr>\<c-\>\<c-n>ccABC\<c-l>", 'txin')
call feedkeys("i\<c-\>\<c-n>ccABC\<esc>", 'txin')
call assert_equal(['ABC', 'def', 'ghi'], getline(1,'$'))
call setline(1, ['ABC', 'def', 'ghi'])
" 2) CTRL-\ CTLR-G
call feedkeys("j0\<c-\>\<c-g>ZZZ\<cr>\<c-l>", 'txin')
call assert_equal(['ABC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
call feedkeys("I\<c-\>\<c-g>YYY\<c-l>", 'txin')
call assert_equal(['ABC', 'ZZZ', 'YYYdef', 'ghi'], getline(1,'$'))
set noinsertmode
" CTRL-\ CTRL-G goes to Insert mode when 'insertmode' is set, but
" 'insertmode' is now removed so skip this test
" call feedkeys("j0\<c-\>\<c-g>ZZZ\<cr>\<esc>", 'txin')
" call assert_equal(['ABC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
" call feedkeys("I\<c-\>\<c-g>YYY\<c-l>", 'txin')
" call assert_equal(['ABC', 'ZZZ', 'YYYdef', 'ghi'], getline(1,'$'))
" set noinsertmode
" 3) CTRL-\ CTRL-O
call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
call cursor(1, 1)
@@ -1043,7 +1047,8 @@ endfunc
func Test_edit_F1()
" Pressing <f1>
new
call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
" call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
call feedkeys("i\<f1>\<esc>", 'tnix')
set noinsertmode
call assert_equal('help', &buftype)
bw

View File

@@ -133,7 +133,8 @@ func Test_normal02_selectmode2()
" some basic select mode tests
call Setup_NewWindow()
50
call feedkeys(":set im\n\<c-o>gHc\<c-o>:set noim\n", 'tx')
" call feedkeys(":set im\n\<c-o>gHc\<c-o>:set noim\n", 'tx')
call feedkeys("i\<c-o>gHc\<esc>", 'tx')
call assert_equal('c51', getline('.'))
" clean up
bw!
@@ -2113,11 +2114,11 @@ fun! Test_normal40_ctrl_bsl()
call assert_equal('n', mode())
call assert_equal(1, col('.'))
"imap <buffer> , <c-\><c-n>
set im
" set im
exe ":norm! \<c-\>\<c-n>dw"
set noim
" set noim
call assert_equal('are some words', getline(1))
call assert_false(&insertmode)
" call assert_false(&insertmode)
" clean up
bw!