blob: 312095ed900244bf33806a6b5ed3dcff98da2935 [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 Moolenaar7591bb32019-03-30 13:53:47 +010021/* Set when doing something for completion that may call edit() recursively,
22 * which is not allowed. */
23static 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);
31static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
32static void check_auto_format(int);
33static void redo_literal(int c);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010034static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +000035#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void check_spell_redraw(void);
Bram Moolenaar217ad922005-03-20 22:37:15 +000037#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
39static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010040static void replace_join(int off);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010041static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static void replace_flush(void);
43static void replace_do_bs(int limit_col);
44static int del_char_after_col(int limit_col);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static void ins_reg(void);
46static void ins_ctrl_g(void);
47static void ins_ctrl_hat(void);
48static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010052static int ins_start_select(int c);
53static void ins_insert(int replaceState);
54static void ins_ctrl_o(void);
55static void ins_shift(int c, int lastc);
56static void ins_del(void);
57static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000058#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000060#endif
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010061static void ins_left(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010062static void ins_home(int c);
63static void ins_end(int c);
64static void ins_s_left(void);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010065static void ins_right(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static void ins_s_right(void);
67static void ins_up(int startcol);
68static void ins_pageup(void);
69static void ins_down(int startcol);
70static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static int ins_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010076static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010082#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +010084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
86static colnr_T Insstart_textlen; /* length of line when insert started */
87static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +010088static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
90static char_u *last_insert = NULL; /* the text of the previous insert,
91 K_SPECIAL and CSI are escaped */
92static int last_insert_skip; /* nr of chars in front of previous insert */
93static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +000094static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
96#ifdef FEAT_CINDENT
97static int can_cindent; /* may do cindenting on this line */
98#endif
99
100static int old_indent = 0; /* for ^^D command in insert mode */
101
102#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000103static int revins_on; /* reverse insert mode on */
104static int revins_chars; /* how much to skip after edit */
105static int revins_legal; /* was the last char 'legal'? */
106static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
108
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109static int ins_need_undo; /* call u_save() before inserting a
110 char. Set when edit() is called.
111 after that arrow_used is used. */
112
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100113static int did_add_space = FALSE; // auto_format() added an extra space
114 // under the cursor
115static int dont_sync_undo = FALSE; // CTRL-G U prevents syncing undo for
116 // the next left/right cursor key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
118/*
119 * edit(): Start inserting text.
120 *
121 * "cmdchar" can be:
122 * 'i' normal insert command
123 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100124 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 * 'R' replace command
126 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
127 * but still only one <CR> is inserted. The <Esc> is not used for redo.
128 * 'g' "gI" command.
129 * 'V' "gR" command for Virtual Replace mode.
130 * 'v' "gr" command for single character Virtual Replace mode.
131 *
132 * This function is not called recursively. For CTRL-O commands, it returns
133 * and lets the caller handle the Normal-mode command.
134 *
135 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
136 */
137 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100138edit(
139 int cmdchar,
140 int startln, /* if set, insert at start of line */
141 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142{
143 int c = 0;
144 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100145 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000146 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 int i;
149 int did_backspace = TRUE; /* previous char was backspace */
150#ifdef FEAT_CINDENT
151 int line_is_white = FALSE; /* line is empty before insert */
152#endif
153 linenr_T old_topline = 0; /* topline before insertion */
154#ifdef FEAT_DIFF
155 int old_topfill = -1;
156#endif
157 int inserted_space = FALSE; /* just inserted a space */
158 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000159 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200160#ifdef FEAT_JOB_CHANNEL
161 int cmdchar_todo = cmdchar;
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000164 /* Remember whether editing was restarted after CTRL-O. */
165 did_restart_edit = restart_edit;
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 /* sleep before redrawing, needed for "CTRL-O :" that results in an
168 * error message */
169 check_for_delay(TRUE);
170
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100171 /* set Insstart_orig to Insstart */
172 update_Insstart_orig = TRUE;
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#ifdef HAVE_SANDBOX
175 /* Don't allow inserting in the sandbox. */
176 if (sandbox != 0)
177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100178 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 return FALSE;
180 }
181#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000182 /* Don't allow changes in the buffer while editing the cmdline. The
183 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000184 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100186 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000187 return FALSE;
188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000190 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100191 if (ins_compl_active() || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100193 emsg(_(e_secure));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000194 return FALSE;
195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 ins_compl_clear(); /* clear stuff for CTRL-X mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaar843ee412004-06-30 16:16:41 +0000198 /*
199 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
200 */
201 if (cmdchar != 'r' && cmdchar != 'v')
202 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100203 pos_T save_cursor = curwin->w_cursor;
204
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100205#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000206 if (cmdchar == 'R')
207 ptr = (char_u *)"r";
208 else if (cmdchar == 'V')
209 ptr = (char_u *)"v";
210 else
211 ptr = (char_u *)"i";
212 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200213 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100214#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200215 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100216
Bram Moolenaar097c9922013-05-19 21:15:15 +0200217 /* Make sure the cursor didn't move. Do call check_cursor_col() in
218 * case the text was modified. Since Insert mode was not started yet
219 * a call to check_cursor_col() may move the cursor, especially with
220 * the "A" command, thus set State to avoid that. Also check that the
221 * line number is still valid (lines may have been deleted).
222 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100223 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100224#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200225 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100226#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200227 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100228 {
229 int save_state = State;
230
231 curwin->w_cursor = save_cursor;
232 State = INSERT;
233 check_cursor_col();
234 State = save_state;
235 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000236 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000237
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200238#ifdef FEAT_CONCEAL
239 /* Check if the cursor line needs redrawing before changing State. If
240 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200241 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200242#endif
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244#ifdef FEAT_MOUSE
245 /*
246 * When doing a paste with the middle mouse button, Insstart is set to
247 * where the paste started.
248 */
249 if (where_paste_started.lnum != 0)
250 Insstart = where_paste_started;
251 else
252#endif
253 {
254 Insstart = curwin->w_cursor;
255 if (startln)
256 Insstart.col = 0;
257 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000258 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 Insstart_blank_vcol = MAXCOL;
260 if (!did_ai)
261 ai_col = 0;
262
263 if (cmdchar != NUL && restart_edit == 0)
264 {
265 ResetRedobuff();
266 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 if (cmdchar == 'V' || cmdchar == 'v')
268 {
269 /* "gR" or "gr" command */
270 AppendCharToRedobuff('g');
271 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
272 }
273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100275 if (cmdchar == K_PS)
276 AppendCharToRedobuff('a');
277 else
278 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 if (cmdchar == 'g') /* "gI" command */
280 AppendCharToRedobuff('I');
281 else if (cmdchar == 'r') /* "r<CR>" command */
282 count = 1; /* insert only one <CR> */
283 }
284 }
285
286 if (cmdchar == 'R')
287 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 State = REPLACE;
289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 else if (cmdchar == 'V' || cmdchar == 'v')
291 {
292 State = VREPLACE;
293 replaceState = VREPLACE;
294 orig_line_count = curbuf->b_ml.ml_line_count;
295 vr_lines_changed = 1;
296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 else
298 State = INSERT;
299
300 stop_insert_mode = FALSE;
301
302 /*
303 * Need to recompute the cursor position, it might move when the cursor is
304 * on a TAB or special character.
305 */
306 curs_columns(TRUE);
307
308 /*
309 * Enable langmap or IME, indicated by 'iminsert'.
310 * Note that IME may enabled/disabled without us noticing here, thus the
311 * 'iminsert' value may not reflect what is actually used. It is updated
312 * when hitting <Esc>.
313 */
314 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
315 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100316#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
318#endif
319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifdef FEAT_CMDL_INFO
322 clear_showcmd();
323#endif
324#ifdef FEAT_RIGHTLEFT
325 /* there is no reverse replace mode */
326 revins_on = (State == INSERT && p_ri);
327 if (revins_on)
328 undisplay_dollar();
329 revins_chars = 0;
330 revins_legal = 0;
331 revins_scol = -1;
332#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100333 if (!p_ek)
334 /* Disable bracketed paste mode, we won't recognize the escape
335 * sequences. */
336 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
338 /*
339 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100340 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
341 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 */
343 if (restart_edit != 0 && stuff_empty())
344 {
345#ifdef FEAT_MOUSE
346 /*
347 * After a paste we consider text typed to be part of the insert for
348 * the pasted text. You can backspace over the pasted text too.
349 */
350 if (where_paste_started.lnum)
351 arrow_used = FALSE;
352 else
353#endif
354 arrow_used = TRUE;
355 restart_edit = 0;
356
357 /*
358 * If the cursor was after the end-of-line before the CTRL-O and it is
359 * now at the end-of-line, put it after the end-of-line (this is not
360 * correct in very rare cases).
361 * Also do this if curswant is greater than the current virtual
362 * column. Eg after "^O$" or "^O80|".
363 */
364 validate_virtcol();
365 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000366 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 || curwin->w_curswant > curwin->w_virtcol)
368 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
369 {
370 if (ptr[1] == NUL)
371 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 else if (has_mbyte)
373 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000374 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 if (ptr[i] == NUL)
376 curwin->w_cursor.col += i;
377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000379 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 }
381 else
382 arrow_used = FALSE;
383
384 /* we are in insert mode now, don't need to start it anymore */
385 need_start_insertmode = FALSE;
386
387 /* Need to save the line for undo before inserting the first char. */
388 ins_need_undo = TRUE;
389
390#ifdef FEAT_MOUSE
391 where_paste_started.lnum = 0;
392#endif
393#ifdef FEAT_CINDENT
394 can_cindent = TRUE;
395#endif
396#ifdef FEAT_FOLDING
397 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
398 * restarting. */
399 if (!p_im && did_restart_edit == 0)
400 foldOpenCursor();
401#endif
402
403 /*
404 * If 'showmode' is set, show the current (insert/replace/..) mode.
405 * A warning message for changing a readonly file is given here, before
406 * actually changing anything. It's put after the mode, if any.
407 */
408 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000409 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 i = showmode();
411
412 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000413 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
415#ifdef CURSOR_SHAPE
416 ui_cursor_shape(); /* may show different cursor shape */
417#endif
418#ifdef FEAT_DIGRAPHS
419 do_digraph(-1); /* clear digraphs */
420#endif
421
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000422 /*
423 * Get the current length of the redo buffer, those characters have to be
424 * skipped if we want to get to the inserted characters.
425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 ptr = get_inserted();
427 if (ptr == NULL)
428 new_insert_skip = 0;
429 else
430 {
431 new_insert_skip = (int)STRLEN(ptr);
432 vim_free(ptr);
433 }
434
435 old_indent = 0;
436
437 /*
438 * Main loop in Insert mode: repeat until Insert mode is left.
439 */
440 for (;;)
441 {
442#ifdef FEAT_RIGHTLEFT
443 if (!revins_legal)
444 revins_scol = -1; /* reset on illegal motions */
445 else
446 revins_legal = 0;
447#endif
448 if (arrow_used) /* don't repeat insert when arrow key used */
449 count = 0;
450
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100451 if (update_Insstart_orig)
452 Insstart_orig = Insstart;
453
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200454 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 {
456 /* ":stopinsert" used or 'insertmode' reset */
457 count = 0;
458 goto doESCkey;
459 }
460
461 /* set curwin->w_curswant for next K_DOWN or K_UP */
462 if (!arrow_used)
463 curwin->w_set_curswant = TRUE;
464
465 /* If there is no typeahead may check for timestamps (e.g., for when a
466 * menu invoked a shell command). */
467 if (stuff_empty())
468 {
469 did_check_timestamps = FALSE;
470 if (need_check_timestamps)
471 check_timestamps(FALSE);
472 }
473
474 /*
475 * When emsg() was called msg_scroll will have been set.
476 */
477 msg_scroll = FALSE;
478
479#ifdef FEAT_GUI
480 /* When 'mousefocus' is set a mouse movement may have taken us to
481 * another window. "need_mouse_correct" may then be set because of an
482 * autocommand. */
483 if (need_mouse_correct)
484 gui_mouse_correct();
485#endif
486
487#ifdef FEAT_FOLDING
488 /* Open fold at the cursor line, according to 'foldopen'. */
489 if (fdo_flags & FDO_INSERT)
490 foldOpenCursor();
491 /* Close folds where the cursor isn't, according to 'foldclose' */
492 if (!char_avail())
493 foldCheckClose();
494#endif
495
Bram Moolenaarf2732452018-06-03 14:47:35 +0200496#ifdef FEAT_JOB_CHANNEL
497 if (bt_prompt(curbuf))
498 {
499 init_prompt(cmdchar_todo);
500 cmdchar_todo = NUL;
501 }
502#endif
503
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 /*
505 * If we inserted a character at the last position of the last line in
506 * the window, scroll the window one line up. This avoids an extra
507 * redraw.
508 * This is detected when the cursor column is smaller after inserting
509 * something.
510 * Don't do this when the topline changed already, it has
511 * already been adjusted (by insertchar() calling open_line())).
512 */
513 if (curbuf->b_mod_set
514 && curwin->w_p_wrap
515 && !did_backspace
516 && curwin->w_topline == old_topline
517#ifdef FEAT_DIFF
518 && curwin->w_topfill == old_topfill
519#endif
520 )
521 {
522 mincol = curwin->w_wcol;
523 validate_cursor_col();
524
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200525 if (
526#ifdef FEAT_VARTABS
527 (int)curwin->w_wcol < mincol - tabstop_at(
528 get_nolist_virtcol(), curbuf->b_p_ts,
529 curbuf->b_p_vts_array)
530#else
531 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100534 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 && (curwin->w_cursor.lnum != curwin->w_topline
536#ifdef FEAT_DIFF
537 || curwin->w_topfill > 0
538#endif
539 ))
540 {
541#ifdef FEAT_DIFF
542 if (curwin->w_topfill > 0)
543 --curwin->w_topfill;
544 else
545#endif
546#ifdef FEAT_FOLDING
547 if (hasFolding(curwin->w_topline, NULL, &old_topline))
548 set_topline(curwin, old_topline + 1);
549 else
550#endif
551 set_topline(curwin, curwin->w_topline + 1);
552 }
553 }
554
555 /* May need to adjust w_topline to show the cursor. */
556 update_topline();
557
558 did_backspace = FALSE;
559
560 validate_cursor(); /* may set must_redraw */
561
562 /*
563 * Redraw the display when no characters are waiting.
564 * Also shows mode, ruler and positions cursor.
565 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000566 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (curwin->w_p_scb)
569 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar860cae12010-06-05 23:22:07 +0200571 if (curwin->w_p_crb)
572 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 update_curswant();
574 old_topline = curwin->w_topline;
575#ifdef FEAT_DIFF
576 old_topfill = curwin->w_topfill;
577#endif
578
579#ifdef USE_ON_FLY_SCROLL
580 dont_scroll = FALSE; /* allow scrolling here */
581#endif
582
583 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100584 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100586 if (c != K_CURSORHOLD)
587 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200588
589 /* After using CTRL-G U the next cursor key will not break undo. */
590 if (dont_sync_undo == MAYBE)
591 dont_sync_undo = TRUE;
592 else
593 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100594 if (cmdchar == K_PS)
595 /* Got here from normal mode when bracketed paste started. */
596 c = K_PS;
597 else
598 do
599 {
600 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200601
602 if (stop_insert_mode)
603 {
604 // Insert mode ended, possibly from a callback.
605 count = 0;
606 nomove = TRUE;
607 goto doESCkey;
608 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100609 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000611 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
612 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef FEAT_RIGHTLEFT
615 if (p_hkmap && KeyTyped)
616 c = hkmap(c); /* Hebrew mode mapping */
617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000619 /*
620 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000621 * and the cursor is still in the completed word. Only when there is
622 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000623 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100624 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000625 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100626 && curwin->w_cursor.col >= ins_compl_col()
627 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000628 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000629 /* BS: Delete one character from "compl_leader". */
630 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100631 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000632 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000633 continue;
634
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000635 /* When no match was selected or it was edited. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100636 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000637 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000638 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000639 * "compl_leader". Except when at the original match and
640 * there is nothing to add, CTRL-L works like CTRL-P then. */
641 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100642 && (!ctrl_x_mode_line_or_eval()
643 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000644 {
645 ins_compl_addfrommatch();
646 continue;
647 }
648
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000649 /* A non-white character that fits in with the current
650 * completion: Add to "compl_leader". */
651 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000652 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100653#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100654 /* Trigger InsertCharPre. */
655 char_u *str = do_insert_char_pre(c);
656 char_u *p;
657
658 if (str != NULL)
659 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100660 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100661 ins_compl_addleader(PTR2CHAR(p));
662 vim_free(str);
663 }
664 else
665#endif
666 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000667 continue;
668 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000669
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000670 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100671 * ins_compl_enter_selects() is set the Enter key does the
672 * same. */
673 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200674 && (c == CAR || c == K_KENTER || c == NL)))
675 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000676 {
677 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200678 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000679 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000680 }
681 }
682
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
684 * it does fix up the text when finishing completion. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100685 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000686 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000687 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
Bram Moolenaar488c6512005-08-11 20:09:58 +0000689 /* CTRL-\ CTRL-N goes to Normal mode,
690 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
691 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 if (c == Ctrl_BSL)
693 {
694 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000695 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 ++no_mapping;
697 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000698 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 --no_mapping;
700 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000701 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000703 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 vungetc(c);
705 c = Ctrl_BSL;
706 }
707 else if (c == Ctrl_G && p_im)
708 continue;
709 else
710 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000711 if (c == Ctrl_O)
712 {
713 ins_ctrl_o();
714 ins_at_eol = FALSE; /* cursor keeps its column */
715 nomove = TRUE;
716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 count = 0;
718 goto doESCkey;
719 }
720 }
721
722#ifdef FEAT_DIGRAPHS
723 c = do_digraph(c);
724#endif
725
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100726 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (c == Ctrl_V || c == Ctrl_Q)
729 {
730 ins_ctrl_v();
731 c = Ctrl_V; /* pretend CTRL-V is last typed character */
732 continue;
733 }
734
735#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200736 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 {
738 /* A key name preceded by a bang means this key is not to be
739 * inserted. Skip ahead to the re-indenting below.
740 * A key name preceded by a star means that indenting has to be
741 * done before inserting the key. */
742 line_is_white = inindent(0);
743 if (in_cinkeys(c, '!', line_is_white))
744 goto force_cindent;
745 if (can_cindent && in_cinkeys(c, '*', line_is_white)
746 && stop_arrow() == OK)
747 do_c_expr_indent();
748 }
749#endif
750
751#ifdef FEAT_RIGHTLEFT
752 if (curwin->w_p_rl)
753 switch (c)
754 {
755 case K_LEFT: c = K_RIGHT; break;
756 case K_S_LEFT: c = K_S_RIGHT; break;
757 case K_C_LEFT: c = K_C_RIGHT; break;
758 case K_RIGHT: c = K_LEFT; break;
759 case K_S_RIGHT: c = K_S_LEFT; break;
760 case K_C_RIGHT: c = K_C_LEFT; break;
761 }
762#endif
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 /*
765 * If 'keymodel' contains "startsel", may start selection. If it
766 * does, a CTRL-O and c will be stuffed, we need to get these
767 * characters.
768 */
769 if (ins_start_select(c))
770 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 /*
773 * The big switch to handle a character in insert mode.
774 */
775 switch (c)
776 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000777 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (echeck_abbr(ESC + ABBR_OFF))
779 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200780 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000782 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#ifdef FEAT_CMDWIN
784 if (c == Ctrl_C && cmdwin_type != 0)
785 {
786 /* Close the cmdline window. */
787 cmdwin_result = K_IGNORE;
788 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000789 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 goto doESCkey;
791 }
792#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200793#ifdef FEAT_JOB_CHANNEL
794 if (c == Ctrl_C && bt_prompt(curbuf))
795 {
796 if (invoke_prompt_interrupt())
797 {
798 if (!bt_prompt(curbuf))
799 // buffer changed to a non-prompt buffer, get out of
800 // Insert mode
801 goto doESCkey;
802 break;
803 }
804 }
805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
807#ifdef UNIX
808do_intr:
809#endif
810 /* when 'insertmode' set, and not halfway a mapping, don't leave
811 * Insert mode */
812 if (goto_im())
813 {
814 if (got_int)
815 {
816 (void)vgetc(); /* flush all buffers */
817 got_int = FALSE;
818 }
819 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200820 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 break;
822 }
823doESCkey:
824 /*
825 * This is the ONLY return from edit()!
826 */
827 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
828 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000829 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 o_lnum = curwin->w_cursor.lnum;
831
Bram Moolenaar488c6512005-08-11 20:09:58 +0000832 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000833 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100834 // When CTRL-C was typed got_int will be set, with the result
835 // that the autocommands won't be executed. When mapped got_int
836 // is not set, but let's keep the behavior the same.
837 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200838 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000839 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 continue;
843
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000844 case Ctrl_Z: /* suspend when 'insertmode' set */
845 if (!p_im)
846 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100847 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100848#ifdef CURSOR_SHAPE
849 ui_cursor_shape(); /* may need to update cursor shape */
850#endif
851 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000852
853 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000854#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100855 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000856 goto docomplete;
857#endif
858 if (echeck_abbr(Ctrl_O + ABBR_OFF))
859 break;
860 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000861
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000862 /* don't move the cursor left when 'virtualedit' has "onemore". */
863 if (ve_flags & VE_ONEMORE)
864 {
865 ins_at_eol = FALSE;
866 nomove = TRUE;
867 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000868 count = 0;
869 goto doESCkey;
870
Bram Moolenaar572cb562005-08-05 21:35:02 +0000871 case K_INS: /* toggle insert/replace mode */
872 case K_KINS:
873 ins_insert(replaceState);
874 break;
875
876 case K_SELECT: /* end of Select mode mapping - ignore */
877 break;
878
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000879 case K_HELP: /* Help key works like <ESC> <Help> */
880 case K_F1:
881 case K_XF1:
882 stuffcharReadbuff(K_HELP);
883 if (p_im)
884 need_start_insertmode = TRUE;
885 goto doESCkey;
886
887#ifdef FEAT_NETBEANS_INTG
888 case K_F21: /* NetBeans command */
889 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000890 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000891 --no_mapping;
892 netbeans_keycommand(i);
893 break;
894#endif
895
896 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 case NUL:
898 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000899 /* For ^@ the trailing ESC will end the insert, unless there is an
900 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
902 && c != Ctrl_A && !p_im)
903 goto doESCkey; /* quit insert mode */
904 inserted_space = FALSE;
905 break;
906
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000907 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 ins_reg();
909 auto_format(FALSE, TRUE);
910 inserted_space = FALSE;
911 break;
912
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000913 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 ins_ctrl_g();
915 break;
916
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000917 case Ctrl_HAT: /* switch input mode and/or langmap */
918 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 break;
920
921#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000922 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (!p_ari)
924 goto normalchar;
925 ins_ctrl_();
926 break;
927#endif
928
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000929 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200930#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100931 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 goto docomplete;
933#endif
934 /* FALLTHROUGH */
935
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000936 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100937 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000939 if (has_compl_option(FALSE))
940 goto docomplete;
941 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 ins_shift(c, lastc);
945 auto_format(FALSE, TRUE);
946 inserted_space = FALSE;
947 break;
948
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 case K_KDEL:
951 ins_del();
952 auto_format(FALSE, TRUE);
953 break;
954
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 case Ctrl_H:
957 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
958 auto_format(FALSE, TRUE);
959 break;
960
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000961 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200962#ifdef FEAT_JOB_CHANNEL
963 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
964 {
965 // In a prompt window CTRL-W is used for window commands.
966 // Use Shift-CTRL-W to delete a word.
967 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200968 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200969 nomove = TRUE;
970 count = 0;
971 goto doESCkey;
972 }
973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
975 auto_format(FALSE, TRUE);
976 break;
977
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000978 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000979# ifdef FEAT_COMPL_FUNC
980 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100981 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000982 goto docomplete;
983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
985 auto_format(FALSE, TRUE);
986 inserted_space = FALSE;
987 break;
988
989#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000990 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 case K_LEFTMOUSE_NM:
992 case K_LEFTDRAG:
993 case K_LEFTRELEASE:
994 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100995 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 case K_MIDDLEMOUSE:
997 case K_MIDDLEDRAG:
998 case K_MIDDLERELEASE:
999 case K_RIGHTMOUSE:
1000 case K_RIGHTDRAG:
1001 case K_RIGHTRELEASE:
1002 case K_X1MOUSE:
1003 case K_X1DRAG:
1004 case K_X1RELEASE:
1005 case K_X2MOUSE:
1006 case K_X2DRAG:
1007 case K_X2RELEASE:
1008 ins_mouse(c);
1009 break;
1010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001011 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001012 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 break;
1014
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001016 ins_mousescroll(MSCR_UP);
1017 break;
1018
1019 case K_MOUSELEFT: /* Scroll wheel left */
1020 ins_mousescroll(MSCR_LEFT);
1021 break;
1022
1023 case K_MOUSERIGHT: /* Scroll wheel right */
1024 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 break;
1026#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001027 case K_PS:
1028 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1029 if (cmdchar == K_PS)
1030 /* invoked from normal mode, bail out */
1031 goto doESCkey;
1032 break;
1033 case K_PE:
1034 /* Got K_PE without K_PS, ignore. */
1035 break;
1036
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001037#ifdef FEAT_GUI_TABLINE
1038 case K_TABLINE:
1039 case K_TABMENU:
1040 ins_tabline(c);
1041 break;
1042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 break;
1046
Bram Moolenaar754b5602006-02-09 23:53:20 +00001047 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001048 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001049 did_cursorhold = TRUE;
1050 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001051
Bram Moolenaar4f974752019-02-17 17:44:42 +01001052#ifdef FEAT_GUI_MSWIN
1053 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1054 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001055 case K_F4:
1056 if (mod_mask != MOD_MASK_ALT)
1057 goto normalchar;
1058 break;
1059#endif
1060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#ifdef FEAT_GUI
1062 case K_VER_SCROLLBAR:
1063 ins_scroll();
1064 break;
1065
1066 case K_HOR_SCROLLBAR:
1067 ins_horscroll();
1068 break;
1069#endif
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case K_S_HOME:
1074 case K_C_HOME:
1075 ins_home(c);
1076 break;
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 case K_S_END:
1081 case K_C_END:
1082 ins_end(c);
1083 break;
1084
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001085 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001086 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1087 ins_s_left();
1088 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001089 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 break;
1091
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 case K_C_LEFT:
1094 ins_s_left();
1095 break;
1096
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001098 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1099 ins_s_right();
1100 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001101 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 break;
1103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 case K_C_RIGHT:
1106 ins_s_right();
1107 break;
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001110 if (pum_visible())
1111 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001112 if (mod_mask & MOD_MASK_SHIFT)
1113 ins_pageup();
1114 else
1115 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 break;
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 case K_PAGEUP:
1120 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001121 if (pum_visible())
1122 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 ins_pageup();
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001127 if (pum_visible())
1128 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001129 if (mod_mask & MOD_MASK_SHIFT)
1130 ins_pagedown();
1131 else
1132 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 break;
1134
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 case K_PAGEDOWN:
1137 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001138 if (pum_visible())
1139 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 ins_pagedown();
1141 break;
1142
1143#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 ins_drop();
1146 break;
1147#endif
1148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 c = TAB;
1151 /* FALLTHROUGH */
1152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001154#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001155 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 goto docomplete;
1157#endif
1158 inserted_space = FALSE;
1159 if (ins_tab())
1160 goto normalchar; /* insert TAB as a normal char */
1161 auto_format(FALSE, TRUE);
1162 break;
1163
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001164 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 c = CAR;
1166 /* FALLTHROUGH */
1167 case CAR:
1168 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001169#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 /* In a quickfix window a <CR> jumps to the error under the
1171 * cursor. */
1172 if (bt_quickfix(curbuf) && c == CAR)
1173 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001174 if (curwin->w_llist_ref == NULL) /* quickfix window */
1175 do_cmdline_cmd((char_u *)".cc");
1176 else /* location list window */
1177 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 break;
1179 }
1180#endif
1181#ifdef FEAT_CMDWIN
1182 if (cmdwin_type != 0)
1183 {
1184 /* Execute the command in the cmdline window. */
1185 cmdwin_result = CAR;
1186 goto doESCkey;
1187 }
1188#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001189#ifdef FEAT_JOB_CHANNEL
1190 if (bt_prompt(curbuf))
1191 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001192 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001193 if (!bt_prompt(curbuf))
1194 // buffer changed to a non-prompt buffer, get out of
1195 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001196 goto doESCkey;
1197 break;
1198 }
1199#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001200 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 goto doESCkey; /* out of memory */
1202 auto_format(FALSE, FALSE);
1203 inserted_space = FALSE;
1204 break;
1205
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001206 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001207 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 if (has_compl_option(TRUE))
1210 goto docomplete;
1211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001213#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 c = ins_digraph();
1215 if (c == NUL)
1216 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001218 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar572cb562005-08-05 21:35:02 +00001220 case Ctrl_X: /* Enter CTRL-X mode */
1221 ins_ctrl_x();
1222 break;
1223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001225 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 goto normalchar;
1227 goto docomplete;
1228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001230 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 goto normalchar;
1232 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001233
1234 case 's': /* Spelling completion after ^X */
1235 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001236 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001237 goto normalchar;
1238 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001240 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001241 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1244 if (p_im)
1245 {
1246 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1247 break;
1248 goto doESCkey;
1249 }
1250 goto normalchar;
1251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 /* FALLTHROUGH */
1253
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001254 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 case Ctrl_N:
1256 /* if 'complete' is empty then plain ^P is no longer special,
1257 * but it is under other ^X modes */
1258 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001259 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001260 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 goto normalchar;
1262
1263docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001264 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001265#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001266 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001267#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001268 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001270#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001271 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001272#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001273 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 case Ctrl_Y: /* copy from previous line or scroll down */
1277 case Ctrl_E: /* copy from next line or scroll up */
1278 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 break;
1280
1281 default:
1282#ifdef UNIX
1283 if (c == intr_char) /* special interrupt char */
1284 goto do_intr;
1285#endif
1286
Bram Moolenaare659c952011-05-19 17:25:41 +02001287normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001289 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001291#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001292 if (!p_paste)
1293 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001294 /* Trigger InsertCharPre. */
1295 char_u *str = do_insert_char_pre(c);
1296 char_u *p;
1297
1298 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001299 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001300 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001301 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001302 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001303 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001304 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001305 c = PTR2CHAR(p);
1306 if (c == CAR || c == K_KENTER || c == NL)
1307 ins_eol(c);
1308 else
1309 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001310 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001311 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001312 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001313 vim_free(str);
1314 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001315 }
1316
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001317 /* If the new value is already inserted or an empty string
1318 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001319 if (c == NUL)
1320 break;
1321 }
1322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#ifdef FEAT_SMARTINDENT
1324 /* Try to perform smart-indenting. */
1325 ins_try_si(c);
1326#endif
1327
1328 if (c == ' ')
1329 {
1330 inserted_space = TRUE;
1331#ifdef FEAT_CINDENT
1332 if (inindent(0))
1333 can_cindent = FALSE;
1334#endif
1335 if (Insstart_blank_vcol == MAXCOL
1336 && curwin->w_cursor.lnum == Insstart.lnum)
1337 Insstart_blank_vcol = get_nolist_virtcol();
1338 }
1339
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001340 /* Insert a normal character and check for abbreviations on a
1341 * special character. Let CTRL-] expand abbreviations without
1342 * inserting it. */
1343 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001344 // Add ABBR_OFF for characters above 0x100, this is
1345 // what check_abbr() expects.
1346 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1347 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
1349 insert_special(c, FALSE, FALSE);
1350#ifdef FEAT_RIGHTLEFT
1351 revins_legal++;
1352 revins_chars++;
1353#endif
1354 }
1355
1356 auto_format(FALSE, TRUE);
1357
1358#ifdef FEAT_FOLDING
1359 /* When inserting a character the cursor line must never be in a
1360 * closed fold. */
1361 foldOpenCursor();
1362#endif
1363 break;
1364 } /* end of switch (c) */
1365
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001366 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001367 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001368#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001369 /* but not in CTRL-X mode, a script can't restore the state */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001370 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001371#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001372 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001373 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /* If the cursor was moved we didn't just insert a space */
1376 if (arrow_used)
1377 inserted_space = FALSE;
1378
1379#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001380 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
1382force_cindent:
1383 /*
1384 * Indent now if a key was typed that is in 'cinkeys'.
1385 */
1386 if (in_cinkeys(c, ' ', line_is_white))
1387 {
1388 if (stop_arrow() == OK)
1389 /* re-indent the current line */
1390 do_c_expr_indent();
1391 }
1392 }
1393#endif /* FEAT_CINDENT */
1394
1395 } /* for (;;) */
1396 /* NOTREACHED */
1397}
1398
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001399 int
1400ins_need_undo_get(void)
1401{
1402 return ins_need_undo;
1403}
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405/*
1406 * Redraw for Insert mode.
1407 * This is postponed until getting the next character to make '$' in the 'cpo'
1408 * option work correctly.
1409 * Only redraw when there are no characters available. This speeds up
1410 * inserting sequences of characters (e.g., for CTRL-R).
1411 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001412 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001413ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001415#ifdef FEAT_CONCEAL
1416 linenr_T conceal_old_cursor_line = 0;
1417 linenr_T conceal_new_cursor_line = 0;
1418 int conceal_update_lines = FALSE;
1419#endif
1420
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001421 if (char_avail())
1422 return;
1423
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001424 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1425 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001426 if (ready && (has_cursormovedI()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001427# ifdef FEAT_TEXT_PROP
1428 || popup_visible
1429# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001430# if defined(FEAT_CONCEAL)
1431 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001432# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001433 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001434 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001435 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001436 {
1437# ifdef FEAT_SYN_HL
1438 /* Need to update the screen first, to make sure syntax
1439 * highlighting is correct after making a change (e.g., inserting
1440 * a "(". The autocommand may also require a redraw, so it's done
1441 * again below, unfortunately. */
1442 if (syntax_present(curwin) && must_redraw)
1443 update_screen(0);
1444# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001445 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001446 {
1447 /* Make sure curswant is correct, an autocommand may call
1448 * getcurpos(). */
1449 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001450 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001451 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001452#ifdef FEAT_TEXT_PROP
1453 if (popup_visible)
1454 popup_check_cursor_pos();
1455#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001456# ifdef FEAT_CONCEAL
1457 if (curwin->w_p_cole > 0)
1458 {
1459 conceal_old_cursor_line = last_cursormoved.lnum;
1460 conceal_new_cursor_line = curwin->w_cursor.lnum;
1461 conceal_update_lines = TRUE;
1462 }
1463# endif
1464 last_cursormoved = curwin->w_cursor;
1465 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001466
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001467 /* Trigger TextChangedI if b_changedtick differs. */
1468 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001469 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001470 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001471 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001472 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001473 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001474
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001475 // save and restore curwin and curbuf, in case the autocmd changes them
1476 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001477 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001478 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001479 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001480 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1481 u_save(curwin->w_cursor.lnum,
1482 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001484
Bram Moolenaar5a093432018-02-10 18:15:19 +01001485 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1486 * TextChangedI will need to trigger for backwards compatibility, thus use
1487 * different b_last_changedtick* variables. */
1488 if (ready && has_textchangedP()
1489 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1490 && pum_visible())
1491 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001492 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001493 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001494
1495 // save and restore curwin and curbuf, in case the autocmd changes them
1496 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001497 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001498 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001499 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001500 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1501 u_save(curwin->w_cursor.lnum,
1502 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001503 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001504
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001505 // Trigger SafeState if nothing is pending.
1506 may_trigger_safestate(ready
1507 && !ins_compl_active()
1508 && !pum_visible());
1509
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001510#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001511 if ((conceal_update_lines
1512 && (conceal_old_cursor_line != conceal_new_cursor_line
1513 || conceal_cursor_line(curwin)))
1514 || need_cursor_line_redraw)
1515 {
1516 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001517 redrawWinline(curwin, conceal_old_cursor_line);
1518 redrawWinline(curwin, conceal_new_cursor_line == 0
1519 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001520 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001521 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001522 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001523#endif
1524 if (must_redraw)
1525 update_screen(0);
1526 else if (clear_cmdline || redraw_cmdline)
1527 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001528 showruler(FALSE);
1529 setcursor();
1530 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531}
1532
1533/*
1534 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1535 */
1536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001537ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001540 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
1542 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001543 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
1545 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001548 did_putchar = TRUE;
1549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1551
1552#ifdef FEAT_CMDL_INFO
1553 add_to_showcmd_c(Ctrl_V);
1554#endif
1555
1556 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001557 if (did_putchar)
1558 /* when the line fits in 'columns' the '^' is at the start of the next
1559 * line and will not removed by the redraw */
1560 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#ifdef FEAT_CMDL_INFO
1562 clear_showcmd();
1563#endif
1564 insert_special(c, FALSE, TRUE);
1565#ifdef FEAT_RIGHTLEFT
1566 revins_chars++;
1567 revins_legal++;
1568#endif
1569}
1570
1571/*
1572 * Put a character directly onto the screen. It's not stored in a buffer.
1573 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1574 */
1575static int pc_status;
1576#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1577#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1578#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1579#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581static int pc_attr;
1582static int pc_row;
1583static int pc_col;
1584
1585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001586edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
1588 int attr;
1589
1590 if (ScreenLines != NULL)
1591 {
1592 update_topline(); /* just in case w_topline isn't valid */
1593 validate_cursor();
1594 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001595 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 else
1597 attr = 0;
1598 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001599 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601#ifdef FEAT_RIGHTLEFT
1602 if (curwin->w_p_rl)
1603 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001604 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (has_mbyte)
1606 {
1607 int fix_col = mb_fix_col(pc_col, pc_row);
1608
1609 if (fix_col != pc_col)
1610 {
1611 screen_putchar(' ', pc_row, fix_col, attr);
1612 --curwin->w_wcol;
1613 pc_status = PC_STATUS_RIGHT;
1614 }
1615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
1617 else
1618#endif
1619 {
1620 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (mb_lefthalve(pc_row, pc_col))
1622 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
1624
1625 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1629 pc_status = PC_STATUS_SET;
1630 }
1631 screen_putchar(c, pc_row, pc_col, attr);
1632 }
1633}
1634
Bram Moolenaarf2732452018-06-03 14:47:35 +02001635#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1636/*
1637 * Return the effective prompt for the current buffer.
1638 */
1639 char_u *
1640prompt_text(void)
1641{
1642 if (curbuf->b_prompt_text == NULL)
1643 return (char_u *)"% ";
1644 return curbuf->b_prompt_text;
1645}
1646
1647/*
1648 * Prepare for prompt mode: Make sure the last line has the prompt text.
1649 * Move the cursor to this line.
1650 */
1651 static void
1652init_prompt(int cmdchar_todo)
1653{
1654 char_u *prompt = prompt_text();
1655 char_u *text;
1656
1657 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1658 text = ml_get_curline();
1659 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1660 {
1661 // prompt is missing, insert it or append a line with it
1662 if (*text == NUL)
1663 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1664 else
1665 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1666 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1667 coladvance((colnr_T)MAXCOL);
1668 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1669 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001670
1671 // Insert always starts after the prompt, allow editing text after it.
1672 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1673 || Insstart_orig.col != (int)STRLEN(prompt))
1674 {
1675 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001676 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001677 Insstart_orig = Insstart;
1678 Insstart_textlen = Insstart.col;
1679 Insstart_blank_vcol = MAXCOL;
1680 arrow_used = FALSE;
1681 }
1682
Bram Moolenaarf2732452018-06-03 14:47:35 +02001683 if (cmdchar_todo == 'A')
1684 coladvance((colnr_T)MAXCOL);
1685 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001686 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001687 /* Make sure the cursor is in a valid position. */
1688 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001689}
1690
1691/*
1692 * Return TRUE if the cursor is in the editable position of the prompt line.
1693 */
1694 int
1695prompt_curpos_editable()
1696{
1697 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1698 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1699}
1700#endif
1701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702/*
1703 * Undo the previous edit_putchar().
1704 */
1705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001706edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (pc_status == PC_STATUS_RIGHT)
1711 ++curwin->w_wcol;
1712 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001713 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1716 }
1717}
1718
1719/*
1720 * Called when p_dollar is set: display a '$' at the end of the changed text
1721 * Only works when cursor is in the line that changes.
1722 */
1723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001724display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 colnr_T save_col;
1727
1728 if (!redrawing())
1729 return;
1730
1731 cursor_off();
1732 save_col = curwin->w_cursor.col;
1733 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (has_mbyte)
1735 {
1736 char_u *p;
1737
1738 /* If on the last byte of a multi-byte move to the first byte. */
1739 p = ml_get_curline();
1740 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001743 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 {
1745 edit_putchar('$', FALSE);
1746 dollar_vcol = curwin->w_virtcol;
1747 }
1748 curwin->w_cursor.col = save_col;
1749}
1750
1751/*
1752 * Call this function before moving the cursor from the normal insert position
1753 * in insert mode.
1754 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001756undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001758 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001760 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001761 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
1763}
1764
1765/*
1766 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1767 * Keep the cursor on the same character.
1768 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1769 * type == INDENT_DEC decrease indent (for CTRL-D)
1770 * type == INDENT_SET set indent to "amount"
1771 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1772 */
1773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774change_indent(
1775 int type,
1776 int amount,
1777 int round,
1778 int replaced, /* replaced character, put on replace stack */
1779 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 int vcol;
1782 int last_vcol;
1783 int insstart_less; /* reduction for Insstart.col */
1784 int new_cursor_col;
1785 int i;
1786 char_u *ptr;
1787 int save_p_list;
1788 int start_col;
1789 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 colnr_T orig_col = 0; /* init for GCC */
1791 char_u *new_line, *orig_line = NULL; /* init for GCC */
1792
1793 /* VREPLACE mode needs to know what the line was like before changing */
1794 if (State & VREPLACE_FLAG)
1795 {
1796 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1797 orig_col = curwin->w_cursor.col;
1798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 /* for the following tricks we don't want list mode */
1801 save_p_list = curwin->w_p_list;
1802 curwin->w_p_list = FALSE;
1803 vc = getvcol_nolist(&curwin->w_cursor);
1804 vcol = vc;
1805
1806 /*
1807 * For Replace mode we need to fix the replace stack later, which is only
1808 * possible when the cursor is in the indent. Remember the number of
1809 * characters before the cursor if it's possible.
1810 */
1811 start_col = curwin->w_cursor.col;
1812
1813 /* determine offset from first non-blank */
1814 new_cursor_col = curwin->w_cursor.col;
1815 beginline(BL_WHITE);
1816 new_cursor_col -= curwin->w_cursor.col;
1817
1818 insstart_less = curwin->w_cursor.col;
1819
1820 /*
1821 * If the cursor is in the indent, compute how many screen columns the
1822 * cursor is to the left of the first non-blank.
1823 */
1824 if (new_cursor_col < 0)
1825 vcol = get_indent() - vcol;
1826
1827 if (new_cursor_col > 0) /* can't fix replace stack */
1828 start_col = -1;
1829
1830 /*
1831 * Set the new indent. The cursor will be put on the first non-blank.
1832 */
1833 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001834 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 else
1836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 int save_State = State;
1838
1839 /* Avoid being called recursively. */
1840 if (State & VREPLACE_FLAG)
1841 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001842 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
1845 insstart_less -= curwin->w_cursor.col;
1846
1847 /*
1848 * Try to put cursor on same character.
1849 * If the cursor is at or after the first non-blank in the line,
1850 * compute the cursor column relative to the column of the first
1851 * non-blank character.
1852 * If we are not in insert mode, leave the cursor on the first non-blank.
1853 * If the cursor is before the first non-blank, position it relative
1854 * to the first non-blank, counted in screen columns.
1855 */
1856 if (new_cursor_col >= 0)
1857 {
1858 /*
1859 * When changing the indent while the cursor is touching it, reset
1860 * Insstart_col to 0.
1861 */
1862 if (new_cursor_col == 0)
1863 insstart_less = MAXCOL;
1864 new_cursor_col += curwin->w_cursor.col;
1865 }
1866 else if (!(State & INSERT))
1867 new_cursor_col = curwin->w_cursor.col;
1868 else
1869 {
1870 /*
1871 * Compute the screen column where the cursor should be.
1872 */
1873 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001874 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
1876 /*
1877 * Advance the cursor until we reach the right screen column.
1878 */
1879 vcol = last_vcol = 0;
1880 new_cursor_col = -1;
1881 ptr = ml_get_curline();
1882 while (vcol <= (int)curwin->w_virtcol)
1883 {
1884 last_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001886 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001889 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 }
1891 vcol = last_vcol;
1892
1893 /*
1894 * May need to insert spaces to be able to position the cursor on
1895 * the right screen column.
1896 */
1897 if (vcol != (int)curwin->w_virtcol)
1898 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001899 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001901 ptr = alloc(i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (ptr != NULL)
1903 {
1904 new_cursor_col += i;
1905 ptr[i] = NUL;
1906 while (--i >= 0)
1907 ptr[i] = ' ';
1908 ins_str(ptr);
1909 vim_free(ptr);
1910 }
1911 }
1912
1913 /*
1914 * When changing the indent while the cursor is in it, reset
1915 * Insstart_col to 0.
1916 */
1917 insstart_less = MAXCOL;
1918 }
1919
1920 curwin->w_p_list = save_p_list;
1921
1922 if (new_cursor_col <= 0)
1923 curwin->w_cursor.col = 0;
1924 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001925 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 curwin->w_set_curswant = TRUE;
1927 changed_cline_bef_curs();
1928
1929 /*
1930 * May have to adjust the start of the insert.
1931 */
1932 if (State & INSERT)
1933 {
1934 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1935 {
1936 if ((int)Insstart.col <= insstart_less)
1937 Insstart.col = 0;
1938 else
1939 Insstart.col -= insstart_less;
1940 }
1941 if ((int)ai_col <= insstart_less)
1942 ai_col = 0;
1943 else
1944 ai_col -= insstart_less;
1945 }
1946
1947 /*
1948 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1949 * If the number of characters before the cursor decreased, need to pop a
1950 * few characters from the replace stack.
1951 * If the number of characters before the cursor increased, need to push a
1952 * few NULs onto the replace stack.
1953 */
1954 if (REPLACE_NORMAL(State) && start_col >= 0)
1955 {
1956 while (start_col > (int)curwin->w_cursor.col)
1957 {
1958 replace_join(0); /* remove a NUL from the replace stack */
1959 --start_col;
1960 }
1961 while (start_col < (int)curwin->w_cursor.col || replaced)
1962 {
1963 replace_push(NUL);
1964 if (replaced)
1965 {
1966 replace_push(replaced);
1967 replaced = NUL;
1968 }
1969 ++start_col;
1970 }
1971 }
1972
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 /*
1974 * For VREPLACE mode, we also have to fix the replace stack. In this case
1975 * it is always possible because we backspace over the whole line and then
1976 * put it back again the way we wanted it.
1977 */
1978 if (State & VREPLACE_FLAG)
1979 {
1980 /* If orig_line didn't allocate, just return. At least we did the job,
1981 * even if you can't backspace. */
1982 if (orig_line == NULL)
1983 return;
1984
1985 /* Save new line */
1986 new_line = vim_strsave(ml_get_curline());
1987 if (new_line == NULL)
1988 return;
1989
1990 /* We only put back the new line up to the cursor */
1991 new_line[curwin->w_cursor.col] = NUL;
1992
1993 /* Put back original line */
1994 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1995 curwin->w_cursor.col = orig_col;
1996
1997 /* Backspace from cursor to start of line */
1998 backspace_until_column(0);
1999
2000 /* Insert new stuff into line again */
2001 ins_bytes(new_line);
2002
2003 vim_free(new_line);
2004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005}
2006
2007/*
2008 * Truncate the space at the end of a line. This is to be used only in an
2009 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2010 * modes.
2011 */
2012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002013truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014{
2015 int i;
2016
2017 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002018 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
2020 if (State & REPLACE_FLAG)
2021 replace_join(0); /* remove a NUL from the replace stack */
2022 }
2023 line[i + 1] = NUL;
2024}
2025
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026/*
2027 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2028 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002029 * Will attempt not to go before "col" even when there is a composing
2030 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 */
2032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002033backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 while ((int)curwin->w_cursor.col > col)
2036 {
2037 curwin->w_cursor.col--;
2038 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002039 replace_do_bs(col);
2040 else if (!del_char_after_col(col))
2041 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
2043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002045/*
2046 * Like del_char(), but make sure not to go before column "limit_col".
2047 * Only matters when there are composing characters.
2048 * Return TRUE when something was deleted.
2049 */
2050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002051del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002052{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002053 if (enc_utf8 && limit_col >= 0)
2054 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002055 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002056
2057 /* Make sure the cursor is at the start of a character, but
2058 * skip forward again when going too far back because of a
2059 * composing character. */
2060 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002061 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002062 {
2063 int l = utf_ptr2len(ml_get_cursor());
2064
2065 if (l == 0) /* end of line */
2066 break;
2067 curwin->w_cursor.col += l;
2068 }
2069 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2070 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002071 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002072 }
2073 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002074 (void)del_char(FALSE);
2075 return TRUE;
2076}
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078/*
2079 * Next character is interpreted literally.
2080 * A one, two or three digit decimal number is interpreted as its byte value.
2081 * If one or two digits are entered, the next character is given to vungetc().
2082 * For Unicode a character > 255 may be returned.
2083 */
2084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002085get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
2087 int cc;
2088 int nc;
2089 int i;
2090 int hex = FALSE;
2091 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
2094 if (got_int)
2095 return Ctrl_C;
2096
2097#ifdef FEAT_GUI
2098 /*
2099 * In GUI there is no point inserting the internal code for a special key.
2100 * It is more useful to insert the string "<KEY>" instead. This would
2101 * probably be useful in a text window too, but it would not be
2102 * vi-compatible (maybe there should be an option for it?) -- webb
2103 */
2104 if (gui.in_use)
2105 ++allow_keys;
2106#endif
2107#ifdef USE_ON_FLY_SCROLL
2108 dont_scroll = TRUE; /* disallow scrolling here */
2109#endif
2110 ++no_mapping; /* don't map the next key hits */
2111 cc = 0;
2112 i = 0;
2113 for (;;)
2114 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002115 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01002117 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 add_to_showcmd(nc);
2119#endif
2120 if (nc == 'x' || nc == 'X')
2121 hex = TRUE;
2122 else if (nc == 'o' || nc == 'O')
2123 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 else if (nc == 'u' || nc == 'U')
2125 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 else
2127 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002128 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
2130 if (!vim_isxdigit(nc))
2131 break;
2132 cc = cc * 16 + hex2nr(nc);
2133 }
2134 else if (octal)
2135 {
2136 if (nc < '0' || nc > '7')
2137 break;
2138 cc = cc * 8 + nc - '0';
2139 }
2140 else
2141 {
2142 if (!VIM_ISDIGIT(nc))
2143 break;
2144 cc = cc * 10 + nc - '0';
2145 }
2146
2147 ++i;
2148 }
2149
Bram Moolenaar13505972019-01-24 15:04:48 +01002150 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 cc = 255; /* limit range to 0-255 */
2152 nc = 0;
2153
2154 if (hex) /* hex: up to two chars */
2155 {
2156 if (i >= 2)
2157 break;
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 else if (unicode) /* Unicode: up to four or eight chars */
2160 {
2161 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
2162 break;
2163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 else if (i >= 3) /* decimal or octal: up to three chars */
2165 break;
2166 }
2167 if (i == 0) /* no number entered */
2168 {
2169 if (nc == K_ZERO) /* NUL is stored as NL */
2170 {
2171 cc = '\n';
2172 nc = 0;
2173 }
2174 else
2175 {
2176 cc = nc;
2177 nc = 0;
2178 }
2179 }
2180
2181 if (cc == 0) /* NUL is stored as NL */
2182 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00002183 if (enc_dbcs && (cc & 0xff) == 0)
2184 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
2185 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187 --no_mapping;
2188#ifdef FEAT_GUI
2189 if (gui.in_use)
2190 --allow_keys;
2191#endif
2192 if (nc)
2193 vungetc(nc);
2194 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
2195 return cc;
2196}
2197
2198/*
2199 * Insert character, taking care of special keys and mod_mask
2200 */
2201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202insert_special(
2203 int c,
2204 int allow_modmask,
2205 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206{
2207 char_u *p;
2208 int len;
2209
2210 /*
2211 * Special function key, translate into "<Key>". Up to the last '>' is
2212 * inserted with ins_str(), so as not to replace characters in replace
2213 * mode.
2214 * Only use mod_mask for special keys, to avoid things like <S-Space>,
2215 * unless 'allow_modmask' is TRUE.
2216 */
Bram Moolenaard0573012017-10-28 21:11:06 +02002217#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 /* Command-key never produces a normal key */
2219 if (mod_mask & MOD_MASK_CMD)
2220 allow_modmask = TRUE;
2221#endif
2222 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
2223 {
2224 p = get_special_key_name(c, mod_mask);
2225 len = (int)STRLEN(p);
2226 c = p[len - 1];
2227 if (len > 2)
2228 {
2229 if (stop_arrow() == FAIL)
2230 return;
2231 p[len - 1] = NUL;
2232 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002233 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 ctrlv = FALSE;
2235 }
2236 }
2237 if (stop_arrow() == OK)
2238 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2239}
2240
2241/*
2242 * Special characters in this context are those that need processing other
2243 * than the simple insertion that can be performed here. This includes ESC
2244 * which terminates the insert, and CR/NL which need special processing to
2245 * open up a new line. This routine tries to optimize insertions performed by
2246 * the "redo", "undo" or "put" commands, so it needs to know when it should
2247 * stop and defer processing to the "normal" mechanism.
2248 * '0' and '^' are special, because they can be followed by CTRL-D.
2249 */
2250#ifdef EBCDIC
2251# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2252#else
2253# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2254#endif
2255
Bram Moolenaar13505972019-01-24 15:04:48 +01002256#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002258/*
2259 * "flags": INSCHAR_FORMAT - force formatting
2260 * INSCHAR_CTRLV - char typed just after CTRL-V
2261 * INSCHAR_NO_FEX - don't use 'formatexpr'
2262 *
2263 * NOTE: passes the flags value straight through to internal_format() which,
2264 * beside INSCHAR_FORMAT (above), is also looking for these:
2265 * INSCHAR_DO_COM - format comments
2266 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002269insertchar(
2270 int c, /* character to insert or NUL */
2271 int flags, /* INSCHAR_FORMAT, etc. */
2272 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002277 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002279 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282 /*
2283 * Try to break the line in two or more pieces when:
2284 * - Always do this if we have been called to do formatting only.
2285 * - Always do this when 'formatoptions' has the 'a' flag and the line
2286 * ends in white space.
2287 * - Otherwise:
2288 * - Don't do this if inserting a blank
2289 * - Don't do this if an existing character is being replaced, unless
2290 * we're in VREPLACE mode.
2291 * - Do this if the cursor is not on the line where insert started
2292 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2293 * before the insert.
2294 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2295 * before 'textwidth'
2296 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002297 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002298 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002299 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 && *ml_get_cursor() != NUL)
2303 && (curwin->w_cursor.lnum != Insstart.lnum
2304 || ((!has_format_option(FO_INS_LONG)
2305 || Insstart_textlen <= (colnr_T)textwidth)
2306 && (!fo_ins_blank
2307 || Insstart_blank_vcol <= (colnr_T)textwidth
2308 ))))))
2309 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002310 /* Format with 'formatexpr' when it's set. Use internal formatting
2311 * when 'formatexpr' isn't set or it returns non-zero. */
2312#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002313 int do_internal = TRUE;
2314 colnr_T virtcol = get_nolist_virtcol()
2315 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002316
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002317 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2318 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002319 {
2320 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
2321 /* It may be required to save for undo again, e.g. when setline()
2322 * was called. */
2323 ins_need_undo = TRUE;
2324 }
2325 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002327 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 if (c == NUL) /* only formatting was wanted */
2331 return;
2332
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002333 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (did_ai && (int)c == end_comment_pending)
2335 {
2336 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002337 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 int middle_len, end_len;
2339 int i;
2340
2341 /*
2342 * Need to remove existing (middle) comment leader and insert end
2343 * comment leader. First, check what comment leader we can find.
2344 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002345 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002346 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002348 // Skip middle-comment string
2349 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 ++p;
2351 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002352 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002353 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 --middle_len;
2355
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002356 // Find the end-comment string
2357 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 ++p;
2359 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2360
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002361 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002363 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 ;
2365 i++;
2366
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002367 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 i -= middle_len;
2369
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002370 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2372 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002373 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 backspace_until_column(i);
2375
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002376 // Insert the end-comment string, except for the last
2377 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 ins_bytes_len(lead_end, end_len - 1);
2379 }
2380 }
2381 }
2382 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 did_ai = FALSE;
2385#ifdef FEAT_SMARTINDENT
2386 did_si = FALSE;
2387 can_si = FALSE;
2388 can_si_back = FALSE;
2389#endif
2390
2391 /*
2392 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2393 * This speeds up normal text input considerably.
2394 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2395 * need to re-indent at a ':', or any other character (but not what
2396 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002397 * Don't do this when there an InsertCharPre autocommand is defined,
2398 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002399 * Do the check for InsertCharPre before the call to vpeekc() because the
2400 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 */
2402#ifdef USE_ON_FLY_SCROLL
2403 dont_scroll = FALSE; /* allow scrolling here */
2404#endif
2405
2406 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002408 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 && vpeekc() != NUL
2410 && !(State & REPLACE_FLAG)
2411#ifdef FEAT_CINDENT
2412 && !cindent_on()
2413#endif
2414#ifdef FEAT_RIGHTLEFT
2415 && !p_ri
2416#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002417 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
2419#define INPUT_BUFLEN 100
2420 char_u buf[INPUT_BUFLEN + 1];
2421 int i;
2422 colnr_T virtcol = 0;
2423
2424 buf[0] = c;
2425 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002426 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 virtcol = get_nolist_virtcol();
2428 /*
2429 * Stop the string when:
2430 * - no more chars available
2431 * - finding a special character (command key)
2432 * - buffer is full
2433 * - running into the 'textwidth' boundary
2434 * - need to check for abbreviation: A non-word char after a word-char
2435 */
2436 while ( (c = vpeekc()) != NUL
2437 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 && i < INPUT_BUFLEN
2440 && (textwidth == 0
2441 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2442 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2443 {
2444#ifdef FEAT_RIGHTLEFT
2445 c = vgetc();
2446 if (p_hkmap && KeyTyped)
2447 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 buf[i++] = c;
2449#else
2450 buf[i++] = vgetc();
2451#endif
2452 }
2453
2454#ifdef FEAT_DIGRAPHS
2455 do_digraph(-1); /* clear digraphs */
2456 do_digraph(buf[i-1]); /* may be the start of a digraph */
2457#endif
2458 buf[i] = NUL;
2459 ins_str(buf);
2460 if (flags & INSCHAR_CTRLV)
2461 {
2462 redo_literal(*buf);
2463 i = 1;
2464 }
2465 else
2466 i = 0;
2467 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002468 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470 else
2471 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002472 int cc;
2473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2475 {
2476 char_u buf[MB_MAXBYTES + 1];
2477
2478 (*mb_char2bytes)(c, buf);
2479 buf[cc] = NUL;
2480 ins_char_bytes(buf, cc);
2481 AppendCharToRedobuff(c);
2482 }
2483 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 ins_char(c);
2486 if (flags & INSCHAR_CTRLV)
2487 redo_literal(c);
2488 else
2489 AppendCharToRedobuff(c);
2490 }
2491 }
2492}
2493
2494/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002495 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002496 *
2497 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2498 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002499 */
2500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002501internal_format(
2502 int textwidth,
2503 int second_indent,
2504 int flags,
2505 int format_only,
2506 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002507{
2508 int cc;
2509 int save_char = NUL;
2510 int haveto_redraw = FALSE;
2511 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002512 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002513 int fo_white_par = has_format_option(FO_WHITE_PAR);
2514 int first_line = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002515 colnr_T leader_len;
2516 int no_leader = FALSE;
2517 int do_comments = (flags & INSCHAR_DO_COM);
Bram Moolenaar0026d472014-09-09 16:32:39 +02002518#ifdef FEAT_LINEBREAK
2519 int has_lbr = curwin->w_p_lbr;
2520
2521 /* make sure win_lbr_chartabsize() counts correctly */
2522 curwin->w_p_lbr = FALSE;
2523#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002524
2525 /*
2526 * When 'ai' is off we don't want a space under the cursor to be
2527 * deleted. Replace it with an 'x' temporarily.
2528 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002529 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002530 {
2531 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002532 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002533 {
2534 save_char = cc;
2535 pchar_cursor('x');
2536 }
2537 }
2538
2539 /*
2540 * Repeat breaking lines, until the current line is not too long.
2541 */
2542 while (!got_int)
2543 {
2544 int startcol; /* Cursor column at entry */
2545 int wantcol; /* column at textwidth border */
2546 int foundcol; /* column for start of spaces */
2547 int end_foundcol = 0; /* column for start of word */
2548 colnr_T len;
2549 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002550 int orig_col = 0;
2551 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002552 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002553 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002554 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002555
Bram Moolenaar97b98102009-11-17 16:41:01 +00002556 virtcol = get_nolist_virtcol()
2557 + char2cells(c != NUL ? c : gchar_cursor());
2558 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002559 break;
2560
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002561 if (no_leader)
2562 do_comments = FALSE;
2563 else if (!(flags & INSCHAR_FORMAT)
2564 && has_format_option(FO_WRAP_COMS))
2565 do_comments = TRUE;
2566
2567 /* Don't break until after the comment leader */
2568 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002569 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002570 else
2571 leader_len = 0;
2572
2573 /* If the line doesn't start with a comment leader, then don't
2574 * start one in a following broken line. Avoids that a %word
2575 * moved to the start of the next line causes all following lines
2576 * to start with %. */
2577 if (leader_len == 0)
2578 no_leader = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002579 if (!(flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002580 && leader_len == 0
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002581 && !has_format_option(FO_WRAP))
2582
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002583 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002584 if ((startcol = curwin->w_cursor.col) == 0)
2585 break;
2586
2587 /* find column of textwidth border */
2588 coladvance((colnr_T)textwidth);
2589 wantcol = curwin->w_cursor.col;
2590
Bram Moolenaar97b98102009-11-17 16:41:01 +00002591 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002592 foundcol = 0;
2593
2594 /*
2595 * Find position to break at.
2596 * Stop at first entered white when 'formatoptions' has 'v'
2597 */
2598 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002599 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002600 || curwin->w_cursor.lnum != Insstart.lnum
2601 || curwin->w_cursor.col >= Insstart.col)
2602 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002603 if (curwin->w_cursor.col == startcol && c != NUL)
2604 cc = c;
2605 else
2606 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002607 if (WHITECHAR(cc))
2608 {
2609 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002610 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002611
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002612 // find start of sequence of blanks
2613 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002614 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2615 {
2616 dec_cursor();
2617 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002618
2619 // Increment count of how many whitespace chars in this
2620 // group; we only need to know if it's more than one.
2621 if (wcc < 2)
2622 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002623 }
2624 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
2625 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002626
2627 // Don't break after a period when 'formatoptions' has 'p' and
2628 // there are less than two spaces.
2629 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2630 continue;
2631
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002632 /* Don't break until after the comment leader */
2633 if (curwin->w_cursor.col < leader_len)
2634 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002635 if (has_format_option(FO_ONE_LETTER))
2636 {
2637 /* do not break after one-letter words */
2638 if (curwin->w_cursor.col == 0)
2639 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002640 /* do not break "#a b" when 'tw' is 2 */
2641 if (curwin->w_cursor.col <= leader_len)
2642 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002643 col = curwin->w_cursor.col;
2644 dec_cursor();
2645 cc = gchar_cursor();
2646
2647 if (WHITECHAR(cc))
2648 continue; /* one-letter, continue */
2649 curwin->w_cursor.col = col;
2650 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002651
2652 inc_cursor();
2653
2654 end_foundcol = end_col + 1;
2655 foundcol = curwin->w_cursor.col;
2656 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002657 break;
2658 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002659 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002660 {
2661 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002662 if (curwin->w_cursor.col != startcol)
2663 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002664 /* Don't break until after the comment leader */
2665 if (curwin->w_cursor.col < leader_len)
2666 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002667 col = curwin->w_cursor.col;
2668 inc_cursor();
2669 /* Don't change end_foundcol if already set. */
2670 if (foundcol != curwin->w_cursor.col)
2671 {
2672 foundcol = curwin->w_cursor.col;
2673 end_foundcol = foundcol;
2674 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2675 break;
2676 }
2677 curwin->w_cursor.col = col;
2678 }
2679
2680 if (curwin->w_cursor.col == 0)
2681 break;
2682
2683 col = curwin->w_cursor.col;
2684
2685 dec_cursor();
2686 cc = gchar_cursor();
2687
2688 if (WHITECHAR(cc))
2689 continue; /* break with space */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002690 /* Don't break until after the comment leader */
2691 if (curwin->w_cursor.col < leader_len)
2692 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002693
2694 curwin->w_cursor.col = col;
2695
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002696 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002697 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002698 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2699 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002700 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002701 if (curwin->w_cursor.col == 0)
2702 break;
2703 dec_cursor();
2704 }
2705
2706 if (foundcol == 0) /* no spaces, cannot break line */
2707 {
2708 curwin->w_cursor.col = startcol;
2709 break;
2710 }
2711
2712 /* Going to break the line, remove any "$" now. */
2713 undisplay_dollar();
2714
2715 /*
2716 * Offset between cursor position and line break is used by replace
2717 * stack functions. VREPLACE does not use this, and backspaces
2718 * over the text instead.
2719 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002720 if (State & VREPLACE_FLAG)
2721 orig_col = startcol; /* Will start backspacing from here */
2722 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002723 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002724
2725 /*
2726 * adjust startcol for spaces that will be deleted and
2727 * characters that will remain on top line
2728 */
2729 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002730 while ((cc = gchar_cursor(), WHITECHAR(cc))
2731 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002732 inc_cursor();
2733 startcol -= curwin->w_cursor.col;
2734 if (startcol < 0)
2735 startcol = 0;
2736
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002737 if (State & VREPLACE_FLAG)
2738 {
2739 /*
2740 * In VREPLACE mode, we will backspace over the text to be
2741 * wrapped, so save a copy now to put on the next line.
2742 */
2743 saved_text = vim_strsave(ml_get_cursor());
2744 curwin->w_cursor.col = orig_col;
2745 if (saved_text == NULL)
2746 break; /* Can't do it, out of memory */
2747 saved_text[startcol] = NUL;
2748
2749 /* Backspace over characters that will move to the next line */
2750 if (!fo_white_par)
2751 backspace_until_column(foundcol);
2752 }
2753 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002754 {
2755 /* put cursor after pos. to break line */
2756 if (!fo_white_par)
2757 curwin->w_cursor.col = foundcol;
2758 }
2759
2760 /*
2761 * Split the line just before the margin.
2762 * Only insert/delete lines, but don't really redraw the window.
2763 */
2764 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2765 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002766 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002767 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002768 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2769 if (!(flags & INSCHAR_COM_LIST))
2770 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002771
2772 replace_offset = 0;
2773 if (first_line)
2774 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002775 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002776 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002777 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002778 * This section is for auto-wrap of numeric lists. When not
2779 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2780 * flag will be set and open_line() will handle it (as seen
2781 * above). The code here (and in get_number_indent()) will
2782 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002783 */
2784 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002785 second_indent =
2786 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002787 if (second_indent >= 0)
2788 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002789 if (State & VREPLACE_FLAG)
2790 change_indent(INDENT_SET, second_indent,
2791 FALSE, NUL, TRUE);
2792 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002793 if (leader_len > 0 && second_indent - leader_len > 0)
2794 {
2795 int i;
2796 int padding = second_indent - leader_len;
2797
2798 /* We started at the first_line of a numbered list
2799 * that has a comment. the open_line() function has
2800 * inserted the proper comment leader and positioned
2801 * the cursor at the end of the split line. Now we
2802 * add the additional whitespace needed after the
2803 * comment leader for the numbered list. */
2804 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002805 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002806 }
2807 else
2808 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002809 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002810 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002811 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002812 }
2813 first_line = FALSE;
2814 }
2815
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002816 if (State & VREPLACE_FLAG)
2817 {
2818 /*
2819 * In VREPLACE mode we have backspaced over the text to be
2820 * moved, now we re-insert it into the new line.
2821 */
2822 ins_bytes(saved_text);
2823 vim_free(saved_text);
2824 }
2825 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002826 {
2827 /*
2828 * Check if cursor is not past the NUL off the line, cindent
2829 * may have added or removed indent.
2830 */
2831 curwin->w_cursor.col += startcol;
2832 len = (colnr_T)STRLEN(ml_get_curline());
2833 if (curwin->w_cursor.col > len)
2834 curwin->w_cursor.col = len;
2835 }
2836
2837 haveto_redraw = TRUE;
2838#ifdef FEAT_CINDENT
2839 can_cindent = TRUE;
2840#endif
2841 /* moved the cursor, don't autoindent or cindent now */
2842 did_ai = FALSE;
2843#ifdef FEAT_SMARTINDENT
2844 did_si = FALSE;
2845 can_si = FALSE;
2846 can_si_back = FALSE;
2847#endif
2848 line_breakcheck();
2849 }
2850
2851 if (save_char != NUL) /* put back space after cursor */
2852 pchar_cursor(save_char);
2853
Bram Moolenaar0026d472014-09-09 16:32:39 +02002854#ifdef FEAT_LINEBREAK
2855 curwin->w_p_lbr = has_lbr;
2856#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002857 if (!format_only && haveto_redraw)
2858 {
2859 update_topline();
2860 redraw_curbuf_later(VALID);
2861 }
2862}
2863
2864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * Called after inserting or deleting text: When 'formatoptions' includes the
2866 * 'a' flag format from the current line until the end of the paragraph.
2867 * Keep the cursor at the same position relative to the text.
2868 * The caller must have saved the cursor line for undo, following ones will be
2869 * saved here.
2870 */
2871 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002872auto_format(
2873 int trailblank, /* when TRUE also format with trailing blank */
2874 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 pos_T pos;
2877 colnr_T len;
2878 char_u *old;
2879 char_u *new, *pnew;
2880 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002881 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
2883 if (!has_format_option(FO_AUTO))
2884 return;
2885
2886 pos = curwin->w_cursor;
2887 old = ml_get_curline();
2888
2889 /* may remove added space */
2890 check_auto_format(FALSE);
2891
2892 /* Don't format in Insert mode when the cursor is on a trailing blank, the
2893 * user might insert normal text next. Also skip formatting when "1" is
2894 * in 'formatoptions' and there is a single character before the cursor.
2895 * Otherwise the line would be broken and when typing another non-white
2896 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002897 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 if (*old != NUL && !trailblank && wasatend)
2899 {
2900 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002901 cc = gchar_cursor();
2902 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2903 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002905 cc = gchar_cursor();
2906 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
2908 curwin->w_cursor = pos;
2909 return;
2910 }
2911 curwin->w_cursor = pos;
2912 }
2913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
2915 * comments. */
2916 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002917 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
2920 /*
2921 * May start formatting in a previous line, so that after "x" a word is
2922 * moved to the previous line if it fits there now. Only when this is not
2923 * the start of a paragraph.
2924 */
2925 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2926 {
2927 --curwin->w_cursor.lnum;
2928 if (u_save_cursor() == FAIL)
2929 return;
2930 }
2931
2932 /*
2933 * Do the formatting and restore the cursor position. "saved_cursor" will
2934 * be adjusted for the text formatting.
2935 */
2936 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002937 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 curwin->w_cursor = saved_cursor;
2939 saved_cursor.lnum = 0;
2940
2941 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2942 {
2943 /* "cannot happen" */
2944 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2945 coladvance((colnr_T)MAXCOL);
2946 }
2947 else
2948 check_cursor_col();
2949
2950 /* Insert mode: If the cursor is now after the end of the line while it
2951 * previously wasn't, the line was broken. Because of the rule above we
2952 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2953 * formatted. */
2954 if (!wasatend && has_format_option(FO_WHITE_PAR))
2955 {
2956 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002957 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 if (curwin->w_cursor.col == len)
2959 {
2960 pnew = vim_strnsave(new, len + 2);
2961 pnew[len] = ' ';
2962 pnew[len + 1] = NUL;
2963 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
2964 /* remove the space later */
2965 did_add_space = TRUE;
2966 }
2967 else
2968 /* may remove added space */
2969 check_auto_format(FALSE);
2970 }
2971
2972 check_cursor();
2973}
2974
2975/*
2976 * When an extra space was added to continue a paragraph for auto-formatting,
2977 * delete it now. The space must be under the cursor, just after the insert
2978 * position.
2979 */
2980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002981check_auto_format(
2982 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002985 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 if (did_add_space)
2988 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002989 cc = gchar_cursor();
2990 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 /* Somehow the space was removed already. */
2992 did_add_space = FALSE;
2993 else
2994 {
2995 if (!end_insert)
2996 {
2997 inc_cursor();
2998 c = gchar_cursor();
2999 dec_cursor();
3000 }
3001 if (c != NUL)
3002 {
3003 /* The space is no longer at the end of the line, delete it. */
3004 del_char(FALSE);
3005 did_add_space = FALSE;
3006 }
3007 }
3008 }
3009}
3010
3011/*
3012 * Find out textwidth to be used for formatting:
3013 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02003014 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 * if invalid value, use 0.
3016 * Set default to window width (maximum 79) for "gq" operator.
3017 */
3018 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003019comp_textwidth(
3020 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 int textwidth;
3023
3024 textwidth = curbuf->b_p_tw;
3025 if (textwidth == 0 && curbuf->b_p_wm)
3026 {
3027 /* The width is the window width minus 'wrapmargin' minus all the
3028 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003029 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#ifdef FEAT_CMDWIN
3031 if (cmdwin_type != 0)
3032 textwidth -= 1;
3033#endif
3034#ifdef FEAT_FOLDING
3035 textwidth -= curwin->w_p_fdc;
3036#endif
3037#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003038 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 textwidth -= 1;
3040#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02003041 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 textwidth -= 8;
3043 }
3044 if (textwidth < 0)
3045 textwidth = 0;
3046 if (ff && textwidth == 0)
3047 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003048 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (textwidth > 79)
3050 textwidth = 79;
3051 }
3052 return textwidth;
3053}
3054
3055/*
3056 * Put a character in the redo buffer, for when just after a CTRL-V.
3057 */
3058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003059redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061 char_u buf[10];
3062
3063 /* Only digits need special treatment. Translate them into a string of
3064 * three digits. */
3065 if (VIM_ISDIGIT(c))
3066 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003067 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 AppendToRedobuff(buf);
3069 }
3070 else
3071 AppendCharToRedobuff(c);
3072}
3073
3074/*
3075 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003076 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079start_arrow(
3080 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003082 start_arrow_common(end_insert_pos, TRUE);
3083}
3084
3085/*
3086 * Like start_arrow() but with end_change argument.
3087 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
3088 */
3089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003090start_arrow_with_change(
3091 pos_T *end_insert_pos, /* can be NULL */
3092 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003093{
3094 start_arrow_common(end_insert_pos, end_change);
3095 if (!end_change)
3096 {
3097 AppendCharToRedobuff(Ctrl_G);
3098 AppendCharToRedobuff('U');
3099 }
3100}
3101
3102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003103start_arrow_common(
3104 pos_T *end_insert_pos, /* can be NULL */
3105 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003106{
3107 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
3109 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01003110 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 arrow_used = TRUE; /* this means we stopped the current insert */
3112 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003113#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003114 check_spell_redraw();
3115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116}
3117
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003118#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003119/*
3120 * If we skipped highlighting word at cursor, do it now.
3121 * It may be skipped again, thus reset spell_redraw_lnum first.
3122 */
3123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003124check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003125{
3126 if (spell_redraw_lnum != 0)
3127 {
3128 linenr_T lnum = spell_redraw_lnum;
3129
3130 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01003131 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00003132 }
3133}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003134
Bram Moolenaar217ad922005-03-20 22:37:15 +00003135#endif
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137/*
3138 * stop_arrow() is called before a change is made in insert mode.
3139 * If an arrow key has been used, start a new insertion.
3140 * Returns FAIL if undo is impossible, shouldn't insert then.
3141 */
3142 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003143stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
3145 if (arrow_used)
3146 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02003147 Insstart = curwin->w_cursor; /* new insertion starts here */
3148 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
3149 /* Don't update the original insert position when moved to the
3150 * right, except when nothing was inserted yet. */
3151 update_Insstart_orig = FALSE;
3152 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
3153
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (u_save_cursor() == OK)
3155 {
3156 arrow_used = FALSE;
3157 ins_need_undo = FALSE;
3158 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02003159
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (State & VREPLACE_FLAG)
3162 {
3163 orig_line_count = curbuf->b_ml.ml_line_count;
3164 vr_lines_changed = 1;
3165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 ResetRedobuff();
3167 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00003168 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 else if (ins_need_undo)
3171 {
3172 if (u_save_cursor() == OK)
3173 ins_need_undo = FALSE;
3174 }
3175
3176#ifdef FEAT_FOLDING
3177 /* Always open fold at the cursor line when inserting something. */
3178 foldOpenCursor();
3179#endif
3180
3181 return (arrow_used || ins_need_undo ? FAIL : OK);
3182}
3183
3184/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003185 * Do a few things to stop inserting.
3186 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
3187 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 */
3189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003190stop_insert(
3191 pos_T *end_insert_pos,
3192 int esc, /* called by ins_esc() */
3193 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003195 int cc;
3196 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198 stop_redo_ins();
3199 replace_flush(); /* abandon replace stack */
3200
3201 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003202 * Save the inserted text for later redo with ^@ and CTRL-A.
3203 * Don't do it when "restart_edit" was set and nothing was inserted,
3204 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003206 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003207 if (did_restart_edit == 0 || (ptr != NULL
3208 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003209 {
3210 vim_free(last_insert);
3211 last_insert = ptr;
3212 last_insert_skip = new_insert_skip;
3213 }
3214 else
3215 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003217 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 /* Auto-format now. It may seem strange to do this when stopping an
3220 * insertion (or moving the cursor), but it's required when appending
3221 * a line and having it end in a space. But only do it when something
3222 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003223 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003225 pos_T tpos = curwin->w_cursor;
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 /* When the cursor is at the end of the line after a space the
3228 * formatting will move it to the following word. Avoid that by
3229 * moving the cursor onto the space. */
3230 cc = 'x';
3231 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
3232 {
3233 dec_cursor();
3234 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003235 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003236 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
3238
3239 auto_format(TRUE, FALSE);
3240
Bram Moolenaar1c465442017-03-12 20:10:05 +01003241 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003242 {
3243 if (gchar_cursor() != NUL)
3244 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003245 // If the cursor is still at the same character, also keep
3246 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003247 if (gchar_cursor() == NUL
3248 && curwin->w_cursor.lnum == tpos.lnum
3249 && curwin->w_cursor.col == tpos.col)
3250 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
3253
3254 /* If a space was inserted for auto-formatting, remove it now. */
3255 check_auto_format(TRUE);
3256
3257 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003258 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00003259 * Do this when ESC was used or moving the cursor up/down.
3260 * Check for the old position still being valid, just in case the text
3261 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01003262 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003263 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3264 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003266 pos_T tpos = curwin->w_cursor;
3267
3268 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003269 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003270 for (;;)
3271 {
3272 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3273 --curwin->w_cursor.col;
3274 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003275 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003276 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003277 if (del_char(TRUE) == FAIL)
3278 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003279 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003280 if (curwin->w_cursor.lnum != tpos.lnum)
3281 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003282 else
3283 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003284 /* reset tpos, could have been invalidated in the loop above */
3285 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003286 tpos.col++;
3287 if (cc != NUL && gchar_pos(&tpos) == NUL)
3288 ++curwin->w_cursor.col; /* put cursor back on the NUL */
3289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 /* <C-S-Right> may have started Visual mode, adjust the position for
3292 * deleted characters. */
3293 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3294 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003295 int len = (int)STRLEN(ml_get_curline());
3296
3297 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003299 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
3304 }
3305 did_ai = FALSE;
3306#ifdef FEAT_SMARTINDENT
3307 did_si = FALSE;
3308 can_si = FALSE;
3309 can_si_back = FALSE;
3310#endif
3311
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003312 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3313 * now in a different buffer. */
3314 if (end_insert_pos != NULL)
3315 {
3316 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003317 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003318 curbuf->b_op_end = *end_insert_pos;
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320}
3321
3322/*
3323 * Set the last inserted text to a single character.
3324 * Used for the replace command.
3325 */
3326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 char_u *s;
3330
3331 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (last_insert != NULL)
3334 {
3335 s = last_insert;
3336 /* Use the CTRL-V only when entering a special char */
3337 if (c < ' ' || c == DEL)
3338 *s++ = Ctrl_V;
3339 s = add_char2buf(c, s);
3340 *s++ = ESC;
3341 *s++ = NUL;
3342 last_insert_skip = 0;
3343 }
3344}
3345
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003346#if defined(EXITFREE) || defined(PROTO)
3347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003348free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003349{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003350 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003351}
3352#endif
3353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354/*
3355 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3356 * and CSI. Handle multi-byte characters.
3357 * Returns a pointer to after the added bytes.
3358 */
3359 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003360add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003362 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 int i;
3364 int len;
3365
3366 len = (*mb_char2bytes)(c, temp);
3367 for (i = 0; i < len; ++i)
3368 {
3369 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
3371 if (c == K_SPECIAL)
3372 {
3373 *s++ = K_SPECIAL;
3374 *s++ = KS_SPECIAL;
3375 *s++ = KE_FILLER;
3376 }
3377#ifdef FEAT_GUI
3378 else if (c == CSI)
3379 {
3380 *s++ = CSI;
3381 *s++ = KS_EXTRA;
3382 *s++ = (int)KE_CSI;
3383 }
3384#endif
3385 else
3386 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return s;
3389}
3390
3391/*
3392 * move cursor to start of line
3393 * if flags & BL_WHITE move to first non-white
3394 * if flags & BL_SOL move to first non-white if startofline is set,
3395 * otherwise keep "curswant" column
3396 * if flags & BL_FIX don't leave the cursor on a NUL.
3397 */
3398 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003399beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 if ((flags & BL_SOL) && !p_sol)
3402 coladvance(curwin->w_curswant);
3403 else
3404 {
3405 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 if (flags & (BL_WHITE | BL_SOL))
3409 {
3410 char_u *ptr;
3411
Bram Moolenaar1c465442017-03-12 20:10:05 +01003412 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3414 ++curwin->w_cursor.col;
3415 }
3416 curwin->w_set_curswant = TRUE;
3417 }
3418}
3419
3420/*
3421 * oneright oneleft cursor_down cursor_up
3422 *
3423 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003424 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 * Return OK when successful, FAIL when we hit a line of file boundary.
3426 */
3427
3428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003429oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (virtual_active())
3435 {
3436 pos_T prevpos = curwin->w_cursor;
3437
3438 /* Adjust for multi-wide char (excluding TAB) */
3439 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003440 coladvance(getviscol() + ((*ptr != TAB
3441 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 ? ptr2cells(ptr) : 1));
3443 curwin->w_set_curswant = TRUE;
3444 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
3445 return (prevpos.col != curwin->w_cursor.col
3446 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003450 if (*ptr == NUL)
3451 return FAIL; /* already at the very end */
3452
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003453 if (has_mbyte)
3454 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003456 l = 1;
3457
3458 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3459 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003460 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003461 return FAIL;
3462 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 curwin->w_set_curswant = TRUE;
3465 return OK;
3466}
3467
3468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003469oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (virtual_active())
3472 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003473#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 int v = getviscol();
3477
3478 if (v == 0)
3479 return FAIL;
3480
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003481#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 /* We might get stuck on 'showbreak', skip over it. */
3483 width = 1;
3484 for (;;)
3485 {
3486 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02003487 /* getviscol() is slow, skip it when 'showbreak' is empty,
3488 * 'breakindent' is not set and there are no multi-byte
3489 * characters */
3490 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003491 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 break;
3493 ++width;
3494 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003495#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 if (curwin->w_cursor.coladd == 1)
3500 {
3501 char_u *ptr;
3502
3503 /* Adjust for multi-wide char (not a TAB) */
3504 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003505 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3506 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 curwin->w_cursor.coladd = 0;
3508 }
3509
3510 curwin->w_set_curswant = TRUE;
3511 return OK;
3512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 if (curwin->w_cursor.col == 0)
3515 return FAIL;
3516
3517 curwin->w_set_curswant = TRUE;
3518 --curwin->w_cursor.col;
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 /* if the character on the left of the current cursor is a multi-byte
3521 * character, move to its first byte */
3522 if (has_mbyte)
3523 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 return OK;
3525}
3526
3527 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003528cursor_up(
3529 long n,
3530 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532 linenr_T lnum;
3533
3534 if (n > 0)
3535 {
3536 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003537 /* This fails if the cursor is already in the first line or the count
3538 * is larger than the line number and '-' is in 'cpoptions' */
3539 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return FAIL;
3541 if (n >= lnum)
3542 lnum = 1;
3543 else
3544#ifdef FEAT_FOLDING
3545 if (hasAnyFolding(curwin))
3546 {
3547 /*
3548 * Count each sequence of folded lines as one logical line.
3549 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003550 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 (void)hasFolding(lnum, &lnum, NULL);
3552
3553 while (n--)
3554 {
3555 /* move up one line */
3556 --lnum;
3557 if (lnum <= 1)
3558 break;
3559 /* If we entered a fold, move to the beginning, unless in
3560 * Insert mode or when 'foldopen' contains "all": it will open
3561 * in a moment. */
3562 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3563 (void)hasFolding(lnum, &lnum, NULL);
3564 }
3565 if (lnum < 1)
3566 lnum = 1;
3567 }
3568 else
3569#endif
3570 lnum -= n;
3571 curwin->w_cursor.lnum = lnum;
3572 }
3573
3574 /* try to advance to the column we want to be at */
3575 coladvance(curwin->w_curswant);
3576
3577 if (upd_topline)
3578 update_topline(); /* make sure curwin->w_topline is valid */
3579
3580 return OK;
3581}
3582
3583/*
3584 * Cursor down a number of logical lines.
3585 */
3586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587cursor_down(
3588 long n,
3589 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
3591 linenr_T lnum;
3592
3593 if (n > 0)
3594 {
3595 lnum = curwin->w_cursor.lnum;
3596#ifdef FEAT_FOLDING
3597 /* Move to last line of fold, will fail if it's the end-of-file. */
3598 (void)hasFolding(lnum, NULL, &lnum);
3599#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003600 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003601 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003602 if (lnum >= curbuf->b_ml.ml_line_count
3603 || (lnum + n > curbuf->b_ml.ml_line_count
3604 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 return FAIL;
3606 if (lnum + n >= curbuf->b_ml.ml_line_count)
3607 lnum = curbuf->b_ml.ml_line_count;
3608 else
3609#ifdef FEAT_FOLDING
3610 if (hasAnyFolding(curwin))
3611 {
3612 linenr_T last;
3613
3614 /* count each sequence of folded lines as one logical line */
3615 while (n--)
3616 {
3617 if (hasFolding(lnum, NULL, &last))
3618 lnum = last + 1;
3619 else
3620 ++lnum;
3621 if (lnum >= curbuf->b_ml.ml_line_count)
3622 break;
3623 }
3624 if (lnum > curbuf->b_ml.ml_line_count)
3625 lnum = curbuf->b_ml.ml_line_count;
3626 }
3627 else
3628#endif
3629 lnum += n;
3630 curwin->w_cursor.lnum = lnum;
3631 }
3632
3633 /* try to advance to the column we want to be at */
3634 coladvance(curwin->w_curswant);
3635
3636 if (upd_topline)
3637 update_topline(); /* make sure curwin->w_topline is valid */
3638
3639 return OK;
3640}
3641
3642/*
3643 * Stuff the last inserted text in the read buffer.
3644 * Last_insert actually is a copy of the redo buffer, so we
3645 * first have to remove the command.
3646 */
3647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003648stuff_inserted(
3649 int c, /* Command character to be inserted */
3650 long count, /* Repeat this many times */
3651 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
3653 char_u *esc_ptr;
3654 char_u *ptr;
3655 char_u *last_ptr;
3656 char_u last = NUL;
3657
3658 ptr = get_last_insert();
3659 if (ptr == NULL)
3660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003661 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 return FAIL;
3663 }
3664
3665 /* may want to stuff the command character, to start Insert mode */
3666 if (c != NUL)
3667 stuffcharReadbuff(c);
3668 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
3669 *esc_ptr = NUL; /* remove the ESC */
3670
3671 /* when the last char is either "0" or "^" it will be quoted if no ESC
3672 * comes after it OR if it will inserted more than once and "ptr"
3673 * starts with ^D. -- Acevedo
3674 */
3675 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3676 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3677 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3678 {
3679 last = *last_ptr;
3680 *last_ptr = NUL;
3681 }
3682
3683 do
3684 {
3685 stuffReadbuff(ptr);
3686 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
3687 if (last)
3688 stuffReadbuff((char_u *)(last == '0'
3689 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3690 : IF_EB("\026^", CTRL_V_STR "^")));
3691 }
3692 while (--count > 0);
3693
3694 if (last)
3695 *last_ptr = last;
3696
3697 if (esc_ptr != NULL)
3698 *esc_ptr = ESC; /* put the ESC back */
3699
3700 /* may want to stuff a trailing ESC, to get out of Insert mode */
3701 if (!no_esc)
3702 stuffcharReadbuff(ESC);
3703
3704 return OK;
3705}
3706
3707 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003708get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
3710 if (last_insert == NULL)
3711 return NULL;
3712 return last_insert + last_insert_skip;
3713}
3714
3715/*
3716 * Get last inserted string, and remove trailing <Esc>.
3717 * Returns pointer to allocated memory (must be freed) or NULL.
3718 */
3719 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 char_u *s;
3723 int len;
3724
3725 if (last_insert == NULL)
3726 return NULL;
3727 s = vim_strsave(last_insert + last_insert_skip);
3728 if (s != NULL)
3729 {
3730 len = (int)STRLEN(s);
3731 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
3732 s[len - 1] = NUL;
3733 }
3734 return s;
3735}
3736
3737/*
3738 * Check the word in front of the cursor for an abbreviation.
3739 * Called when the non-id character "c" has been entered.
3740 * When an abbreviation is recognized it is removed from the text and
3741 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3742 */
3743 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003744echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
3746 /* Don't check for abbreviation in paste mode, when disabled and just
3747 * after moving around with cursor keys. */
3748 if (p_paste || no_abbr || arrow_used)
3749 return FALSE;
3750
3751 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3752 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3753}
3754
3755/*
3756 * replace-stack functions
3757 *
3758 * When replacing characters, the replaced characters are remembered for each
3759 * new character. This is used to re-insert the old text when backspacing.
3760 *
3761 * There is a NUL headed list of characters for each character that is
3762 * currently in the file after the insertion point. When BS is used, one NUL
3763 * headed list is put back for the deleted character.
3764 *
3765 * For a newline, there are two NUL headed lists. One contains the characters
3766 * that the NL replaced. The extra one stores the characters after the cursor
3767 * that were deleted (always white space).
3768 *
3769 * Replace_offset is normally 0, in which case replace_push will add a new
3770 * character at the end of the stack. If replace_offset is not 0, that many
3771 * characters will be left on the stack above the newly inserted character.
3772 */
3773
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003774static char_u *replace_stack = NULL;
3775static long replace_stack_nr = 0; /* next entry in replace stack */
3776static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
3778 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779replace_push(
3780 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781{
3782 char_u *p;
3783
3784 if (replace_stack_nr < replace_offset) /* nothing to do */
3785 return;
3786 if (replace_stack_len <= replace_stack_nr)
3787 {
3788 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003789 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (p == NULL) /* out of memory */
3791 {
3792 replace_stack_len -= 50;
3793 return;
3794 }
3795 if (replace_stack != NULL)
3796 {
3797 mch_memmove(p, replace_stack,
3798 (size_t)(replace_stack_nr * sizeof(char_u)));
3799 vim_free(replace_stack);
3800 }
3801 replace_stack = p;
3802 }
3803 p = replace_stack + replace_stack_nr - replace_offset;
3804 if (replace_offset)
3805 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3806 *p = c;
3807 ++replace_stack_nr;
3808}
3809
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003810/*
3811 * Push a character onto the replace stack. Handles a multi-byte character in
3812 * reverse byte order, so that the first byte is popped off first.
3813 * Return the number of bytes done (includes composing characters).
3814 */
3815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003816replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003817{
3818 int l = (*mb_ptr2len)(p);
3819 int j;
3820
3821 for (j = l - 1; j >= 0; --j)
3822 replace_push(p[j]);
3823 return l;
3824}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826/*
3827 * Pop one item from the replace stack.
3828 * return -1 if stack empty
3829 * return replaced character or NUL otherwise
3830 */
3831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003832replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833{
3834 if (replace_stack_nr == 0)
3835 return -1;
3836 return (int)replace_stack[--replace_stack_nr];
3837}
3838
3839/*
3840 * Join the top two items on the replace stack. This removes to "off"'th NUL
3841 * encountered.
3842 */
3843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003844replace_join(
3845 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
3847 int i;
3848
3849 for (i = replace_stack_nr; --i >= 0; )
3850 if (replace_stack[i] == NUL && off-- <= 0)
3851 {
3852 --replace_stack_nr;
3853 mch_memmove(replace_stack + i, replace_stack + i + 1,
3854 (size_t)(replace_stack_nr - i));
3855 return;
3856 }
3857}
3858
3859/*
3860 * Pop bytes from the replace stack until a NUL is found, and insert them
3861 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3862 */
3863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003864replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 int cc;
3867 int oldState = State;
3868
3869 State = NORMAL; /* don't want REPLACE here */
3870 while ((cc = replace_pop()) > 0)
3871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 dec_cursor();
3874 }
3875 State = oldState;
3876}
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878/*
3879 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3880 * indicates a multi-byte char, pop the other bytes too.
3881 */
3882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003883mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884{
3885 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003886 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 int i;
3888 int c;
3889
3890 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3891 {
3892 buf[0] = cc;
3893 for (i = 1; i < n; ++i)
3894 buf[i] = replace_pop();
3895 ins_bytes_len(buf, n);
3896 }
3897 else
3898 ins_char(cc);
3899
3900 if (enc_utf8)
3901 /* Handle composing chars. */
3902 for (;;)
3903 {
3904 c = replace_pop();
3905 if (c == -1) /* stack empty */
3906 break;
3907 if ((n = MB_BYTE2LEN(c)) == 1)
3908 {
3909 /* Not a multi-byte char, put it back. */
3910 replace_push(c);
3911 break;
3912 }
3913 else
3914 {
3915 buf[0] = c;
3916 for (i = 1; i < n; ++i)
3917 buf[i] = replace_pop();
3918 if (utf_iscomposing(utf_ptr2char(buf)))
3919 ins_bytes_len(buf, n);
3920 else
3921 {
3922 /* Not a composing char, put it back. */
3923 for (i = n - 1; i >= 0; --i)
3924 replace_push(buf[i]);
3925 break;
3926 }
3927 }
3928 }
3929}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931/*
3932 * make the replace stack empty
3933 * (called when exiting replace mode)
3934 */
3935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003938 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 replace_stack_len = 0;
3940 replace_stack_nr = 0;
3941}
3942
3943/*
3944 * Handle doing a BS for one character.
3945 * cc < 0: replace stack empty, just move cursor
3946 * cc == 0: character was inserted, delete it
3947 * cc > 0: character was replaced, put cc (first byte of original char) back
3948 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003949 * When "limit_col" is >= 0, don't delete before this column. Matters when
3950 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 */
3952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003953replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 int orig_len = 0;
3957 int ins_len;
3958 int orig_vcols = 0;
3959 colnr_T start_vcol;
3960 char_u *p;
3961 int i;
3962 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964 cc = replace_pop();
3965 if (cc > 0)
3966 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01003967#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003968 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003969
3970 if (curbuf->b_has_textprop)
3971 {
3972 // Do not adjust text properties for individual delete and insert
3973 // operations, do it afterwards on the resulting text.
3974 len_before = STRLEN(ml_get_curline());
3975 ++text_prop_frozen;
3976 }
3977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (State & VREPLACE_FLAG)
3979 {
3980 /* Get the number of screen cells used by the character we are
3981 * going to delete. */
3982 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3983 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (has_mbyte)
3986 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003987 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003989 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 replace_push(cc);
3991 }
3992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 {
3994 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003996 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998 replace_pop_ins();
3999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (State & VREPLACE_FLAG)
4001 {
4002 /* Get the number of screen cells used by the inserted characters */
4003 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004004 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 vcol = start_vcol;
4006 for (i = 0; i < ins_len; ++i)
4007 {
4008 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004009 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011 vcol -= start_vcol;
4012
4013 /* Delete spaces that were inserted after the cursor to keep the
4014 * text aligned. */
4015 curwin->w_cursor.col += ins_len;
4016 while (vcol > orig_vcols && gchar_cursor() == ' ')
4017 {
4018 del_char(FALSE);
4019 ++orig_vcols;
4020 }
4021 curwin->w_cursor.col -= ins_len;
4022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
Bram Moolenaar196d1572019-01-02 23:47:18 +01004024 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01004026
4027#ifdef FEAT_TEXT_PROP
4028 if (curbuf->b_has_textprop)
4029 {
4030 size_t len_now = STRLEN(ml_get_curline());
4031
4032 --text_prop_frozen;
4033 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02004034 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01004035 }
4036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004039 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040}
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
4043/*
4044 * Map Hebrew keyboard when in hkmap mode.
4045 */
4046 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004047hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048{
4049 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
4050 {
4051 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
4052 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
4053 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
4054 static char_u map[26] =
4055 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
4056 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
4057 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
4058 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
4059 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
4060 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
4061 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
4062 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
4063 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
4064
4065 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
4066 return (int)(map[CharOrd(c)] - 1 + p_aleph);
4067 /* '-1'='sofit' */
4068 else if (c == 'x')
4069 return 'X';
4070 else if (c == 'q')
4071 return '\''; /* {geresh}={'} */
4072 else if (c == 246)
4073 return ' '; /* \"o --> ' ' for a german keyboard */
4074 else if (c == 228)
4075 return ' '; /* \"a --> ' ' -- / -- */
4076 else if (c == 252)
4077 return ' '; /* \"u --> ' ' -- / -- */
4078#ifdef EBCDIC
4079 else if (islower(c))
4080#else
4081 /* NOTE: islower() does not do the right thing for us on Linux so we
4082 * do this the same was as 5.7 and previous, so it works correctly on
4083 * all systems. Specifically, the e.g. Delete and Arrow keys are
4084 * munged and won't work if e.g. searching for Hebrew text.
4085 */
4086 else if (c >= 'a' && c <= 'z')
4087#endif
4088 return (int)(map[CharOrdLow(c)] + p_aleph);
4089 else
4090 return c;
4091 }
4092 else
4093 {
4094 switch (c)
4095 {
4096 case '`': return ';';
4097 case '/': return '.';
4098 case '\'': return ',';
4099 case 'q': return '/';
4100 case 'w': return '\'';
4101
4102 /* Hebrew letters - set offset from 'a' */
4103 case ',': c = '{'; break;
4104 case '.': c = 'v'; break;
4105 case ';': c = 't'; break;
4106 default: {
4107 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
4108
4109#ifdef EBCDIC
4110 /* see note about islower() above */
4111 if (!islower(c))
4112#else
4113 if (c < 'a' || c > 'z')
4114#endif
4115 return c;
4116 c = str[CharOrdLow(c)];
4117 break;
4118 }
4119 }
4120
4121 return (int)(CharOrdLow(c) + p_aleph);
4122 }
4123}
4124#endif
4125
4126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004127ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 int need_redraw = FALSE;
4130 int regname;
4131 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004132 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
4134 /*
4135 * If we are going to wait for a character, show a '"'.
4136 */
4137 pc_status = PC_STATUS_UNSET;
4138 if (redrawing() && !char_avail())
4139 {
4140 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00004141 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143 edit_putchar('"', TRUE);
4144#ifdef FEAT_CMDL_INFO
4145 add_to_showcmd_c(Ctrl_R);
4146#endif
4147 }
4148
4149#ifdef USE_ON_FLY_SCROLL
4150 dont_scroll = TRUE; /* disallow scrolling here */
4151#endif
4152
4153 /*
4154 * Don't map the register name. This also prevents the mode message to be
4155 * deleted when ESC is hit.
4156 */
4157 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004158 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
4161 {
4162 /* Get a third key for literal register insertion */
4163 literally = regname;
4164#ifdef FEAT_CMDL_INFO
4165 add_to_showcmd_c(literally);
4166#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004167 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
4170 --no_mapping;
4171
4172#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02004173 /* Don't call u_sync() while typing the expression or giving an error
4174 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 ++no_u_sync;
4176 if (regname == '=')
4177 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004178 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004179# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004181# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004182 /* Sync undo when evaluating the expression calls setline() or
4183 * append(), so that it can be undone separately. */
4184 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02004185
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004187
4188 // Cursor may be moved back a column.
4189 curwin->w_cursor = curpos;
4190 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004191# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004192 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (im_on)
4194 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00004197 if (regname == NUL || !valid_yank_reg(regname, FALSE))
4198 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004199 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00004201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 else
4203 {
4204#endif
4205 if (literally == Ctrl_O || literally == Ctrl_P)
4206 {
4207 /* Append the command to the redo buffer. */
4208 AppendCharToRedobuff(Ctrl_R);
4209 AppendCharToRedobuff(literally);
4210 AppendCharToRedobuff(regname);
4211
4212 do_put(regname, BACKWARD, 1L,
4213 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
4214 }
4215 else if (insert_reg(regname, literally) == FAIL)
4216 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004217 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 need_redraw = TRUE; /* remove the '"' */
4219 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004220 else if (stop_insert_mode)
4221 /* When the '=' register was used and a function was invoked that
4222 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
4223 * insert anything, need to remove the '"' */
4224 need_redraw = TRUE;
4225
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226#ifdef FEAT_EVAL
4227 }
4228 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004229 if (u_sync_once == 1)
4230 ins_need_undo = TRUE;
4231 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232#endif
4233#ifdef FEAT_CMDL_INFO
4234 clear_showcmd();
4235#endif
4236
4237 /* If the inserted register is empty, we need to remove the '"' */
4238 if (need_redraw || stuff_empty())
4239 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004240
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004241 /* Disallow starting Visual mode here, would get a weird mode. */
4242 if (!vis_active && VIsual_active)
4243 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244}
4245
4246/*
4247 * CTRL-G commands in Insert mode.
4248 */
4249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
4252 int c;
4253
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004254 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 /*
4258 * Don't map the second key. This also prevents the mode message to be
4259 * deleted when ESC is hit.
4260 */
4261 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004262 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 --no_mapping;
4264 switch (c)
4265 {
4266 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
4267 case K_UP:
4268 case Ctrl_K:
4269 case 'k': ins_up(TRUE);
4270 break;
4271
4272 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
4273 case K_DOWN:
4274 case Ctrl_J:
4275 case 'j': ins_down(TRUE);
4276 break;
4277
4278 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004279 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004281
4282 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004283 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004284 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004285 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 break;
4287
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004288 /* CTRL-G U: do not break undo with the next char */
4289 case 'U':
4290 /* Allow one left/right cursor movement with the next char,
4291 * without breaking undo. */
4292 dont_sync_undo = MAYBE;
4293 break;
4294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02004296 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298}
4299
4300/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301 * CTRL-^ in Insert mode.
4302 */
4303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004304ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004305{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004306 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004307 {
4308 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
4309 if (State & LANGMAP)
4310 {
4311 curbuf->b_p_iminsert = B_IMODE_NONE;
4312 State &= ~LANGMAP;
4313 }
4314 else
4315 {
4316 curbuf->b_p_iminsert = B_IMODE_LMAP;
4317 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004318#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004319 im_set_active(FALSE);
4320#endif
4321 }
4322 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004323#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004324 else
4325 {
4326 /* There are no ":lmap" mappings, toggle IM */
4327 if (im_get_status())
4328 {
4329 curbuf->b_p_iminsert = B_IMODE_NONE;
4330 im_set_active(FALSE);
4331 }
4332 else
4333 {
4334 curbuf->b_p_iminsert = B_IMODE_IM;
4335 State &= ~LANGMAP;
4336 im_set_active(TRUE);
4337 }
4338 }
4339#endif
4340 set_iminsert_global();
4341 showmode();
4342#ifdef FEAT_GUI
4343 /* may show different cursor shape or color */
4344 if (gui.in_use)
4345 gui_update_cursor(TRUE, FALSE);
4346#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004347#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004348 /* Show/unshow value of 'keymap' in status lines. */
4349 status_redraw_curbuf();
4350#endif
4351}
4352
4353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 * Handle ESC in insert mode.
4355 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4356 * insert.
4357 */
4358 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004359ins_esc(
4360 long *count,
4361 int cmdchar,
4362 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363{
4364 int temp;
4365 static int disabled_redraw = FALSE;
4366
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004367#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368 check_spell_redraw();
4369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370#if defined(FEAT_HANGULIN)
4371# if defined(ESC_CHG_TO_ENG_MODE)
4372 hangul_input_state_set(0);
4373# endif
4374 if (composing_hangul)
4375 {
4376 push_raw_key(composing_hangul_buffer, 2);
4377 composing_hangul = 0;
4378 }
4379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381 temp = curwin->w_cursor.col;
4382 if (disabled_redraw)
4383 {
4384 --RedrawingDisabled;
4385 disabled_redraw = FALSE;
4386 }
4387 if (!arrow_used)
4388 {
4389 /*
4390 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004391 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4392 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 */
4394 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004395 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
4397 /*
4398 * Repeating insert may take a long time. Check for
4399 * interrupt now and then.
4400 */
4401 if (*count > 0)
4402 {
4403 line_breakcheck();
4404 if (got_int)
4405 *count = 0;
4406 }
4407
4408 if (--*count > 0) /* repeat what was typed */
4409 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004410 /* Vi repeats the insert without replacing characters. */
4411 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4412 State &= ~REPLACE_FLAG;
4413
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 (void)start_redo_ins();
4415 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02004416 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 ++RedrawingDisabled;
4418 disabled_redraw = TRUE;
4419 return FALSE; /* repeat the insert */
4420 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004421 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 undisplay_dollar();
4423 }
4424
4425 /* When an autoindent was removed, curswant stays after the
4426 * indent */
4427 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4428 curwin->w_set_curswant = TRUE;
4429
4430 /* Remember the last Insert position in the '^ mark. */
4431 if (!cmdmod.keepjumps)
4432 curbuf->b_last_insert = curwin->w_cursor;
4433
4434 /*
4435 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004436 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004438 if (!nomove
4439 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004440 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004441 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004442 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443#ifdef FEAT_RIGHTLEFT
4444 && !revins_on
4445#endif
4446 )
4447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4449 {
4450 oneleft();
4451 if (restart_edit != NUL)
4452 ++curwin->w_cursor.coladd;
4453 }
4454 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
4456 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 /* Correct cursor for multi-byte character. */
4458 if (has_mbyte)
4459 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461 }
4462
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004463#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 /* Disable IM to allow typing English directly for Normal mode commands.
4465 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4466 * well). */
4467 if (!(State & LANGMAP))
4468 im_save_status(&curbuf->b_p_iminsert);
4469 im_set_active(FALSE);
4470#endif
4471
4472 State = NORMAL;
4473 /* need to position cursor again (e.g. when on a TAB ) */
4474 changed_cline_bef_curs();
4475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477#ifdef CURSOR_SHAPE
4478 ui_cursor_shape(); /* may show different cursor shape */
4479#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004480 if (!p_ek)
4481 /* Re-enable bracketed paste mode. */
4482 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004484 // When recording or for CTRL-O, need to display the new mode.
4485 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004486 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004488 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004489 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 return TRUE; /* exit Insert mode */
4492}
4493
4494#ifdef FEAT_RIGHTLEFT
4495/*
4496 * Toggle language: hkmap and revins_on.
4497 * Move to end of reverse inserted text.
4498 */
4499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004500ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501{
4502 if (revins_on && revins_chars && revins_scol >= 0)
4503 {
4504 while (gchar_cursor() != NUL && revins_chars--)
4505 ++curwin->w_cursor.col;
4506 }
4507 p_ri = !p_ri;
4508 revins_on = (State == INSERT && p_ri);
4509 if (revins_on)
4510 {
4511 revins_scol = curwin->w_cursor.col;
4512 revins_legal++;
4513 revins_chars = 0;
4514 undisplay_dollar();
4515 }
4516 else
4517 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004518 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 showmode();
4520}
4521#endif
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523/*
4524 * If 'keymodel' contains "startsel", may start selection.
4525 * Returns TRUE when a CTRL-O and other keys stuffed.
4526 */
4527 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004528ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529{
4530 if (km_startsel)
4531 switch (c)
4532 {
4533 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 case K_PAGEUP:
4536 case K_KPAGEUP:
4537 case K_PAGEDOWN:
4538 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004539# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 case K_LEFT:
4541 case K_RIGHT:
4542 case K_UP:
4543 case K_DOWN:
4544 case K_END:
4545 case K_HOME:
4546# endif
4547 if (!(mod_mask & MOD_MASK_SHIFT))
4548 break;
4549 /* FALLTHROUGH */
4550 case K_S_LEFT:
4551 case K_S_RIGHT:
4552 case K_S_UP:
4553 case K_S_DOWN:
4554 case K_S_END:
4555 case K_S_HOME:
4556 /* Start selection right away, the cursor can move with
4557 * CTRL-O when beyond the end of the line. */
4558 start_selection();
4559
4560 /* Execute the key in (insert) Select mode. */
4561 stuffcharReadbuff(Ctrl_O);
4562 if (mod_mask)
4563 {
4564 char_u buf[4];
4565
4566 buf[0] = K_SPECIAL;
4567 buf[1] = KS_MODIFIER;
4568 buf[2] = mod_mask;
4569 buf[3] = NUL;
4570 stuffReadbuff(buf);
4571 }
4572 stuffcharReadbuff(c);
4573 return TRUE;
4574 }
4575 return FALSE;
4576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
4578/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004579 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 */
4581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004582ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004584#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004585 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004586 (char_u *)((State & REPLACE_FLAG) ? "i"
4587 : replaceState == VREPLACE ? "v"
4588 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004589#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004590 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004591 if (State & REPLACE_FLAG)
4592 State = INSERT | (State & LANGMAP);
4593 else
4594 State = replaceState | (State & LANGMAP);
4595 AppendCharToRedobuff(K_INS);
4596 showmode();
4597#ifdef CURSOR_SHAPE
4598 ui_cursor_shape(); /* may show different cursor shape */
4599#endif
4600}
4601
4602/*
4603 * Pressed CTRL-O in Insert mode.
4604 */
4605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004606ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004608 if (State & VREPLACE_FLAG)
4609 restart_edit = 'V';
4610 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004611 if (State & REPLACE_FLAG)
4612 restart_edit = 'R';
4613 else
4614 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004615 if (virtual_active())
4616 ins_at_eol = FALSE; /* cursor always keeps its column */
4617 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 ins_at_eol = (gchar_cursor() == NUL);
4619}
4620
4621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 * If the cursor is on an indent, ^T/^D insert/delete one
4623 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004624 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 * with vi. But vi only supports ^T and ^D after an
4626 * autoindent, we support it everywhere.
4627 */
4628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004629ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 if (stop_arrow() == FAIL)
4632 return;
4633 AppendCharToRedobuff(c);
4634
4635 /*
4636 * 0^D and ^^D: remove all indent.
4637 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004638 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4639 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 --curwin->w_cursor.col;
4642 (void)del_char(FALSE); /* delete the '^' or '0' */
4643 /* In Replace mode, restore the characters that '^' or '0' replaced. */
4644 if (State & REPLACE_FLAG)
4645 replace_pop_ins();
4646 if (lastc == '^')
4647 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004648 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
4650 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004651 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
4653 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4654 did_ai = FALSE;
4655#ifdef FEAT_SMARTINDENT
4656 did_si = FALSE;
4657 can_si = FALSE;
4658 can_si_back = FALSE;
4659#endif
4660#ifdef FEAT_CINDENT
4661 can_cindent = FALSE; /* no cindenting after ^D or ^T */
4662#endif
4663}
4664
4665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004666ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
4668 int temp;
4669
4670 if (stop_arrow() == FAIL)
4671 return;
4672 if (gchar_cursor() == NUL) /* delete newline */
4673 {
4674 temp = curwin->w_cursor.col;
4675 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004676 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004677 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004681 /* Adjust orig_line_count in case more lines have been deleted than
4682 * have been added. That makes sure, that open_line() later
4683 * can access all buffer lines correctly */
4684 if (State & VREPLACE_FLAG &&
4685 orig_line_count > curbuf->b_ml.ml_line_count)
4686 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02004689 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
4690 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 did_ai = FALSE;
4692#ifdef FEAT_SMARTINDENT
4693 did_si = FALSE;
4694 can_si = FALSE;
4695 can_si_back = FALSE;
4696#endif
4697 AppendCharToRedobuff(K_DEL);
4698}
4699
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004700/*
4701 * Delete one character for ins_bs().
4702 */
4703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004704ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004705{
4706 dec_cursor();
4707 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4708 if (State & REPLACE_FLAG)
4709 {
4710 /* Don't delete characters before the insert point when in
4711 * Replace mode */
4712 if (curwin->w_cursor.lnum != Insstart.lnum
4713 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004714 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004715 }
4716 else
4717 (void)del_char(FALSE);
4718}
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720/*
4721 * Handle Backspace, delete-word and delete-line in Insert mode.
4722 * Return TRUE when backspace was actually used.
4723 */
4724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004725ins_bs(
4726 int c,
4727 int mode,
4728 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
4730 linenr_T lnum;
4731 int cc;
4732 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004733 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 colnr_T mincol;
4735 int did_backspace = FALSE;
4736 int in_indent;
4737 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004738 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
4740 /*
4741 * can't delete anything in an empty file
4742 * can't backup past first character in buffer
4743 * can't backup past starting point unless 'backspace' > 1
4744 * can backup to a previous line if 'backspace' == 0
4745 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004746 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 || (
4748#ifdef FEAT_RIGHTLEFT
4749 !revins_on &&
4750#endif
4751 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4752 || (!can_bs(BS_START)
4753 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004754 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4755 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4757 && curwin->w_cursor.col <= ai_col)
4758 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4759 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004760 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return FALSE;
4762 }
4763
4764 if (stop_arrow() == FAIL)
4765 return FALSE;
4766 in_indent = inindent(0);
4767#ifdef FEAT_CINDENT
4768 if (in_indent)
4769 can_cindent = FALSE;
4770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 end_comment_pending = NUL; /* After BS, don't auto-end comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772#ifdef FEAT_RIGHTLEFT
4773 if (revins_on) /* put cursor after last inserted char */
4774 inc_cursor();
4775#endif
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 /* Virtualedit:
4778 * BACKSPACE_CHAR eats a virtual space
4779 * BACKSPACE_WORD eats all coladd
4780 * BACKSPACE_LINE eats all coladd and keeps going
4781 */
4782 if (curwin->w_cursor.coladd > 0)
4783 {
4784 if (mode == BACKSPACE_CHAR)
4785 {
4786 --curwin->w_cursor.coladd;
4787 return TRUE;
4788 }
4789 if (mode == BACKSPACE_WORD)
4790 {
4791 curwin->w_cursor.coladd = 0;
4792 return TRUE;
4793 }
4794 curwin->w_cursor.coladd = 0;
4795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
4797 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004798 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 */
4800 if (curwin->w_cursor.col == 0)
4801 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004802 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004803 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804#ifdef FEAT_RIGHTLEFT
4805 || revins_on
4806#endif
4807 )
4808 {
4809 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4810 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4811 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004812 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004813 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
4815 /*
4816 * In replace mode:
4817 * cc < 0: NL was inserted, delete it
4818 * cc >= 0: NL was replaced, put original characters back
4819 */
4820 cc = -1;
4821 if (State & REPLACE_FLAG)
4822 cc = replace_pop(); /* returns -1 if NL was inserted */
4823 /*
4824 * In replace mode, in the line we started replacing, we only move the
4825 * cursor.
4826 */
4827 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4828 {
4829 dec_cursor();
4830 }
4831 else
4832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (!(State & VREPLACE_FLAG)
4834 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
4836 temp = gchar_cursor(); /* remember current char */
4837 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004838
4839 /* When "aw" is in 'formatoptions' we must delete the space at
4840 * the end of the line, otherwise the line will be broken
4841 * again when auto-formatting. */
4842 if (has_format_option(FO_AUTO)
4843 && has_format_option(FO_WHITE_PAR))
4844 {
4845 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4846 TRUE);
4847 int len;
4848
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004849 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004850 if (len > 0 && ptr[len - 1] == ' ')
4851 ptr[len - 1] = NUL;
4852 }
4853
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004854 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (temp == NUL && gchar_cursor() != NUL)
4856 inc_cursor();
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 else
4859 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861 /*
4862 * In REPLACE mode we have to put back the text that was replaced
4863 * by the NL. On the replace stack is first a NUL-terminated
4864 * sequence of characters that were deleted and then the
4865 * characters that NL replaced.
4866 */
4867 if (State & REPLACE_FLAG)
4868 {
4869 /*
4870 * Do the next ins_char() in NORMAL state, to
4871 * prevent ins_char() from replacing characters and
4872 * avoiding showmatch().
4873 */
4874 oldState = State;
4875 State = NORMAL;
4876 /*
4877 * restore characters (blanks) deleted after cursor
4878 */
4879 while (cc > 0)
4880 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004881 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004883 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 cc = replace_pop();
4885 }
4886 /* restore the characters that NL replaced */
4887 replace_pop_ins();
4888 State = oldState;
4889 }
4890 }
4891 did_ai = FALSE;
4892 }
4893 else
4894 {
4895 /*
4896 * Delete character(s) before the cursor.
4897 */
4898#ifdef FEAT_RIGHTLEFT
4899 if (revins_on) /* put cursor on last inserted char */
4900 dec_cursor();
4901#endif
4902 mincol = 0;
4903 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004904 if (mode == BACKSPACE_LINE
4905 && (curbuf->b_p_ai
4906#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004907 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004908#endif
4909 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#ifdef FEAT_RIGHTLEFT
4911 && !revins_on
4912#endif
4913 )
4914 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004915 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004917 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004919 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
4921
4922 /*
4923 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4924 */
4925 if ( mode == BACKSPACE_CHAR
4926 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004927 || ((get_sts_value() != 0
4928#ifdef FEAT_VARTABS
4929 || tabstop_count(curbuf->b_p_vsts_array)
4930#endif
4931 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004932 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 && (*(ml_get_cursor() - 1) == TAB
4934 || (*(ml_get_cursor() - 1) == ' '
4935 && (!*inserted_space_p
4936 || arrow_used))))))
4937 {
4938 int ts;
4939 colnr_T vcol;
4940 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004941 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942
4943 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 /* Compute the virtual column where we want to be. Since
4945 * 'showbreak' may get in the way, need to get the last column of
4946 * the previous character. */
4947 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004948 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 dec_cursor();
4950 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4951 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004952#ifdef FEAT_VARTABS
4953 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004954 {
4955 ts = (int)get_sw_value(curbuf);
4956 want_vcol = (want_vcol / ts) * ts;
4957 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004958 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004959 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004960 curbuf->b_p_vsts_array);
4961#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004962 if (p_sta && in_indent)
4963 ts = (int)get_sw_value(curbuf);
4964 else
4965 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 /* delete characters until we are at or before want_vcol */
4970 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004971 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004972 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 /* insert extra spaces until we are at want_vcol */
4975 while (vcol < want_vcol)
4976 {
4977 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004978 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4979 && curwin->w_cursor.col < Insstart_orig.col)
4980 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 if (State & VREPLACE_FLAG)
4983 ins_char(' ');
4984 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004987 if ((State & REPLACE_FLAG))
4988 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4991 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004992
4993 /* If we are now back where we started delete one character. Can
4994 * happen when using 'sts' and 'linebreak'. */
4995 if (vcol >= start_vcol)
4996 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998
4999 /*
5000 * Delete upto starting point, start of line or previous word.
5001 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005004 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005006 if (has_mbyte)
5007 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005008 do
5009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005011 if (!revins_on) /* put cursor on char to be deleted */
5012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005014
5015 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005016 /* look multi-byte character class */
5017 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005019 prev_cclass = cclass;
5020 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005022
5023 /* start of word? */
5024 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
5025 {
5026 mode = BACKSPACE_WORD_NOT_SPACE;
5027 temp = vim_iswordc(cc);
5028 }
5029 /* end of word? */
5030 else if (mode == BACKSPACE_WORD_NOT_SPACE
5031 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01005032 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005035 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005037 inc_cursor();
5038#ifdef FEAT_RIGHTLEFT
5039 else if (State & REPLACE_FLAG)
5040 dec_cursor();
5041#endif
5042 break;
5043 }
5044 if (State & REPLACE_FLAG)
5045 replace_do_bs(-1);
5046 else
5047 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005048 if (enc_utf8 && p_deco)
5049 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005050 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005051 /*
5052 * If there are combining characters and 'delcombine' is set
5053 * move the cursor back. Don't back up before the base
5054 * character.
5055 */
5056 if (enc_utf8 && p_deco && cpc[0] != NUL)
5057 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005058#ifdef FEAT_RIGHTLEFT
5059 if (revins_chars)
5060 {
5061 revins_chars--;
5062 revins_legal++;
5063 }
5064 if (revins_on && gchar_cursor() == NUL)
5065 break;
5066#endif
5067 }
5068 /* Just a single backspace?: */
5069 if (mode == BACKSPACE_CHAR)
5070 break;
5071 } while (
5072#ifdef FEAT_RIGHTLEFT
5073 revins_on ||
5074#endif
5075 (curwin->w_cursor.col > mincol
5076 && (curwin->w_cursor.lnum != Insstart_orig.lnum
5077 || curwin->w_cursor.col != Insstart_orig.col)));
5078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 did_backspace = TRUE;
5080 }
5081#ifdef FEAT_SMARTINDENT
5082 did_si = FALSE;
5083 can_si = FALSE;
5084 can_si_back = FALSE;
5085#endif
5086 if (curwin->w_cursor.col <= 1)
5087 did_ai = FALSE;
5088 /*
5089 * It's a little strange to put backspaces into the redo
5090 * buffer, but it makes auto-indent a lot easier to deal
5091 * with.
5092 */
5093 AppendCharToRedobuff(c);
5094
5095 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02005096 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005097 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02005098 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
5100 /* vi behaviour: the cursor moves backward but the character that
5101 * was there remains visible
5102 * Vim behaviour: the cursor moves backward and the character that
5103 * was there is erased from the screen.
5104 * We can emulate the vi behaviour by pretending there is a dollar
5105 * displayed even when there isn't.
5106 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005107 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 dollar_vcol = curwin->w_virtcol;
5109
Bram Moolenaarce3be472008-01-14 19:12:28 +00005110#ifdef FEAT_FOLDING
5111 /* When deleting a char the cursor line must never be in a closed fold.
5112 * E.g., when 'foldmethod' is indent and deleting the first non-white
5113 * char before a Tab. */
5114 if (did_backspace)
5115 foldOpenCursor();
5116#endif
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 return did_backspace;
5119}
5120
Bram Moolenaarec2da362017-01-21 20:04:22 +01005121/*
5122 * Handle receiving P_PS: start paste mode. Inserts the following text up to
5123 * P_PE literally.
5124 * When "drop" is TRUE then consume the text and drop it.
5125 */
5126 int
5127bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
5128{
5129 int c;
5130 char_u buf[NUMBUFLEN + MB_MAXBYTES];
5131 int idx = 0;
5132 char_u *end = find_termcode((char_u *)"PE");
5133 int ret_char = -1;
5134 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005135 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01005136
5137 /* If the end code is too long we can't detect it, read everything. */
5138 if (STRLEN(end) >= NUMBUFLEN)
5139 end = NULL;
5140 ++no_mapping;
5141 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005142 if (!p_paste)
5143 // Also have the side effects of setting 'paste' to make it work much
5144 // faster.
5145 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005146
Bram Moolenaarec2da362017-01-21 20:04:22 +01005147 for (;;)
5148 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005149 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01005150 if (end == NULL && vpeekc() == NUL)
5151 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005152 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005153 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01005154 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005155 if (c == NUL || got_int)
5156 // When CTRL-C was encountered the typeahead will be flushed and we
5157 // won't get the end sequence.
5158 break;
5159
Bram Moolenaarec2da362017-01-21 20:04:22 +01005160 if (has_mbyte)
5161 idx += (*mb_char2bytes)(c, buf + idx);
5162 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005163 buf[idx++] = c;
5164 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02005165 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01005166 {
5167 if (end[idx] == NUL)
5168 break; /* Found the end of paste code. */
5169 continue;
5170 }
5171 if (!drop)
5172 {
5173 switch (mode)
5174 {
5175 case PASTE_CMDLINE:
5176 put_on_cmdline(buf, idx, TRUE);
5177 break;
5178
5179 case PASTE_EX:
5180 if (gap != NULL && ga_grow(gap, idx) == OK)
5181 {
5182 mch_memmove((char *)gap->ga_data + gap->ga_len,
5183 buf, (size_t)idx);
5184 gap->ga_len += idx;
5185 }
5186 break;
5187
5188 case PASTE_INSERT:
5189 if (stop_arrow() == OK)
5190 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005191 c = buf[0];
5192 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
5193 ins_eol(c);
5194 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01005195 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005196 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01005197 AppendToRedobuffLit(buf, idx);
5198 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01005199 }
5200 break;
5201
5202 case PASTE_ONE_CHAR:
5203 if (ret_char == -1)
5204 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01005205 if (has_mbyte)
5206 ret_char = (*mb_ptr2char)(buf);
5207 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005208 ret_char = buf[0];
5209 }
5210 break;
5211 }
5212 }
5213 idx = 0;
5214 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005215
Bram Moolenaarec2da362017-01-21 20:04:22 +01005216 --no_mapping;
5217 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005218 if (!save_paste)
5219 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01005220
5221 return ret_char;
5222}
5223
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005224#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00005225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005226ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005227{
5228 /* We will be leaving the current window, unless closing another tab. */
5229 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
5230 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
5231 {
5232 undisplay_dollar();
5233 start_arrow(&curwin->w_cursor);
5234# ifdef FEAT_CINDENT
5235 can_cindent = TRUE;
5236# endif
5237 }
5238
5239 if (c == K_TABLINE)
5240 goto_tabpage(current_tab);
5241 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005242 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005243 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005244 redraw_statuslines(); /* will redraw the tabline when needed */
5245 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005246}
5247#endif
5248
5249#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005251ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 pos_T tpos;
5254
5255 undisplay_dollar();
5256 tpos = curwin->w_cursor;
5257 if (gui_do_scroll())
5258 {
5259 start_arrow(&tpos);
5260# ifdef FEAT_CINDENT
5261 can_cindent = TRUE;
5262# endif
5263 }
5264}
5265
5266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005267ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 pos_T tpos;
5270
5271 undisplay_dollar();
5272 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005273 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
5275 start_arrow(&tpos);
5276# ifdef FEAT_CINDENT
5277 can_cindent = TRUE;
5278# endif
5279 }
5280}
5281#endif
5282
5283 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005284ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005287 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289#ifdef FEAT_FOLDING
5290 if ((fdo_flags & FDO_HOR) && KeyTyped)
5291 foldOpenCursor();
5292#endif
5293 undisplay_dollar();
5294 tpos = curwin->w_cursor;
5295 if (oneleft() == OK)
5296 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005297#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5298 /* Only call start_arrow() when not busy with preediting, it will
5299 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005300 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005301#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005302 {
5303 start_arrow_with_change(&tpos, end_change);
5304 if (!end_change)
5305 AppendCharToRedobuff(K_LEFT);
5306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307#ifdef FEAT_RIGHTLEFT
5308 /* If exit reversed string, position is fixed */
5309 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5310 revins_legal++;
5311 revins_chars++;
5312#endif
5313 }
5314
5315 /*
5316 * if 'whichwrap' set for cursor in insert mode may go to
5317 * previous line
5318 */
5319 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5320 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005321 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 start_arrow(&tpos);
5323 --(curwin->w_cursor.lnum);
5324 coladvance((colnr_T)MAXCOL);
5325 curwin->w_set_curswant = TRUE; /* so we stay at the end */
5326 }
5327 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005328 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005329 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330}
5331
5332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005333ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334{
5335 pos_T tpos;
5336
5337#ifdef FEAT_FOLDING
5338 if ((fdo_flags & FDO_HOR) && KeyTyped)
5339 foldOpenCursor();
5340#endif
5341 undisplay_dollar();
5342 tpos = curwin->w_cursor;
5343 if (c == K_C_HOME)
5344 curwin->w_cursor.lnum = 1;
5345 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 curwin->w_curswant = 0;
5348 start_arrow(&tpos);
5349}
5350
5351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 pos_T tpos;
5355
5356#ifdef FEAT_FOLDING
5357 if ((fdo_flags & FDO_HOR) && KeyTyped)
5358 foldOpenCursor();
5359#endif
5360 undisplay_dollar();
5361 tpos = curwin->w_cursor;
5362 if (c == K_C_END)
5363 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5364 coladvance((colnr_T)MAXCOL);
5365 curwin->w_curswant = MAXCOL;
5366
5367 start_arrow(&tpos);
5368}
5369
5370 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005371ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005373 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374#ifdef FEAT_FOLDING
5375 if ((fdo_flags & FDO_HOR) && KeyTyped)
5376 foldOpenCursor();
5377#endif
5378 undisplay_dollar();
5379 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5380 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005381 start_arrow_with_change(&curwin->w_cursor, end_change);
5382 if (!end_change)
5383 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 (void)bck_word(1L, FALSE, FALSE);
5385 curwin->w_set_curswant = TRUE;
5386 }
5387 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005388 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005389 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390}
5391
5392 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005393ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005395 int end_change = dont_sync_undo == FALSE; // end undoable change
5396
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#ifdef FEAT_FOLDING
5398 if ((fdo_flags & FDO_HOR) && KeyTyped)
5399 foldOpenCursor();
5400#endif
5401 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005402 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005404 start_arrow_with_change(&curwin->w_cursor, end_change);
5405 if (!end_change)
5406 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 if (virtual_active())
5409 oneright();
5410 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005413 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 ++curwin->w_cursor.col;
5416 }
5417
5418#ifdef FEAT_RIGHTLEFT
5419 revins_legal++;
5420 if (revins_chars)
5421 revins_chars--;
5422#endif
5423 }
5424 /* if 'whichwrap' set for cursor in insert mode, may move the
5425 * cursor to the next line */
5426 else if (vim_strchr(p_ww, ']') != NULL
5427 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5428 {
5429 start_arrow(&curwin->w_cursor);
5430 curwin->w_set_curswant = TRUE;
5431 ++curwin->w_cursor.lnum;
5432 curwin->w_cursor.col = 0;
5433 }
5434 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005435 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005436 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437}
5438
5439 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005440ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005442 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443#ifdef FEAT_FOLDING
5444 if ((fdo_flags & FDO_HOR) && KeyTyped)
5445 foldOpenCursor();
5446#endif
5447 undisplay_dollar();
5448 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5449 || gchar_cursor() != NUL)
5450 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005451 start_arrow_with_change(&curwin->w_cursor, end_change);
5452 if (!end_change)
5453 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 (void)fwd_word(1L, FALSE, 0);
5455 curwin->w_set_curswant = TRUE;
5456 }
5457 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005458 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005459 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460}
5461
5462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005463ins_up(
5464 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465{
5466 pos_T tpos;
5467 linenr_T old_topline = curwin->w_topline;
5468#ifdef FEAT_DIFF
5469 int old_topfill = curwin->w_topfill;
5470#endif
5471
5472 undisplay_dollar();
5473 tpos = curwin->w_cursor;
5474 if (cursor_up(1L, TRUE) == OK)
5475 {
5476 if (startcol)
5477 coladvance(getvcol_nolist(&Insstart));
5478 if (old_topline != curwin->w_topline
5479#ifdef FEAT_DIFF
5480 || old_topfill != curwin->w_topfill
5481#endif
5482 )
5483 redraw_later(VALID);
5484 start_arrow(&tpos);
5485#ifdef FEAT_CINDENT
5486 can_cindent = TRUE;
5487#endif
5488 }
5489 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005490 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491}
5492
5493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005494ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
5496 pos_T tpos;
5497
5498 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005499
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005500 if (mod_mask & MOD_MASK_CTRL)
5501 {
5502 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005503 if (first_tabpage->tp_next != NULL)
5504 {
5505 start_arrow(&curwin->w_cursor);
5506 goto_tabpage(-1);
5507 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005508 return;
5509 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005510
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 tpos = curwin->w_cursor;
5512 if (onepage(BACKWARD, 1L) == OK)
5513 {
5514 start_arrow(&tpos);
5515#ifdef FEAT_CINDENT
5516 can_cindent = TRUE;
5517#endif
5518 }
5519 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005520 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
5522
5523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005524ins_down(
5525 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526{
5527 pos_T tpos;
5528 linenr_T old_topline = curwin->w_topline;
5529#ifdef FEAT_DIFF
5530 int old_topfill = curwin->w_topfill;
5531#endif
5532
5533 undisplay_dollar();
5534 tpos = curwin->w_cursor;
5535 if (cursor_down(1L, TRUE) == OK)
5536 {
5537 if (startcol)
5538 coladvance(getvcol_nolist(&Insstart));
5539 if (old_topline != curwin->w_topline
5540#ifdef FEAT_DIFF
5541 || old_topfill != curwin->w_topfill
5542#endif
5543 )
5544 redraw_later(VALID);
5545 start_arrow(&tpos);
5546#ifdef FEAT_CINDENT
5547 can_cindent = TRUE;
5548#endif
5549 }
5550 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005551 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552}
5553
5554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005555ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 pos_T tpos;
5558
5559 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005560
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005561 if (mod_mask & MOD_MASK_CTRL)
5562 {
5563 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005564 if (first_tabpage->tp_next != NULL)
5565 {
5566 start_arrow(&curwin->w_cursor);
5567 goto_tabpage(0);
5568 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005569 return;
5570 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 tpos = curwin->w_cursor;
5573 if (onepage(FORWARD, 1L) == OK)
5574 {
5575 start_arrow(&tpos);
5576#ifdef FEAT_CINDENT
5577 can_cindent = TRUE;
5578#endif
5579 }
5580 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005581 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583
5584#ifdef FEAT_DND
5585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005586ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5589}
5590#endif
5591
5592/*
5593 * Handle TAB in Insert or Replace mode.
5594 * Return TRUE when the TAB needs to be inserted like a normal character.
5595 */
5596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005597ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
5599 int ind;
5600 int i;
5601 int temp;
5602
5603 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5604 Insstart_blank_vcol = get_nolist_virtcol();
5605 if (echeck_abbr(TAB + ABBR_OFF))
5606 return FALSE;
5607
5608 ind = inindent(0);
5609#ifdef FEAT_CINDENT
5610 if (ind)
5611 can_cindent = FALSE;
5612#endif
5613
5614 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005615 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 */
5617 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005618#ifdef FEAT_VARTABS
5619 && !(p_sta && ind
5620 /* These five lines mean 'tabstop' != 'shiftwidth' */
5621 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5622 || (tabstop_count(curbuf->b_p_vts_array) == 1
5623 && tabstop_first(curbuf->b_p_vts_array)
5624 != get_sw_value(curbuf))
5625 || (tabstop_count(curbuf->b_p_vts_array) == 0
5626 && curbuf->b_p_ts != get_sw_value(curbuf))))
5627 && tabstop_count(curbuf->b_p_vsts_array) == 0
5628#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005629 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005630#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005631 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 return TRUE;
5633
5634 if (stop_arrow() == FAIL)
5635 return TRUE;
5636
5637 did_ai = FALSE;
5638#ifdef FEAT_SMARTINDENT
5639 did_si = FALSE;
5640 can_si = FALSE;
5641 can_si_back = FALSE;
5642#endif
5643 AppendToRedobuff((char_u *)"\t");
5644
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005645#ifdef FEAT_VARTABS
5646 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
5647 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005648 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005649 temp -= get_nolist_virtcol() % temp;
5650 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005651 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005652 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005653 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005654 curbuf->b_p_vsts_array);
5655 else /* otherwise use 'tabstop' */
5656 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5657 curbuf->b_p_vts_array);
5658#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005660 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005661 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
5662 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 else /* otherwise use 'tabstop' */
5664 temp = (int)curbuf->b_p_ts;
5665 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 /*
5669 * Insert the first space with ins_char(). It will delete one char in
5670 * replace mode. Insert the rest with ins_str(); it will not delete any
5671 * chars. For VREPLACE mode, we use ins_char() for all characters.
5672 */
5673 ins_char(' ');
5674 while (--temp > 0)
5675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 if (State & VREPLACE_FLAG)
5677 ins_char(' ');
5678 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {
5680 ins_str((char_u *)" ");
5681 if (State & REPLACE_FLAG) /* no char replaced */
5682 replace_push(NUL);
5683 }
5684 }
5685
5686 /*
5687 * When 'expandtab' not set: Replace spaces by TABs where possible.
5688 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005689#ifdef FEAT_VARTABS
5690 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5691 || get_sts_value() > 0
5692 || (p_sta && ind)))
5693#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005694 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
5697 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 char_u *saved_line = NULL; /* init for GCC */
5699 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 pos_T fpos;
5701 pos_T *cursor;
5702 colnr_T want_vcol, vcol;
5703 int change_col = -1;
5704 int save_list = curwin->w_p_list;
5705
5706 /*
5707 * Get the current line. For VREPLACE mode, don't make real changes
5708 * yet, just work on a copy of the line.
5709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 if (State & VREPLACE_FLAG)
5711 {
5712 pos = curwin->w_cursor;
5713 cursor = &pos;
5714 saved_line = vim_strsave(ml_get_curline());
5715 if (saved_line == NULL)
5716 return FALSE;
5717 ptr = saved_line + pos.col;
5718 }
5719 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 ptr = ml_get_cursor();
5722 cursor = &curwin->w_cursor;
5723 }
5724
5725 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
5726 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5727 curwin->w_p_list = FALSE;
5728
5729 /* Find first white before the cursor */
5730 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005731 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
5733 --fpos.col;
5734 --ptr;
5735 }
5736
5737 /* In Replace mode, don't change characters before the insert point. */
5738 if ((State & REPLACE_FLAG)
5739 && fpos.lnum == Insstart.lnum
5740 && fpos.col < Insstart.col)
5741 {
5742 ptr += Insstart.col - fpos.col;
5743 fpos.col = Insstart.col;
5744 }
5745
5746 /* compute virtual column numbers of first white and cursor */
5747 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5748 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5749
Bram Moolenaar597a4222014-06-25 14:39:50 +02005750 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5751 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01005752 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005754 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (vcol + i > want_vcol)
5756 break;
5757 if (*ptr != TAB)
5758 {
5759 *ptr = TAB;
5760 if (change_col < 0)
5761 {
5762 change_col = fpos.col; /* Column of first change */
5763 /* May have to adjust Insstart */
5764 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5765 Insstart.col = fpos.col;
5766 }
5767 }
5768 ++fpos.col;
5769 ++ptr;
5770 vcol += i;
5771 }
5772
5773 if (change_col >= 0)
5774 {
5775 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005776 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
5778 /* Skip over the spaces we need. */
5779 while (vcol < want_vcol && *ptr == ' ')
5780 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005781 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 ++ptr;
5783 ++repl_off;
5784 }
5785 if (vcol > want_vcol)
5786 {
5787 /* Must have a char with 'showbreak' just before it. */
5788 --ptr;
5789 --repl_off;
5790 }
5791 fpos.col += repl_off;
5792
5793 /* Delete following spaces. */
5794 i = cursor->col - fpos.col;
5795 if (i > 0)
5796 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005797 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005799 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 for (temp = i; --temp >= 0; )
5801 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005802#ifdef FEAT_TEXT_PROP
5803 curbuf->b_ml.ml_line_len -= i;
5804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005806#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005807 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005808 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005809 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005810 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5811 (char_u *)"\t", 1);
5812 }
5813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 cursor->col -= i;
5815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 /*
5817 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5818 * backspacing over the changed spacing and then inserting the new
5819 * spacing.
5820 */
5821 if (State & VREPLACE_FLAG)
5822 {
5823 /* Backspace from real cursor to change_col */
5824 backspace_until_column(change_col);
5825
5826 /* Insert each char in saved_line from changed_col to
5827 * ptr-cursor */
5828 ins_bytes_len(saved_line + change_col,
5829 cursor->col - change_col);
5830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
5832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (State & VREPLACE_FLAG)
5834 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 curwin->w_p_list = save_list;
5836 }
5837
5838 return FALSE;
5839}
5840
5841/*
5842 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005843 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005846ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
5848 int i;
5849
5850 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005851 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005853 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 undisplay_dollar();
5855
5856 /*
5857 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5858 * character under the cursor. Only push a NUL on the replace stack,
5859 * nothing to put back when the NL is deleted.
5860 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005861 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 replace_push(NUL);
5863
5864 /*
5865 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5866 * replacing the next line, so we push all of the characters left on the
5867 * line onto the replace stack. This is not done here though, it is done
5868 * in open_line().
5869 */
5870
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005871 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
5872 * CTRL-O). */
5873 if (virtual_active() && curwin->w_cursor.coladd > 0)
5874 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005875
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 /* NL in reverse insert will always start in the end of
5878 * current line. */
5879 if (revins_on)
5880 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5881#endif
5882
5883 AppendToRedobuff(NL_STR);
5884 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005885 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 old_indent = 0;
5887#ifdef FEAT_CINDENT
5888 can_cindent = TRUE;
5889#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005890#ifdef FEAT_FOLDING
5891 /* When inserting a line the cursor line must never be in a closed fold. */
5892 foldOpenCursor();
5893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005895 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896}
5897
5898#ifdef FEAT_DIGRAPHS
5899/*
5900 * Handle digraph in insert mode.
5901 * Returns character still to be inserted, or NUL when nothing remaining to be
5902 * done.
5903 */
5904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005905ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 int c;
5908 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005909 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
5911 pc_status = PC_STATUS_UNSET;
5912 if (redrawing() && !char_avail())
5913 {
5914 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005915 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
5917 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005918 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919#ifdef FEAT_CMDL_INFO
5920 add_to_showcmd_c(Ctrl_K);
5921#endif
5922 }
5923
5924#ifdef USE_ON_FLY_SCROLL
5925 dont_scroll = TRUE; /* disallow scrolling here */
5926#endif
5927
5928 /* don't map the digraph chars. This also prevents the
5929 * mode message to be deleted when ESC is hit */
5930 ++no_mapping;
5931 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005932 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 --no_mapping;
5934 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005935 if (did_putchar)
5936 /* when the line fits in 'columns' the '?' is at the start of the next
5937 * line and will not be removed by the redraw */
5938 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005939
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 if (IS_SPECIAL(c) || mod_mask) /* special key */
5941 {
5942#ifdef FEAT_CMDL_INFO
5943 clear_showcmd();
5944#endif
5945 insert_special(c, TRUE, FALSE);
5946 return NUL;
5947 }
5948 if (c != ESC)
5949 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005950 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 if (redrawing() && !char_avail())
5952 {
5953 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005954 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
5956 if (char2cells(c) == 1)
5957 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005958 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005960 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 }
5962#ifdef FEAT_CMDL_INFO
5963 add_to_showcmd_c(c);
5964#endif
5965 }
5966 ++no_mapping;
5967 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005968 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 --no_mapping;
5970 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005971 if (did_putchar)
5972 /* when the line fits in 'columns' the '?' is at the start of the
5973 * next line and will not be removed by a redraw */
5974 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 if (cc != ESC)
5976 {
5977 AppendToRedobuff((char_u *)CTRL_V_STR);
5978 c = getdigraph(c, cc, TRUE);
5979#ifdef FEAT_CMDL_INFO
5980 clear_showcmd();
5981#endif
5982 return c;
5983 }
5984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985#ifdef FEAT_CMDL_INFO
5986 clear_showcmd();
5987#endif
5988 return NUL;
5989}
5990#endif
5991
5992/*
5993 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5994 * Returns the char to be inserted, or NUL if none found.
5995 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005996 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005997ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
5999 int c;
6000 int temp;
6001 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006002 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
6004 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6005 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02006006 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 return NUL;
6008 }
6009
6010 /* try to advance to the cursor column */
6011 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006012 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 prev_ptr = ptr;
6014 validate_virtcol();
6015 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
6016 {
6017 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006018 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 }
6020 if ((colnr_T)temp > curwin->w_virtcol)
6021 ptr = prev_ptr;
6022
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006025 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 return c;
6027}
6028
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006029/*
6030 * CTRL-Y or CTRL-E typed in Insert mode.
6031 */
6032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006034{
6035 int c = tc;
6036
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006037 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006038 {
6039 if (c == Ctrl_Y)
6040 scrolldown_clamp();
6041 else
6042 scrollup_clamp();
6043 redraw_later(VALID);
6044 }
6045 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006046 {
6047 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
6048 if (c != NUL)
6049 {
6050 long tw_save;
6051
6052 /* The character must be taken literally, insert like it
6053 * was typed after a CTRL-V, and pretend 'textwidth'
6054 * wasn't set. Digits, 'o' and 'x' are special after a
6055 * CTRL-V, don't use it for these. */
6056 if (c < 256 && !isalnum(c))
6057 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
6058 tw_save = curbuf->b_p_tw;
6059 curbuf->b_p_tw = -1;
6060 insert_special(c, TRUE, FALSE);
6061 curbuf->b_p_tw = tw_save;
6062#ifdef FEAT_RIGHTLEFT
6063 revins_chars++;
6064 revins_legal++;
6065#endif
6066 c = Ctrl_V; /* pretend CTRL-V is last character */
6067 auto_format(FALSE, TRUE);
6068 }
6069 }
6070 return c;
6071}
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#ifdef FEAT_SMARTINDENT
6074/*
6075 * Try to do some very smart auto-indenting.
6076 * Used when inserting a "normal" character.
6077 */
6078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006079ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
6081 pos_T *pos, old_pos;
6082 char_u *ptr;
6083 int i;
6084 int temp;
6085
6086 /*
6087 * do some very smart indenting when entering '{' or '}'
6088 */
6089 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
6090 {
6091 /*
6092 * for '}' set indent equal to indent of line containing matching '{'
6093 */
6094 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
6095 {
6096 old_pos = curwin->w_cursor;
6097 /*
6098 * If the matching '{' has a ')' immediately before it (ignoring
6099 * white-space), then line up with the start of the line
6100 * containing the matching '(' if there is one. This handles the
6101 * case where an "if (..\n..) {" statement continues over multiple
6102 * lines -- webb
6103 */
6104 ptr = ml_get(pos->lnum);
6105 i = pos->col;
6106 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006107 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 ;
6109 curwin->w_cursor.lnum = pos->lnum;
6110 curwin->w_cursor.col = i;
6111 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
6112 curwin->w_cursor = *pos;
6113 i = get_indent();
6114 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006116 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 (void)set_indent(i, SIN_CHANGED);
6119 }
6120 else if (curwin->w_cursor.col > 0)
6121 {
6122 /*
6123 * when inserting '{' after "O" reduce indent, but not
6124 * more than indent of previous line
6125 */
6126 temp = TRUE;
6127 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
6128 {
6129 old_pos = curwin->w_cursor;
6130 i = get_indent();
6131 while (curwin->w_cursor.lnum > 1)
6132 {
6133 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
6134
6135 /* ignore empty lines and lines starting with '#'. */
6136 if (*ptr != '#' && *ptr != NUL)
6137 break;
6138 }
6139 if (get_indent() >= i)
6140 temp = FALSE;
6141 curwin->w_cursor = old_pos;
6142 }
6143 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006144 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 }
6146 }
6147
6148 /*
6149 * set indent of '#' always to 0
6150 */
6151 if (curwin->w_cursor.col > 0 && can_si && c == '#')
6152 {
6153 /* remember current indent for next line */
6154 old_indent = get_indent();
6155 (void)set_indent(0, SIN_CHANGED);
6156 }
6157
6158 /* Adjust ai_col, the char at this position can be deleted. */
6159 if (ai_col > curwin->w_cursor.col)
6160 ai_col = curwin->w_cursor.col;
6161}
6162#endif
6163
6164/*
6165 * Get the value that w_virtcol would have when 'list' is off.
6166 * Unless 'cpo' contains the 'L' flag.
6167 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01006168 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006169get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
Bram Moolenaarf9514162018-11-22 03:08:29 +01006171 // check validity of cursor in current buffer
6172 if (curwin->w_buffer == NULL
6173 || curwin->w_buffer->b_ml.ml_mfp == NULL
6174 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
6175 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
6177 return getvcol_nolist(&curwin->w_cursor);
6178 validate_virtcol();
6179 return curwin->w_virtcol;
6180}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006181
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006182#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006183/*
6184 * Handle the InsertCharPre autocommand.
6185 * "c" is the character that was typed.
6186 * Return a pointer to allocated memory with the replacement string.
6187 * Return NULL to continue inserting "c".
6188 */
6189 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006190do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006191{
Bram Moolenaar704984a2012-06-01 14:57:51 +02006192 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02006193 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01006194 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006195
6196 /* Return quickly when there is nothing to do. */
6197 if (!has_insertcharpre())
6198 return NULL;
6199
Bram Moolenaar704984a2012-06-01 14:57:51 +02006200 if (has_mbyte)
6201 buf[(*mb_char2bytes)(c, buf)] = NUL;
6202 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02006203 {
6204 buf[0] = c;
6205 buf[1] = NUL;
6206 }
6207
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006208 /* Lock the text to avoid weird things from happening. */
6209 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +02006210 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006211
Bram Moolenaar704984a2012-06-01 14:57:51 +02006212 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006213 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02006214 {
6215 /* Get the value of v:char. It may be empty or more than one
6216 * character. Only use it when changed, otherwise continue with the
6217 * original character to avoid breaking autoindent. */
6218 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
6219 res = vim_strsave(get_vim_var_str(VV_CHAR));
6220 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006221
6222 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
6223 --textlock;
6224
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01006225 // Restore the State, it may have been changed.
6226 State = save_State;
6227
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006228 return res;
6229}
6230#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006231
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006232#if defined(FEAT_CINDENT) || defined(PROTO)
6233 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02006234get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006235{
6236 return can_cindent;
6237}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02006238
6239 void
6240set_can_cindent(int val)
6241{
6242 can_cindent = val;
6243}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006244#endif
6245
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006246/*
6247 * Trigger "event" and take care of fixing undo.
6248 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006249 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006250ins_apply_autocmds(event_T event)
6251{
6252 varnumber_T tick = CHANGEDTICK(curbuf);
6253 int r;
6254
6255 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
6256
6257 // If u_savesub() was called then we are not prepared to start
6258 // a new line. Call u_save() with no contents to fix that.
6259 if (tick != CHANGEDTICK(curbuf))
6260 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
6261
6262 return r;
6263}