blob: 41154389ea4b9864e60788ae9d8b0f874d0d17c1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#define BACKSPACE_CHAR 1
17#define BACKSPACE_WORD 2
18#define BACKSPACE_WORD_NOT_SPACE 3
19#define BACKSPACE_LINE 4
20
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010021// Set when doing something for completion that may call edit() recursively,
22// which is not allowed.
Bram Moolenaar7591bb32019-03-30 13:53:47 +010023static int compl_busy = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024
25
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010026static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +020027#ifdef FEAT_JOB_CHANNEL
28static void init_prompt(int cmdchar_todo);
29#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static void insert_special(int, int, int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static void redo_literal(int c);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +000033#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010034static void check_spell_redraw(void);
Bram Moolenaar217ad922005-03-20 22:37:15 +000035#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
37static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static void replace_flush(void);
40static void replace_do_bs(int limit_col);
41static int del_char_after_col(int limit_col);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static void ins_reg(void);
43static void ins_ctrl_g(void);
44static void ins_ctrl_hat(void);
45static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010049static int ins_start_select(int c);
50static void ins_insert(int replaceState);
51static void ins_ctrl_o(void);
52static void ins_shift(int c, int lastc);
53static void ins_del(void);
54static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000055#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010056static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000057#endif
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010058static void ins_left(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void ins_home(int c);
60static void ins_end(int c);
61static void ins_s_left(void);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010062static void ins_right(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010063static void ins_s_right(void);
64static void ins_up(int startcol);
65static void ins_pageup(void);
66static void ins_down(int startcol);
67static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010069static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static int ins_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010073static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010075static int ins_ctrl_ey(int tc);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010076#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010077static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +010078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010080static colnr_T Insstart_textlen; // length of line when insert started
81static colnr_T Insstart_blank_vcol; // vcol for first inserted blank
82static int update_Insstart_orig = TRUE; // set Insstart_orig to Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010084static char_u *last_insert = NULL; // the text of the previous insert,
85 // K_SPECIAL and CSI are escaped
86static int last_insert_skip; // nr of chars in front of previous insert
87static int new_insert_skip; // nr of chars in front of current insert
88static int did_restart_edit; // "restart_edit" when calling edit()
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
90#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010091static int can_cindent; // may do cindenting on this line
Bram Moolenaar071d4272004-06-13 20:20:40 +000092#endif
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010095static int revins_on; // reverse insert mode on
96static int revins_chars; // how much to skip after edit
97static int revins_legal; // was the last char 'legal'?
98static int revins_scol; // start column of revins session
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100101static int ins_need_undo; // call u_save() before inserting a
102 // char. Set when edit() is called.
103 // after that arrow_used is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100105static int dont_sync_undo = FALSE; // CTRL-G U prevents syncing undo for
106 // the next left/right cursor key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108/*
109 * edit(): Start inserting text.
110 *
111 * "cmdchar" can be:
112 * 'i' normal insert command
113 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100114 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 * 'R' replace command
116 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
117 * but still only one <CR> is inserted. The <Esc> is not used for redo.
118 * 'g' "gI" command.
119 * 'V' "gR" command for Virtual Replace mode.
120 * 'v' "gr" command for single character Virtual Replace mode.
121 *
122 * This function is not called recursively. For CTRL-O commands, it returns
123 * and lets the caller handle the Normal-mode command.
124 *
125 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
126 */
127 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100128edit(
129 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100130 int startln, // if set, insert at start of line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100131 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
133 int c = 0;
134 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100135 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000136 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 int i;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100139 int did_backspace = TRUE; // previous char was backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100141 int line_is_white = FALSE; // line is empty before insert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100143 linenr_T old_topline = 0; // topline before insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_DIFF
145 int old_topfill = -1;
146#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100147 int inserted_space = FALSE; // just inserted a space
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 int replaceState = REPLACE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100149 int nomove = FALSE; // don't move cursor on return
Bram Moolenaarf2732452018-06-03 14:47:35 +0200150#ifdef FEAT_JOB_CHANNEL
151 int cmdchar_todo = cmdchar;
152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100154 // Remember whether editing was restarted after CTRL-O.
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000155 did_restart_edit = restart_edit;
156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100157 // sleep before redrawing, needed for "CTRL-O :" that results in an
158 // error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 check_for_delay(TRUE);
160
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100161 // set Insstart_orig to Insstart
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100162 update_Insstart_orig = TRUE;
163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#ifdef HAVE_SANDBOX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100165 // Don't allow inserting in the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 if (sandbox != 0)
167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100168 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 return FALSE;
170 }
171#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100172 // Don't allow changes in the buffer while editing the cmdline. The
173 // caller of getcmdline() may get confused.
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100174 // Don't allow recursive insert mode when busy with completion.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200175 if (textwinlock != 0 || textlock != 0
176 || ins_compl_active() || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000177 {
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200178 emsg(_(e_textwinlock));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000179 return FALSE;
180 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100181 ins_compl_clear(); // clear stuff for CTRL-X mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar843ee412004-06-30 16:16:41 +0000183 /*
184 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
185 */
186 if (cmdchar != 'r' && cmdchar != 'v')
187 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100188 pos_T save_cursor = curwin->w_cursor;
189
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100190#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000191 if (cmdchar == 'R')
192 ptr = (char_u *)"r";
193 else if (cmdchar == 'V')
194 ptr = (char_u *)"v";
195 else
196 ptr = (char_u *)"i";
197 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100198 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100199#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200200 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100202 // Make sure the cursor didn't move. Do call check_cursor_col() in
203 // case the text was modified. Since Insert mode was not started yet
204 // a call to check_cursor_col() may move the cursor, especially with
205 // the "A" command, thus set State to avoid that. Also check that the
206 // line number is still valid (lines may have been deleted).
207 // Do not restore if v:char was set to a non-empty string.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100208 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100209#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200210 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100211#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200212 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100213 {
214 int save_state = State;
215
216 curwin->w_cursor = save_cursor;
217 State = INSERT;
218 check_cursor_col();
219 State = save_state;
220 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000221 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000222
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200223#ifdef FEAT_CONCEAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100224 // Check if the cursor line needs redrawing before changing State. If
225 // 'concealcursor' is "n" it needs to be redrawn without concealing.
Bram Moolenaarb9464822018-05-10 15:09:49 +0200226 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200227#endif
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 /*
230 * When doing a paste with the middle mouse button, Insstart is set to
231 * where the paste started.
232 */
233 if (where_paste_started.lnum != 0)
234 Insstart = where_paste_started;
235 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {
237 Insstart = curwin->w_cursor;
238 if (startln)
239 Insstart.col = 0;
240 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000241 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 Insstart_blank_vcol = MAXCOL;
243 if (!did_ai)
244 ai_col = 0;
245
246 if (cmdchar != NUL && restart_edit == 0)
247 {
248 ResetRedobuff();
249 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 if (cmdchar == 'V' || cmdchar == 'v')
251 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100252 // "gR" or "gr" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 AppendCharToRedobuff('g');
254 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
255 }
256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100258 if (cmdchar == K_PS)
259 AppendCharToRedobuff('a');
260 else
261 AppendCharToRedobuff(cmdchar);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100262 if (cmdchar == 'g') // "gI" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 AppendCharToRedobuff('I');
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100264 else if (cmdchar == 'r') // "r<CR>" command
265 count = 1; // insert only one <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 }
267 }
268
269 if (cmdchar == 'R')
270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 State = REPLACE;
272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 else if (cmdchar == 'V' || cmdchar == 'v')
274 {
275 State = VREPLACE;
276 replaceState = VREPLACE;
277 orig_line_count = curbuf->b_ml.ml_line_count;
278 vr_lines_changed = 1;
279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 else
281 State = INSERT;
282
283 stop_insert_mode = FALSE;
284
285 /*
286 * Need to recompute the cursor position, it might move when the cursor is
287 * on a TAB or special character.
288 */
289 curs_columns(TRUE);
290
291 /*
292 * Enable langmap or IME, indicated by 'iminsert'.
293 * Note that IME may enabled/disabled without us noticing here, thus the
294 * 'iminsert' value may not reflect what is actually used. It is updated
295 * when hitting <Esc>.
296 */
297 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
298 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100299#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
301#endif
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#ifdef FEAT_CMDL_INFO
305 clear_showcmd();
306#endif
307#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100308 // there is no reverse replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 revins_on = (State == INSERT && p_ri);
310 if (revins_on)
311 undisplay_dollar();
312 revins_chars = 0;
313 revins_legal = 0;
314 revins_scol = -1;
315#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100316 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100317 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200318#ifdef FEAT_JOB_CHANNEL
319 ch_log_output = TRUE;
320#endif
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100321 // Disable bracketed paste mode, we won't recognize the escape
322 // sequences.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100323 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100325 // Disable modifyOtherKeys, keys with modifiers would cause exiting
326 // Insert mode.
327 out_str(T_CTE);
328 }
329
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 /*
331 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100332 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
333 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 */
335 if (restart_edit != 0 && stuff_empty())
336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 /*
338 * After a paste we consider text typed to be part of the insert for
339 * the pasted text. You can backspace over the pasted text too.
340 */
341 if (where_paste_started.lnum)
342 arrow_used = FALSE;
343 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 arrow_used = TRUE;
345 restart_edit = 0;
346
347 /*
348 * If the cursor was after the end-of-line before the CTRL-O and it is
349 * now at the end-of-line, put it after the end-of-line (this is not
350 * correct in very rare cases).
351 * Also do this if curswant is greater than the current virtual
352 * column. Eg after "^O$" or "^O80|".
353 */
354 validate_virtcol();
355 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000356 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 || curwin->w_curswant > curwin->w_virtcol)
358 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
359 {
360 if (ptr[1] == NUL)
361 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 else if (has_mbyte)
363 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000364 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (ptr[i] == NUL)
366 curwin->w_cursor.col += i;
367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000369 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371 else
372 arrow_used = FALSE;
373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100374 // we are in insert mode now, don't need to start it anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 need_start_insertmode = FALSE;
376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100377 // Need to save the line for undo before inserting the first char.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 ins_need_undo = TRUE;
379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 where_paste_started.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#ifdef FEAT_CINDENT
382 can_cindent = TRUE;
383#endif
384#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100385 // The cursor line is not in a closed fold, unless 'insertmode' is set or
386 // restarting.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 if (!p_im && did_restart_edit == 0)
388 foldOpenCursor();
389#endif
390
391 /*
392 * If 'showmode' is set, show the current (insert/replace/..) mode.
393 * A warning message for changing a readonly file is given here, before
394 * actually changing anything. It's put after the mode, if any.
395 */
396 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000397 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 i = showmode();
399
400 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000401 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
403#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100404 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#endif
406#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100407 do_digraph(-1); // clear digraphs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408#endif
409
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000410 /*
411 * Get the current length of the redo buffer, those characters have to be
412 * skipped if we want to get to the inserted characters.
413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 ptr = get_inserted();
415 if (ptr == NULL)
416 new_insert_skip = 0;
417 else
418 {
419 new_insert_skip = (int)STRLEN(ptr);
420 vim_free(ptr);
421 }
422
423 old_indent = 0;
424
425 /*
426 * Main loop in Insert mode: repeat until Insert mode is left.
427 */
428 for (;;)
429 {
430#ifdef FEAT_RIGHTLEFT
431 if (!revins_legal)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100432 revins_scol = -1; // reset on illegal motions
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 else
434 revins_legal = 0;
435#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100436 if (arrow_used) // don't repeat insert when arrow key used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 count = 0;
438
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100439 if (update_Insstart_orig)
440 Insstart_orig = Insstart;
441
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200442 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100444 // ":stopinsert" used or 'insertmode' reset
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 count = 0;
446 goto doESCkey;
447 }
448
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100449 // set curwin->w_curswant for next K_DOWN or K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (!arrow_used)
451 curwin->w_set_curswant = TRUE;
452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100453 // If there is no typeahead may check for timestamps (e.g., for when a
454 // menu invoked a shell command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (stuff_empty())
456 {
457 did_check_timestamps = FALSE;
458 if (need_check_timestamps)
459 check_timestamps(FALSE);
460 }
461
462 /*
463 * When emsg() was called msg_scroll will have been set.
464 */
465 msg_scroll = FALSE;
466
467#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100468 // When 'mousefocus' is set a mouse movement may have taken us to
469 // another window. "need_mouse_correct" may then be set because of an
470 // autocommand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 if (need_mouse_correct)
472 gui_mouse_correct();
473#endif
474
475#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100476 // Open fold at the cursor line, according to 'foldopen'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 if (fdo_flags & FDO_INSERT)
478 foldOpenCursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100479 // Close folds where the cursor isn't, according to 'foldclose'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 if (!char_avail())
481 foldCheckClose();
482#endif
483
Bram Moolenaarf2732452018-06-03 14:47:35 +0200484#ifdef FEAT_JOB_CHANNEL
485 if (bt_prompt(curbuf))
486 {
487 init_prompt(cmdchar_todo);
488 cmdchar_todo = NUL;
489 }
490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 /*
493 * If we inserted a character at the last position of the last line in
494 * the window, scroll the window one line up. This avoids an extra
495 * redraw.
496 * This is detected when the cursor column is smaller after inserting
497 * something.
498 * Don't do this when the topline changed already, it has
499 * already been adjusted (by insertchar() calling open_line())).
500 */
501 if (curbuf->b_mod_set
502 && curwin->w_p_wrap
503 && !did_backspace
504 && curwin->w_topline == old_topline
505#ifdef FEAT_DIFF
506 && curwin->w_topfill == old_topfill
507#endif
508 )
509 {
510 mincol = curwin->w_wcol;
511 validate_cursor_col();
512
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200513 if (
514#ifdef FEAT_VARTABS
515 (int)curwin->w_wcol < mincol - tabstop_at(
516 get_nolist_virtcol(), curbuf->b_p_ts,
517 curbuf->b_p_vts_array)
518#else
519 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100522 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 && (curwin->w_cursor.lnum != curwin->w_topline
524#ifdef FEAT_DIFF
525 || curwin->w_topfill > 0
526#endif
527 ))
528 {
529#ifdef FEAT_DIFF
530 if (curwin->w_topfill > 0)
531 --curwin->w_topfill;
532 else
533#endif
534#ifdef FEAT_FOLDING
535 if (hasFolding(curwin->w_topline, NULL, &old_topline))
536 set_topline(curwin, old_topline + 1);
537 else
538#endif
539 set_topline(curwin, curwin->w_topline + 1);
540 }
541 }
542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100543 // May need to adjust w_topline to show the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 update_topline();
545
546 did_backspace = FALSE;
547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100548 validate_cursor(); // may set must_redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550 /*
551 * Redraw the display when no characters are waiting.
552 * Also shows mode, ruler and positions cursor.
553 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000554 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if (curwin->w_p_scb)
557 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar860cae12010-06-05 23:22:07 +0200559 if (curwin->w_p_crb)
560 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 update_curswant();
562 old_topline = curwin->w_topline;
563#ifdef FEAT_DIFF
564 old_topfill = curwin->w_topfill;
565#endif
566
567#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100568 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#endif
570
571 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100572 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100574 if (c != K_CURSORHOLD)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100575 lastc = c; // remember the previous char for CTRL-D
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100577 // After using CTRL-G U the next cursor key will not break undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200578 if (dont_sync_undo == MAYBE)
579 dont_sync_undo = TRUE;
580 else
581 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100582 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100583 // Got here from normal mode when bracketed paste started.
Bram Moolenaarec2da362017-01-21 20:04:22 +0100584 c = K_PS;
585 else
586 do
587 {
588 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200589
590 if (stop_insert_mode)
591 {
592 // Insert mode ended, possibly from a callback.
593 count = 0;
594 nomove = TRUE;
595 goto doESCkey;
596 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100597 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100599 // Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V.
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000600 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#ifdef FEAT_RIGHTLEFT
603 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100604 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000607 /*
608 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000609 * and the cursor is still in the completed word. Only when there is
610 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000611 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100612 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000613 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100614 && curwin->w_cursor.col >= ins_compl_col()
615 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100617 // BS: Delete one character from "compl_leader".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000618 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100619 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000620 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000621 continue;
622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100623 // When no match was selected or it was edited.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100624 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100626 // CTRL-L: Add one character from the current match to
627 // "compl_leader". Except when at the original match and
628 // there is nothing to add, CTRL-L works like CTRL-P then.
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000629 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100630 && (!ctrl_x_mode_line_or_eval()
631 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000632 {
633 ins_compl_addfrommatch();
634 continue;
635 }
636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100637 // A non-white character that fits in with the current
638 // completion: Add to "compl_leader".
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000639 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000640 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100641#if defined(FEAT_EVAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100642 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100643 char_u *str = do_insert_char_pre(c);
644 char_u *p;
645
646 if (str != NULL)
647 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100648 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100649 ins_compl_addleader(PTR2CHAR(p));
650 vim_free(str);
651 }
652 else
653#endif
654 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000655 continue;
656 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100658 // Pressing CTRL-Y selects the current match. When
659 // ins_compl_enter_selects() is set the Enter key does the
660 // same.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100661 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200662 && (c == CAR || c == K_KENTER || c == NL)))
663 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000664 {
665 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200666 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000667 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000668 }
669 }
670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100671 // Prepare for or stop CTRL-X mode. This doesn't do completion, but
672 // it does fix up the text when finishing completion.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100673 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000674 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000675 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100677 // CTRL-\ CTRL-N goes to Normal mode,
678 // CTRL-\ CTRL-G goes to mode selected with 'insertmode',
679 // CTRL-\ CTRL-O is like CTRL-O but without moving the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (c == Ctrl_BSL)
681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100682 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +0000683 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 ++no_mapping;
685 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000686 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 --no_mapping;
688 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000689 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100691 // it's something else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 vungetc(c);
693 c = Ctrl_BSL;
694 }
695 else if (c == Ctrl_G && p_im)
696 continue;
697 else
698 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000699 if (c == Ctrl_O)
700 {
701 ins_ctrl_o();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100702 ins_at_eol = FALSE; // cursor keeps its column
Bram Moolenaar488c6512005-08-11 20:09:58 +0000703 nomove = TRUE;
704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 count = 0;
706 goto doESCkey;
707 }
708 }
709
710#ifdef FEAT_DIGRAPHS
711 c = do_digraph(c);
712#endif
713
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100714 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (c == Ctrl_V || c == Ctrl_Q)
717 {
718 ins_ctrl_v();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100719 c = Ctrl_V; // pretend CTRL-V is last typed character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 continue;
721 }
722
723#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200724 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100726 // A key name preceded by a bang means this key is not to be
727 // inserted. Skip ahead to the re-indenting below.
728 // A key name preceded by a star means that indenting has to be
729 // done before inserting the key.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 line_is_white = inindent(0);
731 if (in_cinkeys(c, '!', line_is_white))
732 goto force_cindent;
733 if (can_cindent && in_cinkeys(c, '*', line_is_white)
734 && stop_arrow() == OK)
735 do_c_expr_indent();
736 }
737#endif
738
739#ifdef FEAT_RIGHTLEFT
740 if (curwin->w_p_rl)
741 switch (c)
742 {
743 case K_LEFT: c = K_RIGHT; break;
744 case K_S_LEFT: c = K_S_RIGHT; break;
745 case K_C_LEFT: c = K_C_RIGHT; break;
746 case K_RIGHT: c = K_LEFT; break;
747 case K_S_RIGHT: c = K_S_LEFT; break;
748 case K_C_RIGHT: c = K_C_LEFT; break;
749 }
750#endif
751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /*
753 * If 'keymodel' contains "startsel", may start selection. If it
754 * does, a CTRL-O and c will be stuffed, we need to get these
755 * characters.
756 */
757 if (ins_start_select(c))
758 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 /*
761 * The big switch to handle a character in insert mode.
762 */
763 switch (c)
764 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100765 case ESC: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (echeck_abbr(ESC + ABBR_OFF))
767 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 case Ctrl_C: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#ifdef FEAT_CMDWIN
772 if (c == Ctrl_C && cmdwin_type != 0)
773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // Close the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 cmdwin_result = K_IGNORE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100776 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000777 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 goto doESCkey;
779 }
780#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200781#ifdef FEAT_JOB_CHANNEL
782 if (c == Ctrl_C && bt_prompt(curbuf))
783 {
784 if (invoke_prompt_interrupt())
785 {
786 if (!bt_prompt(curbuf))
787 // buffer changed to a non-prompt buffer, get out of
788 // Insert mode
789 goto doESCkey;
790 break;
791 }
792 }
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795#ifdef UNIX
796do_intr:
797#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100798 // when 'insertmode' set, and not halfway a mapping, don't leave
799 // Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (goto_im())
801 {
802 if (got_int)
803 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100804 (void)vgetc(); // flush all buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 got_int = FALSE;
806 }
807 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200808 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 break;
810 }
811doESCkey:
812 /*
813 * This is the ONLY return from edit()!
814 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100815 // Always update o_lnum, so that a "CTRL-O ." that adds a line
816 // still puts the cursor back after the inserted text.
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000817 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 o_lnum = curwin->w_cursor.lnum;
819
Bram Moolenaar488c6512005-08-11 20:09:58 +0000820 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000821 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100822 // When CTRL-C was typed got_int will be set, with the result
823 // that the autocommands won't be executed. When mapped got_int
824 // is not set, but let's keep the behavior the same.
825 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200826 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000827 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 continue;
831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 case Ctrl_Z: // suspend when 'insertmode' set
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000833 if (!p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100834 goto normalchar; // insert CTRL-Z as normal char
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100835 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100836#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100837 ui_cursor_shape(); // may need to update cursor shape
Bram Moolenaar74a47162017-02-26 19:09:05 +0100838#endif
839 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 case Ctrl_O: // execute one command
Bram Moolenaare344bea2005-09-01 20:46:49 +0000842#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000844 goto docomplete;
845#endif
846 if (echeck_abbr(Ctrl_O + ABBR_OFF))
847 break;
848 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // don't move the cursor left when 'virtualedit' has "onemore".
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000851 if (ve_flags & VE_ONEMORE)
852 {
853 ins_at_eol = FALSE;
854 nomove = TRUE;
855 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000856 count = 0;
857 goto doESCkey;
858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100859 case K_INS: // toggle insert/replace mode
Bram Moolenaar572cb562005-08-05 21:35:02 +0000860 case K_KINS:
861 ins_insert(replaceState);
862 break;
863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100864 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar572cb562005-08-05 21:35:02 +0000865 break;
866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100867 case K_HELP: // Help key works like <ESC> <Help>
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000868 case K_F1:
869 case K_XF1:
870 stuffcharReadbuff(K_HELP);
871 if (p_im)
872 need_start_insertmode = TRUE;
873 goto doESCkey;
874
875#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100876 case K_F21: // NetBeans command
877 ++no_mapping; // don't map the next key hits
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000878 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000879 --no_mapping;
880 netbeans_keycommand(i);
881 break;
882#endif
883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100884 case K_ZERO: // Insert the previously inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 case NUL:
886 case Ctrl_A:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100887 // For ^@ the trailing ESC will end the insert, unless there is an
888 // error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
890 && c != Ctrl_A && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100891 goto doESCkey; // quit insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 inserted_space = FALSE;
893 break;
894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100895 case Ctrl_R: // insert the contents of a register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ins_reg();
897 auto_format(FALSE, TRUE);
898 inserted_space = FALSE;
899 break;
900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100901 case Ctrl_G: // commands starting with CTRL-G
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ins_ctrl_g();
903 break;
904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 case Ctrl_HAT: // switch input mode and/or langmap
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000906 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 break;
908
909#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 case Ctrl__: // switch between languages
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (!p_ari)
912 goto normalchar;
913 ins_ctrl_();
914 break;
915#endif
916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100917 case Ctrl_D: // Make indent one shiftwidth smaller.
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200918#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100919 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 goto docomplete;
921#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100922 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100924 case Ctrl_T: // Make indent one shiftwidth greater.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000927 if (has_compl_option(FALSE))
928 goto docomplete;
929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 ins_shift(c, lastc);
933 auto_format(FALSE, TRUE);
934 inserted_space = FALSE;
935 break;
936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100937 case K_DEL: // delete character under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 case K_KDEL:
939 ins_del();
940 auto_format(FALSE, TRUE);
941 break;
942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 case K_BS: // delete character before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 case Ctrl_H:
945 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
946 auto_format(FALSE, TRUE);
947 break;
948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100949 case Ctrl_W: // delete word before the cursor
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200950#ifdef FEAT_JOB_CHANNEL
951 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
952 {
953 // In a prompt window CTRL-W is used for window commands.
954 // Use Shift-CTRL-W to delete a word.
955 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200956 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200957 nomove = TRUE;
958 count = 0;
959 goto doESCkey;
960 }
961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
963 auto_format(FALSE, TRUE);
964 break;
965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100966 case Ctrl_U: // delete all inserted text in current line
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000967# ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // CTRL-X CTRL-U completes with 'completefunc'.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100969 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000970 goto docomplete;
971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
973 auto_format(FALSE, TRUE);
974 inserted_space = FALSE;
975 break;
976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100977 case K_LEFTMOUSE: // mouse keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 case K_LEFTMOUSE_NM:
979 case K_LEFTDRAG:
980 case K_LEFTRELEASE:
981 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100982 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 case K_MIDDLEMOUSE:
984 case K_MIDDLEDRAG:
985 case K_MIDDLERELEASE:
986 case K_RIGHTMOUSE:
987 case K_RIGHTDRAG:
988 case K_RIGHTRELEASE:
989 case K_X1MOUSE:
990 case K_X1DRAG:
991 case K_X1RELEASE:
992 case K_X2MOUSE:
993 case K_X2DRAG:
994 case K_X2RELEASE:
995 ins_mouse(c);
996 break;
997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 case K_MOUSEDOWN: // Default action for scroll wheel up: scroll up
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200999 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 break;
1001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 case K_MOUSEUP: // Default action for scroll wheel down: scroll down
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001003 ins_mousescroll(MSCR_UP);
1004 break;
1005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001006 case K_MOUSELEFT: // Scroll wheel left
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001007 ins_mousescroll(MSCR_LEFT);
1008 break;
1009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 case K_MOUSERIGHT: // Scroll wheel right
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001011 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001013
Bram Moolenaarec2da362017-01-21 20:04:22 +01001014 case K_PS:
1015 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1016 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001017 // invoked from normal mode, bail out
Bram Moolenaarec2da362017-01-21 20:04:22 +01001018 goto doESCkey;
1019 break;
1020 case K_PE:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001021 // Got K_PE without K_PS, ignore.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001022 break;
1023
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001024#ifdef FEAT_GUI_TABLINE
1025 case K_TABLINE:
1026 case K_TABMENU:
1027 ins_tabline(c);
1028 break;
1029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001031 case K_IGNORE: // Something mapped to nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 break;
1033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001034 case K_CURSORHOLD: // Didn't type something for a while.
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001035 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001036 did_cursorhold = TRUE;
1037 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001038
Bram Moolenaar4f974752019-02-17 17:44:42 +01001039#ifdef FEAT_GUI_MSWIN
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001040 // On MS-Windows ignore <M-F4>, we get it when closing the window
1041 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001042 case K_F4:
1043 if (mod_mask != MOD_MASK_ALT)
1044 goto normalchar;
1045 break;
1046#endif
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#ifdef FEAT_GUI
1049 case K_VER_SCROLLBAR:
1050 ins_scroll();
1051 break;
1052
1053 case K_HOR_SCROLLBAR:
1054 ins_horscroll();
1055 break;
1056#endif
1057
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001058 case K_HOME: // <Home>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 case K_S_HOME:
1061 case K_C_HOME:
1062 ins_home(c);
1063 break;
1064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001065 case K_END: // <End>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 case K_S_END:
1068 case K_C_END:
1069 ins_end(c);
1070 break;
1071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001072 case K_LEFT: // <Left>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001073 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1074 ins_s_left();
1075 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001076 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 break;
1078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001079 case K_S_LEFT: // <S-Left>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 case K_C_LEFT:
1081 ins_s_left();
1082 break;
1083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001084 case K_RIGHT: // <Right>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001085 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1086 ins_s_right();
1087 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001088 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 break;
1090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001091 case K_S_RIGHT: // <S-Right>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 case K_C_RIGHT:
1093 ins_s_right();
1094 break;
1095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001096 case K_UP: // <Up>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001097 if (pum_visible())
1098 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001099 if (mod_mask & MOD_MASK_SHIFT)
1100 ins_pageup();
1101 else
1102 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 break;
1104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001105 case K_S_UP: // <S-Up>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 case K_PAGEUP:
1107 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001108 if (pum_visible())
1109 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 ins_pageup();
1111 break;
1112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001113 case K_DOWN: // <Down>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001114 if (pum_visible())
1115 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001116 if (mod_mask & MOD_MASK_SHIFT)
1117 ins_pagedown();
1118 else
1119 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001122 case K_S_DOWN: // <S-Down>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 case K_PAGEDOWN:
1124 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001125 if (pum_visible())
1126 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ins_pagedown();
1128 break;
1129
1130#ifdef FEAT_DND
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001131 case K_DROP: // drag-n-drop event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ins_drop();
1133 break;
1134#endif
1135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001136 case K_S_TAB: // When not mapped, use like a normal TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 c = TAB;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001138 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001140 case TAB: // TAB or Complete patterns along path
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001141#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001142 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 goto docomplete;
1144#endif
1145 inserted_space = FALSE;
1146 if (ins_tab())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001147 goto normalchar; // insert TAB as a normal char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 auto_format(FALSE, TRUE);
1149 break;
1150
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001151 case K_KENTER: // <Enter>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 c = CAR;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001153 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 case CAR:
1155 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001156#if defined(FEAT_QUICKFIX)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001157 // In a quickfix window a <CR> jumps to the error under the
1158 // cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (bt_quickfix(curbuf) && c == CAR)
1160 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001161 if (curwin->w_llist_ref == NULL) // quickfix window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001162 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001163 else // location list window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001164 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 break;
1166 }
1167#endif
1168#ifdef FEAT_CMDWIN
1169 if (cmdwin_type != 0)
1170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001171 // Execute the command in the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 cmdwin_result = CAR;
1173 goto doESCkey;
1174 }
1175#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001176#ifdef FEAT_JOB_CHANNEL
1177 if (bt_prompt(curbuf))
1178 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001179 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001180 if (!bt_prompt(curbuf))
1181 // buffer changed to a non-prompt buffer, get out of
1182 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001183 goto doESCkey;
1184 break;
1185 }
1186#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001187 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001188 goto doESCkey; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 auto_format(FALSE, FALSE);
1190 inserted_space = FALSE;
1191 break;
1192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001193 case Ctrl_K: // digraph or keyword completion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001194 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 if (has_compl_option(TRUE))
1197 goto docomplete;
1198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001200#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 c = ins_digraph();
1202 if (c == NUL)
1203 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001205 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001207 case Ctrl_X: // Enter CTRL-X mode
Bram Moolenaar572cb562005-08-05 21:35:02 +00001208 ins_ctrl_x();
1209 break;
1210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001211 case Ctrl_RSB: // Tag name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001212 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 goto normalchar;
1214 goto docomplete;
1215
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001216 case Ctrl_F: // File name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001217 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 goto normalchar;
1219 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001221 case 's': // Spelling completion after ^X
Bram Moolenaar488c6512005-08-11 20:09:58 +00001222 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001223 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001224 goto normalchar;
1225 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 case Ctrl_L: // Whole line completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001228 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001230 // CTRL-L with 'insertmode' set: Leave Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (p_im)
1232 {
1233 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1234 break;
1235 goto doESCkey;
1236 }
1237 goto normalchar;
1238 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001239 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001241 case Ctrl_P: // Do previous/next pattern completion
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 case Ctrl_N:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001243 // if 'complete' is empty then plain ^P is no longer special,
1244 // but it is under other ^X modes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001246 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 goto normalchar;
1249
1250docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001251 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001252#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001253 disable_fold_update++; // don't redraw folds here
Bram Moolenaara6c07602017-03-05 21:18:27 +01001254#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001255 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001256 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001257#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001258 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001259#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001260 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001263 case Ctrl_Y: // copy from previous line or scroll down
1264 case Ctrl_E: // copy from next line or scroll up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001265 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 break;
1267
1268 default:
1269#ifdef UNIX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001270 if (c == intr_char) // special interrupt char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 goto do_intr;
1272#endif
1273
Bram Moolenaare659c952011-05-19 17:25:41 +02001274normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001276 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001278#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001279 if (!p_paste)
1280 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001281 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001282 char_u *str = do_insert_char_pre(c);
1283 char_u *p;
1284
1285 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001286 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001287 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001288 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001289 // Insert the new value of v:char literally.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001290 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001291 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001292 c = PTR2CHAR(p);
1293 if (c == CAR || c == K_KENTER || c == NL)
1294 ins_eol(c);
1295 else
1296 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001297 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001298 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001299 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001300 vim_free(str);
1301 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001302 }
1303
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001304 // If the new value is already inserted or an empty string
1305 // then don't insert any character.
Bram Moolenaare659c952011-05-19 17:25:41 +02001306 if (c == NUL)
1307 break;
1308 }
1309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#ifdef FEAT_SMARTINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001311 // Try to perform smart-indenting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 ins_try_si(c);
1313#endif
1314
1315 if (c == ' ')
1316 {
1317 inserted_space = TRUE;
1318#ifdef FEAT_CINDENT
1319 if (inindent(0))
1320 can_cindent = FALSE;
1321#endif
1322 if (Insstart_blank_vcol == MAXCOL
1323 && curwin->w_cursor.lnum == Insstart.lnum)
1324 Insstart_blank_vcol = get_nolist_virtcol();
1325 }
1326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001327 // Insert a normal character and check for abbreviations on a
1328 // special character. Let CTRL-] expand abbreviations without
1329 // inserting it.
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001330 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001331 // Add ABBR_OFF for characters above 0x100, this is
1332 // what check_abbr() expects.
1333 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1334 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 insert_special(c, FALSE, FALSE);
1337#ifdef FEAT_RIGHTLEFT
1338 revins_legal++;
1339 revins_chars++;
1340#endif
1341 }
1342
1343 auto_format(FALSE, TRUE);
1344
1345#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001346 // When inserting a character the cursor line must never be in a
1347 // closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 foldOpenCursor();
1349#endif
1350 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001351 } // end of switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001353 // If typed something may trigger CursorHoldI again.
Bram Moolenaar245c4102016-04-20 17:37:41 +02001354 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001355#ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001356 // but not in CTRL-X mode, a script can't restore the state
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001357 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001358#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001359 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001360 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001361
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001362 // If the cursor was moved we didn't just insert a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (arrow_used)
1364 inserted_space = FALSE;
1365
1366#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001367 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
1369force_cindent:
1370 /*
1371 * Indent now if a key was typed that is in 'cinkeys'.
1372 */
1373 if (in_cinkeys(c, ' ', line_is_white))
1374 {
1375 if (stop_arrow() == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001376 // re-indent the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 do_c_expr_indent();
1378 }
1379 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001380#endif // FEAT_CINDENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001382 } // for (;;)
1383 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384}
1385
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001386 int
1387ins_need_undo_get(void)
1388{
1389 return ins_need_undo;
1390}
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392/*
1393 * Redraw for Insert mode.
1394 * This is postponed until getting the next character to make '$' in the 'cpo'
1395 * option work correctly.
1396 * Only redraw when there are no characters available. This speeds up
1397 * inserting sequences of characters (e.g., for CTRL-R).
1398 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001399 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001400ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001402#ifdef FEAT_CONCEAL
1403 linenr_T conceal_old_cursor_line = 0;
1404 linenr_T conceal_new_cursor_line = 0;
1405 int conceal_update_lines = FALSE;
1406#endif
1407
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001408 if (char_avail())
1409 return;
1410
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001411 // Trigger CursorMoved if the cursor moved. Not when the popup menu is
1412 // visible, the command might delete it.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001413 if (ready && (has_cursormovedI()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001414# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001415 || popup_visible
1416# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001417# if defined(FEAT_CONCEAL)
1418 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001419# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001420 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001421 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001422 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001423 {
1424# ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001425 // Need to update the screen first, to make sure syntax
1426 // highlighting is correct after making a change (e.g., inserting
1427 // a "(". The autocommand may also require a redraw, so it's done
1428 // again below, unfortunately.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001429 if (syntax_present(curwin) && must_redraw)
1430 update_screen(0);
1431# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001432 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001433 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001434 // Make sure curswant is correct, an autocommand may call
1435 // getcurpos().
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001436 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001437 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001438 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001439#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001440 if (popup_visible)
1441 popup_check_cursor_pos();
1442#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001443# ifdef FEAT_CONCEAL
1444 if (curwin->w_p_cole > 0)
1445 {
1446 conceal_old_cursor_line = last_cursormoved.lnum;
1447 conceal_new_cursor_line = curwin->w_cursor.lnum;
1448 conceal_update_lines = TRUE;
1449 }
1450# endif
1451 last_cursormoved = curwin->w_cursor;
1452 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001454 // Trigger TextChangedI if b_changedtick differs.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001455 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001456 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001457 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001458 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001459 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001460 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001461
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001462 // save and restore curwin and curbuf, in case the autocmd changes them
1463 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001464 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001465 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001466 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001467 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1468 u_save(curwin->w_cursor.lnum,
1469 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001472 // Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1473 // TextChangedI will need to trigger for backwards compatibility, thus use
1474 // different b_last_changedtick* variables.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001475 if (ready && has_textchangedP()
1476 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1477 && pum_visible())
1478 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001479 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001480 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001481
1482 // save and restore curwin and curbuf, in case the autocmd changes them
1483 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001484 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001485 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001486 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001487 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1488 u_save(curwin->w_cursor.lnum,
1489 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001490 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001491
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001492 // Trigger SafeState if nothing is pending.
1493 may_trigger_safestate(ready
1494 && !ins_compl_active()
1495 && !pum_visible());
1496
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001497#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001498 if ((conceal_update_lines
1499 && (conceal_old_cursor_line != conceal_new_cursor_line
1500 || conceal_cursor_line(curwin)))
1501 || need_cursor_line_redraw)
1502 {
1503 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001504 redrawWinline(curwin, conceal_old_cursor_line);
1505 redrawWinline(curwin, conceal_new_cursor_line == 0
1506 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001507 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001508 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001509 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001510#endif
1511 if (must_redraw)
1512 update_screen(0);
1513 else if (clear_cmdline || redraw_cmdline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001514 showmode(); // clear cmdline and show mode
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001515 showruler(FALSE);
1516 setcursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001517 emsg_on_display = FALSE; // may remove error message now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518}
1519
1520/*
1521 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001527 int did_putchar = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001528 int prev_mod_mask = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001530 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00001531 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
1533 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001536 did_putchar = TRUE;
1537 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001538 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
1540#ifdef FEAT_CMDL_INFO
1541 add_to_showcmd_c(Ctrl_V);
1542#endif
1543
1544 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001545 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001546 // when the line fits in 'columns' the '^' is at the start of the next
1547 // line and will not removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001548 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#ifdef FEAT_CMDL_INFO
1550 clear_showcmd();
1551#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001552
1553 if ((c == ESC || c == CSI) && !(prev_mod_mask & MOD_MASK_SHIFT))
1554 // Using CTRL-V: Change any modifyOtherKeys ESC sequence to a normal
1555 // key. Don't do this for CTRL-SHIFT-V.
1556 c = decodeModifyOtherKeys(c);
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 insert_special(c, FALSE, TRUE);
1559#ifdef FEAT_RIGHTLEFT
1560 revins_chars++;
1561 revins_legal++;
1562#endif
1563}
1564
1565/*
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001566 * After getting an ESC or CSI for a literal key: If the typeahead buffer
1567 * contains a modifyOtherKeys sequence then decode it and return the result.
1568 * Otherwise return "c".
1569 * Note that this doesn't wait for characters, they must be in the typeahead
1570 * buffer already.
1571 */
1572 int
1573decodeModifyOtherKeys(int c)
1574{
1575 char_u *p = typebuf.tb_buf + typebuf.tb_off;
1576 int idx;
1577 int form = 0;
1578 int argidx = 0;
1579 int arg[2] = {0, 0};
1580
1581 // Recognize:
1582 // form 0: {lead}{key};{modifier}u
1583 // form 1: {lead}27;{modifier};{key}~
1584 if ((c == CSI || (c == ESC && *p == '[')) && typebuf.tb_len >= 4)
1585 {
1586 idx = (*p == '[');
1587 if (p[idx] == '2' && p[idx + 1] == '7' && p[idx + 2] == ';')
1588 {
1589 form = 1;
1590 idx += 3;
1591 }
1592 while (idx < typebuf.tb_len && argidx < 2)
1593 {
1594 if (p[idx] == ';')
1595 ++argidx;
1596 else if (VIM_ISDIGIT(p[idx]))
1597 arg[argidx] = arg[argidx] * 10 + (p[idx] - '0');
1598 else
1599 break;
1600 ++idx;
1601 }
1602 if (idx < typebuf.tb_len
1603 && p[idx] == (form == 1 ? '~' : 'u')
1604 && argidx == 1)
1605 {
1606 // Match, consume the code.
1607 typebuf.tb_off += idx + 1;
1608 typebuf.tb_len -= idx + 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001609#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1610 if (typebuf.tb_len == 0)
1611 typebuf_was_filled = FALSE;
1612#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001613
1614 mod_mask = decode_modifiers(arg[!form]);
Bram Moolenaar975a8802020-06-06 22:36:24 +02001615 c = merge_modifyOtherKeys(arg[form], &mod_mask);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001616 }
1617 }
1618
1619 return c;
1620}
1621
1622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 * Put a character directly onto the screen. It's not stored in a buffer.
1624 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1625 */
1626static int pc_status;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001627#define PC_STATUS_UNSET 0 // pc_bytes was not set
1628#define PC_STATUS_RIGHT 1 // right halve of double-wide char
1629#define PC_STATUS_LEFT 2 // left halve of double-wide char
1630#define PC_STATUS_SET 3 // pc_bytes was filled
1631static char_u pc_bytes[MB_MAXBYTES + 1]; // saved bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632static int pc_attr;
1633static int pc_row;
1634static int pc_col;
1635
1636 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001637edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 int attr;
1640
1641 if (ScreenLines != NULL)
1642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001643 update_topline(); // just in case w_topline isn't valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 validate_cursor();
1645 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001646 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 else
1648 attr = 0;
1649 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001650 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652#ifdef FEAT_RIGHTLEFT
1653 if (curwin->w_p_rl)
1654 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001655 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (has_mbyte)
1657 {
1658 int fix_col = mb_fix_col(pc_col, pc_row);
1659
1660 if (fix_col != pc_col)
1661 {
1662 screen_putchar(' ', pc_row, fix_col, attr);
1663 --curwin->w_wcol;
1664 pc_status = PC_STATUS_RIGHT;
1665 }
1666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 else
1669#endif
1670 {
1671 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (mb_lefthalve(pc_row, pc_col))
1673 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 }
1675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001676 // save the character to be able to put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1680 pc_status = PC_STATUS_SET;
1681 }
1682 screen_putchar(c, pc_row, pc_col, attr);
1683 }
1684}
1685
Bram Moolenaarf2732452018-06-03 14:47:35 +02001686#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1687/*
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001688 * Return the effective prompt for the specified buffer.
1689 */
1690 char_u *
1691buf_prompt_text(buf_T* buf)
1692{
1693 if (buf->b_prompt_text == NULL)
1694 return (char_u *)"% ";
1695 return buf->b_prompt_text;
1696}
1697
1698/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02001699 * Return the effective prompt for the current buffer.
1700 */
1701 char_u *
1702prompt_text(void)
1703{
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001704 return buf_prompt_text(curbuf);
Bram Moolenaarf2732452018-06-03 14:47:35 +02001705}
1706
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001707
Bram Moolenaarf2732452018-06-03 14:47:35 +02001708/*
1709 * Prepare for prompt mode: Make sure the last line has the prompt text.
1710 * Move the cursor to this line.
1711 */
1712 static void
1713init_prompt(int cmdchar_todo)
1714{
1715 char_u *prompt = prompt_text();
1716 char_u *text;
1717
1718 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1719 text = ml_get_curline();
1720 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1721 {
1722 // prompt is missing, insert it or append a line with it
1723 if (*text == NUL)
1724 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1725 else
1726 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1727 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1728 coladvance((colnr_T)MAXCOL);
1729 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1730 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001731
1732 // Insert always starts after the prompt, allow editing text after it.
1733 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1734 || Insstart_orig.col != (int)STRLEN(prompt))
1735 {
1736 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001737 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001738 Insstart_orig = Insstart;
1739 Insstart_textlen = Insstart.col;
1740 Insstart_blank_vcol = MAXCOL;
1741 arrow_used = FALSE;
1742 }
1743
Bram Moolenaarf2732452018-06-03 14:47:35 +02001744 if (cmdchar_todo == 'A')
1745 coladvance((colnr_T)MAXCOL);
1746 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001747 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001748 // Make sure the cursor is in a valid position.
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001749 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001750}
1751
1752/*
1753 * Return TRUE if the cursor is in the editable position of the prompt line.
1754 */
1755 int
1756prompt_curpos_editable()
1757{
1758 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1759 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1760}
1761#endif
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763/*
1764 * Undo the previous edit_putchar().
1765 */
1766 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001767edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (pc_status == PC_STATUS_RIGHT)
1772 ++curwin->w_wcol;
1773 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001774 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1777 }
1778}
1779
1780/*
1781 * Called when p_dollar is set: display a '$' at the end of the changed text
1782 * Only works when cursor is in the line that changes.
1783 */
1784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 colnr_T save_col;
1788
1789 if (!redrawing())
1790 return;
1791
1792 cursor_off();
1793 save_col = curwin->w_cursor.col;
1794 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (has_mbyte)
1796 {
1797 char_u *p;
1798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001799 // If on the last byte of a multi-byte move to the first byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 p = ml_get_curline();
1801 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1802 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001803 curs_columns(FALSE); // recompute w_wrow and w_wcol
Bram Moolenaar02631462017-09-22 15:20:32 +02001804 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
1806 edit_putchar('$', FALSE);
1807 dollar_vcol = curwin->w_virtcol;
1808 }
1809 curwin->w_cursor.col = save_col;
1810}
1811
1812/*
1813 * Call this function before moving the cursor from the normal insert position
1814 * in insert mode.
1815 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001819 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001821 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001822 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
1824}
1825
1826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 * Truncate the space at the end of a line. This is to be used only in an
1828 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1829 * modes.
1830 */
1831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001832truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833{
1834 int i;
1835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001836 // find start of trailing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +01001837 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
1839 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001840 replace_join(0); // remove a NUL from the replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 line[i + 1] = NUL;
1843}
1844
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845/*
1846 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1847 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001848 * Will attempt not to go before "col" even when there is a composing
1849 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 */
1851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 while ((int)curwin->w_cursor.col > col)
1855 {
1856 curwin->w_cursor.col--;
1857 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001858 replace_do_bs(col);
1859 else if (!del_char_after_col(col))
1860 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001864/*
1865 * Like del_char(), but make sure not to go before column "limit_col".
1866 * Only matters when there are composing characters.
1867 * Return TRUE when something was deleted.
1868 */
1869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001870del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001871{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001872 if (enc_utf8 && limit_col >= 0)
1873 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001874 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001876 // Make sure the cursor is at the start of a character, but
1877 // skip forward again when going too far back because of a
1878 // composing character.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001879 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001880 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001881 {
1882 int l = utf_ptr2len(ml_get_cursor());
1883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001884 if (l == 0) // end of line
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001885 break;
1886 curwin->w_cursor.col += l;
1887 }
1888 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1889 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001890 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001891 }
1892 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001893 (void)del_char(FALSE);
1894 return TRUE;
1895}
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/*
1898 * Next character is interpreted literally.
1899 * A one, two or three digit decimal number is interpreted as its byte value.
1900 * If one or two digits are entered, the next character is given to vungetc().
1901 * For Unicode a character > 255 may be returned.
1902 */
1903 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001904get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
1906 int cc;
1907 int nc;
1908 int i;
1909 int hex = FALSE;
1910 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 if (got_int)
1914 return Ctrl_C;
1915
1916#ifdef FEAT_GUI
1917 /*
1918 * In GUI there is no point inserting the internal code for a special key.
1919 * It is more useful to insert the string "<KEY>" instead. This would
1920 * probably be useful in a text window too, but it would not be
1921 * vi-compatible (maybe there should be an option for it?) -- webb
1922 */
1923 if (gui.in_use)
1924 ++allow_keys;
1925#endif
1926#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001927 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 ++no_mapping; // don't map the next key hits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 cc = 0;
1931 i = 0;
1932 for (;;)
1933 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001934 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001936 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 add_to_showcmd(nc);
1938#endif
1939 if (nc == 'x' || nc == 'X')
1940 hex = TRUE;
1941 else if (nc == 'o' || nc == 'O')
1942 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 else if (nc == 'u' || nc == 'U')
1944 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 else
1946 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001947 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
1949 if (!vim_isxdigit(nc))
1950 break;
1951 cc = cc * 16 + hex2nr(nc);
1952 }
1953 else if (octal)
1954 {
1955 if (nc < '0' || nc > '7')
1956 break;
1957 cc = cc * 8 + nc - '0';
1958 }
1959 else
1960 {
1961 if (!VIM_ISDIGIT(nc))
1962 break;
1963 cc = cc * 10 + nc - '0';
1964 }
1965
1966 ++i;
1967 }
1968
Bram Moolenaar13505972019-01-24 15:04:48 +01001969 if (cc > 255 && unicode == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001970 cc = 255; // limit range to 0-255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 nc = 0;
1972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001973 if (hex) // hex: up to two chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 if (i >= 2)
1976 break;
1977 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001978 else if (unicode) // Unicode: up to four or eight chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1981 break;
1982 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001983 else if (i >= 3) // decimal or octal: up to three chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 break;
1985 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001986 if (i == 0) // no number entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001988 if (nc == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
1990 cc = '\n';
1991 nc = 0;
1992 }
1993 else
1994 {
1995 cc = nc;
1996 nc = 0;
1997 }
1998 }
1999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002000 if (cc == 0) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00002002 if (enc_dbcs && (cc & 0xff) == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002003 cc = '?'; // don't accept an illegal DBCS char, the NUL in the
2004 // second byte will cause trouble!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
2006 --no_mapping;
2007#ifdef FEAT_GUI
2008 if (gui.in_use)
2009 --allow_keys;
2010#endif
2011 if (nc)
2012 vungetc(nc);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002013 got_int = FALSE; // CTRL-C typed after CTRL-V is not an interrupt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 return cc;
2015}
2016
2017/*
2018 * Insert character, taking care of special keys and mod_mask
2019 */
2020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002021insert_special(
2022 int c,
2023 int allow_modmask,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002024 int ctrlv) // c was typed after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025{
2026 char_u *p;
2027 int len;
2028
2029 /*
2030 * Special function key, translate into "<Key>". Up to the last '>' is
2031 * inserted with ins_str(), so as not to replace characters in replace
2032 * mode.
2033 * Only use mod_mask for special keys, to avoid things like <S-Space>,
2034 * unless 'allow_modmask' is TRUE.
2035 */
Bram Moolenaard0573012017-10-28 21:11:06 +02002036#ifdef MACOS_X
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002037 // Command-key never produces a normal key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 if (mod_mask & MOD_MASK_CMD)
2039 allow_modmask = TRUE;
2040#endif
2041 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
2042 {
2043 p = get_special_key_name(c, mod_mask);
2044 len = (int)STRLEN(p);
2045 c = p[len - 1];
2046 if (len > 2)
2047 {
2048 if (stop_arrow() == FAIL)
2049 return;
2050 p[len - 1] = NUL;
2051 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002052 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 ctrlv = FALSE;
2054 }
2055 }
2056 if (stop_arrow() == OK)
2057 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2058}
2059
2060/*
2061 * Special characters in this context are those that need processing other
2062 * than the simple insertion that can be performed here. This includes ESC
2063 * which terminates the insert, and CR/NL which need special processing to
2064 * open up a new line. This routine tries to optimize insertions performed by
2065 * the "redo", "undo" or "put" commands, so it needs to know when it should
2066 * stop and defer processing to the "normal" mechanism.
2067 * '0' and '^' are special, because they can be followed by CTRL-D.
2068 */
2069#ifdef EBCDIC
2070# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2071#else
2072# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2073#endif
2074
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002075/*
2076 * "flags": INSCHAR_FORMAT - force formatting
2077 * INSCHAR_CTRLV - char typed just after CTRL-V
2078 * INSCHAR_NO_FEX - don't use 'formatexpr'
2079 *
2080 * NOTE: passes the flags value straight through to internal_format() which,
2081 * beside INSCHAR_FORMAT (above), is also looking for these:
2082 * INSCHAR_DO_COM - format comments
2083 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002086insertchar(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002087 int c, // character to insert or NUL
2088 int flags, // INSCHAR_FORMAT, etc.
2089 int second_indent) // indent for second line if >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002094 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002096 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099 /*
2100 * Try to break the line in two or more pieces when:
2101 * - Always do this if we have been called to do formatting only.
2102 * - Always do this when 'formatoptions' has the 'a' flag and the line
2103 * ends in white space.
2104 * - Otherwise:
2105 * - Don't do this if inserting a blank
2106 * - Don't do this if an existing character is being replaced, unless
2107 * we're in VREPLACE mode.
2108 * - Do this if the cursor is not on the line where insert started
2109 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2110 * before the insert.
2111 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2112 * before 'textwidth'
2113 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002114 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002115 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002116 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 && *ml_get_cursor() != NUL)
2120 && (curwin->w_cursor.lnum != Insstart.lnum
2121 || ((!has_format_option(FO_INS_LONG)
2122 || Insstart_textlen <= (colnr_T)textwidth)
2123 && (!fo_ins_blank
2124 || Insstart_blank_vcol <= (colnr_T)textwidth
2125 ))))))
2126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002127 // Format with 'formatexpr' when it's set. Use internal formatting
2128 // when 'formatexpr' isn't set or it returns non-zero.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002129#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002130 int do_internal = TRUE;
2131 colnr_T virtcol = get_nolist_virtcol()
2132 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002133
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002134 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2135 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002136 {
2137 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002138 // It may be required to save for undo again, e.g. when setline()
2139 // was called.
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002140 ins_need_undo = TRUE;
2141 }
2142 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002144 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002147 if (c == NUL) // only formatting was wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 return;
2149
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002150 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (did_ai && (int)c == end_comment_pending)
2152 {
2153 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002154 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 int middle_len, end_len;
2156 int i;
2157
2158 /*
2159 * Need to remove existing (middle) comment leader and insert end
2160 * comment leader. First, check what comment leader we can find.
2161 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002162 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002163 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002165 // Skip middle-comment string
2166 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 ++p;
2168 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002169 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002170 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 --middle_len;
2172
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002173 // Find the end-comment string
2174 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 ++p;
2176 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2177
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002178 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002180 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 ;
2182 i++;
2183
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002184 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 i -= middle_len;
2186
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002187 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2189 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002190 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 backspace_until_column(i);
2192
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002193 // Insert the end-comment string, except for the last
2194 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 ins_bytes_len(lead_end, end_len - 1);
2196 }
2197 }
2198 }
2199 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201 did_ai = FALSE;
2202#ifdef FEAT_SMARTINDENT
2203 did_si = FALSE;
2204 can_si = FALSE;
2205 can_si_back = FALSE;
2206#endif
2207
2208 /*
2209 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2210 * This speeds up normal text input considerably.
2211 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2212 * need to re-indent at a ':', or any other character (but not what
2213 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002214 * Don't do this when there an InsertCharPre autocommand is defined,
2215 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002216 * Do the check for InsertCharPre before the call to vpeekc() because the
2217 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 */
2219#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002220 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221#endif
2222
2223 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002225 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 && vpeekc() != NUL
2227 && !(State & REPLACE_FLAG)
2228#ifdef FEAT_CINDENT
2229 && !cindent_on()
2230#endif
2231#ifdef FEAT_RIGHTLEFT
2232 && !p_ri
2233#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002234 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236#define INPUT_BUFLEN 100
2237 char_u buf[INPUT_BUFLEN + 1];
2238 int i;
2239 colnr_T virtcol = 0;
2240
2241 buf[0] = c;
2242 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002243 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 virtcol = get_nolist_virtcol();
2245 /*
2246 * Stop the string when:
2247 * - no more chars available
2248 * - finding a special character (command key)
2249 * - buffer is full
2250 * - running into the 'textwidth' boundary
2251 * - need to check for abbreviation: A non-word char after a word-char
2252 */
2253 while ( (c = vpeekc()) != NUL
2254 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 && i < INPUT_BUFLEN
2257 && (textwidth == 0
2258 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2259 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2260 {
2261#ifdef FEAT_RIGHTLEFT
2262 c = vgetc();
2263 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002264 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 buf[i++] = c;
2266#else
2267 buf[i++] = vgetc();
2268#endif
2269 }
2270
2271#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002272 do_digraph(-1); // clear digraphs
2273 do_digraph(buf[i-1]); // may be the start of a digraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#endif
2275 buf[i] = NUL;
2276 ins_str(buf);
2277 if (flags & INSCHAR_CTRLV)
2278 {
2279 redo_literal(*buf);
2280 i = 1;
2281 }
2282 else
2283 i = 0;
2284 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002285 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287 else
2288 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002289 int cc;
2290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2292 {
2293 char_u buf[MB_MAXBYTES + 1];
2294
2295 (*mb_char2bytes)(c, buf);
2296 buf[cc] = NUL;
2297 ins_char_bytes(buf, cc);
2298 AppendCharToRedobuff(c);
2299 }
2300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 ins_char(c);
2303 if (flags & INSCHAR_CTRLV)
2304 redo_literal(c);
2305 else
2306 AppendCharToRedobuff(c);
2307 }
2308 }
2309}
2310
2311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 * Put a character in the redo buffer, for when just after a CTRL-V.
2313 */
2314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002315redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
2317 char_u buf[10];
2318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002319 // Only digits need special treatment. Translate them into a string of
2320 // three digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (VIM_ISDIGIT(c))
2322 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002323 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 AppendToRedobuff(buf);
2325 }
2326 else
2327 AppendCharToRedobuff(c);
2328}
2329
2330/*
2331 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002332 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002335start_arrow(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002336 pos_T *end_insert_pos) // can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002338 start_arrow_common(end_insert_pos, TRUE);
2339}
2340
2341/*
2342 * Like start_arrow() but with end_change argument.
2343 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2344 */
2345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002346start_arrow_with_change(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002347 pos_T *end_insert_pos, // can be NULL
2348 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002349{
2350 start_arrow_common(end_insert_pos, end_change);
2351 if (!end_change)
2352 {
2353 AppendCharToRedobuff(Ctrl_G);
2354 AppendCharToRedobuff('U');
2355 }
2356}
2357
2358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002359start_arrow_common(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002360 pos_T *end_insert_pos, // can be NULL
2361 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002362{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002363 if (!arrow_used && end_change) // something has been inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
2365 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002366 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002367 arrow_used = TRUE; // this means we stopped the current insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002369#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002370 check_spell_redraw();
2371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372}
2373
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002374#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002375/*
2376 * If we skipped highlighting word at cursor, do it now.
2377 * It may be skipped again, thus reset spell_redraw_lnum first.
2378 */
2379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002380check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002381{
2382 if (spell_redraw_lnum != 0)
2383 {
2384 linenr_T lnum = spell_redraw_lnum;
2385
2386 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002387 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002388 }
2389}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002390
Bram Moolenaar217ad922005-03-20 22:37:15 +00002391#endif
2392
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393/*
2394 * stop_arrow() is called before a change is made in insert mode.
2395 * If an arrow key has been used, start a new insertion.
2396 * Returns FAIL if undo is impossible, shouldn't insert then.
2397 */
2398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002399stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
2401 if (arrow_used)
2402 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002403 Insstart = curwin->w_cursor; // new insertion starts here
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002404 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002405 // Don't update the original insert position when moved to the
2406 // right, except when nothing was inserted yet.
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002407 update_Insstart_orig = FALSE;
2408 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (u_save_cursor() == OK)
2411 {
2412 arrow_used = FALSE;
2413 ins_need_undo = FALSE;
2414 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (State & VREPLACE_FLAG)
2418 {
2419 orig_line_count = curbuf->b_ml.ml_line_count;
2420 vr_lines_changed = 1;
2421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 ResetRedobuff();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002423 AppendToRedobuff((char_u *)"1i"); // pretend we start an insertion
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002424 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426 else if (ins_need_undo)
2427 {
2428 if (u_save_cursor() == OK)
2429 ins_need_undo = FALSE;
2430 }
2431
2432#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002433 // Always open fold at the cursor line when inserting something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 foldOpenCursor();
2435#endif
2436
2437 return (arrow_used || ins_need_undo ? FAIL : OK);
2438}
2439
2440/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002441 * Do a few things to stop inserting.
2442 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2443 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 */
2445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002446stop_insert(
2447 pos_T *end_insert_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002448 int esc, // called by ins_esc()
2449 int nomove) // <c-\><c-o>, don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002451 int cc;
2452 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454 stop_redo_ins();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002455 replace_flush(); // abandon replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002458 * Save the inserted text for later redo with ^@ and CTRL-A.
2459 * Don't do it when "restart_edit" was set and nothing was inserted,
2460 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002462 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002463 if (did_restart_edit == 0 || (ptr != NULL
2464 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002465 {
2466 vim_free(last_insert);
2467 last_insert = ptr;
2468 last_insert_skip = new_insert_skip;
2469 }
2470 else
2471 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002473 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002475 // Auto-format now. It may seem strange to do this when stopping an
2476 // insertion (or moving the cursor), but it's required when appending
2477 // a line and having it end in a space. But only do it when something
2478 // was actually inserted, otherwise undo won't work.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002479 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002481 pos_T tpos = curwin->w_cursor;
2482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002483 // When the cursor is at the end of the line after a space the
2484 // formatting will move it to the following word. Avoid that by
2485 // moving the cursor onto the space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 cc = 'x';
2487 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
2488 {
2489 dec_cursor();
2490 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002491 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002492 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494
2495 auto_format(TRUE, FALSE);
2496
Bram Moolenaar1c465442017-03-12 20:10:05 +01002497 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002498 {
2499 if (gchar_cursor() != NUL)
2500 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002501 // If the cursor is still at the same character, also keep
2502 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002503 if (gchar_cursor() == NUL
2504 && curwin->w_cursor.lnum == tpos.lnum
2505 && curwin->w_cursor.col == tpos.col)
2506 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 }
2509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002510 // If a space was inserted for auto-formatting, remove it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 check_auto_format(TRUE);
2512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002513 // If we just did an auto-indent, remove the white space from the end
2514 // of the line, and put the cursor back.
2515 // Do this when ESC was used or moving the cursor up/down.
2516 // Check for the old position still being valid, just in case the text
2517 // got changed unexpectedly.
Bram Moolenaarf332a652013-11-04 04:20:33 +01002518 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00002519 && curwin->w_cursor.lnum != end_insert_pos->lnum))
2520 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002522 pos_T tpos = curwin->w_cursor;
2523
2524 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002525 check_cursor_col(); // make sure it is not past the line
Bram Moolenaar39f05632006-03-19 22:15:26 +00002526 for (;;)
2527 {
2528 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
2529 --curwin->w_cursor.col;
2530 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002531 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00002532 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00002533 if (del_char(TRUE) == FAIL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002534 break; // should not happen
Bram Moolenaar39f05632006-03-19 22:15:26 +00002535 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002536 if (curwin->w_cursor.lnum != tpos.lnum)
2537 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01002538 else
2539 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002540 // reset tpos, could have been invalidated in the loop above
Bram Moolenaaref6875b2014-11-12 18:59:25 +01002541 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01002542 tpos.col++;
2543 if (cc != NUL && gchar_pos(&tpos) == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002544 ++curwin->w_cursor.col; // put cursor back on the NUL
Bram Moolenaar2f31e392014-10-31 19:20:36 +01002545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002547 // <C-S-Right> may have started Visual mode, adjust the position for
2548 // deleted characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
2550 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002551 int len = (int)STRLEN(ml_get_curline());
2552
2553 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002555 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
2558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560 }
2561 did_ai = FALSE;
2562#ifdef FEAT_SMARTINDENT
2563 did_si = FALSE;
2564 can_si = FALSE;
2565 can_si_back = FALSE;
2566#endif
2567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002568 // Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
2569 // now in a different buffer.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002570 if (end_insert_pos != NULL)
2571 {
2572 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002573 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002574 curbuf->b_op_end = *end_insert_pos;
2575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576}
2577
2578/*
2579 * Set the last inserted text to a single character.
2580 * Used for the replace command.
2581 */
2582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002583set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
2585 char_u *s;
2586
2587 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (last_insert != NULL)
2590 {
2591 s = last_insert;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002592 // Use the CTRL-V only when entering a special char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (c < ' ' || c == DEL)
2594 *s++ = Ctrl_V;
2595 s = add_char2buf(c, s);
2596 *s++ = ESC;
2597 *s++ = NUL;
2598 last_insert_skip = 0;
2599 }
2600}
2601
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002602#if defined(EXITFREE) || defined(PROTO)
2603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002604free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002605{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002606 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002607}
2608#endif
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610/*
2611 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
2612 * and CSI. Handle multi-byte characters.
2613 * Returns a pointer to after the added bytes.
2614 */
2615 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002616add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002618 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 int i;
2620 int len;
2621
2622 len = (*mb_char2bytes)(c, temp);
2623 for (i = 0; i < len; ++i)
2624 {
2625 c = temp[i];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002626 // Need to escape K_SPECIAL and CSI like in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if (c == K_SPECIAL)
2628 {
2629 *s++ = K_SPECIAL;
2630 *s++ = KS_SPECIAL;
2631 *s++ = KE_FILLER;
2632 }
2633#ifdef FEAT_GUI
2634 else if (c == CSI)
2635 {
2636 *s++ = CSI;
2637 *s++ = KS_EXTRA;
2638 *s++ = (int)KE_CSI;
2639 }
2640#endif
2641 else
2642 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 return s;
2645}
2646
2647/*
2648 * move cursor to start of line
2649 * if flags & BL_WHITE move to first non-white
2650 * if flags & BL_SOL move to first non-white if startofline is set,
2651 * otherwise keep "curswant" column
2652 * if flags & BL_FIX don't leave the cursor on a NUL.
2653 */
2654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002655beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 if ((flags & BL_SOL) && !p_sol)
2658 coladvance(curwin->w_curswant);
2659 else
2660 {
2661 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
2664 if (flags & (BL_WHITE | BL_SOL))
2665 {
2666 char_u *ptr;
2667
Bram Moolenaar1c465442017-03-12 20:10:05 +01002668 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
2670 ++curwin->w_cursor.col;
2671 }
2672 curwin->w_set_curswant = TRUE;
2673 }
2674}
2675
2676/*
2677 * oneright oneleft cursor_down cursor_up
2678 *
2679 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002680 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 * Return OK when successful, FAIL when we hit a line of file boundary.
2682 */
2683
2684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002685oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (virtual_active())
2691 {
2692 pos_T prevpos = curwin->w_cursor;
2693
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002694 // Adjust for multi-wide char (excluding TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01002696 coladvance(getviscol() + ((*ptr != TAB
2697 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 ? ptr2cells(ptr) : 1));
2699 curwin->w_set_curswant = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002700 // Return OK if the cursor moved, FAIL otherwise (at window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 return (prevpos.col != curwin->w_cursor.col
2702 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
2703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002706 if (*ptr == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002707 return FAIL; // already at the very end
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002708
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002709 if (has_mbyte)
2710 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002712 l = 1;
2713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002714 // move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
2715 // contains "onemore".
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002716 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002717 return FAIL;
2718 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 curwin->w_set_curswant = TRUE;
2721 return OK;
2722}
2723
2724 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002725oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (virtual_active())
2728 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002729#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 int v = getviscol();
2733
2734 if (v == 0)
2735 return FAIL;
2736
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002737#ifdef FEAT_LINEBREAK
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002738 // We might get stuck on 'showbreak', skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 width = 1;
2740 for (;;)
2741 {
2742 coladvance(v - width);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002743 // getviscol() is slow, skip it when 'showbreak' is empty,
2744 // 'breakindent' is not set and there are no multi-byte
2745 // characters
Bram Moolenaaree857022019-11-09 23:26:40 +01002746 if ((*get_showbreak_value(curwin) == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01002747 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 break;
2749 ++width;
2750 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002751#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755 if (curwin->w_cursor.coladd == 1)
2756 {
2757 char_u *ptr;
2758
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002759 // Adjust for multi-wide char (not a TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01002761 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
2762 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 curwin->w_cursor.coladd = 0;
2764 }
2765
2766 curwin->w_set_curswant = TRUE;
2767 return OK;
2768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
2770 if (curwin->w_cursor.col == 0)
2771 return FAIL;
2772
2773 curwin->w_set_curswant = TRUE;
2774 --curwin->w_cursor.col;
2775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002776 // if the character on the left of the current cursor is a multi-byte
2777 // character, move to its first byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (has_mbyte)
2779 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 return OK;
2781}
2782
2783 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002784cursor_up(
2785 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002786 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788 linenr_T lnum;
2789
2790 if (n > 0)
2791 {
2792 lnum = curwin->w_cursor.lnum;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002793 // This fails if the cursor is already in the first line or the count
2794 // is larger than the line number and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00002795 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 return FAIL;
2797 if (n >= lnum)
2798 lnum = 1;
2799 else
2800#ifdef FEAT_FOLDING
2801 if (hasAnyFolding(curwin))
2802 {
2803 /*
2804 * Count each sequence of folded lines as one logical line.
2805 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002806 // go to the start of the current fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 (void)hasFolding(lnum, &lnum, NULL);
2808
2809 while (n--)
2810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002811 // move up one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 --lnum;
2813 if (lnum <= 1)
2814 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002815 // If we entered a fold, move to the beginning, unless in
2816 // Insert mode or when 'foldopen' contains "all": it will open
2817 // in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
2819 (void)hasFolding(lnum, &lnum, NULL);
2820 }
2821 if (lnum < 1)
2822 lnum = 1;
2823 }
2824 else
2825#endif
2826 lnum -= n;
2827 curwin->w_cursor.lnum = lnum;
2828 }
2829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002830 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 coladvance(curwin->w_curswant);
2832
2833 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002834 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
2836 return OK;
2837}
2838
2839/*
2840 * Cursor down a number of logical lines.
2841 */
2842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002843cursor_down(
2844 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002845 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
2847 linenr_T lnum;
2848
2849 if (n > 0)
2850 {
2851 lnum = curwin->w_cursor.lnum;
2852#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002853 // Move to last line of fold, will fail if it's the end-of-file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 (void)hasFolding(lnum, NULL, &lnum);
2855#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002856 // This fails if the cursor is already in the last line or would move
2857 // beyond the last line and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00002858 if (lnum >= curbuf->b_ml.ml_line_count
2859 || (lnum + n > curbuf->b_ml.ml_line_count
2860 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 return FAIL;
2862 if (lnum + n >= curbuf->b_ml.ml_line_count)
2863 lnum = curbuf->b_ml.ml_line_count;
2864 else
2865#ifdef FEAT_FOLDING
2866 if (hasAnyFolding(curwin))
2867 {
2868 linenr_T last;
2869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002870 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 while (n--)
2872 {
2873 if (hasFolding(lnum, NULL, &last))
2874 lnum = last + 1;
2875 else
2876 ++lnum;
2877 if (lnum >= curbuf->b_ml.ml_line_count)
2878 break;
2879 }
2880 if (lnum > curbuf->b_ml.ml_line_count)
2881 lnum = curbuf->b_ml.ml_line_count;
2882 }
2883 else
2884#endif
2885 lnum += n;
2886 curwin->w_cursor.lnum = lnum;
2887 }
2888
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002889 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 coladvance(curwin->w_curswant);
2891
2892 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002893 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894
2895 return OK;
2896}
2897
2898/*
2899 * Stuff the last inserted text in the read buffer.
2900 * Last_insert actually is a copy of the redo buffer, so we
2901 * first have to remove the command.
2902 */
2903 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002904stuff_inserted(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002905 int c, // Command character to be inserted
2906 long count, // Repeat this many times
2907 int no_esc) // Don't add an ESC at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
2909 char_u *esc_ptr;
2910 char_u *ptr;
2911 char_u *last_ptr;
2912 char_u last = NUL;
2913
2914 ptr = get_last_insert();
2915 if (ptr == NULL)
2916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002917 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 return FAIL;
2919 }
2920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002921 // may want to stuff the command character, to start Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (c != NUL)
2923 stuffcharReadbuff(c);
2924 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002925 *esc_ptr = NUL; // remove the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002927 // when the last char is either "0" or "^" it will be quoted if no ESC
2928 // comes after it OR if it will inserted more than once and "ptr"
2929 // starts with ^D. -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
2931 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
2932 && (no_esc || (*ptr == Ctrl_D && count > 1)))
2933 {
2934 last = *last_ptr;
2935 *last_ptr = NUL;
2936 }
2937
2938 do
2939 {
2940 stuffReadbuff(ptr);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002941 // a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 if (last)
2943 stuffReadbuff((char_u *)(last == '0'
2944 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
2945 : IF_EB("\026^", CTRL_V_STR "^")));
2946 }
2947 while (--count > 0);
2948
2949 if (last)
2950 *last_ptr = last;
2951
2952 if (esc_ptr != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002953 *esc_ptr = ESC; // put the ESC back
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002955 // may want to stuff a trailing ESC, to get out of Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (!no_esc)
2957 stuffcharReadbuff(ESC);
2958
2959 return OK;
2960}
2961
2962 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002963get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 if (last_insert == NULL)
2966 return NULL;
2967 return last_insert + last_insert_skip;
2968}
2969
2970/*
2971 * Get last inserted string, and remove trailing <Esc>.
2972 * Returns pointer to allocated memory (must be freed) or NULL.
2973 */
2974 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002975get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 char_u *s;
2978 int len;
2979
2980 if (last_insert == NULL)
2981 return NULL;
2982 s = vim_strsave(last_insert + last_insert_skip);
2983 if (s != NULL)
2984 {
2985 len = (int)STRLEN(s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002986 if (len > 0 && s[len - 1] == ESC) // remove trailing ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 s[len - 1] = NUL;
2988 }
2989 return s;
2990}
2991
2992/*
2993 * Check the word in front of the cursor for an abbreviation.
2994 * Called when the non-id character "c" has been entered.
2995 * When an abbreviation is recognized it is removed from the text and
2996 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
2997 */
2998 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002999echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003001 // Don't check for abbreviation in paste mode, when disabled and just
3002 // after moving around with cursor keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 if (p_paste || no_abbr || arrow_used)
3004 return FALSE;
3005
3006 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3007 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3008}
3009
3010/*
3011 * replace-stack functions
3012 *
3013 * When replacing characters, the replaced characters are remembered for each
3014 * new character. This is used to re-insert the old text when backspacing.
3015 *
3016 * There is a NUL headed list of characters for each character that is
3017 * currently in the file after the insertion point. When BS is used, one NUL
3018 * headed list is put back for the deleted character.
3019 *
3020 * For a newline, there are two NUL headed lists. One contains the characters
3021 * that the NL replaced. The extra one stores the characters after the cursor
3022 * that were deleted (always white space).
3023 *
3024 * Replace_offset is normally 0, in which case replace_push will add a new
3025 * character at the end of the stack. If replace_offset is not 0, that many
3026 * characters will be left on the stack above the newly inserted character.
3027 */
3028
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003029static char_u *replace_stack = NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003030static long replace_stack_nr = 0; // next entry in replace stack
3031static long replace_stack_len = 0; // max. number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003034replace_push(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003035 int c) // character that is replaced (NUL is none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
3037 char_u *p;
3038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003039 if (replace_stack_nr < replace_offset) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 return;
3041 if (replace_stack_len <= replace_stack_nr)
3042 {
3043 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003044 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003045 if (p == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 {
3047 replace_stack_len -= 50;
3048 return;
3049 }
3050 if (replace_stack != NULL)
3051 {
3052 mch_memmove(p, replace_stack,
3053 (size_t)(replace_stack_nr * sizeof(char_u)));
3054 vim_free(replace_stack);
3055 }
3056 replace_stack = p;
3057 }
3058 p = replace_stack + replace_stack_nr - replace_offset;
3059 if (replace_offset)
3060 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3061 *p = c;
3062 ++replace_stack_nr;
3063}
3064
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003065/*
3066 * Push a character onto the replace stack. Handles a multi-byte character in
3067 * reverse byte order, so that the first byte is popped off first.
3068 * Return the number of bytes done (includes composing characters).
3069 */
3070 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003071replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003072{
3073 int l = (*mb_ptr2len)(p);
3074 int j;
3075
3076 for (j = l - 1; j >= 0; --j)
3077 replace_push(p[j]);
3078 return l;
3079}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003080
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081/*
3082 * Pop one item from the replace stack.
3083 * return -1 if stack empty
3084 * return replaced character or NUL otherwise
3085 */
3086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 if (replace_stack_nr == 0)
3090 return -1;
3091 return (int)replace_stack[--replace_stack_nr];
3092}
3093
3094/*
3095 * Join the top two items on the replace stack. This removes to "off"'th NUL
3096 * encountered.
3097 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003099replace_join(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003100 int off) // offset for which NUL to remove
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101{
3102 int i;
3103
3104 for (i = replace_stack_nr; --i >= 0; )
3105 if (replace_stack[i] == NUL && off-- <= 0)
3106 {
3107 --replace_stack_nr;
3108 mch_memmove(replace_stack + i, replace_stack + i + 1,
3109 (size_t)(replace_stack_nr - i));
3110 return;
3111 }
3112}
3113
3114/*
3115 * Pop bytes from the replace stack until a NUL is found, and insert them
3116 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3117 */
3118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003119replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 int cc;
3122 int oldState = State;
3123
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003124 State = NORMAL; // don't want REPLACE here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 while ((cc = replace_pop()) > 0)
3126 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 dec_cursor();
3129 }
3130 State = oldState;
3131}
3132
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133/*
3134 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3135 * indicates a multi-byte char, pop the other bytes too.
3136 */
3137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003138mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003141 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 int i;
3143 int c;
3144
3145 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3146 {
3147 buf[0] = cc;
3148 for (i = 1; i < n; ++i)
3149 buf[i] = replace_pop();
3150 ins_bytes_len(buf, n);
3151 }
3152 else
3153 ins_char(cc);
3154
3155 if (enc_utf8)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003156 // Handle composing chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 for (;;)
3158 {
3159 c = replace_pop();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003160 if (c == -1) // stack empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 break;
3162 if ((n = MB_BYTE2LEN(c)) == 1)
3163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003164 // Not a multi-byte char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 replace_push(c);
3166 break;
3167 }
3168 else
3169 {
3170 buf[0] = c;
3171 for (i = 1; i < n; ++i)
3172 buf[i] = replace_pop();
3173 if (utf_iscomposing(utf_ptr2char(buf)))
3174 ins_bytes_len(buf, n);
3175 else
3176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003177 // Not a composing char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 for (i = n - 1; i >= 0; --i)
3179 replace_push(buf[i]);
3180 break;
3181 }
3182 }
3183 }
3184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186/*
3187 * make the replace stack empty
3188 * (called when exiting replace mode)
3189 */
3190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003191replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003193 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 replace_stack_len = 0;
3195 replace_stack_nr = 0;
3196}
3197
3198/*
3199 * Handle doing a BS for one character.
3200 * cc < 0: replace stack empty, just move cursor
3201 * cc == 0: character was inserted, delete it
3202 * cc > 0: character was replaced, put cc (first byte of original char) back
3203 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003204 * When "limit_col" is >= 0, don't delete before this column. Matters when
3205 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 */
3207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003208replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 int orig_len = 0;
3212 int ins_len;
3213 int orig_vcols = 0;
3214 colnr_T start_vcol;
3215 char_u *p;
3216 int i;
3217 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 cc = replace_pop();
3220 if (cc > 0)
3221 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003222#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003223 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003224
3225 if (curbuf->b_has_textprop)
3226 {
3227 // Do not adjust text properties for individual delete and insert
3228 // operations, do it afterwards on the resulting text.
3229 len_before = STRLEN(ml_get_curline());
3230 ++text_prop_frozen;
3231 }
3232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (State & VREPLACE_FLAG)
3234 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003235 // Get the number of screen cells used by the character we are
3236 // going to delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3238 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 if (has_mbyte)
3241 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003242 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003244 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 replace_push(cc);
3246 }
3247 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 {
3249 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003251 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
3253 replace_pop_ins();
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (State & VREPLACE_FLAG)
3256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 // Get the number of screen cells used by the inserted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003259 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 vcol = start_vcol;
3261 for (i = 0; i < ins_len; ++i)
3262 {
3263 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003264 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
3266 vcol -= start_vcol;
3267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003268 // Delete spaces that were inserted after the cursor to keep the
3269 // text aligned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 curwin->w_cursor.col += ins_len;
3271 while (vcol > orig_vcols && gchar_cursor() == ' ')
3272 {
3273 del_char(FALSE);
3274 ++orig_vcols;
3275 }
3276 curwin->w_cursor.col -= ins_len;
3277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaar196d1572019-01-02 23:47:18 +01003279 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003281
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003282#ifdef FEAT_PROP_POPUP
Bram Moolenaar196d1572019-01-02 23:47:18 +01003283 if (curbuf->b_has_textprop)
3284 {
3285 size_t len_now = STRLEN(ml_get_curline());
3286
3287 --text_prop_frozen;
3288 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003289 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003290 }
3291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003294 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3298/*
3299 * Map Hebrew keyboard when in hkmap mode.
3300 */
3301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003304 if (p_hkmapp) // phonetic mapping, by Ilya Dogolazky
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
3306 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3307 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3308 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3309 static char_u map[26] =
3310 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3311 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3312 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3313 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3314 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3315 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3316 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3317 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3318 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3319
3320 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3321 return (int)(map[CharOrd(c)] - 1 + p_aleph);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003322 // '-1'='sofit'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 else if (c == 'x')
3324 return 'X';
3325 else if (c == 'q')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003326 return '\''; // {geresh}={'}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 else if (c == 246)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003328 return ' '; // \"o --> ' ' for a german keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 else if (c == 228)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003330 return ' '; // \"a --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 else if (c == 252)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003332 return ' '; // \"u --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#ifdef EBCDIC
3334 else if (islower(c))
3335#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003336 // NOTE: islower() does not do the right thing for us on Linux so we
3337 // do this the same was as 5.7 and previous, so it works correctly on
3338 // all systems. Specifically, the e.g. Delete and Arrow keys are
3339 // munged and won't work if e.g. searching for Hebrew text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else if (c >= 'a' && c <= 'z')
3341#endif
3342 return (int)(map[CharOrdLow(c)] + p_aleph);
3343 else
3344 return c;
3345 }
3346 else
3347 {
3348 switch (c)
3349 {
3350 case '`': return ';';
3351 case '/': return '.';
3352 case '\'': return ',';
3353 case 'q': return '/';
3354 case 'w': return '\'';
3355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003356 // Hebrew letters - set offset from 'a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 case ',': c = '{'; break;
3358 case '.': c = 'v'; break;
3359 case ';': c = 't'; break;
3360 default: {
3361 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3362
3363#ifdef EBCDIC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003364 // see note about islower() above
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (!islower(c))
3366#else
3367 if (c < 'a' || c > 'z')
3368#endif
3369 return c;
3370 c = str[CharOrdLow(c)];
3371 break;
3372 }
3373 }
3374
3375 return (int)(CharOrdLow(c) + p_aleph);
3376 }
3377}
3378#endif
3379
3380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003381ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 int need_redraw = FALSE;
3384 int regname;
3385 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003386 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388 /*
3389 * If we are going to wait for a character, show a '"'.
3390 */
3391 pc_status = PC_STATUS_UNSET;
3392 if (redrawing() && !char_avail())
3393 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003394 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00003395 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
3397 edit_putchar('"', TRUE);
3398#ifdef FEAT_CMDL_INFO
3399 add_to_showcmd_c(Ctrl_R);
3400#endif
3401 }
3402
3403#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003404 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405#endif
3406
3407 /*
3408 * Don't map the register name. This also prevents the mode message to be
3409 * deleted when ESC is hit.
3410 */
3411 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003412 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003413 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3416 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003417 // Get a third key for literal register insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 literally = regname;
3419#ifdef FEAT_CMDL_INFO
3420 add_to_showcmd_c(literally);
3421#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003422 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
3425 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003426 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428#ifdef FEAT_EVAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003429 // Don't call u_sync() while typing the expression or giving an error
3430 // message for it. Only call it explicitly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 ++no_u_sync;
3432 if (regname == '=')
3433 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003434 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003435# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003437# endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003438 // Sync undo when evaluating the expression calls setline() or
3439 // append(), so that it can be undone separately.
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003440 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003443
3444 // Cursor may be moved back a column.
3445 curwin->w_cursor = curpos;
3446 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003447# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003448 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (im_on)
3450 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00003453 if (regname == NUL || !valid_yank_reg(regname, FALSE))
3454 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003455 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003456 need_redraw = TRUE; // remove the '"'
Bram Moolenaar677ee682005-01-27 14:41:15 +00003457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 else
3459 {
3460#endif
3461 if (literally == Ctrl_O || literally == Ctrl_P)
3462 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003463 // Append the command to the redo buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 AppendCharToRedobuff(Ctrl_R);
3465 AppendCharToRedobuff(literally);
3466 AppendCharToRedobuff(regname);
3467
3468 do_put(regname, BACKWARD, 1L,
3469 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
3470 }
3471 else if (insert_reg(regname, literally) == FAIL)
3472 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003473 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003474 need_redraw = TRUE; // remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003476 else if (stop_insert_mode)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003477 // When the '=' register was used and a function was invoked that
3478 // did ":stopinsert" then stuff_empty() returns FALSE but we won't
3479 // insert anything, need to remove the '"'
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003480 need_redraw = TRUE;
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482#ifdef FEAT_EVAL
3483 }
3484 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003485 if (u_sync_once == 1)
3486 ins_need_undo = TRUE;
3487 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#endif
3489#ifdef FEAT_CMDL_INFO
3490 clear_showcmd();
3491#endif
3492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003493 // If the inserted register is empty, we need to remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (need_redraw || stuff_empty())
3495 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003497 // Disallow starting Visual mode here, would get a weird mode.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003498 if (!vis_active && VIsual_active)
3499 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500}
3501
3502/*
3503 * CTRL-G commands in Insert mode.
3504 */
3505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003506ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 int c;
3509
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003510 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 /*
3514 * Don't map the second key. This also prevents the mode message to be
3515 * deleted when ESC is hit.
3516 */
3517 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003518 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003519 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003521 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 switch (c)
3523 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003524 // CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 case K_UP:
3526 case Ctrl_K:
3527 case 'k': ins_up(TRUE);
3528 break;
3529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003530 // CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 case K_DOWN:
3532 case Ctrl_J:
3533 case 'j': ins_down(TRUE);
3534 break;
3535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003536 // CTRL-G u: start new undoable edit
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003537 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003540 // Need to reset Insstart, esp. because a BS that joins
3541 // a line to the previous one must save for undo.
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003542 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003543 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 break;
3545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003546 // CTRL-G U: do not break undo with the next char
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003547 case 'U':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003548 // Allow one left/right cursor movement with the next char,
3549 // without breaking undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003550 dont_sync_undo = MAYBE;
3551 break;
3552
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003553 // Unknown CTRL-G command, reserved for future expansion.
Bram Moolenaar165bc692015-07-21 17:53:25 +02003554 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556}
3557
3558/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 * CTRL-^ in Insert mode.
3560 */
3561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003562ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003563{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003564 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003566 // ":lmap" mappings exists, Toggle use of ":lmap" mappings.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003567 if (State & LANGMAP)
3568 {
3569 curbuf->b_p_iminsert = B_IMODE_NONE;
3570 State &= ~LANGMAP;
3571 }
3572 else
3573 {
3574 curbuf->b_p_iminsert = B_IMODE_LMAP;
3575 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003576#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003577 im_set_active(FALSE);
3578#endif
3579 }
3580 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003581#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003582 else
3583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003584 // There are no ":lmap" mappings, toggle IM
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003585 if (im_get_status())
3586 {
3587 curbuf->b_p_iminsert = B_IMODE_NONE;
3588 im_set_active(FALSE);
3589 }
3590 else
3591 {
3592 curbuf->b_p_iminsert = B_IMODE_IM;
3593 State &= ~LANGMAP;
3594 im_set_active(TRUE);
3595 }
3596 }
3597#endif
3598 set_iminsert_global();
3599 showmode();
3600#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003601 // may show different cursor shape or color
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602 if (gui.in_use)
3603 gui_update_cursor(TRUE, FALSE);
3604#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003605#if defined(FEAT_KEYMAP)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003606 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003607 status_redraw_curbuf();
3608#endif
3609}
3610
3611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 * Handle ESC in insert mode.
3613 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
3614 * insert.
3615 */
3616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617ins_esc(
3618 long *count,
3619 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003620 int nomove) // don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621{
3622 int temp;
3623 static int disabled_redraw = FALSE;
3624
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003625#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003626 check_spell_redraw();
3627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
3629 temp = curwin->w_cursor.col;
3630 if (disabled_redraw)
3631 {
3632 --RedrawingDisabled;
3633 disabled_redraw = FALSE;
3634 }
3635 if (!arrow_used)
3636 {
3637 /*
3638 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00003639 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
3640 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 */
3642 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00003643 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 /*
3646 * Repeating insert may take a long time. Check for
3647 * interrupt now and then.
3648 */
3649 if (*count > 0)
3650 {
3651 line_breakcheck();
3652 if (got_int)
3653 *count = 0;
3654 }
3655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003656 if (--*count > 0) // repeat what was typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003658 // Vi repeats the insert without replacing characters.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003659 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
3660 State &= ~REPLACE_FLAG;
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 (void)start_redo_ins();
3663 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003664 stuffRedoReadbuff(ESC_STR); // no ESC in redo buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 ++RedrawingDisabled;
3666 disabled_redraw = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003667 return FALSE; // repeat the insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01003669 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 undisplay_dollar();
3671 }
3672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003673 // When an autoindent was removed, curswant stays after the
3674 // indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
3676 curwin->w_set_curswant = TRUE;
3677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003678 // Remember the last Insert position in the '^ mark.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (!cmdmod.keepjumps)
3680 curbuf->b_last_insert = curwin->w_cursor;
3681
3682 /*
3683 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00003684 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00003686 if (!nomove
3687 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003688 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003689 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003690 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691#ifdef FEAT_RIGHTLEFT
3692 && !revins_on
3693#endif
3694 )
3695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
3697 {
3698 oneleft();
3699 if (restart_edit != NUL)
3700 ++curwin->w_cursor.coladd;
3701 }
3702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003705 // Correct cursor for multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (has_mbyte)
3707 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709 }
3710
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003711#ifdef HAVE_INPUT_METHOD
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003712 // Disable IM to allow typing English directly for Normal mode commands.
3713 // When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
3714 // well).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 if (!(State & LANGMAP))
3716 im_save_status(&curbuf->b_p_iminsert);
3717 im_set_active(FALSE);
3718#endif
3719
3720 State = NORMAL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003721 // need to position cursor again (e.g. when on a TAB )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 changed_cline_bef_curs();
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725#ifdef CURSOR_SHAPE
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003726 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01003728 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003729 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003730#ifdef FEAT_JOB_CHANNEL
3731 ch_log_output = TRUE;
3732#endif
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003733 // Re-enable bracketed paste mode.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01003734 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003736 // Re-enable modifyOtherKeys.
3737 out_str(T_CTI);
3738 }
3739
Bram Moolenaarad3ec762019-04-21 00:00:13 +02003740 // When recording or for CTRL-O, need to display the new mode.
3741 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02003742 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02003744 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003745 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003747 return TRUE; // exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748}
3749
3750#ifdef FEAT_RIGHTLEFT
3751/*
3752 * Toggle language: hkmap and revins_on.
3753 * Move to end of reverse inserted text.
3754 */
3755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003756ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
3758 if (revins_on && revins_chars && revins_scol >= 0)
3759 {
3760 while (gchar_cursor() != NUL && revins_chars--)
3761 ++curwin->w_cursor.col;
3762 }
3763 p_ri = !p_ri;
3764 revins_on = (State == INSERT && p_ri);
3765 if (revins_on)
3766 {
3767 revins_scol = curwin->w_cursor.col;
3768 revins_legal++;
3769 revins_chars = 0;
3770 undisplay_dollar();
3771 }
3772 else
3773 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003774 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 showmode();
3776}
3777#endif
3778
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779/*
3780 * If 'keymodel' contains "startsel", may start selection.
3781 * Returns TRUE when a CTRL-O and other keys stuffed.
3782 */
3783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003784ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
3786 if (km_startsel)
3787 switch (c)
3788 {
3789 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 case K_PAGEUP:
3792 case K_KPAGEUP:
3793 case K_PAGEDOWN:
3794 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02003795# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 case K_LEFT:
3797 case K_RIGHT:
3798 case K_UP:
3799 case K_DOWN:
3800 case K_END:
3801 case K_HOME:
3802# endif
3803 if (!(mod_mask & MOD_MASK_SHIFT))
3804 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003805 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 case K_S_LEFT:
3807 case K_S_RIGHT:
3808 case K_S_UP:
3809 case K_S_DOWN:
3810 case K_S_END:
3811 case K_S_HOME:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003812 // Start selection right away, the cursor can move with
3813 // CTRL-O when beyond the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 start_selection();
3815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003816 // Execute the key in (insert) Select mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 stuffcharReadbuff(Ctrl_O);
3818 if (mod_mask)
3819 {
3820 char_u buf[4];
3821
3822 buf[0] = K_SPECIAL;
3823 buf[1] = KS_MODIFIER;
3824 buf[2] = mod_mask;
3825 buf[3] = NUL;
3826 stuffReadbuff(buf);
3827 }
3828 stuffcharReadbuff(c);
3829 return TRUE;
3830 }
3831 return FALSE;
3832}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003835 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003836 */
3837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003838ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003839{
Bram Moolenaar14184a32019-02-16 15:10:30 +01003840#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003841 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02003842 (char_u *)((State & REPLACE_FLAG) ? "i"
3843 : replaceState == VREPLACE ? "v"
3844 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01003845#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02003846 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003847 if (State & REPLACE_FLAG)
3848 State = INSERT | (State & LANGMAP);
3849 else
3850 State = replaceState | (State & LANGMAP);
3851 AppendCharToRedobuff(K_INS);
3852 showmode();
3853#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003854 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003855#endif
3856}
3857
3858/*
3859 * Pressed CTRL-O in Insert mode.
3860 */
3861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003862ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003863{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003864 if (State & VREPLACE_FLAG)
3865 restart_edit = 'V';
3866 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003867 if (State & REPLACE_FLAG)
3868 restart_edit = 'R';
3869 else
3870 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 if (virtual_active())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003872 ins_at_eol = FALSE; // cursor always keeps its column
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003873 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003874 ins_at_eol = (gchar_cursor() == NUL);
3875}
3876
3877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 * If the cursor is on an indent, ^T/^D insert/delete one
3879 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00003880 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 * with vi. But vi only supports ^T and ^D after an
3882 * autoindent, we support it everywhere.
3883 */
3884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003885ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
3887 if (stop_arrow() == FAIL)
3888 return;
3889 AppendCharToRedobuff(c);
3890
3891 /*
3892 * 0^D and ^^D: remove all indent.
3893 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00003894 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
3895 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
3897 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003898 (void)del_char(FALSE); // delete the '^' or '0'
3899 // In Replace mode, restore the characters that '^' or '0' replaced.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (State & REPLACE_FLAG)
3901 replace_pop_ins();
3902 if (lastc == '^')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003903 old_indent = get_indent(); // remember curr. indent
Bram Moolenaar21b17e72008-01-16 19:03:13 +00003904 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00003907 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
3909 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
3910 did_ai = FALSE;
3911#ifdef FEAT_SMARTINDENT
3912 did_si = FALSE;
3913 can_si = FALSE;
3914 can_si_back = FALSE;
3915#endif
3916#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003917 can_cindent = FALSE; // no cindenting after ^D or ^T
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918#endif
3919}
3920
3921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003922ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923{
3924 int temp;
3925
3926 if (stop_arrow() == FAIL)
3927 return;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003928 if (gchar_cursor() == NUL) // delete newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
3930 temp = curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003931 if (!can_bs(BS_EOL) // only if "eol" included
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02003932 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003933 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01003935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 curwin->w_cursor.col = temp;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003937 // Adjust orig_line_count in case more lines have been deleted than
3938 // have been added. That makes sure, that open_line() later
3939 // can access all buffer lines correctly
Bram Moolenaar63e82db2018-03-06 12:10:48 +01003940 if (State & VREPLACE_FLAG &&
3941 orig_line_count > curbuf->b_ml.ml_line_count)
3942 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01003943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003945 else if (del_char(FALSE) == FAIL) // delete char under cursor
Bram Moolenaar165bc692015-07-21 17:53:25 +02003946 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 did_ai = FALSE;
3948#ifdef FEAT_SMARTINDENT
3949 did_si = FALSE;
3950 can_si = FALSE;
3951 can_si_back = FALSE;
3952#endif
3953 AppendCharToRedobuff(K_DEL);
3954}
3955
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003956/*
3957 * Delete one character for ins_bs().
3958 */
3959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003960ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003961{
3962 dec_cursor();
3963 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
3964 if (State & REPLACE_FLAG)
3965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003966 // Don't delete characters before the insert point when in
3967 // Replace mode
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003968 if (curwin->w_cursor.lnum != Insstart.lnum
3969 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003970 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003971 }
3972 else
3973 (void)del_char(FALSE);
3974}
3975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976/*
3977 * Handle Backspace, delete-word and delete-line in Insert mode.
3978 * Return TRUE when backspace was actually used.
3979 */
3980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003981ins_bs(
3982 int c,
3983 int mode,
3984 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985{
3986 linenr_T lnum;
3987 int cc;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003988 int temp = 0; // init for GCC
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003989 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 colnr_T mincol;
3991 int did_backspace = FALSE;
3992 int in_indent;
3993 int oldState;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003994 int cpc[MAX_MCO]; // composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996 /*
3997 * can't delete anything in an empty file
3998 * can't backup past first character in buffer
3999 * can't backup past starting point unless 'backspace' > 1
4000 * can backup to a previous line if 'backspace' == 0
4001 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004002 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 || (
4004#ifdef FEAT_RIGHTLEFT
4005 !revins_on &&
4006#endif
4007 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4008 || (!can_bs(BS_START)
4009 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004010 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4011 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4013 && curwin->w_cursor.col <= ai_col)
4014 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4015 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004016 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 return FALSE;
4018 }
4019
4020 if (stop_arrow() == FAIL)
4021 return FALSE;
4022 in_indent = inindent(0);
4023#ifdef FEAT_CINDENT
4024 if (in_indent)
4025 can_cindent = FALSE;
4026#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004027 end_comment_pending = NUL; // After BS, don't auto-end comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 if (revins_on) // put cursor after last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 inc_cursor();
4031#endif
4032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004033 // Virtualedit:
4034 // BACKSPACE_CHAR eats a virtual space
4035 // BACKSPACE_WORD eats all coladd
4036 // BACKSPACE_LINE eats all coladd and keeps going
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 if (curwin->w_cursor.coladd > 0)
4038 {
4039 if (mode == BACKSPACE_CHAR)
4040 {
4041 --curwin->w_cursor.coladd;
4042 return TRUE;
4043 }
4044 if (mode == BACKSPACE_WORD)
4045 {
4046 curwin->w_cursor.coladd = 0;
4047 return TRUE;
4048 }
4049 curwin->w_cursor.coladd = 0;
4050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004053 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 */
4055 if (curwin->w_cursor.col == 0)
4056 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004057 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004058 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059#ifdef FEAT_RIGHTLEFT
4060 || revins_on
4061#endif
4062 )
4063 {
4064 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4065 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4066 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004067 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004068 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 /*
4071 * In replace mode:
4072 * cc < 0: NL was inserted, delete it
4073 * cc >= 0: NL was replaced, put original characters back
4074 */
4075 cc = -1;
4076 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004077 cc = replace_pop(); // returns -1 if NL was inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 /*
4079 * In replace mode, in the line we started replacing, we only move the
4080 * cursor.
4081 */
4082 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4083 {
4084 dec_cursor();
4085 }
4086 else
4087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 if (!(State & VREPLACE_FLAG)
4089 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004091 temp = gchar_cursor(); // remember current char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004094 // When "aw" is in 'formatoptions' we must delete the space at
4095 // the end of the line, otherwise the line will be broken
4096 // again when auto-formatting.
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004097 if (has_format_option(FO_AUTO)
4098 && has_format_option(FO_WHITE_PAR))
4099 {
4100 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4101 TRUE);
4102 int len;
4103
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004104 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004105 if (len > 0 && ptr[len - 1] == ' ')
4106 ptr[len - 1] = NUL;
4107 }
4108
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004109 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (temp == NUL && gchar_cursor() != NUL)
4111 inc_cursor();
4112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 else
4114 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
4116 /*
4117 * In REPLACE mode we have to put back the text that was replaced
4118 * by the NL. On the replace stack is first a NUL-terminated
4119 * sequence of characters that were deleted and then the
4120 * characters that NL replaced.
4121 */
4122 if (State & REPLACE_FLAG)
4123 {
4124 /*
4125 * Do the next ins_char() in NORMAL state, to
4126 * prevent ins_char() from replacing characters and
4127 * avoiding showmatch().
4128 */
4129 oldState = State;
4130 State = NORMAL;
4131 /*
4132 * restore characters (blanks) deleted after cursor
4133 */
4134 while (cc > 0)
4135 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004136 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004138 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 cc = replace_pop();
4140 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004141 // restore the characters that NL replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 replace_pop_ins();
4143 State = oldState;
4144 }
4145 }
4146 did_ai = FALSE;
4147 }
4148 else
4149 {
4150 /*
4151 * Delete character(s) before the cursor.
4152 */
4153#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004154 if (revins_on) // put cursor on last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 dec_cursor();
4156#endif
4157 mincol = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004158 // keep indent
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004159 if (mode == BACKSPACE_LINE
4160 && (curbuf->b_p_ai
4161#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004162 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004163#endif
4164 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165#ifdef FEAT_RIGHTLEFT
4166 && !revins_on
4167#endif
4168 )
4169 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004170 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004172 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004174 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176
4177 /*
4178 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4179 */
4180 if ( mode == BACKSPACE_CHAR
4181 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004182 || ((get_sts_value() != 0
4183#ifdef FEAT_VARTABS
4184 || tabstop_count(curbuf->b_p_vsts_array)
4185#endif
4186 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004187 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 && (*(ml_get_cursor() - 1) == TAB
4189 || (*(ml_get_cursor() - 1) == ' '
4190 && (!*inserted_space_p
4191 || arrow_used))))))
4192 {
4193 int ts;
4194 colnr_T vcol;
4195 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004196 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 *inserted_space_p = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004199 // Compute the virtual column where we want to be. Since
4200 // 'showbreak' may get in the way, need to get the last column of
4201 // the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004203 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 dec_cursor();
4205 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4206 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004207#ifdef FEAT_VARTABS
4208 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004209 {
4210 ts = (int)get_sw_value(curbuf);
4211 want_vcol = (want_vcol / ts) * ts;
4212 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004213 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004214 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004215 curbuf->b_p_vsts_array);
4216#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004217 if (p_sta && in_indent)
4218 ts = (int)get_sw_value(curbuf);
4219 else
4220 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004224 // delete characters until we are at or before want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004226 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004227 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004229 // insert extra spaces until we are at want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 while (vcol < want_vcol)
4231 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004232 // Remember the first char we inserted
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004233 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4234 && curwin->w_cursor.col < Insstart_orig.col)
4235 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 if (State & VREPLACE_FLAG)
4238 ins_char(' ');
4239 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
4241 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004242 if ((State & REPLACE_FLAG))
4243 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4246 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004248 // If we are now back where we started delete one character. Can
4249 // happen when using 'sts' and 'linebreak'.
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004250 if (vcol >= start_vcol)
4251 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253
4254 /*
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004255 * Delete up to starting point, start of line or previous word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004259 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004261 if (has_mbyte)
4262 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004263 do
4264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004266 if (!revins_on) // put cursor on char to be deleted
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004269
4270 cc = gchar_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004271 // look multi-byte character class
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004272 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004274 prev_cclass = cclass;
4275 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004278 // start of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004279 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4280 {
4281 mode = BACKSPACE_WORD_NOT_SPACE;
4282 temp = vim_iswordc(cc);
4283 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004284 // end of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004285 else if (mode == BACKSPACE_WORD_NOT_SPACE
4286 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004287 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004290 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004292 inc_cursor();
4293#ifdef FEAT_RIGHTLEFT
4294 else if (State & REPLACE_FLAG)
4295 dec_cursor();
4296#endif
4297 break;
4298 }
4299 if (State & REPLACE_FLAG)
4300 replace_do_bs(-1);
4301 else
4302 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004303 if (enc_utf8 && p_deco)
4304 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004305 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004306 /*
4307 * If there are combining characters and 'delcombine' is set
4308 * move the cursor back. Don't back up before the base
4309 * character.
4310 */
4311 if (enc_utf8 && p_deco && cpc[0] != NUL)
4312 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004313#ifdef FEAT_RIGHTLEFT
4314 if (revins_chars)
4315 {
4316 revins_chars--;
4317 revins_legal++;
4318 }
4319 if (revins_on && gchar_cursor() == NUL)
4320 break;
4321#endif
4322 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004323 // Just a single backspace?:
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004324 if (mode == BACKSPACE_CHAR)
4325 break;
4326 } while (
4327#ifdef FEAT_RIGHTLEFT
4328 revins_on ||
4329#endif
4330 (curwin->w_cursor.col > mincol
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02004331 && (can_bs(BS_NOSTOP)
4332 || (curwin->w_cursor.lnum != Insstart_orig.lnum
4333 || curwin->w_cursor.col != Insstart_orig.col)
4334 )));
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 did_backspace = TRUE;
4337 }
4338#ifdef FEAT_SMARTINDENT
4339 did_si = FALSE;
4340 can_si = FALSE;
4341 can_si_back = FALSE;
4342#endif
4343 if (curwin->w_cursor.col <= 1)
4344 did_ai = FALSE;
4345 /*
4346 * It's a little strange to put backspaces into the redo
4347 * buffer, but it makes auto-indent a lot easier to deal
4348 * with.
4349 */
4350 AppendCharToRedobuff(c);
4351
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004352 // If deleted before the insertion point, adjust it
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004353 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004354 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004355 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004357 // vi behaviour: the cursor moves backward but the character that
4358 // was there remains visible
4359 // Vim behaviour: the cursor moves backward and the character that
4360 // was there is erased from the screen.
4361 // We can emulate the vi behaviour by pretending there is a dollar
4362 // displayed even when there isn't.
4363 // --pkv Sun Jan 19 01:56:40 EST 2003
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004364 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 dollar_vcol = curwin->w_virtcol;
4366
Bram Moolenaarce3be472008-01-14 19:12:28 +00004367#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // When deleting a char the cursor line must never be in a closed fold.
4369 // E.g., when 'foldmethod' is indent and deleting the first non-white
4370 // char before a Tab.
Bram Moolenaarce3be472008-01-14 19:12:28 +00004371 if (did_backspace)
4372 foldOpenCursor();
4373#endif
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 return did_backspace;
4376}
4377
Bram Moolenaarec2da362017-01-21 20:04:22 +01004378/*
4379 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4380 * P_PE literally.
4381 * When "drop" is TRUE then consume the text and drop it.
4382 */
4383 int
4384bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4385{
4386 int c;
4387 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4388 int idx = 0;
4389 char_u *end = find_termcode((char_u *)"PE");
4390 int ret_char = -1;
4391 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004392 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004393
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004394 // If the end code is too long we can't detect it, read everything.
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01004395 if (end != NULL && STRLEN(end) >= NUMBUFLEN)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004396 end = NULL;
4397 ++no_mapping;
4398 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004399 if (!p_paste)
4400 // Also have the side effects of setting 'paste' to make it work much
4401 // faster.
4402 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004403
Bram Moolenaarec2da362017-01-21 20:04:22 +01004404 for (;;)
4405 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004406 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004407 if (end == NULL && vpeekc() == NUL)
4408 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004409 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004410 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004411 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004412 if (c == NUL || got_int || (ex_normal_busy > 0 && c == Ctrl_C))
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004413 // When CTRL-C was encountered the typeahead will be flushed and we
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004414 // won't get the end sequence. Except when using ":normal".
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004415 break;
4416
Bram Moolenaarec2da362017-01-21 20:04:22 +01004417 if (has_mbyte)
4418 idx += (*mb_char2bytes)(c, buf + idx);
4419 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004420 buf[idx++] = c;
4421 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004422 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004423 {
4424 if (end[idx] == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004425 break; // Found the end of paste code.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004426 continue;
4427 }
4428 if (!drop)
4429 {
4430 switch (mode)
4431 {
4432 case PASTE_CMDLINE:
4433 put_on_cmdline(buf, idx, TRUE);
4434 break;
4435
4436 case PASTE_EX:
4437 if (gap != NULL && ga_grow(gap, idx) == OK)
4438 {
4439 mch_memmove((char *)gap->ga_data + gap->ga_len,
4440 buf, (size_t)idx);
4441 gap->ga_len += idx;
4442 }
4443 break;
4444
4445 case PASTE_INSERT:
4446 if (stop_arrow() == OK)
4447 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004448 c = buf[0];
4449 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
4450 ins_eol(c);
4451 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01004452 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004453 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01004454 AppendToRedobuffLit(buf, idx);
4455 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01004456 }
4457 break;
4458
4459 case PASTE_ONE_CHAR:
4460 if (ret_char == -1)
4461 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01004462 if (has_mbyte)
4463 ret_char = (*mb_ptr2char)(buf);
4464 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004465 ret_char = buf[0];
4466 }
4467 break;
4468 }
4469 }
4470 idx = 0;
4471 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004472
Bram Moolenaarec2da362017-01-21 20:04:22 +01004473 --no_mapping;
4474 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004475 if (!save_paste)
4476 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01004477
4478 return ret_char;
4479}
4480
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004481#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004483ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004484{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // We will be leaving the current window, unless closing another tab.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004486 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
4487 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
4488 {
4489 undisplay_dollar();
4490 start_arrow(&curwin->w_cursor);
4491# ifdef FEAT_CINDENT
4492 can_cindent = TRUE;
4493# endif
4494 }
4495
4496 if (c == K_TABLINE)
4497 goto_tabpage(current_tab);
4498 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004499 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004500 handle_tabmenu();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004501 redraw_statuslines(); // will redraw the tabline when needed
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004502 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004503}
4504#endif
4505
4506#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004508ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
4510 pos_T tpos;
4511
4512 undisplay_dollar();
4513 tpos = curwin->w_cursor;
4514 if (gui_do_scroll())
4515 {
4516 start_arrow(&tpos);
4517# ifdef FEAT_CINDENT
4518 can_cindent = TRUE;
4519# endif
4520 }
4521}
4522
4523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004524ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 pos_T tpos;
4527
4528 undisplay_dollar();
4529 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004530 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
4532 start_arrow(&tpos);
4533# ifdef FEAT_CINDENT
4534 can_cindent = TRUE;
4535# endif
4536 }
4537}
4538#endif
4539
4540 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004541ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542{
4543 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004544 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546#ifdef FEAT_FOLDING
4547 if ((fdo_flags & FDO_HOR) && KeyTyped)
4548 foldOpenCursor();
4549#endif
4550 undisplay_dollar();
4551 tpos = curwin->w_cursor;
4552 if (oneleft() == OK)
4553 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004554#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004555 // Only call start_arrow() when not busy with preediting, it will
4556 // break undo. K_LEFT is inserted in im_correct_cursor().
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004557 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004558#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004559 {
4560 start_arrow_with_change(&tpos, end_change);
4561 if (!end_change)
4562 AppendCharToRedobuff(K_LEFT);
4563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004565 // If exit reversed string, position is fixed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
4567 revins_legal++;
4568 revins_chars++;
4569#endif
4570 }
4571
4572 /*
4573 * if 'whichwrap' set for cursor in insert mode may go to
4574 * previous line
4575 */
4576 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
4577 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004578 // always break undo when moving upwards/downwards, else undo may break
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 start_arrow(&tpos);
4580 --(curwin->w_cursor.lnum);
4581 coladvance((colnr_T)MAXCOL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004582 curwin->w_set_curswant = TRUE; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
4584 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004585 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004586 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587}
4588
4589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004590ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591{
4592 pos_T tpos;
4593
4594#ifdef FEAT_FOLDING
4595 if ((fdo_flags & FDO_HOR) && KeyTyped)
4596 foldOpenCursor();
4597#endif
4598 undisplay_dollar();
4599 tpos = curwin->w_cursor;
4600 if (c == K_C_HOME)
4601 curwin->w_cursor.lnum = 1;
4602 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 curwin->w_curswant = 0;
4605 start_arrow(&tpos);
4606}
4607
4608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004609ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
4611 pos_T tpos;
4612
4613#ifdef FEAT_FOLDING
4614 if ((fdo_flags & FDO_HOR) && KeyTyped)
4615 foldOpenCursor();
4616#endif
4617 undisplay_dollar();
4618 tpos = curwin->w_cursor;
4619 if (c == K_C_END)
4620 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4621 coladvance((colnr_T)MAXCOL);
4622 curwin->w_curswant = MAXCOL;
4623
4624 start_arrow(&tpos);
4625}
4626
4627 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004628ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004630 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#ifdef FEAT_FOLDING
4632 if ((fdo_flags & FDO_HOR) && KeyTyped)
4633 foldOpenCursor();
4634#endif
4635 undisplay_dollar();
4636 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
4637 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004638 start_arrow_with_change(&curwin->w_cursor, end_change);
4639 if (!end_change)
4640 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 (void)bck_word(1L, FALSE, FALSE);
4642 curwin->w_set_curswant = TRUE;
4643 }
4644 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004645 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004646 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647}
4648
4649 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004650ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004652 int end_change = dont_sync_undo == FALSE; // end undoable change
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#ifdef FEAT_FOLDING
4655 if ((fdo_flags & FDO_HOR) && KeyTyped)
4656 foldOpenCursor();
4657#endif
4658 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004659 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004661 start_arrow_with_change(&curwin->w_cursor, end_change);
4662 if (!end_change)
4663 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (virtual_active())
4666 oneright();
4667 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004670 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 ++curwin->w_cursor.col;
4673 }
4674
4675#ifdef FEAT_RIGHTLEFT
4676 revins_legal++;
4677 if (revins_chars)
4678 revins_chars--;
4679#endif
4680 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004681 // if 'whichwrap' set for cursor in insert mode, may move the
4682 // cursor to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 else if (vim_strchr(p_ww, ']') != NULL
4684 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4685 {
4686 start_arrow(&curwin->w_cursor);
4687 curwin->w_set_curswant = TRUE;
4688 ++curwin->w_cursor.lnum;
4689 curwin->w_cursor.col = 0;
4690 }
4691 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004692 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004693 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694}
4695
4696 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004697ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004699 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#ifdef FEAT_FOLDING
4701 if ((fdo_flags & FDO_HOR) && KeyTyped)
4702 foldOpenCursor();
4703#endif
4704 undisplay_dollar();
4705 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4706 || gchar_cursor() != NUL)
4707 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004708 start_arrow_with_change(&curwin->w_cursor, end_change);
4709 if (!end_change)
4710 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 (void)fwd_word(1L, FALSE, 0);
4712 curwin->w_set_curswant = TRUE;
4713 }
4714 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004715 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004716 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717}
4718
4719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004720ins_up(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004721 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 pos_T tpos;
4724 linenr_T old_topline = curwin->w_topline;
4725#ifdef FEAT_DIFF
4726 int old_topfill = curwin->w_topfill;
4727#endif
4728
4729 undisplay_dollar();
4730 tpos = curwin->w_cursor;
4731 if (cursor_up(1L, TRUE) == OK)
4732 {
4733 if (startcol)
4734 coladvance(getvcol_nolist(&Insstart));
4735 if (old_topline != curwin->w_topline
4736#ifdef FEAT_DIFF
4737 || old_topfill != curwin->w_topfill
4738#endif
4739 )
4740 redraw_later(VALID);
4741 start_arrow(&tpos);
4742#ifdef FEAT_CINDENT
4743 can_cindent = TRUE;
4744#endif
4745 }
4746 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004747 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748}
4749
4750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004751ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 pos_T tpos;
4754
4755 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004756
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004757 if (mod_mask & MOD_MASK_CTRL)
4758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004759 // <C-PageUp>: tab page back
Bram Moolenaarbc444822006-10-17 11:37:50 +00004760 if (first_tabpage->tp_next != NULL)
4761 {
4762 start_arrow(&curwin->w_cursor);
4763 goto_tabpage(-1);
4764 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004765 return;
4766 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 tpos = curwin->w_cursor;
4769 if (onepage(BACKWARD, 1L) == OK)
4770 {
4771 start_arrow(&tpos);
4772#ifdef FEAT_CINDENT
4773 can_cindent = TRUE;
4774#endif
4775 }
4776 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004777 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778}
4779
4780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004781ins_down(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004782 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
4784 pos_T tpos;
4785 linenr_T old_topline = curwin->w_topline;
4786#ifdef FEAT_DIFF
4787 int old_topfill = curwin->w_topfill;
4788#endif
4789
4790 undisplay_dollar();
4791 tpos = curwin->w_cursor;
4792 if (cursor_down(1L, TRUE) == OK)
4793 {
4794 if (startcol)
4795 coladvance(getvcol_nolist(&Insstart));
4796 if (old_topline != curwin->w_topline
4797#ifdef FEAT_DIFF
4798 || old_topfill != curwin->w_topfill
4799#endif
4800 )
4801 redraw_later(VALID);
4802 start_arrow(&tpos);
4803#ifdef FEAT_CINDENT
4804 can_cindent = TRUE;
4805#endif
4806 }
4807 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004808 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809}
4810
4811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004812ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 pos_T tpos;
4815
4816 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004817
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004818 if (mod_mask & MOD_MASK_CTRL)
4819 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004820 // <C-PageDown>: tab page forward
Bram Moolenaarbc444822006-10-17 11:37:50 +00004821 if (first_tabpage->tp_next != NULL)
4822 {
4823 start_arrow(&curwin->w_cursor);
4824 goto_tabpage(0);
4825 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004826 return;
4827 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 tpos = curwin->w_cursor;
4830 if (onepage(FORWARD, 1L) == OK)
4831 {
4832 start_arrow(&tpos);
4833#ifdef FEAT_CINDENT
4834 can_cindent = TRUE;
4835#endif
4836 }
4837 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004838 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839}
4840
4841#ifdef FEAT_DND
4842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004843ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844{
4845 do_put('~', BACKWARD, 1L, PUT_CURSEND);
4846}
4847#endif
4848
4849/*
4850 * Handle TAB in Insert or Replace mode.
4851 * Return TRUE when the TAB needs to be inserted like a normal character.
4852 */
4853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004854ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 int ind;
4857 int i;
4858 int temp;
4859
4860 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
4861 Insstart_blank_vcol = get_nolist_virtcol();
4862 if (echeck_abbr(TAB + ABBR_OFF))
4863 return FALSE;
4864
4865 ind = inindent(0);
4866#ifdef FEAT_CINDENT
4867 if (ind)
4868 can_cindent = FALSE;
4869#endif
4870
4871 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004872 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 */
4874 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004875#ifdef FEAT_VARTABS
4876 && !(p_sta && ind
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004877 // These five lines mean 'tabstop' != 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004878 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
4879 || (tabstop_count(curbuf->b_p_vts_array) == 1
4880 && tabstop_first(curbuf->b_p_vts_array)
4881 != get_sw_value(curbuf))
4882 || (tabstop_count(curbuf->b_p_vts_array) == 0
4883 && curbuf->b_p_ts != get_sw_value(curbuf))))
4884 && tabstop_count(curbuf->b_p_vsts_array) == 0
4885#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004886 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004887#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02004888 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 return TRUE;
4890
4891 if (stop_arrow() == FAIL)
4892 return TRUE;
4893
4894 did_ai = FALSE;
4895#ifdef FEAT_SMARTINDENT
4896 did_si = FALSE;
4897 can_si = FALSE;
4898 can_si_back = FALSE;
4899#endif
4900 AppendToRedobuff((char_u *)"\t");
4901
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004902#ifdef FEAT_VARTABS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004904 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004905 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004906 temp -= get_nolist_virtcol() % temp;
4907 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004908 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004909 // use 'softtabstop' when set
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004910 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004911 curbuf->b_p_vsts_array);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004912 else // otherwise use 'tabstop'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004913 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
4914 curbuf->b_p_vts_array);
4915#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004916 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004917 temp = (int)get_sw_value(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 else if (curbuf->b_p_sts != 0) // use 'softtabstop' when set
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02004919 temp = (int)get_sts_value();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004920 else // otherwise use 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 temp = (int)curbuf->b_p_ts;
4922 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
4925 /*
4926 * Insert the first space with ins_char(). It will delete one char in
4927 * replace mode. Insert the rest with ins_str(); it will not delete any
4928 * chars. For VREPLACE mode, we use ins_char() for all characters.
4929 */
4930 ins_char(' ');
4931 while (--temp > 0)
4932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (State & VREPLACE_FLAG)
4934 ins_char(' ');
4935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
4937 ins_str((char_u *)" ");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004938 if (State & REPLACE_FLAG) // no char replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 replace_push(NUL);
4940 }
4941 }
4942
4943 /*
4944 * When 'expandtab' not set: Replace spaces by TABs where possible.
4945 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004946#ifdef FEAT_VARTABS
4947 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
4948 || get_sts_value() > 0
4949 || (p_sta && ind)))
4950#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02004951 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
4954 char_u *ptr;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 char_u *saved_line = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 pos_T fpos;
4958 pos_T *cursor;
4959 colnr_T want_vcol, vcol;
4960 int change_col = -1;
4961 int save_list = curwin->w_p_list;
4962
4963 /*
4964 * Get the current line. For VREPLACE mode, don't make real changes
4965 * yet, just work on a copy of the line.
4966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (State & VREPLACE_FLAG)
4968 {
4969 pos = curwin->w_cursor;
4970 cursor = &pos;
4971 saved_line = vim_strsave(ml_get_curline());
4972 if (saved_line == NULL)
4973 return FALSE;
4974 ptr = saved_line + pos.col;
4975 }
4976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
4978 ptr = ml_get_cursor();
4979 cursor = &curwin->w_cursor;
4980 }
4981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004982 // When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
4984 curwin->w_p_list = FALSE;
4985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // Find first white before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004988 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 --fpos.col;
4991 --ptr;
4992 }
4993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 // In Replace mode, don't change characters before the insert point.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if ((State & REPLACE_FLAG)
4996 && fpos.lnum == Insstart.lnum
4997 && fpos.col < Insstart.col)
4998 {
4999 ptr += Insstart.col - fpos.col;
5000 fpos.col = Insstart.col;
5001 }
5002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005003 // compute virtual column numbers of first white and cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5005 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005007 // Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5008 // and 'linebreak' adding extra virtual columns.
Bram Moolenaar1c465442017-03-12 20:10:05 +01005009 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005011 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (vcol + i > want_vcol)
5013 break;
5014 if (*ptr != TAB)
5015 {
5016 *ptr = TAB;
5017 if (change_col < 0)
5018 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005019 change_col = fpos.col; // Column of first change
5020 // May have to adjust Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5022 Insstart.col = fpos.col;
5023 }
5024 }
5025 ++fpos.col;
5026 ++ptr;
5027 vcol += i;
5028 }
5029
5030 if (change_col >= 0)
5031 {
5032 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005033 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005035 // Skip over the spaces we need.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 while (vcol < want_vcol && *ptr == ' ')
5037 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005038 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 ++ptr;
5040 ++repl_off;
5041 }
5042 if (vcol > want_vcol)
5043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005044 // Must have a char with 'showbreak' just before it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 --ptr;
5046 --repl_off;
5047 }
5048 fpos.col += repl_off;
5049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005050 // Delete following spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 i = cursor->col - fpos.col;
5052 if (i > 0)
5053 {
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005054#ifdef FEAT_PROP_POPUP
5055 if (!(State & VREPLACE_FLAG))
5056 {
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005057 char_u *newp;
5058 int col;
5059
5060 newp = alloc(curbuf->b_ml.ml_line_len - i);
5061 if (newp == NULL)
5062 return FALSE;
5063
5064 col = ptr - curbuf->b_ml.ml_line_ptr;
5065 if (col > 0)
5066 mch_memmove(newp, ptr - col, col);
5067 mch_memmove(newp + col, ptr + i,
5068 curbuf->b_ml.ml_line_len - col - i);
5069
5070 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
5071 vim_free(curbuf->b_ml.ml_line_ptr);
5072 curbuf->b_ml.ml_line_ptr = newp;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005073 curbuf->b_ml.ml_line_len -= i;
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005074 curbuf->b_ml.ml_flags =
5075 (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005076 }
5077 else
5078#endif
5079 STRMOVE(ptr, ptr + i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005080 // correct replace stack.
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005081 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 for (temp = i; --temp >= 0; )
5083 replace_join(repl_off);
5084 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005085#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005086 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005087 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005088 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005089 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5090 (char_u *)"\t", 1);
5091 }
5092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 cursor->col -= i;
5094
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 /*
5096 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5097 * backspacing over the changed spacing and then inserting the new
5098 * spacing.
5099 */
5100 if (State & VREPLACE_FLAG)
5101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005102 // Backspace from real cursor to change_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 backspace_until_column(change_col);
5104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005105 // Insert each char in saved_line from changed_col to
5106 // ptr-cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 ins_bytes_len(saved_line + change_col,
5108 cursor->col - change_col);
5109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (State & VREPLACE_FLAG)
5113 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 curwin->w_p_list = save_list;
5115 }
5116
5117 return FALSE;
5118}
5119
5120/*
5121 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005122 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005125ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
5127 int i;
5128
5129 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005130 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005132 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 undisplay_dollar();
5134
5135 /*
5136 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5137 * character under the cursor. Only push a NUL on the replace stack,
5138 * nothing to put back when the NL is deleted.
5139 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005140 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 replace_push(NUL);
5142
5143 /*
5144 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5145 * replacing the next line, so we push all of the characters left on the
5146 * line onto the replace stack. This is not done here though, it is done
5147 * in open_line().
5148 */
5149
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005150 // Put cursor on NUL if on the last char and coladd is 1 (happens after
5151 // CTRL-O).
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005152 if (virtual_active() && curwin->w_cursor.coladd > 0)
5153 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005154
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005156 // NL in reverse insert will always start in the end of
5157 // current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (revins_on)
5159 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5160#endif
5161
5162 AppendToRedobuff(NL_STR);
5163 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005164 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 old_indent = 0;
5166#ifdef FEAT_CINDENT
5167 can_cindent = TRUE;
5168#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005169#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005170 // When inserting a line the cursor line must never be in a closed fold.
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005171 foldOpenCursor();
5172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005174 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175}
5176
5177#ifdef FEAT_DIGRAPHS
5178/*
5179 * Handle digraph in insert mode.
5180 * Returns character still to be inserted, or NUL when nothing remaining to be
5181 * done.
5182 */
5183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005184ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
5186 int c;
5187 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005188 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 pc_status = PC_STATUS_UNSET;
5191 if (redrawing() && !char_avail())
5192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005193 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005194 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005197 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#ifdef FEAT_CMDL_INFO
5199 add_to_showcmd_c(Ctrl_K);
5200#endif
5201 }
5202
5203#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005204 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#endif
5206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005207 // don't map the digraph chars. This also prevents the
5208 // mode message to be deleted when ESC is hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ++no_mapping;
5210 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005211 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 --no_mapping;
5213 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005214 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005215 // when the line fits in 'columns' the '?' is at the start of the next
5216 // line and will not be removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005217 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 if (IS_SPECIAL(c) || mod_mask) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221#ifdef FEAT_CMDL_INFO
5222 clear_showcmd();
5223#endif
5224 insert_special(c, TRUE, FALSE);
5225 return NUL;
5226 }
5227 if (c != ESC)
5228 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005229 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 if (redrawing() && !char_avail())
5231 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005233 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
5235 if (char2cells(c) == 1)
5236 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005237 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005239 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241#ifdef FEAT_CMDL_INFO
5242 add_to_showcmd_c(c);
5243#endif
5244 }
5245 ++no_mapping;
5246 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005247 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 --no_mapping;
5249 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005250 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005251 // when the line fits in 'columns' the '?' is at the start of the
5252 // next line and will not be removed by a redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005253 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if (cc != ESC)
5255 {
5256 AppendToRedobuff((char_u *)CTRL_V_STR);
5257 c = getdigraph(c, cc, TRUE);
5258#ifdef FEAT_CMDL_INFO
5259 clear_showcmd();
5260#endif
5261 return c;
5262 }
5263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264#ifdef FEAT_CMDL_INFO
5265 clear_showcmd();
5266#endif
5267 return NUL;
5268}
5269#endif
5270
5271/*
5272 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5273 * Returns the char to be inserted, or NUL if none found.
5274 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005276ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 int c;
5279 int temp;
5280 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005281 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5284 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005285 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 return NUL;
5287 }
5288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 // try to advance to the cursor column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005291 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 prev_ptr = ptr;
5293 validate_virtcol();
5294 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5295 {
5296 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005297 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
5299 if ((colnr_T)temp > curwin->w_virtcol)
5300 ptr = prev_ptr;
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005304 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 return c;
5306}
5307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308/*
5309 * CTRL-Y or CTRL-E typed in Insert mode.
5310 */
5311 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005312ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313{
5314 int c = tc;
5315
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005316 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005317 {
5318 if (c == Ctrl_Y)
5319 scrolldown_clamp();
5320 else
5321 scrollup_clamp();
5322 redraw_later(VALID);
5323 }
5324 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 {
5326 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5327 if (c != NUL)
5328 {
5329 long tw_save;
5330
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005331 // The character must be taken literally, insert like it
5332 // was typed after a CTRL-V, and pretend 'textwidth'
5333 // wasn't set. Digits, 'o' and 'x' are special after a
5334 // CTRL-V, don't use it for these.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 if (c < 256 && !isalnum(c))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005336 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 tw_save = curbuf->b_p_tw;
5338 curbuf->b_p_tw = -1;
5339 insert_special(c, TRUE, FALSE);
5340 curbuf->b_p_tw = tw_save;
5341#ifdef FEAT_RIGHTLEFT
5342 revins_chars++;
5343 revins_legal++;
5344#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005345 c = Ctrl_V; // pretend CTRL-V is last character
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 auto_format(FALSE, TRUE);
5347 }
5348 }
5349 return c;
5350}
5351
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352/*
5353 * Get the value that w_virtcol would have when 'list' is off.
5354 * Unless 'cpo' contains the 'L' flag.
5355 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005356 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005357get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005359 // check validity of cursor in current buffer
5360 if (curwin->w_buffer == NULL
5361 || curwin->w_buffer->b_ml.ml_mfp == NULL
5362 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5363 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5365 return getvcol_nolist(&curwin->w_cursor);
5366 validate_virtcol();
5367 return curwin->w_virtcol;
5368}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005369
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005370#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005371/*
5372 * Handle the InsertCharPre autocommand.
5373 * "c" is the character that was typed.
5374 * Return a pointer to allocated memory with the replacement string.
5375 * Return NULL to continue inserting "c".
5376 */
5377 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005378do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005379{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005380 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005381 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005382 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005384 // Return quickly when there is nothing to do.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005385 if (!has_insertcharpre())
5386 return NULL;
5387
Bram Moolenaar704984a2012-06-01 14:57:51 +02005388 if (has_mbyte)
5389 buf[(*mb_char2bytes)(c, buf)] = NUL;
5390 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005391 {
5392 buf[0] = c;
5393 buf[1] = NUL;
5394 }
5395
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005396 // Lock the text to avoid weird things from happening.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005397 ++textwinlock;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005398 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005399
Bram Moolenaar704984a2012-06-01 14:57:51 +02005400 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005401 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005402 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005403 // Get the value of v:char. It may be empty or more than one
5404 // character. Only use it when changed, otherwise continue with the
5405 // original character to avoid breaking autoindent.
Bram Moolenaar704984a2012-06-01 14:57:51 +02005406 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5407 res = vim_strsave(get_vim_var_str(VV_CHAR));
5408 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005410 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005411 --textwinlock;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005412
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005413 // Restore the State, it may have been changed.
5414 State = save_State;
5415
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005416 return res;
5417}
5418#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005419
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005420#if defined(FEAT_CINDENT) || defined(PROTO)
5421 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005422get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005423{
5424 return can_cindent;
5425}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005426
5427 void
5428set_can_cindent(int val)
5429{
5430 can_cindent = val;
5431}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005432#endif
5433
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005434/*
5435 * Trigger "event" and take care of fixing undo.
5436 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005437 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005438ins_apply_autocmds(event_T event)
5439{
5440 varnumber_T tick = CHANGEDTICK(curbuf);
5441 int r;
5442
5443 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5444
5445 // If u_savesub() was called then we are not prepared to start
5446 // a new line. Call u_save() with no contents to fix that.
Bram Moolenaardb934952020-04-27 20:18:31 +02005447 // Except when leaving Insert mode.
5448 if (event != EVENT_INSERTLEAVE && tick != CHANGEDTICK(curbuf))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005449 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5450
5451 return r;
5452}