blob: 78f5cd74fe1aea7723a70c469a1b5459ad7d2692 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#define BACKSPACE_CHAR 1
17#define BACKSPACE_WORD 2
18#define BACKSPACE_WORD_NOT_SPACE 3
19#define BACKSPACE_LINE 4
20
Bram Moolenaar7591bb32019-03-30 13:53:47 +010021/* Set when doing something for completion that may call edit() recursively,
22 * which is not allowed. */
23static int compl_busy = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024
25
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010026static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +020027#ifdef FEAT_JOB_CHANNEL
28static void init_prompt(int cmdchar_todo);
29#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static void insert_special(int, int, int);
31static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
32static void check_auto_format(int);
33static void redo_literal(int c);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010034static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +000035#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void check_spell_redraw(void);
Bram Moolenaar217ad922005-03-20 22:37:15 +000037#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
39static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010040static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010041static void replace_flush(void);
42static void replace_do_bs(int limit_col);
43static int del_char_after_col(int limit_col);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static void ins_reg(void);
45static void ins_ctrl_g(void);
46static void ins_ctrl_hat(void);
47static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010049static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010051static int ins_start_select(int c);
52static void ins_insert(int replaceState);
53static void ins_ctrl_o(void);
54static void ins_shift(int c, int lastc);
55static void ins_del(void);
56static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000057#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010058static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000059#endif
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010060static void ins_left(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010061static void ins_home(int c);
62static void ins_end(int c);
63static void ins_s_left(void);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010064static void ins_right(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010065static void ins_s_right(void);
66static void ins_up(int startcol);
67static void ins_pageup(void);
68static void ins_down(int startcol);
69static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010073static int ins_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010075static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010077static int ins_ctrl_ey(int tc);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010078#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +010080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
82static colnr_T Insstart_textlen; /* length of line when insert started */
83static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +010084static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
86static char_u *last_insert = NULL; /* the text of the previous insert,
87 K_SPECIAL and CSI are escaped */
88static int last_insert_skip; /* nr of chars in front of previous insert */
89static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +000090static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92#ifdef FEAT_CINDENT
93static int can_cindent; /* may do cindenting on this line */
94#endif
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +000097static int revins_on; /* reverse insert mode on */
98static int revins_chars; /* how much to skip after edit */
99static int revins_legal; /* was the last char 'legal'? */
100static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101#endif
102
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103static int ins_need_undo; /* call u_save() before inserting a
104 char. Set when edit() is called.
105 after that arrow_used is used. */
106
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100107static int did_add_space = FALSE; // auto_format() added an extra space
108 // under the cursor
109static int dont_sync_undo = FALSE; // CTRL-G U prevents syncing undo for
110 // the next left/right cursor key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
112/*
113 * edit(): Start inserting text.
114 *
115 * "cmdchar" can be:
116 * 'i' normal insert command
117 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100118 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 * 'R' replace command
120 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
121 * but still only one <CR> is inserted. The <Esc> is not used for redo.
122 * 'g' "gI" command.
123 * 'V' "gR" command for Virtual Replace mode.
124 * 'v' "gr" command for single character Virtual Replace mode.
125 *
126 * This function is not called recursively. For CTRL-O commands, it returns
127 * and lets the caller handle the Normal-mode command.
128 *
129 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
130 */
131 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100132edit(
133 int cmdchar,
134 int startln, /* if set, insert at start of line */
135 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136{
137 int c = 0;
138 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100139 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000140 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 int i;
143 int did_backspace = TRUE; /* previous char was backspace */
144#ifdef FEAT_CINDENT
145 int line_is_white = FALSE; /* line is empty before insert */
146#endif
147 linenr_T old_topline = 0; /* topline before insertion */
148#ifdef FEAT_DIFF
149 int old_topfill = -1;
150#endif
151 int inserted_space = FALSE; /* just inserted a space */
152 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000153 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200154#ifdef FEAT_JOB_CHANNEL
155 int cmdchar_todo = cmdchar;
156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000158 /* Remember whether editing was restarted after CTRL-O. */
159 did_restart_edit = restart_edit;
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 /* sleep before redrawing, needed for "CTRL-O :" that results in an
162 * error message */
163 check_for_delay(TRUE);
164
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100165 /* set Insstart_orig to Insstart */
166 update_Insstart_orig = TRUE;
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#ifdef HAVE_SANDBOX
169 /* Don't allow inserting in the sandbox. */
170 if (sandbox != 0)
171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100172 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 return FALSE;
174 }
175#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000176 /* Don't allow changes in the buffer while editing the cmdline. The
177 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000178 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100180 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000181 return FALSE;
182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000184 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100185 if (ins_compl_active() || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100187 emsg(_(e_secure));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000188 return FALSE;
189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 ins_compl_clear(); /* clear stuff for CTRL-X mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar843ee412004-06-30 16:16:41 +0000192 /*
193 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
194 */
195 if (cmdchar != 'r' && cmdchar != 'v')
196 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100197 pos_T save_cursor = curwin->w_cursor;
198
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100199#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000200 if (cmdchar == 'R')
201 ptr = (char_u *)"r";
202 else if (cmdchar == 'V')
203 ptr = (char_u *)"v";
204 else
205 ptr = (char_u *)"i";
206 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200207 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100208#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200209 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100210
Bram Moolenaar097c9922013-05-19 21:15:15 +0200211 /* Make sure the cursor didn't move. Do call check_cursor_col() in
212 * case the text was modified. Since Insert mode was not started yet
213 * a call to check_cursor_col() may move the cursor, especially with
214 * the "A" command, thus set State to avoid that. Also check that the
215 * line number is still valid (lines may have been deleted).
216 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100217 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100218#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200219 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100220#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200221 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100222 {
223 int save_state = State;
224
225 curwin->w_cursor = save_cursor;
226 State = INSERT;
227 check_cursor_col();
228 State = save_state;
229 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000230 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000231
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200232#ifdef FEAT_CONCEAL
233 /* Check if the cursor line needs redrawing before changing State. If
234 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200235 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200236#endif
237
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#ifdef FEAT_MOUSE
239 /*
240 * When doing a paste with the middle mouse button, Insstart is set to
241 * where the paste started.
242 */
243 if (where_paste_started.lnum != 0)
244 Insstart = where_paste_started;
245 else
246#endif
247 {
248 Insstart = curwin->w_cursor;
249 if (startln)
250 Insstart.col = 0;
251 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000252 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 Insstart_blank_vcol = MAXCOL;
254 if (!did_ai)
255 ai_col = 0;
256
257 if (cmdchar != NUL && restart_edit == 0)
258 {
259 ResetRedobuff();
260 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 if (cmdchar == 'V' || cmdchar == 'v')
262 {
263 /* "gR" or "gr" command */
264 AppendCharToRedobuff('g');
265 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
266 }
267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100269 if (cmdchar == K_PS)
270 AppendCharToRedobuff('a');
271 else
272 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 if (cmdchar == 'g') /* "gI" command */
274 AppendCharToRedobuff('I');
275 else if (cmdchar == 'r') /* "r<CR>" command */
276 count = 1; /* insert only one <CR> */
277 }
278 }
279
280 if (cmdchar == 'R')
281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 State = REPLACE;
283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 else if (cmdchar == 'V' || cmdchar == 'v')
285 {
286 State = VREPLACE;
287 replaceState = VREPLACE;
288 orig_line_count = curbuf->b_ml.ml_line_count;
289 vr_lines_changed = 1;
290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 else
292 State = INSERT;
293
294 stop_insert_mode = FALSE;
295
296 /*
297 * Need to recompute the cursor position, it might move when the cursor is
298 * on a TAB or special character.
299 */
300 curs_columns(TRUE);
301
302 /*
303 * Enable langmap or IME, indicated by 'iminsert'.
304 * Note that IME may enabled/disabled without us noticing here, thus the
305 * 'iminsert' value may not reflect what is actually used. It is updated
306 * when hitting <Esc>.
307 */
308 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
309 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100310#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
312#endif
313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifdef FEAT_CMDL_INFO
316 clear_showcmd();
317#endif
318#ifdef FEAT_RIGHTLEFT
319 /* there is no reverse replace mode */
320 revins_on = (State == INSERT && p_ri);
321 if (revins_on)
322 undisplay_dollar();
323 revins_chars = 0;
324 revins_legal = 0;
325 revins_scol = -1;
326#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100327 if (!p_ek)
328 /* Disable bracketed paste mode, we won't recognize the escape
329 * sequences. */
330 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
332 /*
333 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100334 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
335 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 */
337 if (restart_edit != 0 && stuff_empty())
338 {
339#ifdef FEAT_MOUSE
340 /*
341 * After a paste we consider text typed to be part of the insert for
342 * the pasted text. You can backspace over the pasted text too.
343 */
344 if (where_paste_started.lnum)
345 arrow_used = FALSE;
346 else
347#endif
348 arrow_used = TRUE;
349 restart_edit = 0;
350
351 /*
352 * If the cursor was after the end-of-line before the CTRL-O and it is
353 * now at the end-of-line, put it after the end-of-line (this is not
354 * correct in very rare cases).
355 * Also do this if curswant is greater than the current virtual
356 * column. Eg after "^O$" or "^O80|".
357 */
358 validate_virtcol();
359 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000360 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 || curwin->w_curswant > curwin->w_virtcol)
362 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
363 {
364 if (ptr[1] == NUL)
365 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 else if (has_mbyte)
367 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000368 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 if (ptr[i] == NUL)
370 curwin->w_cursor.col += i;
371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000373 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
375 else
376 arrow_used = FALSE;
377
378 /* we are in insert mode now, don't need to start it anymore */
379 need_start_insertmode = FALSE;
380
381 /* Need to save the line for undo before inserting the first char. */
382 ins_need_undo = TRUE;
383
384#ifdef FEAT_MOUSE
385 where_paste_started.lnum = 0;
386#endif
387#ifdef FEAT_CINDENT
388 can_cindent = TRUE;
389#endif
390#ifdef FEAT_FOLDING
391 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
392 * restarting. */
393 if (!p_im && did_restart_edit == 0)
394 foldOpenCursor();
395#endif
396
397 /*
398 * If 'showmode' is set, show the current (insert/replace/..) mode.
399 * A warning message for changing a readonly file is given here, before
400 * actually changing anything. It's put after the mode, if any.
401 */
402 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000403 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 i = showmode();
405
406 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000407 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
409#ifdef CURSOR_SHAPE
410 ui_cursor_shape(); /* may show different cursor shape */
411#endif
412#ifdef FEAT_DIGRAPHS
413 do_digraph(-1); /* clear digraphs */
414#endif
415
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000416 /*
417 * Get the current length of the redo buffer, those characters have to be
418 * skipped if we want to get to the inserted characters.
419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 ptr = get_inserted();
421 if (ptr == NULL)
422 new_insert_skip = 0;
423 else
424 {
425 new_insert_skip = (int)STRLEN(ptr);
426 vim_free(ptr);
427 }
428
429 old_indent = 0;
430
431 /*
432 * Main loop in Insert mode: repeat until Insert mode is left.
433 */
434 for (;;)
435 {
436#ifdef FEAT_RIGHTLEFT
437 if (!revins_legal)
438 revins_scol = -1; /* reset on illegal motions */
439 else
440 revins_legal = 0;
441#endif
442 if (arrow_used) /* don't repeat insert when arrow key used */
443 count = 0;
444
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100445 if (update_Insstart_orig)
446 Insstart_orig = Insstart;
447
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200448 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {
450 /* ":stopinsert" used or 'insertmode' reset */
451 count = 0;
452 goto doESCkey;
453 }
454
455 /* set curwin->w_curswant for next K_DOWN or K_UP */
456 if (!arrow_used)
457 curwin->w_set_curswant = TRUE;
458
459 /* If there is no typeahead may check for timestamps (e.g., for when a
460 * menu invoked a shell command). */
461 if (stuff_empty())
462 {
463 did_check_timestamps = FALSE;
464 if (need_check_timestamps)
465 check_timestamps(FALSE);
466 }
467
468 /*
469 * When emsg() was called msg_scroll will have been set.
470 */
471 msg_scroll = FALSE;
472
473#ifdef FEAT_GUI
474 /* When 'mousefocus' is set a mouse movement may have taken us to
475 * another window. "need_mouse_correct" may then be set because of an
476 * autocommand. */
477 if (need_mouse_correct)
478 gui_mouse_correct();
479#endif
480
481#ifdef FEAT_FOLDING
482 /* Open fold at the cursor line, according to 'foldopen'. */
483 if (fdo_flags & FDO_INSERT)
484 foldOpenCursor();
485 /* Close folds where the cursor isn't, according to 'foldclose' */
486 if (!char_avail())
487 foldCheckClose();
488#endif
489
Bram Moolenaarf2732452018-06-03 14:47:35 +0200490#ifdef FEAT_JOB_CHANNEL
491 if (bt_prompt(curbuf))
492 {
493 init_prompt(cmdchar_todo);
494 cmdchar_todo = NUL;
495 }
496#endif
497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 /*
499 * If we inserted a character at the last position of the last line in
500 * the window, scroll the window one line up. This avoids an extra
501 * redraw.
502 * This is detected when the cursor column is smaller after inserting
503 * something.
504 * Don't do this when the topline changed already, it has
505 * already been adjusted (by insertchar() calling open_line())).
506 */
507 if (curbuf->b_mod_set
508 && curwin->w_p_wrap
509 && !did_backspace
510 && curwin->w_topline == old_topline
511#ifdef FEAT_DIFF
512 && curwin->w_topfill == old_topfill
513#endif
514 )
515 {
516 mincol = curwin->w_wcol;
517 validate_cursor_col();
518
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200519 if (
520#ifdef FEAT_VARTABS
521 (int)curwin->w_wcol < mincol - tabstop_at(
522 get_nolist_virtcol(), curbuf->b_p_ts,
523 curbuf->b_p_vts_array)
524#else
525 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100528 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 && (curwin->w_cursor.lnum != curwin->w_topline
530#ifdef FEAT_DIFF
531 || curwin->w_topfill > 0
532#endif
533 ))
534 {
535#ifdef FEAT_DIFF
536 if (curwin->w_topfill > 0)
537 --curwin->w_topfill;
538 else
539#endif
540#ifdef FEAT_FOLDING
541 if (hasFolding(curwin->w_topline, NULL, &old_topline))
542 set_topline(curwin, old_topline + 1);
543 else
544#endif
545 set_topline(curwin, curwin->w_topline + 1);
546 }
547 }
548
549 /* May need to adjust w_topline to show the cursor. */
550 update_topline();
551
552 did_backspace = FALSE;
553
554 validate_cursor(); /* may set must_redraw */
555
556 /*
557 * Redraw the display when no characters are waiting.
558 * Also shows mode, ruler and positions cursor.
559 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000560 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (curwin->w_p_scb)
563 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar860cae12010-06-05 23:22:07 +0200565 if (curwin->w_p_crb)
566 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 update_curswant();
568 old_topline = curwin->w_topline;
569#ifdef FEAT_DIFF
570 old_topfill = curwin->w_topfill;
571#endif
572
573#ifdef USE_ON_FLY_SCROLL
574 dont_scroll = FALSE; /* allow scrolling here */
575#endif
576
577 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100578 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100580 if (c != K_CURSORHOLD)
581 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200582
583 /* After using CTRL-G U the next cursor key will not break undo. */
584 if (dont_sync_undo == MAYBE)
585 dont_sync_undo = TRUE;
586 else
587 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100588 if (cmdchar == K_PS)
589 /* Got here from normal mode when bracketed paste started. */
590 c = K_PS;
591 else
592 do
593 {
594 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200595
596 if (stop_insert_mode)
597 {
598 // Insert mode ended, possibly from a callback.
599 count = 0;
600 nomove = TRUE;
601 goto doESCkey;
602 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100603 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000605 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
606 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#ifdef FEAT_RIGHTLEFT
609 if (p_hkmap && KeyTyped)
610 c = hkmap(c); /* Hebrew mode mapping */
611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000613 /*
614 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000615 * and the cursor is still in the completed word. Only when there is
616 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000617 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100618 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000619 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100620 && curwin->w_cursor.col >= ins_compl_col()
621 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000622 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000623 /* BS: Delete one character from "compl_leader". */
624 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100625 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000626 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000627 continue;
628
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000629 /* When no match was selected or it was edited. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100630 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000631 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000632 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000633 * "compl_leader". Except when at the original match and
634 * there is nothing to add, CTRL-L works like CTRL-P then. */
635 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100636 && (!ctrl_x_mode_line_or_eval()
637 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000638 {
639 ins_compl_addfrommatch();
640 continue;
641 }
642
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000643 /* A non-white character that fits in with the current
644 * completion: Add to "compl_leader". */
645 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000646 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100647#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100648 /* Trigger InsertCharPre. */
649 char_u *str = do_insert_char_pre(c);
650 char_u *p;
651
652 if (str != NULL)
653 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100654 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100655 ins_compl_addleader(PTR2CHAR(p));
656 vim_free(str);
657 }
658 else
659#endif
660 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000661 continue;
662 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000663
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000664 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100665 * ins_compl_enter_selects() is set the Enter key does the
666 * same. */
667 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200668 && (c == CAR || c == K_KENTER || c == NL)))
669 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000670 {
671 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200672 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000673 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000674 }
675 }
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
678 * it does fix up the text when finishing completion. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100679 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000680 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000681 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
Bram Moolenaar488c6512005-08-11 20:09:58 +0000683 /* CTRL-\ CTRL-N goes to Normal mode,
684 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
685 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if (c == Ctrl_BSL)
687 {
688 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000689 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 ++no_mapping;
691 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000692 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 --no_mapping;
694 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000695 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000697 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 vungetc(c);
699 c = Ctrl_BSL;
700 }
701 else if (c == Ctrl_G && p_im)
702 continue;
703 else
704 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000705 if (c == Ctrl_O)
706 {
707 ins_ctrl_o();
708 ins_at_eol = FALSE; /* cursor keeps its column */
709 nomove = TRUE;
710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 count = 0;
712 goto doESCkey;
713 }
714 }
715
716#ifdef FEAT_DIGRAPHS
717 c = do_digraph(c);
718#endif
719
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100720 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (c == Ctrl_V || c == Ctrl_Q)
723 {
724 ins_ctrl_v();
725 c = Ctrl_V; /* pretend CTRL-V is last typed character */
726 continue;
727 }
728
729#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200730 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {
732 /* A key name preceded by a bang means this key is not to be
733 * inserted. Skip ahead to the re-indenting below.
734 * A key name preceded by a star means that indenting has to be
735 * done before inserting the key. */
736 line_is_white = inindent(0);
737 if (in_cinkeys(c, '!', line_is_white))
738 goto force_cindent;
739 if (can_cindent && in_cinkeys(c, '*', line_is_white)
740 && stop_arrow() == OK)
741 do_c_expr_indent();
742 }
743#endif
744
745#ifdef FEAT_RIGHTLEFT
746 if (curwin->w_p_rl)
747 switch (c)
748 {
749 case K_LEFT: c = K_RIGHT; break;
750 case K_S_LEFT: c = K_S_RIGHT; break;
751 case K_C_LEFT: c = K_C_RIGHT; break;
752 case K_RIGHT: c = K_LEFT; break;
753 case K_S_RIGHT: c = K_S_LEFT; break;
754 case K_C_RIGHT: c = K_C_LEFT; break;
755 }
756#endif
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /*
759 * If 'keymodel' contains "startsel", may start selection. If it
760 * does, a CTRL-O and c will be stuffed, we need to get these
761 * characters.
762 */
763 if (ins_start_select(c))
764 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 /*
767 * The big switch to handle a character in insert mode.
768 */
769 switch (c)
770 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000771 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (echeck_abbr(ESC + ABBR_OFF))
773 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200774 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000776 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#ifdef FEAT_CMDWIN
778 if (c == Ctrl_C && cmdwin_type != 0)
779 {
780 /* Close the cmdline window. */
781 cmdwin_result = K_IGNORE;
782 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000783 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 goto doESCkey;
785 }
786#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200787#ifdef FEAT_JOB_CHANNEL
788 if (c == Ctrl_C && bt_prompt(curbuf))
789 {
790 if (invoke_prompt_interrupt())
791 {
792 if (!bt_prompt(curbuf))
793 // buffer changed to a non-prompt buffer, get out of
794 // Insert mode
795 goto doESCkey;
796 break;
797 }
798 }
799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801#ifdef UNIX
802do_intr:
803#endif
804 /* when 'insertmode' set, and not halfway a mapping, don't leave
805 * Insert mode */
806 if (goto_im())
807 {
808 if (got_int)
809 {
810 (void)vgetc(); /* flush all buffers */
811 got_int = FALSE;
812 }
813 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200814 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 break;
816 }
817doESCkey:
818 /*
819 * This is the ONLY return from edit()!
820 */
821 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
822 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000823 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 o_lnum = curwin->w_cursor.lnum;
825
Bram Moolenaar488c6512005-08-11 20:09:58 +0000826 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000827 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100828 // When CTRL-C was typed got_int will be set, with the result
829 // that the autocommands won't be executed. When mapped got_int
830 // is not set, but let's keep the behavior the same.
831 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200832 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000833 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 continue;
837
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000838 case Ctrl_Z: /* suspend when 'insertmode' set */
839 if (!p_im)
840 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100841 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100842#ifdef CURSOR_SHAPE
843 ui_cursor_shape(); /* may need to update cursor shape */
844#endif
845 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000846
847 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000848#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100849 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000850 goto docomplete;
851#endif
852 if (echeck_abbr(Ctrl_O + ABBR_OFF))
853 break;
854 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000855
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000856 /* don't move the cursor left when 'virtualedit' has "onemore". */
857 if (ve_flags & VE_ONEMORE)
858 {
859 ins_at_eol = FALSE;
860 nomove = TRUE;
861 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000862 count = 0;
863 goto doESCkey;
864
Bram Moolenaar572cb562005-08-05 21:35:02 +0000865 case K_INS: /* toggle insert/replace mode */
866 case K_KINS:
867 ins_insert(replaceState);
868 break;
869
870 case K_SELECT: /* end of Select mode mapping - ignore */
871 break;
872
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000873 case K_HELP: /* Help key works like <ESC> <Help> */
874 case K_F1:
875 case K_XF1:
876 stuffcharReadbuff(K_HELP);
877 if (p_im)
878 need_start_insertmode = TRUE;
879 goto doESCkey;
880
881#ifdef FEAT_NETBEANS_INTG
882 case K_F21: /* NetBeans command */
883 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000884 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 --no_mapping;
886 netbeans_keycommand(i);
887 break;
888#endif
889
890 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 case NUL:
892 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000893 /* For ^@ the trailing ESC will end the insert, unless there is an
894 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
896 && c != Ctrl_A && !p_im)
897 goto doESCkey; /* quit insert mode */
898 inserted_space = FALSE;
899 break;
900
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000901 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ins_reg();
903 auto_format(FALSE, TRUE);
904 inserted_space = FALSE;
905 break;
906
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000907 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 ins_ctrl_g();
909 break;
910
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000911 case Ctrl_HAT: /* switch input mode and/or langmap */
912 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 break;
914
915#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000916 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (!p_ari)
918 goto normalchar;
919 ins_ctrl_();
920 break;
921#endif
922
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000923 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200924#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 goto docomplete;
927#endif
928 /* FALLTHROUGH */
929
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000930 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100931 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000933 if (has_compl_option(FALSE))
934 goto docomplete;
935 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ins_shift(c, lastc);
939 auto_format(FALSE, TRUE);
940 inserted_space = FALSE;
941 break;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 case K_KDEL:
945 ins_del();
946 auto_format(FALSE, TRUE);
947 break;
948
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 case Ctrl_H:
951 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
952 auto_format(FALSE, TRUE);
953 break;
954
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200956#ifdef FEAT_JOB_CHANNEL
957 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
958 {
959 // In a prompt window CTRL-W is used for window commands.
960 // Use Shift-CTRL-W to delete a word.
961 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200962 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200963 nomove = TRUE;
964 count = 0;
965 goto doESCkey;
966 }
967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
969 auto_format(FALSE, TRUE);
970 break;
971
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000972 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000973# ifdef FEAT_COMPL_FUNC
974 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100975 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000976 goto docomplete;
977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
979 auto_format(FALSE, TRUE);
980 inserted_space = FALSE;
981 break;
982
983#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000984 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 case K_LEFTMOUSE_NM:
986 case K_LEFTDRAG:
987 case K_LEFTRELEASE:
988 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100989 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 case K_MIDDLEMOUSE:
991 case K_MIDDLEDRAG:
992 case K_MIDDLERELEASE:
993 case K_RIGHTMOUSE:
994 case K_RIGHTDRAG:
995 case K_RIGHTRELEASE:
996 case K_X1MOUSE:
997 case K_X1DRAG:
998 case K_X1RELEASE:
999 case K_X2MOUSE:
1000 case K_X2DRAG:
1001 case K_X2RELEASE:
1002 ins_mouse(c);
1003 break;
1004
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001005 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001006 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 break;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001010 ins_mousescroll(MSCR_UP);
1011 break;
1012
1013 case K_MOUSELEFT: /* Scroll wheel left */
1014 ins_mousescroll(MSCR_LEFT);
1015 break;
1016
1017 case K_MOUSERIGHT: /* Scroll wheel right */
1018 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 break;
1020#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001021 case K_PS:
1022 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1023 if (cmdchar == K_PS)
1024 /* invoked from normal mode, bail out */
1025 goto doESCkey;
1026 break;
1027 case K_PE:
1028 /* Got K_PE without K_PS, ignore. */
1029 break;
1030
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001031#ifdef FEAT_GUI_TABLINE
1032 case K_TABLINE:
1033 case K_TABMENU:
1034 ins_tabline(c);
1035 break;
1036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 break;
1040
Bram Moolenaar754b5602006-02-09 23:53:20 +00001041 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001042 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001043 did_cursorhold = TRUE;
1044 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001045
Bram Moolenaar4f974752019-02-17 17:44:42 +01001046#ifdef FEAT_GUI_MSWIN
1047 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1048 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001049 case K_F4:
1050 if (mod_mask != MOD_MASK_ALT)
1051 goto normalchar;
1052 break;
1053#endif
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055#ifdef FEAT_GUI
1056 case K_VER_SCROLLBAR:
1057 ins_scroll();
1058 break;
1059
1060 case K_HOR_SCROLLBAR:
1061 ins_horscroll();
1062 break;
1063#endif
1064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 case K_S_HOME:
1068 case K_C_HOME:
1069 ins_home(c);
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 case K_S_END:
1075 case K_C_END:
1076 ins_end(c);
1077 break;
1078
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001080 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1081 ins_s_left();
1082 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001083 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 break;
1085
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 case K_C_LEFT:
1088 ins_s_left();
1089 break;
1090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001091 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001092 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1093 ins_s_right();
1094 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001095 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 break;
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 case K_C_RIGHT:
1100 ins_s_right();
1101 break;
1102
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001103 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001104 if (pum_visible())
1105 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001106 if (mod_mask & MOD_MASK_SHIFT)
1107 ins_pageup();
1108 else
1109 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 case K_PAGEUP:
1114 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001115 if (pum_visible())
1116 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 ins_pageup();
1118 break;
1119
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001121 if (pum_visible())
1122 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001123 if (mod_mask & MOD_MASK_SHIFT)
1124 ins_pagedown();
1125 else
1126 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 break;
1128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 case K_PAGEDOWN:
1131 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001132 if (pum_visible())
1133 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 ins_pagedown();
1135 break;
1136
1137#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 ins_drop();
1140 break;
1141#endif
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 c = TAB;
1145 /* FALLTHROUGH */
1146
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001147 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001148#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001149 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 goto docomplete;
1151#endif
1152 inserted_space = FALSE;
1153 if (ins_tab())
1154 goto normalchar; /* insert TAB as a normal char */
1155 auto_format(FALSE, TRUE);
1156 break;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 c = CAR;
1160 /* FALLTHROUGH */
1161 case CAR:
1162 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001163#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 /* In a quickfix window a <CR> jumps to the error under the
1165 * cursor. */
1166 if (bt_quickfix(curbuf) && c == CAR)
1167 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001168 if (curwin->w_llist_ref == NULL) /* quickfix window */
1169 do_cmdline_cmd((char_u *)".cc");
1170 else /* location list window */
1171 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 break;
1173 }
1174#endif
1175#ifdef FEAT_CMDWIN
1176 if (cmdwin_type != 0)
1177 {
1178 /* Execute the command in the cmdline window. */
1179 cmdwin_result = CAR;
1180 goto doESCkey;
1181 }
1182#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001183#ifdef FEAT_JOB_CHANNEL
1184 if (bt_prompt(curbuf))
1185 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001186 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001187 if (!bt_prompt(curbuf))
1188 // buffer changed to a non-prompt buffer, get out of
1189 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001190 goto doESCkey;
1191 break;
1192 }
1193#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001194 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 goto doESCkey; /* out of memory */
1196 auto_format(FALSE, FALSE);
1197 inserted_space = FALSE;
1198 break;
1199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001201 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001203 if (has_compl_option(TRUE))
1204 goto docomplete;
1205 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001207#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 c = ins_digraph();
1209 if (c == NUL)
1210 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001212 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
Bram Moolenaar572cb562005-08-05 21:35:02 +00001214 case Ctrl_X: /* Enter CTRL-X mode */
1215 ins_ctrl_x();
1216 break;
1217
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001218 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001219 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 goto normalchar;
1221 goto docomplete;
1222
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001223 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001224 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 goto normalchar;
1226 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001227
1228 case 's': /* Spelling completion after ^X */
1229 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001230 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001231 goto normalchar;
1232 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001235 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
1237 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1238 if (p_im)
1239 {
1240 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1241 break;
1242 goto doESCkey;
1243 }
1244 goto normalchar;
1245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 /* FALLTHROUGH */
1247
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 case Ctrl_N:
1250 /* if 'complete' is empty then plain ^P is no longer special,
1251 * but it is under other ^X modes */
1252 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001253 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001254 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 goto normalchar;
1256
1257docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001258 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001259#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001260 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001261#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001262 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001264#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001265 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001266#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001267 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case Ctrl_Y: /* copy from previous line or scroll down */
1271 case Ctrl_E: /* copy from next line or scroll up */
1272 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 break;
1274
1275 default:
1276#ifdef UNIX
1277 if (c == intr_char) /* special interrupt char */
1278 goto do_intr;
1279#endif
1280
Bram Moolenaare659c952011-05-19 17:25:41 +02001281normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001283 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001285#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001286 if (!p_paste)
1287 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001288 /* Trigger InsertCharPre. */
1289 char_u *str = do_insert_char_pre(c);
1290 char_u *p;
1291
1292 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001293 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001294 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001295 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001296 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001297 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001298 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001299 c = PTR2CHAR(p);
1300 if (c == CAR || c == K_KENTER || c == NL)
1301 ins_eol(c);
1302 else
1303 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001304 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001305 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001306 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001307 vim_free(str);
1308 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001309 }
1310
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001311 /* If the new value is already inserted or an empty string
1312 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001313 if (c == NUL)
1314 break;
1315 }
1316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317#ifdef FEAT_SMARTINDENT
1318 /* Try to perform smart-indenting. */
1319 ins_try_si(c);
1320#endif
1321
1322 if (c == ' ')
1323 {
1324 inserted_space = TRUE;
1325#ifdef FEAT_CINDENT
1326 if (inindent(0))
1327 can_cindent = FALSE;
1328#endif
1329 if (Insstart_blank_vcol == MAXCOL
1330 && curwin->w_cursor.lnum == Insstart.lnum)
1331 Insstart_blank_vcol = get_nolist_virtcol();
1332 }
1333
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001334 /* Insert a normal character and check for abbreviations on a
1335 * special character. Let CTRL-] expand abbreviations without
1336 * inserting it. */
1337 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001338 // Add ABBR_OFF for characters above 0x100, this is
1339 // what check_abbr() expects.
1340 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1341 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
1343 insert_special(c, FALSE, FALSE);
1344#ifdef FEAT_RIGHTLEFT
1345 revins_legal++;
1346 revins_chars++;
1347#endif
1348 }
1349
1350 auto_format(FALSE, TRUE);
1351
1352#ifdef FEAT_FOLDING
1353 /* When inserting a character the cursor line must never be in a
1354 * closed fold. */
1355 foldOpenCursor();
1356#endif
1357 break;
1358 } /* end of switch (c) */
1359
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001360 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001361 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001362#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001363 /* but not in CTRL-X mode, a script can't restore the state */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001364 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001365#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001366 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001367 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 /* If the cursor was moved we didn't just insert a space */
1370 if (arrow_used)
1371 inserted_space = FALSE;
1372
1373#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001374 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
1376force_cindent:
1377 /*
1378 * Indent now if a key was typed that is in 'cinkeys'.
1379 */
1380 if (in_cinkeys(c, ' ', line_is_white))
1381 {
1382 if (stop_arrow() == OK)
1383 /* re-indent the current line */
1384 do_c_expr_indent();
1385 }
1386 }
1387#endif /* FEAT_CINDENT */
1388
1389 } /* for (;;) */
1390 /* NOTREACHED */
1391}
1392
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001393 int
1394ins_need_undo_get(void)
1395{
1396 return ins_need_undo;
1397}
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399/*
1400 * Redraw for Insert mode.
1401 * This is postponed until getting the next character to make '$' in the 'cpo'
1402 * option work correctly.
1403 * Only redraw when there are no characters available. This speeds up
1404 * inserting sequences of characters (e.g., for CTRL-R).
1405 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001406 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001407ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001409#ifdef FEAT_CONCEAL
1410 linenr_T conceal_old_cursor_line = 0;
1411 linenr_T conceal_new_cursor_line = 0;
1412 int conceal_update_lines = FALSE;
1413#endif
1414
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001415 if (char_avail())
1416 return;
1417
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001418 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1419 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001420 if (ready && (has_cursormovedI()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001421# ifdef FEAT_TEXT_PROP
1422 || popup_visible
1423# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001424# if defined(FEAT_CONCEAL)
1425 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001426# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001427 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001428 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001429 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001430 {
1431# ifdef FEAT_SYN_HL
1432 /* Need to update the screen first, to make sure syntax
1433 * highlighting is correct after making a change (e.g., inserting
1434 * a "(". The autocommand may also require a redraw, so it's done
1435 * again below, unfortunately. */
1436 if (syntax_present(curwin) && must_redraw)
1437 update_screen(0);
1438# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001439 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001440 {
1441 /* Make sure curswant is correct, an autocommand may call
1442 * getcurpos(). */
1443 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001444 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001445 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001446#ifdef FEAT_TEXT_PROP
1447 if (popup_visible)
1448 popup_check_cursor_pos();
1449#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001450# ifdef FEAT_CONCEAL
1451 if (curwin->w_p_cole > 0)
1452 {
1453 conceal_old_cursor_line = last_cursormoved.lnum;
1454 conceal_new_cursor_line = curwin->w_cursor.lnum;
1455 conceal_update_lines = TRUE;
1456 }
1457# endif
1458 last_cursormoved = curwin->w_cursor;
1459 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001460
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001461 /* Trigger TextChangedI if b_changedtick differs. */
1462 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001463 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001464 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001465 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001466 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001467 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001468
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001469 // save and restore curwin and curbuf, in case the autocmd changes them
1470 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001471 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001472 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001473 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001474 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1475 u_save(curwin->w_cursor.lnum,
1476 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001478
Bram Moolenaar5a093432018-02-10 18:15:19 +01001479 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1480 * TextChangedI will need to trigger for backwards compatibility, thus use
1481 * different b_last_changedtick* variables. */
1482 if (ready && has_textchangedP()
1483 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1484 && pum_visible())
1485 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001486 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001487 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001488
1489 // save and restore curwin and curbuf, in case the autocmd changes them
1490 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001491 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001492 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001493 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001494 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1495 u_save(curwin->w_cursor.lnum,
1496 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001497 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001498
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001499 // Trigger SafeState if nothing is pending.
1500 may_trigger_safestate(ready
1501 && !ins_compl_active()
1502 && !pum_visible());
1503
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001504#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001505 if ((conceal_update_lines
1506 && (conceal_old_cursor_line != conceal_new_cursor_line
1507 || conceal_cursor_line(curwin)))
1508 || need_cursor_line_redraw)
1509 {
1510 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001511 redrawWinline(curwin, conceal_old_cursor_line);
1512 redrawWinline(curwin, conceal_new_cursor_line == 0
1513 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001514 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001515 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001516 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001517#endif
1518 if (must_redraw)
1519 update_screen(0);
1520 else if (clear_cmdline || redraw_cmdline)
1521 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001522 showruler(FALSE);
1523 setcursor();
1524 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525}
1526
1527/*
1528 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1529 */
1530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001531ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001534 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
1536 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001537 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
1539 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001542 did_putchar = TRUE;
1543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1545
1546#ifdef FEAT_CMDL_INFO
1547 add_to_showcmd_c(Ctrl_V);
1548#endif
1549
1550 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001551 if (did_putchar)
1552 /* when the line fits in 'columns' the '^' is at the start of the next
1553 * line and will not removed by the redraw */
1554 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#ifdef FEAT_CMDL_INFO
1556 clear_showcmd();
1557#endif
1558 insert_special(c, FALSE, TRUE);
1559#ifdef FEAT_RIGHTLEFT
1560 revins_chars++;
1561 revins_legal++;
1562#endif
1563}
1564
1565/*
1566 * Put a character directly onto the screen. It's not stored in a buffer.
1567 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1568 */
1569static int pc_status;
1570#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1571#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1572#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1573#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575static int pc_attr;
1576static int pc_row;
1577static int pc_col;
1578
1579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001580edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 int attr;
1583
1584 if (ScreenLines != NULL)
1585 {
1586 update_topline(); /* just in case w_topline isn't valid */
1587 validate_cursor();
1588 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001589 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 else
1591 attr = 0;
1592 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001593 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595#ifdef FEAT_RIGHTLEFT
1596 if (curwin->w_p_rl)
1597 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001598 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (has_mbyte)
1600 {
1601 int fix_col = mb_fix_col(pc_col, pc_row);
1602
1603 if (fix_col != pc_col)
1604 {
1605 screen_putchar(' ', pc_row, fix_col, attr);
1606 --curwin->w_wcol;
1607 pc_status = PC_STATUS_RIGHT;
1608 }
1609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 else
1612#endif
1613 {
1614 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (mb_lefthalve(pc_row, pc_col))
1616 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 }
1618
1619 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1623 pc_status = PC_STATUS_SET;
1624 }
1625 screen_putchar(c, pc_row, pc_col, attr);
1626 }
1627}
1628
Bram Moolenaarf2732452018-06-03 14:47:35 +02001629#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1630/*
1631 * Return the effective prompt for the current buffer.
1632 */
1633 char_u *
1634prompt_text(void)
1635{
1636 if (curbuf->b_prompt_text == NULL)
1637 return (char_u *)"% ";
1638 return curbuf->b_prompt_text;
1639}
1640
1641/*
1642 * Prepare for prompt mode: Make sure the last line has the prompt text.
1643 * Move the cursor to this line.
1644 */
1645 static void
1646init_prompt(int cmdchar_todo)
1647{
1648 char_u *prompt = prompt_text();
1649 char_u *text;
1650
1651 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1652 text = ml_get_curline();
1653 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1654 {
1655 // prompt is missing, insert it or append a line with it
1656 if (*text == NUL)
1657 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1658 else
1659 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1660 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1661 coladvance((colnr_T)MAXCOL);
1662 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1663 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001664
1665 // Insert always starts after the prompt, allow editing text after it.
1666 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1667 || Insstart_orig.col != (int)STRLEN(prompt))
1668 {
1669 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001670 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001671 Insstart_orig = Insstart;
1672 Insstart_textlen = Insstart.col;
1673 Insstart_blank_vcol = MAXCOL;
1674 arrow_used = FALSE;
1675 }
1676
Bram Moolenaarf2732452018-06-03 14:47:35 +02001677 if (cmdchar_todo == 'A')
1678 coladvance((colnr_T)MAXCOL);
1679 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001680 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001681 /* Make sure the cursor is in a valid position. */
1682 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001683}
1684
1685/*
1686 * Return TRUE if the cursor is in the editable position of the prompt line.
1687 */
1688 int
1689prompt_curpos_editable()
1690{
1691 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1692 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1693}
1694#endif
1695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * Undo the previous edit_putchar().
1698 */
1699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (pc_status == PC_STATUS_RIGHT)
1705 ++curwin->w_wcol;
1706 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001707 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1710 }
1711}
1712
1713/*
1714 * Called when p_dollar is set: display a '$' at the end of the changed text
1715 * Only works when cursor is in the line that changes.
1716 */
1717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001718display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 colnr_T save_col;
1721
1722 if (!redrawing())
1723 return;
1724
1725 cursor_off();
1726 save_col = curwin->w_cursor.col;
1727 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (has_mbyte)
1729 {
1730 char_u *p;
1731
1732 /* If on the last byte of a multi-byte move to the first byte. */
1733 p = ml_get_curline();
1734 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001737 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
1739 edit_putchar('$', FALSE);
1740 dollar_vcol = curwin->w_virtcol;
1741 }
1742 curwin->w_cursor.col = save_col;
1743}
1744
1745/*
1746 * Call this function before moving the cursor from the normal insert position
1747 * in insert mode.
1748 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001749 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001752 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001754 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001755 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 }
1757}
1758
1759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 * Truncate the space at the end of a line. This is to be used only in an
1761 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1762 * modes.
1763 */
1764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 int i;
1768
1769 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001770 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
1772 if (State & REPLACE_FLAG)
1773 replace_join(0); /* remove a NUL from the replace stack */
1774 }
1775 line[i + 1] = NUL;
1776}
1777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778/*
1779 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1780 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001781 * Will attempt not to go before "col" even when there is a composing
1782 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 */
1784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 while ((int)curwin->w_cursor.col > col)
1788 {
1789 curwin->w_cursor.col--;
1790 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001791 replace_do_bs(col);
1792 else if (!del_char_after_col(col))
1793 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
1795}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001797/*
1798 * Like del_char(), but make sure not to go before column "limit_col".
1799 * Only matters when there are composing characters.
1800 * Return TRUE when something was deleted.
1801 */
1802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001804{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001805 if (enc_utf8 && limit_col >= 0)
1806 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001807 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001808
1809 /* Make sure the cursor is at the start of a character, but
1810 * skip forward again when going too far back because of a
1811 * composing character. */
1812 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001813 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001814 {
1815 int l = utf_ptr2len(ml_get_cursor());
1816
1817 if (l == 0) /* end of line */
1818 break;
1819 curwin->w_cursor.col += l;
1820 }
1821 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1822 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001823 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001824 }
1825 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001826 (void)del_char(FALSE);
1827 return TRUE;
1828}
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830/*
1831 * Next character is interpreted literally.
1832 * A one, two or three digit decimal number is interpreted as its byte value.
1833 * If one or two digits are entered, the next character is given to vungetc().
1834 * For Unicode a character > 255 may be returned.
1835 */
1836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
1839 int cc;
1840 int nc;
1841 int i;
1842 int hex = FALSE;
1843 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
1846 if (got_int)
1847 return Ctrl_C;
1848
1849#ifdef FEAT_GUI
1850 /*
1851 * In GUI there is no point inserting the internal code for a special key.
1852 * It is more useful to insert the string "<KEY>" instead. This would
1853 * probably be useful in a text window too, but it would not be
1854 * vi-compatible (maybe there should be an option for it?) -- webb
1855 */
1856 if (gui.in_use)
1857 ++allow_keys;
1858#endif
1859#ifdef USE_ON_FLY_SCROLL
1860 dont_scroll = TRUE; /* disallow scrolling here */
1861#endif
1862 ++no_mapping; /* don't map the next key hits */
1863 cc = 0;
1864 i = 0;
1865 for (;;)
1866 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001867 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001869 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 add_to_showcmd(nc);
1871#endif
1872 if (nc == 'x' || nc == 'X')
1873 hex = TRUE;
1874 else if (nc == 'o' || nc == 'O')
1875 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 else if (nc == 'u' || nc == 'U')
1877 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 else
1879 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001880 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
1882 if (!vim_isxdigit(nc))
1883 break;
1884 cc = cc * 16 + hex2nr(nc);
1885 }
1886 else if (octal)
1887 {
1888 if (nc < '0' || nc > '7')
1889 break;
1890 cc = cc * 8 + nc - '0';
1891 }
1892 else
1893 {
1894 if (!VIM_ISDIGIT(nc))
1895 break;
1896 cc = cc * 10 + nc - '0';
1897 }
1898
1899 ++i;
1900 }
1901
Bram Moolenaar13505972019-01-24 15:04:48 +01001902 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 cc = 255; /* limit range to 0-255 */
1904 nc = 0;
1905
1906 if (hex) /* hex: up to two chars */
1907 {
1908 if (i >= 2)
1909 break;
1910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 else if (unicode) /* Unicode: up to four or eight chars */
1912 {
1913 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1914 break;
1915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 else if (i >= 3) /* decimal or octal: up to three chars */
1917 break;
1918 }
1919 if (i == 0) /* no number entered */
1920 {
1921 if (nc == K_ZERO) /* NUL is stored as NL */
1922 {
1923 cc = '\n';
1924 nc = 0;
1925 }
1926 else
1927 {
1928 cc = nc;
1929 nc = 0;
1930 }
1931 }
1932
1933 if (cc == 0) /* NUL is stored as NL */
1934 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001935 if (enc_dbcs && (cc & 0xff) == 0)
1936 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
1937 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
1939 --no_mapping;
1940#ifdef FEAT_GUI
1941 if (gui.in_use)
1942 --allow_keys;
1943#endif
1944 if (nc)
1945 vungetc(nc);
1946 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
1947 return cc;
1948}
1949
1950/*
1951 * Insert character, taking care of special keys and mod_mask
1952 */
1953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001954insert_special(
1955 int c,
1956 int allow_modmask,
1957 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 char_u *p;
1960 int len;
1961
1962 /*
1963 * Special function key, translate into "<Key>". Up to the last '>' is
1964 * inserted with ins_str(), so as not to replace characters in replace
1965 * mode.
1966 * Only use mod_mask for special keys, to avoid things like <S-Space>,
1967 * unless 'allow_modmask' is TRUE.
1968 */
Bram Moolenaard0573012017-10-28 21:11:06 +02001969#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 /* Command-key never produces a normal key */
1971 if (mod_mask & MOD_MASK_CMD)
1972 allow_modmask = TRUE;
1973#endif
1974 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
1975 {
1976 p = get_special_key_name(c, mod_mask);
1977 len = (int)STRLEN(p);
1978 c = p[len - 1];
1979 if (len > 2)
1980 {
1981 if (stop_arrow() == FAIL)
1982 return;
1983 p[len - 1] = NUL;
1984 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00001985 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 ctrlv = FALSE;
1987 }
1988 }
1989 if (stop_arrow() == OK)
1990 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
1991}
1992
1993/*
1994 * Special characters in this context are those that need processing other
1995 * than the simple insertion that can be performed here. This includes ESC
1996 * which terminates the insert, and CR/NL which need special processing to
1997 * open up a new line. This routine tries to optimize insertions performed by
1998 * the "redo", "undo" or "put" commands, so it needs to know when it should
1999 * stop and defer processing to the "normal" mechanism.
2000 * '0' and '^' are special, because they can be followed by CTRL-D.
2001 */
2002#ifdef EBCDIC
2003# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2004#else
2005# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2006#endif
2007
Bram Moolenaar13505972019-01-24 15:04:48 +01002008#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002010/*
2011 * "flags": INSCHAR_FORMAT - force formatting
2012 * INSCHAR_CTRLV - char typed just after CTRL-V
2013 * INSCHAR_NO_FEX - don't use 'formatexpr'
2014 *
2015 * NOTE: passes the flags value straight through to internal_format() which,
2016 * beside INSCHAR_FORMAT (above), is also looking for these:
2017 * INSCHAR_DO_COM - format comments
2018 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002021insertchar(
2022 int c, /* character to insert or NUL */
2023 int flags, /* INSCHAR_FORMAT, etc. */
2024 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002029 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002031 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
2034 /*
2035 * Try to break the line in two or more pieces when:
2036 * - Always do this if we have been called to do formatting only.
2037 * - Always do this when 'formatoptions' has the 'a' flag and the line
2038 * ends in white space.
2039 * - Otherwise:
2040 * - Don't do this if inserting a blank
2041 * - Don't do this if an existing character is being replaced, unless
2042 * we're in VREPLACE mode.
2043 * - Do this if the cursor is not on the line where insert started
2044 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2045 * before the insert.
2046 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2047 * before 'textwidth'
2048 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002049 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002050 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002051 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 && *ml_get_cursor() != NUL)
2055 && (curwin->w_cursor.lnum != Insstart.lnum
2056 || ((!has_format_option(FO_INS_LONG)
2057 || Insstart_textlen <= (colnr_T)textwidth)
2058 && (!fo_ins_blank
2059 || Insstart_blank_vcol <= (colnr_T)textwidth
2060 ))))))
2061 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002062 /* Format with 'formatexpr' when it's set. Use internal formatting
2063 * when 'formatexpr' isn't set or it returns non-zero. */
2064#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002065 int do_internal = TRUE;
2066 colnr_T virtcol = get_nolist_virtcol()
2067 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002068
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002069 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2070 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002071 {
2072 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
2073 /* It may be required to save for undo again, e.g. when setline()
2074 * was called. */
2075 ins_need_undo = TRUE;
2076 }
2077 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002079 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002081
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (c == NUL) /* only formatting was wanted */
2083 return;
2084
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002085 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (did_ai && (int)c == end_comment_pending)
2087 {
2088 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002089 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 int middle_len, end_len;
2091 int i;
2092
2093 /*
2094 * Need to remove existing (middle) comment leader and insert end
2095 * comment leader. First, check what comment leader we can find.
2096 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002097 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002098 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002100 // Skip middle-comment string
2101 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 ++p;
2103 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002104 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002105 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 --middle_len;
2107
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002108 // Find the end-comment string
2109 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 ++p;
2111 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2112
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002113 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002115 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 ;
2117 i++;
2118
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002119 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 i -= middle_len;
2121
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002122 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2124 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002125 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 backspace_until_column(i);
2127
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002128 // Insert the end-comment string, except for the last
2129 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 ins_bytes_len(lead_end, end_len - 1);
2131 }
2132 }
2133 }
2134 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136 did_ai = FALSE;
2137#ifdef FEAT_SMARTINDENT
2138 did_si = FALSE;
2139 can_si = FALSE;
2140 can_si_back = FALSE;
2141#endif
2142
2143 /*
2144 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2145 * This speeds up normal text input considerably.
2146 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2147 * need to re-indent at a ':', or any other character (but not what
2148 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002149 * Don't do this when there an InsertCharPre autocommand is defined,
2150 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002151 * Do the check for InsertCharPre before the call to vpeekc() because the
2152 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 */
2154#ifdef USE_ON_FLY_SCROLL
2155 dont_scroll = FALSE; /* allow scrolling here */
2156#endif
2157
2158 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002160 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 && vpeekc() != NUL
2162 && !(State & REPLACE_FLAG)
2163#ifdef FEAT_CINDENT
2164 && !cindent_on()
2165#endif
2166#ifdef FEAT_RIGHTLEFT
2167 && !p_ri
2168#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002169 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
2171#define INPUT_BUFLEN 100
2172 char_u buf[INPUT_BUFLEN + 1];
2173 int i;
2174 colnr_T virtcol = 0;
2175
2176 buf[0] = c;
2177 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002178 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 virtcol = get_nolist_virtcol();
2180 /*
2181 * Stop the string when:
2182 * - no more chars available
2183 * - finding a special character (command key)
2184 * - buffer is full
2185 * - running into the 'textwidth' boundary
2186 * - need to check for abbreviation: A non-word char after a word-char
2187 */
2188 while ( (c = vpeekc()) != NUL
2189 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 && i < INPUT_BUFLEN
2192 && (textwidth == 0
2193 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2194 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2195 {
2196#ifdef FEAT_RIGHTLEFT
2197 c = vgetc();
2198 if (p_hkmap && KeyTyped)
2199 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 buf[i++] = c;
2201#else
2202 buf[i++] = vgetc();
2203#endif
2204 }
2205
2206#ifdef FEAT_DIGRAPHS
2207 do_digraph(-1); /* clear digraphs */
2208 do_digraph(buf[i-1]); /* may be the start of a digraph */
2209#endif
2210 buf[i] = NUL;
2211 ins_str(buf);
2212 if (flags & INSCHAR_CTRLV)
2213 {
2214 redo_literal(*buf);
2215 i = 1;
2216 }
2217 else
2218 i = 0;
2219 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002220 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
2222 else
2223 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002224 int cc;
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2227 {
2228 char_u buf[MB_MAXBYTES + 1];
2229
2230 (*mb_char2bytes)(c, buf);
2231 buf[cc] = NUL;
2232 ins_char_bytes(buf, cc);
2233 AppendCharToRedobuff(c);
2234 }
2235 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 ins_char(c);
2238 if (flags & INSCHAR_CTRLV)
2239 redo_literal(c);
2240 else
2241 AppendCharToRedobuff(c);
2242 }
2243 }
2244}
2245
2246/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002247 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002248 *
2249 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2250 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002251 */
2252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002253internal_format(
2254 int textwidth,
2255 int second_indent,
2256 int flags,
2257 int format_only,
2258 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002259{
2260 int cc;
2261 int save_char = NUL;
2262 int haveto_redraw = FALSE;
2263 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002264 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002265 int fo_white_par = has_format_option(FO_WHITE_PAR);
2266 int first_line = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002267 colnr_T leader_len;
2268 int no_leader = FALSE;
2269 int do_comments = (flags & INSCHAR_DO_COM);
Bram Moolenaar0026d472014-09-09 16:32:39 +02002270#ifdef FEAT_LINEBREAK
2271 int has_lbr = curwin->w_p_lbr;
2272
2273 /* make sure win_lbr_chartabsize() counts correctly */
2274 curwin->w_p_lbr = FALSE;
2275#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002276
2277 /*
2278 * When 'ai' is off we don't want a space under the cursor to be
2279 * deleted. Replace it with an 'x' temporarily.
2280 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002281 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002282 {
2283 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002284 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002285 {
2286 save_char = cc;
2287 pchar_cursor('x');
2288 }
2289 }
2290
2291 /*
2292 * Repeat breaking lines, until the current line is not too long.
2293 */
2294 while (!got_int)
2295 {
2296 int startcol; /* Cursor column at entry */
2297 int wantcol; /* column at textwidth border */
2298 int foundcol; /* column for start of spaces */
2299 int end_foundcol = 0; /* column for start of word */
2300 colnr_T len;
2301 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002302 int orig_col = 0;
2303 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002304 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002305 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002306 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002307
Bram Moolenaar97b98102009-11-17 16:41:01 +00002308 virtcol = get_nolist_virtcol()
2309 + char2cells(c != NUL ? c : gchar_cursor());
2310 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002311 break;
2312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002313 if (no_leader)
2314 do_comments = FALSE;
2315 else if (!(flags & INSCHAR_FORMAT)
2316 && has_format_option(FO_WRAP_COMS))
2317 do_comments = TRUE;
2318
2319 /* Don't break until after the comment leader */
2320 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002321 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002322 else
2323 leader_len = 0;
2324
2325 /* If the line doesn't start with a comment leader, then don't
2326 * start one in a following broken line. Avoids that a %word
2327 * moved to the start of the next line causes all following lines
2328 * to start with %. */
2329 if (leader_len == 0)
2330 no_leader = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002331 if (!(flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002332 && leader_len == 0
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002333 && !has_format_option(FO_WRAP))
2334
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002335 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002336 if ((startcol = curwin->w_cursor.col) == 0)
2337 break;
2338
2339 /* find column of textwidth border */
2340 coladvance((colnr_T)textwidth);
2341 wantcol = curwin->w_cursor.col;
2342
Bram Moolenaar97b98102009-11-17 16:41:01 +00002343 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002344 foundcol = 0;
2345
2346 /*
2347 * Find position to break at.
2348 * Stop at first entered white when 'formatoptions' has 'v'
2349 */
2350 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002351 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002352 || curwin->w_cursor.lnum != Insstart.lnum
2353 || curwin->w_cursor.col >= Insstart.col)
2354 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002355 if (curwin->w_cursor.col == startcol && c != NUL)
2356 cc = c;
2357 else
2358 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002359 if (WHITECHAR(cc))
2360 {
2361 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002362 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002363
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002364 // find start of sequence of blanks
2365 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002366 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2367 {
2368 dec_cursor();
2369 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002370
2371 // Increment count of how many whitespace chars in this
2372 // group; we only need to know if it's more than one.
2373 if (wcc < 2)
2374 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002375 }
2376 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
2377 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002378
2379 // Don't break after a period when 'formatoptions' has 'p' and
2380 // there are less than two spaces.
2381 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2382 continue;
2383
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002384 /* Don't break until after the comment leader */
2385 if (curwin->w_cursor.col < leader_len)
2386 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002387 if (has_format_option(FO_ONE_LETTER))
2388 {
2389 /* do not break after one-letter words */
2390 if (curwin->w_cursor.col == 0)
2391 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002392 /* do not break "#a b" when 'tw' is 2 */
2393 if (curwin->w_cursor.col <= leader_len)
2394 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002395 col = curwin->w_cursor.col;
2396 dec_cursor();
2397 cc = gchar_cursor();
2398
2399 if (WHITECHAR(cc))
2400 continue; /* one-letter, continue */
2401 curwin->w_cursor.col = col;
2402 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002403
2404 inc_cursor();
2405
2406 end_foundcol = end_col + 1;
2407 foundcol = curwin->w_cursor.col;
2408 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002409 break;
2410 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002411 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002412 {
2413 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002414 if (curwin->w_cursor.col != startcol)
2415 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002416 /* Don't break until after the comment leader */
2417 if (curwin->w_cursor.col < leader_len)
2418 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002419 col = curwin->w_cursor.col;
2420 inc_cursor();
2421 /* Don't change end_foundcol if already set. */
2422 if (foundcol != curwin->w_cursor.col)
2423 {
2424 foundcol = curwin->w_cursor.col;
2425 end_foundcol = foundcol;
2426 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2427 break;
2428 }
2429 curwin->w_cursor.col = col;
2430 }
2431
2432 if (curwin->w_cursor.col == 0)
2433 break;
2434
2435 col = curwin->w_cursor.col;
2436
2437 dec_cursor();
2438 cc = gchar_cursor();
2439
2440 if (WHITECHAR(cc))
2441 continue; /* break with space */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002442 /* Don't break until after the comment leader */
2443 if (curwin->w_cursor.col < leader_len)
2444 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002445
2446 curwin->w_cursor.col = col;
2447
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002448 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002449 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002450 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2451 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002452 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002453 if (curwin->w_cursor.col == 0)
2454 break;
2455 dec_cursor();
2456 }
2457
2458 if (foundcol == 0) /* no spaces, cannot break line */
2459 {
2460 curwin->w_cursor.col = startcol;
2461 break;
2462 }
2463
2464 /* Going to break the line, remove any "$" now. */
2465 undisplay_dollar();
2466
2467 /*
2468 * Offset between cursor position and line break is used by replace
2469 * stack functions. VREPLACE does not use this, and backspaces
2470 * over the text instead.
2471 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002472 if (State & VREPLACE_FLAG)
2473 orig_col = startcol; /* Will start backspacing from here */
2474 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002475 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002476
2477 /*
2478 * adjust startcol for spaces that will be deleted and
2479 * characters that will remain on top line
2480 */
2481 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002482 while ((cc = gchar_cursor(), WHITECHAR(cc))
2483 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002484 inc_cursor();
2485 startcol -= curwin->w_cursor.col;
2486 if (startcol < 0)
2487 startcol = 0;
2488
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002489 if (State & VREPLACE_FLAG)
2490 {
2491 /*
2492 * In VREPLACE mode, we will backspace over the text to be
2493 * wrapped, so save a copy now to put on the next line.
2494 */
2495 saved_text = vim_strsave(ml_get_cursor());
2496 curwin->w_cursor.col = orig_col;
2497 if (saved_text == NULL)
2498 break; /* Can't do it, out of memory */
2499 saved_text[startcol] = NUL;
2500
2501 /* Backspace over characters that will move to the next line */
2502 if (!fo_white_par)
2503 backspace_until_column(foundcol);
2504 }
2505 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002506 {
2507 /* put cursor after pos. to break line */
2508 if (!fo_white_par)
2509 curwin->w_cursor.col = foundcol;
2510 }
2511
2512 /*
2513 * Split the line just before the margin.
2514 * Only insert/delete lines, but don't really redraw the window.
2515 */
2516 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2517 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002518 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002519 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002520 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2521 if (!(flags & INSCHAR_COM_LIST))
2522 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002523
2524 replace_offset = 0;
2525 if (first_line)
2526 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002527 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002528 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002529 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002530 * This section is for auto-wrap of numeric lists. When not
2531 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2532 * flag will be set and open_line() will handle it (as seen
2533 * above). The code here (and in get_number_indent()) will
2534 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002535 */
2536 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002537 second_indent =
2538 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002539 if (second_indent >= 0)
2540 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002541 if (State & VREPLACE_FLAG)
2542 change_indent(INDENT_SET, second_indent,
2543 FALSE, NUL, TRUE);
2544 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002545 if (leader_len > 0 && second_indent - leader_len > 0)
2546 {
2547 int i;
2548 int padding = second_indent - leader_len;
2549
2550 /* We started at the first_line of a numbered list
2551 * that has a comment. the open_line() function has
2552 * inserted the proper comment leader and positioned
2553 * the cursor at the end of the split line. Now we
2554 * add the additional whitespace needed after the
2555 * comment leader for the numbered list. */
2556 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002557 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002558 }
2559 else
2560 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002561 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002562 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002563 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002564 }
2565 first_line = FALSE;
2566 }
2567
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002568 if (State & VREPLACE_FLAG)
2569 {
2570 /*
2571 * In VREPLACE mode we have backspaced over the text to be
2572 * moved, now we re-insert it into the new line.
2573 */
2574 ins_bytes(saved_text);
2575 vim_free(saved_text);
2576 }
2577 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002578 {
2579 /*
2580 * Check if cursor is not past the NUL off the line, cindent
2581 * may have added or removed indent.
2582 */
2583 curwin->w_cursor.col += startcol;
2584 len = (colnr_T)STRLEN(ml_get_curline());
2585 if (curwin->w_cursor.col > len)
2586 curwin->w_cursor.col = len;
2587 }
2588
2589 haveto_redraw = TRUE;
2590#ifdef FEAT_CINDENT
2591 can_cindent = TRUE;
2592#endif
2593 /* moved the cursor, don't autoindent or cindent now */
2594 did_ai = FALSE;
2595#ifdef FEAT_SMARTINDENT
2596 did_si = FALSE;
2597 can_si = FALSE;
2598 can_si_back = FALSE;
2599#endif
2600 line_breakcheck();
2601 }
2602
2603 if (save_char != NUL) /* put back space after cursor */
2604 pchar_cursor(save_char);
2605
Bram Moolenaar0026d472014-09-09 16:32:39 +02002606#ifdef FEAT_LINEBREAK
2607 curwin->w_p_lbr = has_lbr;
2608#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002609 if (!format_only && haveto_redraw)
2610 {
2611 update_topline();
2612 redraw_curbuf_later(VALID);
2613 }
2614}
2615
2616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 * Called after inserting or deleting text: When 'formatoptions' includes the
2618 * 'a' flag format from the current line until the end of the paragraph.
2619 * Keep the cursor at the same position relative to the text.
2620 * The caller must have saved the cursor line for undo, following ones will be
2621 * saved here.
2622 */
2623 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002624auto_format(
2625 int trailblank, /* when TRUE also format with trailing blank */
2626 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 pos_T pos;
2629 colnr_T len;
2630 char_u *old;
2631 char_u *new, *pnew;
2632 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002633 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 if (!has_format_option(FO_AUTO))
2636 return;
2637
2638 pos = curwin->w_cursor;
2639 old = ml_get_curline();
2640
2641 /* may remove added space */
2642 check_auto_format(FALSE);
2643
2644 /* Don't format in Insert mode when the cursor is on a trailing blank, the
2645 * user might insert normal text next. Also skip formatting when "1" is
2646 * in 'formatoptions' and there is a single character before the cursor.
2647 * Otherwise the line would be broken and when typing another non-white
2648 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002649 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (*old != NUL && !trailblank && wasatend)
2651 {
2652 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002653 cc = gchar_cursor();
2654 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2655 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002657 cc = gchar_cursor();
2658 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
2660 curwin->w_cursor = pos;
2661 return;
2662 }
2663 curwin->w_cursor = pos;
2664 }
2665
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
2667 * comments. */
2668 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002669 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 /*
2673 * May start formatting in a previous line, so that after "x" a word is
2674 * moved to the previous line if it fits there now. Only when this is not
2675 * the start of a paragraph.
2676 */
2677 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2678 {
2679 --curwin->w_cursor.lnum;
2680 if (u_save_cursor() == FAIL)
2681 return;
2682 }
2683
2684 /*
2685 * Do the formatting and restore the cursor position. "saved_cursor" will
2686 * be adjusted for the text formatting.
2687 */
2688 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002689 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 curwin->w_cursor = saved_cursor;
2691 saved_cursor.lnum = 0;
2692
2693 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2694 {
2695 /* "cannot happen" */
2696 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2697 coladvance((colnr_T)MAXCOL);
2698 }
2699 else
2700 check_cursor_col();
2701
2702 /* Insert mode: If the cursor is now after the end of the line while it
2703 * previously wasn't, the line was broken. Because of the rule above we
2704 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2705 * formatted. */
2706 if (!wasatend && has_format_option(FO_WHITE_PAR))
2707 {
2708 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002709 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (curwin->w_cursor.col == len)
2711 {
2712 pnew = vim_strnsave(new, len + 2);
2713 pnew[len] = ' ';
2714 pnew[len + 1] = NUL;
2715 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
2716 /* remove the space later */
2717 did_add_space = TRUE;
2718 }
2719 else
2720 /* may remove added space */
2721 check_auto_format(FALSE);
2722 }
2723
2724 check_cursor();
2725}
2726
2727/*
2728 * When an extra space was added to continue a paragraph for auto-formatting,
2729 * delete it now. The space must be under the cursor, just after the insert
2730 * position.
2731 */
2732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002733check_auto_format(
2734 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
2736 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002737 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 if (did_add_space)
2740 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002741 cc = gchar_cursor();
2742 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 /* Somehow the space was removed already. */
2744 did_add_space = FALSE;
2745 else
2746 {
2747 if (!end_insert)
2748 {
2749 inc_cursor();
2750 c = gchar_cursor();
2751 dec_cursor();
2752 }
2753 if (c != NUL)
2754 {
2755 /* The space is no longer at the end of the line, delete it. */
2756 del_char(FALSE);
2757 did_add_space = FALSE;
2758 }
2759 }
2760 }
2761}
2762
2763/*
2764 * Find out textwidth to be used for formatting:
2765 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02002766 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 * if invalid value, use 0.
2768 * Set default to window width (maximum 79) for "gq" operator.
2769 */
2770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002771comp_textwidth(
2772 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
2774 int textwidth;
2775
2776 textwidth = curbuf->b_p_tw;
2777 if (textwidth == 0 && curbuf->b_p_wm)
2778 {
2779 /* The width is the window width minus 'wrapmargin' minus all the
2780 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02002781 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782#ifdef FEAT_CMDWIN
2783 if (cmdwin_type != 0)
2784 textwidth -= 1;
2785#endif
2786#ifdef FEAT_FOLDING
2787 textwidth -= curwin->w_p_fdc;
2788#endif
2789#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002790 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 textwidth -= 1;
2792#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02002793 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 textwidth -= 8;
2795 }
2796 if (textwidth < 0)
2797 textwidth = 0;
2798 if (ff && textwidth == 0)
2799 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002800 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (textwidth > 79)
2802 textwidth = 79;
2803 }
2804 return textwidth;
2805}
2806
2807/*
2808 * Put a character in the redo buffer, for when just after a CTRL-V.
2809 */
2810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002811redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
2813 char_u buf[10];
2814
2815 /* Only digits need special treatment. Translate them into a string of
2816 * three digits. */
2817 if (VIM_ISDIGIT(c))
2818 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002819 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 AppendToRedobuff(buf);
2821 }
2822 else
2823 AppendCharToRedobuff(c);
2824}
2825
2826/*
2827 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002828 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002831start_arrow(
2832 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002834 start_arrow_common(end_insert_pos, TRUE);
2835}
2836
2837/*
2838 * Like start_arrow() but with end_change argument.
2839 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2840 */
2841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002842start_arrow_with_change(
2843 pos_T *end_insert_pos, /* can be NULL */
2844 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002845{
2846 start_arrow_common(end_insert_pos, end_change);
2847 if (!end_change)
2848 {
2849 AppendCharToRedobuff(Ctrl_G);
2850 AppendCharToRedobuff('U');
2851 }
2852}
2853
2854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002855start_arrow_common(
2856 pos_T *end_insert_pos, /* can be NULL */
2857 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002858{
2859 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
2861 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002862 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 arrow_used = TRUE; /* this means we stopped the current insert */
2864 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002865#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002866 check_spell_redraw();
2867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868}
2869
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002870#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002871/*
2872 * If we skipped highlighting word at cursor, do it now.
2873 * It may be skipped again, thus reset spell_redraw_lnum first.
2874 */
2875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002876check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002877{
2878 if (spell_redraw_lnum != 0)
2879 {
2880 linenr_T lnum = spell_redraw_lnum;
2881
2882 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002883 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002884 }
2885}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002886
Bram Moolenaar217ad922005-03-20 22:37:15 +00002887#endif
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889/*
2890 * stop_arrow() is called before a change is made in insert mode.
2891 * If an arrow key has been used, start a new insertion.
2892 * Returns FAIL if undo is impossible, shouldn't insert then.
2893 */
2894 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002895stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
2897 if (arrow_used)
2898 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002899 Insstart = curwin->w_cursor; /* new insertion starts here */
2900 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
2901 /* Don't update the original insert position when moved to the
2902 * right, except when nothing was inserted yet. */
2903 update_Insstart_orig = FALSE;
2904 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2905
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 if (u_save_cursor() == OK)
2907 {
2908 arrow_used = FALSE;
2909 ins_need_undo = FALSE;
2910 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002911
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (State & VREPLACE_FLAG)
2914 {
2915 orig_line_count = curbuf->b_ml.ml_line_count;
2916 vr_lines_changed = 1;
2917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 ResetRedobuff();
2919 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002920 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
2922 else if (ins_need_undo)
2923 {
2924 if (u_save_cursor() == OK)
2925 ins_need_undo = FALSE;
2926 }
2927
2928#ifdef FEAT_FOLDING
2929 /* Always open fold at the cursor line when inserting something. */
2930 foldOpenCursor();
2931#endif
2932
2933 return (arrow_used || ins_need_undo ? FAIL : OK);
2934}
2935
2936/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002937 * Do a few things to stop inserting.
2938 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2939 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 */
2941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002942stop_insert(
2943 pos_T *end_insert_pos,
2944 int esc, /* called by ins_esc() */
2945 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002947 int cc;
2948 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 stop_redo_ins();
2951 replace_flush(); /* abandon replace stack */
2952
2953 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002954 * Save the inserted text for later redo with ^@ and CTRL-A.
2955 * Don't do it when "restart_edit" was set and nothing was inserted,
2956 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002958 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002959 if (did_restart_edit == 0 || (ptr != NULL
2960 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002961 {
2962 vim_free(last_insert);
2963 last_insert = ptr;
2964 last_insert_skip = new_insert_skip;
2965 }
2966 else
2967 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002969 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 {
2971 /* Auto-format now. It may seem strange to do this when stopping an
2972 * insertion (or moving the cursor), but it's required when appending
2973 * a line and having it end in a space. But only do it when something
2974 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002975 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002977 pos_T tpos = curwin->w_cursor;
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 /* When the cursor is at the end of the line after a space the
2980 * formatting will move it to the following word. Avoid that by
2981 * moving the cursor onto the space. */
2982 cc = 'x';
2983 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
2984 {
2985 dec_cursor();
2986 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002987 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002988 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 }
2990
2991 auto_format(TRUE, FALSE);
2992
Bram Moolenaar1c465442017-03-12 20:10:05 +01002993 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002994 {
2995 if (gchar_cursor() != NUL)
2996 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002997 // If the cursor is still at the same character, also keep
2998 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002999 if (gchar_cursor() == NUL
3000 && curwin->w_cursor.lnum == tpos.lnum
3001 && curwin->w_cursor.col == tpos.col)
3002 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
3005
3006 /* If a space was inserted for auto-formatting, remove it now. */
3007 check_auto_format(TRUE);
3008
3009 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003010 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00003011 * Do this when ESC was used or moving the cursor up/down.
3012 * Check for the old position still being valid, just in case the text
3013 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01003014 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003015 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3016 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003018 pos_T tpos = curwin->w_cursor;
3019
3020 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003021 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003022 for (;;)
3023 {
3024 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3025 --curwin->w_cursor.col;
3026 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003027 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003028 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003029 if (del_char(TRUE) == FAIL)
3030 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003031 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003032 if (curwin->w_cursor.lnum != tpos.lnum)
3033 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003034 else
3035 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003036 /* reset tpos, could have been invalidated in the loop above */
3037 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003038 tpos.col++;
3039 if (cc != NUL && gchar_pos(&tpos) == NUL)
3040 ++curwin->w_cursor.col; /* put cursor back on the NUL */
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 /* <C-S-Right> may have started Visual mode, adjust the position for
3044 * deleted characters. */
3045 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3046 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003047 int len = (int)STRLEN(ml_get_curline());
3048
3049 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003051 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
3056 }
3057 did_ai = FALSE;
3058#ifdef FEAT_SMARTINDENT
3059 did_si = FALSE;
3060 can_si = FALSE;
3061 can_si_back = FALSE;
3062#endif
3063
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003064 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3065 * now in a different buffer. */
3066 if (end_insert_pos != NULL)
3067 {
3068 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003069 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003070 curbuf->b_op_end = *end_insert_pos;
3071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072}
3073
3074/*
3075 * Set the last inserted text to a single character.
3076 * Used for the replace command.
3077 */
3078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 char_u *s;
3082
3083 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (last_insert != NULL)
3086 {
3087 s = last_insert;
3088 /* Use the CTRL-V only when entering a special char */
3089 if (c < ' ' || c == DEL)
3090 *s++ = Ctrl_V;
3091 s = add_char2buf(c, s);
3092 *s++ = ESC;
3093 *s++ = NUL;
3094 last_insert_skip = 0;
3095 }
3096}
3097
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003098#if defined(EXITFREE) || defined(PROTO)
3099 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003100free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003101{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003102 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003103}
3104#endif
3105
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106/*
3107 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3108 * and CSI. Handle multi-byte characters.
3109 * Returns a pointer to after the added bytes.
3110 */
3111 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003112add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003114 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 int i;
3116 int len;
3117
3118 len = (*mb_char2bytes)(c, temp);
3119 for (i = 0; i < len; ++i)
3120 {
3121 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
3123 if (c == K_SPECIAL)
3124 {
3125 *s++ = K_SPECIAL;
3126 *s++ = KS_SPECIAL;
3127 *s++ = KE_FILLER;
3128 }
3129#ifdef FEAT_GUI
3130 else if (c == CSI)
3131 {
3132 *s++ = CSI;
3133 *s++ = KS_EXTRA;
3134 *s++ = (int)KE_CSI;
3135 }
3136#endif
3137 else
3138 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return s;
3141}
3142
3143/*
3144 * move cursor to start of line
3145 * if flags & BL_WHITE move to first non-white
3146 * if flags & BL_SOL move to first non-white if startofline is set,
3147 * otherwise keep "curswant" column
3148 * if flags & BL_FIX don't leave the cursor on a NUL.
3149 */
3150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003151beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 if ((flags & BL_SOL) && !p_sol)
3154 coladvance(curwin->w_curswant);
3155 else
3156 {
3157 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
3160 if (flags & (BL_WHITE | BL_SOL))
3161 {
3162 char_u *ptr;
3163
Bram Moolenaar1c465442017-03-12 20:10:05 +01003164 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3166 ++curwin->w_cursor.col;
3167 }
3168 curwin->w_set_curswant = TRUE;
3169 }
3170}
3171
3172/*
3173 * oneright oneleft cursor_down cursor_up
3174 *
3175 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003176 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 * Return OK when successful, FAIL when we hit a line of file boundary.
3178 */
3179
3180 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003181oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (virtual_active())
3187 {
3188 pos_T prevpos = curwin->w_cursor;
3189
3190 /* Adjust for multi-wide char (excluding TAB) */
3191 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003192 coladvance(getviscol() + ((*ptr != TAB
3193 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 ? ptr2cells(ptr) : 1));
3195 curwin->w_set_curswant = TRUE;
3196 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
3197 return (prevpos.col != curwin->w_cursor.col
3198 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
3201 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003202 if (*ptr == NUL)
3203 return FAIL; /* already at the very end */
3204
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003205 if (has_mbyte)
3206 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003208 l = 1;
3209
3210 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3211 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003212 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003213 return FAIL;
3214 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
3216 curwin->w_set_curswant = TRUE;
3217 return OK;
3218}
3219
3220 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (virtual_active())
3224 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003225#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 int v = getviscol();
3229
3230 if (v == 0)
3231 return FAIL;
3232
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003233#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /* We might get stuck on 'showbreak', skip over it. */
3235 width = 1;
3236 for (;;)
3237 {
3238 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02003239 /* getviscol() is slow, skip it when 'showbreak' is empty,
3240 * 'breakindent' is not set and there are no multi-byte
3241 * characters */
3242 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003243 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 break;
3245 ++width;
3246 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003247#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
3251 if (curwin->w_cursor.coladd == 1)
3252 {
3253 char_u *ptr;
3254
3255 /* Adjust for multi-wide char (not a TAB) */
3256 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003257 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3258 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 curwin->w_cursor.coladd = 0;
3260 }
3261
3262 curwin->w_set_curswant = TRUE;
3263 return OK;
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 if (curwin->w_cursor.col == 0)
3267 return FAIL;
3268
3269 curwin->w_set_curswant = TRUE;
3270 --curwin->w_cursor.col;
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 /* if the character on the left of the current cursor is a multi-byte
3273 * character, move to its first byte */
3274 if (has_mbyte)
3275 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 return OK;
3277}
3278
3279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003280cursor_up(
3281 long n,
3282 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 linenr_T lnum;
3285
3286 if (n > 0)
3287 {
3288 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003289 /* This fails if the cursor is already in the first line or the count
3290 * is larger than the line number and '-' is in 'cpoptions' */
3291 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return FAIL;
3293 if (n >= lnum)
3294 lnum = 1;
3295 else
3296#ifdef FEAT_FOLDING
3297 if (hasAnyFolding(curwin))
3298 {
3299 /*
3300 * Count each sequence of folded lines as one logical line.
3301 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003302 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 (void)hasFolding(lnum, &lnum, NULL);
3304
3305 while (n--)
3306 {
3307 /* move up one line */
3308 --lnum;
3309 if (lnum <= 1)
3310 break;
3311 /* If we entered a fold, move to the beginning, unless in
3312 * Insert mode or when 'foldopen' contains "all": it will open
3313 * in a moment. */
3314 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3315 (void)hasFolding(lnum, &lnum, NULL);
3316 }
3317 if (lnum < 1)
3318 lnum = 1;
3319 }
3320 else
3321#endif
3322 lnum -= n;
3323 curwin->w_cursor.lnum = lnum;
3324 }
3325
3326 /* try to advance to the column we want to be at */
3327 coladvance(curwin->w_curswant);
3328
3329 if (upd_topline)
3330 update_topline(); /* make sure curwin->w_topline is valid */
3331
3332 return OK;
3333}
3334
3335/*
3336 * Cursor down a number of logical lines.
3337 */
3338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339cursor_down(
3340 long n,
3341 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 linenr_T lnum;
3344
3345 if (n > 0)
3346 {
3347 lnum = curwin->w_cursor.lnum;
3348#ifdef FEAT_FOLDING
3349 /* Move to last line of fold, will fail if it's the end-of-file. */
3350 (void)hasFolding(lnum, NULL, &lnum);
3351#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003352 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003353 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003354 if (lnum >= curbuf->b_ml.ml_line_count
3355 || (lnum + n > curbuf->b_ml.ml_line_count
3356 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 return FAIL;
3358 if (lnum + n >= curbuf->b_ml.ml_line_count)
3359 lnum = curbuf->b_ml.ml_line_count;
3360 else
3361#ifdef FEAT_FOLDING
3362 if (hasAnyFolding(curwin))
3363 {
3364 linenr_T last;
3365
3366 /* count each sequence of folded lines as one logical line */
3367 while (n--)
3368 {
3369 if (hasFolding(lnum, NULL, &last))
3370 lnum = last + 1;
3371 else
3372 ++lnum;
3373 if (lnum >= curbuf->b_ml.ml_line_count)
3374 break;
3375 }
3376 if (lnum > curbuf->b_ml.ml_line_count)
3377 lnum = curbuf->b_ml.ml_line_count;
3378 }
3379 else
3380#endif
3381 lnum += n;
3382 curwin->w_cursor.lnum = lnum;
3383 }
3384
3385 /* try to advance to the column we want to be at */
3386 coladvance(curwin->w_curswant);
3387
3388 if (upd_topline)
3389 update_topline(); /* make sure curwin->w_topline is valid */
3390
3391 return OK;
3392}
3393
3394/*
3395 * Stuff the last inserted text in the read buffer.
3396 * Last_insert actually is a copy of the redo buffer, so we
3397 * first have to remove the command.
3398 */
3399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003400stuff_inserted(
3401 int c, /* Command character to be inserted */
3402 long count, /* Repeat this many times */
3403 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404{
3405 char_u *esc_ptr;
3406 char_u *ptr;
3407 char_u *last_ptr;
3408 char_u last = NUL;
3409
3410 ptr = get_last_insert();
3411 if (ptr == NULL)
3412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003413 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 return FAIL;
3415 }
3416
3417 /* may want to stuff the command character, to start Insert mode */
3418 if (c != NUL)
3419 stuffcharReadbuff(c);
3420 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
3421 *esc_ptr = NUL; /* remove the ESC */
3422
3423 /* when the last char is either "0" or "^" it will be quoted if no ESC
3424 * comes after it OR if it will inserted more than once and "ptr"
3425 * starts with ^D. -- Acevedo
3426 */
3427 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3428 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3429 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3430 {
3431 last = *last_ptr;
3432 *last_ptr = NUL;
3433 }
3434
3435 do
3436 {
3437 stuffReadbuff(ptr);
3438 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
3439 if (last)
3440 stuffReadbuff((char_u *)(last == '0'
3441 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3442 : IF_EB("\026^", CTRL_V_STR "^")));
3443 }
3444 while (--count > 0);
3445
3446 if (last)
3447 *last_ptr = last;
3448
3449 if (esc_ptr != NULL)
3450 *esc_ptr = ESC; /* put the ESC back */
3451
3452 /* may want to stuff a trailing ESC, to get out of Insert mode */
3453 if (!no_esc)
3454 stuffcharReadbuff(ESC);
3455
3456 return OK;
3457}
3458
3459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003460get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 if (last_insert == NULL)
3463 return NULL;
3464 return last_insert + last_insert_skip;
3465}
3466
3467/*
3468 * Get last inserted string, and remove trailing <Esc>.
3469 * Returns pointer to allocated memory (must be freed) or NULL.
3470 */
3471 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003472get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 char_u *s;
3475 int len;
3476
3477 if (last_insert == NULL)
3478 return NULL;
3479 s = vim_strsave(last_insert + last_insert_skip);
3480 if (s != NULL)
3481 {
3482 len = (int)STRLEN(s);
3483 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
3484 s[len - 1] = NUL;
3485 }
3486 return s;
3487}
3488
3489/*
3490 * Check the word in front of the cursor for an abbreviation.
3491 * Called when the non-id character "c" has been entered.
3492 * When an abbreviation is recognized it is removed from the text and
3493 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3494 */
3495 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003496echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 /* Don't check for abbreviation in paste mode, when disabled and just
3499 * after moving around with cursor keys. */
3500 if (p_paste || no_abbr || arrow_used)
3501 return FALSE;
3502
3503 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3504 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3505}
3506
3507/*
3508 * replace-stack functions
3509 *
3510 * When replacing characters, the replaced characters are remembered for each
3511 * new character. This is used to re-insert the old text when backspacing.
3512 *
3513 * There is a NUL headed list of characters for each character that is
3514 * currently in the file after the insertion point. When BS is used, one NUL
3515 * headed list is put back for the deleted character.
3516 *
3517 * For a newline, there are two NUL headed lists. One contains the characters
3518 * that the NL replaced. The extra one stores the characters after the cursor
3519 * that were deleted (always white space).
3520 *
3521 * Replace_offset is normally 0, in which case replace_push will add a new
3522 * character at the end of the stack. If replace_offset is not 0, that many
3523 * characters will be left on the stack above the newly inserted character.
3524 */
3525
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003526static char_u *replace_stack = NULL;
3527static long replace_stack_nr = 0; /* next entry in replace stack */
3528static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003531replace_push(
3532 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533{
3534 char_u *p;
3535
3536 if (replace_stack_nr < replace_offset) /* nothing to do */
3537 return;
3538 if (replace_stack_len <= replace_stack_nr)
3539 {
3540 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003541 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (p == NULL) /* out of memory */
3543 {
3544 replace_stack_len -= 50;
3545 return;
3546 }
3547 if (replace_stack != NULL)
3548 {
3549 mch_memmove(p, replace_stack,
3550 (size_t)(replace_stack_nr * sizeof(char_u)));
3551 vim_free(replace_stack);
3552 }
3553 replace_stack = p;
3554 }
3555 p = replace_stack + replace_stack_nr - replace_offset;
3556 if (replace_offset)
3557 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3558 *p = c;
3559 ++replace_stack_nr;
3560}
3561
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003562/*
3563 * Push a character onto the replace stack. Handles a multi-byte character in
3564 * reverse byte order, so that the first byte is popped off first.
3565 * Return the number of bytes done (includes composing characters).
3566 */
3567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003569{
3570 int l = (*mb_ptr2len)(p);
3571 int j;
3572
3573 for (j = l - 1; j >= 0; --j)
3574 replace_push(p[j]);
3575 return l;
3576}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003577
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578/*
3579 * Pop one item from the replace stack.
3580 * return -1 if stack empty
3581 * return replaced character or NUL otherwise
3582 */
3583 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
3586 if (replace_stack_nr == 0)
3587 return -1;
3588 return (int)replace_stack[--replace_stack_nr];
3589}
3590
3591/*
3592 * Join the top two items on the replace stack. This removes to "off"'th NUL
3593 * encountered.
3594 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003596replace_join(
3597 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598{
3599 int i;
3600
3601 for (i = replace_stack_nr; --i >= 0; )
3602 if (replace_stack[i] == NUL && off-- <= 0)
3603 {
3604 --replace_stack_nr;
3605 mch_memmove(replace_stack + i, replace_stack + i + 1,
3606 (size_t)(replace_stack_nr - i));
3607 return;
3608 }
3609}
3610
3611/*
3612 * Pop bytes from the replace stack until a NUL is found, and insert them
3613 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3614 */
3615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 int cc;
3619 int oldState = State;
3620
3621 State = NORMAL; /* don't want REPLACE here */
3622 while ((cc = replace_pop()) > 0)
3623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 dec_cursor();
3626 }
3627 State = oldState;
3628}
3629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630/*
3631 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3632 * indicates a multi-byte char, pop the other bytes too.
3633 */
3634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
3637 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003638 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 int i;
3640 int c;
3641
3642 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3643 {
3644 buf[0] = cc;
3645 for (i = 1; i < n; ++i)
3646 buf[i] = replace_pop();
3647 ins_bytes_len(buf, n);
3648 }
3649 else
3650 ins_char(cc);
3651
3652 if (enc_utf8)
3653 /* Handle composing chars. */
3654 for (;;)
3655 {
3656 c = replace_pop();
3657 if (c == -1) /* stack empty */
3658 break;
3659 if ((n = MB_BYTE2LEN(c)) == 1)
3660 {
3661 /* Not a multi-byte char, put it back. */
3662 replace_push(c);
3663 break;
3664 }
3665 else
3666 {
3667 buf[0] = c;
3668 for (i = 1; i < n; ++i)
3669 buf[i] = replace_pop();
3670 if (utf_iscomposing(utf_ptr2char(buf)))
3671 ins_bytes_len(buf, n);
3672 else
3673 {
3674 /* Not a composing char, put it back. */
3675 for (i = n - 1; i >= 0; --i)
3676 replace_push(buf[i]);
3677 break;
3678 }
3679 }
3680 }
3681}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
3683/*
3684 * make the replace stack empty
3685 * (called when exiting replace mode)
3686 */
3687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003688replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003690 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 replace_stack_len = 0;
3692 replace_stack_nr = 0;
3693}
3694
3695/*
3696 * Handle doing a BS for one character.
3697 * cc < 0: replace stack empty, just move cursor
3698 * cc == 0: character was inserted, delete it
3699 * cc > 0: character was replaced, put cc (first byte of original char) back
3700 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003701 * When "limit_col" is >= 0, don't delete before this column. Matters when
3702 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 */
3704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 int orig_len = 0;
3709 int ins_len;
3710 int orig_vcols = 0;
3711 colnr_T start_vcol;
3712 char_u *p;
3713 int i;
3714 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
3716 cc = replace_pop();
3717 if (cc > 0)
3718 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01003719#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003720 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003721
3722 if (curbuf->b_has_textprop)
3723 {
3724 // Do not adjust text properties for individual delete and insert
3725 // operations, do it afterwards on the resulting text.
3726 len_before = STRLEN(ml_get_curline());
3727 ++text_prop_frozen;
3728 }
3729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (State & VREPLACE_FLAG)
3731 {
3732 /* Get the number of screen cells used by the character we are
3733 * going to delete. */
3734 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3735 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (has_mbyte)
3738 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003739 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003741 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 replace_push(cc);
3743 }
3744 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
3746 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003748 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750 replace_pop_ins();
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 if (State & VREPLACE_FLAG)
3753 {
3754 /* Get the number of screen cells used by the inserted characters */
3755 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003756 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 vcol = start_vcol;
3758 for (i = 0; i < ins_len; ++i)
3759 {
3760 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003761 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763 vcol -= start_vcol;
3764
3765 /* Delete spaces that were inserted after the cursor to keep the
3766 * text aligned. */
3767 curwin->w_cursor.col += ins_len;
3768 while (vcol > orig_vcols && gchar_cursor() == ' ')
3769 {
3770 del_char(FALSE);
3771 ++orig_vcols;
3772 }
3773 curwin->w_cursor.col -= ins_len;
3774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
Bram Moolenaar196d1572019-01-02 23:47:18 +01003776 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003778
3779#ifdef FEAT_TEXT_PROP
3780 if (curbuf->b_has_textprop)
3781 {
3782 size_t len_now = STRLEN(ml_get_curline());
3783
3784 --text_prop_frozen;
3785 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003786 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003787 }
3788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003791 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792}
3793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3795/*
3796 * Map Hebrew keyboard when in hkmap mode.
3797 */
3798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003799hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
3801 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
3802 {
3803 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3804 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3805 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3806 static char_u map[26] =
3807 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3808 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3809 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3810 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3811 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3812 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3813 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3814 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3815 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3816
3817 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3818 return (int)(map[CharOrd(c)] - 1 + p_aleph);
3819 /* '-1'='sofit' */
3820 else if (c == 'x')
3821 return 'X';
3822 else if (c == 'q')
3823 return '\''; /* {geresh}={'} */
3824 else if (c == 246)
3825 return ' '; /* \"o --> ' ' for a german keyboard */
3826 else if (c == 228)
3827 return ' '; /* \"a --> ' ' -- / -- */
3828 else if (c == 252)
3829 return ' '; /* \"u --> ' ' -- / -- */
3830#ifdef EBCDIC
3831 else if (islower(c))
3832#else
3833 /* NOTE: islower() does not do the right thing for us on Linux so we
3834 * do this the same was as 5.7 and previous, so it works correctly on
3835 * all systems. Specifically, the e.g. Delete and Arrow keys are
3836 * munged and won't work if e.g. searching for Hebrew text.
3837 */
3838 else if (c >= 'a' && c <= 'z')
3839#endif
3840 return (int)(map[CharOrdLow(c)] + p_aleph);
3841 else
3842 return c;
3843 }
3844 else
3845 {
3846 switch (c)
3847 {
3848 case '`': return ';';
3849 case '/': return '.';
3850 case '\'': return ',';
3851 case 'q': return '/';
3852 case 'w': return '\'';
3853
3854 /* Hebrew letters - set offset from 'a' */
3855 case ',': c = '{'; break;
3856 case '.': c = 'v'; break;
3857 case ';': c = 't'; break;
3858 default: {
3859 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3860
3861#ifdef EBCDIC
3862 /* see note about islower() above */
3863 if (!islower(c))
3864#else
3865 if (c < 'a' || c > 'z')
3866#endif
3867 return c;
3868 c = str[CharOrdLow(c)];
3869 break;
3870 }
3871 }
3872
3873 return (int)(CharOrdLow(c) + p_aleph);
3874 }
3875}
3876#endif
3877
3878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003879ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
3881 int need_redraw = FALSE;
3882 int regname;
3883 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003884 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885
3886 /*
3887 * If we are going to wait for a character, show a '"'.
3888 */
3889 pc_status = PC_STATUS_UNSET;
3890 if (redrawing() && !char_avail())
3891 {
3892 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00003893 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
3895 edit_putchar('"', TRUE);
3896#ifdef FEAT_CMDL_INFO
3897 add_to_showcmd_c(Ctrl_R);
3898#endif
3899 }
3900
3901#ifdef USE_ON_FLY_SCROLL
3902 dont_scroll = TRUE; /* disallow scrolling here */
3903#endif
3904
3905 /*
3906 * Don't map the register name. This also prevents the mode message to be
3907 * deleted when ESC is hit.
3908 */
3909 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003910 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3913 {
3914 /* Get a third key for literal register insertion */
3915 literally = regname;
3916#ifdef FEAT_CMDL_INFO
3917 add_to_showcmd_c(literally);
3918#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003919 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 --no_mapping;
3923
3924#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02003925 /* Don't call u_sync() while typing the expression or giving an error
3926 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 ++no_u_sync;
3928 if (regname == '=')
3929 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003930 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003931# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003933# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003934 /* Sync undo when evaluating the expression calls setline() or
3935 * append(), so that it can be undone separately. */
3936 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003939
3940 // Cursor may be moved back a column.
3941 curwin->w_cursor = curpos;
3942 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003943# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003944 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (im_on)
3946 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003947# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00003949 if (regname == NUL || !valid_yank_reg(regname, FALSE))
3950 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003951 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00003953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 else
3955 {
3956#endif
3957 if (literally == Ctrl_O || literally == Ctrl_P)
3958 {
3959 /* Append the command to the redo buffer. */
3960 AppendCharToRedobuff(Ctrl_R);
3961 AppendCharToRedobuff(literally);
3962 AppendCharToRedobuff(regname);
3963
3964 do_put(regname, BACKWARD, 1L,
3965 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
3966 }
3967 else if (insert_reg(regname, literally) == FAIL)
3968 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003969 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 need_redraw = TRUE; /* remove the '"' */
3971 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003972 else if (stop_insert_mode)
3973 /* When the '=' register was used and a function was invoked that
3974 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
3975 * insert anything, need to remove the '"' */
3976 need_redraw = TRUE;
3977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978#ifdef FEAT_EVAL
3979 }
3980 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003981 if (u_sync_once == 1)
3982 ins_need_undo = TRUE;
3983 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#endif
3985#ifdef FEAT_CMDL_INFO
3986 clear_showcmd();
3987#endif
3988
3989 /* If the inserted register is empty, we need to remove the '"' */
3990 if (need_redraw || stuff_empty())
3991 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003992
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003993 /* Disallow starting Visual mode here, would get a weird mode. */
3994 if (!vis_active && VIsual_active)
3995 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997
3998/*
3999 * CTRL-G commands in Insert mode.
4000 */
4001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004002ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
4004 int c;
4005
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004006 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 /*
4010 * Don't map the second key. This also prevents the mode message to be
4011 * deleted when ESC is hit.
4012 */
4013 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004014 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 --no_mapping;
4016 switch (c)
4017 {
4018 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
4019 case K_UP:
4020 case Ctrl_K:
4021 case 'k': ins_up(TRUE);
4022 break;
4023
4024 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
4025 case K_DOWN:
4026 case Ctrl_J:
4027 case 'j': ins_down(TRUE);
4028 break;
4029
4030 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004031 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004033
4034 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004035 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004036 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004037 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 break;
4039
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004040 /* CTRL-G U: do not break undo with the next char */
4041 case 'U':
4042 /* Allow one left/right cursor movement with the next char,
4043 * without breaking undo. */
4044 dont_sync_undo = MAYBE;
4045 break;
4046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02004048 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050}
4051
4052/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004053 * CTRL-^ in Insert mode.
4054 */
4055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004056ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004057{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004058 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004059 {
4060 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
4061 if (State & LANGMAP)
4062 {
4063 curbuf->b_p_iminsert = B_IMODE_NONE;
4064 State &= ~LANGMAP;
4065 }
4066 else
4067 {
4068 curbuf->b_p_iminsert = B_IMODE_LMAP;
4069 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004070#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004071 im_set_active(FALSE);
4072#endif
4073 }
4074 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004075#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004076 else
4077 {
4078 /* There are no ":lmap" mappings, toggle IM */
4079 if (im_get_status())
4080 {
4081 curbuf->b_p_iminsert = B_IMODE_NONE;
4082 im_set_active(FALSE);
4083 }
4084 else
4085 {
4086 curbuf->b_p_iminsert = B_IMODE_IM;
4087 State &= ~LANGMAP;
4088 im_set_active(TRUE);
4089 }
4090 }
4091#endif
4092 set_iminsert_global();
4093 showmode();
4094#ifdef FEAT_GUI
4095 /* may show different cursor shape or color */
4096 if (gui.in_use)
4097 gui_update_cursor(TRUE, FALSE);
4098#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004099#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004100 /* Show/unshow value of 'keymap' in status lines. */
4101 status_redraw_curbuf();
4102#endif
4103}
4104
4105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 * Handle ESC in insert mode.
4107 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4108 * insert.
4109 */
4110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004111ins_esc(
4112 long *count,
4113 int cmdchar,
4114 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 int temp;
4117 static int disabled_redraw = FALSE;
4118
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004119#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120 check_spell_redraw();
4121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122#if defined(FEAT_HANGULIN)
4123# if defined(ESC_CHG_TO_ENG_MODE)
4124 hangul_input_state_set(0);
4125# endif
4126 if (composing_hangul)
4127 {
4128 push_raw_key(composing_hangul_buffer, 2);
4129 composing_hangul = 0;
4130 }
4131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 temp = curwin->w_cursor.col;
4134 if (disabled_redraw)
4135 {
4136 --RedrawingDisabled;
4137 disabled_redraw = FALSE;
4138 }
4139 if (!arrow_used)
4140 {
4141 /*
4142 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004143 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4144 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 */
4146 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004147 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
4150 * Repeating insert may take a long time. Check for
4151 * interrupt now and then.
4152 */
4153 if (*count > 0)
4154 {
4155 line_breakcheck();
4156 if (got_int)
4157 *count = 0;
4158 }
4159
4160 if (--*count > 0) /* repeat what was typed */
4161 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004162 /* Vi repeats the insert without replacing characters. */
4163 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4164 State &= ~REPLACE_FLAG;
4165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 (void)start_redo_ins();
4167 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02004168 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 ++RedrawingDisabled;
4170 disabled_redraw = TRUE;
4171 return FALSE; /* repeat the insert */
4172 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004173 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 undisplay_dollar();
4175 }
4176
4177 /* When an autoindent was removed, curswant stays after the
4178 * indent */
4179 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4180 curwin->w_set_curswant = TRUE;
4181
4182 /* Remember the last Insert position in the '^ mark. */
4183 if (!cmdmod.keepjumps)
4184 curbuf->b_last_insert = curwin->w_cursor;
4185
4186 /*
4187 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004188 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004190 if (!nomove
4191 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004192 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004193 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004194 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195#ifdef FEAT_RIGHTLEFT
4196 && !revins_on
4197#endif
4198 )
4199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4201 {
4202 oneleft();
4203 if (restart_edit != NUL)
4204 ++curwin->w_cursor.coladd;
4205 }
4206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
4208 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 /* Correct cursor for multi-byte character. */
4210 if (has_mbyte)
4211 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213 }
4214
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004215#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 /* Disable IM to allow typing English directly for Normal mode commands.
4217 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4218 * well). */
4219 if (!(State & LANGMAP))
4220 im_save_status(&curbuf->b_p_iminsert);
4221 im_set_active(FALSE);
4222#endif
4223
4224 State = NORMAL;
4225 /* need to position cursor again (e.g. when on a TAB ) */
4226 changed_cline_bef_curs();
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229#ifdef CURSOR_SHAPE
4230 ui_cursor_shape(); /* may show different cursor shape */
4231#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004232 if (!p_ek)
4233 /* Re-enable bracketed paste mode. */
4234 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004236 // When recording or for CTRL-O, need to display the new mode.
4237 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004238 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004240 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004241 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 return TRUE; /* exit Insert mode */
4244}
4245
4246#ifdef FEAT_RIGHTLEFT
4247/*
4248 * Toggle language: hkmap and revins_on.
4249 * Move to end of reverse inserted text.
4250 */
4251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004252ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 if (revins_on && revins_chars && revins_scol >= 0)
4255 {
4256 while (gchar_cursor() != NUL && revins_chars--)
4257 ++curwin->w_cursor.col;
4258 }
4259 p_ri = !p_ri;
4260 revins_on = (State == INSERT && p_ri);
4261 if (revins_on)
4262 {
4263 revins_scol = curwin->w_cursor.col;
4264 revins_legal++;
4265 revins_chars = 0;
4266 undisplay_dollar();
4267 }
4268 else
4269 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004270 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 showmode();
4272}
4273#endif
4274
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275/*
4276 * If 'keymodel' contains "startsel", may start selection.
4277 * Returns TRUE when a CTRL-O and other keys stuffed.
4278 */
4279 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004280ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281{
4282 if (km_startsel)
4283 switch (c)
4284 {
4285 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 case K_PAGEUP:
4288 case K_KPAGEUP:
4289 case K_PAGEDOWN:
4290 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004291# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 case K_LEFT:
4293 case K_RIGHT:
4294 case K_UP:
4295 case K_DOWN:
4296 case K_END:
4297 case K_HOME:
4298# endif
4299 if (!(mod_mask & MOD_MASK_SHIFT))
4300 break;
4301 /* FALLTHROUGH */
4302 case K_S_LEFT:
4303 case K_S_RIGHT:
4304 case K_S_UP:
4305 case K_S_DOWN:
4306 case K_S_END:
4307 case K_S_HOME:
4308 /* Start selection right away, the cursor can move with
4309 * CTRL-O when beyond the end of the line. */
4310 start_selection();
4311
4312 /* Execute the key in (insert) Select mode. */
4313 stuffcharReadbuff(Ctrl_O);
4314 if (mod_mask)
4315 {
4316 char_u buf[4];
4317
4318 buf[0] = K_SPECIAL;
4319 buf[1] = KS_MODIFIER;
4320 buf[2] = mod_mask;
4321 buf[3] = NUL;
4322 stuffReadbuff(buf);
4323 }
4324 stuffcharReadbuff(c);
4325 return TRUE;
4326 }
4327 return FALSE;
4328}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329
4330/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004331 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332 */
4333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004334ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004335{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004336#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004338 (char_u *)((State & REPLACE_FLAG) ? "i"
4339 : replaceState == VREPLACE ? "v"
4340 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004341#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004342 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004343 if (State & REPLACE_FLAG)
4344 State = INSERT | (State & LANGMAP);
4345 else
4346 State = replaceState | (State & LANGMAP);
4347 AppendCharToRedobuff(K_INS);
4348 showmode();
4349#ifdef CURSOR_SHAPE
4350 ui_cursor_shape(); /* may show different cursor shape */
4351#endif
4352}
4353
4354/*
4355 * Pressed CTRL-O in Insert mode.
4356 */
4357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004358ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004359{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004360 if (State & VREPLACE_FLAG)
4361 restart_edit = 'V';
4362 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363 if (State & REPLACE_FLAG)
4364 restart_edit = 'R';
4365 else
4366 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004367 if (virtual_active())
4368 ins_at_eol = FALSE; /* cursor always keeps its column */
4369 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004370 ins_at_eol = (gchar_cursor() == NUL);
4371}
4372
4373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 * If the cursor is on an indent, ^T/^D insert/delete one
4375 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004376 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 * with vi. But vi only supports ^T and ^D after an
4378 * autoindent, we support it everywhere.
4379 */
4380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004381ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
4383 if (stop_arrow() == FAIL)
4384 return;
4385 AppendCharToRedobuff(c);
4386
4387 /*
4388 * 0^D and ^^D: remove all indent.
4389 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004390 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4391 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
4393 --curwin->w_cursor.col;
4394 (void)del_char(FALSE); /* delete the '^' or '0' */
4395 /* In Replace mode, restore the characters that '^' or '0' replaced. */
4396 if (State & REPLACE_FLAG)
4397 replace_pop_ins();
4398 if (lastc == '^')
4399 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004400 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004403 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4406 did_ai = FALSE;
4407#ifdef FEAT_SMARTINDENT
4408 did_si = FALSE;
4409 can_si = FALSE;
4410 can_si_back = FALSE;
4411#endif
4412#ifdef FEAT_CINDENT
4413 can_cindent = FALSE; /* no cindenting after ^D or ^T */
4414#endif
4415}
4416
4417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004418ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419{
4420 int temp;
4421
4422 if (stop_arrow() == FAIL)
4423 return;
4424 if (gchar_cursor() == NUL) /* delete newline */
4425 {
4426 temp = curwin->w_cursor.col;
4427 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004428 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004429 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004433 /* Adjust orig_line_count in case more lines have been deleted than
4434 * have been added. That makes sure, that open_line() later
4435 * can access all buffer lines correctly */
4436 if (State & VREPLACE_FLAG &&
4437 orig_line_count > curbuf->b_ml.ml_line_count)
4438 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02004441 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
4442 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 did_ai = FALSE;
4444#ifdef FEAT_SMARTINDENT
4445 did_si = FALSE;
4446 can_si = FALSE;
4447 can_si_back = FALSE;
4448#endif
4449 AppendCharToRedobuff(K_DEL);
4450}
4451
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004452/*
4453 * Delete one character for ins_bs().
4454 */
4455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004456ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004457{
4458 dec_cursor();
4459 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4460 if (State & REPLACE_FLAG)
4461 {
4462 /* Don't delete characters before the insert point when in
4463 * Replace mode */
4464 if (curwin->w_cursor.lnum != Insstart.lnum
4465 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004466 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004467 }
4468 else
4469 (void)del_char(FALSE);
4470}
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472/*
4473 * Handle Backspace, delete-word and delete-line in Insert mode.
4474 * Return TRUE when backspace was actually used.
4475 */
4476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004477ins_bs(
4478 int c,
4479 int mode,
4480 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481{
4482 linenr_T lnum;
4483 int cc;
4484 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004485 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 colnr_T mincol;
4487 int did_backspace = FALSE;
4488 int in_indent;
4489 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004490 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 /*
4493 * can't delete anything in an empty file
4494 * can't backup past first character in buffer
4495 * can't backup past starting point unless 'backspace' > 1
4496 * can backup to a previous line if 'backspace' == 0
4497 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004498 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 || (
4500#ifdef FEAT_RIGHTLEFT
4501 !revins_on &&
4502#endif
4503 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4504 || (!can_bs(BS_START)
4505 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004506 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4507 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4509 && curwin->w_cursor.col <= ai_col)
4510 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4511 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004512 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 return FALSE;
4514 }
4515
4516 if (stop_arrow() == FAIL)
4517 return FALSE;
4518 in_indent = inindent(0);
4519#ifdef FEAT_CINDENT
4520 if (in_indent)
4521 can_cindent = FALSE;
4522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 end_comment_pending = NUL; /* After BS, don't auto-end comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524#ifdef FEAT_RIGHTLEFT
4525 if (revins_on) /* put cursor after last inserted char */
4526 inc_cursor();
4527#endif
4528
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 /* Virtualedit:
4530 * BACKSPACE_CHAR eats a virtual space
4531 * BACKSPACE_WORD eats all coladd
4532 * BACKSPACE_LINE eats all coladd and keeps going
4533 */
4534 if (curwin->w_cursor.coladd > 0)
4535 {
4536 if (mode == BACKSPACE_CHAR)
4537 {
4538 --curwin->w_cursor.coladd;
4539 return TRUE;
4540 }
4541 if (mode == BACKSPACE_WORD)
4542 {
4543 curwin->w_cursor.coladd = 0;
4544 return TRUE;
4545 }
4546 curwin->w_cursor.coladd = 0;
4547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004550 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 */
4552 if (curwin->w_cursor.col == 0)
4553 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004554 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004555 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556#ifdef FEAT_RIGHTLEFT
4557 || revins_on
4558#endif
4559 )
4560 {
4561 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4562 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4563 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004564 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004565 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 /*
4568 * In replace mode:
4569 * cc < 0: NL was inserted, delete it
4570 * cc >= 0: NL was replaced, put original characters back
4571 */
4572 cc = -1;
4573 if (State & REPLACE_FLAG)
4574 cc = replace_pop(); /* returns -1 if NL was inserted */
4575 /*
4576 * In replace mode, in the line we started replacing, we only move the
4577 * cursor.
4578 */
4579 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4580 {
4581 dec_cursor();
4582 }
4583 else
4584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (!(State & VREPLACE_FLAG)
4586 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
4588 temp = gchar_cursor(); /* remember current char */
4589 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004590
4591 /* When "aw" is in 'formatoptions' we must delete the space at
4592 * the end of the line, otherwise the line will be broken
4593 * again when auto-formatting. */
4594 if (has_format_option(FO_AUTO)
4595 && has_format_option(FO_WHITE_PAR))
4596 {
4597 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4598 TRUE);
4599 int len;
4600
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004601 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004602 if (len > 0 && ptr[len - 1] == ' ')
4603 ptr[len - 1] = NUL;
4604 }
4605
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004606 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (temp == NUL && gchar_cursor() != NUL)
4608 inc_cursor();
4609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 else
4611 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613 /*
4614 * In REPLACE mode we have to put back the text that was replaced
4615 * by the NL. On the replace stack is first a NUL-terminated
4616 * sequence of characters that were deleted and then the
4617 * characters that NL replaced.
4618 */
4619 if (State & REPLACE_FLAG)
4620 {
4621 /*
4622 * Do the next ins_char() in NORMAL state, to
4623 * prevent ins_char() from replacing characters and
4624 * avoiding showmatch().
4625 */
4626 oldState = State;
4627 State = NORMAL;
4628 /*
4629 * restore characters (blanks) deleted after cursor
4630 */
4631 while (cc > 0)
4632 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004633 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004635 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 cc = replace_pop();
4637 }
4638 /* restore the characters that NL replaced */
4639 replace_pop_ins();
4640 State = oldState;
4641 }
4642 }
4643 did_ai = FALSE;
4644 }
4645 else
4646 {
4647 /*
4648 * Delete character(s) before the cursor.
4649 */
4650#ifdef FEAT_RIGHTLEFT
4651 if (revins_on) /* put cursor on last inserted char */
4652 dec_cursor();
4653#endif
4654 mincol = 0;
4655 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004656 if (mode == BACKSPACE_LINE
4657 && (curbuf->b_p_ai
4658#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004659 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004660#endif
4661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662#ifdef FEAT_RIGHTLEFT
4663 && !revins_on
4664#endif
4665 )
4666 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004667 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004669 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004671 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673
4674 /*
4675 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4676 */
4677 if ( mode == BACKSPACE_CHAR
4678 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004679 || ((get_sts_value() != 0
4680#ifdef FEAT_VARTABS
4681 || tabstop_count(curbuf->b_p_vsts_array)
4682#endif
4683 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004684 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 && (*(ml_get_cursor() - 1) == TAB
4686 || (*(ml_get_cursor() - 1) == ' '
4687 && (!*inserted_space_p
4688 || arrow_used))))))
4689 {
4690 int ts;
4691 colnr_T vcol;
4692 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004693 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
4695 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 /* Compute the virtual column where we want to be. Since
4697 * 'showbreak' may get in the way, need to get the last column of
4698 * the previous character. */
4699 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004700 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 dec_cursor();
4702 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4703 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004704#ifdef FEAT_VARTABS
4705 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004706 {
4707 ts = (int)get_sw_value(curbuf);
4708 want_vcol = (want_vcol / ts) * ts;
4709 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004710 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004711 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004712 curbuf->b_p_vsts_array);
4713#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004714 if (p_sta && in_indent)
4715 ts = (int)get_sw_value(curbuf);
4716 else
4717 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720
4721 /* delete characters until we are at or before want_vcol */
4722 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004723 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004724 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
4726 /* insert extra spaces until we are at want_vcol */
4727 while (vcol < want_vcol)
4728 {
4729 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004730 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4731 && curwin->w_cursor.col < Insstart_orig.col)
4732 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (State & VREPLACE_FLAG)
4735 ins_char(' ');
4736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
4738 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004739 if ((State & REPLACE_FLAG))
4740 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
4742 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4743 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004744
4745 /* If we are now back where we started delete one character. Can
4746 * happen when using 'sts' and 'linebreak'. */
4747 if (vcol >= start_vcol)
4748 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
4750
4751 /*
4752 * Delete upto starting point, start of line or previous word.
4753 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004756 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004758 if (has_mbyte)
4759 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004760 do
4761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004763 if (!revins_on) /* put cursor on char to be deleted */
4764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004766
4767 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004768 /* look multi-byte character class */
4769 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004771 prev_cclass = cclass;
4772 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004774
4775 /* start of word? */
4776 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4777 {
4778 mode = BACKSPACE_WORD_NOT_SPACE;
4779 temp = vim_iswordc(cc);
4780 }
4781 /* end of word? */
4782 else if (mode == BACKSPACE_WORD_NOT_SPACE
4783 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004784 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004787 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004789 inc_cursor();
4790#ifdef FEAT_RIGHTLEFT
4791 else if (State & REPLACE_FLAG)
4792 dec_cursor();
4793#endif
4794 break;
4795 }
4796 if (State & REPLACE_FLAG)
4797 replace_do_bs(-1);
4798 else
4799 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004800 if (enc_utf8 && p_deco)
4801 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004802 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004803 /*
4804 * If there are combining characters and 'delcombine' is set
4805 * move the cursor back. Don't back up before the base
4806 * character.
4807 */
4808 if (enc_utf8 && p_deco && cpc[0] != NUL)
4809 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004810#ifdef FEAT_RIGHTLEFT
4811 if (revins_chars)
4812 {
4813 revins_chars--;
4814 revins_legal++;
4815 }
4816 if (revins_on && gchar_cursor() == NUL)
4817 break;
4818#endif
4819 }
4820 /* Just a single backspace?: */
4821 if (mode == BACKSPACE_CHAR)
4822 break;
4823 } while (
4824#ifdef FEAT_RIGHTLEFT
4825 revins_on ||
4826#endif
4827 (curwin->w_cursor.col > mincol
4828 && (curwin->w_cursor.lnum != Insstart_orig.lnum
4829 || curwin->w_cursor.col != Insstart_orig.col)));
4830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 did_backspace = TRUE;
4832 }
4833#ifdef FEAT_SMARTINDENT
4834 did_si = FALSE;
4835 can_si = FALSE;
4836 can_si_back = FALSE;
4837#endif
4838 if (curwin->w_cursor.col <= 1)
4839 did_ai = FALSE;
4840 /*
4841 * It's a little strange to put backspaces into the redo
4842 * buffer, but it makes auto-indent a lot easier to deal
4843 * with.
4844 */
4845 AppendCharToRedobuff(c);
4846
4847 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004848 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004849 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004850 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
4852 /* vi behaviour: the cursor moves backward but the character that
4853 * was there remains visible
4854 * Vim behaviour: the cursor moves backward and the character that
4855 * was there is erased from the screen.
4856 * We can emulate the vi behaviour by pretending there is a dollar
4857 * displayed even when there isn't.
4858 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004859 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 dollar_vcol = curwin->w_virtcol;
4861
Bram Moolenaarce3be472008-01-14 19:12:28 +00004862#ifdef FEAT_FOLDING
4863 /* When deleting a char the cursor line must never be in a closed fold.
4864 * E.g., when 'foldmethod' is indent and deleting the first non-white
4865 * char before a Tab. */
4866 if (did_backspace)
4867 foldOpenCursor();
4868#endif
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 return did_backspace;
4871}
4872
Bram Moolenaarec2da362017-01-21 20:04:22 +01004873/*
4874 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4875 * P_PE literally.
4876 * When "drop" is TRUE then consume the text and drop it.
4877 */
4878 int
4879bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4880{
4881 int c;
4882 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4883 int idx = 0;
4884 char_u *end = find_termcode((char_u *)"PE");
4885 int ret_char = -1;
4886 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004887 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004888
4889 /* If the end code is too long we can't detect it, read everything. */
4890 if (STRLEN(end) >= NUMBUFLEN)
4891 end = NULL;
4892 ++no_mapping;
4893 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004894 if (!p_paste)
4895 // Also have the side effects of setting 'paste' to make it work much
4896 // faster.
4897 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004898
Bram Moolenaarec2da362017-01-21 20:04:22 +01004899 for (;;)
4900 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004901 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004902 if (end == NULL && vpeekc() == NUL)
4903 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004904 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004905 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004906 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004907 if (c == NUL || got_int)
4908 // When CTRL-C was encountered the typeahead will be flushed and we
4909 // won't get the end sequence.
4910 break;
4911
Bram Moolenaarec2da362017-01-21 20:04:22 +01004912 if (has_mbyte)
4913 idx += (*mb_char2bytes)(c, buf + idx);
4914 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004915 buf[idx++] = c;
4916 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004917 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004918 {
4919 if (end[idx] == NUL)
4920 break; /* Found the end of paste code. */
4921 continue;
4922 }
4923 if (!drop)
4924 {
4925 switch (mode)
4926 {
4927 case PASTE_CMDLINE:
4928 put_on_cmdline(buf, idx, TRUE);
4929 break;
4930
4931 case PASTE_EX:
4932 if (gap != NULL && ga_grow(gap, idx) == OK)
4933 {
4934 mch_memmove((char *)gap->ga_data + gap->ga_len,
4935 buf, (size_t)idx);
4936 gap->ga_len += idx;
4937 }
4938 break;
4939
4940 case PASTE_INSERT:
4941 if (stop_arrow() == OK)
4942 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004943 c = buf[0];
4944 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
4945 ins_eol(c);
4946 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01004947 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004948 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01004949 AppendToRedobuffLit(buf, idx);
4950 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01004951 }
4952 break;
4953
4954 case PASTE_ONE_CHAR:
4955 if (ret_char == -1)
4956 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01004957 if (has_mbyte)
4958 ret_char = (*mb_ptr2char)(buf);
4959 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004960 ret_char = buf[0];
4961 }
4962 break;
4963 }
4964 }
4965 idx = 0;
4966 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004967
Bram Moolenaarec2da362017-01-21 20:04:22 +01004968 --no_mapping;
4969 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004970 if (!save_paste)
4971 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01004972
4973 return ret_char;
4974}
4975
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004976#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004978ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004979{
4980 /* We will be leaving the current window, unless closing another tab. */
4981 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
4982 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
4983 {
4984 undisplay_dollar();
4985 start_arrow(&curwin->w_cursor);
4986# ifdef FEAT_CINDENT
4987 can_cindent = TRUE;
4988# endif
4989 }
4990
4991 if (c == K_TABLINE)
4992 goto_tabpage(current_tab);
4993 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004994 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004995 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004996 redraw_statuslines(); /* will redraw the tabline when needed */
4997 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004998}
4999#endif
5000
5001#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005003ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
5005 pos_T tpos;
5006
5007 undisplay_dollar();
5008 tpos = curwin->w_cursor;
5009 if (gui_do_scroll())
5010 {
5011 start_arrow(&tpos);
5012# ifdef FEAT_CINDENT
5013 can_cindent = TRUE;
5014# endif
5015 }
5016}
5017
5018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005019ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021 pos_T tpos;
5022
5023 undisplay_dollar();
5024 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005025 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
5027 start_arrow(&tpos);
5028# ifdef FEAT_CINDENT
5029 can_cindent = TRUE;
5030# endif
5031 }
5032}
5033#endif
5034
5035 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005036ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
5038 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005039 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
5041#ifdef FEAT_FOLDING
5042 if ((fdo_flags & FDO_HOR) && KeyTyped)
5043 foldOpenCursor();
5044#endif
5045 undisplay_dollar();
5046 tpos = curwin->w_cursor;
5047 if (oneleft() == OK)
5048 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005049#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5050 /* Only call start_arrow() when not busy with preediting, it will
5051 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005052 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005053#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005054 {
5055 start_arrow_with_change(&tpos, end_change);
5056 if (!end_change)
5057 AppendCharToRedobuff(K_LEFT);
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059#ifdef FEAT_RIGHTLEFT
5060 /* If exit reversed string, position is fixed */
5061 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5062 revins_legal++;
5063 revins_chars++;
5064#endif
5065 }
5066
5067 /*
5068 * if 'whichwrap' set for cursor in insert mode may go to
5069 * previous line
5070 */
5071 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5072 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005073 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 start_arrow(&tpos);
5075 --(curwin->w_cursor.lnum);
5076 coladvance((colnr_T)MAXCOL);
5077 curwin->w_set_curswant = TRUE; /* so we stay at the end */
5078 }
5079 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005080 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005081 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082}
5083
5084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005085ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 pos_T tpos;
5088
5089#ifdef FEAT_FOLDING
5090 if ((fdo_flags & FDO_HOR) && KeyTyped)
5091 foldOpenCursor();
5092#endif
5093 undisplay_dollar();
5094 tpos = curwin->w_cursor;
5095 if (c == K_C_HOME)
5096 curwin->w_cursor.lnum = 1;
5097 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 curwin->w_curswant = 0;
5100 start_arrow(&tpos);
5101}
5102
5103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
5106 pos_T tpos;
5107
5108#ifdef FEAT_FOLDING
5109 if ((fdo_flags & FDO_HOR) && KeyTyped)
5110 foldOpenCursor();
5111#endif
5112 undisplay_dollar();
5113 tpos = curwin->w_cursor;
5114 if (c == K_C_END)
5115 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5116 coladvance((colnr_T)MAXCOL);
5117 curwin->w_curswant = MAXCOL;
5118
5119 start_arrow(&tpos);
5120}
5121
5122 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005123ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005125 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126#ifdef FEAT_FOLDING
5127 if ((fdo_flags & FDO_HOR) && KeyTyped)
5128 foldOpenCursor();
5129#endif
5130 undisplay_dollar();
5131 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5132 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005133 start_arrow_with_change(&curwin->w_cursor, end_change);
5134 if (!end_change)
5135 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 (void)bck_word(1L, FALSE, FALSE);
5137 curwin->w_set_curswant = TRUE;
5138 }
5139 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005140 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005141 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142}
5143
5144 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005145ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005147 int end_change = dont_sync_undo == FALSE; // end undoable change
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149#ifdef FEAT_FOLDING
5150 if ((fdo_flags & FDO_HOR) && KeyTyped)
5151 foldOpenCursor();
5152#endif
5153 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005154 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005156 start_arrow_with_change(&curwin->w_cursor, end_change);
5157 if (!end_change)
5158 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (virtual_active())
5161 oneright();
5162 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005165 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 ++curwin->w_cursor.col;
5168 }
5169
5170#ifdef FEAT_RIGHTLEFT
5171 revins_legal++;
5172 if (revins_chars)
5173 revins_chars--;
5174#endif
5175 }
5176 /* if 'whichwrap' set for cursor in insert mode, may move the
5177 * cursor to the next line */
5178 else if (vim_strchr(p_ww, ']') != NULL
5179 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5180 {
5181 start_arrow(&curwin->w_cursor);
5182 curwin->w_set_curswant = TRUE;
5183 ++curwin->w_cursor.lnum;
5184 curwin->w_cursor.col = 0;
5185 }
5186 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005187 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005188 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189}
5190
5191 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005192ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005194 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#ifdef FEAT_FOLDING
5196 if ((fdo_flags & FDO_HOR) && KeyTyped)
5197 foldOpenCursor();
5198#endif
5199 undisplay_dollar();
5200 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5201 || gchar_cursor() != NUL)
5202 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005203 start_arrow_with_change(&curwin->w_cursor, end_change);
5204 if (!end_change)
5205 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 (void)fwd_word(1L, FALSE, 0);
5207 curwin->w_set_curswant = TRUE;
5208 }
5209 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005210 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005211 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212}
5213
5214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005215ins_up(
5216 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
5218 pos_T tpos;
5219 linenr_T old_topline = curwin->w_topline;
5220#ifdef FEAT_DIFF
5221 int old_topfill = curwin->w_topfill;
5222#endif
5223
5224 undisplay_dollar();
5225 tpos = curwin->w_cursor;
5226 if (cursor_up(1L, TRUE) == OK)
5227 {
5228 if (startcol)
5229 coladvance(getvcol_nolist(&Insstart));
5230 if (old_topline != curwin->w_topline
5231#ifdef FEAT_DIFF
5232 || old_topfill != curwin->w_topfill
5233#endif
5234 )
5235 redraw_later(VALID);
5236 start_arrow(&tpos);
5237#ifdef FEAT_CINDENT
5238 can_cindent = TRUE;
5239#endif
5240 }
5241 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005242 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243}
5244
5245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005246ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247{
5248 pos_T tpos;
5249
5250 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005251
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005252 if (mod_mask & MOD_MASK_CTRL)
5253 {
5254 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005255 if (first_tabpage->tp_next != NULL)
5256 {
5257 start_arrow(&curwin->w_cursor);
5258 goto_tabpage(-1);
5259 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005260 return;
5261 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 tpos = curwin->w_cursor;
5264 if (onepage(BACKWARD, 1L) == OK)
5265 {
5266 start_arrow(&tpos);
5267#ifdef FEAT_CINDENT
5268 can_cindent = TRUE;
5269#endif
5270 }
5271 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005272 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273}
5274
5275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005276ins_down(
5277 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
5279 pos_T tpos;
5280 linenr_T old_topline = curwin->w_topline;
5281#ifdef FEAT_DIFF
5282 int old_topfill = curwin->w_topfill;
5283#endif
5284
5285 undisplay_dollar();
5286 tpos = curwin->w_cursor;
5287 if (cursor_down(1L, TRUE) == OK)
5288 {
5289 if (startcol)
5290 coladvance(getvcol_nolist(&Insstart));
5291 if (old_topline != curwin->w_topline
5292#ifdef FEAT_DIFF
5293 || old_topfill != curwin->w_topfill
5294#endif
5295 )
5296 redraw_later(VALID);
5297 start_arrow(&tpos);
5298#ifdef FEAT_CINDENT
5299 can_cindent = TRUE;
5300#endif
5301 }
5302 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005303 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304}
5305
5306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005307ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309 pos_T tpos;
5310
5311 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005312
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005313 if (mod_mask & MOD_MASK_CTRL)
5314 {
5315 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005316 if (first_tabpage->tp_next != NULL)
5317 {
5318 start_arrow(&curwin->w_cursor);
5319 goto_tabpage(0);
5320 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005321 return;
5322 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 tpos = curwin->w_cursor;
5325 if (onepage(FORWARD, 1L) == OK)
5326 {
5327 start_arrow(&tpos);
5328#ifdef FEAT_CINDENT
5329 can_cindent = TRUE;
5330#endif
5331 }
5332 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005333 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334}
5335
5336#ifdef FEAT_DND
5337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005338ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
5340 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5341}
5342#endif
5343
5344/*
5345 * Handle TAB in Insert or Replace mode.
5346 * Return TRUE when the TAB needs to be inserted like a normal character.
5347 */
5348 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005349ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 int ind;
5352 int i;
5353 int temp;
5354
5355 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5356 Insstart_blank_vcol = get_nolist_virtcol();
5357 if (echeck_abbr(TAB + ABBR_OFF))
5358 return FALSE;
5359
5360 ind = inindent(0);
5361#ifdef FEAT_CINDENT
5362 if (ind)
5363 can_cindent = FALSE;
5364#endif
5365
5366 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005367 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 */
5369 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005370#ifdef FEAT_VARTABS
5371 && !(p_sta && ind
5372 /* These five lines mean 'tabstop' != 'shiftwidth' */
5373 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5374 || (tabstop_count(curbuf->b_p_vts_array) == 1
5375 && tabstop_first(curbuf->b_p_vts_array)
5376 != get_sw_value(curbuf))
5377 || (tabstop_count(curbuf->b_p_vts_array) == 0
5378 && curbuf->b_p_ts != get_sw_value(curbuf))))
5379 && tabstop_count(curbuf->b_p_vsts_array) == 0
5380#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005381 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005382#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005383 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 return TRUE;
5385
5386 if (stop_arrow() == FAIL)
5387 return TRUE;
5388
5389 did_ai = FALSE;
5390#ifdef FEAT_SMARTINDENT
5391 did_si = FALSE;
5392 can_si = FALSE;
5393 can_si_back = FALSE;
5394#endif
5395 AppendToRedobuff((char_u *)"\t");
5396
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005397#ifdef FEAT_VARTABS
5398 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
5399 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005400 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005401 temp -= get_nolist_virtcol() % temp;
5402 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005403 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005404 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005405 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005406 curbuf->b_p_vsts_array);
5407 else /* otherwise use 'tabstop' */
5408 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5409 curbuf->b_p_vts_array);
5410#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005412 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005413 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
5414 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 else /* otherwise use 'tabstop' */
5416 temp = (int)curbuf->b_p_ts;
5417 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420 /*
5421 * Insert the first space with ins_char(). It will delete one char in
5422 * replace mode. Insert the rest with ins_str(); it will not delete any
5423 * chars. For VREPLACE mode, we use ins_char() for all characters.
5424 */
5425 ins_char(' ');
5426 while (--temp > 0)
5427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 if (State & VREPLACE_FLAG)
5429 ins_char(' ');
5430 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
5432 ins_str((char_u *)" ");
5433 if (State & REPLACE_FLAG) /* no char replaced */
5434 replace_push(NUL);
5435 }
5436 }
5437
5438 /*
5439 * When 'expandtab' not set: Replace spaces by TABs where possible.
5440 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005441#ifdef FEAT_VARTABS
5442 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5443 || get_sts_value() > 0
5444 || (p_sta && ind)))
5445#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005446 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
5449 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 char_u *saved_line = NULL; /* init for GCC */
5451 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 pos_T fpos;
5453 pos_T *cursor;
5454 colnr_T want_vcol, vcol;
5455 int change_col = -1;
5456 int save_list = curwin->w_p_list;
5457
5458 /*
5459 * Get the current line. For VREPLACE mode, don't make real changes
5460 * yet, just work on a copy of the line.
5461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 if (State & VREPLACE_FLAG)
5463 {
5464 pos = curwin->w_cursor;
5465 cursor = &pos;
5466 saved_line = vim_strsave(ml_get_curline());
5467 if (saved_line == NULL)
5468 return FALSE;
5469 ptr = saved_line + pos.col;
5470 }
5471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 {
5473 ptr = ml_get_cursor();
5474 cursor = &curwin->w_cursor;
5475 }
5476
5477 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
5478 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5479 curwin->w_p_list = FALSE;
5480
5481 /* Find first white before the cursor */
5482 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005483 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
5485 --fpos.col;
5486 --ptr;
5487 }
5488
5489 /* In Replace mode, don't change characters before the insert point. */
5490 if ((State & REPLACE_FLAG)
5491 && fpos.lnum == Insstart.lnum
5492 && fpos.col < Insstart.col)
5493 {
5494 ptr += Insstart.col - fpos.col;
5495 fpos.col = Insstart.col;
5496 }
5497
5498 /* compute virtual column numbers of first white and cursor */
5499 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5500 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5501
Bram Moolenaar597a4222014-06-25 14:39:50 +02005502 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5503 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01005504 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005506 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 if (vcol + i > want_vcol)
5508 break;
5509 if (*ptr != TAB)
5510 {
5511 *ptr = TAB;
5512 if (change_col < 0)
5513 {
5514 change_col = fpos.col; /* Column of first change */
5515 /* May have to adjust Insstart */
5516 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5517 Insstart.col = fpos.col;
5518 }
5519 }
5520 ++fpos.col;
5521 ++ptr;
5522 vcol += i;
5523 }
5524
5525 if (change_col >= 0)
5526 {
5527 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005528 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
5530 /* Skip over the spaces we need. */
5531 while (vcol < want_vcol && *ptr == ' ')
5532 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005533 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 ++ptr;
5535 ++repl_off;
5536 }
5537 if (vcol > want_vcol)
5538 {
5539 /* Must have a char with 'showbreak' just before it. */
5540 --ptr;
5541 --repl_off;
5542 }
5543 fpos.col += repl_off;
5544
5545 /* Delete following spaces. */
5546 i = cursor->col - fpos.col;
5547 if (i > 0)
5548 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005549 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005551 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 for (temp = i; --temp >= 0; )
5553 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005554#ifdef FEAT_TEXT_PROP
5555 curbuf->b_ml.ml_line_len -= i;
5556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005558#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005559 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005560 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005561 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005562 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5563 (char_u *)"\t", 1);
5564 }
5565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 cursor->col -= i;
5567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 /*
5569 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5570 * backspacing over the changed spacing and then inserting the new
5571 * spacing.
5572 */
5573 if (State & VREPLACE_FLAG)
5574 {
5575 /* Backspace from real cursor to change_col */
5576 backspace_until_column(change_col);
5577
5578 /* Insert each char in saved_line from changed_col to
5579 * ptr-cursor */
5580 ins_bytes_len(saved_line + change_col,
5581 cursor->col - change_col);
5582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 }
5584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 if (State & VREPLACE_FLAG)
5586 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 curwin->w_p_list = save_list;
5588 }
5589
5590 return FALSE;
5591}
5592
5593/*
5594 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005595 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005597 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005598ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
5600 int i;
5601
5602 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005603 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 undisplay_dollar();
5607
5608 /*
5609 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5610 * character under the cursor. Only push a NUL on the replace stack,
5611 * nothing to put back when the NL is deleted.
5612 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005613 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 replace_push(NUL);
5615
5616 /*
5617 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5618 * replacing the next line, so we push all of the characters left on the
5619 * line onto the replace stack. This is not done here though, it is done
5620 * in open_line().
5621 */
5622
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005623 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
5624 * CTRL-O). */
5625 if (virtual_active() && curwin->w_cursor.coladd > 0)
5626 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005627
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 /* NL in reverse insert will always start in the end of
5630 * current line. */
5631 if (revins_on)
5632 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5633#endif
5634
5635 AppendToRedobuff(NL_STR);
5636 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005637 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 old_indent = 0;
5639#ifdef FEAT_CINDENT
5640 can_cindent = TRUE;
5641#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005642#ifdef FEAT_FOLDING
5643 /* When inserting a line the cursor line must never be in a closed fold. */
5644 foldOpenCursor();
5645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005647 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648}
5649
5650#ifdef FEAT_DIGRAPHS
5651/*
5652 * Handle digraph in insert mode.
5653 * Returns character still to be inserted, or NUL when nothing remaining to be
5654 * done.
5655 */
5656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005657ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
5659 int c;
5660 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005661 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 pc_status = PC_STATUS_UNSET;
5664 if (redrawing() && !char_avail())
5665 {
5666 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005667 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005670 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_CMDL_INFO
5672 add_to_showcmd_c(Ctrl_K);
5673#endif
5674 }
5675
5676#ifdef USE_ON_FLY_SCROLL
5677 dont_scroll = TRUE; /* disallow scrolling here */
5678#endif
5679
5680 /* don't map the digraph chars. This also prevents the
5681 * mode message to be deleted when ESC is hit */
5682 ++no_mapping;
5683 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005684 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 --no_mapping;
5686 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005687 if (did_putchar)
5688 /* when the line fits in 'columns' the '?' is at the start of the next
5689 * line and will not be removed by the redraw */
5690 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005691
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (IS_SPECIAL(c) || mod_mask) /* special key */
5693 {
5694#ifdef FEAT_CMDL_INFO
5695 clear_showcmd();
5696#endif
5697 insert_special(c, TRUE, FALSE);
5698 return NUL;
5699 }
5700 if (c != ESC)
5701 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005702 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 if (redrawing() && !char_avail())
5704 {
5705 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005706 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
5708 if (char2cells(c) == 1)
5709 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005710 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005712 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714#ifdef FEAT_CMDL_INFO
5715 add_to_showcmd_c(c);
5716#endif
5717 }
5718 ++no_mapping;
5719 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005720 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 --no_mapping;
5722 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005723 if (did_putchar)
5724 /* when the line fits in 'columns' the '?' is at the start of the
5725 * next line and will not be removed by a redraw */
5726 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 if (cc != ESC)
5728 {
5729 AppendToRedobuff((char_u *)CTRL_V_STR);
5730 c = getdigraph(c, cc, TRUE);
5731#ifdef FEAT_CMDL_INFO
5732 clear_showcmd();
5733#endif
5734 return c;
5735 }
5736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737#ifdef FEAT_CMDL_INFO
5738 clear_showcmd();
5739#endif
5740 return NUL;
5741}
5742#endif
5743
5744/*
5745 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5746 * Returns the char to be inserted, or NUL if none found.
5747 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005749ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751 int c;
5752 int temp;
5753 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005754 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
5756 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5757 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005758 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 return NUL;
5760 }
5761
5762 /* try to advance to the cursor column */
5763 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005764 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 prev_ptr = ptr;
5766 validate_virtcol();
5767 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5768 {
5769 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005770 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 }
5772 if ((colnr_T)temp > curwin->w_virtcol)
5773 ptr = prev_ptr;
5774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005777 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 return c;
5779}
5780
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005781/*
5782 * CTRL-Y or CTRL-E typed in Insert mode.
5783 */
5784 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005785ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005786{
5787 int c = tc;
5788
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005789 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005790 {
5791 if (c == Ctrl_Y)
5792 scrolldown_clamp();
5793 else
5794 scrollup_clamp();
5795 redraw_later(VALID);
5796 }
5797 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005798 {
5799 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5800 if (c != NUL)
5801 {
5802 long tw_save;
5803
5804 /* The character must be taken literally, insert like it
5805 * was typed after a CTRL-V, and pretend 'textwidth'
5806 * wasn't set. Digits, 'o' and 'x' are special after a
5807 * CTRL-V, don't use it for these. */
5808 if (c < 256 && !isalnum(c))
5809 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
5810 tw_save = curbuf->b_p_tw;
5811 curbuf->b_p_tw = -1;
5812 insert_special(c, TRUE, FALSE);
5813 curbuf->b_p_tw = tw_save;
5814#ifdef FEAT_RIGHTLEFT
5815 revins_chars++;
5816 revins_legal++;
5817#endif
5818 c = Ctrl_V; /* pretend CTRL-V is last character */
5819 auto_format(FALSE, TRUE);
5820 }
5821 }
5822 return c;
5823}
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825/*
5826 * Get the value that w_virtcol would have when 'list' is off.
5827 * Unless 'cpo' contains the 'L' flag.
5828 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005829 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005830get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005832 // check validity of cursor in current buffer
5833 if (curwin->w_buffer == NULL
5834 || curwin->w_buffer->b_ml.ml_mfp == NULL
5835 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5836 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5838 return getvcol_nolist(&curwin->w_cursor);
5839 validate_virtcol();
5840 return curwin->w_virtcol;
5841}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005842
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005843#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005844/*
5845 * Handle the InsertCharPre autocommand.
5846 * "c" is the character that was typed.
5847 * Return a pointer to allocated memory with the replacement string.
5848 * Return NULL to continue inserting "c".
5849 */
5850 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005851do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005852{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005853 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005854 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005855 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005856
5857 /* Return quickly when there is nothing to do. */
5858 if (!has_insertcharpre())
5859 return NULL;
5860
Bram Moolenaar704984a2012-06-01 14:57:51 +02005861 if (has_mbyte)
5862 buf[(*mb_char2bytes)(c, buf)] = NUL;
5863 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005864 {
5865 buf[0] = c;
5866 buf[1] = NUL;
5867 }
5868
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005869 /* Lock the text to avoid weird things from happening. */
5870 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005871 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005872
Bram Moolenaar704984a2012-06-01 14:57:51 +02005873 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005874 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005875 {
5876 /* Get the value of v:char. It may be empty or more than one
5877 * character. Only use it when changed, otherwise continue with the
5878 * original character to avoid breaking autoindent. */
5879 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5880 res = vim_strsave(get_vim_var_str(VV_CHAR));
5881 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005882
5883 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
5884 --textlock;
5885
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005886 // Restore the State, it may have been changed.
5887 State = save_State;
5888
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005889 return res;
5890}
5891#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005892
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005893#if defined(FEAT_CINDENT) || defined(PROTO)
5894 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005895get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005896{
5897 return can_cindent;
5898}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005899
5900 void
5901set_can_cindent(int val)
5902{
5903 can_cindent = val;
5904}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005905#endif
5906
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005907/*
5908 * Trigger "event" and take care of fixing undo.
5909 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005910 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005911ins_apply_autocmds(event_T event)
5912{
5913 varnumber_T tick = CHANGEDTICK(curbuf);
5914 int r;
5915
5916 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5917
5918 // If u_savesub() was called then we are not prepared to start
5919 // a new line. Call u_save() with no contents to fix that.
5920 if (tick != CHANGEDTICK(curbuf))
5921 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5922
5923 return r;
5924}