blob: cc41d49cc741db7f629bc0bc2b4f83f7118775aa [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 undisplay_dollar(void);
31static void insert_special(int, int, int);
32static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
33static void check_auto_format(int);
34static void redo_literal(int c);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +000036#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void check_spell_redraw(void);
Bram Moolenaar217ad922005-03-20 22:37:15 +000038#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
40static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010041static void replace_join(int off);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010043static void replace_flush(void);
44static void replace_do_bs(int limit_col);
45static int del_char_after_col(int limit_col);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void ins_reg(void);
47static void ins_ctrl_g(void);
48static void ins_ctrl_hat(void);
49static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010051static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000052#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010053static int ins_start_select(int c);
54static void ins_insert(int replaceState);
55static void ins_ctrl_o(void);
56static void ins_shift(int c, int lastc);
57static void ins_del(void);
58static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010060static void ins_mouse(int c);
61static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +000063#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000065#endif
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010066static void ins_left(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010067static void ins_home(int c);
68static void ins_end(int c);
69static void ins_s_left(void);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010070static void ins_right(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void ins_s_right(void);
72static void ins_up(int startcol);
73static void ins_pageup(void);
74static void ins_down(int startcol);
75static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010077static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static int ins_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010087#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +010089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91static colnr_T Insstart_textlen; /* length of line when insert started */
92static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +010093static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95static char_u *last_insert = NULL; /* the text of the previous insert,
96 K_SPECIAL and CSI are escaped */
97static int last_insert_skip; /* nr of chars in front of previous insert */
98static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +000099static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
101#ifdef FEAT_CINDENT
102static int can_cindent; /* may do cindenting on this line */
103#endif
104
105static int old_indent = 0; /* for ^^D command in insert mode */
106
107#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000108static int revins_on; /* reverse insert mode on */
109static int revins_chars; /* how much to skip after edit */
110static int revins_legal; /* was the last char 'legal'? */
111static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#endif
113
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114static int ins_need_undo; /* call u_save() before inserting a
115 char. Set when edit() is called.
116 after that arrow_used is used. */
117
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100118static int did_add_space = FALSE; // auto_format() added an extra space
119 // under the cursor
120static int dont_sync_undo = FALSE; // CTRL-G U prevents syncing undo for
121 // the next left/right cursor key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
124 * edit(): Start inserting text.
125 *
126 * "cmdchar" can be:
127 * 'i' normal insert command
128 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100129 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 * 'R' replace command
131 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
132 * but still only one <CR> is inserted. The <Esc> is not used for redo.
133 * 'g' "gI" command.
134 * 'V' "gR" command for Virtual Replace mode.
135 * 'v' "gr" command for single character Virtual Replace mode.
136 *
137 * This function is not called recursively. For CTRL-O commands, it returns
138 * and lets the caller handle the Normal-mode command.
139 *
140 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
141 */
142 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100143edit(
144 int cmdchar,
145 int startln, /* if set, insert at start of line */
146 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147{
148 int c = 0;
149 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100150 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000151 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 int i;
154 int did_backspace = TRUE; /* previous char was backspace */
155#ifdef FEAT_CINDENT
156 int line_is_white = FALSE; /* line is empty before insert */
157#endif
158 linenr_T old_topline = 0; /* topline before insertion */
159#ifdef FEAT_DIFF
160 int old_topfill = -1;
161#endif
162 int inserted_space = FALSE; /* just inserted a space */
163 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000164 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200165#ifdef FEAT_JOB_CHANNEL
166 int cmdchar_todo = cmdchar;
167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000169 /* Remember whether editing was restarted after CTRL-O. */
170 did_restart_edit = restart_edit;
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 /* sleep before redrawing, needed for "CTRL-O :" that results in an
173 * error message */
174 check_for_delay(TRUE);
175
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100176 /* set Insstart_orig to Insstart */
177 update_Insstart_orig = TRUE;
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#ifdef HAVE_SANDBOX
180 /* Don't allow inserting in the sandbox. */
181 if (sandbox != 0)
182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100183 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 return FALSE;
185 }
186#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000187 /* Don't allow changes in the buffer while editing the cmdline. The
188 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000189 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000190 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100191 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000192 return FALSE;
193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000195 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100196 if (ins_compl_active() || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100198 emsg(_(e_secure));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000199 return FALSE;
200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 ins_compl_clear(); /* clear stuff for CTRL-X mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaar843ee412004-06-30 16:16:41 +0000203 /*
204 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
205 */
206 if (cmdchar != 'r' && cmdchar != 'v')
207 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100208 pos_T save_cursor = curwin->w_cursor;
209
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100210#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000211 if (cmdchar == 'R')
212 ptr = (char_u *)"r";
213 else if (cmdchar == 'V')
214 ptr = (char_u *)"v";
215 else
216 ptr = (char_u *)"i";
217 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200218 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100219#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200220 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100221
Bram Moolenaar097c9922013-05-19 21:15:15 +0200222 /* Make sure the cursor didn't move. Do call check_cursor_col() in
223 * case the text was modified. Since Insert mode was not started yet
224 * a call to check_cursor_col() may move the cursor, especially with
225 * the "A" command, thus set State to avoid that. Also check that the
226 * line number is still valid (lines may have been deleted).
227 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100228 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100229#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200230 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100231#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200232 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100233 {
234 int save_state = State;
235
236 curwin->w_cursor = save_cursor;
237 State = INSERT;
238 check_cursor_col();
239 State = save_state;
240 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000241 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000242
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200243#ifdef FEAT_CONCEAL
244 /* Check if the cursor line needs redrawing before changing State. If
245 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200246 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200247#endif
248
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_MOUSE
250 /*
251 * When doing a paste with the middle mouse button, Insstart is set to
252 * where the paste started.
253 */
254 if (where_paste_started.lnum != 0)
255 Insstart = where_paste_started;
256 else
257#endif
258 {
259 Insstart = curwin->w_cursor;
260 if (startln)
261 Insstart.col = 0;
262 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000263 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 Insstart_blank_vcol = MAXCOL;
265 if (!did_ai)
266 ai_col = 0;
267
268 if (cmdchar != NUL && restart_edit == 0)
269 {
270 ResetRedobuff();
271 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (cmdchar == 'V' || cmdchar == 'v')
273 {
274 /* "gR" or "gr" command */
275 AppendCharToRedobuff('g');
276 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
277 }
278 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100280 if (cmdchar == K_PS)
281 AppendCharToRedobuff('a');
282 else
283 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 if (cmdchar == 'g') /* "gI" command */
285 AppendCharToRedobuff('I');
286 else if (cmdchar == 'r') /* "r<CR>" command */
287 count = 1; /* insert only one <CR> */
288 }
289 }
290
291 if (cmdchar == 'R')
292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 State = REPLACE;
294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 else if (cmdchar == 'V' || cmdchar == 'v')
296 {
297 State = VREPLACE;
298 replaceState = VREPLACE;
299 orig_line_count = curbuf->b_ml.ml_line_count;
300 vr_lines_changed = 1;
301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 else
303 State = INSERT;
304
305 stop_insert_mode = FALSE;
306
307 /*
308 * Need to recompute the cursor position, it might move when the cursor is
309 * on a TAB or special character.
310 */
311 curs_columns(TRUE);
312
313 /*
314 * Enable langmap or IME, indicated by 'iminsert'.
315 * Note that IME may enabled/disabled without us noticing here, thus the
316 * 'iminsert' value may not reflect what is actually used. It is updated
317 * when hitting <Esc>.
318 */
319 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
320 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100321#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
323#endif
324
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#ifdef FEAT_MOUSE
326 setmouse();
327#endif
328#ifdef FEAT_CMDL_INFO
329 clear_showcmd();
330#endif
331#ifdef FEAT_RIGHTLEFT
332 /* there is no reverse replace mode */
333 revins_on = (State == INSERT && p_ri);
334 if (revins_on)
335 undisplay_dollar();
336 revins_chars = 0;
337 revins_legal = 0;
338 revins_scol = -1;
339#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100340 if (!p_ek)
341 /* Disable bracketed paste mode, we won't recognize the escape
342 * sequences. */
343 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
345 /*
346 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100347 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
348 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 */
350 if (restart_edit != 0 && stuff_empty())
351 {
352#ifdef FEAT_MOUSE
353 /*
354 * After a paste we consider text typed to be part of the insert for
355 * the pasted text. You can backspace over the pasted text too.
356 */
357 if (where_paste_started.lnum)
358 arrow_used = FALSE;
359 else
360#endif
361 arrow_used = TRUE;
362 restart_edit = 0;
363
364 /*
365 * If the cursor was after the end-of-line before the CTRL-O and it is
366 * now at the end-of-line, put it after the end-of-line (this is not
367 * correct in very rare cases).
368 * Also do this if curswant is greater than the current virtual
369 * column. Eg after "^O$" or "^O80|".
370 */
371 validate_virtcol();
372 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000373 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 || curwin->w_curswant > curwin->w_virtcol)
375 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
376 {
377 if (ptr[1] == NUL)
378 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 else if (has_mbyte)
380 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000381 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 if (ptr[i] == NUL)
383 curwin->w_cursor.col += i;
384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000386 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 else
389 arrow_used = FALSE;
390
391 /* we are in insert mode now, don't need to start it anymore */
392 need_start_insertmode = FALSE;
393
394 /* Need to save the line for undo before inserting the first char. */
395 ins_need_undo = TRUE;
396
397#ifdef FEAT_MOUSE
398 where_paste_started.lnum = 0;
399#endif
400#ifdef FEAT_CINDENT
401 can_cindent = TRUE;
402#endif
403#ifdef FEAT_FOLDING
404 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
405 * restarting. */
406 if (!p_im && did_restart_edit == 0)
407 foldOpenCursor();
408#endif
409
410 /*
411 * If 'showmode' is set, show the current (insert/replace/..) mode.
412 * A warning message for changing a readonly file is given here, before
413 * actually changing anything. It's put after the mode, if any.
414 */
415 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000416 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 i = showmode();
418
419 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000420 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
422#ifdef CURSOR_SHAPE
423 ui_cursor_shape(); /* may show different cursor shape */
424#endif
425#ifdef FEAT_DIGRAPHS
426 do_digraph(-1); /* clear digraphs */
427#endif
428
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000429 /*
430 * Get the current length of the redo buffer, those characters have to be
431 * skipped if we want to get to the inserted characters.
432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 ptr = get_inserted();
434 if (ptr == NULL)
435 new_insert_skip = 0;
436 else
437 {
438 new_insert_skip = (int)STRLEN(ptr);
439 vim_free(ptr);
440 }
441
442 old_indent = 0;
443
444 /*
445 * Main loop in Insert mode: repeat until Insert mode is left.
446 */
447 for (;;)
448 {
449#ifdef FEAT_RIGHTLEFT
450 if (!revins_legal)
451 revins_scol = -1; /* reset on illegal motions */
452 else
453 revins_legal = 0;
454#endif
455 if (arrow_used) /* don't repeat insert when arrow key used */
456 count = 0;
457
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100458 if (update_Insstart_orig)
459 Insstart_orig = Insstart;
460
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200461 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {
463 /* ":stopinsert" used or 'insertmode' reset */
464 count = 0;
465 goto doESCkey;
466 }
467
468 /* set curwin->w_curswant for next K_DOWN or K_UP */
469 if (!arrow_used)
470 curwin->w_set_curswant = TRUE;
471
472 /* If there is no typeahead may check for timestamps (e.g., for when a
473 * menu invoked a shell command). */
474 if (stuff_empty())
475 {
476 did_check_timestamps = FALSE;
477 if (need_check_timestamps)
478 check_timestamps(FALSE);
479 }
480
481 /*
482 * When emsg() was called msg_scroll will have been set.
483 */
484 msg_scroll = FALSE;
485
486#ifdef FEAT_GUI
487 /* When 'mousefocus' is set a mouse movement may have taken us to
488 * another window. "need_mouse_correct" may then be set because of an
489 * autocommand. */
490 if (need_mouse_correct)
491 gui_mouse_correct();
492#endif
493
494#ifdef FEAT_FOLDING
495 /* Open fold at the cursor line, according to 'foldopen'. */
496 if (fdo_flags & FDO_INSERT)
497 foldOpenCursor();
498 /* Close folds where the cursor isn't, according to 'foldclose' */
499 if (!char_avail())
500 foldCheckClose();
501#endif
502
Bram Moolenaarf2732452018-06-03 14:47:35 +0200503#ifdef FEAT_JOB_CHANNEL
504 if (bt_prompt(curbuf))
505 {
506 init_prompt(cmdchar_todo);
507 cmdchar_todo = NUL;
508 }
509#endif
510
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 /*
512 * If we inserted a character at the last position of the last line in
513 * the window, scroll the window one line up. This avoids an extra
514 * redraw.
515 * This is detected when the cursor column is smaller after inserting
516 * something.
517 * Don't do this when the topline changed already, it has
518 * already been adjusted (by insertchar() calling open_line())).
519 */
520 if (curbuf->b_mod_set
521 && curwin->w_p_wrap
522 && !did_backspace
523 && curwin->w_topline == old_topline
524#ifdef FEAT_DIFF
525 && curwin->w_topfill == old_topfill
526#endif
527 )
528 {
529 mincol = curwin->w_wcol;
530 validate_cursor_col();
531
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200532 if (
533#ifdef FEAT_VARTABS
534 (int)curwin->w_wcol < mincol - tabstop_at(
535 get_nolist_virtcol(), curbuf->b_p_ts,
536 curbuf->b_p_vts_array)
537#else
538 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100541 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 && (curwin->w_cursor.lnum != curwin->w_topline
543#ifdef FEAT_DIFF
544 || curwin->w_topfill > 0
545#endif
546 ))
547 {
548#ifdef FEAT_DIFF
549 if (curwin->w_topfill > 0)
550 --curwin->w_topfill;
551 else
552#endif
553#ifdef FEAT_FOLDING
554 if (hasFolding(curwin->w_topline, NULL, &old_topline))
555 set_topline(curwin, old_topline + 1);
556 else
557#endif
558 set_topline(curwin, curwin->w_topline + 1);
559 }
560 }
561
562 /* May need to adjust w_topline to show the cursor. */
563 update_topline();
564
565 did_backspace = FALSE;
566
567 validate_cursor(); /* may set must_redraw */
568
569 /*
570 * Redraw the display when no characters are waiting.
571 * Also shows mode, ruler and positions cursor.
572 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000573 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (curwin->w_p_scb)
576 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
Bram Moolenaar860cae12010-06-05 23:22:07 +0200578 if (curwin->w_p_crb)
579 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 update_curswant();
581 old_topline = curwin->w_topline;
582#ifdef FEAT_DIFF
583 old_topfill = curwin->w_topfill;
584#endif
585
586#ifdef USE_ON_FLY_SCROLL
587 dont_scroll = FALSE; /* allow scrolling here */
588#endif
589
590 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100591 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100593 if (c != K_CURSORHOLD)
594 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200595
596 /* After using CTRL-G U the next cursor key will not break undo. */
597 if (dont_sync_undo == MAYBE)
598 dont_sync_undo = TRUE;
599 else
600 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100601 if (cmdchar == K_PS)
602 /* Got here from normal mode when bracketed paste started. */
603 c = K_PS;
604 else
605 do
606 {
607 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200608
609 if (stop_insert_mode)
610 {
611 // Insert mode ended, possibly from a callback.
612 count = 0;
613 nomove = TRUE;
614 goto doESCkey;
615 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100616 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000618 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
619 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#ifdef FEAT_RIGHTLEFT
622 if (p_hkmap && KeyTyped)
623 c = hkmap(c); /* Hebrew mode mapping */
624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000626 /*
627 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000628 * and the cursor is still in the completed word. Only when there is
629 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000630 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100631 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000632 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100633 && curwin->w_cursor.col >= ins_compl_col()
634 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000635 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000636 /* BS: Delete one character from "compl_leader". */
637 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100638 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000639 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000640 continue;
641
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000642 /* When no match was selected or it was edited. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100643 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000644 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000645 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000646 * "compl_leader". Except when at the original match and
647 * there is nothing to add, CTRL-L works like CTRL-P then. */
648 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100649 && (!ctrl_x_mode_line_or_eval()
650 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000651 {
652 ins_compl_addfrommatch();
653 continue;
654 }
655
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000656 /* A non-white character that fits in with the current
657 * completion: Add to "compl_leader". */
658 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000659 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100660#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100661 /* Trigger InsertCharPre. */
662 char_u *str = do_insert_char_pre(c);
663 char_u *p;
664
665 if (str != NULL)
666 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100667 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100668 ins_compl_addleader(PTR2CHAR(p));
669 vim_free(str);
670 }
671 else
672#endif
673 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000674 continue;
675 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000676
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000677 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100678 * ins_compl_enter_selects() is set the Enter key does the
679 * same. */
680 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200681 && (c == CAR || c == K_KENTER || c == NL)))
682 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000683 {
684 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200685 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000686 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000687 }
688 }
689
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
691 * it does fix up the text when finishing completion. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100692 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000693 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000694 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
Bram Moolenaar488c6512005-08-11 20:09:58 +0000696 /* CTRL-\ CTRL-N goes to Normal mode,
697 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
698 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (c == Ctrl_BSL)
700 {
701 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000702 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 ++no_mapping;
704 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000705 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 --no_mapping;
707 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000708 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000710 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 vungetc(c);
712 c = Ctrl_BSL;
713 }
714 else if (c == Ctrl_G && p_im)
715 continue;
716 else
717 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000718 if (c == Ctrl_O)
719 {
720 ins_ctrl_o();
721 ins_at_eol = FALSE; /* cursor keeps its column */
722 nomove = TRUE;
723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 count = 0;
725 goto doESCkey;
726 }
727 }
728
729#ifdef FEAT_DIGRAPHS
730 c = do_digraph(c);
731#endif
732
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100733 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (c == Ctrl_V || c == Ctrl_Q)
736 {
737 ins_ctrl_v();
738 c = Ctrl_V; /* pretend CTRL-V is last typed character */
739 continue;
740 }
741
742#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200743 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
745 /* A key name preceded by a bang means this key is not to be
746 * inserted. Skip ahead to the re-indenting below.
747 * A key name preceded by a star means that indenting has to be
748 * done before inserting the key. */
749 line_is_white = inindent(0);
750 if (in_cinkeys(c, '!', line_is_white))
751 goto force_cindent;
752 if (can_cindent && in_cinkeys(c, '*', line_is_white)
753 && stop_arrow() == OK)
754 do_c_expr_indent();
755 }
756#endif
757
758#ifdef FEAT_RIGHTLEFT
759 if (curwin->w_p_rl)
760 switch (c)
761 {
762 case K_LEFT: c = K_RIGHT; break;
763 case K_S_LEFT: c = K_S_RIGHT; break;
764 case K_C_LEFT: c = K_C_RIGHT; break;
765 case K_RIGHT: c = K_LEFT; break;
766 case K_S_RIGHT: c = K_S_LEFT; break;
767 case K_C_RIGHT: c = K_C_LEFT; break;
768 }
769#endif
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 /*
772 * If 'keymodel' contains "startsel", may start selection. If it
773 * does, a CTRL-O and c will be stuffed, we need to get these
774 * characters.
775 */
776 if (ins_start_select(c))
777 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779 /*
780 * The big switch to handle a character in insert mode.
781 */
782 switch (c)
783 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000784 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (echeck_abbr(ESC + ABBR_OFF))
786 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200787 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000789 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790#ifdef FEAT_CMDWIN
791 if (c == Ctrl_C && cmdwin_type != 0)
792 {
793 /* Close the cmdline window. */
794 cmdwin_result = K_IGNORE;
795 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000796 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 goto doESCkey;
798 }
799#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200800#ifdef FEAT_JOB_CHANNEL
801 if (c == Ctrl_C && bt_prompt(curbuf))
802 {
803 if (invoke_prompt_interrupt())
804 {
805 if (!bt_prompt(curbuf))
806 // buffer changed to a non-prompt buffer, get out of
807 // Insert mode
808 goto doESCkey;
809 break;
810 }
811 }
812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814#ifdef UNIX
815do_intr:
816#endif
817 /* when 'insertmode' set, and not halfway a mapping, don't leave
818 * Insert mode */
819 if (goto_im())
820 {
821 if (got_int)
822 {
823 (void)vgetc(); /* flush all buffers */
824 got_int = FALSE;
825 }
826 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200827 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 break;
829 }
830doESCkey:
831 /*
832 * This is the ONLY return from edit()!
833 */
834 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
835 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000836 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 o_lnum = curwin->w_cursor.lnum;
838
Bram Moolenaar488c6512005-08-11 20:09:58 +0000839 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000840 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100841 // When CTRL-C was typed got_int will be set, with the result
842 // that the autocommands won't be executed. When mapped got_int
843 // is not set, but let's keep the behavior the same.
844 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200845 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000846 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 continue;
850
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000851 case Ctrl_Z: /* suspend when 'insertmode' set */
852 if (!p_im)
853 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100854 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100855#ifdef CURSOR_SHAPE
856 ui_cursor_shape(); /* may need to update cursor shape */
857#endif
858 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000859
860 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000861#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100862 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000863 goto docomplete;
864#endif
865 if (echeck_abbr(Ctrl_O + ABBR_OFF))
866 break;
867 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000868
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000869 /* don't move the cursor left when 'virtualedit' has "onemore". */
870 if (ve_flags & VE_ONEMORE)
871 {
872 ins_at_eol = FALSE;
873 nomove = TRUE;
874 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000875 count = 0;
876 goto doESCkey;
877
Bram Moolenaar572cb562005-08-05 21:35:02 +0000878 case K_INS: /* toggle insert/replace mode */
879 case K_KINS:
880 ins_insert(replaceState);
881 break;
882
883 case K_SELECT: /* end of Select mode mapping - ignore */
884 break;
885
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000886 case K_HELP: /* Help key works like <ESC> <Help> */
887 case K_F1:
888 case K_XF1:
889 stuffcharReadbuff(K_HELP);
890 if (p_im)
891 need_start_insertmode = TRUE;
892 goto doESCkey;
893
894#ifdef FEAT_NETBEANS_INTG
895 case K_F21: /* NetBeans command */
896 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000897 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000898 --no_mapping;
899 netbeans_keycommand(i);
900 break;
901#endif
902
903 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 case NUL:
905 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000906 /* For ^@ the trailing ESC will end the insert, unless there is an
907 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
909 && c != Ctrl_A && !p_im)
910 goto doESCkey; /* quit insert mode */
911 inserted_space = FALSE;
912 break;
913
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000914 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 ins_reg();
916 auto_format(FALSE, TRUE);
917 inserted_space = FALSE;
918 break;
919
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000920 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 ins_ctrl_g();
922 break;
923
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000924 case Ctrl_HAT: /* switch input mode and/or langmap */
925 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 break;
927
928#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000929 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (!p_ari)
931 goto normalchar;
932 ins_ctrl_();
933 break;
934#endif
935
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000936 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200937#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100938 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 goto docomplete;
940#endif
941 /* FALLTHROUGH */
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100944 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000946 if (has_compl_option(FALSE))
947 goto docomplete;
948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200950
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 ins_shift(c, lastc);
952 auto_format(FALSE, TRUE);
953 inserted_space = FALSE;
954 break;
955
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000956 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 case K_KDEL:
958 ins_del();
959 auto_format(FALSE, TRUE);
960 break;
961
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000962 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 case Ctrl_H:
964 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
965 auto_format(FALSE, TRUE);
966 break;
967
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000968 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200969#ifdef FEAT_JOB_CHANNEL
970 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
971 {
972 // In a prompt window CTRL-W is used for window commands.
973 // Use Shift-CTRL-W to delete a word.
974 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200975 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200976 nomove = TRUE;
977 count = 0;
978 goto doESCkey;
979 }
980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
982 auto_format(FALSE, TRUE);
983 break;
984
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000985 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000986# ifdef FEAT_COMPL_FUNC
987 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100988 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000989 goto docomplete;
990# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
992 auto_format(FALSE, TRUE);
993 inserted_space = FALSE;
994 break;
995
996#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000997 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 case K_LEFTMOUSE_NM:
999 case K_LEFTDRAG:
1000 case K_LEFTRELEASE:
1001 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001002 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 case K_MIDDLEMOUSE:
1004 case K_MIDDLEDRAG:
1005 case K_MIDDLERELEASE:
1006 case K_RIGHTMOUSE:
1007 case K_RIGHTDRAG:
1008 case K_RIGHTRELEASE:
1009 case K_X1MOUSE:
1010 case K_X1DRAG:
1011 case K_X1RELEASE:
1012 case K_X2MOUSE:
1013 case K_X2DRAG:
1014 case K_X2RELEASE:
1015 ins_mouse(c);
1016 break;
1017
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001019 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 break;
1021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001023 ins_mousescroll(MSCR_UP);
1024 break;
1025
1026 case K_MOUSELEFT: /* Scroll wheel left */
1027 ins_mousescroll(MSCR_LEFT);
1028 break;
1029
1030 case K_MOUSERIGHT: /* Scroll wheel right */
1031 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 break;
1033#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001034 case K_PS:
1035 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1036 if (cmdchar == K_PS)
1037 /* invoked from normal mode, bail out */
1038 goto doESCkey;
1039 break;
1040 case K_PE:
1041 /* Got K_PE without K_PS, ignore. */
1042 break;
1043
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001044#ifdef FEAT_GUI_TABLINE
1045 case K_TABLINE:
1046 case K_TABMENU:
1047 ins_tabline(c);
1048 break;
1049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 break;
1053
Bram Moolenaar754b5602006-02-09 23:53:20 +00001054 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001055 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001056 did_cursorhold = TRUE;
1057 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001058
Bram Moolenaar4f974752019-02-17 17:44:42 +01001059#ifdef FEAT_GUI_MSWIN
1060 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1061 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001062 case K_F4:
1063 if (mod_mask != MOD_MASK_ALT)
1064 goto normalchar;
1065 break;
1066#endif
1067
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#ifdef FEAT_GUI
1069 case K_VER_SCROLLBAR:
1070 ins_scroll();
1071 break;
1072
1073 case K_HOR_SCROLLBAR:
1074 ins_horscroll();
1075 break;
1076#endif
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 case K_S_HOME:
1081 case K_C_HOME:
1082 ins_home(c);
1083 break;
1084
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001085 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 case K_S_END:
1088 case K_C_END:
1089 ins_end(c);
1090 break;
1091
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001093 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1094 ins_s_left();
1095 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001096 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 break;
1098
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 case K_C_LEFT:
1101 ins_s_left();
1102 break;
1103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001105 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1106 ins_s_right();
1107 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001108 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 break;
1110
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001111 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 case K_C_RIGHT:
1113 ins_s_right();
1114 break;
1115
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001117 if (pum_visible())
1118 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001119 if (mod_mask & MOD_MASK_SHIFT)
1120 ins_pageup();
1121 else
1122 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 break;
1124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001125 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 case K_PAGEUP:
1127 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001128 if (pum_visible())
1129 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 ins_pageup();
1131 break;
1132
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001134 if (pum_visible())
1135 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001136 if (mod_mask & MOD_MASK_SHIFT)
1137 ins_pagedown();
1138 else
1139 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 break;
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 case K_PAGEDOWN:
1144 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001145 if (pum_visible())
1146 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 ins_pagedown();
1148 break;
1149
1150#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 ins_drop();
1153 break;
1154#endif
1155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 c = TAB;
1158 /* FALLTHROUGH */
1159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001161#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001162 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 goto docomplete;
1164#endif
1165 inserted_space = FALSE;
1166 if (ins_tab())
1167 goto normalchar; /* insert TAB as a normal char */
1168 auto_format(FALSE, TRUE);
1169 break;
1170
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001171 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 c = CAR;
1173 /* FALLTHROUGH */
1174 case CAR:
1175 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001176#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 /* In a quickfix window a <CR> jumps to the error under the
1178 * cursor. */
1179 if (bt_quickfix(curbuf) && c == CAR)
1180 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001181 if (curwin->w_llist_ref == NULL) /* quickfix window */
1182 do_cmdline_cmd((char_u *)".cc");
1183 else /* location list window */
1184 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 break;
1186 }
1187#endif
1188#ifdef FEAT_CMDWIN
1189 if (cmdwin_type != 0)
1190 {
1191 /* Execute the command in the cmdline window. */
1192 cmdwin_result = CAR;
1193 goto doESCkey;
1194 }
1195#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001196#ifdef FEAT_JOB_CHANNEL
1197 if (bt_prompt(curbuf))
1198 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001199 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001200 if (!bt_prompt(curbuf))
1201 // buffer changed to a non-prompt buffer, get out of
1202 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001203 goto doESCkey;
1204 break;
1205 }
1206#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001207 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 goto doESCkey; /* out of memory */
1209 auto_format(FALSE, FALSE);
1210 inserted_space = FALSE;
1211 break;
1212
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001213 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001214 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001216 if (has_compl_option(TRUE))
1217 goto docomplete;
1218 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001220#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 c = ins_digraph();
1222 if (c == NUL)
1223 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001225 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar572cb562005-08-05 21:35:02 +00001227 case Ctrl_X: /* Enter CTRL-X mode */
1228 ins_ctrl_x();
1229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001232 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 goto normalchar;
1234 goto docomplete;
1235
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001236 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001237 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 goto normalchar;
1239 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001240
1241 case 's': /* Spelling completion after ^X */
1242 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001243 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001244 goto normalchar;
1245 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001248 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
1250 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1251 if (p_im)
1252 {
1253 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1254 break;
1255 goto doESCkey;
1256 }
1257 goto normalchar;
1258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 /* FALLTHROUGH */
1260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001261 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 case Ctrl_N:
1263 /* if 'complete' is empty then plain ^P is no longer special,
1264 * but it is under other ^X modes */
1265 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001266 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001267 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 goto normalchar;
1269
1270docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001271 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001272#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001273 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001274#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001275 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001277#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001278 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001279#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001280 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001283 case Ctrl_Y: /* copy from previous line or scroll down */
1284 case Ctrl_E: /* copy from next line or scroll up */
1285 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 break;
1287
1288 default:
1289#ifdef UNIX
1290 if (c == intr_char) /* special interrupt char */
1291 goto do_intr;
1292#endif
1293
Bram Moolenaare659c952011-05-19 17:25:41 +02001294normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001296 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001298#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001299 if (!p_paste)
1300 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001301 /* Trigger InsertCharPre. */
1302 char_u *str = do_insert_char_pre(c);
1303 char_u *p;
1304
1305 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001306 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001307 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001308 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001309 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001310 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001311 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001312 c = PTR2CHAR(p);
1313 if (c == CAR || c == K_KENTER || c == NL)
1314 ins_eol(c);
1315 else
1316 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001317 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001318 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001319 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001320 vim_free(str);
1321 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001322 }
1323
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001324 /* If the new value is already inserted or an empty string
1325 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001326 if (c == NUL)
1327 break;
1328 }
1329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#ifdef FEAT_SMARTINDENT
1331 /* Try to perform smart-indenting. */
1332 ins_try_si(c);
1333#endif
1334
1335 if (c == ' ')
1336 {
1337 inserted_space = TRUE;
1338#ifdef FEAT_CINDENT
1339 if (inindent(0))
1340 can_cindent = FALSE;
1341#endif
1342 if (Insstart_blank_vcol == MAXCOL
1343 && curwin->w_cursor.lnum == Insstart.lnum)
1344 Insstart_blank_vcol = get_nolist_virtcol();
1345 }
1346
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001347 /* Insert a normal character and check for abbreviations on a
1348 * special character. Let CTRL-] expand abbreviations without
1349 * inserting it. */
1350 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001351 // Add ABBR_OFF for characters above 0x100, this is
1352 // what check_abbr() expects.
1353 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1354 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
1356 insert_special(c, FALSE, FALSE);
1357#ifdef FEAT_RIGHTLEFT
1358 revins_legal++;
1359 revins_chars++;
1360#endif
1361 }
1362
1363 auto_format(FALSE, TRUE);
1364
1365#ifdef FEAT_FOLDING
1366 /* When inserting a character the cursor line must never be in a
1367 * closed fold. */
1368 foldOpenCursor();
1369#endif
1370 break;
1371 } /* end of switch (c) */
1372
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001373 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001374 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001375#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001376 /* but not in CTRL-X mode, a script can't restore the state */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001377 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001378#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001379 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001380 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 /* If the cursor was moved we didn't just insert a space */
1383 if (arrow_used)
1384 inserted_space = FALSE;
1385
1386#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001387 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 {
1389force_cindent:
1390 /*
1391 * Indent now if a key was typed that is in 'cinkeys'.
1392 */
1393 if (in_cinkeys(c, ' ', line_is_white))
1394 {
1395 if (stop_arrow() == OK)
1396 /* re-indent the current line */
1397 do_c_expr_indent();
1398 }
1399 }
1400#endif /* FEAT_CINDENT */
1401
1402 } /* for (;;) */
1403 /* NOTREACHED */
1404}
1405
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001406 int
1407ins_need_undo_get(void)
1408{
1409 return ins_need_undo;
1410}
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412/*
1413 * Redraw for Insert mode.
1414 * This is postponed until getting the next character to make '$' in the 'cpo'
1415 * option work correctly.
1416 * Only redraw when there are no characters available. This speeds up
1417 * inserting sequences of characters (e.g., for CTRL-R).
1418 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001419 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001420ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001422#ifdef FEAT_CONCEAL
1423 linenr_T conceal_old_cursor_line = 0;
1424 linenr_T conceal_new_cursor_line = 0;
1425 int conceal_update_lines = FALSE;
1426#endif
1427
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001428 if (char_avail())
1429 return;
1430
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001431 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1432 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001433 if (ready && (has_cursormovedI()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001434# ifdef FEAT_TEXT_PROP
1435 || popup_visible
1436# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001437# if defined(FEAT_CONCEAL)
1438 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001439# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001440 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001441 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001442 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001443 {
1444# ifdef FEAT_SYN_HL
1445 /* Need to update the screen first, to make sure syntax
1446 * highlighting is correct after making a change (e.g., inserting
1447 * a "(". The autocommand may also require a redraw, so it's done
1448 * again below, unfortunately. */
1449 if (syntax_present(curwin) && must_redraw)
1450 update_screen(0);
1451# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001452 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001453 {
1454 /* Make sure curswant is correct, an autocommand may call
1455 * getcurpos(). */
1456 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001457 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001458 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001459#ifdef FEAT_TEXT_PROP
1460 if (popup_visible)
1461 popup_check_cursor_pos();
1462#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001463# ifdef FEAT_CONCEAL
1464 if (curwin->w_p_cole > 0)
1465 {
1466 conceal_old_cursor_line = last_cursormoved.lnum;
1467 conceal_new_cursor_line = curwin->w_cursor.lnum;
1468 conceal_update_lines = TRUE;
1469 }
1470# endif
1471 last_cursormoved = curwin->w_cursor;
1472 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001473
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001474 /* Trigger TextChangedI if b_changedtick differs. */
1475 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001476 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001477 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001478 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001479 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001480 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001481
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001482 // save and restore curwin and curbuf, in case the autocmd changes them
1483 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001484 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001485 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001486 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001487 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1488 u_save(curwin->w_cursor.lnum,
1489 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001491
Bram Moolenaar5a093432018-02-10 18:15:19 +01001492 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1493 * TextChangedI will need to trigger for backwards compatibility, thus use
1494 * different b_last_changedtick* variables. */
1495 if (ready && has_textchangedP()
1496 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1497 && pum_visible())
1498 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001499 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001500 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001501
1502 // save and restore curwin and curbuf, in case the autocmd changes them
1503 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001504 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001505 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001506 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001507 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1508 u_save(curwin->w_cursor.lnum,
1509 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001510 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001511
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001512#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001513 if ((conceal_update_lines
1514 && (conceal_old_cursor_line != conceal_new_cursor_line
1515 || conceal_cursor_line(curwin)))
1516 || need_cursor_line_redraw)
1517 {
1518 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001519 redrawWinline(curwin, conceal_old_cursor_line);
1520 redrawWinline(curwin, conceal_new_cursor_line == 0
1521 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001522 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001523 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001524 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001525#endif
1526 if (must_redraw)
1527 update_screen(0);
1528 else if (clear_cmdline || redraw_cmdline)
1529 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001530 showruler(FALSE);
1531 setcursor();
1532 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533}
1534
1535/*
1536 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1537 */
1538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001539ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
1541 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001542 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001545 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
1547 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001550 did_putchar = TRUE;
1551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1553
1554#ifdef FEAT_CMDL_INFO
1555 add_to_showcmd_c(Ctrl_V);
1556#endif
1557
1558 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001559 if (did_putchar)
1560 /* when the line fits in 'columns' the '^' is at the start of the next
1561 * line and will not removed by the redraw */
1562 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563#ifdef FEAT_CMDL_INFO
1564 clear_showcmd();
1565#endif
1566 insert_special(c, FALSE, TRUE);
1567#ifdef FEAT_RIGHTLEFT
1568 revins_chars++;
1569 revins_legal++;
1570#endif
1571}
1572
1573/*
1574 * Put a character directly onto the screen. It's not stored in a buffer.
1575 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1576 */
1577static int pc_status;
1578#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1579#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1580#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1581#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583static int pc_attr;
1584static int pc_row;
1585static int pc_col;
1586
1587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001588edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 int attr;
1591
1592 if (ScreenLines != NULL)
1593 {
1594 update_topline(); /* just in case w_topline isn't valid */
1595 validate_cursor();
1596 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001597 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 else
1599 attr = 0;
1600 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001601 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603#ifdef FEAT_RIGHTLEFT
1604 if (curwin->w_p_rl)
1605 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001606 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (has_mbyte)
1608 {
1609 int fix_col = mb_fix_col(pc_col, pc_row);
1610
1611 if (fix_col != pc_col)
1612 {
1613 screen_putchar(' ', pc_row, fix_col, attr);
1614 --curwin->w_wcol;
1615 pc_status = PC_STATUS_RIGHT;
1616 }
1617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 else
1620#endif
1621 {
1622 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (mb_lefthalve(pc_row, pc_col))
1624 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626
1627 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1631 pc_status = PC_STATUS_SET;
1632 }
1633 screen_putchar(c, pc_row, pc_col, attr);
1634 }
1635}
1636
Bram Moolenaarf2732452018-06-03 14:47:35 +02001637#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1638/*
1639 * Return the effective prompt for the current buffer.
1640 */
1641 char_u *
1642prompt_text(void)
1643{
1644 if (curbuf->b_prompt_text == NULL)
1645 return (char_u *)"% ";
1646 return curbuf->b_prompt_text;
1647}
1648
1649/*
1650 * Prepare for prompt mode: Make sure the last line has the prompt text.
1651 * Move the cursor to this line.
1652 */
1653 static void
1654init_prompt(int cmdchar_todo)
1655{
1656 char_u *prompt = prompt_text();
1657 char_u *text;
1658
1659 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1660 text = ml_get_curline();
1661 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1662 {
1663 // prompt is missing, insert it or append a line with it
1664 if (*text == NUL)
1665 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1666 else
1667 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1668 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1669 coladvance((colnr_T)MAXCOL);
1670 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1671 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001672
1673 // Insert always starts after the prompt, allow editing text after it.
1674 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1675 || Insstart_orig.col != (int)STRLEN(prompt))
1676 {
1677 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001678 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001679 Insstart_orig = Insstart;
1680 Insstart_textlen = Insstart.col;
1681 Insstart_blank_vcol = MAXCOL;
1682 arrow_used = FALSE;
1683 }
1684
Bram Moolenaarf2732452018-06-03 14:47:35 +02001685 if (cmdchar_todo == 'A')
1686 coladvance((colnr_T)MAXCOL);
1687 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001688 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001689 /* Make sure the cursor is in a valid position. */
1690 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001691}
1692
1693/*
1694 * Return TRUE if the cursor is in the editable position of the prompt line.
1695 */
1696 int
1697prompt_curpos_editable()
1698{
1699 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1700 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1701}
1702#endif
1703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704/*
1705 * Undo the previous edit_putchar().
1706 */
1707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001708edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 if (pc_status == PC_STATUS_RIGHT)
1713 ++curwin->w_wcol;
1714 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001715 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1718 }
1719}
1720
1721/*
1722 * Called when p_dollar is set: display a '$' at the end of the changed text
1723 * Only works when cursor is in the line that changes.
1724 */
1725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001726display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 colnr_T save_col;
1729
1730 if (!redrawing())
1731 return;
1732
1733 cursor_off();
1734 save_col = curwin->w_cursor.col;
1735 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 if (has_mbyte)
1737 {
1738 char_u *p;
1739
1740 /* If on the last byte of a multi-byte move to the first byte. */
1741 p = ml_get_curline();
1742 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001745 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
1747 edit_putchar('$', FALSE);
1748 dollar_vcol = curwin->w_virtcol;
1749 }
1750 curwin->w_cursor.col = save_col;
1751}
1752
1753/*
1754 * Call this function before moving the cursor from the normal insert position
1755 * in insert mode.
1756 */
1757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001758undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001760 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001762 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001763 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
1765}
1766
1767/*
1768 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1769 * Keep the cursor on the same character.
1770 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1771 * type == INDENT_DEC decrease indent (for CTRL-D)
1772 * type == INDENT_SET set indent to "amount"
1773 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1774 */
1775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776change_indent(
1777 int type,
1778 int amount,
1779 int round,
1780 int replaced, /* replaced character, put on replace stack */
1781 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 int vcol;
1784 int last_vcol;
1785 int insstart_less; /* reduction for Insstart.col */
1786 int new_cursor_col;
1787 int i;
1788 char_u *ptr;
1789 int save_p_list;
1790 int start_col;
1791 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 colnr_T orig_col = 0; /* init for GCC */
1793 char_u *new_line, *orig_line = NULL; /* init for GCC */
1794
1795 /* VREPLACE mode needs to know what the line was like before changing */
1796 if (State & VREPLACE_FLAG)
1797 {
1798 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1799 orig_col = curwin->w_cursor.col;
1800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 /* for the following tricks we don't want list mode */
1803 save_p_list = curwin->w_p_list;
1804 curwin->w_p_list = FALSE;
1805 vc = getvcol_nolist(&curwin->w_cursor);
1806 vcol = vc;
1807
1808 /*
1809 * For Replace mode we need to fix the replace stack later, which is only
1810 * possible when the cursor is in the indent. Remember the number of
1811 * characters before the cursor if it's possible.
1812 */
1813 start_col = curwin->w_cursor.col;
1814
1815 /* determine offset from first non-blank */
1816 new_cursor_col = curwin->w_cursor.col;
1817 beginline(BL_WHITE);
1818 new_cursor_col -= curwin->w_cursor.col;
1819
1820 insstart_less = curwin->w_cursor.col;
1821
1822 /*
1823 * If the cursor is in the indent, compute how many screen columns the
1824 * cursor is to the left of the first non-blank.
1825 */
1826 if (new_cursor_col < 0)
1827 vcol = get_indent() - vcol;
1828
1829 if (new_cursor_col > 0) /* can't fix replace stack */
1830 start_col = -1;
1831
1832 /*
1833 * Set the new indent. The cursor will be put on the first non-blank.
1834 */
1835 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001836 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 else
1838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int save_State = State;
1840
1841 /* Avoid being called recursively. */
1842 if (State & VREPLACE_FLAG)
1843 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001844 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847 insstart_less -= curwin->w_cursor.col;
1848
1849 /*
1850 * Try to put cursor on same character.
1851 * If the cursor is at or after the first non-blank in the line,
1852 * compute the cursor column relative to the column of the first
1853 * non-blank character.
1854 * If we are not in insert mode, leave the cursor on the first non-blank.
1855 * If the cursor is before the first non-blank, position it relative
1856 * to the first non-blank, counted in screen columns.
1857 */
1858 if (new_cursor_col >= 0)
1859 {
1860 /*
1861 * When changing the indent while the cursor is touching it, reset
1862 * Insstart_col to 0.
1863 */
1864 if (new_cursor_col == 0)
1865 insstart_less = MAXCOL;
1866 new_cursor_col += curwin->w_cursor.col;
1867 }
1868 else if (!(State & INSERT))
1869 new_cursor_col = curwin->w_cursor.col;
1870 else
1871 {
1872 /*
1873 * Compute the screen column where the cursor should be.
1874 */
1875 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001876 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 /*
1879 * Advance the cursor until we reach the right screen column.
1880 */
1881 vcol = last_vcol = 0;
1882 new_cursor_col = -1;
1883 ptr = ml_get_curline();
1884 while (vcol <= (int)curwin->w_virtcol)
1885 {
1886 last_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001888 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001891 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893 vcol = last_vcol;
1894
1895 /*
1896 * May need to insert spaces to be able to position the cursor on
1897 * the right screen column.
1898 */
1899 if (vcol != (int)curwin->w_virtcol)
1900 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001901 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001903 ptr = alloc(i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (ptr != NULL)
1905 {
1906 new_cursor_col += i;
1907 ptr[i] = NUL;
1908 while (--i >= 0)
1909 ptr[i] = ' ';
1910 ins_str(ptr);
1911 vim_free(ptr);
1912 }
1913 }
1914
1915 /*
1916 * When changing the indent while the cursor is in it, reset
1917 * Insstart_col to 0.
1918 */
1919 insstart_less = MAXCOL;
1920 }
1921
1922 curwin->w_p_list = save_p_list;
1923
1924 if (new_cursor_col <= 0)
1925 curwin->w_cursor.col = 0;
1926 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001927 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 curwin->w_set_curswant = TRUE;
1929 changed_cline_bef_curs();
1930
1931 /*
1932 * May have to adjust the start of the insert.
1933 */
1934 if (State & INSERT)
1935 {
1936 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1937 {
1938 if ((int)Insstart.col <= insstart_less)
1939 Insstart.col = 0;
1940 else
1941 Insstart.col -= insstart_less;
1942 }
1943 if ((int)ai_col <= insstart_less)
1944 ai_col = 0;
1945 else
1946 ai_col -= insstart_less;
1947 }
1948
1949 /*
1950 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1951 * If the number of characters before the cursor decreased, need to pop a
1952 * few characters from the replace stack.
1953 * If the number of characters before the cursor increased, need to push a
1954 * few NULs onto the replace stack.
1955 */
1956 if (REPLACE_NORMAL(State) && start_col >= 0)
1957 {
1958 while (start_col > (int)curwin->w_cursor.col)
1959 {
1960 replace_join(0); /* remove a NUL from the replace stack */
1961 --start_col;
1962 }
1963 while (start_col < (int)curwin->w_cursor.col || replaced)
1964 {
1965 replace_push(NUL);
1966 if (replaced)
1967 {
1968 replace_push(replaced);
1969 replaced = NUL;
1970 }
1971 ++start_col;
1972 }
1973 }
1974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 /*
1976 * For VREPLACE mode, we also have to fix the replace stack. In this case
1977 * it is always possible because we backspace over the whole line and then
1978 * put it back again the way we wanted it.
1979 */
1980 if (State & VREPLACE_FLAG)
1981 {
1982 /* If orig_line didn't allocate, just return. At least we did the job,
1983 * even if you can't backspace. */
1984 if (orig_line == NULL)
1985 return;
1986
1987 /* Save new line */
1988 new_line = vim_strsave(ml_get_curline());
1989 if (new_line == NULL)
1990 return;
1991
1992 /* We only put back the new line up to the cursor */
1993 new_line[curwin->w_cursor.col] = NUL;
1994
1995 /* Put back original line */
1996 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1997 curwin->w_cursor.col = orig_col;
1998
1999 /* Backspace from cursor to start of line */
2000 backspace_until_column(0);
2001
2002 /* Insert new stuff into line again */
2003 ins_bytes(new_line);
2004
2005 vim_free(new_line);
2006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007}
2008
2009/*
2010 * Truncate the space at the end of a line. This is to be used only in an
2011 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2012 * modes.
2013 */
2014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002015truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 int i;
2018
2019 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002020 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 if (State & REPLACE_FLAG)
2023 replace_join(0); /* remove a NUL from the replace stack */
2024 }
2025 line[i + 1] = NUL;
2026}
2027
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028/*
2029 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2030 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002031 * Will attempt not to go before "col" even when there is a composing
2032 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 */
2034 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002035backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036{
2037 while ((int)curwin->w_cursor.col > col)
2038 {
2039 curwin->w_cursor.col--;
2040 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002041 replace_do_bs(col);
2042 else if (!del_char_after_col(col))
2043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002047/*
2048 * Like del_char(), but make sure not to go before column "limit_col".
2049 * Only matters when there are composing characters.
2050 * Return TRUE when something was deleted.
2051 */
2052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002053del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002054{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002055 if (enc_utf8 && limit_col >= 0)
2056 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002057 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002058
2059 /* Make sure the cursor is at the start of a character, but
2060 * skip forward again when going too far back because of a
2061 * composing character. */
2062 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002063 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002064 {
2065 int l = utf_ptr2len(ml_get_cursor());
2066
2067 if (l == 0) /* end of line */
2068 break;
2069 curwin->w_cursor.col += l;
2070 }
2071 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2072 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002073 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002074 }
2075 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002076 (void)del_char(FALSE);
2077 return TRUE;
2078}
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080/*
2081 * Next character is interpreted literally.
2082 * A one, two or three digit decimal number is interpreted as its byte value.
2083 * If one or two digits are entered, the next character is given to vungetc().
2084 * For Unicode a character > 255 may be returned.
2085 */
2086 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002087get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
2089 int cc;
2090 int nc;
2091 int i;
2092 int hex = FALSE;
2093 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
2096 if (got_int)
2097 return Ctrl_C;
2098
2099#ifdef FEAT_GUI
2100 /*
2101 * In GUI there is no point inserting the internal code for a special key.
2102 * It is more useful to insert the string "<KEY>" instead. This would
2103 * probably be useful in a text window too, but it would not be
2104 * vi-compatible (maybe there should be an option for it?) -- webb
2105 */
2106 if (gui.in_use)
2107 ++allow_keys;
2108#endif
2109#ifdef USE_ON_FLY_SCROLL
2110 dont_scroll = TRUE; /* disallow scrolling here */
2111#endif
2112 ++no_mapping; /* don't map the next key hits */
2113 cc = 0;
2114 i = 0;
2115 for (;;)
2116 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002117 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01002119 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 add_to_showcmd(nc);
2121#endif
2122 if (nc == 'x' || nc == 'X')
2123 hex = TRUE;
2124 else if (nc == 'o' || nc == 'O')
2125 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 else if (nc == 'u' || nc == 'U')
2127 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 else
2129 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002130 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
2132 if (!vim_isxdigit(nc))
2133 break;
2134 cc = cc * 16 + hex2nr(nc);
2135 }
2136 else if (octal)
2137 {
2138 if (nc < '0' || nc > '7')
2139 break;
2140 cc = cc * 8 + nc - '0';
2141 }
2142 else
2143 {
2144 if (!VIM_ISDIGIT(nc))
2145 break;
2146 cc = cc * 10 + nc - '0';
2147 }
2148
2149 ++i;
2150 }
2151
Bram Moolenaar13505972019-01-24 15:04:48 +01002152 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 cc = 255; /* limit range to 0-255 */
2154 nc = 0;
2155
2156 if (hex) /* hex: up to two chars */
2157 {
2158 if (i >= 2)
2159 break;
2160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 else if (unicode) /* Unicode: up to four or eight chars */
2162 {
2163 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
2164 break;
2165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 else if (i >= 3) /* decimal or octal: up to three chars */
2167 break;
2168 }
2169 if (i == 0) /* no number entered */
2170 {
2171 if (nc == K_ZERO) /* NUL is stored as NL */
2172 {
2173 cc = '\n';
2174 nc = 0;
2175 }
2176 else
2177 {
2178 cc = nc;
2179 nc = 0;
2180 }
2181 }
2182
2183 if (cc == 0) /* NUL is stored as NL */
2184 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00002185 if (enc_dbcs && (cc & 0xff) == 0)
2186 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
2187 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 --no_mapping;
2190#ifdef FEAT_GUI
2191 if (gui.in_use)
2192 --allow_keys;
2193#endif
2194 if (nc)
2195 vungetc(nc);
2196 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
2197 return cc;
2198}
2199
2200/*
2201 * Insert character, taking care of special keys and mod_mask
2202 */
2203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002204insert_special(
2205 int c,
2206 int allow_modmask,
2207 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 char_u *p;
2210 int len;
2211
2212 /*
2213 * Special function key, translate into "<Key>". Up to the last '>' is
2214 * inserted with ins_str(), so as not to replace characters in replace
2215 * mode.
2216 * Only use mod_mask for special keys, to avoid things like <S-Space>,
2217 * unless 'allow_modmask' is TRUE.
2218 */
Bram Moolenaard0573012017-10-28 21:11:06 +02002219#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 /* Command-key never produces a normal key */
2221 if (mod_mask & MOD_MASK_CMD)
2222 allow_modmask = TRUE;
2223#endif
2224 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
2225 {
2226 p = get_special_key_name(c, mod_mask);
2227 len = (int)STRLEN(p);
2228 c = p[len - 1];
2229 if (len > 2)
2230 {
2231 if (stop_arrow() == FAIL)
2232 return;
2233 p[len - 1] = NUL;
2234 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002235 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 ctrlv = FALSE;
2237 }
2238 }
2239 if (stop_arrow() == OK)
2240 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2241}
2242
2243/*
2244 * Special characters in this context are those that need processing other
2245 * than the simple insertion that can be performed here. This includes ESC
2246 * which terminates the insert, and CR/NL which need special processing to
2247 * open up a new line. This routine tries to optimize insertions performed by
2248 * the "redo", "undo" or "put" commands, so it needs to know when it should
2249 * stop and defer processing to the "normal" mechanism.
2250 * '0' and '^' are special, because they can be followed by CTRL-D.
2251 */
2252#ifdef EBCDIC
2253# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2254#else
2255# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2256#endif
2257
Bram Moolenaar13505972019-01-24 15:04:48 +01002258#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002260/*
2261 * "flags": INSCHAR_FORMAT - force formatting
2262 * INSCHAR_CTRLV - char typed just after CTRL-V
2263 * INSCHAR_NO_FEX - don't use 'formatexpr'
2264 *
2265 * NOTE: passes the flags value straight through to internal_format() which,
2266 * beside INSCHAR_FORMAT (above), is also looking for these:
2267 * INSCHAR_DO_COM - format comments
2268 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271insertchar(
2272 int c, /* character to insert or NUL */
2273 int flags, /* INSCHAR_FORMAT, etc. */
2274 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 int textwidth;
2277#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002281 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002283 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 /*
2287 * Try to break the line in two or more pieces when:
2288 * - Always do this if we have been called to do formatting only.
2289 * - Always do this when 'formatoptions' has the 'a' flag and the line
2290 * ends in white space.
2291 * - Otherwise:
2292 * - Don't do this if inserting a blank
2293 * - Don't do this if an existing character is being replaced, unless
2294 * we're in VREPLACE mode.
2295 * - Do this if the cursor is not on the line where insert started
2296 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2297 * before the insert.
2298 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2299 * before 'textwidth'
2300 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002301 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002302 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002303 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 && *ml_get_cursor() != NUL)
2307 && (curwin->w_cursor.lnum != Insstart.lnum
2308 || ((!has_format_option(FO_INS_LONG)
2309 || Insstart_textlen <= (colnr_T)textwidth)
2310 && (!fo_ins_blank
2311 || Insstart_blank_vcol <= (colnr_T)textwidth
2312 ))))))
2313 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002314 /* Format with 'formatexpr' when it's set. Use internal formatting
2315 * when 'formatexpr' isn't set or it returns non-zero. */
2316#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002317 int do_internal = TRUE;
2318 colnr_T virtcol = get_nolist_virtcol()
2319 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002320
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002321 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2322 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002323 {
2324 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
2325 /* It may be required to save for undo again, e.g. when setline()
2326 * was called. */
2327 ins_need_undo = TRUE;
2328 }
2329 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002331 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (c == NUL) /* only formatting was wanted */
2335 return;
2336
2337#ifdef FEAT_COMMENTS
2338 /* Check whether this character should end a comment. */
2339 if (did_ai && (int)c == end_comment_pending)
2340 {
2341 char_u *line;
2342 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
2343 int middle_len, end_len;
2344 int i;
2345
2346 /*
2347 * Need to remove existing (middle) comment leader and insert end
2348 * comment leader. First, check what comment leader we can find.
2349 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002350 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
2352 {
2353 /* Skip middle-comment string */
2354 while (*p && p[-1] != ':') /* find end of middle flags */
2355 ++p;
2356 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2357 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002358 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 --middle_len;
2360
2361 /* Find the end-comment string */
2362 while (*p && p[-1] != ':') /* find end of end flags */
2363 ++p;
2364 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2365
2366 /* Skip white space before the cursor */
2367 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002368 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 ;
2370 i++;
2371
2372 /* Skip to before the middle leader */
2373 i -= middle_len;
2374
2375 /* Check some expected things before we go on */
2376 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2377 {
2378 /* Backspace over all the stuff we want to replace */
2379 backspace_until_column(i);
2380
2381 /*
2382 * Insert the end-comment string, except for the last
2383 * character, which will get inserted as normal later.
2384 */
2385 ins_bytes_len(lead_end, end_len - 1);
2386 }
2387 }
2388 }
2389 end_comment_pending = NUL;
2390#endif
2391
2392 did_ai = FALSE;
2393#ifdef FEAT_SMARTINDENT
2394 did_si = FALSE;
2395 can_si = FALSE;
2396 can_si_back = FALSE;
2397#endif
2398
2399 /*
2400 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2401 * This speeds up normal text input considerably.
2402 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2403 * need to re-indent at a ':', or any other character (but not what
2404 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002405 * Don't do this when there an InsertCharPre autocommand is defined,
2406 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002407 * Do the check for InsertCharPre before the call to vpeekc() because the
2408 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 */
2410#ifdef USE_ON_FLY_SCROLL
2411 dont_scroll = FALSE; /* allow scrolling here */
2412#endif
2413
2414 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002416 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 && vpeekc() != NUL
2418 && !(State & REPLACE_FLAG)
2419#ifdef FEAT_CINDENT
2420 && !cindent_on()
2421#endif
2422#ifdef FEAT_RIGHTLEFT
2423 && !p_ri
2424#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002425 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427#define INPUT_BUFLEN 100
2428 char_u buf[INPUT_BUFLEN + 1];
2429 int i;
2430 colnr_T virtcol = 0;
2431
2432 buf[0] = c;
2433 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002434 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 virtcol = get_nolist_virtcol();
2436 /*
2437 * Stop the string when:
2438 * - no more chars available
2439 * - finding a special character (command key)
2440 * - buffer is full
2441 * - running into the 'textwidth' boundary
2442 * - need to check for abbreviation: A non-word char after a word-char
2443 */
2444 while ( (c = vpeekc()) != NUL
2445 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 && i < INPUT_BUFLEN
2448 && (textwidth == 0
2449 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2450 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2451 {
2452#ifdef FEAT_RIGHTLEFT
2453 c = vgetc();
2454 if (p_hkmap && KeyTyped)
2455 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 buf[i++] = c;
2457#else
2458 buf[i++] = vgetc();
2459#endif
2460 }
2461
2462#ifdef FEAT_DIGRAPHS
2463 do_digraph(-1); /* clear digraphs */
2464 do_digraph(buf[i-1]); /* may be the start of a digraph */
2465#endif
2466 buf[i] = NUL;
2467 ins_str(buf);
2468 if (flags & INSCHAR_CTRLV)
2469 {
2470 redo_literal(*buf);
2471 i = 1;
2472 }
2473 else
2474 i = 0;
2475 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002476 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
2478 else
2479 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002480 int cc;
2481
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2483 {
2484 char_u buf[MB_MAXBYTES + 1];
2485
2486 (*mb_char2bytes)(c, buf);
2487 buf[cc] = NUL;
2488 ins_char_bytes(buf, cc);
2489 AppendCharToRedobuff(c);
2490 }
2491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
2493 ins_char(c);
2494 if (flags & INSCHAR_CTRLV)
2495 redo_literal(c);
2496 else
2497 AppendCharToRedobuff(c);
2498 }
2499 }
2500}
2501
2502/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002503 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002504 *
2505 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2506 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002507 */
2508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002509internal_format(
2510 int textwidth,
2511 int second_indent,
2512 int flags,
2513 int format_only,
2514 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002515{
2516 int cc;
2517 int save_char = NUL;
2518 int haveto_redraw = FALSE;
2519 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002520 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002521 int fo_white_par = has_format_option(FO_WHITE_PAR);
2522 int first_line = TRUE;
2523#ifdef FEAT_COMMENTS
2524 colnr_T leader_len;
2525 int no_leader = FALSE;
2526 int do_comments = (flags & INSCHAR_DO_COM);
2527#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02002528#ifdef FEAT_LINEBREAK
2529 int has_lbr = curwin->w_p_lbr;
2530
2531 /* make sure win_lbr_chartabsize() counts correctly */
2532 curwin->w_p_lbr = FALSE;
2533#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002534
2535 /*
2536 * When 'ai' is off we don't want a space under the cursor to be
2537 * deleted. Replace it with an 'x' temporarily.
2538 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002539 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002540 {
2541 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002542 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002543 {
2544 save_char = cc;
2545 pchar_cursor('x');
2546 }
2547 }
2548
2549 /*
2550 * Repeat breaking lines, until the current line is not too long.
2551 */
2552 while (!got_int)
2553 {
2554 int startcol; /* Cursor column at entry */
2555 int wantcol; /* column at textwidth border */
2556 int foundcol; /* column for start of spaces */
2557 int end_foundcol = 0; /* column for start of word */
2558 colnr_T len;
2559 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002560 int orig_col = 0;
2561 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002562 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002563 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002564 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002565
Bram Moolenaar97b98102009-11-17 16:41:01 +00002566 virtcol = get_nolist_virtcol()
2567 + char2cells(c != NUL ? c : gchar_cursor());
2568 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002569 break;
2570
2571#ifdef FEAT_COMMENTS
2572 if (no_leader)
2573 do_comments = FALSE;
2574 else if (!(flags & INSCHAR_FORMAT)
2575 && has_format_option(FO_WRAP_COMS))
2576 do_comments = TRUE;
2577
2578 /* Don't break until after the comment leader */
2579 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002580 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002581 else
2582 leader_len = 0;
2583
2584 /* If the line doesn't start with a comment leader, then don't
2585 * start one in a following broken line. Avoids that a %word
2586 * moved to the start of the next line causes all following lines
2587 * to start with %. */
2588 if (leader_len == 0)
2589 no_leader = TRUE;
2590#endif
2591 if (!(flags & INSCHAR_FORMAT)
2592#ifdef FEAT_COMMENTS
2593 && leader_len == 0
2594#endif
2595 && !has_format_option(FO_WRAP))
2596
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002597 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002598 if ((startcol = curwin->w_cursor.col) == 0)
2599 break;
2600
2601 /* find column of textwidth border */
2602 coladvance((colnr_T)textwidth);
2603 wantcol = curwin->w_cursor.col;
2604
Bram Moolenaar97b98102009-11-17 16:41:01 +00002605 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002606 foundcol = 0;
2607
2608 /*
2609 * Find position to break at.
2610 * Stop at first entered white when 'formatoptions' has 'v'
2611 */
2612 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002613 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002614 || curwin->w_cursor.lnum != Insstart.lnum
2615 || curwin->w_cursor.col >= Insstart.col)
2616 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002617 if (curwin->w_cursor.col == startcol && c != NUL)
2618 cc = c;
2619 else
2620 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002621 if (WHITECHAR(cc))
2622 {
2623 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002624 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002625
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002626 // find start of sequence of blanks
2627 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002628 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2629 {
2630 dec_cursor();
2631 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002632
2633 // Increment count of how many whitespace chars in this
2634 // group; we only need to know if it's more than one.
2635 if (wcc < 2)
2636 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002637 }
2638 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
2639 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002640
2641 // Don't break after a period when 'formatoptions' has 'p' and
2642 // there are less than two spaces.
2643 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2644 continue;
2645
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002646#ifdef FEAT_COMMENTS
2647 /* Don't break until after the comment leader */
2648 if (curwin->w_cursor.col < leader_len)
2649 break;
2650#endif
2651 if (has_format_option(FO_ONE_LETTER))
2652 {
2653 /* do not break after one-letter words */
2654 if (curwin->w_cursor.col == 0)
2655 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002656#ifdef FEAT_COMMENTS
2657 /* do not break "#a b" when 'tw' is 2 */
2658 if (curwin->w_cursor.col <= leader_len)
2659 break;
2660#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002661 col = curwin->w_cursor.col;
2662 dec_cursor();
2663 cc = gchar_cursor();
2664
2665 if (WHITECHAR(cc))
2666 continue; /* one-letter, continue */
2667 curwin->w_cursor.col = col;
2668 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002669
2670 inc_cursor();
2671
2672 end_foundcol = end_col + 1;
2673 foundcol = curwin->w_cursor.col;
2674 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002675 break;
2676 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002677 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002678 {
2679 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002680 if (curwin->w_cursor.col != startcol)
2681 {
2682#ifdef FEAT_COMMENTS
2683 /* Don't break until after the comment leader */
2684 if (curwin->w_cursor.col < leader_len)
2685 break;
2686#endif
2687 col = curwin->w_cursor.col;
2688 inc_cursor();
2689 /* Don't change end_foundcol if already set. */
2690 if (foundcol != curwin->w_cursor.col)
2691 {
2692 foundcol = curwin->w_cursor.col;
2693 end_foundcol = foundcol;
2694 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2695 break;
2696 }
2697 curwin->w_cursor.col = col;
2698 }
2699
2700 if (curwin->w_cursor.col == 0)
2701 break;
2702
2703 col = curwin->w_cursor.col;
2704
2705 dec_cursor();
2706 cc = gchar_cursor();
2707
2708 if (WHITECHAR(cc))
2709 continue; /* break with space */
2710#ifdef FEAT_COMMENTS
2711 /* Don't break until after the comment leader */
2712 if (curwin->w_cursor.col < leader_len)
2713 break;
2714#endif
2715
2716 curwin->w_cursor.col = col;
2717
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002718 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002719 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002720 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2721 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002722 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002723 if (curwin->w_cursor.col == 0)
2724 break;
2725 dec_cursor();
2726 }
2727
2728 if (foundcol == 0) /* no spaces, cannot break line */
2729 {
2730 curwin->w_cursor.col = startcol;
2731 break;
2732 }
2733
2734 /* Going to break the line, remove any "$" now. */
2735 undisplay_dollar();
2736
2737 /*
2738 * Offset between cursor position and line break is used by replace
2739 * stack functions. VREPLACE does not use this, and backspaces
2740 * over the text instead.
2741 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002742 if (State & VREPLACE_FLAG)
2743 orig_col = startcol; /* Will start backspacing from here */
2744 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002745 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002746
2747 /*
2748 * adjust startcol for spaces that will be deleted and
2749 * characters that will remain on top line
2750 */
2751 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002752 while ((cc = gchar_cursor(), WHITECHAR(cc))
2753 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002754 inc_cursor();
2755 startcol -= curwin->w_cursor.col;
2756 if (startcol < 0)
2757 startcol = 0;
2758
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002759 if (State & VREPLACE_FLAG)
2760 {
2761 /*
2762 * In VREPLACE mode, we will backspace over the text to be
2763 * wrapped, so save a copy now to put on the next line.
2764 */
2765 saved_text = vim_strsave(ml_get_cursor());
2766 curwin->w_cursor.col = orig_col;
2767 if (saved_text == NULL)
2768 break; /* Can't do it, out of memory */
2769 saved_text[startcol] = NUL;
2770
2771 /* Backspace over characters that will move to the next line */
2772 if (!fo_white_par)
2773 backspace_until_column(foundcol);
2774 }
2775 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002776 {
2777 /* put cursor after pos. to break line */
2778 if (!fo_white_par)
2779 curwin->w_cursor.col = foundcol;
2780 }
2781
2782 /*
2783 * Split the line just before the margin.
2784 * Only insert/delete lines, but don't really redraw the window.
2785 */
2786 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2787 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
2788#ifdef FEAT_COMMENTS
2789 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002790 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002791#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002792 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2793 if (!(flags & INSCHAR_COM_LIST))
2794 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002795
2796 replace_offset = 0;
2797 if (first_line)
2798 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002799 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002800 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002801 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002802 * This section is for auto-wrap of numeric lists. When not
2803 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2804 * flag will be set and open_line() will handle it (as seen
2805 * above). The code here (and in get_number_indent()) will
2806 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002807 */
2808 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002809 second_indent =
2810 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002811 if (second_indent >= 0)
2812 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002813 if (State & VREPLACE_FLAG)
2814 change_indent(INDENT_SET, second_indent,
2815 FALSE, NUL, TRUE);
2816 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002817#ifdef FEAT_COMMENTS
2818 if (leader_len > 0 && second_indent - leader_len > 0)
2819 {
2820 int i;
2821 int padding = second_indent - leader_len;
2822
2823 /* We started at the first_line of a numbered list
2824 * that has a comment. the open_line() function has
2825 * inserted the proper comment leader and positioned
2826 * the cursor at the end of the split line. Now we
2827 * add the additional whitespace needed after the
2828 * comment leader for the numbered list. */
2829 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002830 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002831 }
2832 else
2833 {
2834#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002835 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002836#ifdef FEAT_COMMENTS
2837 }
2838#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002839 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002840 }
2841 first_line = FALSE;
2842 }
2843
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002844 if (State & VREPLACE_FLAG)
2845 {
2846 /*
2847 * In VREPLACE mode we have backspaced over the text to be
2848 * moved, now we re-insert it into the new line.
2849 */
2850 ins_bytes(saved_text);
2851 vim_free(saved_text);
2852 }
2853 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002854 {
2855 /*
2856 * Check if cursor is not past the NUL off the line, cindent
2857 * may have added or removed indent.
2858 */
2859 curwin->w_cursor.col += startcol;
2860 len = (colnr_T)STRLEN(ml_get_curline());
2861 if (curwin->w_cursor.col > len)
2862 curwin->w_cursor.col = len;
2863 }
2864
2865 haveto_redraw = TRUE;
2866#ifdef FEAT_CINDENT
2867 can_cindent = TRUE;
2868#endif
2869 /* moved the cursor, don't autoindent or cindent now */
2870 did_ai = FALSE;
2871#ifdef FEAT_SMARTINDENT
2872 did_si = FALSE;
2873 can_si = FALSE;
2874 can_si_back = FALSE;
2875#endif
2876 line_breakcheck();
2877 }
2878
2879 if (save_char != NUL) /* put back space after cursor */
2880 pchar_cursor(save_char);
2881
Bram Moolenaar0026d472014-09-09 16:32:39 +02002882#ifdef FEAT_LINEBREAK
2883 curwin->w_p_lbr = has_lbr;
2884#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002885 if (!format_only && haveto_redraw)
2886 {
2887 update_topline();
2888 redraw_curbuf_later(VALID);
2889 }
2890}
2891
2892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 * Called after inserting or deleting text: When 'formatoptions' includes the
2894 * 'a' flag format from the current line until the end of the paragraph.
2895 * Keep the cursor at the same position relative to the text.
2896 * The caller must have saved the cursor line for undo, following ones will be
2897 * saved here.
2898 */
2899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002900auto_format(
2901 int trailblank, /* when TRUE also format with trailing blank */
2902 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 pos_T pos;
2905 colnr_T len;
2906 char_u *old;
2907 char_u *new, *pnew;
2908 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002909 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
2911 if (!has_format_option(FO_AUTO))
2912 return;
2913
2914 pos = curwin->w_cursor;
2915 old = ml_get_curline();
2916
2917 /* may remove added space */
2918 check_auto_format(FALSE);
2919
2920 /* Don't format in Insert mode when the cursor is on a trailing blank, the
2921 * user might insert normal text next. Also skip formatting when "1" is
2922 * in 'formatoptions' and there is a single character before the cursor.
2923 * Otherwise the line would be broken and when typing another non-white
2924 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002925 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (*old != NUL && !trailblank && wasatend)
2927 {
2928 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002929 cc = gchar_cursor();
2930 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2931 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002933 cc = gchar_cursor();
2934 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
2936 curwin->w_cursor = pos;
2937 return;
2938 }
2939 curwin->w_cursor = pos;
2940 }
2941
2942#ifdef FEAT_COMMENTS
2943 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
2944 * comments. */
2945 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02002946 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 return;
2948#endif
2949
2950 /*
2951 * May start formatting in a previous line, so that after "x" a word is
2952 * moved to the previous line if it fits there now. Only when this is not
2953 * the start of a paragraph.
2954 */
2955 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2956 {
2957 --curwin->w_cursor.lnum;
2958 if (u_save_cursor() == FAIL)
2959 return;
2960 }
2961
2962 /*
2963 * Do the formatting and restore the cursor position. "saved_cursor" will
2964 * be adjusted for the text formatting.
2965 */
2966 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002967 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 curwin->w_cursor = saved_cursor;
2969 saved_cursor.lnum = 0;
2970
2971 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2972 {
2973 /* "cannot happen" */
2974 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2975 coladvance((colnr_T)MAXCOL);
2976 }
2977 else
2978 check_cursor_col();
2979
2980 /* Insert mode: If the cursor is now after the end of the line while it
2981 * previously wasn't, the line was broken. Because of the rule above we
2982 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2983 * formatted. */
2984 if (!wasatend && has_format_option(FO_WHITE_PAR))
2985 {
2986 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002987 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 if (curwin->w_cursor.col == len)
2989 {
2990 pnew = vim_strnsave(new, len + 2);
2991 pnew[len] = ' ';
2992 pnew[len + 1] = NUL;
2993 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
2994 /* remove the space later */
2995 did_add_space = TRUE;
2996 }
2997 else
2998 /* may remove added space */
2999 check_auto_format(FALSE);
3000 }
3001
3002 check_cursor();
3003}
3004
3005/*
3006 * When an extra space was added to continue a paragraph for auto-formatting,
3007 * delete it now. The space must be under the cursor, just after the insert
3008 * position.
3009 */
3010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003011check_auto_format(
3012 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003015 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
3017 if (did_add_space)
3018 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003019 cc = gchar_cursor();
3020 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 /* Somehow the space was removed already. */
3022 did_add_space = FALSE;
3023 else
3024 {
3025 if (!end_insert)
3026 {
3027 inc_cursor();
3028 c = gchar_cursor();
3029 dec_cursor();
3030 }
3031 if (c != NUL)
3032 {
3033 /* The space is no longer at the end of the line, delete it. */
3034 del_char(FALSE);
3035 did_add_space = FALSE;
3036 }
3037 }
3038 }
3039}
3040
3041/*
3042 * Find out textwidth to be used for formatting:
3043 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02003044 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 * if invalid value, use 0.
3046 * Set default to window width (maximum 79) for "gq" operator.
3047 */
3048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003049comp_textwidth(
3050 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 int textwidth;
3053
3054 textwidth = curbuf->b_p_tw;
3055 if (textwidth == 0 && curbuf->b_p_wm)
3056 {
3057 /* The width is the window width minus 'wrapmargin' minus all the
3058 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003059 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#ifdef FEAT_CMDWIN
3061 if (cmdwin_type != 0)
3062 textwidth -= 1;
3063#endif
3064#ifdef FEAT_FOLDING
3065 textwidth -= curwin->w_p_fdc;
3066#endif
3067#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003068 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 textwidth -= 1;
3070#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02003071 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 textwidth -= 8;
3073 }
3074 if (textwidth < 0)
3075 textwidth = 0;
3076 if (ff && textwidth == 0)
3077 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003078 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (textwidth > 79)
3080 textwidth = 79;
3081 }
3082 return textwidth;
3083}
3084
3085/*
3086 * Put a character in the redo buffer, for when just after a CTRL-V.
3087 */
3088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003089redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 char_u buf[10];
3092
3093 /* Only digits need special treatment. Translate them into a string of
3094 * three digits. */
3095 if (VIM_ISDIGIT(c))
3096 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003097 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 AppendToRedobuff(buf);
3099 }
3100 else
3101 AppendCharToRedobuff(c);
3102}
3103
3104/*
3105 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003106 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003109start_arrow(
3110 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003112 start_arrow_common(end_insert_pos, TRUE);
3113}
3114
3115/*
3116 * Like start_arrow() but with end_change argument.
3117 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
3118 */
3119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003120start_arrow_with_change(
3121 pos_T *end_insert_pos, /* can be NULL */
3122 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003123{
3124 start_arrow_common(end_insert_pos, end_change);
3125 if (!end_change)
3126 {
3127 AppendCharToRedobuff(Ctrl_G);
3128 AppendCharToRedobuff('U');
3129 }
3130}
3131
3132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003133start_arrow_common(
3134 pos_T *end_insert_pos, /* can be NULL */
3135 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003136{
3137 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01003140 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 arrow_used = TRUE; /* this means we stopped the current insert */
3142 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003143#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003144 check_spell_redraw();
3145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146}
3147
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003148#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003149/*
3150 * If we skipped highlighting word at cursor, do it now.
3151 * It may be skipped again, thus reset spell_redraw_lnum first.
3152 */
3153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003154check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003155{
3156 if (spell_redraw_lnum != 0)
3157 {
3158 linenr_T lnum = spell_redraw_lnum;
3159
3160 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01003161 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00003162 }
3163}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003164
Bram Moolenaar217ad922005-03-20 22:37:15 +00003165#endif
3166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167/*
3168 * stop_arrow() is called before a change is made in insert mode.
3169 * If an arrow key has been used, start a new insertion.
3170 * Returns FAIL if undo is impossible, shouldn't insert then.
3171 */
3172 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003173stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174{
3175 if (arrow_used)
3176 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02003177 Insstart = curwin->w_cursor; /* new insertion starts here */
3178 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
3179 /* Don't update the original insert position when moved to the
3180 * right, except when nothing was inserted yet. */
3181 update_Insstart_orig = FALSE;
3182 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (u_save_cursor() == OK)
3185 {
3186 arrow_used = FALSE;
3187 ins_need_undo = FALSE;
3188 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02003189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (State & VREPLACE_FLAG)
3192 {
3193 orig_line_count = curbuf->b_ml.ml_line_count;
3194 vr_lines_changed = 1;
3195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 ResetRedobuff();
3197 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00003198 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200 else if (ins_need_undo)
3201 {
3202 if (u_save_cursor() == OK)
3203 ins_need_undo = FALSE;
3204 }
3205
3206#ifdef FEAT_FOLDING
3207 /* Always open fold at the cursor line when inserting something. */
3208 foldOpenCursor();
3209#endif
3210
3211 return (arrow_used || ins_need_undo ? FAIL : OK);
3212}
3213
3214/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003215 * Do a few things to stop inserting.
3216 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
3217 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 */
3219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220stop_insert(
3221 pos_T *end_insert_pos,
3222 int esc, /* called by ins_esc() */
3223 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003225 int cc;
3226 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 stop_redo_ins();
3229 replace_flush(); /* abandon replace stack */
3230
3231 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003232 * Save the inserted text for later redo with ^@ and CTRL-A.
3233 * Don't do it when "restart_edit" was set and nothing was inserted,
3234 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003236 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003237 if (did_restart_edit == 0 || (ptr != NULL
3238 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003239 {
3240 vim_free(last_insert);
3241 last_insert = ptr;
3242 last_insert_skip = new_insert_skip;
3243 }
3244 else
3245 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003247 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 {
3249 /* Auto-format now. It may seem strange to do this when stopping an
3250 * insertion (or moving the cursor), but it's required when appending
3251 * a line and having it end in a space. But only do it when something
3252 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003253 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003255 pos_T tpos = curwin->w_cursor;
3256
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 /* When the cursor is at the end of the line after a space the
3258 * formatting will move it to the following word. Avoid that by
3259 * moving the cursor onto the space. */
3260 cc = 'x';
3261 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
3262 {
3263 dec_cursor();
3264 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003265 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003266 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268
3269 auto_format(TRUE, FALSE);
3270
Bram Moolenaar1c465442017-03-12 20:10:05 +01003271 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003272 {
3273 if (gchar_cursor() != NUL)
3274 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003275 // If the cursor is still at the same character, also keep
3276 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003277 if (gchar_cursor() == NUL
3278 && curwin->w_cursor.lnum == tpos.lnum
3279 && curwin->w_cursor.col == tpos.col)
3280 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283
3284 /* If a space was inserted for auto-formatting, remove it now. */
3285 check_auto_format(TRUE);
3286
3287 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003288 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00003289 * Do this when ESC was used or moving the cursor up/down.
3290 * Check for the old position still being valid, just in case the text
3291 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01003292 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003293 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3294 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003296 pos_T tpos = curwin->w_cursor;
3297
3298 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003299 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003300 for (;;)
3301 {
3302 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3303 --curwin->w_cursor.col;
3304 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003305 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003306 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003307 if (del_char(TRUE) == FAIL)
3308 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003309 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003310 if (curwin->w_cursor.lnum != tpos.lnum)
3311 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003312 else
3313 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003314 /* reset tpos, could have been invalidated in the loop above */
3315 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003316 tpos.col++;
3317 if (cc != NUL && gchar_pos(&tpos) == NUL)
3318 ++curwin->w_cursor.col; /* put cursor back on the NUL */
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 /* <C-S-Right> may have started Visual mode, adjust the position for
3322 * deleted characters. */
3323 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3324 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003325 int len = (int)STRLEN(ml_get_curline());
3326
3327 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003329 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
3332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 }
3335 did_ai = FALSE;
3336#ifdef FEAT_SMARTINDENT
3337 did_si = FALSE;
3338 can_si = FALSE;
3339 can_si_back = FALSE;
3340#endif
3341
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003342 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3343 * now in a different buffer. */
3344 if (end_insert_pos != NULL)
3345 {
3346 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003347 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003348 curbuf->b_op_end = *end_insert_pos;
3349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350}
3351
3352/*
3353 * Set the last inserted text to a single character.
3354 * Used for the replace command.
3355 */
3356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003357set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 char_u *s;
3360
3361 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (last_insert != NULL)
3364 {
3365 s = last_insert;
3366 /* Use the CTRL-V only when entering a special char */
3367 if (c < ' ' || c == DEL)
3368 *s++ = Ctrl_V;
3369 s = add_char2buf(c, s);
3370 *s++ = ESC;
3371 *s++ = NUL;
3372 last_insert_skip = 0;
3373 }
3374}
3375
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003376#if defined(EXITFREE) || defined(PROTO)
3377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003378free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003379{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003380 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003381}
3382#endif
3383
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384/*
3385 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3386 * and CSI. Handle multi-byte characters.
3387 * Returns a pointer to after the added bytes.
3388 */
3389 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003390add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003392 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 int i;
3394 int len;
3395
3396 len = (*mb_char2bytes)(c, temp);
3397 for (i = 0; i < len; ++i)
3398 {
3399 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
3401 if (c == K_SPECIAL)
3402 {
3403 *s++ = K_SPECIAL;
3404 *s++ = KS_SPECIAL;
3405 *s++ = KE_FILLER;
3406 }
3407#ifdef FEAT_GUI
3408 else if (c == CSI)
3409 {
3410 *s++ = CSI;
3411 *s++ = KS_EXTRA;
3412 *s++ = (int)KE_CSI;
3413 }
3414#endif
3415 else
3416 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 return s;
3419}
3420
3421/*
3422 * move cursor to start of line
3423 * if flags & BL_WHITE move to first non-white
3424 * if flags & BL_SOL move to first non-white if startofline is set,
3425 * otherwise keep "curswant" column
3426 * if flags & BL_FIX don't leave the cursor on a NUL.
3427 */
3428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003429beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 if ((flags & BL_SOL) && !p_sol)
3432 coladvance(curwin->w_curswant);
3433 else
3434 {
3435 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438 if (flags & (BL_WHITE | BL_SOL))
3439 {
3440 char_u *ptr;
3441
Bram Moolenaar1c465442017-03-12 20:10:05 +01003442 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3444 ++curwin->w_cursor.col;
3445 }
3446 curwin->w_set_curswant = TRUE;
3447 }
3448}
3449
3450/*
3451 * oneright oneleft cursor_down cursor_up
3452 *
3453 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003454 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 * Return OK when successful, FAIL when we hit a line of file boundary.
3456 */
3457
3458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003459oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
3461 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 if (virtual_active())
3465 {
3466 pos_T prevpos = curwin->w_cursor;
3467
3468 /* Adjust for multi-wide char (excluding TAB) */
3469 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003470 coladvance(getviscol() + ((*ptr != TAB
3471 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 ? ptr2cells(ptr) : 1));
3473 curwin->w_set_curswant = TRUE;
3474 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
3475 return (prevpos.col != curwin->w_cursor.col
3476 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003480 if (*ptr == NUL)
3481 return FAIL; /* already at the very end */
3482
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003483 if (has_mbyte)
3484 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003486 l = 1;
3487
3488 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3489 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003490 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003491 return FAIL;
3492 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 curwin->w_set_curswant = TRUE;
3495 return OK;
3496}
3497
3498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003499oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (virtual_active())
3502 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003503#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 int v = getviscol();
3507
3508 if (v == 0)
3509 return FAIL;
3510
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003511#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 /* We might get stuck on 'showbreak', skip over it. */
3513 width = 1;
3514 for (;;)
3515 {
3516 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02003517 /* getviscol() is slow, skip it when 'showbreak' is empty,
3518 * 'breakindent' is not set and there are no multi-byte
3519 * characters */
3520 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003521 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 break;
3523 ++width;
3524 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003525#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 if (curwin->w_cursor.coladd == 1)
3530 {
3531 char_u *ptr;
3532
3533 /* Adjust for multi-wide char (not a TAB) */
3534 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003535 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3536 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 curwin->w_cursor.coladd = 0;
3538 }
3539
3540 curwin->w_set_curswant = TRUE;
3541 return OK;
3542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
3544 if (curwin->w_cursor.col == 0)
3545 return FAIL;
3546
3547 curwin->w_set_curswant = TRUE;
3548 --curwin->w_cursor.col;
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /* if the character on the left of the current cursor is a multi-byte
3551 * character, move to its first byte */
3552 if (has_mbyte)
3553 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 return OK;
3555}
3556
3557 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003558cursor_up(
3559 long n,
3560 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561{
3562 linenr_T lnum;
3563
3564 if (n > 0)
3565 {
3566 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003567 /* This fails if the cursor is already in the first line or the count
3568 * is larger than the line number and '-' is in 'cpoptions' */
3569 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return FAIL;
3571 if (n >= lnum)
3572 lnum = 1;
3573 else
3574#ifdef FEAT_FOLDING
3575 if (hasAnyFolding(curwin))
3576 {
3577 /*
3578 * Count each sequence of folded lines as one logical line.
3579 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003580 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 (void)hasFolding(lnum, &lnum, NULL);
3582
3583 while (n--)
3584 {
3585 /* move up one line */
3586 --lnum;
3587 if (lnum <= 1)
3588 break;
3589 /* If we entered a fold, move to the beginning, unless in
3590 * Insert mode or when 'foldopen' contains "all": it will open
3591 * in a moment. */
3592 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3593 (void)hasFolding(lnum, &lnum, NULL);
3594 }
3595 if (lnum < 1)
3596 lnum = 1;
3597 }
3598 else
3599#endif
3600 lnum -= n;
3601 curwin->w_cursor.lnum = lnum;
3602 }
3603
3604 /* try to advance to the column we want to be at */
3605 coladvance(curwin->w_curswant);
3606
3607 if (upd_topline)
3608 update_topline(); /* make sure curwin->w_topline is valid */
3609
3610 return OK;
3611}
3612
3613/*
3614 * Cursor down a number of logical lines.
3615 */
3616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617cursor_down(
3618 long n,
3619 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 linenr_T lnum;
3622
3623 if (n > 0)
3624 {
3625 lnum = curwin->w_cursor.lnum;
3626#ifdef FEAT_FOLDING
3627 /* Move to last line of fold, will fail if it's the end-of-file. */
3628 (void)hasFolding(lnum, NULL, &lnum);
3629#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003630 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003631 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003632 if (lnum >= curbuf->b_ml.ml_line_count
3633 || (lnum + n > curbuf->b_ml.ml_line_count
3634 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 return FAIL;
3636 if (lnum + n >= curbuf->b_ml.ml_line_count)
3637 lnum = curbuf->b_ml.ml_line_count;
3638 else
3639#ifdef FEAT_FOLDING
3640 if (hasAnyFolding(curwin))
3641 {
3642 linenr_T last;
3643
3644 /* count each sequence of folded lines as one logical line */
3645 while (n--)
3646 {
3647 if (hasFolding(lnum, NULL, &last))
3648 lnum = last + 1;
3649 else
3650 ++lnum;
3651 if (lnum >= curbuf->b_ml.ml_line_count)
3652 break;
3653 }
3654 if (lnum > curbuf->b_ml.ml_line_count)
3655 lnum = curbuf->b_ml.ml_line_count;
3656 }
3657 else
3658#endif
3659 lnum += n;
3660 curwin->w_cursor.lnum = lnum;
3661 }
3662
3663 /* try to advance to the column we want to be at */
3664 coladvance(curwin->w_curswant);
3665
3666 if (upd_topline)
3667 update_topline(); /* make sure curwin->w_topline is valid */
3668
3669 return OK;
3670}
3671
3672/*
3673 * Stuff the last inserted text in the read buffer.
3674 * Last_insert actually is a copy of the redo buffer, so we
3675 * first have to remove the command.
3676 */
3677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003678stuff_inserted(
3679 int c, /* Command character to be inserted */
3680 long count, /* Repeat this many times */
3681 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 char_u *esc_ptr;
3684 char_u *ptr;
3685 char_u *last_ptr;
3686 char_u last = NUL;
3687
3688 ptr = get_last_insert();
3689 if (ptr == NULL)
3690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003691 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 return FAIL;
3693 }
3694
3695 /* may want to stuff the command character, to start Insert mode */
3696 if (c != NUL)
3697 stuffcharReadbuff(c);
3698 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
3699 *esc_ptr = NUL; /* remove the ESC */
3700
3701 /* when the last char is either "0" or "^" it will be quoted if no ESC
3702 * comes after it OR if it will inserted more than once and "ptr"
3703 * starts with ^D. -- Acevedo
3704 */
3705 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3706 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3707 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3708 {
3709 last = *last_ptr;
3710 *last_ptr = NUL;
3711 }
3712
3713 do
3714 {
3715 stuffReadbuff(ptr);
3716 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
3717 if (last)
3718 stuffReadbuff((char_u *)(last == '0'
3719 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3720 : IF_EB("\026^", CTRL_V_STR "^")));
3721 }
3722 while (--count > 0);
3723
3724 if (last)
3725 *last_ptr = last;
3726
3727 if (esc_ptr != NULL)
3728 *esc_ptr = ESC; /* put the ESC back */
3729
3730 /* may want to stuff a trailing ESC, to get out of Insert mode */
3731 if (!no_esc)
3732 stuffcharReadbuff(ESC);
3733
3734 return OK;
3735}
3736
3737 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003738get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 if (last_insert == NULL)
3741 return NULL;
3742 return last_insert + last_insert_skip;
3743}
3744
3745/*
3746 * Get last inserted string, and remove trailing <Esc>.
3747 * Returns pointer to allocated memory (must be freed) or NULL.
3748 */
3749 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003750get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
3752 char_u *s;
3753 int len;
3754
3755 if (last_insert == NULL)
3756 return NULL;
3757 s = vim_strsave(last_insert + last_insert_skip);
3758 if (s != NULL)
3759 {
3760 len = (int)STRLEN(s);
3761 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
3762 s[len - 1] = NUL;
3763 }
3764 return s;
3765}
3766
3767/*
3768 * Check the word in front of the cursor for an abbreviation.
3769 * Called when the non-id character "c" has been entered.
3770 * When an abbreviation is recognized it is removed from the text and
3771 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3772 */
3773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003774echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
3776 /* Don't check for abbreviation in paste mode, when disabled and just
3777 * after moving around with cursor keys. */
3778 if (p_paste || no_abbr || arrow_used)
3779 return FALSE;
3780
3781 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3782 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3783}
3784
3785/*
3786 * replace-stack functions
3787 *
3788 * When replacing characters, the replaced characters are remembered for each
3789 * new character. This is used to re-insert the old text when backspacing.
3790 *
3791 * There is a NUL headed list of characters for each character that is
3792 * currently in the file after the insertion point. When BS is used, one NUL
3793 * headed list is put back for the deleted character.
3794 *
3795 * For a newline, there are two NUL headed lists. One contains the characters
3796 * that the NL replaced. The extra one stores the characters after the cursor
3797 * that were deleted (always white space).
3798 *
3799 * Replace_offset is normally 0, in which case replace_push will add a new
3800 * character at the end of the stack. If replace_offset is not 0, that many
3801 * characters will be left on the stack above the newly inserted character.
3802 */
3803
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003804static char_u *replace_stack = NULL;
3805static long replace_stack_nr = 0; /* next entry in replace stack */
3806static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
3808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003809replace_push(
3810 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812 char_u *p;
3813
3814 if (replace_stack_nr < replace_offset) /* nothing to do */
3815 return;
3816 if (replace_stack_len <= replace_stack_nr)
3817 {
3818 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003819 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 if (p == NULL) /* out of memory */
3821 {
3822 replace_stack_len -= 50;
3823 return;
3824 }
3825 if (replace_stack != NULL)
3826 {
3827 mch_memmove(p, replace_stack,
3828 (size_t)(replace_stack_nr * sizeof(char_u)));
3829 vim_free(replace_stack);
3830 }
3831 replace_stack = p;
3832 }
3833 p = replace_stack + replace_stack_nr - replace_offset;
3834 if (replace_offset)
3835 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3836 *p = c;
3837 ++replace_stack_nr;
3838}
3839
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003840/*
3841 * Push a character onto the replace stack. Handles a multi-byte character in
3842 * reverse byte order, so that the first byte is popped off first.
3843 * Return the number of bytes done (includes composing characters).
3844 */
3845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003846replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003847{
3848 int l = (*mb_ptr2len)(p);
3849 int j;
3850
3851 for (j = l - 1; j >= 0; --j)
3852 replace_push(p[j]);
3853 return l;
3854}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856/*
3857 * Pop one item from the replace stack.
3858 * return -1 if stack empty
3859 * return replaced character or NUL otherwise
3860 */
3861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003862replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863{
3864 if (replace_stack_nr == 0)
3865 return -1;
3866 return (int)replace_stack[--replace_stack_nr];
3867}
3868
3869/*
3870 * Join the top two items on the replace stack. This removes to "off"'th NUL
3871 * encountered.
3872 */
3873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003874replace_join(
3875 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
3877 int i;
3878
3879 for (i = replace_stack_nr; --i >= 0; )
3880 if (replace_stack[i] == NUL && off-- <= 0)
3881 {
3882 --replace_stack_nr;
3883 mch_memmove(replace_stack + i, replace_stack + i + 1,
3884 (size_t)(replace_stack_nr - i));
3885 return;
3886 }
3887}
3888
3889/*
3890 * Pop bytes from the replace stack until a NUL is found, and insert them
3891 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3892 */
3893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003894replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
3896 int cc;
3897 int oldState = State;
3898
3899 State = NORMAL; /* don't want REPLACE here */
3900 while ((cc = replace_pop()) > 0)
3901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 dec_cursor();
3904 }
3905 State = oldState;
3906}
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908/*
3909 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3910 * indicates a multi-byte char, pop the other bytes too.
3911 */
3912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003913mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
3915 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003916 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 int i;
3918 int c;
3919
3920 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3921 {
3922 buf[0] = cc;
3923 for (i = 1; i < n; ++i)
3924 buf[i] = replace_pop();
3925 ins_bytes_len(buf, n);
3926 }
3927 else
3928 ins_char(cc);
3929
3930 if (enc_utf8)
3931 /* Handle composing chars. */
3932 for (;;)
3933 {
3934 c = replace_pop();
3935 if (c == -1) /* stack empty */
3936 break;
3937 if ((n = MB_BYTE2LEN(c)) == 1)
3938 {
3939 /* Not a multi-byte char, put it back. */
3940 replace_push(c);
3941 break;
3942 }
3943 else
3944 {
3945 buf[0] = c;
3946 for (i = 1; i < n; ++i)
3947 buf[i] = replace_pop();
3948 if (utf_iscomposing(utf_ptr2char(buf)))
3949 ins_bytes_len(buf, n);
3950 else
3951 {
3952 /* Not a composing char, put it back. */
3953 for (i = n - 1; i >= 0; --i)
3954 replace_push(buf[i]);
3955 break;
3956 }
3957 }
3958 }
3959}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960
3961/*
3962 * make the replace stack empty
3963 * (called when exiting replace mode)
3964 */
3965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003966replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003968 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 replace_stack_len = 0;
3970 replace_stack_nr = 0;
3971}
3972
3973/*
3974 * Handle doing a BS for one character.
3975 * cc < 0: replace stack empty, just move cursor
3976 * cc == 0: character was inserted, delete it
3977 * cc > 0: character was replaced, put cc (first byte of original char) back
3978 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003979 * When "limit_col" is >= 0, don't delete before this column. Matters when
3980 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 */
3982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003983replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
3985 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 int orig_len = 0;
3987 int ins_len;
3988 int orig_vcols = 0;
3989 colnr_T start_vcol;
3990 char_u *p;
3991 int i;
3992 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 cc = replace_pop();
3995 if (cc > 0)
3996 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01003997#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003998 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003999
4000 if (curbuf->b_has_textprop)
4001 {
4002 // Do not adjust text properties for individual delete and insert
4003 // operations, do it afterwards on the resulting text.
4004 len_before = STRLEN(ml_get_curline());
4005 ++text_prop_frozen;
4006 }
4007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 if (State & VREPLACE_FLAG)
4009 {
4010 /* Get the number of screen cells used by the character we are
4011 * going to delete. */
4012 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
4013 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
4014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 if (has_mbyte)
4016 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004017 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004019 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 replace_push(cc);
4021 }
4022 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
4024 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004026 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028 replace_pop_ins();
4029
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 if (State & VREPLACE_FLAG)
4031 {
4032 /* Get the number of screen cells used by the inserted characters */
4033 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004034 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 vcol = start_vcol;
4036 for (i = 0; i < ins_len; ++i)
4037 {
4038 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004039 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
4041 vcol -= start_vcol;
4042
4043 /* Delete spaces that were inserted after the cursor to keep the
4044 * text aligned. */
4045 curwin->w_cursor.col += ins_len;
4046 while (vcol > orig_vcols && gchar_cursor() == ' ')
4047 {
4048 del_char(FALSE);
4049 ++orig_vcols;
4050 }
4051 curwin->w_cursor.col -= ins_len;
4052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
Bram Moolenaar196d1572019-01-02 23:47:18 +01004054 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01004056
4057#ifdef FEAT_TEXT_PROP
4058 if (curbuf->b_has_textprop)
4059 {
4060 size_t len_now = STRLEN(ml_get_curline());
4061
4062 --text_prop_frozen;
4063 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02004064 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01004065 }
4066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004069 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070}
4071
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
4073/*
4074 * Map Hebrew keyboard when in hkmap mode.
4075 */
4076 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004077hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078{
4079 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
4080 {
4081 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
4082 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
4083 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
4084 static char_u map[26] =
4085 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
4086 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
4087 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
4088 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
4089 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
4090 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
4091 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
4092 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
4093 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
4094
4095 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
4096 return (int)(map[CharOrd(c)] - 1 + p_aleph);
4097 /* '-1'='sofit' */
4098 else if (c == 'x')
4099 return 'X';
4100 else if (c == 'q')
4101 return '\''; /* {geresh}={'} */
4102 else if (c == 246)
4103 return ' '; /* \"o --> ' ' for a german keyboard */
4104 else if (c == 228)
4105 return ' '; /* \"a --> ' ' -- / -- */
4106 else if (c == 252)
4107 return ' '; /* \"u --> ' ' -- / -- */
4108#ifdef EBCDIC
4109 else if (islower(c))
4110#else
4111 /* NOTE: islower() does not do the right thing for us on Linux so we
4112 * do this the same was as 5.7 and previous, so it works correctly on
4113 * all systems. Specifically, the e.g. Delete and Arrow keys are
4114 * munged and won't work if e.g. searching for Hebrew text.
4115 */
4116 else if (c >= 'a' && c <= 'z')
4117#endif
4118 return (int)(map[CharOrdLow(c)] + p_aleph);
4119 else
4120 return c;
4121 }
4122 else
4123 {
4124 switch (c)
4125 {
4126 case '`': return ';';
4127 case '/': return '.';
4128 case '\'': return ',';
4129 case 'q': return '/';
4130 case 'w': return '\'';
4131
4132 /* Hebrew letters - set offset from 'a' */
4133 case ',': c = '{'; break;
4134 case '.': c = 'v'; break;
4135 case ';': c = 't'; break;
4136 default: {
4137 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
4138
4139#ifdef EBCDIC
4140 /* see note about islower() above */
4141 if (!islower(c))
4142#else
4143 if (c < 'a' || c > 'z')
4144#endif
4145 return c;
4146 c = str[CharOrdLow(c)];
4147 break;
4148 }
4149 }
4150
4151 return (int)(CharOrdLow(c) + p_aleph);
4152 }
4153}
4154#endif
4155
4156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004157ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
4159 int need_redraw = FALSE;
4160 int regname;
4161 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004162 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 /*
4165 * If we are going to wait for a character, show a '"'.
4166 */
4167 pc_status = PC_STATUS_UNSET;
4168 if (redrawing() && !char_avail())
4169 {
4170 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00004171 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 edit_putchar('"', TRUE);
4174#ifdef FEAT_CMDL_INFO
4175 add_to_showcmd_c(Ctrl_R);
4176#endif
4177 }
4178
4179#ifdef USE_ON_FLY_SCROLL
4180 dont_scroll = TRUE; /* disallow scrolling here */
4181#endif
4182
4183 /*
4184 * Don't map the register name. This also prevents the mode message to be
4185 * deleted when ESC is hit.
4186 */
4187 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004188 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
4191 {
4192 /* Get a third key for literal register insertion */
4193 literally = regname;
4194#ifdef FEAT_CMDL_INFO
4195 add_to_showcmd_c(literally);
4196#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004197 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 --no_mapping;
4201
4202#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02004203 /* Don't call u_sync() while typing the expression or giving an error
4204 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 ++no_u_sync;
4206 if (regname == '=')
4207 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004208 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004209# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004211# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004212 /* Sync undo when evaluating the expression calls setline() or
4213 * append(), so that it can be undone separately. */
4214 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02004215
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004217
4218 // Cursor may be moved back a column.
4219 curwin->w_cursor = curpos;
4220 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004221# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004222 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 if (im_on)
4224 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00004227 if (regname == NUL || !valid_yank_reg(regname, FALSE))
4228 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004229 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00004231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 else
4233 {
4234#endif
4235 if (literally == Ctrl_O || literally == Ctrl_P)
4236 {
4237 /* Append the command to the redo buffer. */
4238 AppendCharToRedobuff(Ctrl_R);
4239 AppendCharToRedobuff(literally);
4240 AppendCharToRedobuff(regname);
4241
4242 do_put(regname, BACKWARD, 1L,
4243 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
4244 }
4245 else if (insert_reg(regname, literally) == FAIL)
4246 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004247 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 need_redraw = TRUE; /* remove the '"' */
4249 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004250 else if (stop_insert_mode)
4251 /* When the '=' register was used and a function was invoked that
4252 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
4253 * insert anything, need to remove the '"' */
4254 need_redraw = TRUE;
4255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#ifdef FEAT_EVAL
4257 }
4258 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004259 if (u_sync_once == 1)
4260 ins_need_undo = TRUE;
4261 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#endif
4263#ifdef FEAT_CMDL_INFO
4264 clear_showcmd();
4265#endif
4266
4267 /* If the inserted register is empty, we need to remove the '"' */
4268 if (need_redraw || stuff_empty())
4269 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004270
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004271 /* Disallow starting Visual mode here, would get a weird mode. */
4272 if (!vis_active && VIsual_active)
4273 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274}
4275
4276/*
4277 * CTRL-G commands in Insert mode.
4278 */
4279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004280ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281{
4282 int c;
4283
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004284 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 /*
4288 * Don't map the second key. This also prevents the mode message to be
4289 * deleted when ESC is hit.
4290 */
4291 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004292 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 --no_mapping;
4294 switch (c)
4295 {
4296 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
4297 case K_UP:
4298 case Ctrl_K:
4299 case 'k': ins_up(TRUE);
4300 break;
4301
4302 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
4303 case K_DOWN:
4304 case Ctrl_J:
4305 case 'j': ins_down(TRUE);
4306 break;
4307
4308 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004309 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004311
4312 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004313 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004314 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004315 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 break;
4317
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004318 /* CTRL-G U: do not break undo with the next char */
4319 case 'U':
4320 /* Allow one left/right cursor movement with the next char,
4321 * without breaking undo. */
4322 dont_sync_undo = MAYBE;
4323 break;
4324
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02004326 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328}
4329
4330/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004331 * CTRL-^ in Insert mode.
4332 */
4333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004334ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004335{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004336 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 {
4338 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
4339 if (State & LANGMAP)
4340 {
4341 curbuf->b_p_iminsert = B_IMODE_NONE;
4342 State &= ~LANGMAP;
4343 }
4344 else
4345 {
4346 curbuf->b_p_iminsert = B_IMODE_LMAP;
4347 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004348#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004349 im_set_active(FALSE);
4350#endif
4351 }
4352 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004353#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 else
4355 {
4356 /* There are no ":lmap" mappings, toggle IM */
4357 if (im_get_status())
4358 {
4359 curbuf->b_p_iminsert = B_IMODE_NONE;
4360 im_set_active(FALSE);
4361 }
4362 else
4363 {
4364 curbuf->b_p_iminsert = B_IMODE_IM;
4365 State &= ~LANGMAP;
4366 im_set_active(TRUE);
4367 }
4368 }
4369#endif
4370 set_iminsert_global();
4371 showmode();
4372#ifdef FEAT_GUI
4373 /* may show different cursor shape or color */
4374 if (gui.in_use)
4375 gui_update_cursor(TRUE, FALSE);
4376#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004377#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004378 /* Show/unshow value of 'keymap' in status lines. */
4379 status_redraw_curbuf();
4380#endif
4381}
4382
4383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 * Handle ESC in insert mode.
4385 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4386 * insert.
4387 */
4388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004389ins_esc(
4390 long *count,
4391 int cmdchar,
4392 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
4394 int temp;
4395 static int disabled_redraw = FALSE;
4396
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004397#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 check_spell_redraw();
4399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400#if defined(FEAT_HANGULIN)
4401# if defined(ESC_CHG_TO_ENG_MODE)
4402 hangul_input_state_set(0);
4403# endif
4404 if (composing_hangul)
4405 {
4406 push_raw_key(composing_hangul_buffer, 2);
4407 composing_hangul = 0;
4408 }
4409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
4411 temp = curwin->w_cursor.col;
4412 if (disabled_redraw)
4413 {
4414 --RedrawingDisabled;
4415 disabled_redraw = FALSE;
4416 }
4417 if (!arrow_used)
4418 {
4419 /*
4420 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004421 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4422 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 */
4424 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004425 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 /*
4428 * Repeating insert may take a long time. Check for
4429 * interrupt now and then.
4430 */
4431 if (*count > 0)
4432 {
4433 line_breakcheck();
4434 if (got_int)
4435 *count = 0;
4436 }
4437
4438 if (--*count > 0) /* repeat what was typed */
4439 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004440 /* Vi repeats the insert without replacing characters. */
4441 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4442 State &= ~REPLACE_FLAG;
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 (void)start_redo_ins();
4445 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02004446 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 ++RedrawingDisabled;
4448 disabled_redraw = TRUE;
4449 return FALSE; /* repeat the insert */
4450 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004451 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 undisplay_dollar();
4453 }
4454
4455 /* When an autoindent was removed, curswant stays after the
4456 * indent */
4457 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4458 curwin->w_set_curswant = TRUE;
4459
4460 /* Remember the last Insert position in the '^ mark. */
4461 if (!cmdmod.keepjumps)
4462 curbuf->b_last_insert = curwin->w_cursor;
4463
4464 /*
4465 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004466 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004468 if (!nomove
4469 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004470 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004471 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004472 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#ifdef FEAT_RIGHTLEFT
4474 && !revins_on
4475#endif
4476 )
4477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4479 {
4480 oneleft();
4481 if (restart_edit != NUL)
4482 ++curwin->w_cursor.coladd;
4483 }
4484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 /* Correct cursor for multi-byte character. */
4488 if (has_mbyte)
4489 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 }
4492
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004493#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 /* Disable IM to allow typing English directly for Normal mode commands.
4495 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4496 * well). */
4497 if (!(State & LANGMAP))
4498 im_save_status(&curbuf->b_p_iminsert);
4499 im_set_active(FALSE);
4500#endif
4501
4502 State = NORMAL;
4503 /* need to position cursor again (e.g. when on a TAB ) */
4504 changed_cline_bef_curs();
4505
4506#ifdef FEAT_MOUSE
4507 setmouse();
4508#endif
4509#ifdef CURSOR_SHAPE
4510 ui_cursor_shape(); /* may show different cursor shape */
4511#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004512 if (!p_ek)
4513 /* Re-enable bracketed paste mode. */
4514 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004516 // When recording or for CTRL-O, need to display the new mode.
4517 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004518 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004520 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004521 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
4523 return TRUE; /* exit Insert mode */
4524}
4525
4526#ifdef FEAT_RIGHTLEFT
4527/*
4528 * Toggle language: hkmap and revins_on.
4529 * Move to end of reverse inserted text.
4530 */
4531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004532ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533{
4534 if (revins_on && revins_chars && revins_scol >= 0)
4535 {
4536 while (gchar_cursor() != NUL && revins_chars--)
4537 ++curwin->w_cursor.col;
4538 }
4539 p_ri = !p_ri;
4540 revins_on = (State == INSERT && p_ri);
4541 if (revins_on)
4542 {
4543 revins_scol = curwin->w_cursor.col;
4544 revins_legal++;
4545 revins_chars = 0;
4546 undisplay_dollar();
4547 }
4548 else
4549 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004550 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 showmode();
4552}
4553#endif
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555/*
4556 * If 'keymodel' contains "startsel", may start selection.
4557 * Returns TRUE when a CTRL-O and other keys stuffed.
4558 */
4559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004560ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
4562 if (km_startsel)
4563 switch (c)
4564 {
4565 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 case K_PAGEUP:
4568 case K_KPAGEUP:
4569 case K_PAGEDOWN:
4570 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004571# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 case K_LEFT:
4573 case K_RIGHT:
4574 case K_UP:
4575 case K_DOWN:
4576 case K_END:
4577 case K_HOME:
4578# endif
4579 if (!(mod_mask & MOD_MASK_SHIFT))
4580 break;
4581 /* FALLTHROUGH */
4582 case K_S_LEFT:
4583 case K_S_RIGHT:
4584 case K_S_UP:
4585 case K_S_DOWN:
4586 case K_S_END:
4587 case K_S_HOME:
4588 /* Start selection right away, the cursor can move with
4589 * CTRL-O when beyond the end of the line. */
4590 start_selection();
4591
4592 /* Execute the key in (insert) Select mode. */
4593 stuffcharReadbuff(Ctrl_O);
4594 if (mod_mask)
4595 {
4596 char_u buf[4];
4597
4598 buf[0] = K_SPECIAL;
4599 buf[1] = KS_MODIFIER;
4600 buf[2] = mod_mask;
4601 buf[3] = NUL;
4602 stuffReadbuff(buf);
4603 }
4604 stuffcharReadbuff(c);
4605 return TRUE;
4606 }
4607 return FALSE;
4608}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609
4610/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004611 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 */
4613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004614ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004615{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004616#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004617 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004618 (char_u *)((State & REPLACE_FLAG) ? "i"
4619 : replaceState == VREPLACE ? "v"
4620 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004621#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004622 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004623 if (State & REPLACE_FLAG)
4624 State = INSERT | (State & LANGMAP);
4625 else
4626 State = replaceState | (State & LANGMAP);
4627 AppendCharToRedobuff(K_INS);
4628 showmode();
4629#ifdef CURSOR_SHAPE
4630 ui_cursor_shape(); /* may show different cursor shape */
4631#endif
4632}
4633
4634/*
4635 * Pressed CTRL-O in Insert mode.
4636 */
4637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004638ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004640 if (State & VREPLACE_FLAG)
4641 restart_edit = 'V';
4642 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004643 if (State & REPLACE_FLAG)
4644 restart_edit = 'R';
4645 else
4646 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 if (virtual_active())
4648 ins_at_eol = FALSE; /* cursor always keeps its column */
4649 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004650 ins_at_eol = (gchar_cursor() == NUL);
4651}
4652
4653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 * If the cursor is on an indent, ^T/^D insert/delete one
4655 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004656 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 * with vi. But vi only supports ^T and ^D after an
4658 * autoindent, we support it everywhere.
4659 */
4660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004661ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662{
4663 if (stop_arrow() == FAIL)
4664 return;
4665 AppendCharToRedobuff(c);
4666
4667 /*
4668 * 0^D and ^^D: remove all indent.
4669 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004670 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4671 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 {
4673 --curwin->w_cursor.col;
4674 (void)del_char(FALSE); /* delete the '^' or '0' */
4675 /* In Replace mode, restore the characters that '^' or '0' replaced. */
4676 if (State & REPLACE_FLAG)
4677 replace_pop_ins();
4678 if (lastc == '^')
4679 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004680 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004683 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
4685 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4686 did_ai = FALSE;
4687#ifdef FEAT_SMARTINDENT
4688 did_si = FALSE;
4689 can_si = FALSE;
4690 can_si_back = FALSE;
4691#endif
4692#ifdef FEAT_CINDENT
4693 can_cindent = FALSE; /* no cindenting after ^D or ^T */
4694#endif
4695}
4696
4697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004698ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 int temp;
4701
4702 if (stop_arrow() == FAIL)
4703 return;
4704 if (gchar_cursor() == NUL) /* delete newline */
4705 {
4706 temp = curwin->w_cursor.col;
4707 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004708 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004709 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004713 /* Adjust orig_line_count in case more lines have been deleted than
4714 * have been added. That makes sure, that open_line() later
4715 * can access all buffer lines correctly */
4716 if (State & VREPLACE_FLAG &&
4717 orig_line_count > curbuf->b_ml.ml_line_count)
4718 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02004721 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
4722 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 did_ai = FALSE;
4724#ifdef FEAT_SMARTINDENT
4725 did_si = FALSE;
4726 can_si = FALSE;
4727 can_si_back = FALSE;
4728#endif
4729 AppendCharToRedobuff(K_DEL);
4730}
4731
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004732/*
4733 * Delete one character for ins_bs().
4734 */
4735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004736ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004737{
4738 dec_cursor();
4739 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4740 if (State & REPLACE_FLAG)
4741 {
4742 /* Don't delete characters before the insert point when in
4743 * Replace mode */
4744 if (curwin->w_cursor.lnum != Insstart.lnum
4745 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004746 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004747 }
4748 else
4749 (void)del_char(FALSE);
4750}
4751
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752/*
4753 * Handle Backspace, delete-word and delete-line in Insert mode.
4754 * Return TRUE when backspace was actually used.
4755 */
4756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004757ins_bs(
4758 int c,
4759 int mode,
4760 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761{
4762 linenr_T lnum;
4763 int cc;
4764 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004765 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 colnr_T mincol;
4767 int did_backspace = FALSE;
4768 int in_indent;
4769 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004770 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 /*
4773 * can't delete anything in an empty file
4774 * can't backup past first character in buffer
4775 * can't backup past starting point unless 'backspace' > 1
4776 * can backup to a previous line if 'backspace' == 0
4777 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004778 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 || (
4780#ifdef FEAT_RIGHTLEFT
4781 !revins_on &&
4782#endif
4783 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4784 || (!can_bs(BS_START)
4785 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004786 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4787 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4789 && curwin->w_cursor.col <= ai_col)
4790 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4791 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004792 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 return FALSE;
4794 }
4795
4796 if (stop_arrow() == FAIL)
4797 return FALSE;
4798 in_indent = inindent(0);
4799#ifdef FEAT_CINDENT
4800 if (in_indent)
4801 can_cindent = FALSE;
4802#endif
4803#ifdef FEAT_COMMENTS
4804 end_comment_pending = NUL; /* After BS, don't auto-end comment */
4805#endif
4806#ifdef FEAT_RIGHTLEFT
4807 if (revins_on) /* put cursor after last inserted char */
4808 inc_cursor();
4809#endif
4810
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 /* Virtualedit:
4812 * BACKSPACE_CHAR eats a virtual space
4813 * BACKSPACE_WORD eats all coladd
4814 * BACKSPACE_LINE eats all coladd and keeps going
4815 */
4816 if (curwin->w_cursor.coladd > 0)
4817 {
4818 if (mode == BACKSPACE_CHAR)
4819 {
4820 --curwin->w_cursor.coladd;
4821 return TRUE;
4822 }
4823 if (mode == BACKSPACE_WORD)
4824 {
4825 curwin->w_cursor.coladd = 0;
4826 return TRUE;
4827 }
4828 curwin->w_cursor.coladd = 0;
4829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
4831 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004832 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 */
4834 if (curwin->w_cursor.col == 0)
4835 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004836 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004837 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#ifdef FEAT_RIGHTLEFT
4839 || revins_on
4840#endif
4841 )
4842 {
4843 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4844 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4845 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004846 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004847 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 /*
4850 * In replace mode:
4851 * cc < 0: NL was inserted, delete it
4852 * cc >= 0: NL was replaced, put original characters back
4853 */
4854 cc = -1;
4855 if (State & REPLACE_FLAG)
4856 cc = replace_pop(); /* returns -1 if NL was inserted */
4857 /*
4858 * In replace mode, in the line we started replacing, we only move the
4859 * cursor.
4860 */
4861 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4862 {
4863 dec_cursor();
4864 }
4865 else
4866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 if (!(State & VREPLACE_FLAG)
4868 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
4870 temp = gchar_cursor(); /* remember current char */
4871 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004872
4873 /* When "aw" is in 'formatoptions' we must delete the space at
4874 * the end of the line, otherwise the line will be broken
4875 * again when auto-formatting. */
4876 if (has_format_option(FO_AUTO)
4877 && has_format_option(FO_WHITE_PAR))
4878 {
4879 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4880 TRUE);
4881 int len;
4882
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004883 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004884 if (len > 0 && ptr[len - 1] == ' ')
4885 ptr[len - 1] = NUL;
4886 }
4887
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004888 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 if (temp == NUL && gchar_cursor() != NUL)
4890 inc_cursor();
4891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 else
4893 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 /*
4896 * In REPLACE mode we have to put back the text that was replaced
4897 * by the NL. On the replace stack is first a NUL-terminated
4898 * sequence of characters that were deleted and then the
4899 * characters that NL replaced.
4900 */
4901 if (State & REPLACE_FLAG)
4902 {
4903 /*
4904 * Do the next ins_char() in NORMAL state, to
4905 * prevent ins_char() from replacing characters and
4906 * avoiding showmatch().
4907 */
4908 oldState = State;
4909 State = NORMAL;
4910 /*
4911 * restore characters (blanks) deleted after cursor
4912 */
4913 while (cc > 0)
4914 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004915 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004917 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 cc = replace_pop();
4919 }
4920 /* restore the characters that NL replaced */
4921 replace_pop_ins();
4922 State = oldState;
4923 }
4924 }
4925 did_ai = FALSE;
4926 }
4927 else
4928 {
4929 /*
4930 * Delete character(s) before the cursor.
4931 */
4932#ifdef FEAT_RIGHTLEFT
4933 if (revins_on) /* put cursor on last inserted char */
4934 dec_cursor();
4935#endif
4936 mincol = 0;
4937 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004938 if (mode == BACKSPACE_LINE
4939 && (curbuf->b_p_ai
4940#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004941 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004942#endif
4943 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#ifdef FEAT_RIGHTLEFT
4945 && !revins_on
4946#endif
4947 )
4948 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004949 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004951 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004953 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955
4956 /*
4957 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4958 */
4959 if ( mode == BACKSPACE_CHAR
4960 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004961 || ((get_sts_value() != 0
4962#ifdef FEAT_VARTABS
4963 || tabstop_count(curbuf->b_p_vsts_array)
4964#endif
4965 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004966 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 && (*(ml_get_cursor() - 1) == TAB
4968 || (*(ml_get_cursor() - 1) == ' '
4969 && (!*inserted_space_p
4970 || arrow_used))))))
4971 {
4972 int ts;
4973 colnr_T vcol;
4974 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004975 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 /* Compute the virtual column where we want to be. Since
4979 * 'showbreak' may get in the way, need to get the last column of
4980 * the previous character. */
4981 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004982 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 dec_cursor();
4984 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4985 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004986#ifdef FEAT_VARTABS
4987 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004988 {
4989 ts = (int)get_sw_value(curbuf);
4990 want_vcol = (want_vcol / ts) * ts;
4991 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004992 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004993 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004994 curbuf->b_p_vsts_array);
4995#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004996 if (p_sta && in_indent)
4997 ts = (int)get_sw_value(curbuf);
4998 else
4999 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
5003 /* delete characters until we are at or before want_vcol */
5004 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01005005 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00005006 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
5008 /* insert extra spaces until we are at want_vcol */
5009 while (vcol < want_vcol)
5010 {
5011 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02005012 if (curwin->w_cursor.lnum == Insstart_orig.lnum
5013 && curwin->w_cursor.col < Insstart_orig.col)
5014 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (State & VREPLACE_FLAG)
5017 ins_char(' ');
5018 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00005021 if ((State & REPLACE_FLAG))
5022 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
5025 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00005026
5027 /* If we are now back where we started delete one character. Can
5028 * happen when using 'sts' and 'linebreak'. */
5029 if (vcol >= start_vcol)
5030 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032
5033 /*
5034 * Delete upto starting point, start of line or previous word.
5035 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005036 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005038 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005040 if (has_mbyte)
5041 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005042 do
5043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005045 if (!revins_on) /* put cursor on char to be deleted */
5046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005048
5049 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005050 /* look multi-byte character class */
5051 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005053 prev_cclass = cclass;
5054 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005056
5057 /* start of word? */
5058 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
5059 {
5060 mode = BACKSPACE_WORD_NOT_SPACE;
5061 temp = vim_iswordc(cc);
5062 }
5063 /* end of word? */
5064 else if (mode == BACKSPACE_WORD_NOT_SPACE
5065 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01005066 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005069 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005071 inc_cursor();
5072#ifdef FEAT_RIGHTLEFT
5073 else if (State & REPLACE_FLAG)
5074 dec_cursor();
5075#endif
5076 break;
5077 }
5078 if (State & REPLACE_FLAG)
5079 replace_do_bs(-1);
5080 else
5081 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005082 if (enc_utf8 && p_deco)
5083 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005084 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005085 /*
5086 * If there are combining characters and 'delcombine' is set
5087 * move the cursor back. Don't back up before the base
5088 * character.
5089 */
5090 if (enc_utf8 && p_deco && cpc[0] != NUL)
5091 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005092#ifdef FEAT_RIGHTLEFT
5093 if (revins_chars)
5094 {
5095 revins_chars--;
5096 revins_legal++;
5097 }
5098 if (revins_on && gchar_cursor() == NUL)
5099 break;
5100#endif
5101 }
5102 /* Just a single backspace?: */
5103 if (mode == BACKSPACE_CHAR)
5104 break;
5105 } while (
5106#ifdef FEAT_RIGHTLEFT
5107 revins_on ||
5108#endif
5109 (curwin->w_cursor.col > mincol
5110 && (curwin->w_cursor.lnum != Insstart_orig.lnum
5111 || curwin->w_cursor.col != Insstart_orig.col)));
5112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 did_backspace = TRUE;
5114 }
5115#ifdef FEAT_SMARTINDENT
5116 did_si = FALSE;
5117 can_si = FALSE;
5118 can_si_back = FALSE;
5119#endif
5120 if (curwin->w_cursor.col <= 1)
5121 did_ai = FALSE;
5122 /*
5123 * It's a little strange to put backspaces into the redo
5124 * buffer, but it makes auto-indent a lot easier to deal
5125 * with.
5126 */
5127 AppendCharToRedobuff(c);
5128
5129 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02005130 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005131 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02005132 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 /* vi behaviour: the cursor moves backward but the character that
5135 * was there remains visible
5136 * Vim behaviour: the cursor moves backward and the character that
5137 * was there is erased from the screen.
5138 * We can emulate the vi behaviour by pretending there is a dollar
5139 * displayed even when there isn't.
5140 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005141 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 dollar_vcol = curwin->w_virtcol;
5143
Bram Moolenaarce3be472008-01-14 19:12:28 +00005144#ifdef FEAT_FOLDING
5145 /* When deleting a char the cursor line must never be in a closed fold.
5146 * E.g., when 'foldmethod' is indent and deleting the first non-white
5147 * char before a Tab. */
5148 if (did_backspace)
5149 foldOpenCursor();
5150#endif
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 return did_backspace;
5153}
5154
5155#ifdef FEAT_MOUSE
5156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005157ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158{
5159 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005160 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
5162# ifdef FEAT_GUI
5163 /* When GUI is active, also move/paste when 'mouse' is empty */
5164 if (!gui.in_use)
5165# endif
5166 if (!mouse_has(MOUSE_INSERT))
5167 return;
5168
5169 undisplay_dollar();
5170 tpos = curwin->w_cursor;
5171 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
5172 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005173 win_T *new_curwin = curwin;
5174
5175 if (curwin != old_curwin && win_valid(old_curwin))
5176 {
5177 /* Mouse took us to another window. We need to go back to the
5178 * previous one to stop insert there properly. */
5179 curwin = old_curwin;
5180 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005181#ifdef FEAT_JOB_CHANNEL
5182 if (bt_prompt(curbuf))
5183 // Restart Insert mode when re-entering the prompt buffer.
5184 curbuf->b_prompt_insert = 'A';
5185#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005186 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005187 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005188 if (curwin != new_curwin && win_valid(new_curwin))
5189 {
5190 curwin = new_curwin;
5191 curbuf = curwin->w_buffer;
5192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193# ifdef FEAT_CINDENT
5194 can_cindent = TRUE;
5195# endif
5196 }
5197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 /* redraw status lines (in case another window became active) */
5199 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200}
5201
5202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005203ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204{
5205 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005206 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005207 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
5209 tpos = curwin->w_cursor;
5210
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01005211 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
5213 int row, col;
5214
5215 row = mouse_row;
5216 col = mouse_col;
5217
5218 /* find the window at the pointer coordinates */
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02005219 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005220 if (wp == NULL)
5221 return;
5222 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 curbuf = curwin->w_buffer;
5224 }
5225 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 undisplay_dollar();
5227
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005228 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005229 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005230 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005231 if (dir == MSCR_DOWN || dir == MSCR_UP)
5232 {
5233 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5234 scroll_redraw(dir,
5235 (long)(curwin->w_botline - curwin->w_topline));
5236 else
5237 scroll_redraw(dir, 3L);
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02005238# ifdef FEAT_TEXT_PROP
5239 if (WIN_IS_POPUP(curwin))
5240 popup_set_firstline(curwin);
5241# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005242 }
5243#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005244 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005245 {
5246 int val, step = 6;
5247
5248 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02005249 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005250 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
5251 if (val < 0)
5252 val = 0;
5253 gui_do_horiz_scroll(val, TRUE);
5254 }
5255#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005256 did_scroll = TRUE;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 curwin->w_redr_status = TRUE;
5260
5261 curwin = old_curwin;
5262 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005264 /* The popup menu may overlay the window, need to redraw it.
5265 * TODO: Would be more efficient to only redraw the windows that are
5266 * overlapped by the popup menu. */
5267 if (pum_visible() && did_scroll)
5268 {
5269 redraw_all_later(NOT_VALID);
5270 ins_compl_show_pum();
5271 }
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005272
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005273 if (!EQUAL_POS(curwin->w_cursor, tpos))
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
Bram Moolenaarec2da362017-01-21 20:04:22 +01005283/*
5284 * Handle receiving P_PS: start paste mode. Inserts the following text up to
5285 * P_PE literally.
5286 * When "drop" is TRUE then consume the text and drop it.
5287 */
5288 int
5289bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
5290{
5291 int c;
5292 char_u buf[NUMBUFLEN + MB_MAXBYTES];
5293 int idx = 0;
5294 char_u *end = find_termcode((char_u *)"PE");
5295 int ret_char = -1;
5296 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005297 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01005298
5299 /* If the end code is too long we can't detect it, read everything. */
5300 if (STRLEN(end) >= NUMBUFLEN)
5301 end = NULL;
5302 ++no_mapping;
5303 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005304 if (!p_paste)
5305 // Also have the side effects of setting 'paste' to make it work much
5306 // faster.
5307 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005308
Bram Moolenaarec2da362017-01-21 20:04:22 +01005309 for (;;)
5310 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005311 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01005312 if (end == NULL && vpeekc() == NUL)
5313 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005314 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005315 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01005316 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005317 if (c == NUL || got_int)
5318 // When CTRL-C was encountered the typeahead will be flushed and we
5319 // won't get the end sequence.
5320 break;
5321
Bram Moolenaarec2da362017-01-21 20:04:22 +01005322 if (has_mbyte)
5323 idx += (*mb_char2bytes)(c, buf + idx);
5324 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005325 buf[idx++] = c;
5326 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02005327 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01005328 {
5329 if (end[idx] == NUL)
5330 break; /* Found the end of paste code. */
5331 continue;
5332 }
5333 if (!drop)
5334 {
5335 switch (mode)
5336 {
5337 case PASTE_CMDLINE:
5338 put_on_cmdline(buf, idx, TRUE);
5339 break;
5340
5341 case PASTE_EX:
5342 if (gap != NULL && ga_grow(gap, idx) == OK)
5343 {
5344 mch_memmove((char *)gap->ga_data + gap->ga_len,
5345 buf, (size_t)idx);
5346 gap->ga_len += idx;
5347 }
5348 break;
5349
5350 case PASTE_INSERT:
5351 if (stop_arrow() == OK)
5352 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005353 c = buf[0];
5354 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
5355 ins_eol(c);
5356 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01005357 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005358 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01005359 AppendToRedobuffLit(buf, idx);
5360 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01005361 }
5362 break;
5363
5364 case PASTE_ONE_CHAR:
5365 if (ret_char == -1)
5366 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01005367 if (has_mbyte)
5368 ret_char = (*mb_ptr2char)(buf);
5369 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005370 ret_char = buf[0];
5371 }
5372 break;
5373 }
5374 }
5375 idx = 0;
5376 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005377
Bram Moolenaarec2da362017-01-21 20:04:22 +01005378 --no_mapping;
5379 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005380 if (!save_paste)
5381 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01005382
5383 return ret_char;
5384}
5385
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005386#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00005387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005388ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005389{
5390 /* We will be leaving the current window, unless closing another tab. */
5391 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
5392 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
5393 {
5394 undisplay_dollar();
5395 start_arrow(&curwin->w_cursor);
5396# ifdef FEAT_CINDENT
5397 can_cindent = TRUE;
5398# endif
5399 }
5400
5401 if (c == K_TABLINE)
5402 goto_tabpage(current_tab);
5403 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005404 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005405 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005406 redraw_statuslines(); /* will redraw the tabline when needed */
5407 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005408}
5409#endif
5410
5411#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005413ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 pos_T tpos;
5416
5417 undisplay_dollar();
5418 tpos = curwin->w_cursor;
5419 if (gui_do_scroll())
5420 {
5421 start_arrow(&tpos);
5422# ifdef FEAT_CINDENT
5423 can_cindent = TRUE;
5424# endif
5425 }
5426}
5427
5428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005429ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 pos_T tpos;
5432
5433 undisplay_dollar();
5434 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005435 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {
5437 start_arrow(&tpos);
5438# ifdef FEAT_CINDENT
5439 can_cindent = TRUE;
5440# endif
5441 }
5442}
5443#endif
5444
5445 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005446ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005449 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451#ifdef FEAT_FOLDING
5452 if ((fdo_flags & FDO_HOR) && KeyTyped)
5453 foldOpenCursor();
5454#endif
5455 undisplay_dollar();
5456 tpos = curwin->w_cursor;
5457 if (oneleft() == OK)
5458 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005459#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5460 /* Only call start_arrow() when not busy with preediting, it will
5461 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005462 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005463#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005464 {
5465 start_arrow_with_change(&tpos, end_change);
5466 if (!end_change)
5467 AppendCharToRedobuff(K_LEFT);
5468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469#ifdef FEAT_RIGHTLEFT
5470 /* If exit reversed string, position is fixed */
5471 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5472 revins_legal++;
5473 revins_chars++;
5474#endif
5475 }
5476
5477 /*
5478 * if 'whichwrap' set for cursor in insert mode may go to
5479 * previous line
5480 */
5481 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5482 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005483 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 start_arrow(&tpos);
5485 --(curwin->w_cursor.lnum);
5486 coladvance((colnr_T)MAXCOL);
5487 curwin->w_set_curswant = TRUE; /* so we stay at the end */
5488 }
5489 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005490 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005491 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492}
5493
5494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005495ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
5497 pos_T tpos;
5498
5499#ifdef FEAT_FOLDING
5500 if ((fdo_flags & FDO_HOR) && KeyTyped)
5501 foldOpenCursor();
5502#endif
5503 undisplay_dollar();
5504 tpos = curwin->w_cursor;
5505 if (c == K_C_HOME)
5506 curwin->w_cursor.lnum = 1;
5507 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 curwin->w_curswant = 0;
5510 start_arrow(&tpos);
5511}
5512
5513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005514ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 pos_T tpos;
5517
5518#ifdef FEAT_FOLDING
5519 if ((fdo_flags & FDO_HOR) && KeyTyped)
5520 foldOpenCursor();
5521#endif
5522 undisplay_dollar();
5523 tpos = curwin->w_cursor;
5524 if (c == K_C_END)
5525 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5526 coladvance((colnr_T)MAXCOL);
5527 curwin->w_curswant = MAXCOL;
5528
5529 start_arrow(&tpos);
5530}
5531
5532 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005533ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005535 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536#ifdef FEAT_FOLDING
5537 if ((fdo_flags & FDO_HOR) && KeyTyped)
5538 foldOpenCursor();
5539#endif
5540 undisplay_dollar();
5541 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5542 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005543 start_arrow_with_change(&curwin->w_cursor, end_change);
5544 if (!end_change)
5545 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 (void)bck_word(1L, FALSE, FALSE);
5547 curwin->w_set_curswant = TRUE;
5548 }
5549 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005550 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005551 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552}
5553
5554 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005555ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005557 int end_change = dont_sync_undo == FALSE; // end undoable change
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559#ifdef FEAT_FOLDING
5560 if ((fdo_flags & FDO_HOR) && KeyTyped)
5561 foldOpenCursor();
5562#endif
5563 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005564 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005566 start_arrow_with_change(&curwin->w_cursor, end_change);
5567 if (!end_change)
5568 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 if (virtual_active())
5571 oneright();
5572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005575 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 ++curwin->w_cursor.col;
5578 }
5579
5580#ifdef FEAT_RIGHTLEFT
5581 revins_legal++;
5582 if (revins_chars)
5583 revins_chars--;
5584#endif
5585 }
5586 /* if 'whichwrap' set for cursor in insert mode, may move the
5587 * cursor to the next line */
5588 else if (vim_strchr(p_ww, ']') != NULL
5589 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5590 {
5591 start_arrow(&curwin->w_cursor);
5592 curwin->w_set_curswant = TRUE;
5593 ++curwin->w_cursor.lnum;
5594 curwin->w_cursor.col = 0;
5595 }
5596 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005597 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005598 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599}
5600
5601 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005602ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005604 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605#ifdef FEAT_FOLDING
5606 if ((fdo_flags & FDO_HOR) && KeyTyped)
5607 foldOpenCursor();
5608#endif
5609 undisplay_dollar();
5610 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5611 || gchar_cursor() != NUL)
5612 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005613 start_arrow_with_change(&curwin->w_cursor, end_change);
5614 if (!end_change)
5615 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 (void)fwd_word(1L, FALSE, 0);
5617 curwin->w_set_curswant = TRUE;
5618 }
5619 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005620 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005621 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622}
5623
5624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005625ins_up(
5626 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 pos_T tpos;
5629 linenr_T old_topline = curwin->w_topline;
5630#ifdef FEAT_DIFF
5631 int old_topfill = curwin->w_topfill;
5632#endif
5633
5634 undisplay_dollar();
5635 tpos = curwin->w_cursor;
5636 if (cursor_up(1L, TRUE) == OK)
5637 {
5638 if (startcol)
5639 coladvance(getvcol_nolist(&Insstart));
5640 if (old_topline != curwin->w_topline
5641#ifdef FEAT_DIFF
5642 || old_topfill != curwin->w_topfill
5643#endif
5644 )
5645 redraw_later(VALID);
5646 start_arrow(&tpos);
5647#ifdef FEAT_CINDENT
5648 can_cindent = TRUE;
5649#endif
5650 }
5651 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005652 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653}
5654
5655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005656ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 pos_T tpos;
5659
5660 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005661
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005662 if (mod_mask & MOD_MASK_CTRL)
5663 {
5664 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005665 if (first_tabpage->tp_next != NULL)
5666 {
5667 start_arrow(&curwin->w_cursor);
5668 goto_tabpage(-1);
5669 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005670 return;
5671 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005672
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 tpos = curwin->w_cursor;
5674 if (onepage(BACKWARD, 1L) == OK)
5675 {
5676 start_arrow(&tpos);
5677#ifdef FEAT_CINDENT
5678 can_cindent = TRUE;
5679#endif
5680 }
5681 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005682 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683}
5684
5685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005686ins_down(
5687 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 pos_T tpos;
5690 linenr_T old_topline = curwin->w_topline;
5691#ifdef FEAT_DIFF
5692 int old_topfill = curwin->w_topfill;
5693#endif
5694
5695 undisplay_dollar();
5696 tpos = curwin->w_cursor;
5697 if (cursor_down(1L, TRUE) == OK)
5698 {
5699 if (startcol)
5700 coladvance(getvcol_nolist(&Insstart));
5701 if (old_topline != curwin->w_topline
5702#ifdef FEAT_DIFF
5703 || old_topfill != curwin->w_topfill
5704#endif
5705 )
5706 redraw_later(VALID);
5707 start_arrow(&tpos);
5708#ifdef FEAT_CINDENT
5709 can_cindent = TRUE;
5710#endif
5711 }
5712 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005713 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714}
5715
5716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005717ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 pos_T tpos;
5720
5721 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005722
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005723 if (mod_mask & MOD_MASK_CTRL)
5724 {
5725 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005726 if (first_tabpage->tp_next != NULL)
5727 {
5728 start_arrow(&curwin->w_cursor);
5729 goto_tabpage(0);
5730 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005731 return;
5732 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005733
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 tpos = curwin->w_cursor;
5735 if (onepage(FORWARD, 1L) == OK)
5736 {
5737 start_arrow(&tpos);
5738#ifdef FEAT_CINDENT
5739 can_cindent = TRUE;
5740#endif
5741 }
5742 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005743 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744}
5745
5746#ifdef FEAT_DND
5747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005748ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5751}
5752#endif
5753
5754/*
5755 * Handle TAB in Insert or Replace mode.
5756 * Return TRUE when the TAB needs to be inserted like a normal character.
5757 */
5758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005759ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
5761 int ind;
5762 int i;
5763 int temp;
5764
5765 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5766 Insstart_blank_vcol = get_nolist_virtcol();
5767 if (echeck_abbr(TAB + ABBR_OFF))
5768 return FALSE;
5769
5770 ind = inindent(0);
5771#ifdef FEAT_CINDENT
5772 if (ind)
5773 can_cindent = FALSE;
5774#endif
5775
5776 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005777 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 */
5779 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005780#ifdef FEAT_VARTABS
5781 && !(p_sta && ind
5782 /* These five lines mean 'tabstop' != 'shiftwidth' */
5783 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5784 || (tabstop_count(curbuf->b_p_vts_array) == 1
5785 && tabstop_first(curbuf->b_p_vts_array)
5786 != get_sw_value(curbuf))
5787 || (tabstop_count(curbuf->b_p_vts_array) == 0
5788 && curbuf->b_p_ts != get_sw_value(curbuf))))
5789 && tabstop_count(curbuf->b_p_vsts_array) == 0
5790#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005791 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005792#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005793 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 return TRUE;
5795
5796 if (stop_arrow() == FAIL)
5797 return TRUE;
5798
5799 did_ai = FALSE;
5800#ifdef FEAT_SMARTINDENT
5801 did_si = FALSE;
5802 can_si = FALSE;
5803 can_si_back = FALSE;
5804#endif
5805 AppendToRedobuff((char_u *)"\t");
5806
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005807#ifdef FEAT_VARTABS
5808 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
5809 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005810 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005811 temp -= get_nolist_virtcol() % temp;
5812 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005813 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005814 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005815 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005816 curbuf->b_p_vsts_array);
5817 else /* otherwise use 'tabstop' */
5818 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5819 curbuf->b_p_vts_array);
5820#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005822 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005823 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
5824 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 else /* otherwise use 'tabstop' */
5826 temp = (int)curbuf->b_p_ts;
5827 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
5830 /*
5831 * Insert the first space with ins_char(). It will delete one char in
5832 * replace mode. Insert the rest with ins_str(); it will not delete any
5833 * chars. For VREPLACE mode, we use ins_char() for all characters.
5834 */
5835 ins_char(' ');
5836 while (--temp > 0)
5837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 if (State & VREPLACE_FLAG)
5839 ins_char(' ');
5840 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
5842 ins_str((char_u *)" ");
5843 if (State & REPLACE_FLAG) /* no char replaced */
5844 replace_push(NUL);
5845 }
5846 }
5847
5848 /*
5849 * When 'expandtab' not set: Replace spaces by TABs where possible.
5850 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005851#ifdef FEAT_VARTABS
5852 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5853 || get_sts_value() > 0
5854 || (p_sta && ind)))
5855#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005856 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 {
5859 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 char_u *saved_line = NULL; /* init for GCC */
5861 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 pos_T fpos;
5863 pos_T *cursor;
5864 colnr_T want_vcol, vcol;
5865 int change_col = -1;
5866 int save_list = curwin->w_p_list;
5867
5868 /*
5869 * Get the current line. For VREPLACE mode, don't make real changes
5870 * yet, just work on a copy of the line.
5871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 if (State & VREPLACE_FLAG)
5873 {
5874 pos = curwin->w_cursor;
5875 cursor = &pos;
5876 saved_line = vim_strsave(ml_get_curline());
5877 if (saved_line == NULL)
5878 return FALSE;
5879 ptr = saved_line + pos.col;
5880 }
5881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
5883 ptr = ml_get_cursor();
5884 cursor = &curwin->w_cursor;
5885 }
5886
5887 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
5888 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5889 curwin->w_p_list = FALSE;
5890
5891 /* Find first white before the cursor */
5892 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005893 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {
5895 --fpos.col;
5896 --ptr;
5897 }
5898
5899 /* In Replace mode, don't change characters before the insert point. */
5900 if ((State & REPLACE_FLAG)
5901 && fpos.lnum == Insstart.lnum
5902 && fpos.col < Insstart.col)
5903 {
5904 ptr += Insstart.col - fpos.col;
5905 fpos.col = Insstart.col;
5906 }
5907
5908 /* compute virtual column numbers of first white and cursor */
5909 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5910 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5911
Bram Moolenaar597a4222014-06-25 14:39:50 +02005912 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5913 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01005914 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005916 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 if (vcol + i > want_vcol)
5918 break;
5919 if (*ptr != TAB)
5920 {
5921 *ptr = TAB;
5922 if (change_col < 0)
5923 {
5924 change_col = fpos.col; /* Column of first change */
5925 /* May have to adjust Insstart */
5926 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5927 Insstart.col = fpos.col;
5928 }
5929 }
5930 ++fpos.col;
5931 ++ptr;
5932 vcol += i;
5933 }
5934
5935 if (change_col >= 0)
5936 {
5937 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005938 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
5940 /* Skip over the spaces we need. */
5941 while (vcol < want_vcol && *ptr == ' ')
5942 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005943 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 ++ptr;
5945 ++repl_off;
5946 }
5947 if (vcol > want_vcol)
5948 {
5949 /* Must have a char with 'showbreak' just before it. */
5950 --ptr;
5951 --repl_off;
5952 }
5953 fpos.col += repl_off;
5954
5955 /* Delete following spaces. */
5956 i = cursor->col - fpos.col;
5957 if (i > 0)
5958 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005959 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005961 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 for (temp = i; --temp >= 0; )
5963 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005964#ifdef FEAT_TEXT_PROP
5965 curbuf->b_ml.ml_line_len -= i;
5966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005968#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005969 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005970 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005971 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005972 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5973 (char_u *)"\t", 1);
5974 }
5975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 cursor->col -= i;
5977
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 /*
5979 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5980 * backspacing over the changed spacing and then inserting the new
5981 * spacing.
5982 */
5983 if (State & VREPLACE_FLAG)
5984 {
5985 /* Backspace from real cursor to change_col */
5986 backspace_until_column(change_col);
5987
5988 /* Insert each char in saved_line from changed_col to
5989 * ptr-cursor */
5990 ins_bytes_len(saved_line + change_col,
5991 cursor->col - change_col);
5992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 }
5994
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 if (State & VREPLACE_FLAG)
5996 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 curwin->w_p_list = save_list;
5998 }
5999
6000 return FALSE;
6001}
6002
6003/*
6004 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02006005 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 int i;
6011
6012 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02006013 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02006015 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 undisplay_dollar();
6017
6018 /*
6019 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
6020 * character under the cursor. Only push a NUL on the replace stack,
6021 * nothing to put back when the NL is deleted.
6022 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006023 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 replace_push(NUL);
6025
6026 /*
6027 * In VREPLACE mode, a NL replaces the rest of the line, and starts
6028 * replacing the next line, so we push all of the characters left on the
6029 * line onto the replace stack. This is not done here though, it is done
6030 * in open_line().
6031 */
6032
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006033 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
6034 * CTRL-O). */
6035 if (virtual_active() && curwin->w_cursor.coladd > 0)
6036 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006037
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 /* NL in reverse insert will always start in the end of
6040 * current line. */
6041 if (revins_on)
6042 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6043#endif
6044
6045 AppendToRedobuff(NL_STR);
6046 i = open_line(FORWARD,
6047#ifdef FEAT_COMMENTS
6048 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
6049#endif
6050 0, old_indent);
6051 old_indent = 0;
6052#ifdef FEAT_CINDENT
6053 can_cindent = TRUE;
6054#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00006055#ifdef FEAT_FOLDING
6056 /* When inserting a line the cursor line must never be in a closed fold. */
6057 foldOpenCursor();
6058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
Bram Moolenaar24a2d722018-04-24 19:36:43 +02006060 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061}
6062
6063#ifdef FEAT_DIGRAPHS
6064/*
6065 * Handle digraph in insert mode.
6066 * Returns character still to be inserted, or NUL when nothing remaining to be
6067 * done.
6068 */
6069 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006070ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 int c;
6073 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006074 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
6076 pc_status = PC_STATUS_UNSET;
6077 if (redrawing() && !char_avail())
6078 {
6079 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006080 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
6082 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006083 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084#ifdef FEAT_CMDL_INFO
6085 add_to_showcmd_c(Ctrl_K);
6086#endif
6087 }
6088
6089#ifdef USE_ON_FLY_SCROLL
6090 dont_scroll = TRUE; /* disallow scrolling here */
6091#endif
6092
6093 /* don't map the digraph chars. This also prevents the
6094 * mode message to be deleted when ESC is hit */
6095 ++no_mapping;
6096 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006097 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 --no_mapping;
6099 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006100 if (did_putchar)
6101 /* when the line fits in 'columns' the '?' is at the start of the next
6102 * line and will not be removed by the redraw */
6103 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02006104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (IS_SPECIAL(c) || mod_mask) /* special key */
6106 {
6107#ifdef FEAT_CMDL_INFO
6108 clear_showcmd();
6109#endif
6110 insert_special(c, TRUE, FALSE);
6111 return NUL;
6112 }
6113 if (c != ESC)
6114 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006115 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 if (redrawing() && !char_avail())
6117 {
6118 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006119 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121 if (char2cells(c) == 1)
6122 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00006123 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006125 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 }
6127#ifdef FEAT_CMDL_INFO
6128 add_to_showcmd_c(c);
6129#endif
6130 }
6131 ++no_mapping;
6132 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006133 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 --no_mapping;
6135 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006136 if (did_putchar)
6137 /* when the line fits in 'columns' the '?' is at the start of the
6138 * next line and will not be removed by a redraw */
6139 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (cc != ESC)
6141 {
6142 AppendToRedobuff((char_u *)CTRL_V_STR);
6143 c = getdigraph(c, cc, TRUE);
6144#ifdef FEAT_CMDL_INFO
6145 clear_showcmd();
6146#endif
6147 return c;
6148 }
6149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150#ifdef FEAT_CMDL_INFO
6151 clear_showcmd();
6152#endif
6153 return NUL;
6154}
6155#endif
6156
6157/*
6158 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
6159 * Returns the char to be inserted, or NUL if none found.
6160 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02006161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006162ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
6164 int c;
6165 int temp;
6166 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006167 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168
6169 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6170 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02006171 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 return NUL;
6173 }
6174
6175 /* try to advance to the cursor column */
6176 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006177 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 prev_ptr = ptr;
6179 validate_virtcol();
6180 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
6181 {
6182 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006183 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
6185 if ((colnr_T)temp > curwin->w_virtcol)
6186 ptr = prev_ptr;
6187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006190 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 return c;
6192}
6193
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006194/*
6195 * CTRL-Y or CTRL-E typed in Insert mode.
6196 */
6197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006198ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006199{
6200 int c = tc;
6201
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006202 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006203 {
6204 if (c == Ctrl_Y)
6205 scrolldown_clamp();
6206 else
6207 scrollup_clamp();
6208 redraw_later(VALID);
6209 }
6210 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006211 {
6212 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
6213 if (c != NUL)
6214 {
6215 long tw_save;
6216
6217 /* The character must be taken literally, insert like it
6218 * was typed after a CTRL-V, and pretend 'textwidth'
6219 * wasn't set. Digits, 'o' and 'x' are special after a
6220 * CTRL-V, don't use it for these. */
6221 if (c < 256 && !isalnum(c))
6222 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
6223 tw_save = curbuf->b_p_tw;
6224 curbuf->b_p_tw = -1;
6225 insert_special(c, TRUE, FALSE);
6226 curbuf->b_p_tw = tw_save;
6227#ifdef FEAT_RIGHTLEFT
6228 revins_chars++;
6229 revins_legal++;
6230#endif
6231 c = Ctrl_V; /* pretend CTRL-V is last character */
6232 auto_format(FALSE, TRUE);
6233 }
6234 }
6235 return c;
6236}
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238#ifdef FEAT_SMARTINDENT
6239/*
6240 * Try to do some very smart auto-indenting.
6241 * Used when inserting a "normal" character.
6242 */
6243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006244ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 pos_T *pos, old_pos;
6247 char_u *ptr;
6248 int i;
6249 int temp;
6250
6251 /*
6252 * do some very smart indenting when entering '{' or '}'
6253 */
6254 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
6255 {
6256 /*
6257 * for '}' set indent equal to indent of line containing matching '{'
6258 */
6259 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
6260 {
6261 old_pos = curwin->w_cursor;
6262 /*
6263 * If the matching '{' has a ')' immediately before it (ignoring
6264 * white-space), then line up with the start of the line
6265 * containing the matching '(' if there is one. This handles the
6266 * case where an "if (..\n..) {" statement continues over multiple
6267 * lines -- webb
6268 */
6269 ptr = ml_get(pos->lnum);
6270 i = pos->col;
6271 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006272 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 ;
6274 curwin->w_cursor.lnum = pos->lnum;
6275 curwin->w_cursor.col = i;
6276 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
6277 curwin->w_cursor = *pos;
6278 i = get_indent();
6279 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006281 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 (void)set_indent(i, SIN_CHANGED);
6284 }
6285 else if (curwin->w_cursor.col > 0)
6286 {
6287 /*
6288 * when inserting '{' after "O" reduce indent, but not
6289 * more than indent of previous line
6290 */
6291 temp = TRUE;
6292 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
6293 {
6294 old_pos = curwin->w_cursor;
6295 i = get_indent();
6296 while (curwin->w_cursor.lnum > 1)
6297 {
6298 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
6299
6300 /* ignore empty lines and lines starting with '#'. */
6301 if (*ptr != '#' && *ptr != NUL)
6302 break;
6303 }
6304 if (get_indent() >= i)
6305 temp = FALSE;
6306 curwin->w_cursor = old_pos;
6307 }
6308 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006309 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 }
6311 }
6312
6313 /*
6314 * set indent of '#' always to 0
6315 */
6316 if (curwin->w_cursor.col > 0 && can_si && c == '#')
6317 {
6318 /* remember current indent for next line */
6319 old_indent = get_indent();
6320 (void)set_indent(0, SIN_CHANGED);
6321 }
6322
6323 /* Adjust ai_col, the char at this position can be deleted. */
6324 if (ai_col > curwin->w_cursor.col)
6325 ai_col = curwin->w_cursor.col;
6326}
6327#endif
6328
6329/*
6330 * Get the value that w_virtcol would have when 'list' is off.
6331 * Unless 'cpo' contains the 'L' flag.
6332 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01006333 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006334get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
Bram Moolenaarf9514162018-11-22 03:08:29 +01006336 // check validity of cursor in current buffer
6337 if (curwin->w_buffer == NULL
6338 || curwin->w_buffer->b_ml.ml_mfp == NULL
6339 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
6340 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
6342 return getvcol_nolist(&curwin->w_cursor);
6343 validate_virtcol();
6344 return curwin->w_virtcol;
6345}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006346
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006347#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006348/*
6349 * Handle the InsertCharPre autocommand.
6350 * "c" is the character that was typed.
6351 * Return a pointer to allocated memory with the replacement string.
6352 * Return NULL to continue inserting "c".
6353 */
6354 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006355do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006356{
Bram Moolenaar704984a2012-06-01 14:57:51 +02006357 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02006358 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01006359 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006360
6361 /* Return quickly when there is nothing to do. */
6362 if (!has_insertcharpre())
6363 return NULL;
6364
Bram Moolenaar704984a2012-06-01 14:57:51 +02006365 if (has_mbyte)
6366 buf[(*mb_char2bytes)(c, buf)] = NUL;
6367 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02006368 {
6369 buf[0] = c;
6370 buf[1] = NUL;
6371 }
6372
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006373 /* Lock the text to avoid weird things from happening. */
6374 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +02006375 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006376
Bram Moolenaar704984a2012-06-01 14:57:51 +02006377 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006378 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02006379 {
6380 /* Get the value of v:char. It may be empty or more than one
6381 * character. Only use it when changed, otherwise continue with the
6382 * original character to avoid breaking autoindent. */
6383 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
6384 res = vim_strsave(get_vim_var_str(VV_CHAR));
6385 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006386
6387 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
6388 --textlock;
6389
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01006390 // Restore the State, it may have been changed.
6391 State = save_State;
6392
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006393 return res;
6394}
6395#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006396
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006397#if defined(FEAT_CINDENT) || defined(PROTO)
6398 int
6399can_cindent_get(void)
6400{
6401 return can_cindent;
6402}
6403#endif
6404
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006405/*
6406 * Trigger "event" and take care of fixing undo.
6407 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006408 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006409ins_apply_autocmds(event_T event)
6410{
6411 varnumber_T tick = CHANGEDTICK(curbuf);
6412 int r;
6413
6414 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
6415
6416 // If u_savesub() was called then we are not prepared to start
6417 // a new line. Call u_save() with no contents to fix that.
6418 if (tick != CHANGEDTICK(curbuf))
6419 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
6420
6421 return r;
6422}