blob: 4f53333cdef7b5932562e07745fb6cd84bd3f63d [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)
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100326 {
327 // Disable bracketed paste mode, we won't recognize the escape
328 // sequences.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100329 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100331 // Disable modifyOtherKeys, keys with modifiers would cause exiting
332 // Insert mode.
333 out_str(T_CTE);
334 }
335
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 /*
337 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100338 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
339 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 */
341 if (restart_edit != 0 && stuff_empty())
342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 /*
344 * After a paste we consider text typed to be part of the insert for
345 * the pasted text. You can backspace over the pasted text too.
346 */
347 if (where_paste_started.lnum)
348 arrow_used = FALSE;
349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 arrow_used = TRUE;
351 restart_edit = 0;
352
353 /*
354 * If the cursor was after the end-of-line before the CTRL-O and it is
355 * now at the end-of-line, put it after the end-of-line (this is not
356 * correct in very rare cases).
357 * Also do this if curswant is greater than the current virtual
358 * column. Eg after "^O$" or "^O80|".
359 */
360 validate_virtcol();
361 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000362 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 || curwin->w_curswant > curwin->w_virtcol)
364 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
365 {
366 if (ptr[1] == NUL)
367 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 else if (has_mbyte)
369 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000370 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 if (ptr[i] == NUL)
372 curwin->w_cursor.col += i;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000375 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 }
377 else
378 arrow_used = FALSE;
379
380 /* we are in insert mode now, don't need to start it anymore */
381 need_start_insertmode = FALSE;
382
383 /* Need to save the line for undo before inserting the first char. */
384 ins_need_undo = TRUE;
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 where_paste_started.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_CINDENT
388 can_cindent = TRUE;
389#endif
390#ifdef FEAT_FOLDING
391 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
392 * restarting. */
393 if (!p_im && did_restart_edit == 0)
394 foldOpenCursor();
395#endif
396
397 /*
398 * If 'showmode' is set, show the current (insert/replace/..) mode.
399 * A warning message for changing a readonly file is given here, before
400 * actually changing anything. It's put after the mode, if any.
401 */
402 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000403 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 i = showmode();
405
406 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000407 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
409#ifdef CURSOR_SHAPE
410 ui_cursor_shape(); /* may show different cursor shape */
411#endif
412#ifdef FEAT_DIGRAPHS
413 do_digraph(-1); /* clear digraphs */
414#endif
415
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000416 /*
417 * Get the current length of the redo buffer, those characters have to be
418 * skipped if we want to get to the inserted characters.
419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 ptr = get_inserted();
421 if (ptr == NULL)
422 new_insert_skip = 0;
423 else
424 {
425 new_insert_skip = (int)STRLEN(ptr);
426 vim_free(ptr);
427 }
428
429 old_indent = 0;
430
431 /*
432 * Main loop in Insert mode: repeat until Insert mode is left.
433 */
434 for (;;)
435 {
436#ifdef FEAT_RIGHTLEFT
437 if (!revins_legal)
438 revins_scol = -1; /* reset on illegal motions */
439 else
440 revins_legal = 0;
441#endif
442 if (arrow_used) /* don't repeat insert when arrow key used */
443 count = 0;
444
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100445 if (update_Insstart_orig)
446 Insstart_orig = Insstart;
447
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200448 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {
450 /* ":stopinsert" used or 'insertmode' reset */
451 count = 0;
452 goto doESCkey;
453 }
454
455 /* set curwin->w_curswant for next K_DOWN or K_UP */
456 if (!arrow_used)
457 curwin->w_set_curswant = TRUE;
458
459 /* If there is no typeahead may check for timestamps (e.g., for when a
460 * menu invoked a shell command). */
461 if (stuff_empty())
462 {
463 did_check_timestamps = FALSE;
464 if (need_check_timestamps)
465 check_timestamps(FALSE);
466 }
467
468 /*
469 * When emsg() was called msg_scroll will have been set.
470 */
471 msg_scroll = FALSE;
472
473#ifdef FEAT_GUI
474 /* When 'mousefocus' is set a mouse movement may have taken us to
475 * another window. "need_mouse_correct" may then be set because of an
476 * autocommand. */
477 if (need_mouse_correct)
478 gui_mouse_correct();
479#endif
480
481#ifdef FEAT_FOLDING
482 /* Open fold at the cursor line, according to 'foldopen'. */
483 if (fdo_flags & FDO_INSERT)
484 foldOpenCursor();
485 /* Close folds where the cursor isn't, according to 'foldclose' */
486 if (!char_avail())
487 foldCheckClose();
488#endif
489
Bram Moolenaarf2732452018-06-03 14:47:35 +0200490#ifdef FEAT_JOB_CHANNEL
491 if (bt_prompt(curbuf))
492 {
493 init_prompt(cmdchar_todo);
494 cmdchar_todo = NUL;
495 }
496#endif
497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 /*
499 * If we inserted a character at the last position of the last line in
500 * the window, scroll the window one line up. This avoids an extra
501 * redraw.
502 * This is detected when the cursor column is smaller after inserting
503 * something.
504 * Don't do this when the topline changed already, it has
505 * already been adjusted (by insertchar() calling open_line())).
506 */
507 if (curbuf->b_mod_set
508 && curwin->w_p_wrap
509 && !did_backspace
510 && curwin->w_topline == old_topline
511#ifdef FEAT_DIFF
512 && curwin->w_topfill == old_topfill
513#endif
514 )
515 {
516 mincol = curwin->w_wcol;
517 validate_cursor_col();
518
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200519 if (
520#ifdef FEAT_VARTABS
521 (int)curwin->w_wcol < mincol - tabstop_at(
522 get_nolist_virtcol(), curbuf->b_p_ts,
523 curbuf->b_p_vts_array)
524#else
525 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100528 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 && (curwin->w_cursor.lnum != curwin->w_topline
530#ifdef FEAT_DIFF
531 || curwin->w_topfill > 0
532#endif
533 ))
534 {
535#ifdef FEAT_DIFF
536 if (curwin->w_topfill > 0)
537 --curwin->w_topfill;
538 else
539#endif
540#ifdef FEAT_FOLDING
541 if (hasFolding(curwin->w_topline, NULL, &old_topline))
542 set_topline(curwin, old_topline + 1);
543 else
544#endif
545 set_topline(curwin, curwin->w_topline + 1);
546 }
547 }
548
549 /* May need to adjust w_topline to show the cursor. */
550 update_topline();
551
552 did_backspace = FALSE;
553
554 validate_cursor(); /* may set must_redraw */
555
556 /*
557 * Redraw the display when no characters are waiting.
558 * Also shows mode, ruler and positions cursor.
559 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000560 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (curwin->w_p_scb)
563 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar860cae12010-06-05 23:22:07 +0200565 if (curwin->w_p_crb)
566 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 update_curswant();
568 old_topline = curwin->w_topline;
569#ifdef FEAT_DIFF
570 old_topfill = curwin->w_topfill;
571#endif
572
573#ifdef USE_ON_FLY_SCROLL
574 dont_scroll = FALSE; /* allow scrolling here */
575#endif
576
577 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100578 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100580 if (c != K_CURSORHOLD)
581 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200582
583 /* After using CTRL-G U the next cursor key will not break undo. */
584 if (dont_sync_undo == MAYBE)
585 dont_sync_undo = TRUE;
586 else
587 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100588 if (cmdchar == K_PS)
589 /* Got here from normal mode when bracketed paste started. */
590 c = K_PS;
591 else
592 do
593 {
594 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200595
596 if (stop_insert_mode)
597 {
598 // Insert mode ended, possibly from a callback.
599 count = 0;
600 nomove = TRUE;
601 goto doESCkey;
602 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100603 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000605 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
606 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#ifdef FEAT_RIGHTLEFT
609 if (p_hkmap && KeyTyped)
610 c = hkmap(c); /* Hebrew mode mapping */
611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000613 /*
614 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000615 * and the cursor is still in the completed word. Only when there is
616 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000617 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100618 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000619 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100620 && curwin->w_cursor.col >= ins_compl_col()
621 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000622 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000623 /* BS: Delete one character from "compl_leader". */
624 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100625 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000626 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000627 continue;
628
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000629 /* When no match was selected or it was edited. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100630 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000631 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000632 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000633 * "compl_leader". Except when at the original match and
634 * there is nothing to add, CTRL-L works like CTRL-P then. */
635 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100636 && (!ctrl_x_mode_line_or_eval()
637 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000638 {
639 ins_compl_addfrommatch();
640 continue;
641 }
642
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000643 /* A non-white character that fits in with the current
644 * completion: Add to "compl_leader". */
645 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000646 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100647#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100648 /* Trigger InsertCharPre. */
649 char_u *str = do_insert_char_pre(c);
650 char_u *p;
651
652 if (str != NULL)
653 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100654 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100655 ins_compl_addleader(PTR2CHAR(p));
656 vim_free(str);
657 }
658 else
659#endif
660 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000661 continue;
662 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000663
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000664 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100665 * ins_compl_enter_selects() is set the Enter key does the
666 * same. */
667 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200668 && (c == CAR || c == K_KENTER || c == NL)))
669 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000670 {
671 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200672 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000673 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000674 }
675 }
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
678 * it does fix up the text when finishing completion. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100679 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000680 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000681 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
Bram Moolenaar488c6512005-08-11 20:09:58 +0000683 /* CTRL-\ CTRL-N goes to Normal mode,
684 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
685 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if (c == Ctrl_BSL)
687 {
688 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000689 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 ++no_mapping;
691 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000692 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 --no_mapping;
694 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000695 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000697 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 vungetc(c);
699 c = Ctrl_BSL;
700 }
701 else if (c == Ctrl_G && p_im)
702 continue;
703 else
704 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000705 if (c == Ctrl_O)
706 {
707 ins_ctrl_o();
708 ins_at_eol = FALSE; /* cursor keeps its column */
709 nomove = TRUE;
710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 count = 0;
712 goto doESCkey;
713 }
714 }
715
716#ifdef FEAT_DIGRAPHS
717 c = do_digraph(c);
718#endif
719
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100720 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (c == Ctrl_V || c == Ctrl_Q)
723 {
724 ins_ctrl_v();
725 c = Ctrl_V; /* pretend CTRL-V is last typed character */
726 continue;
727 }
728
729#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200730 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {
732 /* A key name preceded by a bang means this key is not to be
733 * inserted. Skip ahead to the re-indenting below.
734 * A key name preceded by a star means that indenting has to be
735 * done before inserting the key. */
736 line_is_white = inindent(0);
737 if (in_cinkeys(c, '!', line_is_white))
738 goto force_cindent;
739 if (can_cindent && in_cinkeys(c, '*', line_is_white)
740 && stop_arrow() == OK)
741 do_c_expr_indent();
742 }
743#endif
744
745#ifdef FEAT_RIGHTLEFT
746 if (curwin->w_p_rl)
747 switch (c)
748 {
749 case K_LEFT: c = K_RIGHT; break;
750 case K_S_LEFT: c = K_S_RIGHT; break;
751 case K_C_LEFT: c = K_C_RIGHT; break;
752 case K_RIGHT: c = K_LEFT; break;
753 case K_S_RIGHT: c = K_S_LEFT; break;
754 case K_C_RIGHT: c = K_C_LEFT; break;
755 }
756#endif
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /*
759 * If 'keymodel' contains "startsel", may start selection. If it
760 * does, a CTRL-O and c will be stuffed, we need to get these
761 * characters.
762 */
763 if (ins_start_select(c))
764 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 /*
767 * The big switch to handle a character in insert mode.
768 */
769 switch (c)
770 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000771 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (echeck_abbr(ESC + ABBR_OFF))
773 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200774 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000776 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#ifdef FEAT_CMDWIN
778 if (c == Ctrl_C && cmdwin_type != 0)
779 {
780 /* Close the cmdline window. */
781 cmdwin_result = K_IGNORE;
782 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000783 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 goto doESCkey;
785 }
786#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200787#ifdef FEAT_JOB_CHANNEL
788 if (c == Ctrl_C && bt_prompt(curbuf))
789 {
790 if (invoke_prompt_interrupt())
791 {
792 if (!bt_prompt(curbuf))
793 // buffer changed to a non-prompt buffer, get out of
794 // Insert mode
795 goto doESCkey;
796 break;
797 }
798 }
799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801#ifdef UNIX
802do_intr:
803#endif
804 /* when 'insertmode' set, and not halfway a mapping, don't leave
805 * Insert mode */
806 if (goto_im())
807 {
808 if (got_int)
809 {
810 (void)vgetc(); /* flush all buffers */
811 got_int = FALSE;
812 }
813 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200814 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 break;
816 }
817doESCkey:
818 /*
819 * This is the ONLY return from edit()!
820 */
821 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
822 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000823 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 o_lnum = curwin->w_cursor.lnum;
825
Bram Moolenaar488c6512005-08-11 20:09:58 +0000826 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000827 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100828 // When CTRL-C was typed got_int will be set, with the result
829 // that the autocommands won't be executed. When mapped got_int
830 // is not set, but let's keep the behavior the same.
831 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200832 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000833 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 continue;
837
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000838 case Ctrl_Z: /* suspend when 'insertmode' set */
839 if (!p_im)
840 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100841 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100842#ifdef CURSOR_SHAPE
843 ui_cursor_shape(); /* may need to update cursor shape */
844#endif
845 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000846
847 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000848#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100849 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000850 goto docomplete;
851#endif
852 if (echeck_abbr(Ctrl_O + ABBR_OFF))
853 break;
854 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000855
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000856 /* don't move the cursor left when 'virtualedit' has "onemore". */
857 if (ve_flags & VE_ONEMORE)
858 {
859 ins_at_eol = FALSE;
860 nomove = TRUE;
861 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000862 count = 0;
863 goto doESCkey;
864
Bram Moolenaar572cb562005-08-05 21:35:02 +0000865 case K_INS: /* toggle insert/replace mode */
866 case K_KINS:
867 ins_insert(replaceState);
868 break;
869
870 case K_SELECT: /* end of Select mode mapping - ignore */
871 break;
872
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000873 case K_HELP: /* Help key works like <ESC> <Help> */
874 case K_F1:
875 case K_XF1:
876 stuffcharReadbuff(K_HELP);
877 if (p_im)
878 need_start_insertmode = TRUE;
879 goto doESCkey;
880
881#ifdef FEAT_NETBEANS_INTG
882 case K_F21: /* NetBeans command */
883 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000884 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 --no_mapping;
886 netbeans_keycommand(i);
887 break;
888#endif
889
890 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 case NUL:
892 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000893 /* For ^@ the trailing ESC will end the insert, unless there is an
894 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
896 && c != Ctrl_A && !p_im)
897 goto doESCkey; /* quit insert mode */
898 inserted_space = FALSE;
899 break;
900
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000901 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ins_reg();
903 auto_format(FALSE, TRUE);
904 inserted_space = FALSE;
905 break;
906
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000907 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 ins_ctrl_g();
909 break;
910
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000911 case Ctrl_HAT: /* switch input mode and/or langmap */
912 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 break;
914
915#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000916 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (!p_ari)
918 goto normalchar;
919 ins_ctrl_();
920 break;
921#endif
922
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000923 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200924#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 goto docomplete;
927#endif
928 /* FALLTHROUGH */
929
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000930 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100931 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000933 if (has_compl_option(FALSE))
934 goto docomplete;
935 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ins_shift(c, lastc);
939 auto_format(FALSE, TRUE);
940 inserted_space = FALSE;
941 break;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 case K_KDEL:
945 ins_del();
946 auto_format(FALSE, TRUE);
947 break;
948
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 case Ctrl_H:
951 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
952 auto_format(FALSE, TRUE);
953 break;
954
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200956#ifdef FEAT_JOB_CHANNEL
957 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
958 {
959 // In a prompt window CTRL-W is used for window commands.
960 // Use Shift-CTRL-W to delete a word.
961 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200962 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200963 nomove = TRUE;
964 count = 0;
965 goto doESCkey;
966 }
967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
969 auto_format(FALSE, TRUE);
970 break;
971
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000972 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000973# ifdef FEAT_COMPL_FUNC
974 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100975 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000976 goto docomplete;
977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
979 auto_format(FALSE, TRUE);
980 inserted_space = FALSE;
981 break;
982
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000983 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 case K_LEFTMOUSE_NM:
985 case K_LEFTDRAG:
986 case K_LEFTRELEASE:
987 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100988 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 case K_MIDDLEMOUSE:
990 case K_MIDDLEDRAG:
991 case K_MIDDLERELEASE:
992 case K_RIGHTMOUSE:
993 case K_RIGHTDRAG:
994 case K_RIGHTRELEASE:
995 case K_X1MOUSE:
996 case K_X1DRAG:
997 case K_X1RELEASE:
998 case K_X2MOUSE:
999 case K_X2DRAG:
1000 case K_X2RELEASE:
1001 ins_mouse(c);
1002 break;
1003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001005 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 break;
1007
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001009 ins_mousescroll(MSCR_UP);
1010 break;
1011
1012 case K_MOUSELEFT: /* Scroll wheel left */
1013 ins_mousescroll(MSCR_LEFT);
1014 break;
1015
1016 case K_MOUSERIGHT: /* Scroll wheel right */
1017 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001019
Bram Moolenaarec2da362017-01-21 20:04:22 +01001020 case K_PS:
1021 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1022 if (cmdchar == K_PS)
1023 /* invoked from normal mode, bail out */
1024 goto doESCkey;
1025 break;
1026 case K_PE:
1027 /* Got K_PE without K_PS, ignore. */
1028 break;
1029
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001030#ifdef FEAT_GUI_TABLINE
1031 case K_TABLINE:
1032 case K_TABMENU:
1033 ins_tabline(c);
1034 break;
1035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 break;
1039
Bram Moolenaar754b5602006-02-09 23:53:20 +00001040 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001041 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001042 did_cursorhold = TRUE;
1043 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001044
Bram Moolenaar4f974752019-02-17 17:44:42 +01001045#ifdef FEAT_GUI_MSWIN
1046 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1047 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001048 case K_F4:
1049 if (mod_mask != MOD_MASK_ALT)
1050 goto normalchar;
1051 break;
1052#endif
1053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054#ifdef FEAT_GUI
1055 case K_VER_SCROLLBAR:
1056 ins_scroll();
1057 break;
1058
1059 case K_HOR_SCROLLBAR:
1060 ins_horscroll();
1061 break;
1062#endif
1063
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001064 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 case K_S_HOME:
1067 case K_C_HOME:
1068 ins_home(c);
1069 break;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case K_S_END:
1074 case K_C_END:
1075 ins_end(c);
1076 break;
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001079 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1080 ins_s_left();
1081 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001082 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 break;
1084
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001085 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 case K_C_LEFT:
1087 ins_s_left();
1088 break;
1089
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001090 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001091 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1092 ins_s_right();
1093 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001094 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 break;
1096
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 case K_C_RIGHT:
1099 ins_s_right();
1100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001103 if (pum_visible())
1104 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001105 if (mod_mask & MOD_MASK_SHIFT)
1106 ins_pageup();
1107 else
1108 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 break;
1110
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001111 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 case K_PAGEUP:
1113 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001114 if (pum_visible())
1115 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 ins_pageup();
1117 break;
1118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001119 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001120 if (pum_visible())
1121 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001122 if (mod_mask & MOD_MASK_SHIFT)
1123 ins_pagedown();
1124 else
1125 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 break;
1127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 case K_PAGEDOWN:
1130 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001131 if (pum_visible())
1132 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 ins_pagedown();
1134 break;
1135
1136#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 ins_drop();
1139 break;
1140#endif
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 c = TAB;
1144 /* FALLTHROUGH */
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001147#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001148 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 goto docomplete;
1150#endif
1151 inserted_space = FALSE;
1152 if (ins_tab())
1153 goto normalchar; /* insert TAB as a normal char */
1154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 c = CAR;
1159 /* FALLTHROUGH */
1160 case CAR:
1161 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001162#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 /* In a quickfix window a <CR> jumps to the error under the
1164 * cursor. */
1165 if (bt_quickfix(curbuf) && c == CAR)
1166 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001167 if (curwin->w_llist_ref == NULL) /* quickfix window */
1168 do_cmdline_cmd((char_u *)".cc");
1169 else /* location list window */
1170 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 break;
1172 }
1173#endif
1174#ifdef FEAT_CMDWIN
1175 if (cmdwin_type != 0)
1176 {
1177 /* Execute the command in the cmdline window. */
1178 cmdwin_result = CAR;
1179 goto doESCkey;
1180 }
1181#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001182#ifdef FEAT_JOB_CHANNEL
1183 if (bt_prompt(curbuf))
1184 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001185 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001186 if (!bt_prompt(curbuf))
1187 // buffer changed to a non-prompt buffer, get out of
1188 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001189 goto doESCkey;
1190 break;
1191 }
1192#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001193 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 goto doESCkey; /* out of memory */
1195 auto_format(FALSE, FALSE);
1196 inserted_space = FALSE;
1197 break;
1198
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001199 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001200 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001202 if (has_compl_option(TRUE))
1203 goto docomplete;
1204 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001206#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 c = ins_digraph();
1208 if (c == NUL)
1209 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001210#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001211 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Bram Moolenaar572cb562005-08-05 21:35:02 +00001213 case Ctrl_X: /* Enter CTRL-X mode */
1214 ins_ctrl_x();
1215 break;
1216
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001218 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 goto normalchar;
1220 goto docomplete;
1221
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001222 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001223 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 goto normalchar;
1225 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001226
1227 case 's': /* Spelling completion after ^X */
1228 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001229 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001230 goto normalchar;
1231 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001233 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001234 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1237 if (p_im)
1238 {
1239 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1240 break;
1241 goto doESCkey;
1242 }
1243 goto normalchar;
1244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 /* FALLTHROUGH */
1246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 case Ctrl_N:
1249 /* if 'complete' is empty then plain ^P is no longer special,
1250 * but it is under other ^X modes */
1251 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001252 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 goto normalchar;
1255
1256docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001257 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001258#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001259 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001260#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001261 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001262 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001263#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001264 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001265#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001266 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 case Ctrl_Y: /* copy from previous line or scroll down */
1270 case Ctrl_E: /* copy from next line or scroll up */
1271 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 break;
1273
1274 default:
1275#ifdef UNIX
1276 if (c == intr_char) /* special interrupt char */
1277 goto do_intr;
1278#endif
1279
Bram Moolenaare659c952011-05-19 17:25:41 +02001280normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001282 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001284#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001285 if (!p_paste)
1286 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001287 /* Trigger InsertCharPre. */
1288 char_u *str = do_insert_char_pre(c);
1289 char_u *p;
1290
1291 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001292 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001293 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001294 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001295 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001296 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001297 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001298 c = PTR2CHAR(p);
1299 if (c == CAR || c == K_KENTER || c == NL)
1300 ins_eol(c);
1301 else
1302 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001303 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001304 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001305 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001306 vim_free(str);
1307 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001308 }
1309
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001310 /* If the new value is already inserted or an empty string
1311 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001312 if (c == NUL)
1313 break;
1314 }
1315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316#ifdef FEAT_SMARTINDENT
1317 /* Try to perform smart-indenting. */
1318 ins_try_si(c);
1319#endif
1320
1321 if (c == ' ')
1322 {
1323 inserted_space = TRUE;
1324#ifdef FEAT_CINDENT
1325 if (inindent(0))
1326 can_cindent = FALSE;
1327#endif
1328 if (Insstart_blank_vcol == MAXCOL
1329 && curwin->w_cursor.lnum == Insstart.lnum)
1330 Insstart_blank_vcol = get_nolist_virtcol();
1331 }
1332
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001333 /* Insert a normal character and check for abbreviations on a
1334 * special character. Let CTRL-] expand abbreviations without
1335 * inserting it. */
1336 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001337 // Add ABBR_OFF for characters above 0x100, this is
1338 // what check_abbr() expects.
1339 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1340 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
1342 insert_special(c, FALSE, FALSE);
1343#ifdef FEAT_RIGHTLEFT
1344 revins_legal++;
1345 revins_chars++;
1346#endif
1347 }
1348
1349 auto_format(FALSE, TRUE);
1350
1351#ifdef FEAT_FOLDING
1352 /* When inserting a character the cursor line must never be in a
1353 * closed fold. */
1354 foldOpenCursor();
1355#endif
1356 break;
1357 } /* end of switch (c) */
1358
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001359 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001360 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001361#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001362 /* but not in CTRL-X mode, a script can't restore the state */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001363 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001364#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001365 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001366 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 /* If the cursor was moved we didn't just insert a space */
1369 if (arrow_used)
1370 inserted_space = FALSE;
1371
1372#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001373 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
1375force_cindent:
1376 /*
1377 * Indent now if a key was typed that is in 'cinkeys'.
1378 */
1379 if (in_cinkeys(c, ' ', line_is_white))
1380 {
1381 if (stop_arrow() == OK)
1382 /* re-indent the current line */
1383 do_c_expr_indent();
1384 }
1385 }
1386#endif /* FEAT_CINDENT */
1387
1388 } /* for (;;) */
1389 /* NOTREACHED */
1390}
1391
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001392 int
1393ins_need_undo_get(void)
1394{
1395 return ins_need_undo;
1396}
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398/*
1399 * Redraw for Insert mode.
1400 * This is postponed until getting the next character to make '$' in the 'cpo'
1401 * option work correctly.
1402 * Only redraw when there are no characters available. This speeds up
1403 * inserting sequences of characters (e.g., for CTRL-R).
1404 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001405 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001406ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001408#ifdef FEAT_CONCEAL
1409 linenr_T conceal_old_cursor_line = 0;
1410 linenr_T conceal_new_cursor_line = 0;
1411 int conceal_update_lines = FALSE;
1412#endif
1413
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001414 if (char_avail())
1415 return;
1416
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001417 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1418 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001419 if (ready && (has_cursormovedI()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001420# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001421 || popup_visible
1422# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001423# if defined(FEAT_CONCEAL)
1424 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001425# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001426 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001427 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001428 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001429 {
1430# ifdef FEAT_SYN_HL
1431 /* Need to update the screen first, to make sure syntax
1432 * highlighting is correct after making a change (e.g., inserting
1433 * a "(". The autocommand may also require a redraw, so it's done
1434 * again below, unfortunately. */
1435 if (syntax_present(curwin) && must_redraw)
1436 update_screen(0);
1437# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001438 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001439 {
1440 /* Make sure curswant is correct, an autocommand may call
1441 * getcurpos(). */
1442 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001443 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001444 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001445#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001446 if (popup_visible)
1447 popup_check_cursor_pos();
1448#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001449# ifdef FEAT_CONCEAL
1450 if (curwin->w_p_cole > 0)
1451 {
1452 conceal_old_cursor_line = last_cursormoved.lnum;
1453 conceal_new_cursor_line = curwin->w_cursor.lnum;
1454 conceal_update_lines = TRUE;
1455 }
1456# endif
1457 last_cursormoved = curwin->w_cursor;
1458 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001459
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001460 /* Trigger TextChangedI if b_changedtick differs. */
1461 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001462 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001463 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001464 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001465 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001466 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001467
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001468 // save and restore curwin and curbuf, in case the autocmd changes them
1469 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001470 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001471 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001472 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001473 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1474 u_save(curwin->w_cursor.lnum,
1475 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001477
Bram Moolenaar5a093432018-02-10 18:15:19 +01001478 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1479 * TextChangedI will need to trigger for backwards compatibility, thus use
1480 * different b_last_changedtick* variables. */
1481 if (ready && has_textchangedP()
1482 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1483 && pum_visible())
1484 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001485 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001486 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001487
1488 // save and restore curwin and curbuf, in case the autocmd changes them
1489 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001490 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001491 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001492 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001493 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1494 u_save(curwin->w_cursor.lnum,
1495 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001496 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001497
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001498 // Trigger SafeState if nothing is pending.
1499 may_trigger_safestate(ready
1500 && !ins_compl_active()
1501 && !pum_visible());
1502
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001503#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001504 if ((conceal_update_lines
1505 && (conceal_old_cursor_line != conceal_new_cursor_line
1506 || conceal_cursor_line(curwin)))
1507 || need_cursor_line_redraw)
1508 {
1509 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001510 redrawWinline(curwin, conceal_old_cursor_line);
1511 redrawWinline(curwin, conceal_new_cursor_line == 0
1512 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001513 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001514 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001515 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001516#endif
1517 if (must_redraw)
1518 update_screen(0);
1519 else if (clear_cmdline || redraw_cmdline)
1520 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001521 showruler(FALSE);
1522 setcursor();
1523 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524}
1525
1526/*
1527 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1528 */
1529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001530ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001533 int did_putchar = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001534 int prev_mod_mask = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
1536 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001537 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
1539 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001542 did_putchar = TRUE;
1543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1545
1546#ifdef FEAT_CMDL_INFO
1547 add_to_showcmd_c(Ctrl_V);
1548#endif
1549
1550 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001551 if (did_putchar)
1552 /* when the line fits in 'columns' the '^' is at the start of the next
1553 * line and will not removed by the redraw */
1554 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#ifdef FEAT_CMDL_INFO
1556 clear_showcmd();
1557#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001558
1559 if ((c == ESC || c == CSI) && !(prev_mod_mask & MOD_MASK_SHIFT))
1560 // Using CTRL-V: Change any modifyOtherKeys ESC sequence to a normal
1561 // key. Don't do this for CTRL-SHIFT-V.
1562 c = decodeModifyOtherKeys(c);
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 insert_special(c, FALSE, TRUE);
1565#ifdef FEAT_RIGHTLEFT
1566 revins_chars++;
1567 revins_legal++;
1568#endif
1569}
1570
1571/*
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001572 * After getting an ESC or CSI for a literal key: If the typeahead buffer
1573 * contains a modifyOtherKeys sequence then decode it and return the result.
1574 * Otherwise return "c".
1575 * Note that this doesn't wait for characters, they must be in the typeahead
1576 * buffer already.
1577 */
1578 int
1579decodeModifyOtherKeys(int c)
1580{
1581 char_u *p = typebuf.tb_buf + typebuf.tb_off;
1582 int idx;
1583 int form = 0;
1584 int argidx = 0;
1585 int arg[2] = {0, 0};
1586
1587 // Recognize:
1588 // form 0: {lead}{key};{modifier}u
1589 // form 1: {lead}27;{modifier};{key}~
1590 if ((c == CSI || (c == ESC && *p == '[')) && typebuf.tb_len >= 4)
1591 {
1592 idx = (*p == '[');
1593 if (p[idx] == '2' && p[idx + 1] == '7' && p[idx + 2] == ';')
1594 {
1595 form = 1;
1596 idx += 3;
1597 }
1598 while (idx < typebuf.tb_len && argidx < 2)
1599 {
1600 if (p[idx] == ';')
1601 ++argidx;
1602 else if (VIM_ISDIGIT(p[idx]))
1603 arg[argidx] = arg[argidx] * 10 + (p[idx] - '0');
1604 else
1605 break;
1606 ++idx;
1607 }
1608 if (idx < typebuf.tb_len
1609 && p[idx] == (form == 1 ? '~' : 'u')
1610 && argidx == 1)
1611 {
1612 // Match, consume the code.
1613 typebuf.tb_off += idx + 1;
1614 typebuf.tb_len -= idx + 1;
1615
1616 mod_mask = decode_modifiers(arg[!form]);
1617 c = merge_modifyOtherKeys(arg[form]);
1618 }
1619 }
1620
1621 return c;
1622}
1623
1624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 * Put a character directly onto the screen. It's not stored in a buffer.
1626 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1627 */
1628static int pc_status;
1629#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1630#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1631#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1632#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634static int pc_attr;
1635static int pc_row;
1636static int pc_col;
1637
1638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
1641 int attr;
1642
1643 if (ScreenLines != NULL)
1644 {
1645 update_topline(); /* just in case w_topline isn't valid */
1646 validate_cursor();
1647 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001648 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 else
1650 attr = 0;
1651 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001652 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#ifdef FEAT_RIGHTLEFT
1655 if (curwin->w_p_rl)
1656 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001657 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (has_mbyte)
1659 {
1660 int fix_col = mb_fix_col(pc_col, pc_row);
1661
1662 if (fix_col != pc_col)
1663 {
1664 screen_putchar(' ', pc_row, fix_col, attr);
1665 --curwin->w_wcol;
1666 pc_status = PC_STATUS_RIGHT;
1667 }
1668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 else
1671#endif
1672 {
1673 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (mb_lefthalve(pc_row, pc_col))
1675 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
1677
1678 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1682 pc_status = PC_STATUS_SET;
1683 }
1684 screen_putchar(c, pc_row, pc_col, attr);
1685 }
1686}
1687
Bram Moolenaarf2732452018-06-03 14:47:35 +02001688#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1689/*
1690 * Return the effective prompt for the current buffer.
1691 */
1692 char_u *
1693prompt_text(void)
1694{
1695 if (curbuf->b_prompt_text == NULL)
1696 return (char_u *)"% ";
1697 return curbuf->b_prompt_text;
1698}
1699
1700/*
1701 * Prepare for prompt mode: Make sure the last line has the prompt text.
1702 * Move the cursor to this line.
1703 */
1704 static void
1705init_prompt(int cmdchar_todo)
1706{
1707 char_u *prompt = prompt_text();
1708 char_u *text;
1709
1710 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1711 text = ml_get_curline();
1712 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1713 {
1714 // prompt is missing, insert it or append a line with it
1715 if (*text == NUL)
1716 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1717 else
1718 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1719 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1720 coladvance((colnr_T)MAXCOL);
1721 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1722 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001723
1724 // Insert always starts after the prompt, allow editing text after it.
1725 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1726 || Insstart_orig.col != (int)STRLEN(prompt))
1727 {
1728 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001729 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001730 Insstart_orig = Insstart;
1731 Insstart_textlen = Insstart.col;
1732 Insstart_blank_vcol = MAXCOL;
1733 arrow_used = FALSE;
1734 }
1735
Bram Moolenaarf2732452018-06-03 14:47:35 +02001736 if (cmdchar_todo == 'A')
1737 coladvance((colnr_T)MAXCOL);
1738 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001739 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001740 /* Make sure the cursor is in a valid position. */
1741 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001742}
1743
1744/*
1745 * Return TRUE if the cursor is in the editable position of the prompt line.
1746 */
1747 int
1748prompt_curpos_editable()
1749{
1750 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1751 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1752}
1753#endif
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755/*
1756 * Undo the previous edit_putchar().
1757 */
1758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001759edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (pc_status == PC_STATUS_RIGHT)
1764 ++curwin->w_wcol;
1765 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001766 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1769 }
1770}
1771
1772/*
1773 * Called when p_dollar is set: display a '$' at the end of the changed text
1774 * Only works when cursor is in the line that changes.
1775 */
1776 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001777display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 colnr_T save_col;
1780
1781 if (!redrawing())
1782 return;
1783
1784 cursor_off();
1785 save_col = curwin->w_cursor.col;
1786 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (has_mbyte)
1788 {
1789 char_u *p;
1790
1791 /* If on the last byte of a multi-byte move to the first byte. */
1792 p = ml_get_curline();
1793 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001796 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
1798 edit_putchar('$', FALSE);
1799 dollar_vcol = curwin->w_virtcol;
1800 }
1801 curwin->w_cursor.col = save_col;
1802}
1803
1804/*
1805 * Call this function before moving the cursor from the normal insert position
1806 * in insert mode.
1807 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001809undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001811 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001813 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001814 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816}
1817
1818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 * Truncate the space at the end of a line. This is to be used only in an
1820 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1821 * modes.
1822 */
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 int i;
1827
1828 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001829 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
1831 if (State & REPLACE_FLAG)
1832 replace_join(0); /* remove a NUL from the replace stack */
1833 }
1834 line[i + 1] = NUL;
1835}
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837/*
1838 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1839 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001840 * Will attempt not to go before "col" even when there is a composing
1841 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 */
1843 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001844backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845{
1846 while ((int)curwin->w_cursor.col > col)
1847 {
1848 curwin->w_cursor.col--;
1849 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001850 replace_do_bs(col);
1851 else if (!del_char_after_col(col))
1852 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001856/*
1857 * Like del_char(), but make sure not to go before column "limit_col".
1858 * Only matters when there are composing characters.
1859 * Return TRUE when something was deleted.
1860 */
1861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001862del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001863{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001864 if (enc_utf8 && limit_col >= 0)
1865 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001866 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001867
1868 /* Make sure the cursor is at the start of a character, but
1869 * skip forward again when going too far back because of a
1870 * composing character. */
1871 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001872 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001873 {
1874 int l = utf_ptr2len(ml_get_cursor());
1875
1876 if (l == 0) /* end of line */
1877 break;
1878 curwin->w_cursor.col += l;
1879 }
1880 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1881 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001882 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001883 }
1884 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001885 (void)del_char(FALSE);
1886 return TRUE;
1887}
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889/*
1890 * Next character is interpreted literally.
1891 * A one, two or three digit decimal number is interpreted as its byte value.
1892 * If one or two digits are entered, the next character is given to vungetc().
1893 * For Unicode a character > 255 may be returned.
1894 */
1895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001896get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
1898 int cc;
1899 int nc;
1900 int i;
1901 int hex = FALSE;
1902 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 if (got_int)
1906 return Ctrl_C;
1907
1908#ifdef FEAT_GUI
1909 /*
1910 * In GUI there is no point inserting the internal code for a special key.
1911 * It is more useful to insert the string "<KEY>" instead. This would
1912 * probably be useful in a text window too, but it would not be
1913 * vi-compatible (maybe there should be an option for it?) -- webb
1914 */
1915 if (gui.in_use)
1916 ++allow_keys;
1917#endif
1918#ifdef USE_ON_FLY_SCROLL
1919 dont_scroll = TRUE; /* disallow scrolling here */
1920#endif
1921 ++no_mapping; /* don't map the next key hits */
1922 cc = 0;
1923 i = 0;
1924 for (;;)
1925 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001926 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001928 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 add_to_showcmd(nc);
1930#endif
1931 if (nc == 'x' || nc == 'X')
1932 hex = TRUE;
1933 else if (nc == 'o' || nc == 'O')
1934 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 else if (nc == 'u' || nc == 'U')
1936 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 else
1938 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001939 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 if (!vim_isxdigit(nc))
1942 break;
1943 cc = cc * 16 + hex2nr(nc);
1944 }
1945 else if (octal)
1946 {
1947 if (nc < '0' || nc > '7')
1948 break;
1949 cc = cc * 8 + nc - '0';
1950 }
1951 else
1952 {
1953 if (!VIM_ISDIGIT(nc))
1954 break;
1955 cc = cc * 10 + nc - '0';
1956 }
1957
1958 ++i;
1959 }
1960
Bram Moolenaar13505972019-01-24 15:04:48 +01001961 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 cc = 255; /* limit range to 0-255 */
1963 nc = 0;
1964
1965 if (hex) /* hex: up to two chars */
1966 {
1967 if (i >= 2)
1968 break;
1969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 else if (unicode) /* Unicode: up to four or eight chars */
1971 {
1972 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1973 break;
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 else if (i >= 3) /* decimal or octal: up to three chars */
1976 break;
1977 }
1978 if (i == 0) /* no number entered */
1979 {
1980 if (nc == K_ZERO) /* NUL is stored as NL */
1981 {
1982 cc = '\n';
1983 nc = 0;
1984 }
1985 else
1986 {
1987 cc = nc;
1988 nc = 0;
1989 }
1990 }
1991
1992 if (cc == 0) /* NUL is stored as NL */
1993 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001994 if (enc_dbcs && (cc & 0xff) == 0)
1995 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
1996 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
1998 --no_mapping;
1999#ifdef FEAT_GUI
2000 if (gui.in_use)
2001 --allow_keys;
2002#endif
2003 if (nc)
2004 vungetc(nc);
2005 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
2006 return cc;
2007}
2008
2009/*
2010 * Insert character, taking care of special keys and mod_mask
2011 */
2012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002013insert_special(
2014 int c,
2015 int allow_modmask,
2016 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 char_u *p;
2019 int len;
2020
2021 /*
2022 * Special function key, translate into "<Key>". Up to the last '>' is
2023 * inserted with ins_str(), so as not to replace characters in replace
2024 * mode.
2025 * Only use mod_mask for special keys, to avoid things like <S-Space>,
2026 * unless 'allow_modmask' is TRUE.
2027 */
Bram Moolenaard0573012017-10-28 21:11:06 +02002028#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 /* Command-key never produces a normal key */
2030 if (mod_mask & MOD_MASK_CMD)
2031 allow_modmask = TRUE;
2032#endif
2033 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
2034 {
2035 p = get_special_key_name(c, mod_mask);
2036 len = (int)STRLEN(p);
2037 c = p[len - 1];
2038 if (len > 2)
2039 {
2040 if (stop_arrow() == FAIL)
2041 return;
2042 p[len - 1] = NUL;
2043 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002044 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 ctrlv = FALSE;
2046 }
2047 }
2048 if (stop_arrow() == OK)
2049 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2050}
2051
2052/*
2053 * Special characters in this context are those that need processing other
2054 * than the simple insertion that can be performed here. This includes ESC
2055 * which terminates the insert, and CR/NL which need special processing to
2056 * open up a new line. This routine tries to optimize insertions performed by
2057 * the "redo", "undo" or "put" commands, so it needs to know when it should
2058 * stop and defer processing to the "normal" mechanism.
2059 * '0' and '^' are special, because they can be followed by CTRL-D.
2060 */
2061#ifdef EBCDIC
2062# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2063#else
2064# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2065#endif
2066
Bram Moolenaar13505972019-01-24 15:04:48 +01002067#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002069/*
2070 * "flags": INSCHAR_FORMAT - force formatting
2071 * INSCHAR_CTRLV - char typed just after CTRL-V
2072 * INSCHAR_NO_FEX - don't use 'formatexpr'
2073 *
2074 * NOTE: passes the flags value straight through to internal_format() which,
2075 * beside INSCHAR_FORMAT (above), is also looking for these:
2076 * INSCHAR_DO_COM - format comments
2077 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002080insertchar(
2081 int c, /* character to insert or NUL */
2082 int flags, /* INSCHAR_FORMAT, etc. */
2083 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002088 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002090 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093 /*
2094 * Try to break the line in two or more pieces when:
2095 * - Always do this if we have been called to do formatting only.
2096 * - Always do this when 'formatoptions' has the 'a' flag and the line
2097 * ends in white space.
2098 * - Otherwise:
2099 * - Don't do this if inserting a blank
2100 * - Don't do this if an existing character is being replaced, unless
2101 * we're in VREPLACE mode.
2102 * - Do this if the cursor is not on the line where insert started
2103 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2104 * before the insert.
2105 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2106 * before 'textwidth'
2107 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002108 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002109 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002110 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 && *ml_get_cursor() != NUL)
2114 && (curwin->w_cursor.lnum != Insstart.lnum
2115 || ((!has_format_option(FO_INS_LONG)
2116 || Insstart_textlen <= (colnr_T)textwidth)
2117 && (!fo_ins_blank
2118 || Insstart_blank_vcol <= (colnr_T)textwidth
2119 ))))))
2120 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002121 /* Format with 'formatexpr' when it's set. Use internal formatting
2122 * when 'formatexpr' isn't set or it returns non-zero. */
2123#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002124 int do_internal = TRUE;
2125 colnr_T virtcol = get_nolist_virtcol()
2126 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002127
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002128 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2129 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002130 {
2131 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
2132 /* It may be required to save for undo again, e.g. when setline()
2133 * was called. */
2134 ins_need_undo = TRUE;
2135 }
2136 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002138 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002140
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (c == NUL) /* only formatting was wanted */
2142 return;
2143
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002144 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (did_ai && (int)c == end_comment_pending)
2146 {
2147 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002148 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 int middle_len, end_len;
2150 int i;
2151
2152 /*
2153 * Need to remove existing (middle) comment leader and insert end
2154 * comment leader. First, check what comment leader we can find.
2155 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002156 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002157 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002159 // Skip middle-comment string
2160 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 ++p;
2162 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002163 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002164 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 --middle_len;
2166
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002167 // Find the end-comment string
2168 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 ++p;
2170 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2171
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002172 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002174 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 ;
2176 i++;
2177
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002178 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 i -= middle_len;
2180
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002181 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2183 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002184 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 backspace_until_column(i);
2186
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002187 // Insert the end-comment string, except for the last
2188 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 ins_bytes_len(lead_end, end_len - 1);
2190 }
2191 }
2192 }
2193 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 did_ai = FALSE;
2196#ifdef FEAT_SMARTINDENT
2197 did_si = FALSE;
2198 can_si = FALSE;
2199 can_si_back = FALSE;
2200#endif
2201
2202 /*
2203 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2204 * This speeds up normal text input considerably.
2205 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2206 * need to re-indent at a ':', or any other character (but not what
2207 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002208 * Don't do this when there an InsertCharPre autocommand is defined,
2209 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002210 * Do the check for InsertCharPre before the call to vpeekc() because the
2211 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 */
2213#ifdef USE_ON_FLY_SCROLL
2214 dont_scroll = FALSE; /* allow scrolling here */
2215#endif
2216
2217 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002219 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 && vpeekc() != NUL
2221 && !(State & REPLACE_FLAG)
2222#ifdef FEAT_CINDENT
2223 && !cindent_on()
2224#endif
2225#ifdef FEAT_RIGHTLEFT
2226 && !p_ri
2227#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002228 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230#define INPUT_BUFLEN 100
2231 char_u buf[INPUT_BUFLEN + 1];
2232 int i;
2233 colnr_T virtcol = 0;
2234
2235 buf[0] = c;
2236 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002237 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 virtcol = get_nolist_virtcol();
2239 /*
2240 * Stop the string when:
2241 * - no more chars available
2242 * - finding a special character (command key)
2243 * - buffer is full
2244 * - running into the 'textwidth' boundary
2245 * - need to check for abbreviation: A non-word char after a word-char
2246 */
2247 while ( (c = vpeekc()) != NUL
2248 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 && i < INPUT_BUFLEN
2251 && (textwidth == 0
2252 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2253 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2254 {
2255#ifdef FEAT_RIGHTLEFT
2256 c = vgetc();
2257 if (p_hkmap && KeyTyped)
2258 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 buf[i++] = c;
2260#else
2261 buf[i++] = vgetc();
2262#endif
2263 }
2264
2265#ifdef FEAT_DIGRAPHS
2266 do_digraph(-1); /* clear digraphs */
2267 do_digraph(buf[i-1]); /* may be the start of a digraph */
2268#endif
2269 buf[i] = NUL;
2270 ins_str(buf);
2271 if (flags & INSCHAR_CTRLV)
2272 {
2273 redo_literal(*buf);
2274 i = 1;
2275 }
2276 else
2277 i = 0;
2278 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002279 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281 else
2282 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002283 int cc;
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2286 {
2287 char_u buf[MB_MAXBYTES + 1];
2288
2289 (*mb_char2bytes)(c, buf);
2290 buf[cc] = NUL;
2291 ins_char_bytes(buf, cc);
2292 AppendCharToRedobuff(c);
2293 }
2294 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 ins_char(c);
2297 if (flags & INSCHAR_CTRLV)
2298 redo_literal(c);
2299 else
2300 AppendCharToRedobuff(c);
2301 }
2302 }
2303}
2304
2305/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002306 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002307 *
2308 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2309 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002310 */
2311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002312internal_format(
2313 int textwidth,
2314 int second_indent,
2315 int flags,
2316 int format_only,
2317 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002318{
2319 int cc;
2320 int save_char = NUL;
2321 int haveto_redraw = FALSE;
2322 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002323 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002324 int fo_white_par = has_format_option(FO_WHITE_PAR);
2325 int first_line = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002326 colnr_T leader_len;
2327 int no_leader = FALSE;
2328 int do_comments = (flags & INSCHAR_DO_COM);
Bram Moolenaar0026d472014-09-09 16:32:39 +02002329#ifdef FEAT_LINEBREAK
2330 int has_lbr = curwin->w_p_lbr;
2331
2332 /* make sure win_lbr_chartabsize() counts correctly */
2333 curwin->w_p_lbr = FALSE;
2334#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002335
2336 /*
2337 * When 'ai' is off we don't want a space under the cursor to be
2338 * deleted. Replace it with an 'x' temporarily.
2339 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002340 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002341 {
2342 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002343 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002344 {
2345 save_char = cc;
2346 pchar_cursor('x');
2347 }
2348 }
2349
2350 /*
2351 * Repeat breaking lines, until the current line is not too long.
2352 */
2353 while (!got_int)
2354 {
2355 int startcol; /* Cursor column at entry */
2356 int wantcol; /* column at textwidth border */
2357 int foundcol; /* column for start of spaces */
2358 int end_foundcol = 0; /* column for start of word */
2359 colnr_T len;
2360 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002361 int orig_col = 0;
2362 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002363 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002364 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002365 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002366
Bram Moolenaar97b98102009-11-17 16:41:01 +00002367 virtcol = get_nolist_virtcol()
2368 + char2cells(c != NUL ? c : gchar_cursor());
2369 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002370 break;
2371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002372 if (no_leader)
2373 do_comments = FALSE;
2374 else if (!(flags & INSCHAR_FORMAT)
2375 && has_format_option(FO_WRAP_COMS))
2376 do_comments = TRUE;
2377
2378 /* Don't break until after the comment leader */
2379 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002380 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002381 else
2382 leader_len = 0;
2383
2384 /* If the line doesn't start with a comment leader, then don't
2385 * start one in a following broken line. Avoids that a %word
2386 * moved to the start of the next line causes all following lines
2387 * to start with %. */
2388 if (leader_len == 0)
2389 no_leader = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002390 if (!(flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002391 && leader_len == 0
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002392 && !has_format_option(FO_WRAP))
2393
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002394 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002395 if ((startcol = curwin->w_cursor.col) == 0)
2396 break;
2397
2398 /* find column of textwidth border */
2399 coladvance((colnr_T)textwidth);
2400 wantcol = curwin->w_cursor.col;
2401
Bram Moolenaar97b98102009-11-17 16:41:01 +00002402 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002403 foundcol = 0;
2404
2405 /*
2406 * Find position to break at.
2407 * Stop at first entered white when 'formatoptions' has 'v'
2408 */
2409 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002410 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002411 || curwin->w_cursor.lnum != Insstart.lnum
2412 || curwin->w_cursor.col >= Insstart.col)
2413 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002414 if (curwin->w_cursor.col == startcol && c != NUL)
2415 cc = c;
2416 else
2417 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002418 if (WHITECHAR(cc))
2419 {
2420 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002421 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002422
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002423 // find start of sequence of blanks
2424 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002425 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2426 {
2427 dec_cursor();
2428 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002429
2430 // Increment count of how many whitespace chars in this
2431 // group; we only need to know if it's more than one.
2432 if (wcc < 2)
2433 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002434 }
2435 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
2436 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002437
2438 // Don't break after a period when 'formatoptions' has 'p' and
2439 // there are less than two spaces.
2440 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2441 continue;
2442
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002443 /* Don't break until after the comment leader */
2444 if (curwin->w_cursor.col < leader_len)
2445 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002446 if (has_format_option(FO_ONE_LETTER))
2447 {
2448 /* do not break after one-letter words */
2449 if (curwin->w_cursor.col == 0)
2450 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002451 /* do not break "#a b" when 'tw' is 2 */
2452 if (curwin->w_cursor.col <= leader_len)
2453 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002454 col = curwin->w_cursor.col;
2455 dec_cursor();
2456 cc = gchar_cursor();
2457
2458 if (WHITECHAR(cc))
2459 continue; /* one-letter, continue */
2460 curwin->w_cursor.col = col;
2461 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002462
2463 inc_cursor();
2464
2465 end_foundcol = end_col + 1;
2466 foundcol = curwin->w_cursor.col;
2467 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002468 break;
2469 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002470 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002471 {
2472 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002473 if (curwin->w_cursor.col != startcol)
2474 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002475 /* Don't break until after the comment leader */
2476 if (curwin->w_cursor.col < leader_len)
2477 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002478 col = curwin->w_cursor.col;
2479 inc_cursor();
2480 /* Don't change end_foundcol if already set. */
2481 if (foundcol != curwin->w_cursor.col)
2482 {
2483 foundcol = curwin->w_cursor.col;
2484 end_foundcol = foundcol;
2485 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2486 break;
2487 }
2488 curwin->w_cursor.col = col;
2489 }
2490
2491 if (curwin->w_cursor.col == 0)
2492 break;
2493
2494 col = curwin->w_cursor.col;
2495
2496 dec_cursor();
2497 cc = gchar_cursor();
2498
2499 if (WHITECHAR(cc))
2500 continue; /* break with space */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002501 /* Don't break until after the comment leader */
2502 if (curwin->w_cursor.col < leader_len)
2503 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002504
2505 curwin->w_cursor.col = col;
2506
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002507 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002508 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002509 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2510 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002511 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002512 if (curwin->w_cursor.col == 0)
2513 break;
2514 dec_cursor();
2515 }
2516
2517 if (foundcol == 0) /* no spaces, cannot break line */
2518 {
2519 curwin->w_cursor.col = startcol;
2520 break;
2521 }
2522
2523 /* Going to break the line, remove any "$" now. */
2524 undisplay_dollar();
2525
2526 /*
2527 * Offset between cursor position and line break is used by replace
2528 * stack functions. VREPLACE does not use this, and backspaces
2529 * over the text instead.
2530 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002531 if (State & VREPLACE_FLAG)
2532 orig_col = startcol; /* Will start backspacing from here */
2533 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002534 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002535
2536 /*
2537 * adjust startcol for spaces that will be deleted and
2538 * characters that will remain on top line
2539 */
2540 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002541 while ((cc = gchar_cursor(), WHITECHAR(cc))
2542 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002543 inc_cursor();
2544 startcol -= curwin->w_cursor.col;
2545 if (startcol < 0)
2546 startcol = 0;
2547
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002548 if (State & VREPLACE_FLAG)
2549 {
2550 /*
2551 * In VREPLACE mode, we will backspace over the text to be
2552 * wrapped, so save a copy now to put on the next line.
2553 */
2554 saved_text = vim_strsave(ml_get_cursor());
2555 curwin->w_cursor.col = orig_col;
2556 if (saved_text == NULL)
2557 break; /* Can't do it, out of memory */
2558 saved_text[startcol] = NUL;
2559
2560 /* Backspace over characters that will move to the next line */
2561 if (!fo_white_par)
2562 backspace_until_column(foundcol);
2563 }
2564 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002565 {
2566 /* put cursor after pos. to break line */
2567 if (!fo_white_par)
2568 curwin->w_cursor.col = foundcol;
2569 }
2570
2571 /*
2572 * Split the line just before the margin.
2573 * Only insert/delete lines, but don't really redraw the window.
2574 */
2575 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2576 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002577 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002578 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002579 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2580 if (!(flags & INSCHAR_COM_LIST))
2581 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002582
2583 replace_offset = 0;
2584 if (first_line)
2585 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002586 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002587 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002588 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002589 * This section is for auto-wrap of numeric lists. When not
2590 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2591 * flag will be set and open_line() will handle it (as seen
2592 * above). The code here (and in get_number_indent()) will
2593 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002594 */
2595 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002596 second_indent =
2597 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002598 if (second_indent >= 0)
2599 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002600 if (State & VREPLACE_FLAG)
2601 change_indent(INDENT_SET, second_indent,
2602 FALSE, NUL, TRUE);
2603 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002604 if (leader_len > 0 && second_indent - leader_len > 0)
2605 {
2606 int i;
2607 int padding = second_indent - leader_len;
2608
2609 /* We started at the first_line of a numbered list
2610 * that has a comment. the open_line() function has
2611 * inserted the proper comment leader and positioned
2612 * the cursor at the end of the split line. Now we
2613 * add the additional whitespace needed after the
2614 * comment leader for the numbered list. */
2615 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002616 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002617 }
2618 else
2619 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002620 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002621 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002622 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002623 }
2624 first_line = FALSE;
2625 }
2626
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002627 if (State & VREPLACE_FLAG)
2628 {
2629 /*
2630 * In VREPLACE mode we have backspaced over the text to be
2631 * moved, now we re-insert it into the new line.
2632 */
2633 ins_bytes(saved_text);
2634 vim_free(saved_text);
2635 }
2636 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002637 {
2638 /*
2639 * Check if cursor is not past the NUL off the line, cindent
2640 * may have added or removed indent.
2641 */
2642 curwin->w_cursor.col += startcol;
2643 len = (colnr_T)STRLEN(ml_get_curline());
2644 if (curwin->w_cursor.col > len)
2645 curwin->w_cursor.col = len;
2646 }
2647
2648 haveto_redraw = TRUE;
2649#ifdef FEAT_CINDENT
2650 can_cindent = TRUE;
2651#endif
2652 /* moved the cursor, don't autoindent or cindent now */
2653 did_ai = FALSE;
2654#ifdef FEAT_SMARTINDENT
2655 did_si = FALSE;
2656 can_si = FALSE;
2657 can_si_back = FALSE;
2658#endif
2659 line_breakcheck();
2660 }
2661
2662 if (save_char != NUL) /* put back space after cursor */
2663 pchar_cursor(save_char);
2664
Bram Moolenaar0026d472014-09-09 16:32:39 +02002665#ifdef FEAT_LINEBREAK
2666 curwin->w_p_lbr = has_lbr;
2667#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002668 if (!format_only && haveto_redraw)
2669 {
2670 update_topline();
2671 redraw_curbuf_later(VALID);
2672 }
2673}
2674
2675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 * Called after inserting or deleting text: When 'formatoptions' includes the
2677 * 'a' flag format from the current line until the end of the paragraph.
2678 * Keep the cursor at the same position relative to the text.
2679 * The caller must have saved the cursor line for undo, following ones will be
2680 * saved here.
2681 */
2682 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002683auto_format(
2684 int trailblank, /* when TRUE also format with trailing blank */
2685 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 pos_T pos;
2688 colnr_T len;
2689 char_u *old;
2690 char_u *new, *pnew;
2691 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002692 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 if (!has_format_option(FO_AUTO))
2695 return;
2696
2697 pos = curwin->w_cursor;
2698 old = ml_get_curline();
2699
2700 /* may remove added space */
2701 check_auto_format(FALSE);
2702
2703 /* Don't format in Insert mode when the cursor is on a trailing blank, the
2704 * user might insert normal text next. Also skip formatting when "1" is
2705 * in 'formatoptions' and there is a single character before the cursor.
2706 * Otherwise the line would be broken and when typing another non-white
2707 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002708 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (*old != NUL && !trailblank && wasatend)
2710 {
2711 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002712 cc = gchar_cursor();
2713 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2714 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002716 cc = gchar_cursor();
2717 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
2719 curwin->w_cursor = pos;
2720 return;
2721 }
2722 curwin->w_cursor = pos;
2723 }
2724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
2726 * comments. */
2727 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002728 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 /*
2732 * May start formatting in a previous line, so that after "x" a word is
2733 * moved to the previous line if it fits there now. Only when this is not
2734 * the start of a paragraph.
2735 */
2736 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2737 {
2738 --curwin->w_cursor.lnum;
2739 if (u_save_cursor() == FAIL)
2740 return;
2741 }
2742
2743 /*
2744 * Do the formatting and restore the cursor position. "saved_cursor" will
2745 * be adjusted for the text formatting.
2746 */
2747 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002748 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 curwin->w_cursor = saved_cursor;
2750 saved_cursor.lnum = 0;
2751
2752 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2753 {
2754 /* "cannot happen" */
2755 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2756 coladvance((colnr_T)MAXCOL);
2757 }
2758 else
2759 check_cursor_col();
2760
2761 /* Insert mode: If the cursor is now after the end of the line while it
2762 * previously wasn't, the line was broken. Because of the rule above we
2763 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2764 * formatted. */
2765 if (!wasatend && has_format_option(FO_WHITE_PAR))
2766 {
2767 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002768 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (curwin->w_cursor.col == len)
2770 {
2771 pnew = vim_strnsave(new, len + 2);
2772 pnew[len] = ' ';
2773 pnew[len + 1] = NUL;
2774 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
2775 /* remove the space later */
2776 did_add_space = TRUE;
2777 }
2778 else
2779 /* may remove added space */
2780 check_auto_format(FALSE);
2781 }
2782
2783 check_cursor();
2784}
2785
2786/*
2787 * When an extra space was added to continue a paragraph for auto-formatting,
2788 * delete it now. The space must be under the cursor, just after the insert
2789 * position.
2790 */
2791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002792check_auto_format(
2793 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002796 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 if (did_add_space)
2799 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002800 cc = gchar_cursor();
2801 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 /* Somehow the space was removed already. */
2803 did_add_space = FALSE;
2804 else
2805 {
2806 if (!end_insert)
2807 {
2808 inc_cursor();
2809 c = gchar_cursor();
2810 dec_cursor();
2811 }
2812 if (c != NUL)
2813 {
2814 /* The space is no longer at the end of the line, delete it. */
2815 del_char(FALSE);
2816 did_add_space = FALSE;
2817 }
2818 }
2819 }
2820}
2821
2822/*
2823 * Find out textwidth to be used for formatting:
2824 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02002825 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 * if invalid value, use 0.
2827 * Set default to window width (maximum 79) for "gq" operator.
2828 */
2829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002830comp_textwidth(
2831 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
2833 int textwidth;
2834
2835 textwidth = curbuf->b_p_tw;
2836 if (textwidth == 0 && curbuf->b_p_wm)
2837 {
2838 /* The width is the window width minus 'wrapmargin' minus all the
2839 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02002840 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841#ifdef FEAT_CMDWIN
2842 if (cmdwin_type != 0)
2843 textwidth -= 1;
2844#endif
2845#ifdef FEAT_FOLDING
2846 textwidth -= curwin->w_p_fdc;
2847#endif
2848#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002849 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 textwidth -= 1;
2851#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02002852 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 textwidth -= 8;
2854 }
2855 if (textwidth < 0)
2856 textwidth = 0;
2857 if (ff && textwidth == 0)
2858 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002859 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (textwidth > 79)
2861 textwidth = 79;
2862 }
2863 return textwidth;
2864}
2865
2866/*
2867 * Put a character in the redo buffer, for when just after a CTRL-V.
2868 */
2869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002870redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
2872 char_u buf[10];
2873
2874 /* Only digits need special treatment. Translate them into a string of
2875 * three digits. */
2876 if (VIM_ISDIGIT(c))
2877 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002878 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 AppendToRedobuff(buf);
2880 }
2881 else
2882 AppendCharToRedobuff(c);
2883}
2884
2885/*
2886 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002887 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002890start_arrow(
2891 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002893 start_arrow_common(end_insert_pos, TRUE);
2894}
2895
2896/*
2897 * Like start_arrow() but with end_change argument.
2898 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2899 */
2900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002901start_arrow_with_change(
2902 pos_T *end_insert_pos, /* can be NULL */
2903 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002904{
2905 start_arrow_common(end_insert_pos, end_change);
2906 if (!end_change)
2907 {
2908 AppendCharToRedobuff(Ctrl_G);
2909 AppendCharToRedobuff('U');
2910 }
2911}
2912
2913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914start_arrow_common(
2915 pos_T *end_insert_pos, /* can be NULL */
2916 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002917{
2918 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
2920 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002921 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 arrow_used = TRUE; /* this means we stopped the current insert */
2923 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002924#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002925 check_spell_redraw();
2926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927}
2928
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002929#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002930/*
2931 * If we skipped highlighting word at cursor, do it now.
2932 * It may be skipped again, thus reset spell_redraw_lnum first.
2933 */
2934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002936{
2937 if (spell_redraw_lnum != 0)
2938 {
2939 linenr_T lnum = spell_redraw_lnum;
2940
2941 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002942 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002943 }
2944}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002945
Bram Moolenaar217ad922005-03-20 22:37:15 +00002946#endif
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948/*
2949 * stop_arrow() is called before a change is made in insert mode.
2950 * If an arrow key has been used, start a new insertion.
2951 * Returns FAIL if undo is impossible, shouldn't insert then.
2952 */
2953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 if (arrow_used)
2957 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002958 Insstart = curwin->w_cursor; /* new insertion starts here */
2959 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
2960 /* Don't update the original insert position when moved to the
2961 * right, except when nothing was inserted yet. */
2962 update_Insstart_orig = FALSE;
2963 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 if (u_save_cursor() == OK)
2966 {
2967 arrow_used = FALSE;
2968 ins_need_undo = FALSE;
2969 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002970
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if (State & VREPLACE_FLAG)
2973 {
2974 orig_line_count = curbuf->b_ml.ml_line_count;
2975 vr_lines_changed = 1;
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 ResetRedobuff();
2978 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002979 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981 else if (ins_need_undo)
2982 {
2983 if (u_save_cursor() == OK)
2984 ins_need_undo = FALSE;
2985 }
2986
2987#ifdef FEAT_FOLDING
2988 /* Always open fold at the cursor line when inserting something. */
2989 foldOpenCursor();
2990#endif
2991
2992 return (arrow_used || ins_need_undo ? FAIL : OK);
2993}
2994
2995/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002996 * Do a few things to stop inserting.
2997 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2998 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 */
3000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003001stop_insert(
3002 pos_T *end_insert_pos,
3003 int esc, /* called by ins_esc() */
3004 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003006 int cc;
3007 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
3009 stop_redo_ins();
3010 replace_flush(); /* abandon replace stack */
3011
3012 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003013 * Save the inserted text for later redo with ^@ and CTRL-A.
3014 * Don't do it when "restart_edit" was set and nothing was inserted,
3015 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003017 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003018 if (did_restart_edit == 0 || (ptr != NULL
3019 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003020 {
3021 vim_free(last_insert);
3022 last_insert = ptr;
3023 last_insert_skip = new_insert_skip;
3024 }
3025 else
3026 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003028 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
3030 /* Auto-format now. It may seem strange to do this when stopping an
3031 * insertion (or moving the cursor), but it's required when appending
3032 * a line and having it end in a space. But only do it when something
3033 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003034 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003036 pos_T tpos = curwin->w_cursor;
3037
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 /* When the cursor is at the end of the line after a space the
3039 * formatting will move it to the following word. Avoid that by
3040 * moving the cursor onto the space. */
3041 cc = 'x';
3042 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
3043 {
3044 dec_cursor();
3045 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003046 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003047 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
3049
3050 auto_format(TRUE, FALSE);
3051
Bram Moolenaar1c465442017-03-12 20:10:05 +01003052 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003053 {
3054 if (gchar_cursor() != NUL)
3055 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003056 // If the cursor is still at the same character, also keep
3057 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003058 if (gchar_cursor() == NUL
3059 && curwin->w_cursor.lnum == tpos.lnum
3060 && curwin->w_cursor.col == tpos.col)
3061 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064
3065 /* If a space was inserted for auto-formatting, remove it now. */
3066 check_auto_format(TRUE);
3067
3068 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003069 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00003070 * Do this when ESC was used or moving the cursor up/down.
3071 * Check for the old position still being valid, just in case the text
3072 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01003073 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003074 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3075 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003077 pos_T tpos = curwin->w_cursor;
3078
3079 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003080 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003081 for (;;)
3082 {
3083 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3084 --curwin->w_cursor.col;
3085 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003086 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003087 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003088 if (del_char(TRUE) == FAIL)
3089 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003090 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003091 if (curwin->w_cursor.lnum != tpos.lnum)
3092 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003093 else
3094 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003095 /* reset tpos, could have been invalidated in the loop above */
3096 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003097 tpos.col++;
3098 if (cc != NUL && gchar_pos(&tpos) == NUL)
3099 ++curwin->w_cursor.col; /* put cursor back on the NUL */
3100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 /* <C-S-Right> may have started Visual mode, adjust the position for
3103 * deleted characters. */
3104 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3105 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003106 int len = (int)STRLEN(ml_get_curline());
3107
3108 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003110 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
3115 }
3116 did_ai = FALSE;
3117#ifdef FEAT_SMARTINDENT
3118 did_si = FALSE;
3119 can_si = FALSE;
3120 can_si_back = FALSE;
3121#endif
3122
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003123 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3124 * now in a different buffer. */
3125 if (end_insert_pos != NULL)
3126 {
3127 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003128 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003129 curbuf->b_op_end = *end_insert_pos;
3130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131}
3132
3133/*
3134 * Set the last inserted text to a single character.
3135 * Used for the replace command.
3136 */
3137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003138set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 char_u *s;
3141
3142 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (last_insert != NULL)
3145 {
3146 s = last_insert;
3147 /* Use the CTRL-V only when entering a special char */
3148 if (c < ' ' || c == DEL)
3149 *s++ = Ctrl_V;
3150 s = add_char2buf(c, s);
3151 *s++ = ESC;
3152 *s++ = NUL;
3153 last_insert_skip = 0;
3154 }
3155}
3156
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003157#if defined(EXITFREE) || defined(PROTO)
3158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003159free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003160{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003161 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003162}
3163#endif
3164
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165/*
3166 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3167 * and CSI. Handle multi-byte characters.
3168 * Returns a pointer to after the added bytes.
3169 */
3170 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003171add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003173 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 int i;
3175 int len;
3176
3177 len = (*mb_char2bytes)(c, temp);
3178 for (i = 0; i < len; ++i)
3179 {
3180 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
3182 if (c == K_SPECIAL)
3183 {
3184 *s++ = K_SPECIAL;
3185 *s++ = KS_SPECIAL;
3186 *s++ = KE_FILLER;
3187 }
3188#ifdef FEAT_GUI
3189 else if (c == CSI)
3190 {
3191 *s++ = CSI;
3192 *s++ = KS_EXTRA;
3193 *s++ = (int)KE_CSI;
3194 }
3195#endif
3196 else
3197 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 return s;
3200}
3201
3202/*
3203 * move cursor to start of line
3204 * if flags & BL_WHITE move to first non-white
3205 * if flags & BL_SOL move to first non-white if startofline is set,
3206 * otherwise keep "curswant" column
3207 * if flags & BL_FIX don't leave the cursor on a NUL.
3208 */
3209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003210beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 if ((flags & BL_SOL) && !p_sol)
3213 coladvance(curwin->w_curswant);
3214 else
3215 {
3216 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 if (flags & (BL_WHITE | BL_SOL))
3220 {
3221 char_u *ptr;
3222
Bram Moolenaar1c465442017-03-12 20:10:05 +01003223 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3225 ++curwin->w_cursor.col;
3226 }
3227 curwin->w_set_curswant = TRUE;
3228 }
3229}
3230
3231/*
3232 * oneright oneleft cursor_down cursor_up
3233 *
3234 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003235 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 * Return OK when successful, FAIL when we hit a line of file boundary.
3237 */
3238
3239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (virtual_active())
3246 {
3247 pos_T prevpos = curwin->w_cursor;
3248
3249 /* Adjust for multi-wide char (excluding TAB) */
3250 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003251 coladvance(getviscol() + ((*ptr != TAB
3252 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 ? ptr2cells(ptr) : 1));
3254 curwin->w_set_curswant = TRUE;
3255 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
3256 return (prevpos.col != curwin->w_cursor.col
3257 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003261 if (*ptr == NUL)
3262 return FAIL; /* already at the very end */
3263
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003264 if (has_mbyte)
3265 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003267 l = 1;
3268
3269 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3270 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003271 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003272 return FAIL;
3273 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275 curwin->w_set_curswant = TRUE;
3276 return OK;
3277}
3278
3279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003280oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (virtual_active())
3283 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003284#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 int v = getviscol();
3288
3289 if (v == 0)
3290 return FAIL;
3291
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003292#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 /* We might get stuck on 'showbreak', skip over it. */
3294 width = 1;
3295 for (;;)
3296 {
3297 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02003298 /* getviscol() is slow, skip it when 'showbreak' is empty,
3299 * 'breakindent' is not set and there are no multi-byte
3300 * characters */
Bram Moolenaaree857022019-11-09 23:26:40 +01003301 if ((*get_showbreak_value(curwin) == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003302 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 break;
3304 ++width;
3305 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003306#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
3310 if (curwin->w_cursor.coladd == 1)
3311 {
3312 char_u *ptr;
3313
3314 /* Adjust for multi-wide char (not a TAB) */
3315 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003316 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3317 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 curwin->w_cursor.coladd = 0;
3319 }
3320
3321 curwin->w_set_curswant = TRUE;
3322 return OK;
3323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
3325 if (curwin->w_cursor.col == 0)
3326 return FAIL;
3327
3328 curwin->w_set_curswant = TRUE;
3329 --curwin->w_cursor.col;
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 /* if the character on the left of the current cursor is a multi-byte
3332 * character, move to its first byte */
3333 if (has_mbyte)
3334 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return OK;
3336}
3337
3338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339cursor_up(
3340 long n,
3341 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 linenr_T lnum;
3344
3345 if (n > 0)
3346 {
3347 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003348 /* This fails if the cursor is already in the first line or the count
3349 * is larger than the line number and '-' is in 'cpoptions' */
3350 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 return FAIL;
3352 if (n >= lnum)
3353 lnum = 1;
3354 else
3355#ifdef FEAT_FOLDING
3356 if (hasAnyFolding(curwin))
3357 {
3358 /*
3359 * Count each sequence of folded lines as one logical line.
3360 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003361 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 (void)hasFolding(lnum, &lnum, NULL);
3363
3364 while (n--)
3365 {
3366 /* move up one line */
3367 --lnum;
3368 if (lnum <= 1)
3369 break;
3370 /* If we entered a fold, move to the beginning, unless in
3371 * Insert mode or when 'foldopen' contains "all": it will open
3372 * in a moment. */
3373 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3374 (void)hasFolding(lnum, &lnum, NULL);
3375 }
3376 if (lnum < 1)
3377 lnum = 1;
3378 }
3379 else
3380#endif
3381 lnum -= n;
3382 curwin->w_cursor.lnum = lnum;
3383 }
3384
3385 /* try to advance to the column we want to be at */
3386 coladvance(curwin->w_curswant);
3387
3388 if (upd_topline)
3389 update_topline(); /* make sure curwin->w_topline is valid */
3390
3391 return OK;
3392}
3393
3394/*
3395 * Cursor down a number of logical lines.
3396 */
3397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003398cursor_down(
3399 long n,
3400 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 linenr_T lnum;
3403
3404 if (n > 0)
3405 {
3406 lnum = curwin->w_cursor.lnum;
3407#ifdef FEAT_FOLDING
3408 /* Move to last line of fold, will fail if it's the end-of-file. */
3409 (void)hasFolding(lnum, NULL, &lnum);
3410#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003411 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003412 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003413 if (lnum >= curbuf->b_ml.ml_line_count
3414 || (lnum + n > curbuf->b_ml.ml_line_count
3415 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 return FAIL;
3417 if (lnum + n >= curbuf->b_ml.ml_line_count)
3418 lnum = curbuf->b_ml.ml_line_count;
3419 else
3420#ifdef FEAT_FOLDING
3421 if (hasAnyFolding(curwin))
3422 {
3423 linenr_T last;
3424
3425 /* count each sequence of folded lines as one logical line */
3426 while (n--)
3427 {
3428 if (hasFolding(lnum, NULL, &last))
3429 lnum = last + 1;
3430 else
3431 ++lnum;
3432 if (lnum >= curbuf->b_ml.ml_line_count)
3433 break;
3434 }
3435 if (lnum > curbuf->b_ml.ml_line_count)
3436 lnum = curbuf->b_ml.ml_line_count;
3437 }
3438 else
3439#endif
3440 lnum += n;
3441 curwin->w_cursor.lnum = lnum;
3442 }
3443
3444 /* try to advance to the column we want to be at */
3445 coladvance(curwin->w_curswant);
3446
3447 if (upd_topline)
3448 update_topline(); /* make sure curwin->w_topline is valid */
3449
3450 return OK;
3451}
3452
3453/*
3454 * Stuff the last inserted text in the read buffer.
3455 * Last_insert actually is a copy of the redo buffer, so we
3456 * first have to remove the command.
3457 */
3458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003459stuff_inserted(
3460 int c, /* Command character to be inserted */
3461 long count, /* Repeat this many times */
3462 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
3464 char_u *esc_ptr;
3465 char_u *ptr;
3466 char_u *last_ptr;
3467 char_u last = NUL;
3468
3469 ptr = get_last_insert();
3470 if (ptr == NULL)
3471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003472 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 return FAIL;
3474 }
3475
3476 /* may want to stuff the command character, to start Insert mode */
3477 if (c != NUL)
3478 stuffcharReadbuff(c);
3479 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
3480 *esc_ptr = NUL; /* remove the ESC */
3481
3482 /* when the last char is either "0" or "^" it will be quoted if no ESC
3483 * comes after it OR if it will inserted more than once and "ptr"
3484 * starts with ^D. -- Acevedo
3485 */
3486 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3487 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3488 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3489 {
3490 last = *last_ptr;
3491 *last_ptr = NUL;
3492 }
3493
3494 do
3495 {
3496 stuffReadbuff(ptr);
3497 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
3498 if (last)
3499 stuffReadbuff((char_u *)(last == '0'
3500 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3501 : IF_EB("\026^", CTRL_V_STR "^")));
3502 }
3503 while (--count > 0);
3504
3505 if (last)
3506 *last_ptr = last;
3507
3508 if (esc_ptr != NULL)
3509 *esc_ptr = ESC; /* put the ESC back */
3510
3511 /* may want to stuff a trailing ESC, to get out of Insert mode */
3512 if (!no_esc)
3513 stuffcharReadbuff(ESC);
3514
3515 return OK;
3516}
3517
3518 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003519get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
3521 if (last_insert == NULL)
3522 return NULL;
3523 return last_insert + last_insert_skip;
3524}
3525
3526/*
3527 * Get last inserted string, and remove trailing <Esc>.
3528 * Returns pointer to allocated memory (must be freed) or NULL.
3529 */
3530 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003531get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
3533 char_u *s;
3534 int len;
3535
3536 if (last_insert == NULL)
3537 return NULL;
3538 s = vim_strsave(last_insert + last_insert_skip);
3539 if (s != NULL)
3540 {
3541 len = (int)STRLEN(s);
3542 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
3543 s[len - 1] = NUL;
3544 }
3545 return s;
3546}
3547
3548/*
3549 * Check the word in front of the cursor for an abbreviation.
3550 * Called when the non-id character "c" has been entered.
3551 * When an abbreviation is recognized it is removed from the text and
3552 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3553 */
3554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003555echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
3557 /* Don't check for abbreviation in paste mode, when disabled and just
3558 * after moving around with cursor keys. */
3559 if (p_paste || no_abbr || arrow_used)
3560 return FALSE;
3561
3562 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3563 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3564}
3565
3566/*
3567 * replace-stack functions
3568 *
3569 * When replacing characters, the replaced characters are remembered for each
3570 * new character. This is used to re-insert the old text when backspacing.
3571 *
3572 * There is a NUL headed list of characters for each character that is
3573 * currently in the file after the insertion point. When BS is used, one NUL
3574 * headed list is put back for the deleted character.
3575 *
3576 * For a newline, there are two NUL headed lists. One contains the characters
3577 * that the NL replaced. The extra one stores the characters after the cursor
3578 * that were deleted (always white space).
3579 *
3580 * Replace_offset is normally 0, in which case replace_push will add a new
3581 * character at the end of the stack. If replace_offset is not 0, that many
3582 * characters will be left on the stack above the newly inserted character.
3583 */
3584
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003585static char_u *replace_stack = NULL;
3586static long replace_stack_nr = 0; /* next entry in replace stack */
3587static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
3589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003590replace_push(
3591 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
3593 char_u *p;
3594
3595 if (replace_stack_nr < replace_offset) /* nothing to do */
3596 return;
3597 if (replace_stack_len <= replace_stack_nr)
3598 {
3599 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003600 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (p == NULL) /* out of memory */
3602 {
3603 replace_stack_len -= 50;
3604 return;
3605 }
3606 if (replace_stack != NULL)
3607 {
3608 mch_memmove(p, replace_stack,
3609 (size_t)(replace_stack_nr * sizeof(char_u)));
3610 vim_free(replace_stack);
3611 }
3612 replace_stack = p;
3613 }
3614 p = replace_stack + replace_stack_nr - replace_offset;
3615 if (replace_offset)
3616 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3617 *p = c;
3618 ++replace_stack_nr;
3619}
3620
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003621/*
3622 * Push a character onto the replace stack. Handles a multi-byte character in
3623 * reverse byte order, so that the first byte is popped off first.
3624 * Return the number of bytes done (includes composing characters).
3625 */
3626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003627replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003628{
3629 int l = (*mb_ptr2len)(p);
3630 int j;
3631
3632 for (j = l - 1; j >= 0; --j)
3633 replace_push(p[j]);
3634 return l;
3635}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003636
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637/*
3638 * Pop one item from the replace stack.
3639 * return -1 if stack empty
3640 * return replaced character or NUL otherwise
3641 */
3642 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003643replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 if (replace_stack_nr == 0)
3646 return -1;
3647 return (int)replace_stack[--replace_stack_nr];
3648}
3649
3650/*
3651 * Join the top two items on the replace stack. This removes to "off"'th NUL
3652 * encountered.
3653 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655replace_join(
3656 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 int i;
3659
3660 for (i = replace_stack_nr; --i >= 0; )
3661 if (replace_stack[i] == NUL && off-- <= 0)
3662 {
3663 --replace_stack_nr;
3664 mch_memmove(replace_stack + i, replace_stack + i + 1,
3665 (size_t)(replace_stack_nr - i));
3666 return;
3667 }
3668}
3669
3670/*
3671 * Pop bytes from the replace stack until a NUL is found, and insert them
3672 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3673 */
3674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003675replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 int cc;
3678 int oldState = State;
3679
3680 State = NORMAL; /* don't want REPLACE here */
3681 while ((cc = replace_pop()) > 0)
3682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 dec_cursor();
3685 }
3686 State = oldState;
3687}
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689/*
3690 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3691 * indicates a multi-byte char, pop the other bytes too.
3692 */
3693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003694mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695{
3696 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003697 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 int i;
3699 int c;
3700
3701 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3702 {
3703 buf[0] = cc;
3704 for (i = 1; i < n; ++i)
3705 buf[i] = replace_pop();
3706 ins_bytes_len(buf, n);
3707 }
3708 else
3709 ins_char(cc);
3710
3711 if (enc_utf8)
3712 /* Handle composing chars. */
3713 for (;;)
3714 {
3715 c = replace_pop();
3716 if (c == -1) /* stack empty */
3717 break;
3718 if ((n = MB_BYTE2LEN(c)) == 1)
3719 {
3720 /* Not a multi-byte char, put it back. */
3721 replace_push(c);
3722 break;
3723 }
3724 else
3725 {
3726 buf[0] = c;
3727 for (i = 1; i < n; ++i)
3728 buf[i] = replace_pop();
3729 if (utf_iscomposing(utf_ptr2char(buf)))
3730 ins_bytes_len(buf, n);
3731 else
3732 {
3733 /* Not a composing char, put it back. */
3734 for (i = n - 1; i >= 0; --i)
3735 replace_push(buf[i]);
3736 break;
3737 }
3738 }
3739 }
3740}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742/*
3743 * make the replace stack empty
3744 * (called when exiting replace mode)
3745 */
3746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003747replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003749 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 replace_stack_len = 0;
3751 replace_stack_nr = 0;
3752}
3753
3754/*
3755 * Handle doing a BS for one character.
3756 * cc < 0: replace stack empty, just move cursor
3757 * cc == 0: character was inserted, delete it
3758 * cc > 0: character was replaced, put cc (first byte of original char) back
3759 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003760 * When "limit_col" is >= 0, don't delete before this column. Matters when
3761 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 */
3763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003764replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765{
3766 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 int orig_len = 0;
3768 int ins_len;
3769 int orig_vcols = 0;
3770 colnr_T start_vcol;
3771 char_u *p;
3772 int i;
3773 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
3775 cc = replace_pop();
3776 if (cc > 0)
3777 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003778#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003779 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003780
3781 if (curbuf->b_has_textprop)
3782 {
3783 // Do not adjust text properties for individual delete and insert
3784 // operations, do it afterwards on the resulting text.
3785 len_before = STRLEN(ml_get_curline());
3786 ++text_prop_frozen;
3787 }
3788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 if (State & VREPLACE_FLAG)
3790 {
3791 /* Get the number of screen cells used by the character we are
3792 * going to delete. */
3793 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3794 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (has_mbyte)
3797 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003798 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003800 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 replace_push(cc);
3802 }
3803 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003807 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 replace_pop_ins();
3810
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (State & VREPLACE_FLAG)
3812 {
3813 /* Get the number of screen cells used by the inserted characters */
3814 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003815 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 vcol = start_vcol;
3817 for (i = 0; i < ins_len; ++i)
3818 {
3819 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003820 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 vcol -= start_vcol;
3823
3824 /* Delete spaces that were inserted after the cursor to keep the
3825 * text aligned. */
3826 curwin->w_cursor.col += ins_len;
3827 while (vcol > orig_vcols && gchar_cursor() == ' ')
3828 {
3829 del_char(FALSE);
3830 ++orig_vcols;
3831 }
3832 curwin->w_cursor.col -= ins_len;
3833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
Bram Moolenaar196d1572019-01-02 23:47:18 +01003835 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003837
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003838#ifdef FEAT_PROP_POPUP
Bram Moolenaar196d1572019-01-02 23:47:18 +01003839 if (curbuf->b_has_textprop)
3840 {
3841 size_t len_now = STRLEN(ml_get_curline());
3842
3843 --text_prop_frozen;
3844 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003845 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003846 }
3847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003850 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851}
3852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3854/*
3855 * Map Hebrew keyboard when in hkmap mode.
3856 */
3857 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003858hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
3861 {
3862 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3863 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3864 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3865 static char_u map[26] =
3866 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3867 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3868 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3869 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3870 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3871 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3872 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3873 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3874 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3875
3876 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3877 return (int)(map[CharOrd(c)] - 1 + p_aleph);
3878 /* '-1'='sofit' */
3879 else if (c == 'x')
3880 return 'X';
3881 else if (c == 'q')
3882 return '\''; /* {geresh}={'} */
3883 else if (c == 246)
3884 return ' '; /* \"o --> ' ' for a german keyboard */
3885 else if (c == 228)
3886 return ' '; /* \"a --> ' ' -- / -- */
3887 else if (c == 252)
3888 return ' '; /* \"u --> ' ' -- / -- */
3889#ifdef EBCDIC
3890 else if (islower(c))
3891#else
3892 /* NOTE: islower() does not do the right thing for us on Linux so we
3893 * do this the same was as 5.7 and previous, so it works correctly on
3894 * all systems. Specifically, the e.g. Delete and Arrow keys are
3895 * munged and won't work if e.g. searching for Hebrew text.
3896 */
3897 else if (c >= 'a' && c <= 'z')
3898#endif
3899 return (int)(map[CharOrdLow(c)] + p_aleph);
3900 else
3901 return c;
3902 }
3903 else
3904 {
3905 switch (c)
3906 {
3907 case '`': return ';';
3908 case '/': return '.';
3909 case '\'': return ',';
3910 case 'q': return '/';
3911 case 'w': return '\'';
3912
3913 /* Hebrew letters - set offset from 'a' */
3914 case ',': c = '{'; break;
3915 case '.': c = 'v'; break;
3916 case ';': c = 't'; break;
3917 default: {
3918 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3919
3920#ifdef EBCDIC
3921 /* see note about islower() above */
3922 if (!islower(c))
3923#else
3924 if (c < 'a' || c > 'z')
3925#endif
3926 return c;
3927 c = str[CharOrdLow(c)];
3928 break;
3929 }
3930 }
3931
3932 return (int)(CharOrdLow(c) + p_aleph);
3933 }
3934}
3935#endif
3936
3937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003938ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939{
3940 int need_redraw = FALSE;
3941 int regname;
3942 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003943 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
3945 /*
3946 * If we are going to wait for a character, show a '"'.
3947 */
3948 pc_status = PC_STATUS_UNSET;
3949 if (redrawing() && !char_avail())
3950 {
3951 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00003952 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
3954 edit_putchar('"', TRUE);
3955#ifdef FEAT_CMDL_INFO
3956 add_to_showcmd_c(Ctrl_R);
3957#endif
3958 }
3959
3960#ifdef USE_ON_FLY_SCROLL
3961 dont_scroll = TRUE; /* disallow scrolling here */
3962#endif
3963
3964 /*
3965 * Don't map the register name. This also prevents the mode message to be
3966 * deleted when ESC is hit.
3967 */
3968 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003969 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003970 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3973 {
3974 /* Get a third key for literal register insertion */
3975 literally = regname;
3976#ifdef FEAT_CMDL_INFO
3977 add_to_showcmd_c(literally);
3978#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003979 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003983 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
3985#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02003986 /* Don't call u_sync() while typing the expression or giving an error
3987 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 ++no_u_sync;
3989 if (regname == '=')
3990 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003991 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003992# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003994# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003995 /* Sync undo when evaluating the expression calls setline() or
3996 * append(), so that it can be undone separately. */
3997 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004000
4001 // Cursor may be moved back a column.
4002 curwin->w_cursor = curpos;
4003 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004004# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004005 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (im_on)
4007 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004008# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00004010 if (regname == NUL || !valid_yank_reg(regname, FALSE))
4011 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004012 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00004014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 else
4016 {
4017#endif
4018 if (literally == Ctrl_O || literally == Ctrl_P)
4019 {
4020 /* Append the command to the redo buffer. */
4021 AppendCharToRedobuff(Ctrl_R);
4022 AppendCharToRedobuff(literally);
4023 AppendCharToRedobuff(regname);
4024
4025 do_put(regname, BACKWARD, 1L,
4026 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
4027 }
4028 else if (insert_reg(regname, literally) == FAIL)
4029 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004030 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 need_redraw = TRUE; /* remove the '"' */
4032 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004033 else if (stop_insert_mode)
4034 /* When the '=' register was used and a function was invoked that
4035 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
4036 * insert anything, need to remove the '"' */
4037 need_redraw = TRUE;
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039#ifdef FEAT_EVAL
4040 }
4041 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004042 if (u_sync_once == 1)
4043 ins_need_undo = TRUE;
4044 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045#endif
4046#ifdef FEAT_CMDL_INFO
4047 clear_showcmd();
4048#endif
4049
4050 /* If the inserted register is empty, we need to remove the '"' */
4051 if (need_redraw || stuff_empty())
4052 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004053
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004054 /* Disallow starting Visual mode here, would get a weird mode. */
4055 if (!vis_active && VIsual_active)
4056 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057}
4058
4059/*
4060 * CTRL-G commands in Insert mode.
4061 */
4062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004063ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064{
4065 int c;
4066
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004067 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 /*
4071 * Don't map the second key. This also prevents the mode message to be
4072 * deleted when ESC is hit.
4073 */
4074 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004075 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004076 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004078 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 switch (c)
4080 {
4081 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
4082 case K_UP:
4083 case Ctrl_K:
4084 case 'k': ins_up(TRUE);
4085 break;
4086
4087 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
4088 case K_DOWN:
4089 case Ctrl_J:
4090 case 'j': ins_down(TRUE);
4091 break;
4092
4093 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004094 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004096
4097 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004098 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004099 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004100 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 break;
4102
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004103 /* CTRL-G U: do not break undo with the next char */
4104 case 'U':
4105 /* Allow one left/right cursor movement with the next char,
4106 * without breaking undo. */
4107 dont_sync_undo = MAYBE;
4108 break;
4109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02004111 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113}
4114
4115/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004116 * CTRL-^ in Insert mode.
4117 */
4118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004119ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004121 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 {
4123 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
4124 if (State & LANGMAP)
4125 {
4126 curbuf->b_p_iminsert = B_IMODE_NONE;
4127 State &= ~LANGMAP;
4128 }
4129 else
4130 {
4131 curbuf->b_p_iminsert = B_IMODE_LMAP;
4132 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004133#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134 im_set_active(FALSE);
4135#endif
4136 }
4137 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004138#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 else
4140 {
4141 /* There are no ":lmap" mappings, toggle IM */
4142 if (im_get_status())
4143 {
4144 curbuf->b_p_iminsert = B_IMODE_NONE;
4145 im_set_active(FALSE);
4146 }
4147 else
4148 {
4149 curbuf->b_p_iminsert = B_IMODE_IM;
4150 State &= ~LANGMAP;
4151 im_set_active(TRUE);
4152 }
4153 }
4154#endif
4155 set_iminsert_global();
4156 showmode();
4157#ifdef FEAT_GUI
4158 /* may show different cursor shape or color */
4159 if (gui.in_use)
4160 gui_update_cursor(TRUE, FALSE);
4161#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004162#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004163 /* Show/unshow value of 'keymap' in status lines. */
4164 status_redraw_curbuf();
4165#endif
4166}
4167
4168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 * Handle ESC in insert mode.
4170 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4171 * insert.
4172 */
4173 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004174ins_esc(
4175 long *count,
4176 int cmdchar,
4177 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
4179 int temp;
4180 static int disabled_redraw = FALSE;
4181
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004182#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183 check_spell_redraw();
4184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
4186 temp = curwin->w_cursor.col;
4187 if (disabled_redraw)
4188 {
4189 --RedrawingDisabled;
4190 disabled_redraw = FALSE;
4191 }
4192 if (!arrow_used)
4193 {
4194 /*
4195 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004196 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4197 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 */
4199 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004200 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
4202 /*
4203 * Repeating insert may take a long time. Check for
4204 * interrupt now and then.
4205 */
4206 if (*count > 0)
4207 {
4208 line_breakcheck();
4209 if (got_int)
4210 *count = 0;
4211 }
4212
4213 if (--*count > 0) /* repeat what was typed */
4214 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004215 /* Vi repeats the insert without replacing characters. */
4216 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4217 State &= ~REPLACE_FLAG;
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 (void)start_redo_ins();
4220 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02004221 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 ++RedrawingDisabled;
4223 disabled_redraw = TRUE;
4224 return FALSE; /* repeat the insert */
4225 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004226 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 undisplay_dollar();
4228 }
4229
4230 /* When an autoindent was removed, curswant stays after the
4231 * indent */
4232 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4233 curwin->w_set_curswant = TRUE;
4234
4235 /* Remember the last Insert position in the '^ mark. */
4236 if (!cmdmod.keepjumps)
4237 curbuf->b_last_insert = curwin->w_cursor;
4238
4239 /*
4240 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004241 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004243 if (!nomove
4244 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004245 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004246 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004247 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248#ifdef FEAT_RIGHTLEFT
4249 && !revins_on
4250#endif
4251 )
4252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4254 {
4255 oneleft();
4256 if (restart_edit != NUL)
4257 ++curwin->w_cursor.coladd;
4258 }
4259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 {
4261 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 /* Correct cursor for multi-byte character. */
4263 if (has_mbyte)
4264 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 }
4267
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004268#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 /* Disable IM to allow typing English directly for Normal mode commands.
4270 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4271 * well). */
4272 if (!(State & LANGMAP))
4273 im_save_status(&curbuf->b_p_iminsert);
4274 im_set_active(FALSE);
4275#endif
4276
4277 State = NORMAL;
4278 /* need to position cursor again (e.g. when on a TAB ) */
4279 changed_cline_bef_curs();
4280
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282#ifdef CURSOR_SHAPE
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004283 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004285 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004286 {
4287 // Re-enable bracketed paste mode.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004288 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004290 // Re-enable modifyOtherKeys.
4291 out_str(T_CTI);
4292 }
4293
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004294 // When recording or for CTRL-O, need to display the new mode.
4295 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004296 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004298 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004299 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004301 return TRUE; // exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302}
4303
4304#ifdef FEAT_RIGHTLEFT
4305/*
4306 * Toggle language: hkmap and revins_on.
4307 * Move to end of reverse inserted text.
4308 */
4309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004310ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
4312 if (revins_on && revins_chars && revins_scol >= 0)
4313 {
4314 while (gchar_cursor() != NUL && revins_chars--)
4315 ++curwin->w_cursor.col;
4316 }
4317 p_ri = !p_ri;
4318 revins_on = (State == INSERT && p_ri);
4319 if (revins_on)
4320 {
4321 revins_scol = curwin->w_cursor.col;
4322 revins_legal++;
4323 revins_chars = 0;
4324 undisplay_dollar();
4325 }
4326 else
4327 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004328 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 showmode();
4330}
4331#endif
4332
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333/*
4334 * If 'keymodel' contains "startsel", may start selection.
4335 * Returns TRUE when a CTRL-O and other keys stuffed.
4336 */
4337 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004338ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339{
4340 if (km_startsel)
4341 switch (c)
4342 {
4343 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 case K_PAGEUP:
4346 case K_KPAGEUP:
4347 case K_PAGEDOWN:
4348 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004349# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 case K_LEFT:
4351 case K_RIGHT:
4352 case K_UP:
4353 case K_DOWN:
4354 case K_END:
4355 case K_HOME:
4356# endif
4357 if (!(mod_mask & MOD_MASK_SHIFT))
4358 break;
4359 /* FALLTHROUGH */
4360 case K_S_LEFT:
4361 case K_S_RIGHT:
4362 case K_S_UP:
4363 case K_S_DOWN:
4364 case K_S_END:
4365 case K_S_HOME:
4366 /* Start selection right away, the cursor can move with
4367 * CTRL-O when beyond the end of the line. */
4368 start_selection();
4369
4370 /* Execute the key in (insert) Select mode. */
4371 stuffcharReadbuff(Ctrl_O);
4372 if (mod_mask)
4373 {
4374 char_u buf[4];
4375
4376 buf[0] = K_SPECIAL;
4377 buf[1] = KS_MODIFIER;
4378 buf[2] = mod_mask;
4379 buf[3] = NUL;
4380 stuffReadbuff(buf);
4381 }
4382 stuffcharReadbuff(c);
4383 return TRUE;
4384 }
4385 return FALSE;
4386}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004389 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 */
4391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004392ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004394#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004395 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004396 (char_u *)((State & REPLACE_FLAG) ? "i"
4397 : replaceState == VREPLACE ? "v"
4398 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004399#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004400 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 if (State & REPLACE_FLAG)
4402 State = INSERT | (State & LANGMAP);
4403 else
4404 State = replaceState | (State & LANGMAP);
4405 AppendCharToRedobuff(K_INS);
4406 showmode();
4407#ifdef CURSOR_SHAPE
4408 ui_cursor_shape(); /* may show different cursor shape */
4409#endif
4410}
4411
4412/*
4413 * Pressed CTRL-O in Insert mode.
4414 */
4415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004416ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 if (State & VREPLACE_FLAG)
4419 restart_edit = 'V';
4420 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 if (State & REPLACE_FLAG)
4422 restart_edit = 'R';
4423 else
4424 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 if (virtual_active())
4426 ins_at_eol = FALSE; /* cursor always keeps its column */
4427 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 ins_at_eol = (gchar_cursor() == NUL);
4429}
4430
4431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 * If the cursor is on an indent, ^T/^D insert/delete one
4433 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004434 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 * with vi. But vi only supports ^T and ^D after an
4436 * autoindent, we support it everywhere.
4437 */
4438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004439ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
4441 if (stop_arrow() == FAIL)
4442 return;
4443 AppendCharToRedobuff(c);
4444
4445 /*
4446 * 0^D and ^^D: remove all indent.
4447 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004448 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4449 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
4451 --curwin->w_cursor.col;
4452 (void)del_char(FALSE); /* delete the '^' or '0' */
4453 /* In Replace mode, restore the characters that '^' or '0' replaced. */
4454 if (State & REPLACE_FLAG)
4455 replace_pop_ins();
4456 if (lastc == '^')
4457 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004458 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004461 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4464 did_ai = FALSE;
4465#ifdef FEAT_SMARTINDENT
4466 did_si = FALSE;
4467 can_si = FALSE;
4468 can_si_back = FALSE;
4469#endif
4470#ifdef FEAT_CINDENT
4471 can_cindent = FALSE; /* no cindenting after ^D or ^T */
4472#endif
4473}
4474
4475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004476ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477{
4478 int temp;
4479
4480 if (stop_arrow() == FAIL)
4481 return;
4482 if (gchar_cursor() == NUL) /* delete newline */
4483 {
4484 temp = curwin->w_cursor.col;
4485 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004486 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004487 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004491 /* Adjust orig_line_count in case more lines have been deleted than
4492 * have been added. That makes sure, that open_line() later
4493 * can access all buffer lines correctly */
4494 if (State & VREPLACE_FLAG &&
4495 orig_line_count > curbuf->b_ml.ml_line_count)
4496 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02004499 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
4500 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 did_ai = FALSE;
4502#ifdef FEAT_SMARTINDENT
4503 did_si = FALSE;
4504 can_si = FALSE;
4505 can_si_back = FALSE;
4506#endif
4507 AppendCharToRedobuff(K_DEL);
4508}
4509
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004510/*
4511 * Delete one character for ins_bs().
4512 */
4513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004514ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004515{
4516 dec_cursor();
4517 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4518 if (State & REPLACE_FLAG)
4519 {
4520 /* Don't delete characters before the insert point when in
4521 * Replace mode */
4522 if (curwin->w_cursor.lnum != Insstart.lnum
4523 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004524 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004525 }
4526 else
4527 (void)del_char(FALSE);
4528}
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530/*
4531 * Handle Backspace, delete-word and delete-line in Insert mode.
4532 * Return TRUE when backspace was actually used.
4533 */
4534 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004535ins_bs(
4536 int c,
4537 int mode,
4538 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539{
4540 linenr_T lnum;
4541 int cc;
4542 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004543 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 colnr_T mincol;
4545 int did_backspace = FALSE;
4546 int in_indent;
4547 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004548 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 /*
4551 * can't delete anything in an empty file
4552 * can't backup past first character in buffer
4553 * can't backup past starting point unless 'backspace' > 1
4554 * can backup to a previous line if 'backspace' == 0
4555 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004556 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 || (
4558#ifdef FEAT_RIGHTLEFT
4559 !revins_on &&
4560#endif
4561 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4562 || (!can_bs(BS_START)
4563 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004564 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4565 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4567 && curwin->w_cursor.col <= ai_col)
4568 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4569 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004570 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 return FALSE;
4572 }
4573
4574 if (stop_arrow() == FAIL)
4575 return FALSE;
4576 in_indent = inindent(0);
4577#ifdef FEAT_CINDENT
4578 if (in_indent)
4579 can_cindent = FALSE;
4580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 end_comment_pending = NUL; /* After BS, don't auto-end comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582#ifdef FEAT_RIGHTLEFT
4583 if (revins_on) /* put cursor after last inserted char */
4584 inc_cursor();
4585#endif
4586
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 /* Virtualedit:
4588 * BACKSPACE_CHAR eats a virtual space
4589 * BACKSPACE_WORD eats all coladd
4590 * BACKSPACE_LINE eats all coladd and keeps going
4591 */
4592 if (curwin->w_cursor.coladd > 0)
4593 {
4594 if (mode == BACKSPACE_CHAR)
4595 {
4596 --curwin->w_cursor.coladd;
4597 return TRUE;
4598 }
4599 if (mode == BACKSPACE_WORD)
4600 {
4601 curwin->w_cursor.coladd = 0;
4602 return TRUE;
4603 }
4604 curwin->w_cursor.coladd = 0;
4605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004608 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 */
4610 if (curwin->w_cursor.col == 0)
4611 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004612 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004613 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#ifdef FEAT_RIGHTLEFT
4615 || revins_on
4616#endif
4617 )
4618 {
4619 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4620 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4621 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004622 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004623 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625 /*
4626 * In replace mode:
4627 * cc < 0: NL was inserted, delete it
4628 * cc >= 0: NL was replaced, put original characters back
4629 */
4630 cc = -1;
4631 if (State & REPLACE_FLAG)
4632 cc = replace_pop(); /* returns -1 if NL was inserted */
4633 /*
4634 * In replace mode, in the line we started replacing, we only move the
4635 * cursor.
4636 */
4637 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4638 {
4639 dec_cursor();
4640 }
4641 else
4642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (!(State & VREPLACE_FLAG)
4644 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
4646 temp = gchar_cursor(); /* remember current char */
4647 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004648
4649 /* When "aw" is in 'formatoptions' we must delete the space at
4650 * the end of the line, otherwise the line will be broken
4651 * again when auto-formatting. */
4652 if (has_format_option(FO_AUTO)
4653 && has_format_option(FO_WHITE_PAR))
4654 {
4655 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4656 TRUE);
4657 int len;
4658
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004659 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004660 if (len > 0 && ptr[len - 1] == ' ')
4661 ptr[len - 1] = NUL;
4662 }
4663
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004664 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (temp == NUL && gchar_cursor() != NUL)
4666 inc_cursor();
4667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 else
4669 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671 /*
4672 * In REPLACE mode we have to put back the text that was replaced
4673 * by the NL. On the replace stack is first a NUL-terminated
4674 * sequence of characters that were deleted and then the
4675 * characters that NL replaced.
4676 */
4677 if (State & REPLACE_FLAG)
4678 {
4679 /*
4680 * Do the next ins_char() in NORMAL state, to
4681 * prevent ins_char() from replacing characters and
4682 * avoiding showmatch().
4683 */
4684 oldState = State;
4685 State = NORMAL;
4686 /*
4687 * restore characters (blanks) deleted after cursor
4688 */
4689 while (cc > 0)
4690 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004691 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004693 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 cc = replace_pop();
4695 }
4696 /* restore the characters that NL replaced */
4697 replace_pop_ins();
4698 State = oldState;
4699 }
4700 }
4701 did_ai = FALSE;
4702 }
4703 else
4704 {
4705 /*
4706 * Delete character(s) before the cursor.
4707 */
4708#ifdef FEAT_RIGHTLEFT
4709 if (revins_on) /* put cursor on last inserted char */
4710 dec_cursor();
4711#endif
4712 mincol = 0;
4713 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004714 if (mode == BACKSPACE_LINE
4715 && (curbuf->b_p_ai
4716#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004717 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004718#endif
4719 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#ifdef FEAT_RIGHTLEFT
4721 && !revins_on
4722#endif
4723 )
4724 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004725 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004727 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004729 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731
4732 /*
4733 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4734 */
4735 if ( mode == BACKSPACE_CHAR
4736 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004737 || ((get_sts_value() != 0
4738#ifdef FEAT_VARTABS
4739 || tabstop_count(curbuf->b_p_vsts_array)
4740#endif
4741 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004742 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 && (*(ml_get_cursor() - 1) == TAB
4744 || (*(ml_get_cursor() - 1) == ' '
4745 && (!*inserted_space_p
4746 || arrow_used))))))
4747 {
4748 int ts;
4749 colnr_T vcol;
4750 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004751 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 /* Compute the virtual column where we want to be. Since
4755 * 'showbreak' may get in the way, need to get the last column of
4756 * the previous character. */
4757 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004758 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 dec_cursor();
4760 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4761 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004762#ifdef FEAT_VARTABS
4763 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004764 {
4765 ts = (int)get_sw_value(curbuf);
4766 want_vcol = (want_vcol / ts) * ts;
4767 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004768 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004769 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004770 curbuf->b_p_vsts_array);
4771#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004772 if (p_sta && in_indent)
4773 ts = (int)get_sw_value(curbuf);
4774 else
4775 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 /* delete characters until we are at or before want_vcol */
4780 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004781 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004782 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784 /* insert extra spaces until we are at want_vcol */
4785 while (vcol < want_vcol)
4786 {
4787 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004788 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4789 && curwin->w_cursor.col < Insstart_orig.col)
4790 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (State & VREPLACE_FLAG)
4793 ins_char(' ');
4794 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
4796 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004797 if ((State & REPLACE_FLAG))
4798 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 }
4800 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4801 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004802
4803 /* If we are now back where we started delete one character. Can
4804 * happen when using 'sts' and 'linebreak'. */
4805 if (vcol >= start_vcol)
4806 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808
4809 /*
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004810 * Delete up to starting point, start of line or previous word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004812 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004814 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004816 if (has_mbyte)
4817 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004818 do
4819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004821 if (!revins_on) /* put cursor on char to be deleted */
4822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004824
4825 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004826 /* look multi-byte character class */
4827 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004829 prev_cclass = cclass;
4830 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004832
4833 /* start of word? */
4834 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4835 {
4836 mode = BACKSPACE_WORD_NOT_SPACE;
4837 temp = vim_iswordc(cc);
4838 }
4839 /* end of word? */
4840 else if (mode == BACKSPACE_WORD_NOT_SPACE
4841 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004842 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004845 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004847 inc_cursor();
4848#ifdef FEAT_RIGHTLEFT
4849 else if (State & REPLACE_FLAG)
4850 dec_cursor();
4851#endif
4852 break;
4853 }
4854 if (State & REPLACE_FLAG)
4855 replace_do_bs(-1);
4856 else
4857 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004858 if (enc_utf8 && p_deco)
4859 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004860 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004861 /*
4862 * If there are combining characters and 'delcombine' is set
4863 * move the cursor back. Don't back up before the base
4864 * character.
4865 */
4866 if (enc_utf8 && p_deco && cpc[0] != NUL)
4867 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004868#ifdef FEAT_RIGHTLEFT
4869 if (revins_chars)
4870 {
4871 revins_chars--;
4872 revins_legal++;
4873 }
4874 if (revins_on && gchar_cursor() == NUL)
4875 break;
4876#endif
4877 }
4878 /* Just a single backspace?: */
4879 if (mode == BACKSPACE_CHAR)
4880 break;
4881 } while (
4882#ifdef FEAT_RIGHTLEFT
4883 revins_on ||
4884#endif
4885 (curwin->w_cursor.col > mincol
4886 && (curwin->w_cursor.lnum != Insstart_orig.lnum
4887 || curwin->w_cursor.col != Insstart_orig.col)));
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 did_backspace = TRUE;
4890 }
4891#ifdef FEAT_SMARTINDENT
4892 did_si = FALSE;
4893 can_si = FALSE;
4894 can_si_back = FALSE;
4895#endif
4896 if (curwin->w_cursor.col <= 1)
4897 did_ai = FALSE;
4898 /*
4899 * It's a little strange to put backspaces into the redo
4900 * buffer, but it makes auto-indent a lot easier to deal
4901 * with.
4902 */
4903 AppendCharToRedobuff(c);
4904
4905 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004906 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004907 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004908 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
4910 /* vi behaviour: the cursor moves backward but the character that
4911 * was there remains visible
4912 * Vim behaviour: the cursor moves backward and the character that
4913 * was there is erased from the screen.
4914 * We can emulate the vi behaviour by pretending there is a dollar
4915 * displayed even when there isn't.
4916 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004917 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 dollar_vcol = curwin->w_virtcol;
4919
Bram Moolenaarce3be472008-01-14 19:12:28 +00004920#ifdef FEAT_FOLDING
4921 /* When deleting a char the cursor line must never be in a closed fold.
4922 * E.g., when 'foldmethod' is indent and deleting the first non-white
4923 * char before a Tab. */
4924 if (did_backspace)
4925 foldOpenCursor();
4926#endif
4927
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 return did_backspace;
4929}
4930
Bram Moolenaarec2da362017-01-21 20:04:22 +01004931/*
4932 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4933 * P_PE literally.
4934 * When "drop" is TRUE then consume the text and drop it.
4935 */
4936 int
4937bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4938{
4939 int c;
4940 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4941 int idx = 0;
4942 char_u *end = find_termcode((char_u *)"PE");
4943 int ret_char = -1;
4944 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004945 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004946
4947 /* If the end code is too long we can't detect it, read everything. */
4948 if (STRLEN(end) >= NUMBUFLEN)
4949 end = NULL;
4950 ++no_mapping;
4951 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004952 if (!p_paste)
4953 // Also have the side effects of setting 'paste' to make it work much
4954 // faster.
4955 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004956
Bram Moolenaarec2da362017-01-21 20:04:22 +01004957 for (;;)
4958 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004959 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004960 if (end == NULL && vpeekc() == NUL)
4961 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004962 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004963 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004964 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004965 if (c == NUL || got_int)
4966 // When CTRL-C was encountered the typeahead will be flushed and we
4967 // won't get the end sequence.
4968 break;
4969
Bram Moolenaarec2da362017-01-21 20:04:22 +01004970 if (has_mbyte)
4971 idx += (*mb_char2bytes)(c, buf + idx);
4972 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004973 buf[idx++] = c;
4974 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004975 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004976 {
4977 if (end[idx] == NUL)
4978 break; /* Found the end of paste code. */
4979 continue;
4980 }
4981 if (!drop)
4982 {
4983 switch (mode)
4984 {
4985 case PASTE_CMDLINE:
4986 put_on_cmdline(buf, idx, TRUE);
4987 break;
4988
4989 case PASTE_EX:
4990 if (gap != NULL && ga_grow(gap, idx) == OK)
4991 {
4992 mch_memmove((char *)gap->ga_data + gap->ga_len,
4993 buf, (size_t)idx);
4994 gap->ga_len += idx;
4995 }
4996 break;
4997
4998 case PASTE_INSERT:
4999 if (stop_arrow() == OK)
5000 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005001 c = buf[0];
5002 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
5003 ins_eol(c);
5004 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01005005 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005006 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01005007 AppendToRedobuffLit(buf, idx);
5008 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01005009 }
5010 break;
5011
5012 case PASTE_ONE_CHAR:
5013 if (ret_char == -1)
5014 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01005015 if (has_mbyte)
5016 ret_char = (*mb_ptr2char)(buf);
5017 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005018 ret_char = buf[0];
5019 }
5020 break;
5021 }
5022 }
5023 idx = 0;
5024 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005025
Bram Moolenaarec2da362017-01-21 20:04:22 +01005026 --no_mapping;
5027 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005028 if (!save_paste)
5029 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01005030
5031 return ret_char;
5032}
5033
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005034#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00005035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005036ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005037{
5038 /* We will be leaving the current window, unless closing another tab. */
5039 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
5040 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
5041 {
5042 undisplay_dollar();
5043 start_arrow(&curwin->w_cursor);
5044# ifdef FEAT_CINDENT
5045 can_cindent = TRUE;
5046# endif
5047 }
5048
5049 if (c == K_TABLINE)
5050 goto_tabpage(current_tab);
5051 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005052 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005053 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005054 redraw_statuslines(); /* will redraw the tabline when needed */
5055 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005056}
5057#endif
5058
5059#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005061ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062{
5063 pos_T tpos;
5064
5065 undisplay_dollar();
5066 tpos = curwin->w_cursor;
5067 if (gui_do_scroll())
5068 {
5069 start_arrow(&tpos);
5070# ifdef FEAT_CINDENT
5071 can_cindent = TRUE;
5072# endif
5073 }
5074}
5075
5076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005077ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 pos_T tpos;
5080
5081 undisplay_dollar();
5082 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005083 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 start_arrow(&tpos);
5086# ifdef FEAT_CINDENT
5087 can_cindent = TRUE;
5088# endif
5089 }
5090}
5091#endif
5092
5093 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005094ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
5096 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005097 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
5099#ifdef FEAT_FOLDING
5100 if ((fdo_flags & FDO_HOR) && KeyTyped)
5101 foldOpenCursor();
5102#endif
5103 undisplay_dollar();
5104 tpos = curwin->w_cursor;
5105 if (oneleft() == OK)
5106 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005107#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5108 /* Only call start_arrow() when not busy with preediting, it will
5109 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005110 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005111#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005112 {
5113 start_arrow_with_change(&tpos, end_change);
5114 if (!end_change)
5115 AppendCharToRedobuff(K_LEFT);
5116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#ifdef FEAT_RIGHTLEFT
5118 /* If exit reversed string, position is fixed */
5119 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5120 revins_legal++;
5121 revins_chars++;
5122#endif
5123 }
5124
5125 /*
5126 * if 'whichwrap' set for cursor in insert mode may go to
5127 * previous line
5128 */
5129 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5130 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005131 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 start_arrow(&tpos);
5133 --(curwin->w_cursor.lnum);
5134 coladvance((colnr_T)MAXCOL);
5135 curwin->w_set_curswant = TRUE; /* so we stay at the end */
5136 }
5137 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005138 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005139 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140}
5141
5142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005143ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144{
5145 pos_T tpos;
5146
5147#ifdef FEAT_FOLDING
5148 if ((fdo_flags & FDO_HOR) && KeyTyped)
5149 foldOpenCursor();
5150#endif
5151 undisplay_dollar();
5152 tpos = curwin->w_cursor;
5153 if (c == K_C_HOME)
5154 curwin->w_cursor.lnum = 1;
5155 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 curwin->w_curswant = 0;
5158 start_arrow(&tpos);
5159}
5160
5161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005162ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
5164 pos_T tpos;
5165
5166#ifdef FEAT_FOLDING
5167 if ((fdo_flags & FDO_HOR) && KeyTyped)
5168 foldOpenCursor();
5169#endif
5170 undisplay_dollar();
5171 tpos = curwin->w_cursor;
5172 if (c == K_C_END)
5173 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5174 coladvance((colnr_T)MAXCOL);
5175 curwin->w_curswant = MAXCOL;
5176
5177 start_arrow(&tpos);
5178}
5179
5180 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005181ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005183 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184#ifdef FEAT_FOLDING
5185 if ((fdo_flags & FDO_HOR) && KeyTyped)
5186 foldOpenCursor();
5187#endif
5188 undisplay_dollar();
5189 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5190 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005191 start_arrow_with_change(&curwin->w_cursor, end_change);
5192 if (!end_change)
5193 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 (void)bck_word(1L, FALSE, FALSE);
5195 curwin->w_set_curswant = TRUE;
5196 }
5197 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005198 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005199 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200}
5201
5202 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005203ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005205 int end_change = dont_sync_undo == FALSE; // end undoable change
5206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#ifdef FEAT_FOLDING
5208 if ((fdo_flags & FDO_HOR) && KeyTyped)
5209 foldOpenCursor();
5210#endif
5211 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005212 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005214 start_arrow_with_change(&curwin->w_cursor, end_change);
5215 if (!end_change)
5216 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if (virtual_active())
5219 oneright();
5220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005223 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 ++curwin->w_cursor.col;
5226 }
5227
5228#ifdef FEAT_RIGHTLEFT
5229 revins_legal++;
5230 if (revins_chars)
5231 revins_chars--;
5232#endif
5233 }
5234 /* if 'whichwrap' set for cursor in insert mode, may move the
5235 * cursor to the next line */
5236 else if (vim_strchr(p_ww, ']') != NULL
5237 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5238 {
5239 start_arrow(&curwin->w_cursor);
5240 curwin->w_set_curswant = TRUE;
5241 ++curwin->w_cursor.lnum;
5242 curwin->w_cursor.col = 0;
5243 }
5244 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005245 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005246 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247}
5248
5249 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005250ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005252 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253#ifdef FEAT_FOLDING
5254 if ((fdo_flags & FDO_HOR) && KeyTyped)
5255 foldOpenCursor();
5256#endif
5257 undisplay_dollar();
5258 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5259 || gchar_cursor() != NUL)
5260 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005261 start_arrow_with_change(&curwin->w_cursor, end_change);
5262 if (!end_change)
5263 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 (void)fwd_word(1L, FALSE, 0);
5265 curwin->w_set_curswant = TRUE;
5266 }
5267 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005268 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005269 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270}
5271
5272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005273ins_up(
5274 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275{
5276 pos_T tpos;
5277 linenr_T old_topline = curwin->w_topline;
5278#ifdef FEAT_DIFF
5279 int old_topfill = curwin->w_topfill;
5280#endif
5281
5282 undisplay_dollar();
5283 tpos = curwin->w_cursor;
5284 if (cursor_up(1L, TRUE) == OK)
5285 {
5286 if (startcol)
5287 coladvance(getvcol_nolist(&Insstart));
5288 if (old_topline != curwin->w_topline
5289#ifdef FEAT_DIFF
5290 || old_topfill != curwin->w_topfill
5291#endif
5292 )
5293 redraw_later(VALID);
5294 start_arrow(&tpos);
5295#ifdef FEAT_CINDENT
5296 can_cindent = TRUE;
5297#endif
5298 }
5299 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005300 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301}
5302
5303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005304ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305{
5306 pos_T tpos;
5307
5308 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005309
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005310 if (mod_mask & MOD_MASK_CTRL)
5311 {
5312 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005313 if (first_tabpage->tp_next != NULL)
5314 {
5315 start_arrow(&curwin->w_cursor);
5316 goto_tabpage(-1);
5317 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005318 return;
5319 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 tpos = curwin->w_cursor;
5322 if (onepage(BACKWARD, 1L) == OK)
5323 {
5324 start_arrow(&tpos);
5325#ifdef FEAT_CINDENT
5326 can_cindent = TRUE;
5327#endif
5328 }
5329 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005330 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331}
5332
5333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005334ins_down(
5335 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 pos_T tpos;
5338 linenr_T old_topline = curwin->w_topline;
5339#ifdef FEAT_DIFF
5340 int old_topfill = curwin->w_topfill;
5341#endif
5342
5343 undisplay_dollar();
5344 tpos = curwin->w_cursor;
5345 if (cursor_down(1L, TRUE) == OK)
5346 {
5347 if (startcol)
5348 coladvance(getvcol_nolist(&Insstart));
5349 if (old_topline != curwin->w_topline
5350#ifdef FEAT_DIFF
5351 || old_topfill != curwin->w_topfill
5352#endif
5353 )
5354 redraw_later(VALID);
5355 start_arrow(&tpos);
5356#ifdef FEAT_CINDENT
5357 can_cindent = TRUE;
5358#endif
5359 }
5360 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005361 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362}
5363
5364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005365ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 pos_T tpos;
5368
5369 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005370
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005371 if (mod_mask & MOD_MASK_CTRL)
5372 {
5373 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005374 if (first_tabpage->tp_next != NULL)
5375 {
5376 start_arrow(&curwin->w_cursor);
5377 goto_tabpage(0);
5378 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005379 return;
5380 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005381
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 tpos = curwin->w_cursor;
5383 if (onepage(FORWARD, 1L) == OK)
5384 {
5385 start_arrow(&tpos);
5386#ifdef FEAT_CINDENT
5387 can_cindent = TRUE;
5388#endif
5389 }
5390 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005391 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392}
5393
5394#ifdef FEAT_DND
5395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005396ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5399}
5400#endif
5401
5402/*
5403 * Handle TAB in Insert or Replace mode.
5404 * Return TRUE when the TAB needs to be inserted like a normal character.
5405 */
5406 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005407ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
5409 int ind;
5410 int i;
5411 int temp;
5412
5413 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5414 Insstart_blank_vcol = get_nolist_virtcol();
5415 if (echeck_abbr(TAB + ABBR_OFF))
5416 return FALSE;
5417
5418 ind = inindent(0);
5419#ifdef FEAT_CINDENT
5420 if (ind)
5421 can_cindent = FALSE;
5422#endif
5423
5424 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005425 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 */
5427 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005428#ifdef FEAT_VARTABS
5429 && !(p_sta && ind
5430 /* These five lines mean 'tabstop' != 'shiftwidth' */
5431 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5432 || (tabstop_count(curbuf->b_p_vts_array) == 1
5433 && tabstop_first(curbuf->b_p_vts_array)
5434 != get_sw_value(curbuf))
5435 || (tabstop_count(curbuf->b_p_vts_array) == 0
5436 && curbuf->b_p_ts != get_sw_value(curbuf))))
5437 && tabstop_count(curbuf->b_p_vsts_array) == 0
5438#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005439 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005440#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005441 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 return TRUE;
5443
5444 if (stop_arrow() == FAIL)
5445 return TRUE;
5446
5447 did_ai = FALSE;
5448#ifdef FEAT_SMARTINDENT
5449 did_si = FALSE;
5450 can_si = FALSE;
5451 can_si_back = FALSE;
5452#endif
5453 AppendToRedobuff((char_u *)"\t");
5454
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005455#ifdef FEAT_VARTABS
5456 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
5457 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005458 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005459 temp -= get_nolist_virtcol() % temp;
5460 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005461 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005462 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005463 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005464 curbuf->b_p_vsts_array);
5465 else /* otherwise use 'tabstop' */
5466 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5467 curbuf->b_p_vts_array);
5468#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005470 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005471 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
5472 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 else /* otherwise use 'tabstop' */
5474 temp = (int)curbuf->b_p_ts;
5475 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
5478 /*
5479 * Insert the first space with ins_char(). It will delete one char in
5480 * replace mode. Insert the rest with ins_str(); it will not delete any
5481 * chars. For VREPLACE mode, we use ins_char() for all characters.
5482 */
5483 ins_char(' ');
5484 while (--temp > 0)
5485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 if (State & VREPLACE_FLAG)
5487 ins_char(' ');
5488 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 {
5490 ins_str((char_u *)" ");
5491 if (State & REPLACE_FLAG) /* no char replaced */
5492 replace_push(NUL);
5493 }
5494 }
5495
5496 /*
5497 * When 'expandtab' not set: Replace spaces by TABs where possible.
5498 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005499#ifdef FEAT_VARTABS
5500 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5501 || get_sts_value() > 0
5502 || (p_sta && ind)))
5503#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005504 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 char_u *saved_line = NULL; /* init for GCC */
5509 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 pos_T fpos;
5511 pos_T *cursor;
5512 colnr_T want_vcol, vcol;
5513 int change_col = -1;
5514 int save_list = curwin->w_p_list;
5515
5516 /*
5517 * Get the current line. For VREPLACE mode, don't make real changes
5518 * yet, just work on a copy of the line.
5519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (State & VREPLACE_FLAG)
5521 {
5522 pos = curwin->w_cursor;
5523 cursor = &pos;
5524 saved_line = vim_strsave(ml_get_curline());
5525 if (saved_line == NULL)
5526 return FALSE;
5527 ptr = saved_line + pos.col;
5528 }
5529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
5531 ptr = ml_get_cursor();
5532 cursor = &curwin->w_cursor;
5533 }
5534
5535 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
5536 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5537 curwin->w_p_list = FALSE;
5538
5539 /* Find first white before the cursor */
5540 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005541 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
5543 --fpos.col;
5544 --ptr;
5545 }
5546
5547 /* In Replace mode, don't change characters before the insert point. */
5548 if ((State & REPLACE_FLAG)
5549 && fpos.lnum == Insstart.lnum
5550 && fpos.col < Insstart.col)
5551 {
5552 ptr += Insstart.col - fpos.col;
5553 fpos.col = Insstart.col;
5554 }
5555
5556 /* compute virtual column numbers of first white and cursor */
5557 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5558 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5559
Bram Moolenaar597a4222014-06-25 14:39:50 +02005560 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5561 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01005562 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005564 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 if (vcol + i > want_vcol)
5566 break;
5567 if (*ptr != TAB)
5568 {
5569 *ptr = TAB;
5570 if (change_col < 0)
5571 {
5572 change_col = fpos.col; /* Column of first change */
5573 /* May have to adjust Insstart */
5574 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5575 Insstart.col = fpos.col;
5576 }
5577 }
5578 ++fpos.col;
5579 ++ptr;
5580 vcol += i;
5581 }
5582
5583 if (change_col >= 0)
5584 {
5585 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005586 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
5588 /* Skip over the spaces we need. */
5589 while (vcol < want_vcol && *ptr == ' ')
5590 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005591 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 ++ptr;
5593 ++repl_off;
5594 }
5595 if (vcol > want_vcol)
5596 {
5597 /* Must have a char with 'showbreak' just before it. */
5598 --ptr;
5599 --repl_off;
5600 }
5601 fpos.col += repl_off;
5602
5603 /* Delete following spaces. */
5604 i = cursor->col - fpos.col;
5605 if (i > 0)
5606 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005607 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005609 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 for (temp = i; --temp >= 0; )
5611 replace_join(repl_off);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005612#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005613 curbuf->b_ml.ml_line_len -= i;
5614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005616#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005617 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005618 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005619 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005620 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5621 (char_u *)"\t", 1);
5622 }
5623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 cursor->col -= i;
5625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 /*
5627 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5628 * backspacing over the changed spacing and then inserting the new
5629 * spacing.
5630 */
5631 if (State & VREPLACE_FLAG)
5632 {
5633 /* Backspace from real cursor to change_col */
5634 backspace_until_column(change_col);
5635
5636 /* Insert each char in saved_line from changed_col to
5637 * ptr-cursor */
5638 ins_bytes_len(saved_line + change_col,
5639 cursor->col - change_col);
5640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 if (State & VREPLACE_FLAG)
5644 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 curwin->w_p_list = save_list;
5646 }
5647
5648 return FALSE;
5649}
5650
5651/*
5652 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005653 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005656ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 int i;
5659
5660 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005661 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005663 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 undisplay_dollar();
5665
5666 /*
5667 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5668 * character under the cursor. Only push a NUL on the replace stack,
5669 * nothing to put back when the NL is deleted.
5670 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005671 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 replace_push(NUL);
5673
5674 /*
5675 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5676 * replacing the next line, so we push all of the characters left on the
5677 * line onto the replace stack. This is not done here though, it is done
5678 * in open_line().
5679 */
5680
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005681 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
5682 * CTRL-O). */
5683 if (virtual_active() && curwin->w_cursor.coladd > 0)
5684 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 /* NL in reverse insert will always start in the end of
5688 * current line. */
5689 if (revins_on)
5690 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5691#endif
5692
5693 AppendToRedobuff(NL_STR);
5694 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005695 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 old_indent = 0;
5697#ifdef FEAT_CINDENT
5698 can_cindent = TRUE;
5699#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005700#ifdef FEAT_FOLDING
5701 /* When inserting a line the cursor line must never be in a closed fold. */
5702 foldOpenCursor();
5703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005705 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706}
5707
5708#ifdef FEAT_DIGRAPHS
5709/*
5710 * Handle digraph in insert mode.
5711 * Returns character still to be inserted, or NUL when nothing remaining to be
5712 * done.
5713 */
5714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005715ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 int c;
5718 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005719 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
5721 pc_status = PC_STATUS_UNSET;
5722 if (redrawing() && !char_avail())
5723 {
5724 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005725 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
5727 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005728 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729#ifdef FEAT_CMDL_INFO
5730 add_to_showcmd_c(Ctrl_K);
5731#endif
5732 }
5733
5734#ifdef USE_ON_FLY_SCROLL
5735 dont_scroll = TRUE; /* disallow scrolling here */
5736#endif
5737
5738 /* don't map the digraph chars. This also prevents the
5739 * mode message to be deleted when ESC is hit */
5740 ++no_mapping;
5741 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005742 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 --no_mapping;
5744 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005745 if (did_putchar)
5746 /* when the line fits in 'columns' the '?' is at the start of the next
5747 * line and will not be removed by the redraw */
5748 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005749
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (IS_SPECIAL(c) || mod_mask) /* special key */
5751 {
5752#ifdef FEAT_CMDL_INFO
5753 clear_showcmd();
5754#endif
5755 insert_special(c, TRUE, FALSE);
5756 return NUL;
5757 }
5758 if (c != ESC)
5759 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005760 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 if (redrawing() && !char_avail())
5762 {
5763 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005764 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 if (char2cells(c) == 1)
5767 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005768 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005770 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 }
5772#ifdef FEAT_CMDL_INFO
5773 add_to_showcmd_c(c);
5774#endif
5775 }
5776 ++no_mapping;
5777 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005778 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 --no_mapping;
5780 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005781 if (did_putchar)
5782 /* when the line fits in 'columns' the '?' is at the start of the
5783 * next line and will not be removed by a redraw */
5784 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 if (cc != ESC)
5786 {
5787 AppendToRedobuff((char_u *)CTRL_V_STR);
5788 c = getdigraph(c, cc, TRUE);
5789#ifdef FEAT_CMDL_INFO
5790 clear_showcmd();
5791#endif
5792 return c;
5793 }
5794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795#ifdef FEAT_CMDL_INFO
5796 clear_showcmd();
5797#endif
5798 return NUL;
5799}
5800#endif
5801
5802/*
5803 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5804 * Returns the char to be inserted, or NUL if none found.
5805 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005807ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
5809 int c;
5810 int temp;
5811 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005812 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813
5814 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5815 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005816 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 return NUL;
5818 }
5819
5820 /* try to advance to the cursor column */
5821 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005822 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 prev_ptr = ptr;
5824 validate_virtcol();
5825 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5826 {
5827 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005828 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830 if ((colnr_T)temp > curwin->w_virtcol)
5831 ptr = prev_ptr;
5832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005835 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 return c;
5837}
5838
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005839/*
5840 * CTRL-Y or CTRL-E typed in Insert mode.
5841 */
5842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005843ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005844{
5845 int c = tc;
5846
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005847 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005848 {
5849 if (c == Ctrl_Y)
5850 scrolldown_clamp();
5851 else
5852 scrollup_clamp();
5853 redraw_later(VALID);
5854 }
5855 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005856 {
5857 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5858 if (c != NUL)
5859 {
5860 long tw_save;
5861
5862 /* The character must be taken literally, insert like it
5863 * was typed after a CTRL-V, and pretend 'textwidth'
5864 * wasn't set. Digits, 'o' and 'x' are special after a
5865 * CTRL-V, don't use it for these. */
5866 if (c < 256 && !isalnum(c))
5867 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
5868 tw_save = curbuf->b_p_tw;
5869 curbuf->b_p_tw = -1;
5870 insert_special(c, TRUE, FALSE);
5871 curbuf->b_p_tw = tw_save;
5872#ifdef FEAT_RIGHTLEFT
5873 revins_chars++;
5874 revins_legal++;
5875#endif
5876 c = Ctrl_V; /* pretend CTRL-V is last character */
5877 auto_format(FALSE, TRUE);
5878 }
5879 }
5880 return c;
5881}
5882
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883/*
5884 * Get the value that w_virtcol would have when 'list' is off.
5885 * Unless 'cpo' contains the 'L' flag.
5886 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005887 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005888get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005890 // check validity of cursor in current buffer
5891 if (curwin->w_buffer == NULL
5892 || curwin->w_buffer->b_ml.ml_mfp == NULL
5893 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5894 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5896 return getvcol_nolist(&curwin->w_cursor);
5897 validate_virtcol();
5898 return curwin->w_virtcol;
5899}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005900
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005901#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005902/*
5903 * Handle the InsertCharPre autocommand.
5904 * "c" is the character that was typed.
5905 * Return a pointer to allocated memory with the replacement string.
5906 * Return NULL to continue inserting "c".
5907 */
5908 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005909do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005910{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005911 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005912 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005913 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005914
5915 /* Return quickly when there is nothing to do. */
5916 if (!has_insertcharpre())
5917 return NULL;
5918
Bram Moolenaar704984a2012-06-01 14:57:51 +02005919 if (has_mbyte)
5920 buf[(*mb_char2bytes)(c, buf)] = NUL;
5921 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005922 {
5923 buf[0] = c;
5924 buf[1] = NUL;
5925 }
5926
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005927 /* Lock the text to avoid weird things from happening. */
5928 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005929 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005930
Bram Moolenaar704984a2012-06-01 14:57:51 +02005931 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005932 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005933 {
5934 /* Get the value of v:char. It may be empty or more than one
5935 * character. Only use it when changed, otherwise continue with the
5936 * original character to avoid breaking autoindent. */
5937 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5938 res = vim_strsave(get_vim_var_str(VV_CHAR));
5939 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005940
5941 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
5942 --textlock;
5943
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005944 // Restore the State, it may have been changed.
5945 State = save_State;
5946
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005947 return res;
5948}
5949#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005950
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005951#if defined(FEAT_CINDENT) || defined(PROTO)
5952 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005953get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005954{
5955 return can_cindent;
5956}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005957
5958 void
5959set_can_cindent(int val)
5960{
5961 can_cindent = val;
5962}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005963#endif
5964
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005965/*
5966 * Trigger "event" and take care of fixing undo.
5967 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005968 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005969ins_apply_autocmds(event_T event)
5970{
5971 varnumber_T tick = CHANGEDTICK(curbuf);
5972 int r;
5973
5974 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5975
5976 // If u_savesub() was called then we are not prepared to start
5977 // a new line. Call u_save() with no contents to fix that.
5978 if (tick != CHANGEDTICK(curbuf))
5979 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5980
5981 return r;
5982}