blob: 4cbfc773b31d049ded2df0a75b18393987b0fe9a [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 /*
239 * When doing a paste with the middle mouse button, Insstart is set to
240 * where the paste started.
241 */
242 if (where_paste_started.lnum != 0)
243 Insstart = where_paste_started;
244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {
246 Insstart = curwin->w_cursor;
247 if (startln)
248 Insstart.col = 0;
249 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000250 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 Insstart_blank_vcol = MAXCOL;
252 if (!did_ai)
253 ai_col = 0;
254
255 if (cmdchar != NUL && restart_edit == 0)
256 {
257 ResetRedobuff();
258 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (cmdchar == 'V' || cmdchar == 'v')
260 {
261 /* "gR" or "gr" command */
262 AppendCharToRedobuff('g');
263 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
264 }
265 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100267 if (cmdchar == K_PS)
268 AppendCharToRedobuff('a');
269 else
270 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 if (cmdchar == 'g') /* "gI" command */
272 AppendCharToRedobuff('I');
273 else if (cmdchar == 'r') /* "r<CR>" command */
274 count = 1; /* insert only one <CR> */
275 }
276 }
277
278 if (cmdchar == 'R')
279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 State = REPLACE;
281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 else if (cmdchar == 'V' || cmdchar == 'v')
283 {
284 State = VREPLACE;
285 replaceState = VREPLACE;
286 orig_line_count = curbuf->b_ml.ml_line_count;
287 vr_lines_changed = 1;
288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 else
290 State = INSERT;
291
292 stop_insert_mode = FALSE;
293
294 /*
295 * Need to recompute the cursor position, it might move when the cursor is
296 * on a TAB or special character.
297 */
298 curs_columns(TRUE);
299
300 /*
301 * Enable langmap or IME, indicated by 'iminsert'.
302 * Note that IME may enabled/disabled without us noticing here, thus the
303 * 'iminsert' value may not reflect what is actually used. It is updated
304 * when hitting <Esc>.
305 */
306 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
307 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100308#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
310#endif
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#ifdef FEAT_CMDL_INFO
314 clear_showcmd();
315#endif
316#ifdef FEAT_RIGHTLEFT
317 /* there is no reverse replace mode */
318 revins_on = (State == INSERT && p_ri);
319 if (revins_on)
320 undisplay_dollar();
321 revins_chars = 0;
322 revins_legal = 0;
323 revins_scol = -1;
324#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100325 if (!p_ek)
326 /* Disable bracketed paste mode, we won't recognize the escape
327 * sequences. */
328 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329
330 /*
331 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100332 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
333 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 */
335 if (restart_edit != 0 && stuff_empty())
336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 /*
338 * After a paste we consider text typed to be part of the insert for
339 * the pasted text. You can backspace over the pasted text too.
340 */
341 if (where_paste_started.lnum)
342 arrow_used = FALSE;
343 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 arrow_used = TRUE;
345 restart_edit = 0;
346
347 /*
348 * If the cursor was after the end-of-line before the CTRL-O and it is
349 * now at the end-of-line, put it after the end-of-line (this is not
350 * correct in very rare cases).
351 * Also do this if curswant is greater than the current virtual
352 * column. Eg after "^O$" or "^O80|".
353 */
354 validate_virtcol();
355 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000356 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 || curwin->w_curswant > curwin->w_virtcol)
358 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
359 {
360 if (ptr[1] == NUL)
361 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 else if (has_mbyte)
363 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000364 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (ptr[i] == NUL)
366 curwin->w_cursor.col += i;
367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000369 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371 else
372 arrow_used = FALSE;
373
374 /* we are in insert mode now, don't need to start it anymore */
375 need_start_insertmode = FALSE;
376
377 /* Need to save the line for undo before inserting the first char. */
378 ins_need_undo = TRUE;
379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 where_paste_started.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#ifdef FEAT_CINDENT
382 can_cindent = TRUE;
383#endif
384#ifdef FEAT_FOLDING
385 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
386 * restarting. */
387 if (!p_im && did_restart_edit == 0)
388 foldOpenCursor();
389#endif
390
391 /*
392 * If 'showmode' is set, show the current (insert/replace/..) mode.
393 * A warning message for changing a readonly file is given here, before
394 * actually changing anything. It's put after the mode, if any.
395 */
396 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000397 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 i = showmode();
399
400 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000401 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
403#ifdef CURSOR_SHAPE
404 ui_cursor_shape(); /* may show different cursor shape */
405#endif
406#ifdef FEAT_DIGRAPHS
407 do_digraph(-1); /* clear digraphs */
408#endif
409
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000410 /*
411 * Get the current length of the redo buffer, those characters have to be
412 * skipped if we want to get to the inserted characters.
413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 ptr = get_inserted();
415 if (ptr == NULL)
416 new_insert_skip = 0;
417 else
418 {
419 new_insert_skip = (int)STRLEN(ptr);
420 vim_free(ptr);
421 }
422
423 old_indent = 0;
424
425 /*
426 * Main loop in Insert mode: repeat until Insert mode is left.
427 */
428 for (;;)
429 {
430#ifdef FEAT_RIGHTLEFT
431 if (!revins_legal)
432 revins_scol = -1; /* reset on illegal motions */
433 else
434 revins_legal = 0;
435#endif
436 if (arrow_used) /* don't repeat insert when arrow key used */
437 count = 0;
438
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100439 if (update_Insstart_orig)
440 Insstart_orig = Insstart;
441
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200442 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {
444 /* ":stopinsert" used or 'insertmode' reset */
445 count = 0;
446 goto doESCkey;
447 }
448
449 /* set curwin->w_curswant for next K_DOWN or K_UP */
450 if (!arrow_used)
451 curwin->w_set_curswant = TRUE;
452
453 /* If there is no typeahead may check for timestamps (e.g., for when a
454 * menu invoked a shell command). */
455 if (stuff_empty())
456 {
457 did_check_timestamps = FALSE;
458 if (need_check_timestamps)
459 check_timestamps(FALSE);
460 }
461
462 /*
463 * When emsg() was called msg_scroll will have been set.
464 */
465 msg_scroll = FALSE;
466
467#ifdef FEAT_GUI
468 /* When 'mousefocus' is set a mouse movement may have taken us to
469 * another window. "need_mouse_correct" may then be set because of an
470 * autocommand. */
471 if (need_mouse_correct)
472 gui_mouse_correct();
473#endif
474
475#ifdef FEAT_FOLDING
476 /* Open fold at the cursor line, according to 'foldopen'. */
477 if (fdo_flags & FDO_INSERT)
478 foldOpenCursor();
479 /* Close folds where the cursor isn't, according to 'foldclose' */
480 if (!char_avail())
481 foldCheckClose();
482#endif
483
Bram Moolenaarf2732452018-06-03 14:47:35 +0200484#ifdef FEAT_JOB_CHANNEL
485 if (bt_prompt(curbuf))
486 {
487 init_prompt(cmdchar_todo);
488 cmdchar_todo = NUL;
489 }
490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 /*
493 * If we inserted a character at the last position of the last line in
494 * the window, scroll the window one line up. This avoids an extra
495 * redraw.
496 * This is detected when the cursor column is smaller after inserting
497 * something.
498 * Don't do this when the topline changed already, it has
499 * already been adjusted (by insertchar() calling open_line())).
500 */
501 if (curbuf->b_mod_set
502 && curwin->w_p_wrap
503 && !did_backspace
504 && curwin->w_topline == old_topline
505#ifdef FEAT_DIFF
506 && curwin->w_topfill == old_topfill
507#endif
508 )
509 {
510 mincol = curwin->w_wcol;
511 validate_cursor_col();
512
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200513 if (
514#ifdef FEAT_VARTABS
515 (int)curwin->w_wcol < mincol - tabstop_at(
516 get_nolist_virtcol(), curbuf->b_p_ts,
517 curbuf->b_p_vts_array)
518#else
519 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100522 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 && (curwin->w_cursor.lnum != curwin->w_topline
524#ifdef FEAT_DIFF
525 || curwin->w_topfill > 0
526#endif
527 ))
528 {
529#ifdef FEAT_DIFF
530 if (curwin->w_topfill > 0)
531 --curwin->w_topfill;
532 else
533#endif
534#ifdef FEAT_FOLDING
535 if (hasFolding(curwin->w_topline, NULL, &old_topline))
536 set_topline(curwin, old_topline + 1);
537 else
538#endif
539 set_topline(curwin, curwin->w_topline + 1);
540 }
541 }
542
543 /* May need to adjust w_topline to show the cursor. */
544 update_topline();
545
546 did_backspace = FALSE;
547
548 validate_cursor(); /* may set must_redraw */
549
550 /*
551 * Redraw the display when no characters are waiting.
552 * Also shows mode, ruler and positions cursor.
553 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000554 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if (curwin->w_p_scb)
557 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar860cae12010-06-05 23:22:07 +0200559 if (curwin->w_p_crb)
560 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 update_curswant();
562 old_topline = curwin->w_topline;
563#ifdef FEAT_DIFF
564 old_topfill = curwin->w_topfill;
565#endif
566
567#ifdef USE_ON_FLY_SCROLL
568 dont_scroll = FALSE; /* allow scrolling here */
569#endif
570
571 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100572 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100574 if (c != K_CURSORHOLD)
575 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200576
577 /* After using CTRL-G U the next cursor key will not break undo. */
578 if (dont_sync_undo == MAYBE)
579 dont_sync_undo = TRUE;
580 else
581 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100582 if (cmdchar == K_PS)
583 /* Got here from normal mode when bracketed paste started. */
584 c = K_PS;
585 else
586 do
587 {
588 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200589
590 if (stop_insert_mode)
591 {
592 // Insert mode ended, possibly from a callback.
593 count = 0;
594 nomove = TRUE;
595 goto doESCkey;
596 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100597 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000599 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
600 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#ifdef FEAT_RIGHTLEFT
603 if (p_hkmap && KeyTyped)
604 c = hkmap(c); /* Hebrew mode mapping */
605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000607 /*
608 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000609 * and the cursor is still in the completed word. Only when there is
610 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000611 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100612 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000613 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100614 && curwin->w_cursor.col >= ins_compl_col()
615 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000616 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000617 /* BS: Delete one character from "compl_leader". */
618 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100619 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000620 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000621 continue;
622
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000623 /* When no match was selected or it was edited. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100624 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000625 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000626 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000627 * "compl_leader". Except when at the original match and
628 * there is nothing to add, CTRL-L works like CTRL-P then. */
629 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100630 && (!ctrl_x_mode_line_or_eval()
631 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000632 {
633 ins_compl_addfrommatch();
634 continue;
635 }
636
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000637 /* A non-white character that fits in with the current
638 * completion: Add to "compl_leader". */
639 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000640 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100641#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100642 /* Trigger InsertCharPre. */
643 char_u *str = do_insert_char_pre(c);
644 char_u *p;
645
646 if (str != NULL)
647 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100648 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100649 ins_compl_addleader(PTR2CHAR(p));
650 vim_free(str);
651 }
652 else
653#endif
654 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000655 continue;
656 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000657
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000658 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100659 * ins_compl_enter_selects() is set the Enter key does the
660 * same. */
661 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200662 && (c == CAR || c == K_KENTER || c == NL)))
663 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000664 {
665 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200666 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000667 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000668 }
669 }
670
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
672 * it does fix up the text when finishing completion. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100673 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000674 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000675 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
Bram Moolenaar488c6512005-08-11 20:09:58 +0000677 /* CTRL-\ CTRL-N goes to Normal mode,
678 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
679 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (c == Ctrl_BSL)
681 {
682 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000683 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 ++no_mapping;
685 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000686 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 --no_mapping;
688 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000689 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000691 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 vungetc(c);
693 c = Ctrl_BSL;
694 }
695 else if (c == Ctrl_G && p_im)
696 continue;
697 else
698 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000699 if (c == Ctrl_O)
700 {
701 ins_ctrl_o();
702 ins_at_eol = FALSE; /* cursor keeps its column */
703 nomove = TRUE;
704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 count = 0;
706 goto doESCkey;
707 }
708 }
709
710#ifdef FEAT_DIGRAPHS
711 c = do_digraph(c);
712#endif
713
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100714 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (c == Ctrl_V || c == Ctrl_Q)
717 {
718 ins_ctrl_v();
719 c = Ctrl_V; /* pretend CTRL-V is last typed character */
720 continue;
721 }
722
723#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200724 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
726 /* A key name preceded by a bang means this key is not to be
727 * inserted. Skip ahead to the re-indenting below.
728 * A key name preceded by a star means that indenting has to be
729 * done before inserting the key. */
730 line_is_white = inindent(0);
731 if (in_cinkeys(c, '!', line_is_white))
732 goto force_cindent;
733 if (can_cindent && in_cinkeys(c, '*', line_is_white)
734 && stop_arrow() == OK)
735 do_c_expr_indent();
736 }
737#endif
738
739#ifdef FEAT_RIGHTLEFT
740 if (curwin->w_p_rl)
741 switch (c)
742 {
743 case K_LEFT: c = K_RIGHT; break;
744 case K_S_LEFT: c = K_S_RIGHT; break;
745 case K_C_LEFT: c = K_C_RIGHT; break;
746 case K_RIGHT: c = K_LEFT; break;
747 case K_S_RIGHT: c = K_S_LEFT; break;
748 case K_C_RIGHT: c = K_C_LEFT; break;
749 }
750#endif
751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /*
753 * If 'keymodel' contains "startsel", may start selection. If it
754 * does, a CTRL-O and c will be stuffed, we need to get these
755 * characters.
756 */
757 if (ins_start_select(c))
758 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 /*
761 * The big switch to handle a character in insert mode.
762 */
763 switch (c)
764 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000765 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (echeck_abbr(ESC + ABBR_OFF))
767 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200768 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000770 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#ifdef FEAT_CMDWIN
772 if (c == Ctrl_C && cmdwin_type != 0)
773 {
774 /* Close the cmdline window. */
775 cmdwin_result = K_IGNORE;
776 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000777 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 goto doESCkey;
779 }
780#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200781#ifdef FEAT_JOB_CHANNEL
782 if (c == Ctrl_C && bt_prompt(curbuf))
783 {
784 if (invoke_prompt_interrupt())
785 {
786 if (!bt_prompt(curbuf))
787 // buffer changed to a non-prompt buffer, get out of
788 // Insert mode
789 goto doESCkey;
790 break;
791 }
792 }
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795#ifdef UNIX
796do_intr:
797#endif
798 /* when 'insertmode' set, and not halfway a mapping, don't leave
799 * Insert mode */
800 if (goto_im())
801 {
802 if (got_int)
803 {
804 (void)vgetc(); /* flush all buffers */
805 got_int = FALSE;
806 }
807 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200808 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 break;
810 }
811doESCkey:
812 /*
813 * This is the ONLY return from edit()!
814 */
815 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
816 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000817 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 o_lnum = curwin->w_cursor.lnum;
819
Bram Moolenaar488c6512005-08-11 20:09:58 +0000820 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000821 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100822 // When CTRL-C was typed got_int will be set, with the result
823 // that the autocommands won't be executed. When mapped got_int
824 // is not set, but let's keep the behavior the same.
825 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200826 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000827 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 continue;
831
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000832 case Ctrl_Z: /* suspend when 'insertmode' set */
833 if (!p_im)
834 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100835 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100836#ifdef CURSOR_SHAPE
837 ui_cursor_shape(); /* may need to update cursor shape */
838#endif
839 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000840
841 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000842#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000844 goto docomplete;
845#endif
846 if (echeck_abbr(Ctrl_O + ABBR_OFF))
847 break;
848 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000849
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000850 /* don't move the cursor left when 'virtualedit' has "onemore". */
851 if (ve_flags & VE_ONEMORE)
852 {
853 ins_at_eol = FALSE;
854 nomove = TRUE;
855 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000856 count = 0;
857 goto doESCkey;
858
Bram Moolenaar572cb562005-08-05 21:35:02 +0000859 case K_INS: /* toggle insert/replace mode */
860 case K_KINS:
861 ins_insert(replaceState);
862 break;
863
864 case K_SELECT: /* end of Select mode mapping - ignore */
865 break;
866
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000867 case K_HELP: /* Help key works like <ESC> <Help> */
868 case K_F1:
869 case K_XF1:
870 stuffcharReadbuff(K_HELP);
871 if (p_im)
872 need_start_insertmode = TRUE;
873 goto doESCkey;
874
875#ifdef FEAT_NETBEANS_INTG
876 case K_F21: /* NetBeans command */
877 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000878 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000879 --no_mapping;
880 netbeans_keycommand(i);
881 break;
882#endif
883
884 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 case NUL:
886 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000887 /* For ^@ the trailing ESC will end the insert, unless there is an
888 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
890 && c != Ctrl_A && !p_im)
891 goto doESCkey; /* quit insert mode */
892 inserted_space = FALSE;
893 break;
894
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000895 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ins_reg();
897 auto_format(FALSE, TRUE);
898 inserted_space = FALSE;
899 break;
900
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000901 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ins_ctrl_g();
903 break;
904
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000905 case Ctrl_HAT: /* switch input mode and/or langmap */
906 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 break;
908
909#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000910 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (!p_ari)
912 goto normalchar;
913 ins_ctrl_();
914 break;
915#endif
916
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000917 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200918#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100919 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 goto docomplete;
921#endif
922 /* FALLTHROUGH */
923
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000924 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000927 if (has_compl_option(FALSE))
928 goto docomplete;
929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 ins_shift(c, lastc);
933 auto_format(FALSE, TRUE);
934 inserted_space = FALSE;
935 break;
936
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000937 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 case K_KDEL:
939 ins_del();
940 auto_format(FALSE, TRUE);
941 break;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 case Ctrl_H:
945 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
946 auto_format(FALSE, TRUE);
947 break;
948
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200950#ifdef FEAT_JOB_CHANNEL
951 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
952 {
953 // In a prompt window CTRL-W is used for window commands.
954 // Use Shift-CTRL-W to delete a word.
955 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200956 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200957 nomove = TRUE;
958 count = 0;
959 goto doESCkey;
960 }
961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
963 auto_format(FALSE, TRUE);
964 break;
965
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000967# ifdef FEAT_COMPL_FUNC
968 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100969 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000970 goto docomplete;
971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
973 auto_format(FALSE, TRUE);
974 inserted_space = FALSE;
975 break;
976
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000977 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 case K_LEFTMOUSE_NM:
979 case K_LEFTDRAG:
980 case K_LEFTRELEASE:
981 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100982 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 case K_MIDDLEMOUSE:
984 case K_MIDDLEDRAG:
985 case K_MIDDLERELEASE:
986 case K_RIGHTMOUSE:
987 case K_RIGHTDRAG:
988 case K_RIGHTRELEASE:
989 case K_X1MOUSE:
990 case K_X1DRAG:
991 case K_X1RELEASE:
992 case K_X2MOUSE:
993 case K_X2DRAG:
994 case K_X2RELEASE:
995 ins_mouse(c);
996 break;
997
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000998 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200999 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 break;
1001
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001002 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001003 ins_mousescroll(MSCR_UP);
1004 break;
1005
1006 case K_MOUSELEFT: /* Scroll wheel left */
1007 ins_mousescroll(MSCR_LEFT);
1008 break;
1009
1010 case K_MOUSERIGHT: /* Scroll wheel right */
1011 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001013
Bram Moolenaarec2da362017-01-21 20:04:22 +01001014 case K_PS:
1015 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1016 if (cmdchar == K_PS)
1017 /* invoked from normal mode, bail out */
1018 goto doESCkey;
1019 break;
1020 case K_PE:
1021 /* Got K_PE without K_PS, ignore. */
1022 break;
1023
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001024#ifdef FEAT_GUI_TABLINE
1025 case K_TABLINE:
1026 case K_TABMENU:
1027 ins_tabline(c);
1028 break;
1029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001031 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 break;
1033
Bram Moolenaar754b5602006-02-09 23:53:20 +00001034 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001035 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001036 did_cursorhold = TRUE;
1037 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001038
Bram Moolenaar4f974752019-02-17 17:44:42 +01001039#ifdef FEAT_GUI_MSWIN
1040 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1041 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001042 case K_F4:
1043 if (mod_mask != MOD_MASK_ALT)
1044 goto normalchar;
1045 break;
1046#endif
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#ifdef FEAT_GUI
1049 case K_VER_SCROLLBAR:
1050 ins_scroll();
1051 break;
1052
1053 case K_HOR_SCROLLBAR:
1054 ins_horscroll();
1055 break;
1056#endif
1057
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 case K_S_HOME:
1061 case K_C_HOME:
1062 ins_home(c);
1063 break;
1064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 case K_S_END:
1068 case K_C_END:
1069 ins_end(c);
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001073 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1074 ins_s_left();
1075 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001076 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 break;
1078
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 case K_C_LEFT:
1081 ins_s_left();
1082 break;
1083
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001085 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1086 ins_s_right();
1087 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001088 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 break;
1090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001091 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 case K_C_RIGHT:
1093 ins_s_right();
1094 break;
1095
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001096 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001097 if (pum_visible())
1098 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001099 if (mod_mask & MOD_MASK_SHIFT)
1100 ins_pageup();
1101 else
1102 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 break;
1104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001105 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 case K_PAGEUP:
1107 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001108 if (pum_visible())
1109 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 ins_pageup();
1111 break;
1112
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001113 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001114 if (pum_visible())
1115 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001116 if (mod_mask & MOD_MASK_SHIFT)
1117 ins_pagedown();
1118 else
1119 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 case K_PAGEDOWN:
1124 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001125 if (pum_visible())
1126 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ins_pagedown();
1128 break;
1129
1130#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ins_drop();
1133 break;
1134#endif
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 c = TAB;
1138 /* FALLTHROUGH */
1139
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001140 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001141#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001142 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 goto docomplete;
1144#endif
1145 inserted_space = FALSE;
1146 if (ins_tab())
1147 goto normalchar; /* insert TAB as a normal char */
1148 auto_format(FALSE, TRUE);
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 c = CAR;
1153 /* FALLTHROUGH */
1154 case CAR:
1155 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001156#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 /* In a quickfix window a <CR> jumps to the error under the
1158 * cursor. */
1159 if (bt_quickfix(curbuf) && c == CAR)
1160 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001161 if (curwin->w_llist_ref == NULL) /* quickfix window */
1162 do_cmdline_cmd((char_u *)".cc");
1163 else /* location list window */
1164 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 break;
1166 }
1167#endif
1168#ifdef FEAT_CMDWIN
1169 if (cmdwin_type != 0)
1170 {
1171 /* Execute the command in the cmdline window. */
1172 cmdwin_result = CAR;
1173 goto doESCkey;
1174 }
1175#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001176#ifdef FEAT_JOB_CHANNEL
1177 if (bt_prompt(curbuf))
1178 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001179 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001180 if (!bt_prompt(curbuf))
1181 // buffer changed to a non-prompt buffer, get out of
1182 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001183 goto doESCkey;
1184 break;
1185 }
1186#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001187 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 goto doESCkey; /* out of memory */
1189 auto_format(FALSE, FALSE);
1190 inserted_space = FALSE;
1191 break;
1192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001193 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001194 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 if (has_compl_option(TRUE))
1197 goto docomplete;
1198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001200#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 c = ins_digraph();
1202 if (c == NUL)
1203 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001205 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
Bram Moolenaar572cb562005-08-05 21:35:02 +00001207 case Ctrl_X: /* Enter CTRL-X mode */
1208 ins_ctrl_x();
1209 break;
1210
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001212 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 goto normalchar;
1214 goto docomplete;
1215
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001216 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001217 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 goto normalchar;
1219 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001220
1221 case 's': /* Spelling completion after ^X */
1222 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001223 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001224 goto normalchar;
1225 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001228 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
1230 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1231 if (p_im)
1232 {
1233 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1234 break;
1235 goto doESCkey;
1236 }
1237 goto normalchar;
1238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 /* FALLTHROUGH */
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 case Ctrl_N:
1243 /* if 'complete' is empty then plain ^P is no longer special,
1244 * but it is under other ^X modes */
1245 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001246 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 goto normalchar;
1249
1250docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001251 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001252#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001253 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001254#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001255 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001256 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001257#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001258 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001259#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001260 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case Ctrl_Y: /* copy from previous line or scroll down */
1264 case Ctrl_E: /* copy from next line or scroll up */
1265 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 break;
1267
1268 default:
1269#ifdef UNIX
1270 if (c == intr_char) /* special interrupt char */
1271 goto do_intr;
1272#endif
1273
Bram Moolenaare659c952011-05-19 17:25:41 +02001274normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001276 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001278#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001279 if (!p_paste)
1280 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001281 /* Trigger InsertCharPre. */
1282 char_u *str = do_insert_char_pre(c);
1283 char_u *p;
1284
1285 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001286 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001287 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001288 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001289 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001290 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001291 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001292 c = PTR2CHAR(p);
1293 if (c == CAR || c == K_KENTER || c == NL)
1294 ins_eol(c);
1295 else
1296 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001297 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001298 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001299 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001300 vim_free(str);
1301 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001302 }
1303
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001304 /* If the new value is already inserted or an empty string
1305 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001306 if (c == NUL)
1307 break;
1308 }
1309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#ifdef FEAT_SMARTINDENT
1311 /* Try to perform smart-indenting. */
1312 ins_try_si(c);
1313#endif
1314
1315 if (c == ' ')
1316 {
1317 inserted_space = TRUE;
1318#ifdef FEAT_CINDENT
1319 if (inindent(0))
1320 can_cindent = FALSE;
1321#endif
1322 if (Insstart_blank_vcol == MAXCOL
1323 && curwin->w_cursor.lnum == Insstart.lnum)
1324 Insstart_blank_vcol = get_nolist_virtcol();
1325 }
1326
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001327 /* Insert a normal character and check for abbreviations on a
1328 * special character. Let CTRL-] expand abbreviations without
1329 * inserting it. */
1330 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001331 // Add ABBR_OFF for characters above 0x100, this is
1332 // what check_abbr() expects.
1333 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1334 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 insert_special(c, FALSE, FALSE);
1337#ifdef FEAT_RIGHTLEFT
1338 revins_legal++;
1339 revins_chars++;
1340#endif
1341 }
1342
1343 auto_format(FALSE, TRUE);
1344
1345#ifdef FEAT_FOLDING
1346 /* When inserting a character the cursor line must never be in a
1347 * closed fold. */
1348 foldOpenCursor();
1349#endif
1350 break;
1351 } /* end of switch (c) */
1352
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001353 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001354 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001355#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001356 /* but not in CTRL-X mode, a script can't restore the state */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001357 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001358#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001359 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001360 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 /* If the cursor was moved we didn't just insert a space */
1363 if (arrow_used)
1364 inserted_space = FALSE;
1365
1366#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001367 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
1369force_cindent:
1370 /*
1371 * Indent now if a key was typed that is in 'cinkeys'.
1372 */
1373 if (in_cinkeys(c, ' ', line_is_white))
1374 {
1375 if (stop_arrow() == OK)
1376 /* re-indent the current line */
1377 do_c_expr_indent();
1378 }
1379 }
1380#endif /* FEAT_CINDENT */
1381
1382 } /* for (;;) */
1383 /* NOTREACHED */
1384}
1385
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001386 int
1387ins_need_undo_get(void)
1388{
1389 return ins_need_undo;
1390}
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392/*
1393 * Redraw for Insert mode.
1394 * This is postponed until getting the next character to make '$' in the 'cpo'
1395 * option work correctly.
1396 * Only redraw when there are no characters available. This speeds up
1397 * inserting sequences of characters (e.g., for CTRL-R).
1398 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001399 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001400ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001402#ifdef FEAT_CONCEAL
1403 linenr_T conceal_old_cursor_line = 0;
1404 linenr_T conceal_new_cursor_line = 0;
1405 int conceal_update_lines = FALSE;
1406#endif
1407
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001408 if (char_avail())
1409 return;
1410
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001411 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1412 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001413 if (ready && (has_cursormovedI()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001414# ifdef FEAT_TEXT_PROP
1415 || popup_visible
1416# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001417# if defined(FEAT_CONCEAL)
1418 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001419# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001420 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001421 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001422 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001423 {
1424# ifdef FEAT_SYN_HL
1425 /* Need to update the screen first, to make sure syntax
1426 * highlighting is correct after making a change (e.g., inserting
1427 * a "(". The autocommand may also require a redraw, so it's done
1428 * again below, unfortunately. */
1429 if (syntax_present(curwin) && must_redraw)
1430 update_screen(0);
1431# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001432 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001433 {
1434 /* Make sure curswant is correct, an autocommand may call
1435 * getcurpos(). */
1436 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001437 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001438 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001439#ifdef FEAT_TEXT_PROP
1440 if (popup_visible)
1441 popup_check_cursor_pos();
1442#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001443# ifdef FEAT_CONCEAL
1444 if (curwin->w_p_cole > 0)
1445 {
1446 conceal_old_cursor_line = last_cursormoved.lnum;
1447 conceal_new_cursor_line = curwin->w_cursor.lnum;
1448 conceal_update_lines = TRUE;
1449 }
1450# endif
1451 last_cursormoved = curwin->w_cursor;
1452 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001453
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001454 /* Trigger TextChangedI if b_changedtick differs. */
1455 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001456 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001457 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001458 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001459 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001460 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001461
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001462 // save and restore curwin and curbuf, in case the autocmd changes them
1463 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001464 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001465 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001466 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001467 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1468 u_save(curwin->w_cursor.lnum,
1469 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001471
Bram Moolenaar5a093432018-02-10 18:15:19 +01001472 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1473 * TextChangedI will need to trigger for backwards compatibility, thus use
1474 * different b_last_changedtick* variables. */
1475 if (ready && has_textchangedP()
1476 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1477 && pum_visible())
1478 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001479 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001480 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001481
1482 // save and restore curwin and curbuf, in case the autocmd changes them
1483 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001484 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001485 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001486 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001487 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1488 u_save(curwin->w_cursor.lnum,
1489 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001490 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001491
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001492 // Trigger SafeState if nothing is pending.
1493 may_trigger_safestate(ready
1494 && !ins_compl_active()
1495 && !pum_visible());
1496
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001497#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001498 if ((conceal_update_lines
1499 && (conceal_old_cursor_line != conceal_new_cursor_line
1500 || conceal_cursor_line(curwin)))
1501 || need_cursor_line_redraw)
1502 {
1503 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001504 redrawWinline(curwin, conceal_old_cursor_line);
1505 redrawWinline(curwin, conceal_new_cursor_line == 0
1506 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001507 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001508 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001509 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001510#endif
1511 if (must_redraw)
1512 update_screen(0);
1513 else if (clear_cmdline || redraw_cmdline)
1514 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001515 showruler(FALSE);
1516 setcursor();
1517 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518}
1519
1520/*
1521 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001527 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
1529 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001530 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
1532 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001533 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001535 did_putchar = TRUE;
1536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1538
1539#ifdef FEAT_CMDL_INFO
1540 add_to_showcmd_c(Ctrl_V);
1541#endif
1542
1543 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001544 if (did_putchar)
1545 /* when the line fits in 'columns' the '^' is at the start of the next
1546 * line and will not removed by the redraw */
1547 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548#ifdef FEAT_CMDL_INFO
1549 clear_showcmd();
1550#endif
1551 insert_special(c, FALSE, TRUE);
1552#ifdef FEAT_RIGHTLEFT
1553 revins_chars++;
1554 revins_legal++;
1555#endif
1556}
1557
1558/*
1559 * Put a character directly onto the screen. It's not stored in a buffer.
1560 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1561 */
1562static int pc_status;
1563#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1564#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1565#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1566#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568static int pc_attr;
1569static int pc_row;
1570static int pc_col;
1571
1572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001573edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 int attr;
1576
1577 if (ScreenLines != NULL)
1578 {
1579 update_topline(); /* just in case w_topline isn't valid */
1580 validate_cursor();
1581 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001582 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 else
1584 attr = 0;
1585 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001586 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588#ifdef FEAT_RIGHTLEFT
1589 if (curwin->w_p_rl)
1590 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001591 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 if (has_mbyte)
1593 {
1594 int fix_col = mb_fix_col(pc_col, pc_row);
1595
1596 if (fix_col != pc_col)
1597 {
1598 screen_putchar(' ', pc_row, fix_col, attr);
1599 --curwin->w_wcol;
1600 pc_status = PC_STATUS_RIGHT;
1601 }
1602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 else
1605#endif
1606 {
1607 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (mb_lefthalve(pc_row, pc_col))
1609 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611
1612 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1616 pc_status = PC_STATUS_SET;
1617 }
1618 screen_putchar(c, pc_row, pc_col, attr);
1619 }
1620}
1621
Bram Moolenaarf2732452018-06-03 14:47:35 +02001622#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1623/*
1624 * Return the effective prompt for the current buffer.
1625 */
1626 char_u *
1627prompt_text(void)
1628{
1629 if (curbuf->b_prompt_text == NULL)
1630 return (char_u *)"% ";
1631 return curbuf->b_prompt_text;
1632}
1633
1634/*
1635 * Prepare for prompt mode: Make sure the last line has the prompt text.
1636 * Move the cursor to this line.
1637 */
1638 static void
1639init_prompt(int cmdchar_todo)
1640{
1641 char_u *prompt = prompt_text();
1642 char_u *text;
1643
1644 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1645 text = ml_get_curline();
1646 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1647 {
1648 // prompt is missing, insert it or append a line with it
1649 if (*text == NUL)
1650 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1651 else
1652 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1653 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1654 coladvance((colnr_T)MAXCOL);
1655 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1656 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001657
1658 // Insert always starts after the prompt, allow editing text after it.
1659 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1660 || Insstart_orig.col != (int)STRLEN(prompt))
1661 {
1662 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001663 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001664 Insstart_orig = Insstart;
1665 Insstart_textlen = Insstart.col;
1666 Insstart_blank_vcol = MAXCOL;
1667 arrow_used = FALSE;
1668 }
1669
Bram Moolenaarf2732452018-06-03 14:47:35 +02001670 if (cmdchar_todo == 'A')
1671 coladvance((colnr_T)MAXCOL);
1672 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001673 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001674 /* Make sure the cursor is in a valid position. */
1675 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001676}
1677
1678/*
1679 * Return TRUE if the cursor is in the editable position of the prompt line.
1680 */
1681 int
1682prompt_curpos_editable()
1683{
1684 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1685 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1686}
1687#endif
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689/*
1690 * Undo the previous edit_putchar().
1691 */
1692 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001693edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
1695 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (pc_status == PC_STATUS_RIGHT)
1698 ++curwin->w_wcol;
1699 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001700 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1703 }
1704}
1705
1706/*
1707 * Called when p_dollar is set: display a '$' at the end of the changed text
1708 * Only works when cursor is in the line that changes.
1709 */
1710 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001711display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 colnr_T save_col;
1714
1715 if (!redrawing())
1716 return;
1717
1718 cursor_off();
1719 save_col = curwin->w_cursor.col;
1720 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (has_mbyte)
1722 {
1723 char_u *p;
1724
1725 /* If on the last byte of a multi-byte move to the first byte. */
1726 p = ml_get_curline();
1727 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001730 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 {
1732 edit_putchar('$', FALSE);
1733 dollar_vcol = curwin->w_virtcol;
1734 }
1735 curwin->w_cursor.col = save_col;
1736}
1737
1738/*
1739 * Call this function before moving the cursor from the normal insert position
1740 * in insert mode.
1741 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001743undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001745 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001747 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001748 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750}
1751
1752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 * Truncate the space at the end of a line. This is to be used only in an
1754 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1755 * modes.
1756 */
1757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001758truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 int i;
1761
1762 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001763 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
1765 if (State & REPLACE_FLAG)
1766 replace_join(0); /* remove a NUL from the replace stack */
1767 }
1768 line[i + 1] = NUL;
1769}
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771/*
1772 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1773 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001774 * Will attempt not to go before "col" even when there is a composing
1775 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 */
1777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
1780 while ((int)curwin->w_cursor.col > col)
1781 {
1782 curwin->w_cursor.col--;
1783 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001784 replace_do_bs(col);
1785 else if (!del_char_after_col(col))
1786 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001790/*
1791 * Like del_char(), but make sure not to go before column "limit_col".
1792 * Only matters when there are composing characters.
1793 * Return TRUE when something was deleted.
1794 */
1795 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001796del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001797{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001798 if (enc_utf8 && limit_col >= 0)
1799 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001800 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001801
1802 /* Make sure the cursor is at the start of a character, but
1803 * skip forward again when going too far back because of a
1804 * composing character. */
1805 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001806 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001807 {
1808 int l = utf_ptr2len(ml_get_cursor());
1809
1810 if (l == 0) /* end of line */
1811 break;
1812 curwin->w_cursor.col += l;
1813 }
1814 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1815 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001816 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001817 }
1818 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001819 (void)del_char(FALSE);
1820 return TRUE;
1821}
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
1824 * Next character is interpreted literally.
1825 * A one, two or three digit decimal number is interpreted as its byte value.
1826 * If one or two digits are entered, the next character is given to vungetc().
1827 * For Unicode a character > 255 may be returned.
1828 */
1829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001830get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 int cc;
1833 int nc;
1834 int i;
1835 int hex = FALSE;
1836 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 if (got_int)
1840 return Ctrl_C;
1841
1842#ifdef FEAT_GUI
1843 /*
1844 * In GUI there is no point inserting the internal code for a special key.
1845 * It is more useful to insert the string "<KEY>" instead. This would
1846 * probably be useful in a text window too, but it would not be
1847 * vi-compatible (maybe there should be an option for it?) -- webb
1848 */
1849 if (gui.in_use)
1850 ++allow_keys;
1851#endif
1852#ifdef USE_ON_FLY_SCROLL
1853 dont_scroll = TRUE; /* disallow scrolling here */
1854#endif
1855 ++no_mapping; /* don't map the next key hits */
1856 cc = 0;
1857 i = 0;
1858 for (;;)
1859 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001860 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001862 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 add_to_showcmd(nc);
1864#endif
1865 if (nc == 'x' || nc == 'X')
1866 hex = TRUE;
1867 else if (nc == 'o' || nc == 'O')
1868 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 else if (nc == 'u' || nc == 'U')
1870 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 else
1872 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001873 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
1875 if (!vim_isxdigit(nc))
1876 break;
1877 cc = cc * 16 + hex2nr(nc);
1878 }
1879 else if (octal)
1880 {
1881 if (nc < '0' || nc > '7')
1882 break;
1883 cc = cc * 8 + nc - '0';
1884 }
1885 else
1886 {
1887 if (!VIM_ISDIGIT(nc))
1888 break;
1889 cc = cc * 10 + nc - '0';
1890 }
1891
1892 ++i;
1893 }
1894
Bram Moolenaar13505972019-01-24 15:04:48 +01001895 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 cc = 255; /* limit range to 0-255 */
1897 nc = 0;
1898
1899 if (hex) /* hex: up to two chars */
1900 {
1901 if (i >= 2)
1902 break;
1903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 else if (unicode) /* Unicode: up to four or eight chars */
1905 {
1906 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1907 break;
1908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 else if (i >= 3) /* decimal or octal: up to three chars */
1910 break;
1911 }
1912 if (i == 0) /* no number entered */
1913 {
1914 if (nc == K_ZERO) /* NUL is stored as NL */
1915 {
1916 cc = '\n';
1917 nc = 0;
1918 }
1919 else
1920 {
1921 cc = nc;
1922 nc = 0;
1923 }
1924 }
1925
1926 if (cc == 0) /* NUL is stored as NL */
1927 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001928 if (enc_dbcs && (cc & 0xff) == 0)
1929 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
1930 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
1932 --no_mapping;
1933#ifdef FEAT_GUI
1934 if (gui.in_use)
1935 --allow_keys;
1936#endif
1937 if (nc)
1938 vungetc(nc);
1939 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
1940 return cc;
1941}
1942
1943/*
1944 * Insert character, taking care of special keys and mod_mask
1945 */
1946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001947insert_special(
1948 int c,
1949 int allow_modmask,
1950 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
1952 char_u *p;
1953 int len;
1954
1955 /*
1956 * Special function key, translate into "<Key>". Up to the last '>' is
1957 * inserted with ins_str(), so as not to replace characters in replace
1958 * mode.
1959 * Only use mod_mask for special keys, to avoid things like <S-Space>,
1960 * unless 'allow_modmask' is TRUE.
1961 */
Bram Moolenaard0573012017-10-28 21:11:06 +02001962#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 /* Command-key never produces a normal key */
1964 if (mod_mask & MOD_MASK_CMD)
1965 allow_modmask = TRUE;
1966#endif
1967 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
1968 {
1969 p = get_special_key_name(c, mod_mask);
1970 len = (int)STRLEN(p);
1971 c = p[len - 1];
1972 if (len > 2)
1973 {
1974 if (stop_arrow() == FAIL)
1975 return;
1976 p[len - 1] = NUL;
1977 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00001978 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 ctrlv = FALSE;
1980 }
1981 }
1982 if (stop_arrow() == OK)
1983 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
1984}
1985
1986/*
1987 * Special characters in this context are those that need processing other
1988 * than the simple insertion that can be performed here. This includes ESC
1989 * which terminates the insert, and CR/NL which need special processing to
1990 * open up a new line. This routine tries to optimize insertions performed by
1991 * the "redo", "undo" or "put" commands, so it needs to know when it should
1992 * stop and defer processing to the "normal" mechanism.
1993 * '0' and '^' are special, because they can be followed by CTRL-D.
1994 */
1995#ifdef EBCDIC
1996# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
1997#else
1998# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
1999#endif
2000
Bram Moolenaar13505972019-01-24 15:04:48 +01002001#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002003/*
2004 * "flags": INSCHAR_FORMAT - force formatting
2005 * INSCHAR_CTRLV - char typed just after CTRL-V
2006 * INSCHAR_NO_FEX - don't use 'formatexpr'
2007 *
2008 * NOTE: passes the flags value straight through to internal_format() which,
2009 * beside INSCHAR_FORMAT (above), is also looking for these:
2010 * INSCHAR_DO_COM - format comments
2011 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002014insertchar(
2015 int c, /* character to insert or NUL */
2016 int flags, /* INSCHAR_FORMAT, etc. */
2017 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002022 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002024 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 /*
2028 * Try to break the line in two or more pieces when:
2029 * - Always do this if we have been called to do formatting only.
2030 * - Always do this when 'formatoptions' has the 'a' flag and the line
2031 * ends in white space.
2032 * - Otherwise:
2033 * - Don't do this if inserting a blank
2034 * - Don't do this if an existing character is being replaced, unless
2035 * we're in VREPLACE mode.
2036 * - Do this if the cursor is not on the line where insert started
2037 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2038 * before the insert.
2039 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2040 * before 'textwidth'
2041 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002042 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002043 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002044 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 && *ml_get_cursor() != NUL)
2048 && (curwin->w_cursor.lnum != Insstart.lnum
2049 || ((!has_format_option(FO_INS_LONG)
2050 || Insstart_textlen <= (colnr_T)textwidth)
2051 && (!fo_ins_blank
2052 || Insstart_blank_vcol <= (colnr_T)textwidth
2053 ))))))
2054 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002055 /* Format with 'formatexpr' when it's set. Use internal formatting
2056 * when 'formatexpr' isn't set or it returns non-zero. */
2057#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002058 int do_internal = TRUE;
2059 colnr_T virtcol = get_nolist_virtcol()
2060 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002061
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002062 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2063 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002064 {
2065 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
2066 /* It may be required to save for undo again, e.g. when setline()
2067 * was called. */
2068 ins_need_undo = TRUE;
2069 }
2070 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002072 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002074
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (c == NUL) /* only formatting was wanted */
2076 return;
2077
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002078 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if (did_ai && (int)c == end_comment_pending)
2080 {
2081 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002082 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 int middle_len, end_len;
2084 int i;
2085
2086 /*
2087 * Need to remove existing (middle) comment leader and insert end
2088 * comment leader. First, check what comment leader we can find.
2089 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002090 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002091 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002093 // Skip middle-comment string
2094 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 ++p;
2096 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002097 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002098 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 --middle_len;
2100
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002101 // Find the end-comment string
2102 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 ++p;
2104 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2105
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002106 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002108 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 ;
2110 i++;
2111
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002112 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 i -= middle_len;
2114
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002115 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2117 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002118 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 backspace_until_column(i);
2120
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002121 // Insert the end-comment string, except for the last
2122 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 ins_bytes_len(lead_end, end_len - 1);
2124 }
2125 }
2126 }
2127 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 did_ai = FALSE;
2130#ifdef FEAT_SMARTINDENT
2131 did_si = FALSE;
2132 can_si = FALSE;
2133 can_si_back = FALSE;
2134#endif
2135
2136 /*
2137 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2138 * This speeds up normal text input considerably.
2139 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2140 * need to re-indent at a ':', or any other character (but not what
2141 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002142 * Don't do this when there an InsertCharPre autocommand is defined,
2143 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002144 * Do the check for InsertCharPre before the call to vpeekc() because the
2145 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 */
2147#ifdef USE_ON_FLY_SCROLL
2148 dont_scroll = FALSE; /* allow scrolling here */
2149#endif
2150
2151 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002153 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 && vpeekc() != NUL
2155 && !(State & REPLACE_FLAG)
2156#ifdef FEAT_CINDENT
2157 && !cindent_on()
2158#endif
2159#ifdef FEAT_RIGHTLEFT
2160 && !p_ri
2161#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002162 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164#define INPUT_BUFLEN 100
2165 char_u buf[INPUT_BUFLEN + 1];
2166 int i;
2167 colnr_T virtcol = 0;
2168
2169 buf[0] = c;
2170 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002171 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 virtcol = get_nolist_virtcol();
2173 /*
2174 * Stop the string when:
2175 * - no more chars available
2176 * - finding a special character (command key)
2177 * - buffer is full
2178 * - running into the 'textwidth' boundary
2179 * - need to check for abbreviation: A non-word char after a word-char
2180 */
2181 while ( (c = vpeekc()) != NUL
2182 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 && i < INPUT_BUFLEN
2185 && (textwidth == 0
2186 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2187 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2188 {
2189#ifdef FEAT_RIGHTLEFT
2190 c = vgetc();
2191 if (p_hkmap && KeyTyped)
2192 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 buf[i++] = c;
2194#else
2195 buf[i++] = vgetc();
2196#endif
2197 }
2198
2199#ifdef FEAT_DIGRAPHS
2200 do_digraph(-1); /* clear digraphs */
2201 do_digraph(buf[i-1]); /* may be the start of a digraph */
2202#endif
2203 buf[i] = NUL;
2204 ins_str(buf);
2205 if (flags & INSCHAR_CTRLV)
2206 {
2207 redo_literal(*buf);
2208 i = 1;
2209 }
2210 else
2211 i = 0;
2212 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002213 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 }
2215 else
2216 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002217 int cc;
2218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2220 {
2221 char_u buf[MB_MAXBYTES + 1];
2222
2223 (*mb_char2bytes)(c, buf);
2224 buf[cc] = NUL;
2225 ins_char_bytes(buf, cc);
2226 AppendCharToRedobuff(c);
2227 }
2228 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 ins_char(c);
2231 if (flags & INSCHAR_CTRLV)
2232 redo_literal(c);
2233 else
2234 AppendCharToRedobuff(c);
2235 }
2236 }
2237}
2238
2239/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002240 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002241 *
2242 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2243 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002244 */
2245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002246internal_format(
2247 int textwidth,
2248 int second_indent,
2249 int flags,
2250 int format_only,
2251 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002252{
2253 int cc;
2254 int save_char = NUL;
2255 int haveto_redraw = FALSE;
2256 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002257 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002258 int fo_white_par = has_format_option(FO_WHITE_PAR);
2259 int first_line = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002260 colnr_T leader_len;
2261 int no_leader = FALSE;
2262 int do_comments = (flags & INSCHAR_DO_COM);
Bram Moolenaar0026d472014-09-09 16:32:39 +02002263#ifdef FEAT_LINEBREAK
2264 int has_lbr = curwin->w_p_lbr;
2265
2266 /* make sure win_lbr_chartabsize() counts correctly */
2267 curwin->w_p_lbr = FALSE;
2268#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002269
2270 /*
2271 * When 'ai' is off we don't want a space under the cursor to be
2272 * deleted. Replace it with an 'x' temporarily.
2273 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002274 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002275 {
2276 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002277 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002278 {
2279 save_char = cc;
2280 pchar_cursor('x');
2281 }
2282 }
2283
2284 /*
2285 * Repeat breaking lines, until the current line is not too long.
2286 */
2287 while (!got_int)
2288 {
2289 int startcol; /* Cursor column at entry */
2290 int wantcol; /* column at textwidth border */
2291 int foundcol; /* column for start of spaces */
2292 int end_foundcol = 0; /* column for start of word */
2293 colnr_T len;
2294 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002295 int orig_col = 0;
2296 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002297 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002298 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002299 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002300
Bram Moolenaar97b98102009-11-17 16:41:01 +00002301 virtcol = get_nolist_virtcol()
2302 + char2cells(c != NUL ? c : gchar_cursor());
2303 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002304 break;
2305
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002306 if (no_leader)
2307 do_comments = FALSE;
2308 else if (!(flags & INSCHAR_FORMAT)
2309 && has_format_option(FO_WRAP_COMS))
2310 do_comments = TRUE;
2311
2312 /* Don't break until after the comment leader */
2313 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002314 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002315 else
2316 leader_len = 0;
2317
2318 /* If the line doesn't start with a comment leader, then don't
2319 * start one in a following broken line. Avoids that a %word
2320 * moved to the start of the next line causes all following lines
2321 * to start with %. */
2322 if (leader_len == 0)
2323 no_leader = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002324 if (!(flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002325 && leader_len == 0
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002326 && !has_format_option(FO_WRAP))
2327
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002328 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002329 if ((startcol = curwin->w_cursor.col) == 0)
2330 break;
2331
2332 /* find column of textwidth border */
2333 coladvance((colnr_T)textwidth);
2334 wantcol = curwin->w_cursor.col;
2335
Bram Moolenaar97b98102009-11-17 16:41:01 +00002336 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002337 foundcol = 0;
2338
2339 /*
2340 * Find position to break at.
2341 * Stop at first entered white when 'formatoptions' has 'v'
2342 */
2343 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002344 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002345 || curwin->w_cursor.lnum != Insstart.lnum
2346 || curwin->w_cursor.col >= Insstart.col)
2347 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002348 if (curwin->w_cursor.col == startcol && c != NUL)
2349 cc = c;
2350 else
2351 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002352 if (WHITECHAR(cc))
2353 {
2354 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002355 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002356
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002357 // find start of sequence of blanks
2358 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002359 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2360 {
2361 dec_cursor();
2362 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002363
2364 // Increment count of how many whitespace chars in this
2365 // group; we only need to know if it's more than one.
2366 if (wcc < 2)
2367 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002368 }
2369 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
2370 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002371
2372 // Don't break after a period when 'formatoptions' has 'p' and
2373 // there are less than two spaces.
2374 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2375 continue;
2376
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002377 /* Don't break until after the comment leader */
2378 if (curwin->w_cursor.col < leader_len)
2379 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002380 if (has_format_option(FO_ONE_LETTER))
2381 {
2382 /* do not break after one-letter words */
2383 if (curwin->w_cursor.col == 0)
2384 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002385 /* do not break "#a b" when 'tw' is 2 */
2386 if (curwin->w_cursor.col <= leader_len)
2387 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002388 col = curwin->w_cursor.col;
2389 dec_cursor();
2390 cc = gchar_cursor();
2391
2392 if (WHITECHAR(cc))
2393 continue; /* one-letter, continue */
2394 curwin->w_cursor.col = col;
2395 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002396
2397 inc_cursor();
2398
2399 end_foundcol = end_col + 1;
2400 foundcol = curwin->w_cursor.col;
2401 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002402 break;
2403 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002404 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002405 {
2406 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002407 if (curwin->w_cursor.col != startcol)
2408 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002409 /* Don't break until after the comment leader */
2410 if (curwin->w_cursor.col < leader_len)
2411 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002412 col = curwin->w_cursor.col;
2413 inc_cursor();
2414 /* Don't change end_foundcol if already set. */
2415 if (foundcol != curwin->w_cursor.col)
2416 {
2417 foundcol = curwin->w_cursor.col;
2418 end_foundcol = foundcol;
2419 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2420 break;
2421 }
2422 curwin->w_cursor.col = col;
2423 }
2424
2425 if (curwin->w_cursor.col == 0)
2426 break;
2427
2428 col = curwin->w_cursor.col;
2429
2430 dec_cursor();
2431 cc = gchar_cursor();
2432
2433 if (WHITECHAR(cc))
2434 continue; /* break with space */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002435 /* Don't break until after the comment leader */
2436 if (curwin->w_cursor.col < leader_len)
2437 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002438
2439 curwin->w_cursor.col = col;
2440
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002441 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002442 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002443 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2444 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002445 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002446 if (curwin->w_cursor.col == 0)
2447 break;
2448 dec_cursor();
2449 }
2450
2451 if (foundcol == 0) /* no spaces, cannot break line */
2452 {
2453 curwin->w_cursor.col = startcol;
2454 break;
2455 }
2456
2457 /* Going to break the line, remove any "$" now. */
2458 undisplay_dollar();
2459
2460 /*
2461 * Offset between cursor position and line break is used by replace
2462 * stack functions. VREPLACE does not use this, and backspaces
2463 * over the text instead.
2464 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002465 if (State & VREPLACE_FLAG)
2466 orig_col = startcol; /* Will start backspacing from here */
2467 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002468 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002469
2470 /*
2471 * adjust startcol for spaces that will be deleted and
2472 * characters that will remain on top line
2473 */
2474 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002475 while ((cc = gchar_cursor(), WHITECHAR(cc))
2476 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002477 inc_cursor();
2478 startcol -= curwin->w_cursor.col;
2479 if (startcol < 0)
2480 startcol = 0;
2481
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002482 if (State & VREPLACE_FLAG)
2483 {
2484 /*
2485 * In VREPLACE mode, we will backspace over the text to be
2486 * wrapped, so save a copy now to put on the next line.
2487 */
2488 saved_text = vim_strsave(ml_get_cursor());
2489 curwin->w_cursor.col = orig_col;
2490 if (saved_text == NULL)
2491 break; /* Can't do it, out of memory */
2492 saved_text[startcol] = NUL;
2493
2494 /* Backspace over characters that will move to the next line */
2495 if (!fo_white_par)
2496 backspace_until_column(foundcol);
2497 }
2498 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002499 {
2500 /* put cursor after pos. to break line */
2501 if (!fo_white_par)
2502 curwin->w_cursor.col = foundcol;
2503 }
2504
2505 /*
2506 * Split the line just before the margin.
2507 * Only insert/delete lines, but don't really redraw the window.
2508 */
2509 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2510 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002511 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002512 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002513 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2514 if (!(flags & INSCHAR_COM_LIST))
2515 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002516
2517 replace_offset = 0;
2518 if (first_line)
2519 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002520 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002521 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002522 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002523 * This section is for auto-wrap of numeric lists. When not
2524 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2525 * flag will be set and open_line() will handle it (as seen
2526 * above). The code here (and in get_number_indent()) will
2527 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002528 */
2529 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002530 second_indent =
2531 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002532 if (second_indent >= 0)
2533 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002534 if (State & VREPLACE_FLAG)
2535 change_indent(INDENT_SET, second_indent,
2536 FALSE, NUL, TRUE);
2537 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002538 if (leader_len > 0 && second_indent - leader_len > 0)
2539 {
2540 int i;
2541 int padding = second_indent - leader_len;
2542
2543 /* We started at the first_line of a numbered list
2544 * that has a comment. the open_line() function has
2545 * inserted the proper comment leader and positioned
2546 * the cursor at the end of the split line. Now we
2547 * add the additional whitespace needed after the
2548 * comment leader for the numbered list. */
2549 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002550 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002551 }
2552 else
2553 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002554 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002555 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002556 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002557 }
2558 first_line = FALSE;
2559 }
2560
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002561 if (State & VREPLACE_FLAG)
2562 {
2563 /*
2564 * In VREPLACE mode we have backspaced over the text to be
2565 * moved, now we re-insert it into the new line.
2566 */
2567 ins_bytes(saved_text);
2568 vim_free(saved_text);
2569 }
2570 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002571 {
2572 /*
2573 * Check if cursor is not past the NUL off the line, cindent
2574 * may have added or removed indent.
2575 */
2576 curwin->w_cursor.col += startcol;
2577 len = (colnr_T)STRLEN(ml_get_curline());
2578 if (curwin->w_cursor.col > len)
2579 curwin->w_cursor.col = len;
2580 }
2581
2582 haveto_redraw = TRUE;
2583#ifdef FEAT_CINDENT
2584 can_cindent = TRUE;
2585#endif
2586 /* moved the cursor, don't autoindent or cindent now */
2587 did_ai = FALSE;
2588#ifdef FEAT_SMARTINDENT
2589 did_si = FALSE;
2590 can_si = FALSE;
2591 can_si_back = FALSE;
2592#endif
2593 line_breakcheck();
2594 }
2595
2596 if (save_char != NUL) /* put back space after cursor */
2597 pchar_cursor(save_char);
2598
Bram Moolenaar0026d472014-09-09 16:32:39 +02002599#ifdef FEAT_LINEBREAK
2600 curwin->w_p_lbr = has_lbr;
2601#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002602 if (!format_only && haveto_redraw)
2603 {
2604 update_topline();
2605 redraw_curbuf_later(VALID);
2606 }
2607}
2608
2609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 * Called after inserting or deleting text: When 'formatoptions' includes the
2611 * 'a' flag format from the current line until the end of the paragraph.
2612 * Keep the cursor at the same position relative to the text.
2613 * The caller must have saved the cursor line for undo, following ones will be
2614 * saved here.
2615 */
2616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002617auto_format(
2618 int trailblank, /* when TRUE also format with trailing blank */
2619 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
2621 pos_T pos;
2622 colnr_T len;
2623 char_u *old;
2624 char_u *new, *pnew;
2625 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002626 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
2628 if (!has_format_option(FO_AUTO))
2629 return;
2630
2631 pos = curwin->w_cursor;
2632 old = ml_get_curline();
2633
2634 /* may remove added space */
2635 check_auto_format(FALSE);
2636
2637 /* Don't format in Insert mode when the cursor is on a trailing blank, the
2638 * user might insert normal text next. Also skip formatting when "1" is
2639 * in 'formatoptions' and there is a single character before the cursor.
2640 * Otherwise the line would be broken and when typing another non-white
2641 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002642 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (*old != NUL && !trailblank && wasatend)
2644 {
2645 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002646 cc = gchar_cursor();
2647 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2648 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002650 cc = gchar_cursor();
2651 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 curwin->w_cursor = pos;
2654 return;
2655 }
2656 curwin->w_cursor = pos;
2657 }
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
2660 * comments. */
2661 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002662 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 /*
2666 * May start formatting in a previous line, so that after "x" a word is
2667 * moved to the previous line if it fits there now. Only when this is not
2668 * the start of a paragraph.
2669 */
2670 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2671 {
2672 --curwin->w_cursor.lnum;
2673 if (u_save_cursor() == FAIL)
2674 return;
2675 }
2676
2677 /*
2678 * Do the formatting and restore the cursor position. "saved_cursor" will
2679 * be adjusted for the text formatting.
2680 */
2681 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002682 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 curwin->w_cursor = saved_cursor;
2684 saved_cursor.lnum = 0;
2685
2686 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2687 {
2688 /* "cannot happen" */
2689 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2690 coladvance((colnr_T)MAXCOL);
2691 }
2692 else
2693 check_cursor_col();
2694
2695 /* Insert mode: If the cursor is now after the end of the line while it
2696 * previously wasn't, the line was broken. Because of the rule above we
2697 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2698 * formatted. */
2699 if (!wasatend && has_format_option(FO_WHITE_PAR))
2700 {
2701 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002702 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 if (curwin->w_cursor.col == len)
2704 {
2705 pnew = vim_strnsave(new, len + 2);
2706 pnew[len] = ' ';
2707 pnew[len + 1] = NUL;
2708 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
2709 /* remove the space later */
2710 did_add_space = TRUE;
2711 }
2712 else
2713 /* may remove added space */
2714 check_auto_format(FALSE);
2715 }
2716
2717 check_cursor();
2718}
2719
2720/*
2721 * When an extra space was added to continue a paragraph for auto-formatting,
2722 * delete it now. The space must be under the cursor, just after the insert
2723 * position.
2724 */
2725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002726check_auto_format(
2727 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002730 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 if (did_add_space)
2733 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002734 cc = gchar_cursor();
2735 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 /* Somehow the space was removed already. */
2737 did_add_space = FALSE;
2738 else
2739 {
2740 if (!end_insert)
2741 {
2742 inc_cursor();
2743 c = gchar_cursor();
2744 dec_cursor();
2745 }
2746 if (c != NUL)
2747 {
2748 /* The space is no longer at the end of the line, delete it. */
2749 del_char(FALSE);
2750 did_add_space = FALSE;
2751 }
2752 }
2753 }
2754}
2755
2756/*
2757 * Find out textwidth to be used for formatting:
2758 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02002759 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 * if invalid value, use 0.
2761 * Set default to window width (maximum 79) for "gq" operator.
2762 */
2763 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002764comp_textwidth(
2765 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 int textwidth;
2768
2769 textwidth = curbuf->b_p_tw;
2770 if (textwidth == 0 && curbuf->b_p_wm)
2771 {
2772 /* The width is the window width minus 'wrapmargin' minus all the
2773 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02002774 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775#ifdef FEAT_CMDWIN
2776 if (cmdwin_type != 0)
2777 textwidth -= 1;
2778#endif
2779#ifdef FEAT_FOLDING
2780 textwidth -= curwin->w_p_fdc;
2781#endif
2782#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002783 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 textwidth -= 1;
2785#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02002786 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 textwidth -= 8;
2788 }
2789 if (textwidth < 0)
2790 textwidth = 0;
2791 if (ff && textwidth == 0)
2792 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002793 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 if (textwidth > 79)
2795 textwidth = 79;
2796 }
2797 return textwidth;
2798}
2799
2800/*
2801 * Put a character in the redo buffer, for when just after a CTRL-V.
2802 */
2803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002804redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
2806 char_u buf[10];
2807
2808 /* Only digits need special treatment. Translate them into a string of
2809 * three digits. */
2810 if (VIM_ISDIGIT(c))
2811 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002812 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 AppendToRedobuff(buf);
2814 }
2815 else
2816 AppendCharToRedobuff(c);
2817}
2818
2819/*
2820 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002821 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002824start_arrow(
2825 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002827 start_arrow_common(end_insert_pos, TRUE);
2828}
2829
2830/*
2831 * Like start_arrow() but with end_change argument.
2832 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2833 */
2834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002835start_arrow_with_change(
2836 pos_T *end_insert_pos, /* can be NULL */
2837 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002838{
2839 start_arrow_common(end_insert_pos, end_change);
2840 if (!end_change)
2841 {
2842 AppendCharToRedobuff(Ctrl_G);
2843 AppendCharToRedobuff('U');
2844 }
2845}
2846
2847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002848start_arrow_common(
2849 pos_T *end_insert_pos, /* can be NULL */
2850 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002851{
2852 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
2854 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002855 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 arrow_used = TRUE; /* this means we stopped the current insert */
2857 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002858#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002859 check_spell_redraw();
2860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861}
2862
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002863#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002864/*
2865 * If we skipped highlighting word at cursor, do it now.
2866 * It may be skipped again, thus reset spell_redraw_lnum first.
2867 */
2868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002869check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002870{
2871 if (spell_redraw_lnum != 0)
2872 {
2873 linenr_T lnum = spell_redraw_lnum;
2874
2875 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002876 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002877 }
2878}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002879
Bram Moolenaar217ad922005-03-20 22:37:15 +00002880#endif
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882/*
2883 * stop_arrow() is called before a change is made in insert mode.
2884 * If an arrow key has been used, start a new insertion.
2885 * Returns FAIL if undo is impossible, shouldn't insert then.
2886 */
2887 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002888stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
2890 if (arrow_used)
2891 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002892 Insstart = curwin->w_cursor; /* new insertion starts here */
2893 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
2894 /* Don't update the original insert position when moved to the
2895 * right, except when nothing was inserted yet. */
2896 update_Insstart_orig = FALSE;
2897 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 if (u_save_cursor() == OK)
2900 {
2901 arrow_used = FALSE;
2902 ins_need_undo = FALSE;
2903 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002904
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 if (State & VREPLACE_FLAG)
2907 {
2908 orig_line_count = curbuf->b_ml.ml_line_count;
2909 vr_lines_changed = 1;
2910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 ResetRedobuff();
2912 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002913 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 else if (ins_need_undo)
2916 {
2917 if (u_save_cursor() == OK)
2918 ins_need_undo = FALSE;
2919 }
2920
2921#ifdef FEAT_FOLDING
2922 /* Always open fold at the cursor line when inserting something. */
2923 foldOpenCursor();
2924#endif
2925
2926 return (arrow_used || ins_need_undo ? FAIL : OK);
2927}
2928
2929/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002930 * Do a few things to stop inserting.
2931 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2932 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 */
2934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935stop_insert(
2936 pos_T *end_insert_pos,
2937 int esc, /* called by ins_esc() */
2938 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002940 int cc;
2941 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
2943 stop_redo_ins();
2944 replace_flush(); /* abandon replace stack */
2945
2946 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002947 * Save the inserted text for later redo with ^@ and CTRL-A.
2948 * Don't do it when "restart_edit" was set and nothing was inserted,
2949 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002951 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002952 if (did_restart_edit == 0 || (ptr != NULL
2953 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002954 {
2955 vim_free(last_insert);
2956 last_insert = ptr;
2957 last_insert_skip = new_insert_skip;
2958 }
2959 else
2960 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002962 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 {
2964 /* Auto-format now. It may seem strange to do this when stopping an
2965 * insertion (or moving the cursor), but it's required when appending
2966 * a line and having it end in a space. But only do it when something
2967 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002968 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002970 pos_T tpos = curwin->w_cursor;
2971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 /* When the cursor is at the end of the line after a space the
2973 * formatting will move it to the following word. Avoid that by
2974 * moving the cursor onto the space. */
2975 cc = 'x';
2976 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
2977 {
2978 dec_cursor();
2979 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002980 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002981 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983
2984 auto_format(TRUE, FALSE);
2985
Bram Moolenaar1c465442017-03-12 20:10:05 +01002986 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002987 {
2988 if (gchar_cursor() != NUL)
2989 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002990 // If the cursor is still at the same character, also keep
2991 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002992 if (gchar_cursor() == NUL
2993 && curwin->w_cursor.lnum == tpos.lnum
2994 && curwin->w_cursor.col == tpos.col)
2995 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998
2999 /* If a space was inserted for auto-formatting, remove it now. */
3000 check_auto_format(TRUE);
3001
3002 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003003 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00003004 * Do this when ESC was used or moving the cursor up/down.
3005 * Check for the old position still being valid, just in case the text
3006 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01003007 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003008 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3009 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003011 pos_T tpos = curwin->w_cursor;
3012
3013 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003014 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003015 for (;;)
3016 {
3017 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3018 --curwin->w_cursor.col;
3019 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003020 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003021 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003022 if (del_char(TRUE) == FAIL)
3023 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003024 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003025 if (curwin->w_cursor.lnum != tpos.lnum)
3026 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003027 else
3028 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003029 /* reset tpos, could have been invalidated in the loop above */
3030 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003031 tpos.col++;
3032 if (cc != NUL && gchar_pos(&tpos) == NUL)
3033 ++curwin->w_cursor.col; /* put cursor back on the NUL */
3034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 /* <C-S-Right> may have started Visual mode, adjust the position for
3037 * deleted characters. */
3038 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3039 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003040 int len = (int)STRLEN(ml_get_curline());
3041
3042 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003044 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
3049 }
3050 did_ai = FALSE;
3051#ifdef FEAT_SMARTINDENT
3052 did_si = FALSE;
3053 can_si = FALSE;
3054 can_si_back = FALSE;
3055#endif
3056
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003057 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3058 * now in a different buffer. */
3059 if (end_insert_pos != NULL)
3060 {
3061 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003062 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003063 curbuf->b_op_end = *end_insert_pos;
3064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065}
3066
3067/*
3068 * Set the last inserted text to a single character.
3069 * Used for the replace command.
3070 */
3071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003072set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 char_u *s;
3075
3076 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 if (last_insert != NULL)
3079 {
3080 s = last_insert;
3081 /* Use the CTRL-V only when entering a special char */
3082 if (c < ' ' || c == DEL)
3083 *s++ = Ctrl_V;
3084 s = add_char2buf(c, s);
3085 *s++ = ESC;
3086 *s++ = NUL;
3087 last_insert_skip = 0;
3088 }
3089}
3090
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003091#if defined(EXITFREE) || defined(PROTO)
3092 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003093free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003094{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003095 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003096}
3097#endif
3098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099/*
3100 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3101 * and CSI. Handle multi-byte characters.
3102 * Returns a pointer to after the added bytes.
3103 */
3104 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003105add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003107 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 int i;
3109 int len;
3110
3111 len = (*mb_char2bytes)(c, temp);
3112 for (i = 0; i < len; ++i)
3113 {
3114 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
3116 if (c == K_SPECIAL)
3117 {
3118 *s++ = K_SPECIAL;
3119 *s++ = KS_SPECIAL;
3120 *s++ = KE_FILLER;
3121 }
3122#ifdef FEAT_GUI
3123 else if (c == CSI)
3124 {
3125 *s++ = CSI;
3126 *s++ = KS_EXTRA;
3127 *s++ = (int)KE_CSI;
3128 }
3129#endif
3130 else
3131 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 return s;
3134}
3135
3136/*
3137 * move cursor to start of line
3138 * if flags & BL_WHITE move to first non-white
3139 * if flags & BL_SOL move to first non-white if startofline is set,
3140 * otherwise keep "curswant" column
3141 * if flags & BL_FIX don't leave the cursor on a NUL.
3142 */
3143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003144beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145{
3146 if ((flags & BL_SOL) && !p_sol)
3147 coladvance(curwin->w_curswant);
3148 else
3149 {
3150 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 if (flags & (BL_WHITE | BL_SOL))
3154 {
3155 char_u *ptr;
3156
Bram Moolenaar1c465442017-03-12 20:10:05 +01003157 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3159 ++curwin->w_cursor.col;
3160 }
3161 curwin->w_set_curswant = TRUE;
3162 }
3163}
3164
3165/*
3166 * oneright oneleft cursor_down cursor_up
3167 *
3168 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003169 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 * Return OK when successful, FAIL when we hit a line of file boundary.
3171 */
3172
3173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003174oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175{
3176 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 if (virtual_active())
3180 {
3181 pos_T prevpos = curwin->w_cursor;
3182
3183 /* Adjust for multi-wide char (excluding TAB) */
3184 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003185 coladvance(getviscol() + ((*ptr != TAB
3186 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 ? ptr2cells(ptr) : 1));
3188 curwin->w_set_curswant = TRUE;
3189 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
3190 return (prevpos.col != curwin->w_cursor.col
3191 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003195 if (*ptr == NUL)
3196 return FAIL; /* already at the very end */
3197
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003198 if (has_mbyte)
3199 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003201 l = 1;
3202
3203 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3204 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003205 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003206 return FAIL;
3207 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 curwin->w_set_curswant = TRUE;
3210 return OK;
3211}
3212
3213 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (virtual_active())
3217 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003218#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 int v = getviscol();
3222
3223 if (v == 0)
3224 return FAIL;
3225
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003226#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 /* We might get stuck on 'showbreak', skip over it. */
3228 width = 1;
3229 for (;;)
3230 {
3231 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02003232 /* getviscol() is slow, skip it when 'showbreak' is empty,
3233 * 'breakindent' is not set and there are no multi-byte
3234 * characters */
3235 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003236 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 break;
3238 ++width;
3239 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003240#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 if (curwin->w_cursor.coladd == 1)
3245 {
3246 char_u *ptr;
3247
3248 /* Adjust for multi-wide char (not a TAB) */
3249 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003250 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3251 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 curwin->w_cursor.coladd = 0;
3253 }
3254
3255 curwin->w_set_curswant = TRUE;
3256 return OK;
3257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259 if (curwin->w_cursor.col == 0)
3260 return FAIL;
3261
3262 curwin->w_set_curswant = TRUE;
3263 --curwin->w_cursor.col;
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 /* if the character on the left of the current cursor is a multi-byte
3266 * character, move to its first byte */
3267 if (has_mbyte)
3268 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 return OK;
3270}
3271
3272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003273cursor_up(
3274 long n,
3275 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 linenr_T lnum;
3278
3279 if (n > 0)
3280 {
3281 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003282 /* This fails if the cursor is already in the first line or the count
3283 * is larger than the line number and '-' is in 'cpoptions' */
3284 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 return FAIL;
3286 if (n >= lnum)
3287 lnum = 1;
3288 else
3289#ifdef FEAT_FOLDING
3290 if (hasAnyFolding(curwin))
3291 {
3292 /*
3293 * Count each sequence of folded lines as one logical line.
3294 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003295 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 (void)hasFolding(lnum, &lnum, NULL);
3297
3298 while (n--)
3299 {
3300 /* move up one line */
3301 --lnum;
3302 if (lnum <= 1)
3303 break;
3304 /* If we entered a fold, move to the beginning, unless in
3305 * Insert mode or when 'foldopen' contains "all": it will open
3306 * in a moment. */
3307 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3308 (void)hasFolding(lnum, &lnum, NULL);
3309 }
3310 if (lnum < 1)
3311 lnum = 1;
3312 }
3313 else
3314#endif
3315 lnum -= n;
3316 curwin->w_cursor.lnum = lnum;
3317 }
3318
3319 /* try to advance to the column we want to be at */
3320 coladvance(curwin->w_curswant);
3321
3322 if (upd_topline)
3323 update_topline(); /* make sure curwin->w_topline is valid */
3324
3325 return OK;
3326}
3327
3328/*
3329 * Cursor down a number of logical lines.
3330 */
3331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003332cursor_down(
3333 long n,
3334 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 linenr_T lnum;
3337
3338 if (n > 0)
3339 {
3340 lnum = curwin->w_cursor.lnum;
3341#ifdef FEAT_FOLDING
3342 /* Move to last line of fold, will fail if it's the end-of-file. */
3343 (void)hasFolding(lnum, NULL, &lnum);
3344#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003345 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003346 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003347 if (lnum >= curbuf->b_ml.ml_line_count
3348 || (lnum + n > curbuf->b_ml.ml_line_count
3349 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 return FAIL;
3351 if (lnum + n >= curbuf->b_ml.ml_line_count)
3352 lnum = curbuf->b_ml.ml_line_count;
3353 else
3354#ifdef FEAT_FOLDING
3355 if (hasAnyFolding(curwin))
3356 {
3357 linenr_T last;
3358
3359 /* count each sequence of folded lines as one logical line */
3360 while (n--)
3361 {
3362 if (hasFolding(lnum, NULL, &last))
3363 lnum = last + 1;
3364 else
3365 ++lnum;
3366 if (lnum >= curbuf->b_ml.ml_line_count)
3367 break;
3368 }
3369 if (lnum > curbuf->b_ml.ml_line_count)
3370 lnum = curbuf->b_ml.ml_line_count;
3371 }
3372 else
3373#endif
3374 lnum += n;
3375 curwin->w_cursor.lnum = lnum;
3376 }
3377
3378 /* try to advance to the column we want to be at */
3379 coladvance(curwin->w_curswant);
3380
3381 if (upd_topline)
3382 update_topline(); /* make sure curwin->w_topline is valid */
3383
3384 return OK;
3385}
3386
3387/*
3388 * Stuff the last inserted text in the read buffer.
3389 * Last_insert actually is a copy of the redo buffer, so we
3390 * first have to remove the command.
3391 */
3392 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003393stuff_inserted(
3394 int c, /* Command character to be inserted */
3395 long count, /* Repeat this many times */
3396 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
3398 char_u *esc_ptr;
3399 char_u *ptr;
3400 char_u *last_ptr;
3401 char_u last = NUL;
3402
3403 ptr = get_last_insert();
3404 if (ptr == NULL)
3405 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003406 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 return FAIL;
3408 }
3409
3410 /* may want to stuff the command character, to start Insert mode */
3411 if (c != NUL)
3412 stuffcharReadbuff(c);
3413 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
3414 *esc_ptr = NUL; /* remove the ESC */
3415
3416 /* when the last char is either "0" or "^" it will be quoted if no ESC
3417 * comes after it OR if it will inserted more than once and "ptr"
3418 * starts with ^D. -- Acevedo
3419 */
3420 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3421 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3422 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3423 {
3424 last = *last_ptr;
3425 *last_ptr = NUL;
3426 }
3427
3428 do
3429 {
3430 stuffReadbuff(ptr);
3431 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
3432 if (last)
3433 stuffReadbuff((char_u *)(last == '0'
3434 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3435 : IF_EB("\026^", CTRL_V_STR "^")));
3436 }
3437 while (--count > 0);
3438
3439 if (last)
3440 *last_ptr = last;
3441
3442 if (esc_ptr != NULL)
3443 *esc_ptr = ESC; /* put the ESC back */
3444
3445 /* may want to stuff a trailing ESC, to get out of Insert mode */
3446 if (!no_esc)
3447 stuffcharReadbuff(ESC);
3448
3449 return OK;
3450}
3451
3452 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003453get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 if (last_insert == NULL)
3456 return NULL;
3457 return last_insert + last_insert_skip;
3458}
3459
3460/*
3461 * Get last inserted string, and remove trailing <Esc>.
3462 * Returns pointer to allocated memory (must be freed) or NULL.
3463 */
3464 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003465get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 char_u *s;
3468 int len;
3469
3470 if (last_insert == NULL)
3471 return NULL;
3472 s = vim_strsave(last_insert + last_insert_skip);
3473 if (s != NULL)
3474 {
3475 len = (int)STRLEN(s);
3476 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
3477 s[len - 1] = NUL;
3478 }
3479 return s;
3480}
3481
3482/*
3483 * Check the word in front of the cursor for an abbreviation.
3484 * Called when the non-id character "c" has been entered.
3485 * When an abbreviation is recognized it is removed from the text and
3486 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3487 */
3488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003489echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
3491 /* Don't check for abbreviation in paste mode, when disabled and just
3492 * after moving around with cursor keys. */
3493 if (p_paste || no_abbr || arrow_used)
3494 return FALSE;
3495
3496 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3497 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3498}
3499
3500/*
3501 * replace-stack functions
3502 *
3503 * When replacing characters, the replaced characters are remembered for each
3504 * new character. This is used to re-insert the old text when backspacing.
3505 *
3506 * There is a NUL headed list of characters for each character that is
3507 * currently in the file after the insertion point. When BS is used, one NUL
3508 * headed list is put back for the deleted character.
3509 *
3510 * For a newline, there are two NUL headed lists. One contains the characters
3511 * that the NL replaced. The extra one stores the characters after the cursor
3512 * that were deleted (always white space).
3513 *
3514 * Replace_offset is normally 0, in which case replace_push will add a new
3515 * character at the end of the stack. If replace_offset is not 0, that many
3516 * characters will be left on the stack above the newly inserted character.
3517 */
3518
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003519static char_u *replace_stack = NULL;
3520static long replace_stack_nr = 0; /* next entry in replace stack */
3521static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003524replace_push(
3525 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 char_u *p;
3528
3529 if (replace_stack_nr < replace_offset) /* nothing to do */
3530 return;
3531 if (replace_stack_len <= replace_stack_nr)
3532 {
3533 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003534 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (p == NULL) /* out of memory */
3536 {
3537 replace_stack_len -= 50;
3538 return;
3539 }
3540 if (replace_stack != NULL)
3541 {
3542 mch_memmove(p, replace_stack,
3543 (size_t)(replace_stack_nr * sizeof(char_u)));
3544 vim_free(replace_stack);
3545 }
3546 replace_stack = p;
3547 }
3548 p = replace_stack + replace_stack_nr - replace_offset;
3549 if (replace_offset)
3550 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3551 *p = c;
3552 ++replace_stack_nr;
3553}
3554
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003555/*
3556 * Push a character onto the replace stack. Handles a multi-byte character in
3557 * reverse byte order, so that the first byte is popped off first.
3558 * Return the number of bytes done (includes composing characters).
3559 */
3560 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003561replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003562{
3563 int l = (*mb_ptr2len)(p);
3564 int j;
3565
3566 for (j = l - 1; j >= 0; --j)
3567 replace_push(p[j]);
3568 return l;
3569}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571/*
3572 * Pop one item from the replace stack.
3573 * return -1 if stack empty
3574 * return replaced character or NUL otherwise
3575 */
3576 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 if (replace_stack_nr == 0)
3580 return -1;
3581 return (int)replace_stack[--replace_stack_nr];
3582}
3583
3584/*
3585 * Join the top two items on the replace stack. This removes to "off"'th NUL
3586 * encountered.
3587 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003589replace_join(
3590 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
3592 int i;
3593
3594 for (i = replace_stack_nr; --i >= 0; )
3595 if (replace_stack[i] == NUL && off-- <= 0)
3596 {
3597 --replace_stack_nr;
3598 mch_memmove(replace_stack + i, replace_stack + i + 1,
3599 (size_t)(replace_stack_nr - i));
3600 return;
3601 }
3602}
3603
3604/*
3605 * Pop bytes from the replace stack until a NUL is found, and insert them
3606 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3607 */
3608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003609replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 int cc;
3612 int oldState = State;
3613
3614 State = NORMAL; /* don't want REPLACE here */
3615 while ((cc = replace_pop()) > 0)
3616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 dec_cursor();
3619 }
3620 State = oldState;
3621}
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623/*
3624 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3625 * indicates a multi-byte char, pop the other bytes too.
3626 */
3627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003628mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629{
3630 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003631 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 int i;
3633 int c;
3634
3635 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3636 {
3637 buf[0] = cc;
3638 for (i = 1; i < n; ++i)
3639 buf[i] = replace_pop();
3640 ins_bytes_len(buf, n);
3641 }
3642 else
3643 ins_char(cc);
3644
3645 if (enc_utf8)
3646 /* Handle composing chars. */
3647 for (;;)
3648 {
3649 c = replace_pop();
3650 if (c == -1) /* stack empty */
3651 break;
3652 if ((n = MB_BYTE2LEN(c)) == 1)
3653 {
3654 /* Not a multi-byte char, put it back. */
3655 replace_push(c);
3656 break;
3657 }
3658 else
3659 {
3660 buf[0] = c;
3661 for (i = 1; i < n; ++i)
3662 buf[i] = replace_pop();
3663 if (utf_iscomposing(utf_ptr2char(buf)))
3664 ins_bytes_len(buf, n);
3665 else
3666 {
3667 /* Not a composing char, put it back. */
3668 for (i = n - 1; i >= 0; --i)
3669 replace_push(buf[i]);
3670 break;
3671 }
3672 }
3673 }
3674}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
3676/*
3677 * make the replace stack empty
3678 * (called when exiting replace mode)
3679 */
3680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003681replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003683 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 replace_stack_len = 0;
3685 replace_stack_nr = 0;
3686}
3687
3688/*
3689 * Handle doing a BS for one character.
3690 * cc < 0: replace stack empty, just move cursor
3691 * cc == 0: character was inserted, delete it
3692 * cc > 0: character was replaced, put cc (first byte of original char) back
3693 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003694 * When "limit_col" is >= 0, don't delete before this column. Matters when
3695 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 */
3697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003698replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
3700 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 int orig_len = 0;
3702 int ins_len;
3703 int orig_vcols = 0;
3704 colnr_T start_vcol;
3705 char_u *p;
3706 int i;
3707 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 cc = replace_pop();
3710 if (cc > 0)
3711 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01003712#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003713 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003714
3715 if (curbuf->b_has_textprop)
3716 {
3717 // Do not adjust text properties for individual delete and insert
3718 // operations, do it afterwards on the resulting text.
3719 len_before = STRLEN(ml_get_curline());
3720 ++text_prop_frozen;
3721 }
3722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (State & VREPLACE_FLAG)
3724 {
3725 /* Get the number of screen cells used by the character we are
3726 * going to delete. */
3727 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3728 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (has_mbyte)
3731 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003732 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003734 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 replace_push(cc);
3736 }
3737 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
3739 pchar_cursor(cc);
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()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743 replace_pop_ins();
3744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 if (State & VREPLACE_FLAG)
3746 {
3747 /* Get the number of screen cells used by the inserted characters */
3748 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003749 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 vcol = start_vcol;
3751 for (i = 0; i < ins_len; ++i)
3752 {
3753 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003754 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 vcol -= start_vcol;
3757
3758 /* Delete spaces that were inserted after the cursor to keep the
3759 * text aligned. */
3760 curwin->w_cursor.col += ins_len;
3761 while (vcol > orig_vcols && gchar_cursor() == ' ')
3762 {
3763 del_char(FALSE);
3764 ++orig_vcols;
3765 }
3766 curwin->w_cursor.col -= ins_len;
3767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
Bram Moolenaar196d1572019-01-02 23:47:18 +01003769 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003771
3772#ifdef FEAT_TEXT_PROP
3773 if (curbuf->b_has_textprop)
3774 {
3775 size_t len_now = STRLEN(ml_get_curline());
3776
3777 --text_prop_frozen;
3778 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003779 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003780 }
3781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003784 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785}
3786
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3788/*
3789 * Map Hebrew keyboard when in hkmap mode.
3790 */
3791 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003792hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
3794 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
3795 {
3796 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3797 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3798 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3799 static char_u map[26] =
3800 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3801 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3802 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3803 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3804 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3805 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3806 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3807 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3808 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3809
3810 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3811 return (int)(map[CharOrd(c)] - 1 + p_aleph);
3812 /* '-1'='sofit' */
3813 else if (c == 'x')
3814 return 'X';
3815 else if (c == 'q')
3816 return '\''; /* {geresh}={'} */
3817 else if (c == 246)
3818 return ' '; /* \"o --> ' ' for a german keyboard */
3819 else if (c == 228)
3820 return ' '; /* \"a --> ' ' -- / -- */
3821 else if (c == 252)
3822 return ' '; /* \"u --> ' ' -- / -- */
3823#ifdef EBCDIC
3824 else if (islower(c))
3825#else
3826 /* NOTE: islower() does not do the right thing for us on Linux so we
3827 * do this the same was as 5.7 and previous, so it works correctly on
3828 * all systems. Specifically, the e.g. Delete and Arrow keys are
3829 * munged and won't work if e.g. searching for Hebrew text.
3830 */
3831 else if (c >= 'a' && c <= 'z')
3832#endif
3833 return (int)(map[CharOrdLow(c)] + p_aleph);
3834 else
3835 return c;
3836 }
3837 else
3838 {
3839 switch (c)
3840 {
3841 case '`': return ';';
3842 case '/': return '.';
3843 case '\'': return ',';
3844 case 'q': return '/';
3845 case 'w': return '\'';
3846
3847 /* Hebrew letters - set offset from 'a' */
3848 case ',': c = '{'; break;
3849 case '.': c = 'v'; break;
3850 case ';': c = 't'; break;
3851 default: {
3852 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3853
3854#ifdef EBCDIC
3855 /* see note about islower() above */
3856 if (!islower(c))
3857#else
3858 if (c < 'a' || c > 'z')
3859#endif
3860 return c;
3861 c = str[CharOrdLow(c)];
3862 break;
3863 }
3864 }
3865
3866 return (int)(CharOrdLow(c) + p_aleph);
3867 }
3868}
3869#endif
3870
3871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003872ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873{
3874 int need_redraw = FALSE;
3875 int regname;
3876 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003877 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
3879 /*
3880 * If we are going to wait for a character, show a '"'.
3881 */
3882 pc_status = PC_STATUS_UNSET;
3883 if (redrawing() && !char_avail())
3884 {
3885 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00003886 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888 edit_putchar('"', TRUE);
3889#ifdef FEAT_CMDL_INFO
3890 add_to_showcmd_c(Ctrl_R);
3891#endif
3892 }
3893
3894#ifdef USE_ON_FLY_SCROLL
3895 dont_scroll = TRUE; /* disallow scrolling here */
3896#endif
3897
3898 /*
3899 * Don't map the register name. This also prevents the mode message to be
3900 * deleted when ESC is hit.
3901 */
3902 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003903 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3906 {
3907 /* Get a third key for literal register insertion */
3908 literally = regname;
3909#ifdef FEAT_CMDL_INFO
3910 add_to_showcmd_c(literally);
3911#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003912 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
3915 --no_mapping;
3916
3917#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02003918 /* Don't call u_sync() while typing the expression or giving an error
3919 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 ++no_u_sync;
3921 if (regname == '=')
3922 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003923 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003924# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003926# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003927 /* Sync undo when evaluating the expression calls setline() or
3928 * append(), so that it can be undone separately. */
3929 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003932
3933 // Cursor may be moved back a column.
3934 curwin->w_cursor = curpos;
3935 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003936# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003937 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (im_on)
3939 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00003942 if (regname == NUL || !valid_yank_reg(regname, FALSE))
3943 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003944 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00003946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 else
3948 {
3949#endif
3950 if (literally == Ctrl_O || literally == Ctrl_P)
3951 {
3952 /* Append the command to the redo buffer. */
3953 AppendCharToRedobuff(Ctrl_R);
3954 AppendCharToRedobuff(literally);
3955 AppendCharToRedobuff(regname);
3956
3957 do_put(regname, BACKWARD, 1L,
3958 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
3959 }
3960 else if (insert_reg(regname, literally) == FAIL)
3961 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003962 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 need_redraw = TRUE; /* remove the '"' */
3964 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003965 else if (stop_insert_mode)
3966 /* When the '=' register was used and a function was invoked that
3967 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
3968 * insert anything, need to remove the '"' */
3969 need_redraw = TRUE;
3970
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971#ifdef FEAT_EVAL
3972 }
3973 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003974 if (u_sync_once == 1)
3975 ins_need_undo = TRUE;
3976 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977#endif
3978#ifdef FEAT_CMDL_INFO
3979 clear_showcmd();
3980#endif
3981
3982 /* If the inserted register is empty, we need to remove the '"' */
3983 if (need_redraw || stuff_empty())
3984 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003985
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003986 /* Disallow starting Visual mode here, would get a weird mode. */
3987 if (!vis_active && VIsual_active)
3988 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989}
3990
3991/*
3992 * CTRL-G commands in Insert mode.
3993 */
3994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003995ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
3997 int c;
3998
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003999 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
4002 /*
4003 * Don't map the second key. This also prevents the mode message to be
4004 * deleted when ESC is hit.
4005 */
4006 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004007 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 --no_mapping;
4009 switch (c)
4010 {
4011 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
4012 case K_UP:
4013 case Ctrl_K:
4014 case 'k': ins_up(TRUE);
4015 break;
4016
4017 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
4018 case K_DOWN:
4019 case Ctrl_J:
4020 case 'j': ins_down(TRUE);
4021 break;
4022
4023 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004024 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004026
4027 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004028 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004029 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004030 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 break;
4032
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004033 /* CTRL-G U: do not break undo with the next char */
4034 case 'U':
4035 /* Allow one left/right cursor movement with the next char,
4036 * without breaking undo. */
4037 dont_sync_undo = MAYBE;
4038 break;
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02004041 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
4043}
4044
4045/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004046 * CTRL-^ in Insert mode.
4047 */
4048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004049ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004050{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004051 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004052 {
4053 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
4054 if (State & LANGMAP)
4055 {
4056 curbuf->b_p_iminsert = B_IMODE_NONE;
4057 State &= ~LANGMAP;
4058 }
4059 else
4060 {
4061 curbuf->b_p_iminsert = B_IMODE_LMAP;
4062 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004063#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004064 im_set_active(FALSE);
4065#endif
4066 }
4067 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004068#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004069 else
4070 {
4071 /* There are no ":lmap" mappings, toggle IM */
4072 if (im_get_status())
4073 {
4074 curbuf->b_p_iminsert = B_IMODE_NONE;
4075 im_set_active(FALSE);
4076 }
4077 else
4078 {
4079 curbuf->b_p_iminsert = B_IMODE_IM;
4080 State &= ~LANGMAP;
4081 im_set_active(TRUE);
4082 }
4083 }
4084#endif
4085 set_iminsert_global();
4086 showmode();
4087#ifdef FEAT_GUI
4088 /* may show different cursor shape or color */
4089 if (gui.in_use)
4090 gui_update_cursor(TRUE, FALSE);
4091#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004092#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093 /* Show/unshow value of 'keymap' in status lines. */
4094 status_redraw_curbuf();
4095#endif
4096}
4097
4098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 * Handle ESC in insert mode.
4100 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4101 * insert.
4102 */
4103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004104ins_esc(
4105 long *count,
4106 int cmdchar,
4107 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
4109 int temp;
4110 static int disabled_redraw = FALSE;
4111
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004112#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004113 check_spell_redraw();
4114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115#if defined(FEAT_HANGULIN)
4116# if defined(ESC_CHG_TO_ENG_MODE)
4117 hangul_input_state_set(0);
4118# endif
4119 if (composing_hangul)
4120 {
4121 push_raw_key(composing_hangul_buffer, 2);
4122 composing_hangul = 0;
4123 }
4124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 temp = curwin->w_cursor.col;
4127 if (disabled_redraw)
4128 {
4129 --RedrawingDisabled;
4130 disabled_redraw = FALSE;
4131 }
4132 if (!arrow_used)
4133 {
4134 /*
4135 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004136 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4137 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 */
4139 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004140 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /*
4143 * Repeating insert may take a long time. Check for
4144 * interrupt now and then.
4145 */
4146 if (*count > 0)
4147 {
4148 line_breakcheck();
4149 if (got_int)
4150 *count = 0;
4151 }
4152
4153 if (--*count > 0) /* repeat what was typed */
4154 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004155 /* Vi repeats the insert without replacing characters. */
4156 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4157 State &= ~REPLACE_FLAG;
4158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 (void)start_redo_ins();
4160 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02004161 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 ++RedrawingDisabled;
4163 disabled_redraw = TRUE;
4164 return FALSE; /* repeat the insert */
4165 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004166 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 undisplay_dollar();
4168 }
4169
4170 /* When an autoindent was removed, curswant stays after the
4171 * indent */
4172 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4173 curwin->w_set_curswant = TRUE;
4174
4175 /* Remember the last Insert position in the '^ mark. */
4176 if (!cmdmod.keepjumps)
4177 curbuf->b_last_insert = curwin->w_cursor;
4178
4179 /*
4180 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004181 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004183 if (!nomove
4184 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004185 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004186 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004187 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188#ifdef FEAT_RIGHTLEFT
4189 && !revins_on
4190#endif
4191 )
4192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4194 {
4195 oneleft();
4196 if (restart_edit != NUL)
4197 ++curwin->w_cursor.coladd;
4198 }
4199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
4201 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 /* Correct cursor for multi-byte character. */
4203 if (has_mbyte)
4204 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206 }
4207
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004208#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 /* Disable IM to allow typing English directly for Normal mode commands.
4210 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4211 * well). */
4212 if (!(State & LANGMAP))
4213 im_save_status(&curbuf->b_p_iminsert);
4214 im_set_active(FALSE);
4215#endif
4216
4217 State = NORMAL;
4218 /* need to position cursor again (e.g. when on a TAB ) */
4219 changed_cline_bef_curs();
4220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#ifdef CURSOR_SHAPE
4223 ui_cursor_shape(); /* may show different cursor shape */
4224#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004225 if (!p_ek)
4226 /* Re-enable bracketed paste mode. */
4227 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004229 // When recording or for CTRL-O, need to display the new mode.
4230 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004231 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004233 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004234 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
4236 return TRUE; /* exit Insert mode */
4237}
4238
4239#ifdef FEAT_RIGHTLEFT
4240/*
4241 * Toggle language: hkmap and revins_on.
4242 * Move to end of reverse inserted text.
4243 */
4244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004245ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
4247 if (revins_on && revins_chars && revins_scol >= 0)
4248 {
4249 while (gchar_cursor() != NUL && revins_chars--)
4250 ++curwin->w_cursor.col;
4251 }
4252 p_ri = !p_ri;
4253 revins_on = (State == INSERT && p_ri);
4254 if (revins_on)
4255 {
4256 revins_scol = curwin->w_cursor.col;
4257 revins_legal++;
4258 revins_chars = 0;
4259 undisplay_dollar();
4260 }
4261 else
4262 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004263 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 showmode();
4265}
4266#endif
4267
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268/*
4269 * If 'keymodel' contains "startsel", may start selection.
4270 * Returns TRUE when a CTRL-O and other keys stuffed.
4271 */
4272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004273ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274{
4275 if (km_startsel)
4276 switch (c)
4277 {
4278 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 case K_PAGEUP:
4281 case K_KPAGEUP:
4282 case K_PAGEDOWN:
4283 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004284# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 case K_LEFT:
4286 case K_RIGHT:
4287 case K_UP:
4288 case K_DOWN:
4289 case K_END:
4290 case K_HOME:
4291# endif
4292 if (!(mod_mask & MOD_MASK_SHIFT))
4293 break;
4294 /* FALLTHROUGH */
4295 case K_S_LEFT:
4296 case K_S_RIGHT:
4297 case K_S_UP:
4298 case K_S_DOWN:
4299 case K_S_END:
4300 case K_S_HOME:
4301 /* Start selection right away, the cursor can move with
4302 * CTRL-O when beyond the end of the line. */
4303 start_selection();
4304
4305 /* Execute the key in (insert) Select mode. */
4306 stuffcharReadbuff(Ctrl_O);
4307 if (mod_mask)
4308 {
4309 char_u buf[4];
4310
4311 buf[0] = K_SPECIAL;
4312 buf[1] = KS_MODIFIER;
4313 buf[2] = mod_mask;
4314 buf[3] = NUL;
4315 stuffReadbuff(buf);
4316 }
4317 stuffcharReadbuff(c);
4318 return TRUE;
4319 }
4320 return FALSE;
4321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004324 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004325 */
4326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004327ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004328{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004329#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004330 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004331 (char_u *)((State & REPLACE_FLAG) ? "i"
4332 : replaceState == VREPLACE ? "v"
4333 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004334#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004335 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004336 if (State & REPLACE_FLAG)
4337 State = INSERT | (State & LANGMAP);
4338 else
4339 State = replaceState | (State & LANGMAP);
4340 AppendCharToRedobuff(K_INS);
4341 showmode();
4342#ifdef CURSOR_SHAPE
4343 ui_cursor_shape(); /* may show different cursor shape */
4344#endif
4345}
4346
4347/*
4348 * Pressed CTRL-O in Insert mode.
4349 */
4350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004351ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004352{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353 if (State & VREPLACE_FLAG)
4354 restart_edit = 'V';
4355 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004356 if (State & REPLACE_FLAG)
4357 restart_edit = 'R';
4358 else
4359 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004360 if (virtual_active())
4361 ins_at_eol = FALSE; /* cursor always keeps its column */
4362 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363 ins_at_eol = (gchar_cursor() == NUL);
4364}
4365
4366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 * If the cursor is on an indent, ^T/^D insert/delete one
4368 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004369 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 * with vi. But vi only supports ^T and ^D after an
4371 * autoindent, we support it everywhere.
4372 */
4373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004374ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
4376 if (stop_arrow() == FAIL)
4377 return;
4378 AppendCharToRedobuff(c);
4379
4380 /*
4381 * 0^D and ^^D: remove all indent.
4382 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004383 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4384 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 --curwin->w_cursor.col;
4387 (void)del_char(FALSE); /* delete the '^' or '0' */
4388 /* In Replace mode, restore the characters that '^' or '0' replaced. */
4389 if (State & REPLACE_FLAG)
4390 replace_pop_ins();
4391 if (lastc == '^')
4392 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004393 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004396 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4399 did_ai = FALSE;
4400#ifdef FEAT_SMARTINDENT
4401 did_si = FALSE;
4402 can_si = FALSE;
4403 can_si_back = FALSE;
4404#endif
4405#ifdef FEAT_CINDENT
4406 can_cindent = FALSE; /* no cindenting after ^D or ^T */
4407#endif
4408}
4409
4410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004411ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
4413 int temp;
4414
4415 if (stop_arrow() == FAIL)
4416 return;
4417 if (gchar_cursor() == NUL) /* delete newline */
4418 {
4419 temp = curwin->w_cursor.col;
4420 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004421 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004422 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004426 /* Adjust orig_line_count in case more lines have been deleted than
4427 * have been added. That makes sure, that open_line() later
4428 * can access all buffer lines correctly */
4429 if (State & VREPLACE_FLAG &&
4430 orig_line_count > curbuf->b_ml.ml_line_count)
4431 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02004434 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
4435 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 did_ai = FALSE;
4437#ifdef FEAT_SMARTINDENT
4438 did_si = FALSE;
4439 can_si = FALSE;
4440 can_si_back = FALSE;
4441#endif
4442 AppendCharToRedobuff(K_DEL);
4443}
4444
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004445/*
4446 * Delete one character for ins_bs().
4447 */
4448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004449ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004450{
4451 dec_cursor();
4452 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4453 if (State & REPLACE_FLAG)
4454 {
4455 /* Don't delete characters before the insert point when in
4456 * Replace mode */
4457 if (curwin->w_cursor.lnum != Insstart.lnum
4458 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004459 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004460 }
4461 else
4462 (void)del_char(FALSE);
4463}
4464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465/*
4466 * Handle Backspace, delete-word and delete-line in Insert mode.
4467 * Return TRUE when backspace was actually used.
4468 */
4469 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004470ins_bs(
4471 int c,
4472 int mode,
4473 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
4475 linenr_T lnum;
4476 int cc;
4477 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004478 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 colnr_T mincol;
4480 int did_backspace = FALSE;
4481 int in_indent;
4482 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004483 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 /*
4486 * can't delete anything in an empty file
4487 * can't backup past first character in buffer
4488 * can't backup past starting point unless 'backspace' > 1
4489 * can backup to a previous line if 'backspace' == 0
4490 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004491 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 || (
4493#ifdef FEAT_RIGHTLEFT
4494 !revins_on &&
4495#endif
4496 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4497 || (!can_bs(BS_START)
4498 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004499 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4500 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4502 && curwin->w_cursor.col <= ai_col)
4503 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4504 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004505 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 return FALSE;
4507 }
4508
4509 if (stop_arrow() == FAIL)
4510 return FALSE;
4511 in_indent = inindent(0);
4512#ifdef FEAT_CINDENT
4513 if (in_indent)
4514 can_cindent = FALSE;
4515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 end_comment_pending = NUL; /* After BS, don't auto-end comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517#ifdef FEAT_RIGHTLEFT
4518 if (revins_on) /* put cursor after last inserted char */
4519 inc_cursor();
4520#endif
4521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 /* Virtualedit:
4523 * BACKSPACE_CHAR eats a virtual space
4524 * BACKSPACE_WORD eats all coladd
4525 * BACKSPACE_LINE eats all coladd and keeps going
4526 */
4527 if (curwin->w_cursor.coladd > 0)
4528 {
4529 if (mode == BACKSPACE_CHAR)
4530 {
4531 --curwin->w_cursor.coladd;
4532 return TRUE;
4533 }
4534 if (mode == BACKSPACE_WORD)
4535 {
4536 curwin->w_cursor.coladd = 0;
4537 return TRUE;
4538 }
4539 curwin->w_cursor.coladd = 0;
4540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
4542 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004543 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 */
4545 if (curwin->w_cursor.col == 0)
4546 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004547 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004548 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549#ifdef FEAT_RIGHTLEFT
4550 || revins_on
4551#endif
4552 )
4553 {
4554 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4555 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4556 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004557 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004558 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560 /*
4561 * In replace mode:
4562 * cc < 0: NL was inserted, delete it
4563 * cc >= 0: NL was replaced, put original characters back
4564 */
4565 cc = -1;
4566 if (State & REPLACE_FLAG)
4567 cc = replace_pop(); /* returns -1 if NL was inserted */
4568 /*
4569 * In replace mode, in the line we started replacing, we only move the
4570 * cursor.
4571 */
4572 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4573 {
4574 dec_cursor();
4575 }
4576 else
4577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (!(State & VREPLACE_FLAG)
4579 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 temp = gchar_cursor(); /* remember current char */
4582 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004583
4584 /* When "aw" is in 'formatoptions' we must delete the space at
4585 * the end of the line, otherwise the line will be broken
4586 * again when auto-formatting. */
4587 if (has_format_option(FO_AUTO)
4588 && has_format_option(FO_WHITE_PAR))
4589 {
4590 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4591 TRUE);
4592 int len;
4593
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004594 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004595 if (len > 0 && ptr[len - 1] == ' ')
4596 ptr[len - 1] = NUL;
4597 }
4598
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004599 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (temp == NUL && gchar_cursor() != NUL)
4601 inc_cursor();
4602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 else
4604 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 /*
4607 * In REPLACE mode we have to put back the text that was replaced
4608 * by the NL. On the replace stack is first a NUL-terminated
4609 * sequence of characters that were deleted and then the
4610 * characters that NL replaced.
4611 */
4612 if (State & REPLACE_FLAG)
4613 {
4614 /*
4615 * Do the next ins_char() in NORMAL state, to
4616 * prevent ins_char() from replacing characters and
4617 * avoiding showmatch().
4618 */
4619 oldState = State;
4620 State = NORMAL;
4621 /*
4622 * restore characters (blanks) deleted after cursor
4623 */
4624 while (cc > 0)
4625 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004626 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004628 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 cc = replace_pop();
4630 }
4631 /* restore the characters that NL replaced */
4632 replace_pop_ins();
4633 State = oldState;
4634 }
4635 }
4636 did_ai = FALSE;
4637 }
4638 else
4639 {
4640 /*
4641 * Delete character(s) before the cursor.
4642 */
4643#ifdef FEAT_RIGHTLEFT
4644 if (revins_on) /* put cursor on last inserted char */
4645 dec_cursor();
4646#endif
4647 mincol = 0;
4648 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004649 if (mode == BACKSPACE_LINE
4650 && (curbuf->b_p_ai
4651#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004652 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004653#endif
4654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655#ifdef FEAT_RIGHTLEFT
4656 && !revins_on
4657#endif
4658 )
4659 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004660 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004662 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004664 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
4666
4667 /*
4668 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4669 */
4670 if ( mode == BACKSPACE_CHAR
4671 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004672 || ((get_sts_value() != 0
4673#ifdef FEAT_VARTABS
4674 || tabstop_count(curbuf->b_p_vsts_array)
4675#endif
4676 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004677 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 && (*(ml_get_cursor() - 1) == TAB
4679 || (*(ml_get_cursor() - 1) == ' '
4680 && (!*inserted_space_p
4681 || arrow_used))))))
4682 {
4683 int ts;
4684 colnr_T vcol;
4685 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004686 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 /* Compute the virtual column where we want to be. Since
4690 * 'showbreak' may get in the way, need to get the last column of
4691 * the previous character. */
4692 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004693 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 dec_cursor();
4695 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4696 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004697#ifdef FEAT_VARTABS
4698 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004699 {
4700 ts = (int)get_sw_value(curbuf);
4701 want_vcol = (want_vcol / ts) * ts;
4702 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004703 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004704 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004705 curbuf->b_p_vsts_array);
4706#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004707 if (p_sta && in_indent)
4708 ts = (int)get_sw_value(curbuf);
4709 else
4710 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
4714 /* delete characters until we are at or before want_vcol */
4715 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004716 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004717 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718
4719 /* insert extra spaces until we are at want_vcol */
4720 while (vcol < want_vcol)
4721 {
4722 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004723 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4724 && curwin->w_cursor.col < Insstart_orig.col)
4725 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 if (State & VREPLACE_FLAG)
4728 ins_char(' ');
4729 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004732 if ((State & REPLACE_FLAG))
4733 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4736 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004737
4738 /* If we are now back where we started delete one character. Can
4739 * happen when using 'sts' and 'linebreak'. */
4740 if (vcol >= start_vcol)
4741 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743
4744 /*
4745 * Delete upto starting point, start of line or previous word.
4746 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004747 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004749 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004751 if (has_mbyte)
4752 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004753 do
4754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004756 if (!revins_on) /* put cursor on char to be deleted */
4757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004759
4760 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004761 /* look multi-byte character class */
4762 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004764 prev_cclass = cclass;
4765 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004767
4768 /* start of word? */
4769 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4770 {
4771 mode = BACKSPACE_WORD_NOT_SPACE;
4772 temp = vim_iswordc(cc);
4773 }
4774 /* end of word? */
4775 else if (mode == BACKSPACE_WORD_NOT_SPACE
4776 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004777 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004780 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004782 inc_cursor();
4783#ifdef FEAT_RIGHTLEFT
4784 else if (State & REPLACE_FLAG)
4785 dec_cursor();
4786#endif
4787 break;
4788 }
4789 if (State & REPLACE_FLAG)
4790 replace_do_bs(-1);
4791 else
4792 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004793 if (enc_utf8 && p_deco)
4794 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004795 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004796 /*
4797 * If there are combining characters and 'delcombine' is set
4798 * move the cursor back. Don't back up before the base
4799 * character.
4800 */
4801 if (enc_utf8 && p_deco && cpc[0] != NUL)
4802 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004803#ifdef FEAT_RIGHTLEFT
4804 if (revins_chars)
4805 {
4806 revins_chars--;
4807 revins_legal++;
4808 }
4809 if (revins_on && gchar_cursor() == NUL)
4810 break;
4811#endif
4812 }
4813 /* Just a single backspace?: */
4814 if (mode == BACKSPACE_CHAR)
4815 break;
4816 } while (
4817#ifdef FEAT_RIGHTLEFT
4818 revins_on ||
4819#endif
4820 (curwin->w_cursor.col > mincol
4821 && (curwin->w_cursor.lnum != Insstart_orig.lnum
4822 || curwin->w_cursor.col != Insstart_orig.col)));
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 did_backspace = TRUE;
4825 }
4826#ifdef FEAT_SMARTINDENT
4827 did_si = FALSE;
4828 can_si = FALSE;
4829 can_si_back = FALSE;
4830#endif
4831 if (curwin->w_cursor.col <= 1)
4832 did_ai = FALSE;
4833 /*
4834 * It's a little strange to put backspaces into the redo
4835 * buffer, but it makes auto-indent a lot easier to deal
4836 * with.
4837 */
4838 AppendCharToRedobuff(c);
4839
4840 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004841 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004842 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004843 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
4845 /* vi behaviour: the cursor moves backward but the character that
4846 * was there remains visible
4847 * Vim behaviour: the cursor moves backward and the character that
4848 * was there is erased from the screen.
4849 * We can emulate the vi behaviour by pretending there is a dollar
4850 * displayed even when there isn't.
4851 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004852 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 dollar_vcol = curwin->w_virtcol;
4854
Bram Moolenaarce3be472008-01-14 19:12:28 +00004855#ifdef FEAT_FOLDING
4856 /* When deleting a char the cursor line must never be in a closed fold.
4857 * E.g., when 'foldmethod' is indent and deleting the first non-white
4858 * char before a Tab. */
4859 if (did_backspace)
4860 foldOpenCursor();
4861#endif
4862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 return did_backspace;
4864}
4865
Bram Moolenaarec2da362017-01-21 20:04:22 +01004866/*
4867 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4868 * P_PE literally.
4869 * When "drop" is TRUE then consume the text and drop it.
4870 */
4871 int
4872bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4873{
4874 int c;
4875 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4876 int idx = 0;
4877 char_u *end = find_termcode((char_u *)"PE");
4878 int ret_char = -1;
4879 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004880 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004881
4882 /* If the end code is too long we can't detect it, read everything. */
4883 if (STRLEN(end) >= NUMBUFLEN)
4884 end = NULL;
4885 ++no_mapping;
4886 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004887 if (!p_paste)
4888 // Also have the side effects of setting 'paste' to make it work much
4889 // faster.
4890 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004891
Bram Moolenaarec2da362017-01-21 20:04:22 +01004892 for (;;)
4893 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004894 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004895 if (end == NULL && vpeekc() == NUL)
4896 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004897 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004898 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004899 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004900 if (c == NUL || got_int)
4901 // When CTRL-C was encountered the typeahead will be flushed and we
4902 // won't get the end sequence.
4903 break;
4904
Bram Moolenaarec2da362017-01-21 20:04:22 +01004905 if (has_mbyte)
4906 idx += (*mb_char2bytes)(c, buf + idx);
4907 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004908 buf[idx++] = c;
4909 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004910 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004911 {
4912 if (end[idx] == NUL)
4913 break; /* Found the end of paste code. */
4914 continue;
4915 }
4916 if (!drop)
4917 {
4918 switch (mode)
4919 {
4920 case PASTE_CMDLINE:
4921 put_on_cmdline(buf, idx, TRUE);
4922 break;
4923
4924 case PASTE_EX:
4925 if (gap != NULL && ga_grow(gap, idx) == OK)
4926 {
4927 mch_memmove((char *)gap->ga_data + gap->ga_len,
4928 buf, (size_t)idx);
4929 gap->ga_len += idx;
4930 }
4931 break;
4932
4933 case PASTE_INSERT:
4934 if (stop_arrow() == OK)
4935 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004936 c = buf[0];
4937 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
4938 ins_eol(c);
4939 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01004940 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004941 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01004942 AppendToRedobuffLit(buf, idx);
4943 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01004944 }
4945 break;
4946
4947 case PASTE_ONE_CHAR:
4948 if (ret_char == -1)
4949 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01004950 if (has_mbyte)
4951 ret_char = (*mb_ptr2char)(buf);
4952 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004953 ret_char = buf[0];
4954 }
4955 break;
4956 }
4957 }
4958 idx = 0;
4959 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004960
Bram Moolenaarec2da362017-01-21 20:04:22 +01004961 --no_mapping;
4962 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004963 if (!save_paste)
4964 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01004965
4966 return ret_char;
4967}
4968
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004969#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004971ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004972{
4973 /* We will be leaving the current window, unless closing another tab. */
4974 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
4975 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
4976 {
4977 undisplay_dollar();
4978 start_arrow(&curwin->w_cursor);
4979# ifdef FEAT_CINDENT
4980 can_cindent = TRUE;
4981# endif
4982 }
4983
4984 if (c == K_TABLINE)
4985 goto_tabpage(current_tab);
4986 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004987 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004988 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004989 redraw_statuslines(); /* will redraw the tabline when needed */
4990 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004991}
4992#endif
4993
4994#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004996ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
4998 pos_T tpos;
4999
5000 undisplay_dollar();
5001 tpos = curwin->w_cursor;
5002 if (gui_do_scroll())
5003 {
5004 start_arrow(&tpos);
5005# ifdef FEAT_CINDENT
5006 can_cindent = TRUE;
5007# endif
5008 }
5009}
5010
5011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005012ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014 pos_T tpos;
5015
5016 undisplay_dollar();
5017 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005018 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 start_arrow(&tpos);
5021# ifdef FEAT_CINDENT
5022 can_cindent = TRUE;
5023# endif
5024 }
5025}
5026#endif
5027
5028 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005029ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
5031 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005032 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033
5034#ifdef FEAT_FOLDING
5035 if ((fdo_flags & FDO_HOR) && KeyTyped)
5036 foldOpenCursor();
5037#endif
5038 undisplay_dollar();
5039 tpos = curwin->w_cursor;
5040 if (oneleft() == OK)
5041 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005042#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5043 /* Only call start_arrow() when not busy with preediting, it will
5044 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005045 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005046#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005047 {
5048 start_arrow_with_change(&tpos, end_change);
5049 if (!end_change)
5050 AppendCharToRedobuff(K_LEFT);
5051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052#ifdef FEAT_RIGHTLEFT
5053 /* If exit reversed string, position is fixed */
5054 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5055 revins_legal++;
5056 revins_chars++;
5057#endif
5058 }
5059
5060 /*
5061 * if 'whichwrap' set for cursor in insert mode may go to
5062 * previous line
5063 */
5064 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5065 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005066 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 start_arrow(&tpos);
5068 --(curwin->w_cursor.lnum);
5069 coladvance((colnr_T)MAXCOL);
5070 curwin->w_set_curswant = TRUE; /* so we stay at the end */
5071 }
5072 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005073 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005074 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075}
5076
5077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005078ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
5080 pos_T tpos;
5081
5082#ifdef FEAT_FOLDING
5083 if ((fdo_flags & FDO_HOR) && KeyTyped)
5084 foldOpenCursor();
5085#endif
5086 undisplay_dollar();
5087 tpos = curwin->w_cursor;
5088 if (c == K_C_HOME)
5089 curwin->w_cursor.lnum = 1;
5090 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 curwin->w_curswant = 0;
5093 start_arrow(&tpos);
5094}
5095
5096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005097ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
5099 pos_T tpos;
5100
5101#ifdef FEAT_FOLDING
5102 if ((fdo_flags & FDO_HOR) && KeyTyped)
5103 foldOpenCursor();
5104#endif
5105 undisplay_dollar();
5106 tpos = curwin->w_cursor;
5107 if (c == K_C_END)
5108 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5109 coladvance((colnr_T)MAXCOL);
5110 curwin->w_curswant = MAXCOL;
5111
5112 start_arrow(&tpos);
5113}
5114
5115 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005116ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005118 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119#ifdef FEAT_FOLDING
5120 if ((fdo_flags & FDO_HOR) && KeyTyped)
5121 foldOpenCursor();
5122#endif
5123 undisplay_dollar();
5124 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5125 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005126 start_arrow_with_change(&curwin->w_cursor, end_change);
5127 if (!end_change)
5128 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 (void)bck_word(1L, FALSE, FALSE);
5130 curwin->w_set_curswant = TRUE;
5131 }
5132 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005133 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005134 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135}
5136
5137 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005138ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005140 int end_change = dont_sync_undo == FALSE; // end undoable change
5141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142#ifdef FEAT_FOLDING
5143 if ((fdo_flags & FDO_HOR) && KeyTyped)
5144 foldOpenCursor();
5145#endif
5146 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005147 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005149 start_arrow_with_change(&curwin->w_cursor, end_change);
5150 if (!end_change)
5151 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (virtual_active())
5154 oneright();
5155 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005158 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 ++curwin->w_cursor.col;
5161 }
5162
5163#ifdef FEAT_RIGHTLEFT
5164 revins_legal++;
5165 if (revins_chars)
5166 revins_chars--;
5167#endif
5168 }
5169 /* if 'whichwrap' set for cursor in insert mode, may move the
5170 * cursor to the next line */
5171 else if (vim_strchr(p_ww, ']') != NULL
5172 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5173 {
5174 start_arrow(&curwin->w_cursor);
5175 curwin->w_set_curswant = TRUE;
5176 ++curwin->w_cursor.lnum;
5177 curwin->w_cursor.col = 0;
5178 }
5179 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005180 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005181 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182}
5183
5184 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005185ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005187 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#ifdef FEAT_FOLDING
5189 if ((fdo_flags & FDO_HOR) && KeyTyped)
5190 foldOpenCursor();
5191#endif
5192 undisplay_dollar();
5193 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5194 || gchar_cursor() != NUL)
5195 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005196 start_arrow_with_change(&curwin->w_cursor, end_change);
5197 if (!end_change)
5198 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 (void)fwd_word(1L, FALSE, 0);
5200 curwin->w_set_curswant = TRUE;
5201 }
5202 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005203 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005204 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205}
5206
5207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005208ins_up(
5209 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
5211 pos_T tpos;
5212 linenr_T old_topline = curwin->w_topline;
5213#ifdef FEAT_DIFF
5214 int old_topfill = curwin->w_topfill;
5215#endif
5216
5217 undisplay_dollar();
5218 tpos = curwin->w_cursor;
5219 if (cursor_up(1L, TRUE) == OK)
5220 {
5221 if (startcol)
5222 coladvance(getvcol_nolist(&Insstart));
5223 if (old_topline != curwin->w_topline
5224#ifdef FEAT_DIFF
5225 || old_topfill != curwin->w_topfill
5226#endif
5227 )
5228 redraw_later(VALID);
5229 start_arrow(&tpos);
5230#ifdef FEAT_CINDENT
5231 can_cindent = TRUE;
5232#endif
5233 }
5234 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005235 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236}
5237
5238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005239ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
5241 pos_T tpos;
5242
5243 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005244
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005245 if (mod_mask & MOD_MASK_CTRL)
5246 {
5247 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005248 if (first_tabpage->tp_next != NULL)
5249 {
5250 start_arrow(&curwin->w_cursor);
5251 goto_tabpage(-1);
5252 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005253 return;
5254 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 tpos = curwin->w_cursor;
5257 if (onepage(BACKWARD, 1L) == OK)
5258 {
5259 start_arrow(&tpos);
5260#ifdef FEAT_CINDENT
5261 can_cindent = TRUE;
5262#endif
5263 }
5264 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005265 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266}
5267
5268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005269ins_down(
5270 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 pos_T tpos;
5273 linenr_T old_topline = curwin->w_topline;
5274#ifdef FEAT_DIFF
5275 int old_topfill = curwin->w_topfill;
5276#endif
5277
5278 undisplay_dollar();
5279 tpos = curwin->w_cursor;
5280 if (cursor_down(1L, TRUE) == OK)
5281 {
5282 if (startcol)
5283 coladvance(getvcol_nolist(&Insstart));
5284 if (old_topline != curwin->w_topline
5285#ifdef FEAT_DIFF
5286 || old_topfill != curwin->w_topfill
5287#endif
5288 )
5289 redraw_later(VALID);
5290 start_arrow(&tpos);
5291#ifdef FEAT_CINDENT
5292 can_cindent = TRUE;
5293#endif
5294 }
5295 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005296 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297}
5298
5299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005300ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301{
5302 pos_T tpos;
5303
5304 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005305
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005306 if (mod_mask & MOD_MASK_CTRL)
5307 {
5308 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005309 if (first_tabpage->tp_next != NULL)
5310 {
5311 start_arrow(&curwin->w_cursor);
5312 goto_tabpage(0);
5313 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005314 return;
5315 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 tpos = curwin->w_cursor;
5318 if (onepage(FORWARD, 1L) == OK)
5319 {
5320 start_arrow(&tpos);
5321#ifdef FEAT_CINDENT
5322 can_cindent = TRUE;
5323#endif
5324 }
5325 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005326 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327}
5328
5329#ifdef FEAT_DND
5330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005331ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332{
5333 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5334}
5335#endif
5336
5337/*
5338 * Handle TAB in Insert or Replace mode.
5339 * Return TRUE when the TAB needs to be inserted like a normal character.
5340 */
5341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005342ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
5344 int ind;
5345 int i;
5346 int temp;
5347
5348 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5349 Insstart_blank_vcol = get_nolist_virtcol();
5350 if (echeck_abbr(TAB + ABBR_OFF))
5351 return FALSE;
5352
5353 ind = inindent(0);
5354#ifdef FEAT_CINDENT
5355 if (ind)
5356 can_cindent = FALSE;
5357#endif
5358
5359 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005360 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 */
5362 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005363#ifdef FEAT_VARTABS
5364 && !(p_sta && ind
5365 /* These five lines mean 'tabstop' != 'shiftwidth' */
5366 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5367 || (tabstop_count(curbuf->b_p_vts_array) == 1
5368 && tabstop_first(curbuf->b_p_vts_array)
5369 != get_sw_value(curbuf))
5370 || (tabstop_count(curbuf->b_p_vts_array) == 0
5371 && curbuf->b_p_ts != get_sw_value(curbuf))))
5372 && tabstop_count(curbuf->b_p_vsts_array) == 0
5373#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005374 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005375#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005376 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 return TRUE;
5378
5379 if (stop_arrow() == FAIL)
5380 return TRUE;
5381
5382 did_ai = FALSE;
5383#ifdef FEAT_SMARTINDENT
5384 did_si = FALSE;
5385 can_si = FALSE;
5386 can_si_back = FALSE;
5387#endif
5388 AppendToRedobuff((char_u *)"\t");
5389
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005390#ifdef FEAT_VARTABS
5391 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
5392 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005393 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005394 temp -= get_nolist_virtcol() % temp;
5395 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005396 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005397 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005398 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005399 curbuf->b_p_vsts_array);
5400 else /* otherwise use 'tabstop' */
5401 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5402 curbuf->b_p_vts_array);
5403#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005405 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005406 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
5407 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else /* otherwise use 'tabstop' */
5409 temp = (int)curbuf->b_p_ts;
5410 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
5413 /*
5414 * Insert the first space with ins_char(). It will delete one char in
5415 * replace mode. Insert the rest with ins_str(); it will not delete any
5416 * chars. For VREPLACE mode, we use ins_char() for all characters.
5417 */
5418 ins_char(' ');
5419 while (--temp > 0)
5420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 if (State & VREPLACE_FLAG)
5422 ins_char(' ');
5423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
5425 ins_str((char_u *)" ");
5426 if (State & REPLACE_FLAG) /* no char replaced */
5427 replace_push(NUL);
5428 }
5429 }
5430
5431 /*
5432 * When 'expandtab' not set: Replace spaces by TABs where possible.
5433 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005434#ifdef FEAT_VARTABS
5435 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5436 || get_sts_value() > 0
5437 || (p_sta && ind)))
5438#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005439 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
5442 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 char_u *saved_line = NULL; /* init for GCC */
5444 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 pos_T fpos;
5446 pos_T *cursor;
5447 colnr_T want_vcol, vcol;
5448 int change_col = -1;
5449 int save_list = curwin->w_p_list;
5450
5451 /*
5452 * Get the current line. For VREPLACE mode, don't make real changes
5453 * yet, just work on a copy of the line.
5454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 if (State & VREPLACE_FLAG)
5456 {
5457 pos = curwin->w_cursor;
5458 cursor = &pos;
5459 saved_line = vim_strsave(ml_get_curline());
5460 if (saved_line == NULL)
5461 return FALSE;
5462 ptr = saved_line + pos.col;
5463 }
5464 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 ptr = ml_get_cursor();
5467 cursor = &curwin->w_cursor;
5468 }
5469
5470 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
5471 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5472 curwin->w_p_list = FALSE;
5473
5474 /* Find first white before the cursor */
5475 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005476 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 {
5478 --fpos.col;
5479 --ptr;
5480 }
5481
5482 /* In Replace mode, don't change characters before the insert point. */
5483 if ((State & REPLACE_FLAG)
5484 && fpos.lnum == Insstart.lnum
5485 && fpos.col < Insstart.col)
5486 {
5487 ptr += Insstart.col - fpos.col;
5488 fpos.col = Insstart.col;
5489 }
5490
5491 /* compute virtual column numbers of first white and cursor */
5492 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5493 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5494
Bram Moolenaar597a4222014-06-25 14:39:50 +02005495 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5496 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01005497 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005499 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if (vcol + i > want_vcol)
5501 break;
5502 if (*ptr != TAB)
5503 {
5504 *ptr = TAB;
5505 if (change_col < 0)
5506 {
5507 change_col = fpos.col; /* Column of first change */
5508 /* May have to adjust Insstart */
5509 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5510 Insstart.col = fpos.col;
5511 }
5512 }
5513 ++fpos.col;
5514 ++ptr;
5515 vcol += i;
5516 }
5517
5518 if (change_col >= 0)
5519 {
5520 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005521 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
5523 /* Skip over the spaces we need. */
5524 while (vcol < want_vcol && *ptr == ' ')
5525 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005526 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 ++ptr;
5528 ++repl_off;
5529 }
5530 if (vcol > want_vcol)
5531 {
5532 /* Must have a char with 'showbreak' just before it. */
5533 --ptr;
5534 --repl_off;
5535 }
5536 fpos.col += repl_off;
5537
5538 /* Delete following spaces. */
5539 i = cursor->col - fpos.col;
5540 if (i > 0)
5541 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005542 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005544 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 for (temp = i; --temp >= 0; )
5546 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005547#ifdef FEAT_TEXT_PROP
5548 curbuf->b_ml.ml_line_len -= i;
5549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005551#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005552 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005553 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005554 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005555 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5556 (char_u *)"\t", 1);
5557 }
5558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 cursor->col -= i;
5560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 /*
5562 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5563 * backspacing over the changed spacing and then inserting the new
5564 * spacing.
5565 */
5566 if (State & VREPLACE_FLAG)
5567 {
5568 /* Backspace from real cursor to change_col */
5569 backspace_until_column(change_col);
5570
5571 /* Insert each char in saved_line from changed_col to
5572 * ptr-cursor */
5573 ins_bytes_len(saved_line + change_col,
5574 cursor->col - change_col);
5575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 if (State & VREPLACE_FLAG)
5579 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 curwin->w_p_list = save_list;
5581 }
5582
5583 return FALSE;
5584}
5585
5586/*
5587 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005588 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005590 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005591ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 int i;
5594
5595 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005596 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 undisplay_dollar();
5600
5601 /*
5602 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5603 * character under the cursor. Only push a NUL on the replace stack,
5604 * nothing to put back when the NL is deleted.
5605 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005606 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 replace_push(NUL);
5608
5609 /*
5610 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5611 * replacing the next line, so we push all of the characters left on the
5612 * line onto the replace stack. This is not done here though, it is done
5613 * in open_line().
5614 */
5615
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005616 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
5617 * CTRL-O). */
5618 if (virtual_active() && curwin->w_cursor.coladd > 0)
5619 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005620
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 /* NL in reverse insert will always start in the end of
5623 * current line. */
5624 if (revins_on)
5625 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5626#endif
5627
5628 AppendToRedobuff(NL_STR);
5629 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005630 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 old_indent = 0;
5632#ifdef FEAT_CINDENT
5633 can_cindent = TRUE;
5634#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005635#ifdef FEAT_FOLDING
5636 /* When inserting a line the cursor line must never be in a closed fold. */
5637 foldOpenCursor();
5638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005640 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641}
5642
5643#ifdef FEAT_DIGRAPHS
5644/*
5645 * Handle digraph in insert mode.
5646 * Returns character still to be inserted, or NUL when nothing remaining to be
5647 * done.
5648 */
5649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005650ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
5652 int c;
5653 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005654 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
5656 pc_status = PC_STATUS_UNSET;
5657 if (redrawing() && !char_avail())
5658 {
5659 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005660 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005663 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664#ifdef FEAT_CMDL_INFO
5665 add_to_showcmd_c(Ctrl_K);
5666#endif
5667 }
5668
5669#ifdef USE_ON_FLY_SCROLL
5670 dont_scroll = TRUE; /* disallow scrolling here */
5671#endif
5672
5673 /* don't map the digraph chars. This also prevents the
5674 * mode message to be deleted when ESC is hit */
5675 ++no_mapping;
5676 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005677 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 --no_mapping;
5679 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005680 if (did_putchar)
5681 /* when the line fits in 'columns' the '?' is at the start of the next
5682 * line and will not be removed by the redraw */
5683 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005684
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 if (IS_SPECIAL(c) || mod_mask) /* special key */
5686 {
5687#ifdef FEAT_CMDL_INFO
5688 clear_showcmd();
5689#endif
5690 insert_special(c, TRUE, FALSE);
5691 return NUL;
5692 }
5693 if (c != ESC)
5694 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005695 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (redrawing() && !char_avail())
5697 {
5698 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005699 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
5701 if (char2cells(c) == 1)
5702 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005703 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005705 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707#ifdef FEAT_CMDL_INFO
5708 add_to_showcmd_c(c);
5709#endif
5710 }
5711 ++no_mapping;
5712 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005713 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 --no_mapping;
5715 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005716 if (did_putchar)
5717 /* when the line fits in 'columns' the '?' is at the start of the
5718 * next line and will not be removed by a redraw */
5719 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 if (cc != ESC)
5721 {
5722 AppendToRedobuff((char_u *)CTRL_V_STR);
5723 c = getdigraph(c, cc, TRUE);
5724#ifdef FEAT_CMDL_INFO
5725 clear_showcmd();
5726#endif
5727 return c;
5728 }
5729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730#ifdef FEAT_CMDL_INFO
5731 clear_showcmd();
5732#endif
5733 return NUL;
5734}
5735#endif
5736
5737/*
5738 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5739 * Returns the char to be inserted, or NUL if none found.
5740 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005742ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
5744 int c;
5745 int temp;
5746 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005747 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
5749 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5750 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005751 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 return NUL;
5753 }
5754
5755 /* try to advance to the cursor column */
5756 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005757 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 prev_ptr = ptr;
5759 validate_virtcol();
5760 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5761 {
5762 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005763 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765 if ((colnr_T)temp > curwin->w_virtcol)
5766 ptr = prev_ptr;
5767
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005770 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 return c;
5772}
5773
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005774/*
5775 * CTRL-Y or CTRL-E typed in Insert mode.
5776 */
5777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005778ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005779{
5780 int c = tc;
5781
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005782 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005783 {
5784 if (c == Ctrl_Y)
5785 scrolldown_clamp();
5786 else
5787 scrollup_clamp();
5788 redraw_later(VALID);
5789 }
5790 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005791 {
5792 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5793 if (c != NUL)
5794 {
5795 long tw_save;
5796
5797 /* The character must be taken literally, insert like it
5798 * was typed after a CTRL-V, and pretend 'textwidth'
5799 * wasn't set. Digits, 'o' and 'x' are special after a
5800 * CTRL-V, don't use it for these. */
5801 if (c < 256 && !isalnum(c))
5802 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
5803 tw_save = curbuf->b_p_tw;
5804 curbuf->b_p_tw = -1;
5805 insert_special(c, TRUE, FALSE);
5806 curbuf->b_p_tw = tw_save;
5807#ifdef FEAT_RIGHTLEFT
5808 revins_chars++;
5809 revins_legal++;
5810#endif
5811 c = Ctrl_V; /* pretend CTRL-V is last character */
5812 auto_format(FALSE, TRUE);
5813 }
5814 }
5815 return c;
5816}
5817
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818/*
5819 * Get the value that w_virtcol would have when 'list' is off.
5820 * Unless 'cpo' contains the 'L' flag.
5821 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005822 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005823get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005825 // check validity of cursor in current buffer
5826 if (curwin->w_buffer == NULL
5827 || curwin->w_buffer->b_ml.ml_mfp == NULL
5828 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5829 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5831 return getvcol_nolist(&curwin->w_cursor);
5832 validate_virtcol();
5833 return curwin->w_virtcol;
5834}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005835
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005836#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005837/*
5838 * Handle the InsertCharPre autocommand.
5839 * "c" is the character that was typed.
5840 * Return a pointer to allocated memory with the replacement string.
5841 * Return NULL to continue inserting "c".
5842 */
5843 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005844do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005845{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005846 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005847 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005848 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005849
5850 /* Return quickly when there is nothing to do. */
5851 if (!has_insertcharpre())
5852 return NULL;
5853
Bram Moolenaar704984a2012-06-01 14:57:51 +02005854 if (has_mbyte)
5855 buf[(*mb_char2bytes)(c, buf)] = NUL;
5856 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005857 {
5858 buf[0] = c;
5859 buf[1] = NUL;
5860 }
5861
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005862 /* Lock the text to avoid weird things from happening. */
5863 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005864 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005865
Bram Moolenaar704984a2012-06-01 14:57:51 +02005866 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005867 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005868 {
5869 /* Get the value of v:char. It may be empty or more than one
5870 * character. Only use it when changed, otherwise continue with the
5871 * original character to avoid breaking autoindent. */
5872 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5873 res = vim_strsave(get_vim_var_str(VV_CHAR));
5874 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005875
5876 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
5877 --textlock;
5878
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005879 // Restore the State, it may have been changed.
5880 State = save_State;
5881
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005882 return res;
5883}
5884#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005885
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005886#if defined(FEAT_CINDENT) || defined(PROTO)
5887 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005888get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005889{
5890 return can_cindent;
5891}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005892
5893 void
5894set_can_cindent(int val)
5895{
5896 can_cindent = val;
5897}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005898#endif
5899
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005900/*
5901 * Trigger "event" and take care of fixing undo.
5902 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005903 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005904ins_apply_autocmds(event_T event)
5905{
5906 varnumber_T tick = CHANGEDTICK(curbuf);
5907 int r;
5908
5909 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5910
5911 // If u_savesub() was called then we are not prepared to start
5912 // a new line. Call u_save() with no contents to fix that.
5913 if (tick != CHANGEDTICK(curbuf))
5914 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5915
5916 return r;
5917}