blob: 67141448fa1b0ab17f3f9a5181cf8dd0633bebfc [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 Moolenaar8aeec402019-09-15 23:02:04 +02001512 // Trigger SafeState if nothing is pending.
1513 may_trigger_safestate(ready
1514 && !ins_compl_active()
1515 && !pum_visible());
1516
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001517#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001518 if ((conceal_update_lines
1519 && (conceal_old_cursor_line != conceal_new_cursor_line
1520 || conceal_cursor_line(curwin)))
1521 || need_cursor_line_redraw)
1522 {
1523 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001524 redrawWinline(curwin, conceal_old_cursor_line);
1525 redrawWinline(curwin, conceal_new_cursor_line == 0
1526 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001527 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001528 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001529 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001530#endif
1531 if (must_redraw)
1532 update_screen(0);
1533 else if (clear_cmdline || redraw_cmdline)
1534 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001535 showruler(FALSE);
1536 setcursor();
1537 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538}
1539
1540/*
1541 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1542 */
1543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
1546 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001547 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
1549 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001550 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001555 did_putchar = TRUE;
1556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1558
1559#ifdef FEAT_CMDL_INFO
1560 add_to_showcmd_c(Ctrl_V);
1561#endif
1562
1563 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001564 if (did_putchar)
1565 /* when the line fits in 'columns' the '^' is at the start of the next
1566 * line and will not removed by the redraw */
1567 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568#ifdef FEAT_CMDL_INFO
1569 clear_showcmd();
1570#endif
1571 insert_special(c, FALSE, TRUE);
1572#ifdef FEAT_RIGHTLEFT
1573 revins_chars++;
1574 revins_legal++;
1575#endif
1576}
1577
1578/*
1579 * Put a character directly onto the screen. It's not stored in a buffer.
1580 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1581 */
1582static int pc_status;
1583#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1584#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1585#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1586#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588static int pc_attr;
1589static int pc_row;
1590static int pc_col;
1591
1592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001593edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
1595 int attr;
1596
1597 if (ScreenLines != NULL)
1598 {
1599 update_topline(); /* just in case w_topline isn't valid */
1600 validate_cursor();
1601 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001602 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 else
1604 attr = 0;
1605 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001606 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608#ifdef FEAT_RIGHTLEFT
1609 if (curwin->w_p_rl)
1610 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001611 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (has_mbyte)
1613 {
1614 int fix_col = mb_fix_col(pc_col, pc_row);
1615
1616 if (fix_col != pc_col)
1617 {
1618 screen_putchar(' ', pc_row, fix_col, attr);
1619 --curwin->w_wcol;
1620 pc_status = PC_STATUS_RIGHT;
1621 }
1622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
1624 else
1625#endif
1626 {
1627 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (mb_lefthalve(pc_row, pc_col))
1629 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 }
1631
1632 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
1635 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1636 pc_status = PC_STATUS_SET;
1637 }
1638 screen_putchar(c, pc_row, pc_col, attr);
1639 }
1640}
1641
Bram Moolenaarf2732452018-06-03 14:47:35 +02001642#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1643/*
1644 * Return the effective prompt for the current buffer.
1645 */
1646 char_u *
1647prompt_text(void)
1648{
1649 if (curbuf->b_prompt_text == NULL)
1650 return (char_u *)"% ";
1651 return curbuf->b_prompt_text;
1652}
1653
1654/*
1655 * Prepare for prompt mode: Make sure the last line has the prompt text.
1656 * Move the cursor to this line.
1657 */
1658 static void
1659init_prompt(int cmdchar_todo)
1660{
1661 char_u *prompt = prompt_text();
1662 char_u *text;
1663
1664 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1665 text = ml_get_curline();
1666 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1667 {
1668 // prompt is missing, insert it or append a line with it
1669 if (*text == NUL)
1670 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1671 else
1672 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1673 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1674 coladvance((colnr_T)MAXCOL);
1675 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1676 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001677
1678 // Insert always starts after the prompt, allow editing text after it.
1679 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1680 || Insstart_orig.col != (int)STRLEN(prompt))
1681 {
1682 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001683 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001684 Insstart_orig = Insstart;
1685 Insstart_textlen = Insstart.col;
1686 Insstart_blank_vcol = MAXCOL;
1687 arrow_used = FALSE;
1688 }
1689
Bram Moolenaarf2732452018-06-03 14:47:35 +02001690 if (cmdchar_todo == 'A')
1691 coladvance((colnr_T)MAXCOL);
1692 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001693 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001694 /* Make sure the cursor is in a valid position. */
1695 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001696}
1697
1698/*
1699 * Return TRUE if the cursor is in the editable position of the prompt line.
1700 */
1701 int
1702prompt_curpos_editable()
1703{
1704 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1705 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1706}
1707#endif
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709/*
1710 * Undo the previous edit_putchar().
1711 */
1712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001713edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (pc_status == PC_STATUS_RIGHT)
1718 ++curwin->w_wcol;
1719 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001720 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1723 }
1724}
1725
1726/*
1727 * Called when p_dollar is set: display a '$' at the end of the changed text
1728 * Only works when cursor is in the line that changes.
1729 */
1730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001731display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 colnr_T save_col;
1734
1735 if (!redrawing())
1736 return;
1737
1738 cursor_off();
1739 save_col = curwin->w_cursor.col;
1740 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 if (has_mbyte)
1742 {
1743 char_u *p;
1744
1745 /* If on the last byte of a multi-byte move to the first byte. */
1746 p = ml_get_curline();
1747 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001750 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
1752 edit_putchar('$', FALSE);
1753 dollar_vcol = curwin->w_virtcol;
1754 }
1755 curwin->w_cursor.col = save_col;
1756}
1757
1758/*
1759 * Call this function before moving the cursor from the normal insert position
1760 * in insert mode.
1761 */
1762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001765 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001767 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001768 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
1770}
1771
1772/*
1773 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1774 * Keep the cursor on the same character.
1775 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1776 * type == INDENT_DEC decrease indent (for CTRL-D)
1777 * type == INDENT_SET set indent to "amount"
1778 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1779 */
1780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781change_indent(
1782 int type,
1783 int amount,
1784 int round,
1785 int replaced, /* replaced character, put on replace stack */
1786 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 int vcol;
1789 int last_vcol;
1790 int insstart_less; /* reduction for Insstart.col */
1791 int new_cursor_col;
1792 int i;
1793 char_u *ptr;
1794 int save_p_list;
1795 int start_col;
1796 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 colnr_T orig_col = 0; /* init for GCC */
1798 char_u *new_line, *orig_line = NULL; /* init for GCC */
1799
1800 /* VREPLACE mode needs to know what the line was like before changing */
1801 if (State & VREPLACE_FLAG)
1802 {
1803 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1804 orig_col = curwin->w_cursor.col;
1805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 /* for the following tricks we don't want list mode */
1808 save_p_list = curwin->w_p_list;
1809 curwin->w_p_list = FALSE;
1810 vc = getvcol_nolist(&curwin->w_cursor);
1811 vcol = vc;
1812
1813 /*
1814 * For Replace mode we need to fix the replace stack later, which is only
1815 * possible when the cursor is in the indent. Remember the number of
1816 * characters before the cursor if it's possible.
1817 */
1818 start_col = curwin->w_cursor.col;
1819
1820 /* determine offset from first non-blank */
1821 new_cursor_col = curwin->w_cursor.col;
1822 beginline(BL_WHITE);
1823 new_cursor_col -= curwin->w_cursor.col;
1824
1825 insstart_less = curwin->w_cursor.col;
1826
1827 /*
1828 * If the cursor is in the indent, compute how many screen columns the
1829 * cursor is to the left of the first non-blank.
1830 */
1831 if (new_cursor_col < 0)
1832 vcol = get_indent() - vcol;
1833
1834 if (new_cursor_col > 0) /* can't fix replace stack */
1835 start_col = -1;
1836
1837 /*
1838 * Set the new indent. The cursor will be put on the first non-blank.
1839 */
1840 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001841 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 else
1843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 int save_State = State;
1845
1846 /* Avoid being called recursively. */
1847 if (State & VREPLACE_FLAG)
1848 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001849 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
1852 insstart_less -= curwin->w_cursor.col;
1853
1854 /*
1855 * Try to put cursor on same character.
1856 * If the cursor is at or after the first non-blank in the line,
1857 * compute the cursor column relative to the column of the first
1858 * non-blank character.
1859 * If we are not in insert mode, leave the cursor on the first non-blank.
1860 * If the cursor is before the first non-blank, position it relative
1861 * to the first non-blank, counted in screen columns.
1862 */
1863 if (new_cursor_col >= 0)
1864 {
1865 /*
1866 * When changing the indent while the cursor is touching it, reset
1867 * Insstart_col to 0.
1868 */
1869 if (new_cursor_col == 0)
1870 insstart_less = MAXCOL;
1871 new_cursor_col += curwin->w_cursor.col;
1872 }
1873 else if (!(State & INSERT))
1874 new_cursor_col = curwin->w_cursor.col;
1875 else
1876 {
1877 /*
1878 * Compute the screen column where the cursor should be.
1879 */
1880 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001881 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883 /*
1884 * Advance the cursor until we reach the right screen column.
1885 */
1886 vcol = last_vcol = 0;
1887 new_cursor_col = -1;
1888 ptr = ml_get_curline();
1889 while (vcol <= (int)curwin->w_virtcol)
1890 {
1891 last_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001893 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001896 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 vcol = last_vcol;
1899
1900 /*
1901 * May need to insert spaces to be able to position the cursor on
1902 * the right screen column.
1903 */
1904 if (vcol != (int)curwin->w_virtcol)
1905 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001906 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001908 ptr = alloc(i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (ptr != NULL)
1910 {
1911 new_cursor_col += i;
1912 ptr[i] = NUL;
1913 while (--i >= 0)
1914 ptr[i] = ' ';
1915 ins_str(ptr);
1916 vim_free(ptr);
1917 }
1918 }
1919
1920 /*
1921 * When changing the indent while the cursor is in it, reset
1922 * Insstart_col to 0.
1923 */
1924 insstart_less = MAXCOL;
1925 }
1926
1927 curwin->w_p_list = save_p_list;
1928
1929 if (new_cursor_col <= 0)
1930 curwin->w_cursor.col = 0;
1931 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001932 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 curwin->w_set_curswant = TRUE;
1934 changed_cline_bef_curs();
1935
1936 /*
1937 * May have to adjust the start of the insert.
1938 */
1939 if (State & INSERT)
1940 {
1941 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1942 {
1943 if ((int)Insstart.col <= insstart_less)
1944 Insstart.col = 0;
1945 else
1946 Insstart.col -= insstart_less;
1947 }
1948 if ((int)ai_col <= insstart_less)
1949 ai_col = 0;
1950 else
1951 ai_col -= insstart_less;
1952 }
1953
1954 /*
1955 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1956 * If the number of characters before the cursor decreased, need to pop a
1957 * few characters from the replace stack.
1958 * If the number of characters before the cursor increased, need to push a
1959 * few NULs onto the replace stack.
1960 */
1961 if (REPLACE_NORMAL(State) && start_col >= 0)
1962 {
1963 while (start_col > (int)curwin->w_cursor.col)
1964 {
1965 replace_join(0); /* remove a NUL from the replace stack */
1966 --start_col;
1967 }
1968 while (start_col < (int)curwin->w_cursor.col || replaced)
1969 {
1970 replace_push(NUL);
1971 if (replaced)
1972 {
1973 replace_push(replaced);
1974 replaced = NUL;
1975 }
1976 ++start_col;
1977 }
1978 }
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 /*
1981 * For VREPLACE mode, we also have to fix the replace stack. In this case
1982 * it is always possible because we backspace over the whole line and then
1983 * put it back again the way we wanted it.
1984 */
1985 if (State & VREPLACE_FLAG)
1986 {
1987 /* If orig_line didn't allocate, just return. At least we did the job,
1988 * even if you can't backspace. */
1989 if (orig_line == NULL)
1990 return;
1991
1992 /* Save new line */
1993 new_line = vim_strsave(ml_get_curline());
1994 if (new_line == NULL)
1995 return;
1996
1997 /* We only put back the new line up to the cursor */
1998 new_line[curwin->w_cursor.col] = NUL;
1999
2000 /* Put back original line */
2001 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2002 curwin->w_cursor.col = orig_col;
2003
2004 /* Backspace from cursor to start of line */
2005 backspace_until_column(0);
2006
2007 /* Insert new stuff into line again */
2008 ins_bytes(new_line);
2009
2010 vim_free(new_line);
2011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012}
2013
2014/*
2015 * Truncate the space at the end of a line. This is to be used only in an
2016 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2017 * modes.
2018 */
2019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002020truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021{
2022 int i;
2023
2024 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002025 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
2027 if (State & REPLACE_FLAG)
2028 replace_join(0); /* remove a NUL from the replace stack */
2029 }
2030 line[i + 1] = NUL;
2031}
2032
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033/*
2034 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2035 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002036 * Will attempt not to go before "col" even when there is a composing
2037 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 */
2039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002040backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
2042 while ((int)curwin->w_cursor.col > col)
2043 {
2044 curwin->w_cursor.col--;
2045 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002046 replace_do_bs(col);
2047 else if (!del_char_after_col(col))
2048 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002052/*
2053 * Like del_char(), but make sure not to go before column "limit_col".
2054 * Only matters when there are composing characters.
2055 * Return TRUE when something was deleted.
2056 */
2057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002058del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002059{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002060 if (enc_utf8 && limit_col >= 0)
2061 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002062 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002063
2064 /* Make sure the cursor is at the start of a character, but
2065 * skip forward again when going too far back because of a
2066 * composing character. */
2067 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002068 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002069 {
2070 int l = utf_ptr2len(ml_get_cursor());
2071
2072 if (l == 0) /* end of line */
2073 break;
2074 curwin->w_cursor.col += l;
2075 }
2076 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2077 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002078 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002079 }
2080 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002081 (void)del_char(FALSE);
2082 return TRUE;
2083}
2084
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085/*
2086 * Next character is interpreted literally.
2087 * A one, two or three digit decimal number is interpreted as its byte value.
2088 * If one or two digits are entered, the next character is given to vungetc().
2089 * For Unicode a character > 255 may be returned.
2090 */
2091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002092get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093{
2094 int cc;
2095 int nc;
2096 int i;
2097 int hex = FALSE;
2098 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
2101 if (got_int)
2102 return Ctrl_C;
2103
2104#ifdef FEAT_GUI
2105 /*
2106 * In GUI there is no point inserting the internal code for a special key.
2107 * It is more useful to insert the string "<KEY>" instead. This would
2108 * probably be useful in a text window too, but it would not be
2109 * vi-compatible (maybe there should be an option for it?) -- webb
2110 */
2111 if (gui.in_use)
2112 ++allow_keys;
2113#endif
2114#ifdef USE_ON_FLY_SCROLL
2115 dont_scroll = TRUE; /* disallow scrolling here */
2116#endif
2117 ++no_mapping; /* don't map the next key hits */
2118 cc = 0;
2119 i = 0;
2120 for (;;)
2121 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002122 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01002124 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 add_to_showcmd(nc);
2126#endif
2127 if (nc == 'x' || nc == 'X')
2128 hex = TRUE;
2129 else if (nc == 'o' || nc == 'O')
2130 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 else if (nc == 'u' || nc == 'U')
2132 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 else
2134 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002135 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
2137 if (!vim_isxdigit(nc))
2138 break;
2139 cc = cc * 16 + hex2nr(nc);
2140 }
2141 else if (octal)
2142 {
2143 if (nc < '0' || nc > '7')
2144 break;
2145 cc = cc * 8 + nc - '0';
2146 }
2147 else
2148 {
2149 if (!VIM_ISDIGIT(nc))
2150 break;
2151 cc = cc * 10 + nc - '0';
2152 }
2153
2154 ++i;
2155 }
2156
Bram Moolenaar13505972019-01-24 15:04:48 +01002157 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 cc = 255; /* limit range to 0-255 */
2159 nc = 0;
2160
2161 if (hex) /* hex: up to two chars */
2162 {
2163 if (i >= 2)
2164 break;
2165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 else if (unicode) /* Unicode: up to four or eight chars */
2167 {
2168 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
2169 break;
2170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 else if (i >= 3) /* decimal or octal: up to three chars */
2172 break;
2173 }
2174 if (i == 0) /* no number entered */
2175 {
2176 if (nc == K_ZERO) /* NUL is stored as NL */
2177 {
2178 cc = '\n';
2179 nc = 0;
2180 }
2181 else
2182 {
2183 cc = nc;
2184 nc = 0;
2185 }
2186 }
2187
2188 if (cc == 0) /* NUL is stored as NL */
2189 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00002190 if (enc_dbcs && (cc & 0xff) == 0)
2191 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
2192 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 --no_mapping;
2195#ifdef FEAT_GUI
2196 if (gui.in_use)
2197 --allow_keys;
2198#endif
2199 if (nc)
2200 vungetc(nc);
2201 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
2202 return cc;
2203}
2204
2205/*
2206 * Insert character, taking care of special keys and mod_mask
2207 */
2208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002209insert_special(
2210 int c,
2211 int allow_modmask,
2212 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
2214 char_u *p;
2215 int len;
2216
2217 /*
2218 * Special function key, translate into "<Key>". Up to the last '>' is
2219 * inserted with ins_str(), so as not to replace characters in replace
2220 * mode.
2221 * Only use mod_mask for special keys, to avoid things like <S-Space>,
2222 * unless 'allow_modmask' is TRUE.
2223 */
Bram Moolenaard0573012017-10-28 21:11:06 +02002224#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 /* Command-key never produces a normal key */
2226 if (mod_mask & MOD_MASK_CMD)
2227 allow_modmask = TRUE;
2228#endif
2229 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
2230 {
2231 p = get_special_key_name(c, mod_mask);
2232 len = (int)STRLEN(p);
2233 c = p[len - 1];
2234 if (len > 2)
2235 {
2236 if (stop_arrow() == FAIL)
2237 return;
2238 p[len - 1] = NUL;
2239 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002240 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 ctrlv = FALSE;
2242 }
2243 }
2244 if (stop_arrow() == OK)
2245 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2246}
2247
2248/*
2249 * Special characters in this context are those that need processing other
2250 * than the simple insertion that can be performed here. This includes ESC
2251 * which terminates the insert, and CR/NL which need special processing to
2252 * open up a new line. This routine tries to optimize insertions performed by
2253 * the "redo", "undo" or "put" commands, so it needs to know when it should
2254 * stop and defer processing to the "normal" mechanism.
2255 * '0' and '^' are special, because they can be followed by CTRL-D.
2256 */
2257#ifdef EBCDIC
2258# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2259#else
2260# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2261#endif
2262
Bram Moolenaar13505972019-01-24 15:04:48 +01002263#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002265/*
2266 * "flags": INSCHAR_FORMAT - force formatting
2267 * INSCHAR_CTRLV - char typed just after CTRL-V
2268 * INSCHAR_NO_FEX - don't use 'formatexpr'
2269 *
2270 * NOTE: passes the flags value straight through to internal_format() which,
2271 * beside INSCHAR_FORMAT (above), is also looking for these:
2272 * INSCHAR_DO_COM - format comments
2273 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002276insertchar(
2277 int c, /* character to insert or NUL */
2278 int flags, /* INSCHAR_FORMAT, etc. */
2279 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 int textwidth;
2282#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002286 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002288 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291 /*
2292 * Try to break the line in two or more pieces when:
2293 * - Always do this if we have been called to do formatting only.
2294 * - Always do this when 'formatoptions' has the 'a' flag and the line
2295 * ends in white space.
2296 * - Otherwise:
2297 * - Don't do this if inserting a blank
2298 * - Don't do this if an existing character is being replaced, unless
2299 * we're in VREPLACE mode.
2300 * - Do this if the cursor is not on the line where insert started
2301 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2302 * before the insert.
2303 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2304 * before 'textwidth'
2305 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002306 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002307 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002308 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 && *ml_get_cursor() != NUL)
2312 && (curwin->w_cursor.lnum != Insstart.lnum
2313 || ((!has_format_option(FO_INS_LONG)
2314 || Insstart_textlen <= (colnr_T)textwidth)
2315 && (!fo_ins_blank
2316 || Insstart_blank_vcol <= (colnr_T)textwidth
2317 ))))))
2318 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002319 /* Format with 'formatexpr' when it's set. Use internal formatting
2320 * when 'formatexpr' isn't set or it returns non-zero. */
2321#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002322 int do_internal = TRUE;
2323 colnr_T virtcol = get_nolist_virtcol()
2324 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002325
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002326 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2327 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002328 {
2329 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
2330 /* It may be required to save for undo again, e.g. when setline()
2331 * was called. */
2332 ins_need_undo = TRUE;
2333 }
2334 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002336 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (c == NUL) /* only formatting was wanted */
2340 return;
2341
2342#ifdef FEAT_COMMENTS
2343 /* Check whether this character should end a comment. */
2344 if (did_ai && (int)c == end_comment_pending)
2345 {
2346 char_u *line;
2347 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
2348 int middle_len, end_len;
2349 int i;
2350
2351 /*
2352 * Need to remove existing (middle) comment leader and insert end
2353 * comment leader. First, check what comment leader we can find.
2354 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002355 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
2357 {
2358 /* Skip middle-comment string */
2359 while (*p && p[-1] != ':') /* find end of middle flags */
2360 ++p;
2361 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2362 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002363 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 --middle_len;
2365
2366 /* Find the end-comment string */
2367 while (*p && p[-1] != ':') /* find end of end flags */
2368 ++p;
2369 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2370
2371 /* Skip white space before the cursor */
2372 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002373 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 ;
2375 i++;
2376
2377 /* Skip to before the middle leader */
2378 i -= middle_len;
2379
2380 /* Check some expected things before we go on */
2381 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2382 {
2383 /* Backspace over all the stuff we want to replace */
2384 backspace_until_column(i);
2385
2386 /*
2387 * Insert the end-comment string, except for the last
2388 * character, which will get inserted as normal later.
2389 */
2390 ins_bytes_len(lead_end, end_len - 1);
2391 }
2392 }
2393 }
2394 end_comment_pending = NUL;
2395#endif
2396
2397 did_ai = FALSE;
2398#ifdef FEAT_SMARTINDENT
2399 did_si = FALSE;
2400 can_si = FALSE;
2401 can_si_back = FALSE;
2402#endif
2403
2404 /*
2405 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2406 * This speeds up normal text input considerably.
2407 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2408 * need to re-indent at a ':', or any other character (but not what
2409 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002410 * Don't do this when there an InsertCharPre autocommand is defined,
2411 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002412 * Do the check for InsertCharPre before the call to vpeekc() because the
2413 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 */
2415#ifdef USE_ON_FLY_SCROLL
2416 dont_scroll = FALSE; /* allow scrolling here */
2417#endif
2418
2419 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002421 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 && vpeekc() != NUL
2423 && !(State & REPLACE_FLAG)
2424#ifdef FEAT_CINDENT
2425 && !cindent_on()
2426#endif
2427#ifdef FEAT_RIGHTLEFT
2428 && !p_ri
2429#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002430 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432#define INPUT_BUFLEN 100
2433 char_u buf[INPUT_BUFLEN + 1];
2434 int i;
2435 colnr_T virtcol = 0;
2436
2437 buf[0] = c;
2438 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002439 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 virtcol = get_nolist_virtcol();
2441 /*
2442 * Stop the string when:
2443 * - no more chars available
2444 * - finding a special character (command key)
2445 * - buffer is full
2446 * - running into the 'textwidth' boundary
2447 * - need to check for abbreviation: A non-word char after a word-char
2448 */
2449 while ( (c = vpeekc()) != NUL
2450 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 && i < INPUT_BUFLEN
2453 && (textwidth == 0
2454 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2455 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2456 {
2457#ifdef FEAT_RIGHTLEFT
2458 c = vgetc();
2459 if (p_hkmap && KeyTyped)
2460 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 buf[i++] = c;
2462#else
2463 buf[i++] = vgetc();
2464#endif
2465 }
2466
2467#ifdef FEAT_DIGRAPHS
2468 do_digraph(-1); /* clear digraphs */
2469 do_digraph(buf[i-1]); /* may be the start of a digraph */
2470#endif
2471 buf[i] = NUL;
2472 ins_str(buf);
2473 if (flags & INSCHAR_CTRLV)
2474 {
2475 redo_literal(*buf);
2476 i = 1;
2477 }
2478 else
2479 i = 0;
2480 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002481 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
2483 else
2484 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002485 int cc;
2486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2488 {
2489 char_u buf[MB_MAXBYTES + 1];
2490
2491 (*mb_char2bytes)(c, buf);
2492 buf[cc] = NUL;
2493 ins_char_bytes(buf, cc);
2494 AppendCharToRedobuff(c);
2495 }
2496 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
2498 ins_char(c);
2499 if (flags & INSCHAR_CTRLV)
2500 redo_literal(c);
2501 else
2502 AppendCharToRedobuff(c);
2503 }
2504 }
2505}
2506
2507/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002508 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002509 *
2510 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2511 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002512 */
2513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002514internal_format(
2515 int textwidth,
2516 int second_indent,
2517 int flags,
2518 int format_only,
2519 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002520{
2521 int cc;
2522 int save_char = NUL;
2523 int haveto_redraw = FALSE;
2524 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002525 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002526 int fo_white_par = has_format_option(FO_WHITE_PAR);
2527 int first_line = TRUE;
2528#ifdef FEAT_COMMENTS
2529 colnr_T leader_len;
2530 int no_leader = FALSE;
2531 int do_comments = (flags & INSCHAR_DO_COM);
2532#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02002533#ifdef FEAT_LINEBREAK
2534 int has_lbr = curwin->w_p_lbr;
2535
2536 /* make sure win_lbr_chartabsize() counts correctly */
2537 curwin->w_p_lbr = FALSE;
2538#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002539
2540 /*
2541 * When 'ai' is off we don't want a space under the cursor to be
2542 * deleted. Replace it with an 'x' temporarily.
2543 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002544 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002545 {
2546 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002547 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002548 {
2549 save_char = cc;
2550 pchar_cursor('x');
2551 }
2552 }
2553
2554 /*
2555 * Repeat breaking lines, until the current line is not too long.
2556 */
2557 while (!got_int)
2558 {
2559 int startcol; /* Cursor column at entry */
2560 int wantcol; /* column at textwidth border */
2561 int foundcol; /* column for start of spaces */
2562 int end_foundcol = 0; /* column for start of word */
2563 colnr_T len;
2564 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002565 int orig_col = 0;
2566 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002567 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002568 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002569 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002570
Bram Moolenaar97b98102009-11-17 16:41:01 +00002571 virtcol = get_nolist_virtcol()
2572 + char2cells(c != NUL ? c : gchar_cursor());
2573 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002574 break;
2575
2576#ifdef FEAT_COMMENTS
2577 if (no_leader)
2578 do_comments = FALSE;
2579 else if (!(flags & INSCHAR_FORMAT)
2580 && has_format_option(FO_WRAP_COMS))
2581 do_comments = TRUE;
2582
2583 /* Don't break until after the comment leader */
2584 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002585 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002586 else
2587 leader_len = 0;
2588
2589 /* If the line doesn't start with a comment leader, then don't
2590 * start one in a following broken line. Avoids that a %word
2591 * moved to the start of the next line causes all following lines
2592 * to start with %. */
2593 if (leader_len == 0)
2594 no_leader = TRUE;
2595#endif
2596 if (!(flags & INSCHAR_FORMAT)
2597#ifdef FEAT_COMMENTS
2598 && leader_len == 0
2599#endif
2600 && !has_format_option(FO_WRAP))
2601
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002602 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002603 if ((startcol = curwin->w_cursor.col) == 0)
2604 break;
2605
2606 /* find column of textwidth border */
2607 coladvance((colnr_T)textwidth);
2608 wantcol = curwin->w_cursor.col;
2609
Bram Moolenaar97b98102009-11-17 16:41:01 +00002610 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002611 foundcol = 0;
2612
2613 /*
2614 * Find position to break at.
2615 * Stop at first entered white when 'formatoptions' has 'v'
2616 */
2617 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002618 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002619 || curwin->w_cursor.lnum != Insstart.lnum
2620 || curwin->w_cursor.col >= Insstart.col)
2621 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002622 if (curwin->w_cursor.col == startcol && c != NUL)
2623 cc = c;
2624 else
2625 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002626 if (WHITECHAR(cc))
2627 {
2628 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002629 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002630
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002631 // find start of sequence of blanks
2632 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002633 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2634 {
2635 dec_cursor();
2636 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002637
2638 // Increment count of how many whitespace chars in this
2639 // group; we only need to know if it's more than one.
2640 if (wcc < 2)
2641 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002642 }
2643 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
2644 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002645
2646 // Don't break after a period when 'formatoptions' has 'p' and
2647 // there are less than two spaces.
2648 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2649 continue;
2650
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002651#ifdef FEAT_COMMENTS
2652 /* Don't break until after the comment leader */
2653 if (curwin->w_cursor.col < leader_len)
2654 break;
2655#endif
2656 if (has_format_option(FO_ONE_LETTER))
2657 {
2658 /* do not break after one-letter words */
2659 if (curwin->w_cursor.col == 0)
2660 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002661#ifdef FEAT_COMMENTS
2662 /* do not break "#a b" when 'tw' is 2 */
2663 if (curwin->w_cursor.col <= leader_len)
2664 break;
2665#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002666 col = curwin->w_cursor.col;
2667 dec_cursor();
2668 cc = gchar_cursor();
2669
2670 if (WHITECHAR(cc))
2671 continue; /* one-letter, continue */
2672 curwin->w_cursor.col = col;
2673 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002674
2675 inc_cursor();
2676
2677 end_foundcol = end_col + 1;
2678 foundcol = curwin->w_cursor.col;
2679 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002680 break;
2681 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002682 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002683 {
2684 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002685 if (curwin->w_cursor.col != startcol)
2686 {
2687#ifdef FEAT_COMMENTS
2688 /* Don't break until after the comment leader */
2689 if (curwin->w_cursor.col < leader_len)
2690 break;
2691#endif
2692 col = curwin->w_cursor.col;
2693 inc_cursor();
2694 /* Don't change end_foundcol if already set. */
2695 if (foundcol != curwin->w_cursor.col)
2696 {
2697 foundcol = curwin->w_cursor.col;
2698 end_foundcol = foundcol;
2699 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2700 break;
2701 }
2702 curwin->w_cursor.col = col;
2703 }
2704
2705 if (curwin->w_cursor.col == 0)
2706 break;
2707
2708 col = curwin->w_cursor.col;
2709
2710 dec_cursor();
2711 cc = gchar_cursor();
2712
2713 if (WHITECHAR(cc))
2714 continue; /* break with space */
2715#ifdef FEAT_COMMENTS
2716 /* Don't break until after the comment leader */
2717 if (curwin->w_cursor.col < leader_len)
2718 break;
2719#endif
2720
2721 curwin->w_cursor.col = col;
2722
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002723 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002724 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002725 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2726 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002727 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002728 if (curwin->w_cursor.col == 0)
2729 break;
2730 dec_cursor();
2731 }
2732
2733 if (foundcol == 0) /* no spaces, cannot break line */
2734 {
2735 curwin->w_cursor.col = startcol;
2736 break;
2737 }
2738
2739 /* Going to break the line, remove any "$" now. */
2740 undisplay_dollar();
2741
2742 /*
2743 * Offset between cursor position and line break is used by replace
2744 * stack functions. VREPLACE does not use this, and backspaces
2745 * over the text instead.
2746 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002747 if (State & VREPLACE_FLAG)
2748 orig_col = startcol; /* Will start backspacing from here */
2749 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002750 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002751
2752 /*
2753 * adjust startcol for spaces that will be deleted and
2754 * characters that will remain on top line
2755 */
2756 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002757 while ((cc = gchar_cursor(), WHITECHAR(cc))
2758 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002759 inc_cursor();
2760 startcol -= curwin->w_cursor.col;
2761 if (startcol < 0)
2762 startcol = 0;
2763
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002764 if (State & VREPLACE_FLAG)
2765 {
2766 /*
2767 * In VREPLACE mode, we will backspace over the text to be
2768 * wrapped, so save a copy now to put on the next line.
2769 */
2770 saved_text = vim_strsave(ml_get_cursor());
2771 curwin->w_cursor.col = orig_col;
2772 if (saved_text == NULL)
2773 break; /* Can't do it, out of memory */
2774 saved_text[startcol] = NUL;
2775
2776 /* Backspace over characters that will move to the next line */
2777 if (!fo_white_par)
2778 backspace_until_column(foundcol);
2779 }
2780 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002781 {
2782 /* put cursor after pos. to break line */
2783 if (!fo_white_par)
2784 curwin->w_cursor.col = foundcol;
2785 }
2786
2787 /*
2788 * Split the line just before the margin.
2789 * Only insert/delete lines, but don't really redraw the window.
2790 */
2791 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2792 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
2793#ifdef FEAT_COMMENTS
2794 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002795 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002796#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002797 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2798 if (!(flags & INSCHAR_COM_LIST))
2799 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002800
2801 replace_offset = 0;
2802 if (first_line)
2803 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002804 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002805 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002806 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002807 * This section is for auto-wrap of numeric lists. When not
2808 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2809 * flag will be set and open_line() will handle it (as seen
2810 * above). The code here (and in get_number_indent()) will
2811 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002812 */
2813 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002814 second_indent =
2815 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002816 if (second_indent >= 0)
2817 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002818 if (State & VREPLACE_FLAG)
2819 change_indent(INDENT_SET, second_indent,
2820 FALSE, NUL, TRUE);
2821 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002822#ifdef FEAT_COMMENTS
2823 if (leader_len > 0 && second_indent - leader_len > 0)
2824 {
2825 int i;
2826 int padding = second_indent - leader_len;
2827
2828 /* We started at the first_line of a numbered list
2829 * that has a comment. the open_line() function has
2830 * inserted the proper comment leader and positioned
2831 * the cursor at the end of the split line. Now we
2832 * add the additional whitespace needed after the
2833 * comment leader for the numbered list. */
2834 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002835 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002836 }
2837 else
2838 {
2839#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002840 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002841#ifdef FEAT_COMMENTS
2842 }
2843#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002844 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002845 }
2846 first_line = FALSE;
2847 }
2848
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002849 if (State & VREPLACE_FLAG)
2850 {
2851 /*
2852 * In VREPLACE mode we have backspaced over the text to be
2853 * moved, now we re-insert it into the new line.
2854 */
2855 ins_bytes(saved_text);
2856 vim_free(saved_text);
2857 }
2858 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002859 {
2860 /*
2861 * Check if cursor is not past the NUL off the line, cindent
2862 * may have added or removed indent.
2863 */
2864 curwin->w_cursor.col += startcol;
2865 len = (colnr_T)STRLEN(ml_get_curline());
2866 if (curwin->w_cursor.col > len)
2867 curwin->w_cursor.col = len;
2868 }
2869
2870 haveto_redraw = TRUE;
2871#ifdef FEAT_CINDENT
2872 can_cindent = TRUE;
2873#endif
2874 /* moved the cursor, don't autoindent or cindent now */
2875 did_ai = FALSE;
2876#ifdef FEAT_SMARTINDENT
2877 did_si = FALSE;
2878 can_si = FALSE;
2879 can_si_back = FALSE;
2880#endif
2881 line_breakcheck();
2882 }
2883
2884 if (save_char != NUL) /* put back space after cursor */
2885 pchar_cursor(save_char);
2886
Bram Moolenaar0026d472014-09-09 16:32:39 +02002887#ifdef FEAT_LINEBREAK
2888 curwin->w_p_lbr = has_lbr;
2889#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002890 if (!format_only && haveto_redraw)
2891 {
2892 update_topline();
2893 redraw_curbuf_later(VALID);
2894 }
2895}
2896
2897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 * Called after inserting or deleting text: When 'formatoptions' includes the
2899 * 'a' flag format from the current line until the end of the paragraph.
2900 * Keep the cursor at the same position relative to the text.
2901 * The caller must have saved the cursor line for undo, following ones will be
2902 * saved here.
2903 */
2904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905auto_format(
2906 int trailblank, /* when TRUE also format with trailing blank */
2907 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
2909 pos_T pos;
2910 colnr_T len;
2911 char_u *old;
2912 char_u *new, *pnew;
2913 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002914 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
2916 if (!has_format_option(FO_AUTO))
2917 return;
2918
2919 pos = curwin->w_cursor;
2920 old = ml_get_curline();
2921
2922 /* may remove added space */
2923 check_auto_format(FALSE);
2924
2925 /* Don't format in Insert mode when the cursor is on a trailing blank, the
2926 * user might insert normal text next. Also skip formatting when "1" is
2927 * in 'formatoptions' and there is a single character before the cursor.
2928 * Otherwise the line would be broken and when typing another non-white
2929 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002930 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 if (*old != NUL && !trailblank && wasatend)
2932 {
2933 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002934 cc = gchar_cursor();
2935 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2936 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002938 cc = gchar_cursor();
2939 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
2941 curwin->w_cursor = pos;
2942 return;
2943 }
2944 curwin->w_cursor = pos;
2945 }
2946
2947#ifdef FEAT_COMMENTS
2948 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
2949 * comments. */
2950 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02002951 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 return;
2953#endif
2954
2955 /*
2956 * May start formatting in a previous line, so that after "x" a word is
2957 * moved to the previous line if it fits there now. Only when this is not
2958 * the start of a paragraph.
2959 */
2960 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2961 {
2962 --curwin->w_cursor.lnum;
2963 if (u_save_cursor() == FAIL)
2964 return;
2965 }
2966
2967 /*
2968 * Do the formatting and restore the cursor position. "saved_cursor" will
2969 * be adjusted for the text formatting.
2970 */
2971 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002972 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 curwin->w_cursor = saved_cursor;
2974 saved_cursor.lnum = 0;
2975
2976 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2977 {
2978 /* "cannot happen" */
2979 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2980 coladvance((colnr_T)MAXCOL);
2981 }
2982 else
2983 check_cursor_col();
2984
2985 /* Insert mode: If the cursor is now after the end of the line while it
2986 * previously wasn't, the line was broken. Because of the rule above we
2987 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2988 * formatted. */
2989 if (!wasatend && has_format_option(FO_WHITE_PAR))
2990 {
2991 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002992 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 if (curwin->w_cursor.col == len)
2994 {
2995 pnew = vim_strnsave(new, len + 2);
2996 pnew[len] = ' ';
2997 pnew[len + 1] = NUL;
2998 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
2999 /* remove the space later */
3000 did_add_space = TRUE;
3001 }
3002 else
3003 /* may remove added space */
3004 check_auto_format(FALSE);
3005 }
3006
3007 check_cursor();
3008}
3009
3010/*
3011 * When an extra space was added to continue a paragraph for auto-formatting,
3012 * delete it now. The space must be under the cursor, just after the insert
3013 * position.
3014 */
3015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003016check_auto_format(
3017 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003020 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 if (did_add_space)
3023 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003024 cc = gchar_cursor();
3025 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 /* Somehow the space was removed already. */
3027 did_add_space = FALSE;
3028 else
3029 {
3030 if (!end_insert)
3031 {
3032 inc_cursor();
3033 c = gchar_cursor();
3034 dec_cursor();
3035 }
3036 if (c != NUL)
3037 {
3038 /* The space is no longer at the end of the line, delete it. */
3039 del_char(FALSE);
3040 did_add_space = FALSE;
3041 }
3042 }
3043 }
3044}
3045
3046/*
3047 * Find out textwidth to be used for formatting:
3048 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02003049 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 * if invalid value, use 0.
3051 * Set default to window width (maximum 79) for "gq" operator.
3052 */
3053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003054comp_textwidth(
3055 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 int textwidth;
3058
3059 textwidth = curbuf->b_p_tw;
3060 if (textwidth == 0 && curbuf->b_p_wm)
3061 {
3062 /* The width is the window width minus 'wrapmargin' minus all the
3063 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003064 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065#ifdef FEAT_CMDWIN
3066 if (cmdwin_type != 0)
3067 textwidth -= 1;
3068#endif
3069#ifdef FEAT_FOLDING
3070 textwidth -= curwin->w_p_fdc;
3071#endif
3072#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003073 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 textwidth -= 1;
3075#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02003076 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 textwidth -= 8;
3078 }
3079 if (textwidth < 0)
3080 textwidth = 0;
3081 if (ff && textwidth == 0)
3082 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003083 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 if (textwidth > 79)
3085 textwidth = 79;
3086 }
3087 return textwidth;
3088}
3089
3090/*
3091 * Put a character in the redo buffer, for when just after a CTRL-V.
3092 */
3093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003094redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096 char_u buf[10];
3097
3098 /* Only digits need special treatment. Translate them into a string of
3099 * three digits. */
3100 if (VIM_ISDIGIT(c))
3101 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003102 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 AppendToRedobuff(buf);
3104 }
3105 else
3106 AppendCharToRedobuff(c);
3107}
3108
3109/*
3110 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003111 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003114start_arrow(
3115 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003117 start_arrow_common(end_insert_pos, TRUE);
3118}
3119
3120/*
3121 * Like start_arrow() but with end_change argument.
3122 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
3123 */
3124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003125start_arrow_with_change(
3126 pos_T *end_insert_pos, /* can be NULL */
3127 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003128{
3129 start_arrow_common(end_insert_pos, end_change);
3130 if (!end_change)
3131 {
3132 AppendCharToRedobuff(Ctrl_G);
3133 AppendCharToRedobuff('U');
3134 }
3135}
3136
3137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003138start_arrow_common(
3139 pos_T *end_insert_pos, /* can be NULL */
3140 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003141{
3142 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
3144 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01003145 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 arrow_used = TRUE; /* this means we stopped the current insert */
3147 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003148#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003149 check_spell_redraw();
3150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151}
3152
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003153#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003154/*
3155 * If we skipped highlighting word at cursor, do it now.
3156 * It may be skipped again, thus reset spell_redraw_lnum first.
3157 */
3158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003159check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003160{
3161 if (spell_redraw_lnum != 0)
3162 {
3163 linenr_T lnum = spell_redraw_lnum;
3164
3165 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01003166 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00003167 }
3168}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003169
Bram Moolenaar217ad922005-03-20 22:37:15 +00003170#endif
3171
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172/*
3173 * stop_arrow() is called before a change is made in insert mode.
3174 * If an arrow key has been used, start a new insertion.
3175 * Returns FAIL if undo is impossible, shouldn't insert then.
3176 */
3177 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003178stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179{
3180 if (arrow_used)
3181 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02003182 Insstart = curwin->w_cursor; /* new insertion starts here */
3183 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
3184 /* Don't update the original insert position when moved to the
3185 * right, except when nothing was inserted yet. */
3186 update_Insstart_orig = FALSE;
3187 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
3188
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (u_save_cursor() == OK)
3190 {
3191 arrow_used = FALSE;
3192 ins_need_undo = FALSE;
3193 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02003194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 if (State & VREPLACE_FLAG)
3197 {
3198 orig_line_count = curbuf->b_ml.ml_line_count;
3199 vr_lines_changed = 1;
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 ResetRedobuff();
3202 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00003203 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 else if (ins_need_undo)
3206 {
3207 if (u_save_cursor() == OK)
3208 ins_need_undo = FALSE;
3209 }
3210
3211#ifdef FEAT_FOLDING
3212 /* Always open fold at the cursor line when inserting something. */
3213 foldOpenCursor();
3214#endif
3215
3216 return (arrow_used || ins_need_undo ? FAIL : OK);
3217}
3218
3219/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003220 * Do a few things to stop inserting.
3221 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
3222 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 */
3224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225stop_insert(
3226 pos_T *end_insert_pos,
3227 int esc, /* called by ins_esc() */
3228 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003230 int cc;
3231 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232
3233 stop_redo_ins();
3234 replace_flush(); /* abandon replace stack */
3235
3236 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003237 * Save the inserted text for later redo with ^@ and CTRL-A.
3238 * Don't do it when "restart_edit" was set and nothing was inserted,
3239 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003241 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003242 if (did_restart_edit == 0 || (ptr != NULL
3243 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003244 {
3245 vim_free(last_insert);
3246 last_insert = ptr;
3247 last_insert_skip = new_insert_skip;
3248 }
3249 else
3250 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003252 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
3254 /* Auto-format now. It may seem strange to do this when stopping an
3255 * insertion (or moving the cursor), but it's required when appending
3256 * a line and having it end in a space. But only do it when something
3257 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003258 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003260 pos_T tpos = curwin->w_cursor;
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 /* When the cursor is at the end of the line after a space the
3263 * formatting will move it to the following word. Avoid that by
3264 * moving the cursor onto the space. */
3265 cc = 'x';
3266 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
3267 {
3268 dec_cursor();
3269 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003270 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003271 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 }
3273
3274 auto_format(TRUE, FALSE);
3275
Bram Moolenaar1c465442017-03-12 20:10:05 +01003276 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003277 {
3278 if (gchar_cursor() != NUL)
3279 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003280 // If the cursor is still at the same character, also keep
3281 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003282 if (gchar_cursor() == NUL
3283 && curwin->w_cursor.lnum == tpos.lnum
3284 && curwin->w_cursor.col == tpos.col)
3285 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288
3289 /* If a space was inserted for auto-formatting, remove it now. */
3290 check_auto_format(TRUE);
3291
3292 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003293 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00003294 * Do this when ESC was used or moving the cursor up/down.
3295 * Check for the old position still being valid, just in case the text
3296 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01003297 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003298 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3299 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003301 pos_T tpos = curwin->w_cursor;
3302
3303 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003304 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003305 for (;;)
3306 {
3307 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3308 --curwin->w_cursor.col;
3309 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003310 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003311 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003312 if (del_char(TRUE) == FAIL)
3313 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003314 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003315 if (curwin->w_cursor.lnum != tpos.lnum)
3316 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003317 else
3318 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003319 /* reset tpos, could have been invalidated in the loop above */
3320 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003321 tpos.col++;
3322 if (cc != NUL && gchar_pos(&tpos) == NUL)
3323 ++curwin->w_cursor.col; /* put cursor back on the NUL */
3324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 /* <C-S-Right> may have started Visual mode, adjust the position for
3327 * deleted characters. */
3328 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3329 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003330 int len = (int)STRLEN(ml_get_curline());
3331
3332 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003334 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339 }
3340 did_ai = FALSE;
3341#ifdef FEAT_SMARTINDENT
3342 did_si = FALSE;
3343 can_si = FALSE;
3344 can_si_back = FALSE;
3345#endif
3346
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003347 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3348 * now in a different buffer. */
3349 if (end_insert_pos != NULL)
3350 {
3351 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003352 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003353 curbuf->b_op_end = *end_insert_pos;
3354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355}
3356
3357/*
3358 * Set the last inserted text to a single character.
3359 * Used for the replace command.
3360 */
3361 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003362set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 char_u *s;
3365
3366 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (last_insert != NULL)
3369 {
3370 s = last_insert;
3371 /* Use the CTRL-V only when entering a special char */
3372 if (c < ' ' || c == DEL)
3373 *s++ = Ctrl_V;
3374 s = add_char2buf(c, s);
3375 *s++ = ESC;
3376 *s++ = NUL;
3377 last_insert_skip = 0;
3378 }
3379}
3380
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003381#if defined(EXITFREE) || defined(PROTO)
3382 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003383free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003384{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003385 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003386}
3387#endif
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389/*
3390 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3391 * and CSI. Handle multi-byte characters.
3392 * Returns a pointer to after the added bytes.
3393 */
3394 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003395add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003397 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 int i;
3399 int len;
3400
3401 len = (*mb_char2bytes)(c, temp);
3402 for (i = 0; i < len; ++i)
3403 {
3404 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
3406 if (c == K_SPECIAL)
3407 {
3408 *s++ = K_SPECIAL;
3409 *s++ = KS_SPECIAL;
3410 *s++ = KE_FILLER;
3411 }
3412#ifdef FEAT_GUI
3413 else if (c == CSI)
3414 {
3415 *s++ = CSI;
3416 *s++ = KS_EXTRA;
3417 *s++ = (int)KE_CSI;
3418 }
3419#endif
3420 else
3421 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 return s;
3424}
3425
3426/*
3427 * move cursor to start of line
3428 * if flags & BL_WHITE move to first non-white
3429 * if flags & BL_SOL move to first non-white if startofline is set,
3430 * otherwise keep "curswant" column
3431 * if flags & BL_FIX don't leave the cursor on a NUL.
3432 */
3433 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003434beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
3436 if ((flags & BL_SOL) && !p_sol)
3437 coladvance(curwin->w_curswant);
3438 else
3439 {
3440 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 if (flags & (BL_WHITE | BL_SOL))
3444 {
3445 char_u *ptr;
3446
Bram Moolenaar1c465442017-03-12 20:10:05 +01003447 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3449 ++curwin->w_cursor.col;
3450 }
3451 curwin->w_set_curswant = TRUE;
3452 }
3453}
3454
3455/*
3456 * oneright oneleft cursor_down cursor_up
3457 *
3458 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003459 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 * Return OK when successful, FAIL when we hit a line of file boundary.
3461 */
3462
3463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003464oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
3466 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (virtual_active())
3470 {
3471 pos_T prevpos = curwin->w_cursor;
3472
3473 /* Adjust for multi-wide char (excluding TAB) */
3474 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003475 coladvance(getviscol() + ((*ptr != TAB
3476 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 ? ptr2cells(ptr) : 1));
3478 curwin->w_set_curswant = TRUE;
3479 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
3480 return (prevpos.col != curwin->w_cursor.col
3481 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
3484 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003485 if (*ptr == NUL)
3486 return FAIL; /* already at the very end */
3487
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003488 if (has_mbyte)
3489 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003491 l = 1;
3492
3493 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3494 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003495 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003496 return FAIL;
3497 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 curwin->w_set_curswant = TRUE;
3500 return OK;
3501}
3502
3503 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003504oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (virtual_active())
3507 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003508#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 int v = getviscol();
3512
3513 if (v == 0)
3514 return FAIL;
3515
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003516#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 /* We might get stuck on 'showbreak', skip over it. */
3518 width = 1;
3519 for (;;)
3520 {
3521 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02003522 /* getviscol() is slow, skip it when 'showbreak' is empty,
3523 * 'breakindent' is not set and there are no multi-byte
3524 * characters */
3525 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003526 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 break;
3528 ++width;
3529 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003530#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
3534 if (curwin->w_cursor.coladd == 1)
3535 {
3536 char_u *ptr;
3537
3538 /* Adjust for multi-wide char (not a TAB) */
3539 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003540 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3541 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 curwin->w_cursor.coladd = 0;
3543 }
3544
3545 curwin->w_set_curswant = TRUE;
3546 return OK;
3547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
3549 if (curwin->w_cursor.col == 0)
3550 return FAIL;
3551
3552 curwin->w_set_curswant = TRUE;
3553 --curwin->w_cursor.col;
3554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /* if the character on the left of the current cursor is a multi-byte
3556 * character, move to its first byte */
3557 if (has_mbyte)
3558 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 return OK;
3560}
3561
3562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003563cursor_up(
3564 long n,
3565 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566{
3567 linenr_T lnum;
3568
3569 if (n > 0)
3570 {
3571 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003572 /* This fails if the cursor is already in the first line or the count
3573 * is larger than the line number and '-' is in 'cpoptions' */
3574 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 return FAIL;
3576 if (n >= lnum)
3577 lnum = 1;
3578 else
3579#ifdef FEAT_FOLDING
3580 if (hasAnyFolding(curwin))
3581 {
3582 /*
3583 * Count each sequence of folded lines as one logical line.
3584 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003585 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 (void)hasFolding(lnum, &lnum, NULL);
3587
3588 while (n--)
3589 {
3590 /* move up one line */
3591 --lnum;
3592 if (lnum <= 1)
3593 break;
3594 /* If we entered a fold, move to the beginning, unless in
3595 * Insert mode or when 'foldopen' contains "all": it will open
3596 * in a moment. */
3597 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3598 (void)hasFolding(lnum, &lnum, NULL);
3599 }
3600 if (lnum < 1)
3601 lnum = 1;
3602 }
3603 else
3604#endif
3605 lnum -= n;
3606 curwin->w_cursor.lnum = lnum;
3607 }
3608
3609 /* try to advance to the column we want to be at */
3610 coladvance(curwin->w_curswant);
3611
3612 if (upd_topline)
3613 update_topline(); /* make sure curwin->w_topline is valid */
3614
3615 return OK;
3616}
3617
3618/*
3619 * Cursor down a number of logical lines.
3620 */
3621 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003622cursor_down(
3623 long n,
3624 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
3626 linenr_T lnum;
3627
3628 if (n > 0)
3629 {
3630 lnum = curwin->w_cursor.lnum;
3631#ifdef FEAT_FOLDING
3632 /* Move to last line of fold, will fail if it's the end-of-file. */
3633 (void)hasFolding(lnum, NULL, &lnum);
3634#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003635 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003636 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003637 if (lnum >= curbuf->b_ml.ml_line_count
3638 || (lnum + n > curbuf->b_ml.ml_line_count
3639 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 return FAIL;
3641 if (lnum + n >= curbuf->b_ml.ml_line_count)
3642 lnum = curbuf->b_ml.ml_line_count;
3643 else
3644#ifdef FEAT_FOLDING
3645 if (hasAnyFolding(curwin))
3646 {
3647 linenr_T last;
3648
3649 /* count each sequence of folded lines as one logical line */
3650 while (n--)
3651 {
3652 if (hasFolding(lnum, NULL, &last))
3653 lnum = last + 1;
3654 else
3655 ++lnum;
3656 if (lnum >= curbuf->b_ml.ml_line_count)
3657 break;
3658 }
3659 if (lnum > curbuf->b_ml.ml_line_count)
3660 lnum = curbuf->b_ml.ml_line_count;
3661 }
3662 else
3663#endif
3664 lnum += n;
3665 curwin->w_cursor.lnum = lnum;
3666 }
3667
3668 /* try to advance to the column we want to be at */
3669 coladvance(curwin->w_curswant);
3670
3671 if (upd_topline)
3672 update_topline(); /* make sure curwin->w_topline is valid */
3673
3674 return OK;
3675}
3676
3677/*
3678 * Stuff the last inserted text in the read buffer.
3679 * Last_insert actually is a copy of the redo buffer, so we
3680 * first have to remove the command.
3681 */
3682 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003683stuff_inserted(
3684 int c, /* Command character to be inserted */
3685 long count, /* Repeat this many times */
3686 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 char_u *esc_ptr;
3689 char_u *ptr;
3690 char_u *last_ptr;
3691 char_u last = NUL;
3692
3693 ptr = get_last_insert();
3694 if (ptr == NULL)
3695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003696 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 return FAIL;
3698 }
3699
3700 /* may want to stuff the command character, to start Insert mode */
3701 if (c != NUL)
3702 stuffcharReadbuff(c);
3703 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
3704 *esc_ptr = NUL; /* remove the ESC */
3705
3706 /* when the last char is either "0" or "^" it will be quoted if no ESC
3707 * comes after it OR if it will inserted more than once and "ptr"
3708 * starts with ^D. -- Acevedo
3709 */
3710 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3711 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3712 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3713 {
3714 last = *last_ptr;
3715 *last_ptr = NUL;
3716 }
3717
3718 do
3719 {
3720 stuffReadbuff(ptr);
3721 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
3722 if (last)
3723 stuffReadbuff((char_u *)(last == '0'
3724 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3725 : IF_EB("\026^", CTRL_V_STR "^")));
3726 }
3727 while (--count > 0);
3728
3729 if (last)
3730 *last_ptr = last;
3731
3732 if (esc_ptr != NULL)
3733 *esc_ptr = ESC; /* put the ESC back */
3734
3735 /* may want to stuff a trailing ESC, to get out of Insert mode */
3736 if (!no_esc)
3737 stuffcharReadbuff(ESC);
3738
3739 return OK;
3740}
3741
3742 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003743get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 if (last_insert == NULL)
3746 return NULL;
3747 return last_insert + last_insert_skip;
3748}
3749
3750/*
3751 * Get last inserted string, and remove trailing <Esc>.
3752 * Returns pointer to allocated memory (must be freed) or NULL.
3753 */
3754 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003755get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
3757 char_u *s;
3758 int len;
3759
3760 if (last_insert == NULL)
3761 return NULL;
3762 s = vim_strsave(last_insert + last_insert_skip);
3763 if (s != NULL)
3764 {
3765 len = (int)STRLEN(s);
3766 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
3767 s[len - 1] = NUL;
3768 }
3769 return s;
3770}
3771
3772/*
3773 * Check the word in front of the cursor for an abbreviation.
3774 * Called when the non-id character "c" has been entered.
3775 * When an abbreviation is recognized it is removed from the text and
3776 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3777 */
3778 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
3781 /* Don't check for abbreviation in paste mode, when disabled and just
3782 * after moving around with cursor keys. */
3783 if (p_paste || no_abbr || arrow_used)
3784 return FALSE;
3785
3786 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3787 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3788}
3789
3790/*
3791 * replace-stack functions
3792 *
3793 * When replacing characters, the replaced characters are remembered for each
3794 * new character. This is used to re-insert the old text when backspacing.
3795 *
3796 * There is a NUL headed list of characters for each character that is
3797 * currently in the file after the insertion point. When BS is used, one NUL
3798 * headed list is put back for the deleted character.
3799 *
3800 * For a newline, there are two NUL headed lists. One contains the characters
3801 * that the NL replaced. The extra one stores the characters after the cursor
3802 * that were deleted (always white space).
3803 *
3804 * Replace_offset is normally 0, in which case replace_push will add a new
3805 * character at the end of the stack. If replace_offset is not 0, that many
3806 * characters will be left on the stack above the newly inserted character.
3807 */
3808
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003809static char_u *replace_stack = NULL;
3810static long replace_stack_nr = 0; /* next entry in replace stack */
3811static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003814replace_push(
3815 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 char_u *p;
3818
3819 if (replace_stack_nr < replace_offset) /* nothing to do */
3820 return;
3821 if (replace_stack_len <= replace_stack_nr)
3822 {
3823 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003824 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (p == NULL) /* out of memory */
3826 {
3827 replace_stack_len -= 50;
3828 return;
3829 }
3830 if (replace_stack != NULL)
3831 {
3832 mch_memmove(p, replace_stack,
3833 (size_t)(replace_stack_nr * sizeof(char_u)));
3834 vim_free(replace_stack);
3835 }
3836 replace_stack = p;
3837 }
3838 p = replace_stack + replace_stack_nr - replace_offset;
3839 if (replace_offset)
3840 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3841 *p = c;
3842 ++replace_stack_nr;
3843}
3844
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003845/*
3846 * Push a character onto the replace stack. Handles a multi-byte character in
3847 * reverse byte order, so that the first byte is popped off first.
3848 * Return the number of bytes done (includes composing characters).
3849 */
3850 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003851replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003852{
3853 int l = (*mb_ptr2len)(p);
3854 int j;
3855
3856 for (j = l - 1; j >= 0; --j)
3857 replace_push(p[j]);
3858 return l;
3859}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861/*
3862 * Pop one item from the replace stack.
3863 * return -1 if stack empty
3864 * return replaced character or NUL otherwise
3865 */
3866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003867replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
3869 if (replace_stack_nr == 0)
3870 return -1;
3871 return (int)replace_stack[--replace_stack_nr];
3872}
3873
3874/*
3875 * Join the top two items on the replace stack. This removes to "off"'th NUL
3876 * encountered.
3877 */
3878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003879replace_join(
3880 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
3882 int i;
3883
3884 for (i = replace_stack_nr; --i >= 0; )
3885 if (replace_stack[i] == NUL && off-- <= 0)
3886 {
3887 --replace_stack_nr;
3888 mch_memmove(replace_stack + i, replace_stack + i + 1,
3889 (size_t)(replace_stack_nr - i));
3890 return;
3891 }
3892}
3893
3894/*
3895 * Pop bytes from the replace stack until a NUL is found, and insert them
3896 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3897 */
3898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003899replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
3901 int cc;
3902 int oldState = State;
3903
3904 State = NORMAL; /* don't want REPLACE here */
3905 while ((cc = replace_pop()) > 0)
3906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 dec_cursor();
3909 }
3910 State = oldState;
3911}
3912
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913/*
3914 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3915 * indicates a multi-byte char, pop the other bytes too.
3916 */
3917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003918mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
3920 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003921 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 int i;
3923 int c;
3924
3925 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3926 {
3927 buf[0] = cc;
3928 for (i = 1; i < n; ++i)
3929 buf[i] = replace_pop();
3930 ins_bytes_len(buf, n);
3931 }
3932 else
3933 ins_char(cc);
3934
3935 if (enc_utf8)
3936 /* Handle composing chars. */
3937 for (;;)
3938 {
3939 c = replace_pop();
3940 if (c == -1) /* stack empty */
3941 break;
3942 if ((n = MB_BYTE2LEN(c)) == 1)
3943 {
3944 /* Not a multi-byte char, put it back. */
3945 replace_push(c);
3946 break;
3947 }
3948 else
3949 {
3950 buf[0] = c;
3951 for (i = 1; i < n; ++i)
3952 buf[i] = replace_pop();
3953 if (utf_iscomposing(utf_ptr2char(buf)))
3954 ins_bytes_len(buf, n);
3955 else
3956 {
3957 /* Not a composing char, put it back. */
3958 for (i = n - 1; i >= 0; --i)
3959 replace_push(buf[i]);
3960 break;
3961 }
3962 }
3963 }
3964}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
3966/*
3967 * make the replace stack empty
3968 * (called when exiting replace mode)
3969 */
3970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003971replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003973 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 replace_stack_len = 0;
3975 replace_stack_nr = 0;
3976}
3977
3978/*
3979 * Handle doing a BS for one character.
3980 * cc < 0: replace stack empty, just move cursor
3981 * cc == 0: character was inserted, delete it
3982 * cc > 0: character was replaced, put cc (first byte of original char) back
3983 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003984 * When "limit_col" is >= 0, don't delete before this column. Matters when
3985 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 */
3987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003988replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
3990 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 int orig_len = 0;
3992 int ins_len;
3993 int orig_vcols = 0;
3994 colnr_T start_vcol;
3995 char_u *p;
3996 int i;
3997 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 cc = replace_pop();
4000 if (cc > 0)
4001 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01004002#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01004003 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01004004
4005 if (curbuf->b_has_textprop)
4006 {
4007 // Do not adjust text properties for individual delete and insert
4008 // operations, do it afterwards on the resulting text.
4009 len_before = STRLEN(ml_get_curline());
4010 ++text_prop_frozen;
4011 }
4012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (State & VREPLACE_FLAG)
4014 {
4015 /* Get the number of screen cells used by the character we are
4016 * going to delete. */
4017 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
4018 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
4019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if (has_mbyte)
4021 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004022 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004024 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 replace_push(cc);
4026 }
4027 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
4029 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004031 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
4033 replace_pop_ins();
4034
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (State & VREPLACE_FLAG)
4036 {
4037 /* Get the number of screen cells used by the inserted characters */
4038 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004039 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 vcol = start_vcol;
4041 for (i = 0; i < ins_len; ++i)
4042 {
4043 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004044 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 vcol -= start_vcol;
4047
4048 /* Delete spaces that were inserted after the cursor to keep the
4049 * text aligned. */
4050 curwin->w_cursor.col += ins_len;
4051 while (vcol > orig_vcols && gchar_cursor() == ' ')
4052 {
4053 del_char(FALSE);
4054 ++orig_vcols;
4055 }
4056 curwin->w_cursor.col -= ins_len;
4057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
Bram Moolenaar196d1572019-01-02 23:47:18 +01004059 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01004061
4062#ifdef FEAT_TEXT_PROP
4063 if (curbuf->b_has_textprop)
4064 {
4065 size_t len_now = STRLEN(ml_get_curline());
4066
4067 --text_prop_frozen;
4068 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02004069 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01004070 }
4071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 }
4073 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004074 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075}
4076
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
4078/*
4079 * Map Hebrew keyboard when in hkmap mode.
4080 */
4081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004082hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
4084 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
4085 {
4086 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
4087 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
4088 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
4089 static char_u map[26] =
4090 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
4091 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
4092 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
4093 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
4094 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
4095 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
4096 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
4097 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
4098 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
4099
4100 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
4101 return (int)(map[CharOrd(c)] - 1 + p_aleph);
4102 /* '-1'='sofit' */
4103 else if (c == 'x')
4104 return 'X';
4105 else if (c == 'q')
4106 return '\''; /* {geresh}={'} */
4107 else if (c == 246)
4108 return ' '; /* \"o --> ' ' for a german keyboard */
4109 else if (c == 228)
4110 return ' '; /* \"a --> ' ' -- / -- */
4111 else if (c == 252)
4112 return ' '; /* \"u --> ' ' -- / -- */
4113#ifdef EBCDIC
4114 else if (islower(c))
4115#else
4116 /* NOTE: islower() does not do the right thing for us on Linux so we
4117 * do this the same was as 5.7 and previous, so it works correctly on
4118 * all systems. Specifically, the e.g. Delete and Arrow keys are
4119 * munged and won't work if e.g. searching for Hebrew text.
4120 */
4121 else if (c >= 'a' && c <= 'z')
4122#endif
4123 return (int)(map[CharOrdLow(c)] + p_aleph);
4124 else
4125 return c;
4126 }
4127 else
4128 {
4129 switch (c)
4130 {
4131 case '`': return ';';
4132 case '/': return '.';
4133 case '\'': return ',';
4134 case 'q': return '/';
4135 case 'w': return '\'';
4136
4137 /* Hebrew letters - set offset from 'a' */
4138 case ',': c = '{'; break;
4139 case '.': c = 'v'; break;
4140 case ';': c = 't'; break;
4141 default: {
4142 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
4143
4144#ifdef EBCDIC
4145 /* see note about islower() above */
4146 if (!islower(c))
4147#else
4148 if (c < 'a' || c > 'z')
4149#endif
4150 return c;
4151 c = str[CharOrdLow(c)];
4152 break;
4153 }
4154 }
4155
4156 return (int)(CharOrdLow(c) + p_aleph);
4157 }
4158}
4159#endif
4160
4161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004162ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
4164 int need_redraw = FALSE;
4165 int regname;
4166 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004167 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 /*
4170 * If we are going to wait for a character, show a '"'.
4171 */
4172 pc_status = PC_STATUS_UNSET;
4173 if (redrawing() && !char_avail())
4174 {
4175 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00004176 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178 edit_putchar('"', TRUE);
4179#ifdef FEAT_CMDL_INFO
4180 add_to_showcmd_c(Ctrl_R);
4181#endif
4182 }
4183
4184#ifdef USE_ON_FLY_SCROLL
4185 dont_scroll = TRUE; /* disallow scrolling here */
4186#endif
4187
4188 /*
4189 * Don't map the register name. This also prevents the mode message to be
4190 * deleted when ESC is hit.
4191 */
4192 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004193 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
4196 {
4197 /* Get a third key for literal register insertion */
4198 literally = regname;
4199#ifdef FEAT_CMDL_INFO
4200 add_to_showcmd_c(literally);
4201#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004202 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 --no_mapping;
4206
4207#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02004208 /* Don't call u_sync() while typing the expression or giving an error
4209 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 ++no_u_sync;
4211 if (regname == '=')
4212 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004213 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004214# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004216# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004217 /* Sync undo when evaluating the expression calls setline() or
4218 * append(), so that it can be undone separately. */
4219 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02004220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004222
4223 // Cursor may be moved back a column.
4224 curwin->w_cursor = curpos;
4225 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004226# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004227 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (im_on)
4229 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00004232 if (regname == NUL || !valid_yank_reg(regname, FALSE))
4233 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004234 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00004236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 else
4238 {
4239#endif
4240 if (literally == Ctrl_O || literally == Ctrl_P)
4241 {
4242 /* Append the command to the redo buffer. */
4243 AppendCharToRedobuff(Ctrl_R);
4244 AppendCharToRedobuff(literally);
4245 AppendCharToRedobuff(regname);
4246
4247 do_put(regname, BACKWARD, 1L,
4248 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
4249 }
4250 else if (insert_reg(regname, literally) == FAIL)
4251 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004252 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 need_redraw = TRUE; /* remove the '"' */
4254 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004255 else if (stop_insert_mode)
4256 /* When the '=' register was used and a function was invoked that
4257 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
4258 * insert anything, need to remove the '"' */
4259 need_redraw = TRUE;
4260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261#ifdef FEAT_EVAL
4262 }
4263 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004264 if (u_sync_once == 1)
4265 ins_need_undo = TRUE;
4266 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267#endif
4268#ifdef FEAT_CMDL_INFO
4269 clear_showcmd();
4270#endif
4271
4272 /* If the inserted register is empty, we need to remove the '"' */
4273 if (need_redraw || stuff_empty())
4274 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004275
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004276 /* Disallow starting Visual mode here, would get a weird mode. */
4277 if (!vis_active && VIsual_active)
4278 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279}
4280
4281/*
4282 * CTRL-G commands in Insert mode.
4283 */
4284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004285ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286{
4287 int c;
4288
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004289 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 /*
4293 * Don't map the second key. This also prevents the mode message to be
4294 * deleted when ESC is hit.
4295 */
4296 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004297 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 --no_mapping;
4299 switch (c)
4300 {
4301 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
4302 case K_UP:
4303 case Ctrl_K:
4304 case 'k': ins_up(TRUE);
4305 break;
4306
4307 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
4308 case K_DOWN:
4309 case Ctrl_J:
4310 case 'j': ins_down(TRUE);
4311 break;
4312
4313 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004314 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004316
4317 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004318 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004319 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004320 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 break;
4322
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004323 /* CTRL-G U: do not break undo with the next char */
4324 case 'U':
4325 /* Allow one left/right cursor movement with the next char,
4326 * without breaking undo. */
4327 dont_sync_undo = MAYBE;
4328 break;
4329
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02004331 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 }
4333}
4334
4335/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004336 * CTRL-^ in Insert mode.
4337 */
4338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004339ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004341 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342 {
4343 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
4344 if (State & LANGMAP)
4345 {
4346 curbuf->b_p_iminsert = B_IMODE_NONE;
4347 State &= ~LANGMAP;
4348 }
4349 else
4350 {
4351 curbuf->b_p_iminsert = B_IMODE_LMAP;
4352 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004353#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 im_set_active(FALSE);
4355#endif
4356 }
4357 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004358#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004359 else
4360 {
4361 /* There are no ":lmap" mappings, toggle IM */
4362 if (im_get_status())
4363 {
4364 curbuf->b_p_iminsert = B_IMODE_NONE;
4365 im_set_active(FALSE);
4366 }
4367 else
4368 {
4369 curbuf->b_p_iminsert = B_IMODE_IM;
4370 State &= ~LANGMAP;
4371 im_set_active(TRUE);
4372 }
4373 }
4374#endif
4375 set_iminsert_global();
4376 showmode();
4377#ifdef FEAT_GUI
4378 /* may show different cursor shape or color */
4379 if (gui.in_use)
4380 gui_update_cursor(TRUE, FALSE);
4381#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004382#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004383 /* Show/unshow value of 'keymap' in status lines. */
4384 status_redraw_curbuf();
4385#endif
4386}
4387
4388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 * Handle ESC in insert mode.
4390 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4391 * insert.
4392 */
4393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004394ins_esc(
4395 long *count,
4396 int cmdchar,
4397 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
4399 int temp;
4400 static int disabled_redraw = FALSE;
4401
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004402#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 check_spell_redraw();
4404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405#if defined(FEAT_HANGULIN)
4406# if defined(ESC_CHG_TO_ENG_MODE)
4407 hangul_input_state_set(0);
4408# endif
4409 if (composing_hangul)
4410 {
4411 push_raw_key(composing_hangul_buffer, 2);
4412 composing_hangul = 0;
4413 }
4414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416 temp = curwin->w_cursor.col;
4417 if (disabled_redraw)
4418 {
4419 --RedrawingDisabled;
4420 disabled_redraw = FALSE;
4421 }
4422 if (!arrow_used)
4423 {
4424 /*
4425 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004426 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4427 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 */
4429 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004430 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 /*
4433 * Repeating insert may take a long time. Check for
4434 * interrupt now and then.
4435 */
4436 if (*count > 0)
4437 {
4438 line_breakcheck();
4439 if (got_int)
4440 *count = 0;
4441 }
4442
4443 if (--*count > 0) /* repeat what was typed */
4444 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004445 /* Vi repeats the insert without replacing characters. */
4446 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4447 State &= ~REPLACE_FLAG;
4448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 (void)start_redo_ins();
4450 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02004451 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 ++RedrawingDisabled;
4453 disabled_redraw = TRUE;
4454 return FALSE; /* repeat the insert */
4455 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004456 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 undisplay_dollar();
4458 }
4459
4460 /* When an autoindent was removed, curswant stays after the
4461 * indent */
4462 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4463 curwin->w_set_curswant = TRUE;
4464
4465 /* Remember the last Insert position in the '^ mark. */
4466 if (!cmdmod.keepjumps)
4467 curbuf->b_last_insert = curwin->w_cursor;
4468
4469 /*
4470 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004471 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004473 if (!nomove
4474 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004475 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004476 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004477 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478#ifdef FEAT_RIGHTLEFT
4479 && !revins_on
4480#endif
4481 )
4482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4484 {
4485 oneleft();
4486 if (restart_edit != NUL)
4487 ++curwin->w_cursor.coladd;
4488 }
4489 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
4491 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 /* Correct cursor for multi-byte character. */
4493 if (has_mbyte)
4494 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496 }
4497
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004498#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 /* Disable IM to allow typing English directly for Normal mode commands.
4500 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4501 * well). */
4502 if (!(State & LANGMAP))
4503 im_save_status(&curbuf->b_p_iminsert);
4504 im_set_active(FALSE);
4505#endif
4506
4507 State = NORMAL;
4508 /* need to position cursor again (e.g. when on a TAB ) */
4509 changed_cline_bef_curs();
4510
4511#ifdef FEAT_MOUSE
4512 setmouse();
4513#endif
4514#ifdef CURSOR_SHAPE
4515 ui_cursor_shape(); /* may show different cursor shape */
4516#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004517 if (!p_ek)
4518 /* Re-enable bracketed paste mode. */
4519 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004521 // When recording or for CTRL-O, need to display the new mode.
4522 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004523 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004525 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004526 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
4528 return TRUE; /* exit Insert mode */
4529}
4530
4531#ifdef FEAT_RIGHTLEFT
4532/*
4533 * Toggle language: hkmap and revins_on.
4534 * Move to end of reverse inserted text.
4535 */
4536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004537ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538{
4539 if (revins_on && revins_chars && revins_scol >= 0)
4540 {
4541 while (gchar_cursor() != NUL && revins_chars--)
4542 ++curwin->w_cursor.col;
4543 }
4544 p_ri = !p_ri;
4545 revins_on = (State == INSERT && p_ri);
4546 if (revins_on)
4547 {
4548 revins_scol = curwin->w_cursor.col;
4549 revins_legal++;
4550 revins_chars = 0;
4551 undisplay_dollar();
4552 }
4553 else
4554 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004555 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 showmode();
4557}
4558#endif
4559
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560/*
4561 * If 'keymodel' contains "startsel", may start selection.
4562 * Returns TRUE when a CTRL-O and other keys stuffed.
4563 */
4564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004565ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566{
4567 if (km_startsel)
4568 switch (c)
4569 {
4570 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 case K_PAGEUP:
4573 case K_KPAGEUP:
4574 case K_PAGEDOWN:
4575 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004576# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 case K_LEFT:
4578 case K_RIGHT:
4579 case K_UP:
4580 case K_DOWN:
4581 case K_END:
4582 case K_HOME:
4583# endif
4584 if (!(mod_mask & MOD_MASK_SHIFT))
4585 break;
4586 /* FALLTHROUGH */
4587 case K_S_LEFT:
4588 case K_S_RIGHT:
4589 case K_S_UP:
4590 case K_S_DOWN:
4591 case K_S_END:
4592 case K_S_HOME:
4593 /* Start selection right away, the cursor can move with
4594 * CTRL-O when beyond the end of the line. */
4595 start_selection();
4596
4597 /* Execute the key in (insert) Select mode. */
4598 stuffcharReadbuff(Ctrl_O);
4599 if (mod_mask)
4600 {
4601 char_u buf[4];
4602
4603 buf[0] = K_SPECIAL;
4604 buf[1] = KS_MODIFIER;
4605 buf[2] = mod_mask;
4606 buf[3] = NUL;
4607 stuffReadbuff(buf);
4608 }
4609 stuffcharReadbuff(c);
4610 return TRUE;
4611 }
4612 return FALSE;
4613}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004616 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004617 */
4618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004619ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004620{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004621#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004622 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004623 (char_u *)((State & REPLACE_FLAG) ? "i"
4624 : replaceState == VREPLACE ? "v"
4625 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004626#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004627 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004628 if (State & REPLACE_FLAG)
4629 State = INSERT | (State & LANGMAP);
4630 else
4631 State = replaceState | (State & LANGMAP);
4632 AppendCharToRedobuff(K_INS);
4633 showmode();
4634#ifdef CURSOR_SHAPE
4635 ui_cursor_shape(); /* may show different cursor shape */
4636#endif
4637}
4638
4639/*
4640 * Pressed CTRL-O in Insert mode.
4641 */
4642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004643ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645 if (State & VREPLACE_FLAG)
4646 restart_edit = 'V';
4647 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648 if (State & REPLACE_FLAG)
4649 restart_edit = 'R';
4650 else
4651 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652 if (virtual_active())
4653 ins_at_eol = FALSE; /* cursor always keeps its column */
4654 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004655 ins_at_eol = (gchar_cursor() == NUL);
4656}
4657
4658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 * If the cursor is on an indent, ^T/^D insert/delete one
4660 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004661 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 * with vi. But vi only supports ^T and ^D after an
4663 * autoindent, we support it everywhere.
4664 */
4665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004666ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
4668 if (stop_arrow() == FAIL)
4669 return;
4670 AppendCharToRedobuff(c);
4671
4672 /*
4673 * 0^D and ^^D: remove all indent.
4674 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004675 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4676 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
4678 --curwin->w_cursor.col;
4679 (void)del_char(FALSE); /* delete the '^' or '0' */
4680 /* In Replace mode, restore the characters that '^' or '0' replaced. */
4681 if (State & REPLACE_FLAG)
4682 replace_pop_ins();
4683 if (lastc == '^')
4684 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004685 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004688 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4691 did_ai = FALSE;
4692#ifdef FEAT_SMARTINDENT
4693 did_si = FALSE;
4694 can_si = FALSE;
4695 can_si_back = FALSE;
4696#endif
4697#ifdef FEAT_CINDENT
4698 can_cindent = FALSE; /* no cindenting after ^D or ^T */
4699#endif
4700}
4701
4702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004703ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 int temp;
4706
4707 if (stop_arrow() == FAIL)
4708 return;
4709 if (gchar_cursor() == NUL) /* delete newline */
4710 {
4711 temp = curwin->w_cursor.col;
4712 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004713 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004714 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004718 /* Adjust orig_line_count in case more lines have been deleted than
4719 * have been added. That makes sure, that open_line() later
4720 * can access all buffer lines correctly */
4721 if (State & VREPLACE_FLAG &&
4722 orig_line_count > curbuf->b_ml.ml_line_count)
4723 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02004726 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
4727 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 did_ai = FALSE;
4729#ifdef FEAT_SMARTINDENT
4730 did_si = FALSE;
4731 can_si = FALSE;
4732 can_si_back = FALSE;
4733#endif
4734 AppendCharToRedobuff(K_DEL);
4735}
4736
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004737/*
4738 * Delete one character for ins_bs().
4739 */
4740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004741ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004742{
4743 dec_cursor();
4744 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4745 if (State & REPLACE_FLAG)
4746 {
4747 /* Don't delete characters before the insert point when in
4748 * Replace mode */
4749 if (curwin->w_cursor.lnum != Insstart.lnum
4750 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004751 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004752 }
4753 else
4754 (void)del_char(FALSE);
4755}
4756
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757/*
4758 * Handle Backspace, delete-word and delete-line in Insert mode.
4759 * Return TRUE when backspace was actually used.
4760 */
4761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004762ins_bs(
4763 int c,
4764 int mode,
4765 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
4767 linenr_T lnum;
4768 int cc;
4769 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004770 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 colnr_T mincol;
4772 int did_backspace = FALSE;
4773 int in_indent;
4774 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004775 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777 /*
4778 * can't delete anything in an empty file
4779 * can't backup past first character in buffer
4780 * can't backup past starting point unless 'backspace' > 1
4781 * can backup to a previous line if 'backspace' == 0
4782 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004783 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 || (
4785#ifdef FEAT_RIGHTLEFT
4786 !revins_on &&
4787#endif
4788 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4789 || (!can_bs(BS_START)
4790 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004791 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4792 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4794 && curwin->w_cursor.col <= ai_col)
4795 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4796 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004797 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 return FALSE;
4799 }
4800
4801 if (stop_arrow() == FAIL)
4802 return FALSE;
4803 in_indent = inindent(0);
4804#ifdef FEAT_CINDENT
4805 if (in_indent)
4806 can_cindent = FALSE;
4807#endif
4808#ifdef FEAT_COMMENTS
4809 end_comment_pending = NUL; /* After BS, don't auto-end comment */
4810#endif
4811#ifdef FEAT_RIGHTLEFT
4812 if (revins_on) /* put cursor after last inserted char */
4813 inc_cursor();
4814#endif
4815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 /* Virtualedit:
4817 * BACKSPACE_CHAR eats a virtual space
4818 * BACKSPACE_WORD eats all coladd
4819 * BACKSPACE_LINE eats all coladd and keeps going
4820 */
4821 if (curwin->w_cursor.coladd > 0)
4822 {
4823 if (mode == BACKSPACE_CHAR)
4824 {
4825 --curwin->w_cursor.coladd;
4826 return TRUE;
4827 }
4828 if (mode == BACKSPACE_WORD)
4829 {
4830 curwin->w_cursor.coladd = 0;
4831 return TRUE;
4832 }
4833 curwin->w_cursor.coladd = 0;
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
4836 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004837 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 */
4839 if (curwin->w_cursor.col == 0)
4840 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004841 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004842 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843#ifdef FEAT_RIGHTLEFT
4844 || revins_on
4845#endif
4846 )
4847 {
4848 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4849 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4850 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004851 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004852 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 /*
4855 * In replace mode:
4856 * cc < 0: NL was inserted, delete it
4857 * cc >= 0: NL was replaced, put original characters back
4858 */
4859 cc = -1;
4860 if (State & REPLACE_FLAG)
4861 cc = replace_pop(); /* returns -1 if NL was inserted */
4862 /*
4863 * In replace mode, in the line we started replacing, we only move the
4864 * cursor.
4865 */
4866 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4867 {
4868 dec_cursor();
4869 }
4870 else
4871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 if (!(State & VREPLACE_FLAG)
4873 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 {
4875 temp = gchar_cursor(); /* remember current char */
4876 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004877
4878 /* When "aw" is in 'formatoptions' we must delete the space at
4879 * the end of the line, otherwise the line will be broken
4880 * again when auto-formatting. */
4881 if (has_format_option(FO_AUTO)
4882 && has_format_option(FO_WHITE_PAR))
4883 {
4884 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4885 TRUE);
4886 int len;
4887
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004888 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004889 if (len > 0 && ptr[len - 1] == ' ')
4890 ptr[len - 1] = NUL;
4891 }
4892
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004893 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if (temp == NUL && gchar_cursor() != NUL)
4895 inc_cursor();
4896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 else
4898 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
4900 /*
4901 * In REPLACE mode we have to put back the text that was replaced
4902 * by the NL. On the replace stack is first a NUL-terminated
4903 * sequence of characters that were deleted and then the
4904 * characters that NL replaced.
4905 */
4906 if (State & REPLACE_FLAG)
4907 {
4908 /*
4909 * Do the next ins_char() in NORMAL state, to
4910 * prevent ins_char() from replacing characters and
4911 * avoiding showmatch().
4912 */
4913 oldState = State;
4914 State = NORMAL;
4915 /*
4916 * restore characters (blanks) deleted after cursor
4917 */
4918 while (cc > 0)
4919 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004920 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004922 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 cc = replace_pop();
4924 }
4925 /* restore the characters that NL replaced */
4926 replace_pop_ins();
4927 State = oldState;
4928 }
4929 }
4930 did_ai = FALSE;
4931 }
4932 else
4933 {
4934 /*
4935 * Delete character(s) before the cursor.
4936 */
4937#ifdef FEAT_RIGHTLEFT
4938 if (revins_on) /* put cursor on last inserted char */
4939 dec_cursor();
4940#endif
4941 mincol = 0;
4942 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004943 if (mode == BACKSPACE_LINE
4944 && (curbuf->b_p_ai
4945#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004946 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004947#endif
4948 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#ifdef FEAT_RIGHTLEFT
4950 && !revins_on
4951#endif
4952 )
4953 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004954 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004956 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004958 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
4960
4961 /*
4962 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4963 */
4964 if ( mode == BACKSPACE_CHAR
4965 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004966 || ((get_sts_value() != 0
4967#ifdef FEAT_VARTABS
4968 || tabstop_count(curbuf->b_p_vsts_array)
4969#endif
4970 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004971 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 && (*(ml_get_cursor() - 1) == TAB
4973 || (*(ml_get_cursor() - 1) == ' '
4974 && (!*inserted_space_p
4975 || arrow_used))))))
4976 {
4977 int ts;
4978 colnr_T vcol;
4979 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004980 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
4982 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 /* Compute the virtual column where we want to be. Since
4984 * 'showbreak' may get in the way, need to get the last column of
4985 * the previous character. */
4986 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004987 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 dec_cursor();
4989 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4990 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004991#ifdef FEAT_VARTABS
4992 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004993 {
4994 ts = (int)get_sw_value(curbuf);
4995 want_vcol = (want_vcol / ts) * ts;
4996 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004997 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004998 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004999 curbuf->b_p_vsts_array);
5000#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005001 if (p_sta && in_indent)
5002 ts = (int)get_sw_value(curbuf);
5003 else
5004 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
5008 /* delete characters until we are at or before want_vcol */
5009 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01005010 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00005011 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013 /* insert extra spaces until we are at want_vcol */
5014 while (vcol < want_vcol)
5015 {
5016 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02005017 if (curwin->w_cursor.lnum == Insstart_orig.lnum
5018 && curwin->w_cursor.col < Insstart_orig.col)
5019 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (State & VREPLACE_FLAG)
5022 ins_char(' ');
5023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
5025 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00005026 if ((State & REPLACE_FLAG))
5027 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
5030 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00005031
5032 /* If we are now back where we started delete one character. Can
5033 * happen when using 'sts' and 'linebreak'. */
5034 if (vcol >= start_vcol)
5035 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037
5038 /*
5039 * Delete upto starting point, start of line or previous word.
5040 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005041 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005043 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005045 if (has_mbyte)
5046 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005047 do
5048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005050 if (!revins_on) /* put cursor on char to be deleted */
5051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005053
5054 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005055 /* look multi-byte character class */
5056 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005058 prev_cclass = cclass;
5059 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005061
5062 /* start of word? */
5063 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
5064 {
5065 mode = BACKSPACE_WORD_NOT_SPACE;
5066 temp = vim_iswordc(cc);
5067 }
5068 /* end of word? */
5069 else if (mode == BACKSPACE_WORD_NOT_SPACE
5070 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01005071 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005074 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005076 inc_cursor();
5077#ifdef FEAT_RIGHTLEFT
5078 else if (State & REPLACE_FLAG)
5079 dec_cursor();
5080#endif
5081 break;
5082 }
5083 if (State & REPLACE_FLAG)
5084 replace_do_bs(-1);
5085 else
5086 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005087 if (enc_utf8 && p_deco)
5088 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005089 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005090 /*
5091 * If there are combining characters and 'delcombine' is set
5092 * move the cursor back. Don't back up before the base
5093 * character.
5094 */
5095 if (enc_utf8 && p_deco && cpc[0] != NUL)
5096 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01005097#ifdef FEAT_RIGHTLEFT
5098 if (revins_chars)
5099 {
5100 revins_chars--;
5101 revins_legal++;
5102 }
5103 if (revins_on && gchar_cursor() == NUL)
5104 break;
5105#endif
5106 }
5107 /* Just a single backspace?: */
5108 if (mode == BACKSPACE_CHAR)
5109 break;
5110 } while (
5111#ifdef FEAT_RIGHTLEFT
5112 revins_on ||
5113#endif
5114 (curwin->w_cursor.col > mincol
5115 && (curwin->w_cursor.lnum != Insstart_orig.lnum
5116 || curwin->w_cursor.col != Insstart_orig.col)));
5117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 did_backspace = TRUE;
5119 }
5120#ifdef FEAT_SMARTINDENT
5121 did_si = FALSE;
5122 can_si = FALSE;
5123 can_si_back = FALSE;
5124#endif
5125 if (curwin->w_cursor.col <= 1)
5126 did_ai = FALSE;
5127 /*
5128 * It's a little strange to put backspaces into the redo
5129 * buffer, but it makes auto-indent a lot easier to deal
5130 * with.
5131 */
5132 AppendCharToRedobuff(c);
5133
5134 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02005135 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005136 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02005137 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 /* vi behaviour: the cursor moves backward but the character that
5140 * was there remains visible
5141 * Vim behaviour: the cursor moves backward and the character that
5142 * was there is erased from the screen.
5143 * We can emulate the vi behaviour by pretending there is a dollar
5144 * displayed even when there isn't.
5145 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005146 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 dollar_vcol = curwin->w_virtcol;
5148
Bram Moolenaarce3be472008-01-14 19:12:28 +00005149#ifdef FEAT_FOLDING
5150 /* When deleting a char the cursor line must never be in a closed fold.
5151 * E.g., when 'foldmethod' is indent and deleting the first non-white
5152 * char before a Tab. */
5153 if (did_backspace)
5154 foldOpenCursor();
5155#endif
5156
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 return did_backspace;
5158}
5159
5160#ifdef FEAT_MOUSE
5161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005162ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
5164 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005165 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166
5167# ifdef FEAT_GUI
5168 /* When GUI is active, also move/paste when 'mouse' is empty */
5169 if (!gui.in_use)
5170# endif
5171 if (!mouse_has(MOUSE_INSERT))
5172 return;
5173
5174 undisplay_dollar();
5175 tpos = curwin->w_cursor;
5176 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
5177 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005178 win_T *new_curwin = curwin;
5179
5180 if (curwin != old_curwin && win_valid(old_curwin))
5181 {
5182 /* Mouse took us to another window. We need to go back to the
5183 * previous one to stop insert there properly. */
5184 curwin = old_curwin;
5185 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005186#ifdef FEAT_JOB_CHANNEL
5187 if (bt_prompt(curbuf))
5188 // Restart Insert mode when re-entering the prompt buffer.
5189 curbuf->b_prompt_insert = 'A';
5190#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005191 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005192 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005193 if (curwin != new_curwin && win_valid(new_curwin))
5194 {
5195 curwin = new_curwin;
5196 curbuf = curwin->w_buffer;
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198# ifdef FEAT_CINDENT
5199 can_cindent = TRUE;
5200# endif
5201 }
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 /* redraw status lines (in case another window became active) */
5204 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205}
5206
5207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005208ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005211 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005212 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
5214 tpos = curwin->w_cursor;
5215
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01005216 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 int row, col;
5219
5220 row = mouse_row;
5221 col = mouse_col;
5222
5223 /* find the window at the pointer coordinates */
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02005224 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005225 if (wp == NULL)
5226 return;
5227 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 curbuf = curwin->w_buffer;
5229 }
5230 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 undisplay_dollar();
5232
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005233 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005234 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005235 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005236 if (dir == MSCR_DOWN || dir == MSCR_UP)
5237 {
5238 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5239 scroll_redraw(dir,
5240 (long)(curwin->w_botline - curwin->w_topline));
5241 else
5242 scroll_redraw(dir, 3L);
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02005243# ifdef FEAT_TEXT_PROP
5244 if (WIN_IS_POPUP(curwin))
5245 popup_set_firstline(curwin);
5246# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005247 }
5248#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005249 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005250 {
5251 int val, step = 6;
5252
5253 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02005254 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005255 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
5256 if (val < 0)
5257 val = 0;
5258 gui_do_horiz_scroll(val, TRUE);
5259 }
5260#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005261 did_scroll = TRUE;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 curwin->w_redr_status = TRUE;
5265
5266 curwin = old_curwin;
5267 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005269 /* The popup menu may overlay the window, need to redraw it.
5270 * TODO: Would be more efficient to only redraw the windows that are
5271 * overlapped by the popup menu. */
5272 if (pum_visible() && did_scroll)
5273 {
5274 redraw_all_later(NOT_VALID);
5275 ins_compl_show_pum();
5276 }
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00005277
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005278 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 start_arrow(&tpos);
5281# ifdef FEAT_CINDENT
5282 can_cindent = TRUE;
5283# endif
5284 }
5285}
5286#endif
5287
Bram Moolenaarec2da362017-01-21 20:04:22 +01005288/*
5289 * Handle receiving P_PS: start paste mode. Inserts the following text up to
5290 * P_PE literally.
5291 * When "drop" is TRUE then consume the text and drop it.
5292 */
5293 int
5294bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
5295{
5296 int c;
5297 char_u buf[NUMBUFLEN + MB_MAXBYTES];
5298 int idx = 0;
5299 char_u *end = find_termcode((char_u *)"PE");
5300 int ret_char = -1;
5301 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005302 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01005303
5304 /* If the end code is too long we can't detect it, read everything. */
5305 if (STRLEN(end) >= NUMBUFLEN)
5306 end = NULL;
5307 ++no_mapping;
5308 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005309 if (!p_paste)
5310 // Also have the side effects of setting 'paste' to make it work much
5311 // faster.
5312 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005313
Bram Moolenaarec2da362017-01-21 20:04:22 +01005314 for (;;)
5315 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005316 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01005317 if (end == NULL && vpeekc() == NUL)
5318 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005319 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005320 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01005321 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005322 if (c == NUL || got_int)
5323 // When CTRL-C was encountered the typeahead will be flushed and we
5324 // won't get the end sequence.
5325 break;
5326
Bram Moolenaarec2da362017-01-21 20:04:22 +01005327 if (has_mbyte)
5328 idx += (*mb_char2bytes)(c, buf + idx);
5329 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005330 buf[idx++] = c;
5331 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02005332 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01005333 {
5334 if (end[idx] == NUL)
5335 break; /* Found the end of paste code. */
5336 continue;
5337 }
5338 if (!drop)
5339 {
5340 switch (mode)
5341 {
5342 case PASTE_CMDLINE:
5343 put_on_cmdline(buf, idx, TRUE);
5344 break;
5345
5346 case PASTE_EX:
5347 if (gap != NULL && ga_grow(gap, idx) == OK)
5348 {
5349 mch_memmove((char *)gap->ga_data + gap->ga_len,
5350 buf, (size_t)idx);
5351 gap->ga_len += idx;
5352 }
5353 break;
5354
5355 case PASTE_INSERT:
5356 if (stop_arrow() == OK)
5357 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005358 c = buf[0];
5359 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
5360 ins_eol(c);
5361 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01005362 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005363 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01005364 AppendToRedobuffLit(buf, idx);
5365 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01005366 }
5367 break;
5368
5369 case PASTE_ONE_CHAR:
5370 if (ret_char == -1)
5371 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01005372 if (has_mbyte)
5373 ret_char = (*mb_ptr2char)(buf);
5374 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005375 ret_char = buf[0];
5376 }
5377 break;
5378 }
5379 }
5380 idx = 0;
5381 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005382
Bram Moolenaarec2da362017-01-21 20:04:22 +01005383 --no_mapping;
5384 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005385 if (!save_paste)
5386 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01005387
5388 return ret_char;
5389}
5390
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005391#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00005392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005393ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005394{
5395 /* We will be leaving the current window, unless closing another tab. */
5396 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
5397 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
5398 {
5399 undisplay_dollar();
5400 start_arrow(&curwin->w_cursor);
5401# ifdef FEAT_CINDENT
5402 can_cindent = TRUE;
5403# endif
5404 }
5405
5406 if (c == K_TABLINE)
5407 goto_tabpage(current_tab);
5408 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005409 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005410 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005411 redraw_statuslines(); /* will redraw the tabline when needed */
5412 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005413}
5414#endif
5415
5416#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005418ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419{
5420 pos_T tpos;
5421
5422 undisplay_dollar();
5423 tpos = curwin->w_cursor;
5424 if (gui_do_scroll())
5425 {
5426 start_arrow(&tpos);
5427# ifdef FEAT_CINDENT
5428 can_cindent = TRUE;
5429# endif
5430 }
5431}
5432
5433 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005434ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 pos_T tpos;
5437
5438 undisplay_dollar();
5439 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005440 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
5442 start_arrow(&tpos);
5443# ifdef FEAT_CINDENT
5444 can_cindent = TRUE;
5445# endif
5446 }
5447}
5448#endif
5449
5450 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005451ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005454 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
5456#ifdef FEAT_FOLDING
5457 if ((fdo_flags & FDO_HOR) && KeyTyped)
5458 foldOpenCursor();
5459#endif
5460 undisplay_dollar();
5461 tpos = curwin->w_cursor;
5462 if (oneleft() == OK)
5463 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005464#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5465 /* Only call start_arrow() when not busy with preediting, it will
5466 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005467 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005468#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005469 {
5470 start_arrow_with_change(&tpos, end_change);
5471 if (!end_change)
5472 AppendCharToRedobuff(K_LEFT);
5473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474#ifdef FEAT_RIGHTLEFT
5475 /* If exit reversed string, position is fixed */
5476 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5477 revins_legal++;
5478 revins_chars++;
5479#endif
5480 }
5481
5482 /*
5483 * if 'whichwrap' set for cursor in insert mode may go to
5484 * previous line
5485 */
5486 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5487 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005488 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 start_arrow(&tpos);
5490 --(curwin->w_cursor.lnum);
5491 coladvance((colnr_T)MAXCOL);
5492 curwin->w_set_curswant = TRUE; /* so we stay at the end */
5493 }
5494 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005495 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005496 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497}
5498
5499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005500ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
5502 pos_T tpos;
5503
5504#ifdef FEAT_FOLDING
5505 if ((fdo_flags & FDO_HOR) && KeyTyped)
5506 foldOpenCursor();
5507#endif
5508 undisplay_dollar();
5509 tpos = curwin->w_cursor;
5510 if (c == K_C_HOME)
5511 curwin->w_cursor.lnum = 1;
5512 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 curwin->w_curswant = 0;
5515 start_arrow(&tpos);
5516}
5517
5518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005519ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 pos_T tpos;
5522
5523#ifdef FEAT_FOLDING
5524 if ((fdo_flags & FDO_HOR) && KeyTyped)
5525 foldOpenCursor();
5526#endif
5527 undisplay_dollar();
5528 tpos = curwin->w_cursor;
5529 if (c == K_C_END)
5530 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5531 coladvance((colnr_T)MAXCOL);
5532 curwin->w_curswant = MAXCOL;
5533
5534 start_arrow(&tpos);
5535}
5536
5537 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005538ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005540 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#ifdef FEAT_FOLDING
5542 if ((fdo_flags & FDO_HOR) && KeyTyped)
5543 foldOpenCursor();
5544#endif
5545 undisplay_dollar();
5546 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5547 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005548 start_arrow_with_change(&curwin->w_cursor, end_change);
5549 if (!end_change)
5550 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 (void)bck_word(1L, FALSE, FALSE);
5552 curwin->w_set_curswant = TRUE;
5553 }
5554 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005555 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005556 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557}
5558
5559 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005560ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005562 int end_change = dont_sync_undo == FALSE; // end undoable change
5563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564#ifdef FEAT_FOLDING
5565 if ((fdo_flags & FDO_HOR) && KeyTyped)
5566 foldOpenCursor();
5567#endif
5568 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005569 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005571 start_arrow_with_change(&curwin->w_cursor, end_change);
5572 if (!end_change)
5573 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 if (virtual_active())
5576 oneright();
5577 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005580 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 ++curwin->w_cursor.col;
5583 }
5584
5585#ifdef FEAT_RIGHTLEFT
5586 revins_legal++;
5587 if (revins_chars)
5588 revins_chars--;
5589#endif
5590 }
5591 /* if 'whichwrap' set for cursor in insert mode, may move the
5592 * cursor to the next line */
5593 else if (vim_strchr(p_ww, ']') != NULL
5594 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5595 {
5596 start_arrow(&curwin->w_cursor);
5597 curwin->w_set_curswant = TRUE;
5598 ++curwin->w_cursor.lnum;
5599 curwin->w_cursor.col = 0;
5600 }
5601 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005602 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005603 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604}
5605
5606 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005607ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005609 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610#ifdef FEAT_FOLDING
5611 if ((fdo_flags & FDO_HOR) && KeyTyped)
5612 foldOpenCursor();
5613#endif
5614 undisplay_dollar();
5615 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5616 || gchar_cursor() != NUL)
5617 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005618 start_arrow_with_change(&curwin->w_cursor, end_change);
5619 if (!end_change)
5620 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 (void)fwd_word(1L, FALSE, 0);
5622 curwin->w_set_curswant = TRUE;
5623 }
5624 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005625 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005626 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627}
5628
5629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005630ins_up(
5631 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 pos_T tpos;
5634 linenr_T old_topline = curwin->w_topline;
5635#ifdef FEAT_DIFF
5636 int old_topfill = curwin->w_topfill;
5637#endif
5638
5639 undisplay_dollar();
5640 tpos = curwin->w_cursor;
5641 if (cursor_up(1L, TRUE) == OK)
5642 {
5643 if (startcol)
5644 coladvance(getvcol_nolist(&Insstart));
5645 if (old_topline != curwin->w_topline
5646#ifdef FEAT_DIFF
5647 || old_topfill != curwin->w_topfill
5648#endif
5649 )
5650 redraw_later(VALID);
5651 start_arrow(&tpos);
5652#ifdef FEAT_CINDENT
5653 can_cindent = TRUE;
5654#endif
5655 }
5656 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005657 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658}
5659
5660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005661ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 pos_T tpos;
5664
5665 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005666
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005667 if (mod_mask & MOD_MASK_CTRL)
5668 {
5669 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005670 if (first_tabpage->tp_next != NULL)
5671 {
5672 start_arrow(&curwin->w_cursor);
5673 goto_tabpage(-1);
5674 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005675 return;
5676 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 tpos = curwin->w_cursor;
5679 if (onepage(BACKWARD, 1L) == OK)
5680 {
5681 start_arrow(&tpos);
5682#ifdef FEAT_CINDENT
5683 can_cindent = TRUE;
5684#endif
5685 }
5686 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005687 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688}
5689
5690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005691ins_down(
5692 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
5694 pos_T tpos;
5695 linenr_T old_topline = curwin->w_topline;
5696#ifdef FEAT_DIFF
5697 int old_topfill = curwin->w_topfill;
5698#endif
5699
5700 undisplay_dollar();
5701 tpos = curwin->w_cursor;
5702 if (cursor_down(1L, TRUE) == OK)
5703 {
5704 if (startcol)
5705 coladvance(getvcol_nolist(&Insstart));
5706 if (old_topline != curwin->w_topline
5707#ifdef FEAT_DIFF
5708 || old_topfill != curwin->w_topfill
5709#endif
5710 )
5711 redraw_later(VALID);
5712 start_arrow(&tpos);
5713#ifdef FEAT_CINDENT
5714 can_cindent = TRUE;
5715#endif
5716 }
5717 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005718 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719}
5720
5721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005722ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 pos_T tpos;
5725
5726 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005727
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005728 if (mod_mask & MOD_MASK_CTRL)
5729 {
5730 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005731 if (first_tabpage->tp_next != NULL)
5732 {
5733 start_arrow(&curwin->w_cursor);
5734 goto_tabpage(0);
5735 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005736 return;
5737 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 tpos = curwin->w_cursor;
5740 if (onepage(FORWARD, 1L) == OK)
5741 {
5742 start_arrow(&tpos);
5743#ifdef FEAT_CINDENT
5744 can_cindent = TRUE;
5745#endif
5746 }
5747 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005748 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749}
5750
5751#ifdef FEAT_DND
5752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005753ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
5755 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5756}
5757#endif
5758
5759/*
5760 * Handle TAB in Insert or Replace mode.
5761 * Return TRUE when the TAB needs to be inserted like a normal character.
5762 */
5763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005764ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 int ind;
5767 int i;
5768 int temp;
5769
5770 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5771 Insstart_blank_vcol = get_nolist_virtcol();
5772 if (echeck_abbr(TAB + ABBR_OFF))
5773 return FALSE;
5774
5775 ind = inindent(0);
5776#ifdef FEAT_CINDENT
5777 if (ind)
5778 can_cindent = FALSE;
5779#endif
5780
5781 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005782 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 */
5784 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005785#ifdef FEAT_VARTABS
5786 && !(p_sta && ind
5787 /* These five lines mean 'tabstop' != 'shiftwidth' */
5788 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5789 || (tabstop_count(curbuf->b_p_vts_array) == 1
5790 && tabstop_first(curbuf->b_p_vts_array)
5791 != get_sw_value(curbuf))
5792 || (tabstop_count(curbuf->b_p_vts_array) == 0
5793 && curbuf->b_p_ts != get_sw_value(curbuf))))
5794 && tabstop_count(curbuf->b_p_vsts_array) == 0
5795#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005796 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005797#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005798 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 return TRUE;
5800
5801 if (stop_arrow() == FAIL)
5802 return TRUE;
5803
5804 did_ai = FALSE;
5805#ifdef FEAT_SMARTINDENT
5806 did_si = FALSE;
5807 can_si = FALSE;
5808 can_si_back = FALSE;
5809#endif
5810 AppendToRedobuff((char_u *)"\t");
5811
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005812#ifdef FEAT_VARTABS
5813 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
5814 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005815 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005816 temp -= get_nolist_virtcol() % temp;
5817 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005818 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005819 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005820 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005821 curbuf->b_p_vsts_array);
5822 else /* otherwise use 'tabstop' */
5823 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5824 curbuf->b_p_vts_array);
5825#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005827 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005828 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
5829 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 else /* otherwise use 'tabstop' */
5831 temp = (int)curbuf->b_p_ts;
5832 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
5835 /*
5836 * Insert the first space with ins_char(). It will delete one char in
5837 * replace mode. Insert the rest with ins_str(); it will not delete any
5838 * chars. For VREPLACE mode, we use ins_char() for all characters.
5839 */
5840 ins_char(' ');
5841 while (--temp > 0)
5842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 if (State & VREPLACE_FLAG)
5844 ins_char(' ');
5845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
5847 ins_str((char_u *)" ");
5848 if (State & REPLACE_FLAG) /* no char replaced */
5849 replace_push(NUL);
5850 }
5851 }
5852
5853 /*
5854 * When 'expandtab' not set: Replace spaces by TABs where possible.
5855 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005856#ifdef FEAT_VARTABS
5857 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5858 || get_sts_value() > 0
5859 || (p_sta && ind)))
5860#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005861 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
5864 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 char_u *saved_line = NULL; /* init for GCC */
5866 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 pos_T fpos;
5868 pos_T *cursor;
5869 colnr_T want_vcol, vcol;
5870 int change_col = -1;
5871 int save_list = curwin->w_p_list;
5872
5873 /*
5874 * Get the current line. For VREPLACE mode, don't make real changes
5875 * yet, just work on a copy of the line.
5876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 if (State & VREPLACE_FLAG)
5878 {
5879 pos = curwin->w_cursor;
5880 cursor = &pos;
5881 saved_line = vim_strsave(ml_get_curline());
5882 if (saved_line == NULL)
5883 return FALSE;
5884 ptr = saved_line + pos.col;
5885 }
5886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {
5888 ptr = ml_get_cursor();
5889 cursor = &curwin->w_cursor;
5890 }
5891
5892 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
5893 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5894 curwin->w_p_list = FALSE;
5895
5896 /* Find first white before the cursor */
5897 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005898 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 --fpos.col;
5901 --ptr;
5902 }
5903
5904 /* In Replace mode, don't change characters before the insert point. */
5905 if ((State & REPLACE_FLAG)
5906 && fpos.lnum == Insstart.lnum
5907 && fpos.col < Insstart.col)
5908 {
5909 ptr += Insstart.col - fpos.col;
5910 fpos.col = Insstart.col;
5911 }
5912
5913 /* compute virtual column numbers of first white and cursor */
5914 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5915 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5916
Bram Moolenaar597a4222014-06-25 14:39:50 +02005917 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5918 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01005919 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005921 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 if (vcol + i > want_vcol)
5923 break;
5924 if (*ptr != TAB)
5925 {
5926 *ptr = TAB;
5927 if (change_col < 0)
5928 {
5929 change_col = fpos.col; /* Column of first change */
5930 /* May have to adjust Insstart */
5931 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5932 Insstart.col = fpos.col;
5933 }
5934 }
5935 ++fpos.col;
5936 ++ptr;
5937 vcol += i;
5938 }
5939
5940 if (change_col >= 0)
5941 {
5942 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005943 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 /* Skip over the spaces we need. */
5946 while (vcol < want_vcol && *ptr == ' ')
5947 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005948 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 ++ptr;
5950 ++repl_off;
5951 }
5952 if (vcol > want_vcol)
5953 {
5954 /* Must have a char with 'showbreak' just before it. */
5955 --ptr;
5956 --repl_off;
5957 }
5958 fpos.col += repl_off;
5959
5960 /* Delete following spaces. */
5961 i = cursor->col - fpos.col;
5962 if (i > 0)
5963 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005964 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005966 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 for (temp = i; --temp >= 0; )
5968 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005969#ifdef FEAT_TEXT_PROP
5970 curbuf->b_ml.ml_line_len -= i;
5971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005973#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005974 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005975 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005976 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005977 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5978 (char_u *)"\t", 1);
5979 }
5980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 cursor->col -= i;
5982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 /*
5984 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5985 * backspacing over the changed spacing and then inserting the new
5986 * spacing.
5987 */
5988 if (State & VREPLACE_FLAG)
5989 {
5990 /* Backspace from real cursor to change_col */
5991 backspace_until_column(change_col);
5992
5993 /* Insert each char in saved_line from changed_col to
5994 * ptr-cursor */
5995 ins_bytes_len(saved_line + change_col,
5996 cursor->col - change_col);
5997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 }
5999
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (State & VREPLACE_FLAG)
6001 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 curwin->w_p_list = save_list;
6003 }
6004
6005 return FALSE;
6006}
6007
6008/*
6009 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02006010 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006012 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006013ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
6015 int i;
6016
6017 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02006018 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02006020 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 undisplay_dollar();
6022
6023 /*
6024 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
6025 * character under the cursor. Only push a NUL on the replace stack,
6026 * nothing to put back when the NL is deleted.
6027 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006028 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 replace_push(NUL);
6030
6031 /*
6032 * In VREPLACE mode, a NL replaces the rest of the line, and starts
6033 * replacing the next line, so we push all of the characters left on the
6034 * line onto the replace stack. This is not done here though, it is done
6035 * in open_line().
6036 */
6037
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006038 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
6039 * CTRL-O). */
6040 if (virtual_active() && curwin->w_cursor.coladd > 0)
6041 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 /* NL in reverse insert will always start in the end of
6045 * current line. */
6046 if (revins_on)
6047 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6048#endif
6049
6050 AppendToRedobuff(NL_STR);
6051 i = open_line(FORWARD,
6052#ifdef FEAT_COMMENTS
6053 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
6054#endif
6055 0, old_indent);
6056 old_indent = 0;
6057#ifdef FEAT_CINDENT
6058 can_cindent = TRUE;
6059#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00006060#ifdef FEAT_FOLDING
6061 /* When inserting a line the cursor line must never be in a closed fold. */
6062 foldOpenCursor();
6063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064
Bram Moolenaar24a2d722018-04-24 19:36:43 +02006065 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066}
6067
6068#ifdef FEAT_DIGRAPHS
6069/*
6070 * Handle digraph in insert mode.
6071 * Returns character still to be inserted, or NUL when nothing remaining to be
6072 * done.
6073 */
6074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 int c;
6078 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006079 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
6081 pc_status = PC_STATUS_UNSET;
6082 if (redrawing() && !char_avail())
6083 {
6084 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006085 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
6087 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006088 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089#ifdef FEAT_CMDL_INFO
6090 add_to_showcmd_c(Ctrl_K);
6091#endif
6092 }
6093
6094#ifdef USE_ON_FLY_SCROLL
6095 dont_scroll = TRUE; /* disallow scrolling here */
6096#endif
6097
6098 /* don't map the digraph chars. This also prevents the
6099 * mode message to be deleted when ESC is hit */
6100 ++no_mapping;
6101 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006102 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 --no_mapping;
6104 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006105 if (did_putchar)
6106 /* when the line fits in 'columns' the '?' is at the start of the next
6107 * line and will not be removed by the redraw */
6108 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02006109
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if (IS_SPECIAL(c) || mod_mask) /* special key */
6111 {
6112#ifdef FEAT_CMDL_INFO
6113 clear_showcmd();
6114#endif
6115 insert_special(c, TRUE, FALSE);
6116 return NUL;
6117 }
6118 if (c != ESC)
6119 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006120 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 if (redrawing() && !char_avail())
6122 {
6123 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006124 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
6126 if (char2cells(c) == 1)
6127 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00006128 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006130 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 }
6132#ifdef FEAT_CMDL_INFO
6133 add_to_showcmd_c(c);
6134#endif
6135 }
6136 ++no_mapping;
6137 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006138 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 --no_mapping;
6140 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02006141 if (did_putchar)
6142 /* when the line fits in 'columns' the '?' is at the start of the
6143 * next line and will not be removed by a redraw */
6144 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (cc != ESC)
6146 {
6147 AppendToRedobuff((char_u *)CTRL_V_STR);
6148 c = getdigraph(c, cc, TRUE);
6149#ifdef FEAT_CMDL_INFO
6150 clear_showcmd();
6151#endif
6152 return c;
6153 }
6154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155#ifdef FEAT_CMDL_INFO
6156 clear_showcmd();
6157#endif
6158 return NUL;
6159}
6160#endif
6161
6162/*
6163 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
6164 * Returns the char to be inserted, or NUL if none found.
6165 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02006166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
6169 int c;
6170 int temp;
6171 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006172 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
6174 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6175 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02006176 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 return NUL;
6178 }
6179
6180 /* try to advance to the cursor column */
6181 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006182 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 prev_ptr = ptr;
6184 validate_virtcol();
6185 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
6186 {
6187 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006188 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
6190 if ((colnr_T)temp > curwin->w_virtcol)
6191 ptr = prev_ptr;
6192
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006195 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 return c;
6197}
6198
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006199/*
6200 * CTRL-Y or CTRL-E typed in Insert mode.
6201 */
6202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006203ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006204{
6205 int c = tc;
6206
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006207 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006208 {
6209 if (c == Ctrl_Y)
6210 scrolldown_clamp();
6211 else
6212 scrollup_clamp();
6213 redraw_later(VALID);
6214 }
6215 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006216 {
6217 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
6218 if (c != NUL)
6219 {
6220 long tw_save;
6221
6222 /* The character must be taken literally, insert like it
6223 * was typed after a CTRL-V, and pretend 'textwidth'
6224 * wasn't set. Digits, 'o' and 'x' are special after a
6225 * CTRL-V, don't use it for these. */
6226 if (c < 256 && !isalnum(c))
6227 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
6228 tw_save = curbuf->b_p_tw;
6229 curbuf->b_p_tw = -1;
6230 insert_special(c, TRUE, FALSE);
6231 curbuf->b_p_tw = tw_save;
6232#ifdef FEAT_RIGHTLEFT
6233 revins_chars++;
6234 revins_legal++;
6235#endif
6236 c = Ctrl_V; /* pretend CTRL-V is last character */
6237 auto_format(FALSE, TRUE);
6238 }
6239 }
6240 return c;
6241}
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243#ifdef FEAT_SMARTINDENT
6244/*
6245 * Try to do some very smart auto-indenting.
6246 * Used when inserting a "normal" character.
6247 */
6248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006249ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 pos_T *pos, old_pos;
6252 char_u *ptr;
6253 int i;
6254 int temp;
6255
6256 /*
6257 * do some very smart indenting when entering '{' or '}'
6258 */
6259 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
6260 {
6261 /*
6262 * for '}' set indent equal to indent of line containing matching '{'
6263 */
6264 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
6265 {
6266 old_pos = curwin->w_cursor;
6267 /*
6268 * If the matching '{' has a ')' immediately before it (ignoring
6269 * white-space), then line up with the start of the line
6270 * containing the matching '(' if there is one. This handles the
6271 * case where an "if (..\n..) {" statement continues over multiple
6272 * lines -- webb
6273 */
6274 ptr = ml_get(pos->lnum);
6275 i = pos->col;
6276 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006277 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 ;
6279 curwin->w_cursor.lnum = pos->lnum;
6280 curwin->w_cursor.col = i;
6281 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
6282 curwin->w_cursor = *pos;
6283 i = get_indent();
6284 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006286 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 (void)set_indent(i, SIN_CHANGED);
6289 }
6290 else if (curwin->w_cursor.col > 0)
6291 {
6292 /*
6293 * when inserting '{' after "O" reduce indent, but not
6294 * more than indent of previous line
6295 */
6296 temp = TRUE;
6297 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
6298 {
6299 old_pos = curwin->w_cursor;
6300 i = get_indent();
6301 while (curwin->w_cursor.lnum > 1)
6302 {
6303 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
6304
6305 /* ignore empty lines and lines starting with '#'. */
6306 if (*ptr != '#' && *ptr != NUL)
6307 break;
6308 }
6309 if (get_indent() >= i)
6310 temp = FALSE;
6311 curwin->w_cursor = old_pos;
6312 }
6313 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006314 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316 }
6317
6318 /*
6319 * set indent of '#' always to 0
6320 */
6321 if (curwin->w_cursor.col > 0 && can_si && c == '#')
6322 {
6323 /* remember current indent for next line */
6324 old_indent = get_indent();
6325 (void)set_indent(0, SIN_CHANGED);
6326 }
6327
6328 /* Adjust ai_col, the char at this position can be deleted. */
6329 if (ai_col > curwin->w_cursor.col)
6330 ai_col = curwin->w_cursor.col;
6331}
6332#endif
6333
6334/*
6335 * Get the value that w_virtcol would have when 'list' is off.
6336 * Unless 'cpo' contains the 'L' flag.
6337 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01006338 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006339get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340{
Bram Moolenaarf9514162018-11-22 03:08:29 +01006341 // check validity of cursor in current buffer
6342 if (curwin->w_buffer == NULL
6343 || curwin->w_buffer->b_ml.ml_mfp == NULL
6344 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
6345 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
6347 return getvcol_nolist(&curwin->w_cursor);
6348 validate_virtcol();
6349 return curwin->w_virtcol;
6350}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006351
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006352#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006353/*
6354 * Handle the InsertCharPre autocommand.
6355 * "c" is the character that was typed.
6356 * Return a pointer to allocated memory with the replacement string.
6357 * Return NULL to continue inserting "c".
6358 */
6359 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006360do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006361{
Bram Moolenaar704984a2012-06-01 14:57:51 +02006362 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02006363 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01006364 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006365
6366 /* Return quickly when there is nothing to do. */
6367 if (!has_insertcharpre())
6368 return NULL;
6369
Bram Moolenaar704984a2012-06-01 14:57:51 +02006370 if (has_mbyte)
6371 buf[(*mb_char2bytes)(c, buf)] = NUL;
6372 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02006373 {
6374 buf[0] = c;
6375 buf[1] = NUL;
6376 }
6377
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006378 /* Lock the text to avoid weird things from happening. */
6379 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +02006380 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006381
Bram Moolenaar704984a2012-06-01 14:57:51 +02006382 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006383 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02006384 {
6385 /* Get the value of v:char. It may be empty or more than one
6386 * character. Only use it when changed, otherwise continue with the
6387 * original character to avoid breaking autoindent. */
6388 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
6389 res = vim_strsave(get_vim_var_str(VV_CHAR));
6390 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006391
6392 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
6393 --textlock;
6394
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01006395 // Restore the State, it may have been changed.
6396 State = save_State;
6397
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006398 return res;
6399}
6400#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006401
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006402#if defined(FEAT_CINDENT) || defined(PROTO)
6403 int
6404can_cindent_get(void)
6405{
6406 return can_cindent;
6407}
6408#endif
6409
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006410/*
6411 * Trigger "event" and take care of fixing undo.
6412 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01006413 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006414ins_apply_autocmds(event_T event)
6415{
6416 varnumber_T tick = CHANGEDTICK(curbuf);
6417 int r;
6418
6419 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
6420
6421 // If u_savesub() was called then we are not prepared to start
6422 // a new line. Call u_save() with no contents to fix that.
6423 if (tick != CHANGEDTICK(curbuf))
6424 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
6425
6426 return r;
6427}