blob: 1c168b6a8bbd7d48afaa905b3105cd153705f691 [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 Moolenaar3397f742019-06-02 18:40:06 +02001420# ifdef FEAT_TEXT_PROP
1421 || 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 Moolenaar3397f742019-06-02 18:40:06 +02001445#ifdef FEAT_TEXT_PROP
1446 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 Moolenaar071d4272004-06-13 20:20:40 +00001534
1535 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001536 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
1538 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001541 did_putchar = TRUE;
1542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1544
1545#ifdef FEAT_CMDL_INFO
1546 add_to_showcmd_c(Ctrl_V);
1547#endif
1548
1549 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001550 if (did_putchar)
1551 /* when the line fits in 'columns' the '^' is at the start of the next
1552 * line and will not removed by the redraw */
1553 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554#ifdef FEAT_CMDL_INFO
1555 clear_showcmd();
1556#endif
1557 insert_special(c, FALSE, TRUE);
1558#ifdef FEAT_RIGHTLEFT
1559 revins_chars++;
1560 revins_legal++;
1561#endif
1562}
1563
1564/*
1565 * Put a character directly onto the screen. It's not stored in a buffer.
1566 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1567 */
1568static int pc_status;
1569#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1570#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1571#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1572#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574static int pc_attr;
1575static int pc_row;
1576static int pc_col;
1577
1578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 int attr;
1582
1583 if (ScreenLines != NULL)
1584 {
1585 update_topline(); /* just in case w_topline isn't valid */
1586 validate_cursor();
1587 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001588 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 else
1590 attr = 0;
1591 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001592 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594#ifdef FEAT_RIGHTLEFT
1595 if (curwin->w_p_rl)
1596 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001597 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (has_mbyte)
1599 {
1600 int fix_col = mb_fix_col(pc_col, pc_row);
1601
1602 if (fix_col != pc_col)
1603 {
1604 screen_putchar(' ', pc_row, fix_col, attr);
1605 --curwin->w_wcol;
1606 pc_status = PC_STATUS_RIGHT;
1607 }
1608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610 else
1611#endif
1612 {
1613 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (mb_lefthalve(pc_row, pc_col))
1615 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
1617
1618 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1622 pc_status = PC_STATUS_SET;
1623 }
1624 screen_putchar(c, pc_row, pc_col, attr);
1625 }
1626}
1627
Bram Moolenaarf2732452018-06-03 14:47:35 +02001628#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1629/*
1630 * Return the effective prompt for the current buffer.
1631 */
1632 char_u *
1633prompt_text(void)
1634{
1635 if (curbuf->b_prompt_text == NULL)
1636 return (char_u *)"% ";
1637 return curbuf->b_prompt_text;
1638}
1639
1640/*
1641 * Prepare for prompt mode: Make sure the last line has the prompt text.
1642 * Move the cursor to this line.
1643 */
1644 static void
1645init_prompt(int cmdchar_todo)
1646{
1647 char_u *prompt = prompt_text();
1648 char_u *text;
1649
1650 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1651 text = ml_get_curline();
1652 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1653 {
1654 // prompt is missing, insert it or append a line with it
1655 if (*text == NUL)
1656 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1657 else
1658 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1659 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1660 coladvance((colnr_T)MAXCOL);
1661 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1662 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001663
1664 // Insert always starts after the prompt, allow editing text after it.
1665 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1666 || Insstart_orig.col != (int)STRLEN(prompt))
1667 {
1668 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001669 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001670 Insstart_orig = Insstart;
1671 Insstart_textlen = Insstart.col;
1672 Insstart_blank_vcol = MAXCOL;
1673 arrow_used = FALSE;
1674 }
1675
Bram Moolenaarf2732452018-06-03 14:47:35 +02001676 if (cmdchar_todo == 'A')
1677 coladvance((colnr_T)MAXCOL);
1678 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001679 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001680 /* Make sure the cursor is in a valid position. */
1681 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001682}
1683
1684/*
1685 * Return TRUE if the cursor is in the editable position of the prompt line.
1686 */
1687 int
1688prompt_curpos_editable()
1689{
1690 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1691 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1692}
1693#endif
1694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695/*
1696 * Undo the previous edit_putchar().
1697 */
1698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (pc_status == PC_STATUS_RIGHT)
1704 ++curwin->w_wcol;
1705 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001706 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1709 }
1710}
1711
1712/*
1713 * Called when p_dollar is set: display a '$' at the end of the changed text
1714 * Only works when cursor is in the line that changes.
1715 */
1716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001717display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 colnr_T save_col;
1720
1721 if (!redrawing())
1722 return;
1723
1724 cursor_off();
1725 save_col = curwin->w_cursor.col;
1726 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (has_mbyte)
1728 {
1729 char_u *p;
1730
1731 /* If on the last byte of a multi-byte move to the first byte. */
1732 p = ml_get_curline();
1733 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001736 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
1738 edit_putchar('$', FALSE);
1739 dollar_vcol = curwin->w_virtcol;
1740 }
1741 curwin->w_cursor.col = save_col;
1742}
1743
1744/*
1745 * Call this function before moving the cursor from the normal insert position
1746 * in insert mode.
1747 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001749undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001751 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001753 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001754 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756}
1757
1758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 * Truncate the space at the end of a line. This is to be used only in an
1760 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1761 * modes.
1762 */
1763 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001764truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
1766 int i;
1767
1768 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001769 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
1771 if (State & REPLACE_FLAG)
1772 replace_join(0); /* remove a NUL from the replace stack */
1773 }
1774 line[i + 1] = NUL;
1775}
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777/*
1778 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1779 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001780 * Will attempt not to go before "col" even when there is a composing
1781 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 */
1783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001784backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 while ((int)curwin->w_cursor.col > col)
1787 {
1788 curwin->w_cursor.col--;
1789 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001790 replace_do_bs(col);
1791 else if (!del_char_after_col(col))
1792 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
1794}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001796/*
1797 * Like del_char(), but make sure not to go before column "limit_col".
1798 * Only matters when there are composing characters.
1799 * Return TRUE when something was deleted.
1800 */
1801 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001803{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001804 if (enc_utf8 && limit_col >= 0)
1805 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001806 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001807
1808 /* Make sure the cursor is at the start of a character, but
1809 * skip forward again when going too far back because of a
1810 * composing character. */
1811 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001812 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001813 {
1814 int l = utf_ptr2len(ml_get_cursor());
1815
1816 if (l == 0) /* end of line */
1817 break;
1818 curwin->w_cursor.col += l;
1819 }
1820 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1821 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001822 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001823 }
1824 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001825 (void)del_char(FALSE);
1826 return TRUE;
1827}
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829/*
1830 * Next character is interpreted literally.
1831 * A one, two or three digit decimal number is interpreted as its byte value.
1832 * If one or two digits are entered, the next character is given to vungetc().
1833 * For Unicode a character > 255 may be returned.
1834 */
1835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001836get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 int cc;
1839 int nc;
1840 int i;
1841 int hex = FALSE;
1842 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
1845 if (got_int)
1846 return Ctrl_C;
1847
1848#ifdef FEAT_GUI
1849 /*
1850 * In GUI there is no point inserting the internal code for a special key.
1851 * It is more useful to insert the string "<KEY>" instead. This would
1852 * probably be useful in a text window too, but it would not be
1853 * vi-compatible (maybe there should be an option for it?) -- webb
1854 */
1855 if (gui.in_use)
1856 ++allow_keys;
1857#endif
1858#ifdef USE_ON_FLY_SCROLL
1859 dont_scroll = TRUE; /* disallow scrolling here */
1860#endif
1861 ++no_mapping; /* don't map the next key hits */
1862 cc = 0;
1863 i = 0;
1864 for (;;)
1865 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001866 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001868 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 add_to_showcmd(nc);
1870#endif
1871 if (nc == 'x' || nc == 'X')
1872 hex = TRUE;
1873 else if (nc == 'o' || nc == 'O')
1874 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 else if (nc == 'u' || nc == 'U')
1876 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 else
1878 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001879 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
1881 if (!vim_isxdigit(nc))
1882 break;
1883 cc = cc * 16 + hex2nr(nc);
1884 }
1885 else if (octal)
1886 {
1887 if (nc < '0' || nc > '7')
1888 break;
1889 cc = cc * 8 + nc - '0';
1890 }
1891 else
1892 {
1893 if (!VIM_ISDIGIT(nc))
1894 break;
1895 cc = cc * 10 + nc - '0';
1896 }
1897
1898 ++i;
1899 }
1900
Bram Moolenaar13505972019-01-24 15:04:48 +01001901 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 cc = 255; /* limit range to 0-255 */
1903 nc = 0;
1904
1905 if (hex) /* hex: up to two chars */
1906 {
1907 if (i >= 2)
1908 break;
1909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 else if (unicode) /* Unicode: up to four or eight chars */
1911 {
1912 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1913 break;
1914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 else if (i >= 3) /* decimal or octal: up to three chars */
1916 break;
1917 }
1918 if (i == 0) /* no number entered */
1919 {
1920 if (nc == K_ZERO) /* NUL is stored as NL */
1921 {
1922 cc = '\n';
1923 nc = 0;
1924 }
1925 else
1926 {
1927 cc = nc;
1928 nc = 0;
1929 }
1930 }
1931
1932 if (cc == 0) /* NUL is stored as NL */
1933 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001934 if (enc_dbcs && (cc & 0xff) == 0)
1935 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
1936 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 --no_mapping;
1939#ifdef FEAT_GUI
1940 if (gui.in_use)
1941 --allow_keys;
1942#endif
1943 if (nc)
1944 vungetc(nc);
1945 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
1946 return cc;
1947}
1948
1949/*
1950 * Insert character, taking care of special keys and mod_mask
1951 */
1952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001953insert_special(
1954 int c,
1955 int allow_modmask,
1956 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958 char_u *p;
1959 int len;
1960
1961 /*
1962 * Special function key, translate into "<Key>". Up to the last '>' is
1963 * inserted with ins_str(), so as not to replace characters in replace
1964 * mode.
1965 * Only use mod_mask for special keys, to avoid things like <S-Space>,
1966 * unless 'allow_modmask' is TRUE.
1967 */
Bram Moolenaard0573012017-10-28 21:11:06 +02001968#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 /* Command-key never produces a normal key */
1970 if (mod_mask & MOD_MASK_CMD)
1971 allow_modmask = TRUE;
1972#endif
1973 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
1974 {
1975 p = get_special_key_name(c, mod_mask);
1976 len = (int)STRLEN(p);
1977 c = p[len - 1];
1978 if (len > 2)
1979 {
1980 if (stop_arrow() == FAIL)
1981 return;
1982 p[len - 1] = NUL;
1983 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00001984 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 ctrlv = FALSE;
1986 }
1987 }
1988 if (stop_arrow() == OK)
1989 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
1990}
1991
1992/*
1993 * Special characters in this context are those that need processing other
1994 * than the simple insertion that can be performed here. This includes ESC
1995 * which terminates the insert, and CR/NL which need special processing to
1996 * open up a new line. This routine tries to optimize insertions performed by
1997 * the "redo", "undo" or "put" commands, so it needs to know when it should
1998 * stop and defer processing to the "normal" mechanism.
1999 * '0' and '^' are special, because they can be followed by CTRL-D.
2000 */
2001#ifdef EBCDIC
2002# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2003#else
2004# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2005#endif
2006
Bram Moolenaar13505972019-01-24 15:04:48 +01002007#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002009/*
2010 * "flags": INSCHAR_FORMAT - force formatting
2011 * INSCHAR_CTRLV - char typed just after CTRL-V
2012 * INSCHAR_NO_FEX - don't use 'formatexpr'
2013 *
2014 * NOTE: passes the flags value straight through to internal_format() which,
2015 * beside INSCHAR_FORMAT (above), is also looking for these:
2016 * INSCHAR_DO_COM - format comments
2017 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002020insertchar(
2021 int c, /* character to insert or NUL */
2022 int flags, /* INSCHAR_FORMAT, etc. */
2023 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002028 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002030 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 /*
2034 * Try to break the line in two or more pieces when:
2035 * - Always do this if we have been called to do formatting only.
2036 * - Always do this when 'formatoptions' has the 'a' flag and the line
2037 * ends in white space.
2038 * - Otherwise:
2039 * - Don't do this if inserting a blank
2040 * - Don't do this if an existing character is being replaced, unless
2041 * we're in VREPLACE mode.
2042 * - Do this if the cursor is not on the line where insert started
2043 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2044 * before the insert.
2045 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2046 * before 'textwidth'
2047 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002048 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002049 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002050 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 && *ml_get_cursor() != NUL)
2054 && (curwin->w_cursor.lnum != Insstart.lnum
2055 || ((!has_format_option(FO_INS_LONG)
2056 || Insstart_textlen <= (colnr_T)textwidth)
2057 && (!fo_ins_blank
2058 || Insstart_blank_vcol <= (colnr_T)textwidth
2059 ))))))
2060 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002061 /* Format with 'formatexpr' when it's set. Use internal formatting
2062 * when 'formatexpr' isn't set or it returns non-zero. */
2063#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002064 int do_internal = TRUE;
2065 colnr_T virtcol = get_nolist_virtcol()
2066 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002067
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002068 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2069 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002070 {
2071 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
2072 /* It may be required to save for undo again, e.g. when setline()
2073 * was called. */
2074 ins_need_undo = TRUE;
2075 }
2076 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002078 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (c == NUL) /* only formatting was wanted */
2082 return;
2083
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002084 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if (did_ai && (int)c == end_comment_pending)
2086 {
2087 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002088 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 int middle_len, end_len;
2090 int i;
2091
2092 /*
2093 * Need to remove existing (middle) comment leader and insert end
2094 * comment leader. First, check what comment leader we can find.
2095 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002096 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002097 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002099 // Skip middle-comment string
2100 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 ++p;
2102 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002103 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002104 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 --middle_len;
2106
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002107 // Find the end-comment string
2108 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 ++p;
2110 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2111
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002112 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002114 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 ;
2116 i++;
2117
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002118 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 i -= middle_len;
2120
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002121 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2123 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002124 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 backspace_until_column(i);
2126
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002127 // Insert the end-comment string, except for the last
2128 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 ins_bytes_len(lead_end, end_len - 1);
2130 }
2131 }
2132 }
2133 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135 did_ai = FALSE;
2136#ifdef FEAT_SMARTINDENT
2137 did_si = FALSE;
2138 can_si = FALSE;
2139 can_si_back = FALSE;
2140#endif
2141
2142 /*
2143 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2144 * This speeds up normal text input considerably.
2145 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2146 * need to re-indent at a ':', or any other character (but not what
2147 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002148 * Don't do this when there an InsertCharPre autocommand is defined,
2149 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002150 * Do the check for InsertCharPre before the call to vpeekc() because the
2151 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 */
2153#ifdef USE_ON_FLY_SCROLL
2154 dont_scroll = FALSE; /* allow scrolling here */
2155#endif
2156
2157 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002159 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 && vpeekc() != NUL
2161 && !(State & REPLACE_FLAG)
2162#ifdef FEAT_CINDENT
2163 && !cindent_on()
2164#endif
2165#ifdef FEAT_RIGHTLEFT
2166 && !p_ri
2167#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002168 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170#define INPUT_BUFLEN 100
2171 char_u buf[INPUT_BUFLEN + 1];
2172 int i;
2173 colnr_T virtcol = 0;
2174
2175 buf[0] = c;
2176 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002177 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 virtcol = get_nolist_virtcol();
2179 /*
2180 * Stop the string when:
2181 * - no more chars available
2182 * - finding a special character (command key)
2183 * - buffer is full
2184 * - running into the 'textwidth' boundary
2185 * - need to check for abbreviation: A non-word char after a word-char
2186 */
2187 while ( (c = vpeekc()) != NUL
2188 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 && i < INPUT_BUFLEN
2191 && (textwidth == 0
2192 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2193 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2194 {
2195#ifdef FEAT_RIGHTLEFT
2196 c = vgetc();
2197 if (p_hkmap && KeyTyped)
2198 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 buf[i++] = c;
2200#else
2201 buf[i++] = vgetc();
2202#endif
2203 }
2204
2205#ifdef FEAT_DIGRAPHS
2206 do_digraph(-1); /* clear digraphs */
2207 do_digraph(buf[i-1]); /* may be the start of a digraph */
2208#endif
2209 buf[i] = NUL;
2210 ins_str(buf);
2211 if (flags & INSCHAR_CTRLV)
2212 {
2213 redo_literal(*buf);
2214 i = 1;
2215 }
2216 else
2217 i = 0;
2218 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002219 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 }
2221 else
2222 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002223 int cc;
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2226 {
2227 char_u buf[MB_MAXBYTES + 1];
2228
2229 (*mb_char2bytes)(c, buf);
2230 buf[cc] = NUL;
2231 ins_char_bytes(buf, cc);
2232 AppendCharToRedobuff(c);
2233 }
2234 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 ins_char(c);
2237 if (flags & INSCHAR_CTRLV)
2238 redo_literal(c);
2239 else
2240 AppendCharToRedobuff(c);
2241 }
2242 }
2243}
2244
2245/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002246 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002247 *
2248 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2249 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002250 */
2251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002252internal_format(
2253 int textwidth,
2254 int second_indent,
2255 int flags,
2256 int format_only,
2257 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002258{
2259 int cc;
2260 int save_char = NUL;
2261 int haveto_redraw = FALSE;
2262 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002263 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002264 int fo_white_par = has_format_option(FO_WHITE_PAR);
2265 int first_line = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002266 colnr_T leader_len;
2267 int no_leader = FALSE;
2268 int do_comments = (flags & INSCHAR_DO_COM);
Bram Moolenaar0026d472014-09-09 16:32:39 +02002269#ifdef FEAT_LINEBREAK
2270 int has_lbr = curwin->w_p_lbr;
2271
2272 /* make sure win_lbr_chartabsize() counts correctly */
2273 curwin->w_p_lbr = FALSE;
2274#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002275
2276 /*
2277 * When 'ai' is off we don't want a space under the cursor to be
2278 * deleted. Replace it with an 'x' temporarily.
2279 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002280 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002281 {
2282 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002283 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002284 {
2285 save_char = cc;
2286 pchar_cursor('x');
2287 }
2288 }
2289
2290 /*
2291 * Repeat breaking lines, until the current line is not too long.
2292 */
2293 while (!got_int)
2294 {
2295 int startcol; /* Cursor column at entry */
2296 int wantcol; /* column at textwidth border */
2297 int foundcol; /* column for start of spaces */
2298 int end_foundcol = 0; /* column for start of word */
2299 colnr_T len;
2300 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002301 int orig_col = 0;
2302 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002303 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002304 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002305 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002306
Bram Moolenaar97b98102009-11-17 16:41:01 +00002307 virtcol = get_nolist_virtcol()
2308 + char2cells(c != NUL ? c : gchar_cursor());
2309 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002310 break;
2311
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002312 if (no_leader)
2313 do_comments = FALSE;
2314 else if (!(flags & INSCHAR_FORMAT)
2315 && has_format_option(FO_WRAP_COMS))
2316 do_comments = TRUE;
2317
2318 /* Don't break until after the comment leader */
2319 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002320 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002321 else
2322 leader_len = 0;
2323
2324 /* If the line doesn't start with a comment leader, then don't
2325 * start one in a following broken line. Avoids that a %word
2326 * moved to the start of the next line causes all following lines
2327 * to start with %. */
2328 if (leader_len == 0)
2329 no_leader = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002330 if (!(flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002331 && leader_len == 0
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002332 && !has_format_option(FO_WRAP))
2333
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002334 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002335 if ((startcol = curwin->w_cursor.col) == 0)
2336 break;
2337
2338 /* find column of textwidth border */
2339 coladvance((colnr_T)textwidth);
2340 wantcol = curwin->w_cursor.col;
2341
Bram Moolenaar97b98102009-11-17 16:41:01 +00002342 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002343 foundcol = 0;
2344
2345 /*
2346 * Find position to break at.
2347 * Stop at first entered white when 'formatoptions' has 'v'
2348 */
2349 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002350 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002351 || curwin->w_cursor.lnum != Insstart.lnum
2352 || curwin->w_cursor.col >= Insstart.col)
2353 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002354 if (curwin->w_cursor.col == startcol && c != NUL)
2355 cc = c;
2356 else
2357 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002358 if (WHITECHAR(cc))
2359 {
2360 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002361 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002362
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002363 // find start of sequence of blanks
2364 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002365 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2366 {
2367 dec_cursor();
2368 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002369
2370 // Increment count of how many whitespace chars in this
2371 // group; we only need to know if it's more than one.
2372 if (wcc < 2)
2373 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002374 }
2375 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
2376 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002377
2378 // Don't break after a period when 'formatoptions' has 'p' and
2379 // there are less than two spaces.
2380 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2381 continue;
2382
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002383 /* Don't break until after the comment leader */
2384 if (curwin->w_cursor.col < leader_len)
2385 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002386 if (has_format_option(FO_ONE_LETTER))
2387 {
2388 /* do not break after one-letter words */
2389 if (curwin->w_cursor.col == 0)
2390 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002391 /* do not break "#a b" when 'tw' is 2 */
2392 if (curwin->w_cursor.col <= leader_len)
2393 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002394 col = curwin->w_cursor.col;
2395 dec_cursor();
2396 cc = gchar_cursor();
2397
2398 if (WHITECHAR(cc))
2399 continue; /* one-letter, continue */
2400 curwin->w_cursor.col = col;
2401 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002402
2403 inc_cursor();
2404
2405 end_foundcol = end_col + 1;
2406 foundcol = curwin->w_cursor.col;
2407 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002408 break;
2409 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002410 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002411 {
2412 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002413 if (curwin->w_cursor.col != startcol)
2414 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002415 /* Don't break until after the comment leader */
2416 if (curwin->w_cursor.col < leader_len)
2417 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002418 col = curwin->w_cursor.col;
2419 inc_cursor();
2420 /* Don't change end_foundcol if already set. */
2421 if (foundcol != curwin->w_cursor.col)
2422 {
2423 foundcol = curwin->w_cursor.col;
2424 end_foundcol = foundcol;
2425 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2426 break;
2427 }
2428 curwin->w_cursor.col = col;
2429 }
2430
2431 if (curwin->w_cursor.col == 0)
2432 break;
2433
2434 col = curwin->w_cursor.col;
2435
2436 dec_cursor();
2437 cc = gchar_cursor();
2438
2439 if (WHITECHAR(cc))
2440 continue; /* break with space */
Bram Moolenaar97b98102009-11-17 16:41:01 +00002441 /* Don't break until after the comment leader */
2442 if (curwin->w_cursor.col < leader_len)
2443 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002444
2445 curwin->w_cursor.col = col;
2446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002447 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002448 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002449 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2450 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002451 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002452 if (curwin->w_cursor.col == 0)
2453 break;
2454 dec_cursor();
2455 }
2456
2457 if (foundcol == 0) /* no spaces, cannot break line */
2458 {
2459 curwin->w_cursor.col = startcol;
2460 break;
2461 }
2462
2463 /* Going to break the line, remove any "$" now. */
2464 undisplay_dollar();
2465
2466 /*
2467 * Offset between cursor position and line break is used by replace
2468 * stack functions. VREPLACE does not use this, and backspaces
2469 * over the text instead.
2470 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002471 if (State & VREPLACE_FLAG)
2472 orig_col = startcol; /* Will start backspacing from here */
2473 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002474 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002475
2476 /*
2477 * adjust startcol for spaces that will be deleted and
2478 * characters that will remain on top line
2479 */
2480 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002481 while ((cc = gchar_cursor(), WHITECHAR(cc))
2482 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002483 inc_cursor();
2484 startcol -= curwin->w_cursor.col;
2485 if (startcol < 0)
2486 startcol = 0;
2487
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002488 if (State & VREPLACE_FLAG)
2489 {
2490 /*
2491 * In VREPLACE mode, we will backspace over the text to be
2492 * wrapped, so save a copy now to put on the next line.
2493 */
2494 saved_text = vim_strsave(ml_get_cursor());
2495 curwin->w_cursor.col = orig_col;
2496 if (saved_text == NULL)
2497 break; /* Can't do it, out of memory */
2498 saved_text[startcol] = NUL;
2499
2500 /* Backspace over characters that will move to the next line */
2501 if (!fo_white_par)
2502 backspace_until_column(foundcol);
2503 }
2504 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002505 {
2506 /* put cursor after pos. to break line */
2507 if (!fo_white_par)
2508 curwin->w_cursor.col = foundcol;
2509 }
2510
2511 /*
2512 * Split the line just before the margin.
2513 * Only insert/delete lines, but don't really redraw the window.
2514 */
2515 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2516 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002517 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002518 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002519 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2520 if (!(flags & INSCHAR_COM_LIST))
2521 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002522
2523 replace_offset = 0;
2524 if (first_line)
2525 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002526 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002527 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002528 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002529 * This section is for auto-wrap of numeric lists. When not
2530 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2531 * flag will be set and open_line() will handle it (as seen
2532 * above). The code here (and in get_number_indent()) will
2533 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002534 */
2535 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002536 second_indent =
2537 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002538 if (second_indent >= 0)
2539 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002540 if (State & VREPLACE_FLAG)
2541 change_indent(INDENT_SET, second_indent,
2542 FALSE, NUL, TRUE);
2543 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002544 if (leader_len > 0 && second_indent - leader_len > 0)
2545 {
2546 int i;
2547 int padding = second_indent - leader_len;
2548
2549 /* We started at the first_line of a numbered list
2550 * that has a comment. the open_line() function has
2551 * inserted the proper comment leader and positioned
2552 * the cursor at the end of the split line. Now we
2553 * add the additional whitespace needed after the
2554 * comment leader for the numbered list. */
2555 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002556 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002557 }
2558 else
2559 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002560 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002561 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002562 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002563 }
2564 first_line = FALSE;
2565 }
2566
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002567 if (State & VREPLACE_FLAG)
2568 {
2569 /*
2570 * In VREPLACE mode we have backspaced over the text to be
2571 * moved, now we re-insert it into the new line.
2572 */
2573 ins_bytes(saved_text);
2574 vim_free(saved_text);
2575 }
2576 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002577 {
2578 /*
2579 * Check if cursor is not past the NUL off the line, cindent
2580 * may have added or removed indent.
2581 */
2582 curwin->w_cursor.col += startcol;
2583 len = (colnr_T)STRLEN(ml_get_curline());
2584 if (curwin->w_cursor.col > len)
2585 curwin->w_cursor.col = len;
2586 }
2587
2588 haveto_redraw = TRUE;
2589#ifdef FEAT_CINDENT
2590 can_cindent = TRUE;
2591#endif
2592 /* moved the cursor, don't autoindent or cindent now */
2593 did_ai = FALSE;
2594#ifdef FEAT_SMARTINDENT
2595 did_si = FALSE;
2596 can_si = FALSE;
2597 can_si_back = FALSE;
2598#endif
2599 line_breakcheck();
2600 }
2601
2602 if (save_char != NUL) /* put back space after cursor */
2603 pchar_cursor(save_char);
2604
Bram Moolenaar0026d472014-09-09 16:32:39 +02002605#ifdef FEAT_LINEBREAK
2606 curwin->w_p_lbr = has_lbr;
2607#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002608 if (!format_only && haveto_redraw)
2609 {
2610 update_topline();
2611 redraw_curbuf_later(VALID);
2612 }
2613}
2614
2615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 * Called after inserting or deleting text: When 'formatoptions' includes the
2617 * 'a' flag format from the current line until the end of the paragraph.
2618 * Keep the cursor at the same position relative to the text.
2619 * The caller must have saved the cursor line for undo, following ones will be
2620 * saved here.
2621 */
2622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002623auto_format(
2624 int trailblank, /* when TRUE also format with trailing blank */
2625 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 pos_T pos;
2628 colnr_T len;
2629 char_u *old;
2630 char_u *new, *pnew;
2631 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002632 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634 if (!has_format_option(FO_AUTO))
2635 return;
2636
2637 pos = curwin->w_cursor;
2638 old = ml_get_curline();
2639
2640 /* may remove added space */
2641 check_auto_format(FALSE);
2642
2643 /* Don't format in Insert mode when the cursor is on a trailing blank, the
2644 * user might insert normal text next. Also skip formatting when "1" is
2645 * in 'formatoptions' and there is a single character before the cursor.
2646 * Otherwise the line would be broken and when typing another non-white
2647 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002648 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (*old != NUL && !trailblank && wasatend)
2650 {
2651 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002652 cc = gchar_cursor();
2653 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2654 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002656 cc = gchar_cursor();
2657 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 curwin->w_cursor = pos;
2660 return;
2661 }
2662 curwin->w_cursor = pos;
2663 }
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
2666 * comments. */
2667 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002668 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
2671 /*
2672 * May start formatting in a previous line, so that after "x" a word is
2673 * moved to the previous line if it fits there now. Only when this is not
2674 * the start of a paragraph.
2675 */
2676 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2677 {
2678 --curwin->w_cursor.lnum;
2679 if (u_save_cursor() == FAIL)
2680 return;
2681 }
2682
2683 /*
2684 * Do the formatting and restore the cursor position. "saved_cursor" will
2685 * be adjusted for the text formatting.
2686 */
2687 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002688 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 curwin->w_cursor = saved_cursor;
2690 saved_cursor.lnum = 0;
2691
2692 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2693 {
2694 /* "cannot happen" */
2695 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2696 coladvance((colnr_T)MAXCOL);
2697 }
2698 else
2699 check_cursor_col();
2700
2701 /* Insert mode: If the cursor is now after the end of the line while it
2702 * previously wasn't, the line was broken. Because of the rule above we
2703 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2704 * formatted. */
2705 if (!wasatend && has_format_option(FO_WHITE_PAR))
2706 {
2707 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002708 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (curwin->w_cursor.col == len)
2710 {
2711 pnew = vim_strnsave(new, len + 2);
2712 pnew[len] = ' ';
2713 pnew[len + 1] = NUL;
2714 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
2715 /* remove the space later */
2716 did_add_space = TRUE;
2717 }
2718 else
2719 /* may remove added space */
2720 check_auto_format(FALSE);
2721 }
2722
2723 check_cursor();
2724}
2725
2726/*
2727 * When an extra space was added to continue a paragraph for auto-formatting,
2728 * delete it now. The space must be under the cursor, just after the insert
2729 * position.
2730 */
2731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002732check_auto_format(
2733 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734{
2735 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002736 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 if (did_add_space)
2739 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002740 cc = gchar_cursor();
2741 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 /* Somehow the space was removed already. */
2743 did_add_space = FALSE;
2744 else
2745 {
2746 if (!end_insert)
2747 {
2748 inc_cursor();
2749 c = gchar_cursor();
2750 dec_cursor();
2751 }
2752 if (c != NUL)
2753 {
2754 /* The space is no longer at the end of the line, delete it. */
2755 del_char(FALSE);
2756 did_add_space = FALSE;
2757 }
2758 }
2759 }
2760}
2761
2762/*
2763 * Find out textwidth to be used for formatting:
2764 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02002765 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 * if invalid value, use 0.
2767 * Set default to window width (maximum 79) for "gq" operator.
2768 */
2769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002770comp_textwidth(
2771 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
2773 int textwidth;
2774
2775 textwidth = curbuf->b_p_tw;
2776 if (textwidth == 0 && curbuf->b_p_wm)
2777 {
2778 /* The width is the window width minus 'wrapmargin' minus all the
2779 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02002780 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781#ifdef FEAT_CMDWIN
2782 if (cmdwin_type != 0)
2783 textwidth -= 1;
2784#endif
2785#ifdef FEAT_FOLDING
2786 textwidth -= curwin->w_p_fdc;
2787#endif
2788#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002789 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 textwidth -= 1;
2791#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02002792 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 textwidth -= 8;
2794 }
2795 if (textwidth < 0)
2796 textwidth = 0;
2797 if (ff && textwidth == 0)
2798 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002799 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (textwidth > 79)
2801 textwidth = 79;
2802 }
2803 return textwidth;
2804}
2805
2806/*
2807 * Put a character in the redo buffer, for when just after a CTRL-V.
2808 */
2809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002810redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
2812 char_u buf[10];
2813
2814 /* Only digits need special treatment. Translate them into a string of
2815 * three digits. */
2816 if (VIM_ISDIGIT(c))
2817 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002818 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 AppendToRedobuff(buf);
2820 }
2821 else
2822 AppendCharToRedobuff(c);
2823}
2824
2825/*
2826 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002827 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002830start_arrow(
2831 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002833 start_arrow_common(end_insert_pos, TRUE);
2834}
2835
2836/*
2837 * Like start_arrow() but with end_change argument.
2838 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2839 */
2840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841start_arrow_with_change(
2842 pos_T *end_insert_pos, /* can be NULL */
2843 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002844{
2845 start_arrow_common(end_insert_pos, end_change);
2846 if (!end_change)
2847 {
2848 AppendCharToRedobuff(Ctrl_G);
2849 AppendCharToRedobuff('U');
2850 }
2851}
2852
2853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002854start_arrow_common(
2855 pos_T *end_insert_pos, /* can be NULL */
2856 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002857{
2858 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
2860 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002861 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 arrow_used = TRUE; /* this means we stopped the current insert */
2863 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002864#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002865 check_spell_redraw();
2866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867}
2868
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002869#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002870/*
2871 * If we skipped highlighting word at cursor, do it now.
2872 * It may be skipped again, thus reset spell_redraw_lnum first.
2873 */
2874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002875check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002876{
2877 if (spell_redraw_lnum != 0)
2878 {
2879 linenr_T lnum = spell_redraw_lnum;
2880
2881 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002882 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002883 }
2884}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002885
Bram Moolenaar217ad922005-03-20 22:37:15 +00002886#endif
2887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888/*
2889 * stop_arrow() is called before a change is made in insert mode.
2890 * If an arrow key has been used, start a new insertion.
2891 * Returns FAIL if undo is impossible, shouldn't insert then.
2892 */
2893 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002894stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896 if (arrow_used)
2897 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002898 Insstart = curwin->w_cursor; /* new insertion starts here */
2899 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
2900 /* Don't update the original insert position when moved to the
2901 * right, except when nothing was inserted yet. */
2902 update_Insstart_orig = FALSE;
2903 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2904
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 if (u_save_cursor() == OK)
2906 {
2907 arrow_used = FALSE;
2908 ins_need_undo = FALSE;
2909 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002910
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 if (State & VREPLACE_FLAG)
2913 {
2914 orig_line_count = curbuf->b_ml.ml_line_count;
2915 vr_lines_changed = 1;
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 ResetRedobuff();
2918 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002919 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
2921 else if (ins_need_undo)
2922 {
2923 if (u_save_cursor() == OK)
2924 ins_need_undo = FALSE;
2925 }
2926
2927#ifdef FEAT_FOLDING
2928 /* Always open fold at the cursor line when inserting something. */
2929 foldOpenCursor();
2930#endif
2931
2932 return (arrow_used || ins_need_undo ? FAIL : OK);
2933}
2934
2935/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002936 * Do a few things to stop inserting.
2937 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2938 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 */
2940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002941stop_insert(
2942 pos_T *end_insert_pos,
2943 int esc, /* called by ins_esc() */
2944 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002946 int cc;
2947 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 stop_redo_ins();
2950 replace_flush(); /* abandon replace stack */
2951
2952 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002953 * Save the inserted text for later redo with ^@ and CTRL-A.
2954 * Don't do it when "restart_edit" was set and nothing was inserted,
2955 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002957 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002958 if (did_restart_edit == 0 || (ptr != NULL
2959 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002960 {
2961 vim_free(last_insert);
2962 last_insert = ptr;
2963 last_insert_skip = new_insert_skip;
2964 }
2965 else
2966 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002968 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 {
2970 /* Auto-format now. It may seem strange to do this when stopping an
2971 * insertion (or moving the cursor), but it's required when appending
2972 * a line and having it end in a space. But only do it when something
2973 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002974 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002976 pos_T tpos = curwin->w_cursor;
2977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 /* When the cursor is at the end of the line after a space the
2979 * formatting will move it to the following word. Avoid that by
2980 * moving the cursor onto the space. */
2981 cc = 'x';
2982 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
2983 {
2984 dec_cursor();
2985 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002986 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002987 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 }
2989
2990 auto_format(TRUE, FALSE);
2991
Bram Moolenaar1c465442017-03-12 20:10:05 +01002992 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002993 {
2994 if (gchar_cursor() != NUL)
2995 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002996 // If the cursor is still at the same character, also keep
2997 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002998 if (gchar_cursor() == NUL
2999 && curwin->w_cursor.lnum == tpos.lnum
3000 && curwin->w_cursor.col == tpos.col)
3001 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004
3005 /* If a space was inserted for auto-formatting, remove it now. */
3006 check_auto_format(TRUE);
3007
3008 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003009 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00003010 * Do this when ESC was used or moving the cursor up/down.
3011 * Check for the old position still being valid, just in case the text
3012 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01003013 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003014 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3015 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003017 pos_T tpos = curwin->w_cursor;
3018
3019 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003020 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003021 for (;;)
3022 {
3023 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3024 --curwin->w_cursor.col;
3025 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003026 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003027 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003028 if (del_char(TRUE) == FAIL)
3029 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00003030 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003031 if (curwin->w_cursor.lnum != tpos.lnum)
3032 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003033 else
3034 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003035 /* reset tpos, could have been invalidated in the loop above */
3036 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003037 tpos.col++;
3038 if (cc != NUL && gchar_pos(&tpos) == NUL)
3039 ++curwin->w_cursor.col; /* put cursor back on the NUL */
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 /* <C-S-Right> may have started Visual mode, adjust the position for
3043 * deleted characters. */
3044 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3045 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003046 int len = (int)STRLEN(ml_get_curline());
3047
3048 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003050 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
3053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 }
3055 }
3056 did_ai = FALSE;
3057#ifdef FEAT_SMARTINDENT
3058 did_si = FALSE;
3059 can_si = FALSE;
3060 can_si_back = FALSE;
3061#endif
3062
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003063 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3064 * now in a different buffer. */
3065 if (end_insert_pos != NULL)
3066 {
3067 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003068 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003069 curbuf->b_op_end = *end_insert_pos;
3070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071}
3072
3073/*
3074 * Set the last inserted text to a single character.
3075 * Used for the replace command.
3076 */
3077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 char_u *s;
3081
3082 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 if (last_insert != NULL)
3085 {
3086 s = last_insert;
3087 /* Use the CTRL-V only when entering a special char */
3088 if (c < ' ' || c == DEL)
3089 *s++ = Ctrl_V;
3090 s = add_char2buf(c, s);
3091 *s++ = ESC;
3092 *s++ = NUL;
3093 last_insert_skip = 0;
3094 }
3095}
3096
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003097#if defined(EXITFREE) || defined(PROTO)
3098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003099free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003100{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003101 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003102}
3103#endif
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105/*
3106 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3107 * and CSI. Handle multi-byte characters.
3108 * Returns a pointer to after the added bytes.
3109 */
3110 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003111add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003113 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 int i;
3115 int len;
3116
3117 len = (*mb_char2bytes)(c, temp);
3118 for (i = 0; i < len; ++i)
3119 {
3120 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
3122 if (c == K_SPECIAL)
3123 {
3124 *s++ = K_SPECIAL;
3125 *s++ = KS_SPECIAL;
3126 *s++ = KE_FILLER;
3127 }
3128#ifdef FEAT_GUI
3129 else if (c == CSI)
3130 {
3131 *s++ = CSI;
3132 *s++ = KS_EXTRA;
3133 *s++ = (int)KE_CSI;
3134 }
3135#endif
3136 else
3137 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 return s;
3140}
3141
3142/*
3143 * move cursor to start of line
3144 * if flags & BL_WHITE move to first non-white
3145 * if flags & BL_SOL move to first non-white if startofline is set,
3146 * otherwise keep "curswant" column
3147 * if flags & BL_FIX don't leave the cursor on a NUL.
3148 */
3149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003150beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 if ((flags & BL_SOL) && !p_sol)
3153 coladvance(curwin->w_curswant);
3154 else
3155 {
3156 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 if (flags & (BL_WHITE | BL_SOL))
3160 {
3161 char_u *ptr;
3162
Bram Moolenaar1c465442017-03-12 20:10:05 +01003163 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3165 ++curwin->w_cursor.col;
3166 }
3167 curwin->w_set_curswant = TRUE;
3168 }
3169}
3170
3171/*
3172 * oneright oneleft cursor_down cursor_up
3173 *
3174 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003175 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 * Return OK when successful, FAIL when we hit a line of file boundary.
3177 */
3178
3179 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003180oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 if (virtual_active())
3186 {
3187 pos_T prevpos = curwin->w_cursor;
3188
3189 /* Adjust for multi-wide char (excluding TAB) */
3190 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003191 coladvance(getviscol() + ((*ptr != TAB
3192 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 ? ptr2cells(ptr) : 1));
3194 curwin->w_set_curswant = TRUE;
3195 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
3196 return (prevpos.col != curwin->w_cursor.col
3197 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003201 if (*ptr == NUL)
3202 return FAIL; /* already at the very end */
3203
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003204 if (has_mbyte)
3205 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003207 l = 1;
3208
3209 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3210 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003211 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003212 return FAIL;
3213 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215 curwin->w_set_curswant = TRUE;
3216 return OK;
3217}
3218
3219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (virtual_active())
3223 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003224#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 int v = getviscol();
3228
3229 if (v == 0)
3230 return FAIL;
3231
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003232#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 /* We might get stuck on 'showbreak', skip over it. */
3234 width = 1;
3235 for (;;)
3236 {
3237 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02003238 /* getviscol() is slow, skip it when 'showbreak' is empty,
3239 * 'breakindent' is not set and there are no multi-byte
3240 * characters */
Bram Moolenaaree857022019-11-09 23:26:40 +01003241 if ((*get_showbreak_value(curwin) == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003242 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 break;
3244 ++width;
3245 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003246#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250 if (curwin->w_cursor.coladd == 1)
3251 {
3252 char_u *ptr;
3253
3254 /* Adjust for multi-wide char (not a TAB) */
3255 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003256 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3257 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 curwin->w_cursor.coladd = 0;
3259 }
3260
3261 curwin->w_set_curswant = TRUE;
3262 return OK;
3263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
3265 if (curwin->w_cursor.col == 0)
3266 return FAIL;
3267
3268 curwin->w_set_curswant = TRUE;
3269 --curwin->w_cursor.col;
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 /* if the character on the left of the current cursor is a multi-byte
3272 * character, move to its first byte */
3273 if (has_mbyte)
3274 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 return OK;
3276}
3277
3278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003279cursor_up(
3280 long n,
3281 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 linenr_T lnum;
3284
3285 if (n > 0)
3286 {
3287 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003288 /* This fails if the cursor is already in the first line or the count
3289 * is larger than the line number and '-' is in 'cpoptions' */
3290 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return FAIL;
3292 if (n >= lnum)
3293 lnum = 1;
3294 else
3295#ifdef FEAT_FOLDING
3296 if (hasAnyFolding(curwin))
3297 {
3298 /*
3299 * Count each sequence of folded lines as one logical line.
3300 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003301 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 (void)hasFolding(lnum, &lnum, NULL);
3303
3304 while (n--)
3305 {
3306 /* move up one line */
3307 --lnum;
3308 if (lnum <= 1)
3309 break;
3310 /* If we entered a fold, move to the beginning, unless in
3311 * Insert mode or when 'foldopen' contains "all": it will open
3312 * in a moment. */
3313 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3314 (void)hasFolding(lnum, &lnum, NULL);
3315 }
3316 if (lnum < 1)
3317 lnum = 1;
3318 }
3319 else
3320#endif
3321 lnum -= n;
3322 curwin->w_cursor.lnum = lnum;
3323 }
3324
3325 /* try to advance to the column we want to be at */
3326 coladvance(curwin->w_curswant);
3327
3328 if (upd_topline)
3329 update_topline(); /* make sure curwin->w_topline is valid */
3330
3331 return OK;
3332}
3333
3334/*
3335 * Cursor down a number of logical lines.
3336 */
3337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003338cursor_down(
3339 long n,
3340 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 linenr_T lnum;
3343
3344 if (n > 0)
3345 {
3346 lnum = curwin->w_cursor.lnum;
3347#ifdef FEAT_FOLDING
3348 /* Move to last line of fold, will fail if it's the end-of-file. */
3349 (void)hasFolding(lnum, NULL, &lnum);
3350#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003351 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003352 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003353 if (lnum >= curbuf->b_ml.ml_line_count
3354 || (lnum + n > curbuf->b_ml.ml_line_count
3355 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 return FAIL;
3357 if (lnum + n >= curbuf->b_ml.ml_line_count)
3358 lnum = curbuf->b_ml.ml_line_count;
3359 else
3360#ifdef FEAT_FOLDING
3361 if (hasAnyFolding(curwin))
3362 {
3363 linenr_T last;
3364
3365 /* count each sequence of folded lines as one logical line */
3366 while (n--)
3367 {
3368 if (hasFolding(lnum, NULL, &last))
3369 lnum = last + 1;
3370 else
3371 ++lnum;
3372 if (lnum >= curbuf->b_ml.ml_line_count)
3373 break;
3374 }
3375 if (lnum > curbuf->b_ml.ml_line_count)
3376 lnum = curbuf->b_ml.ml_line_count;
3377 }
3378 else
3379#endif
3380 lnum += n;
3381 curwin->w_cursor.lnum = lnum;
3382 }
3383
3384 /* try to advance to the column we want to be at */
3385 coladvance(curwin->w_curswant);
3386
3387 if (upd_topline)
3388 update_topline(); /* make sure curwin->w_topline is valid */
3389
3390 return OK;
3391}
3392
3393/*
3394 * Stuff the last inserted text in the read buffer.
3395 * Last_insert actually is a copy of the redo buffer, so we
3396 * first have to remove the command.
3397 */
3398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003399stuff_inserted(
3400 int c, /* Command character to be inserted */
3401 long count, /* Repeat this many times */
3402 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
3404 char_u *esc_ptr;
3405 char_u *ptr;
3406 char_u *last_ptr;
3407 char_u last = NUL;
3408
3409 ptr = get_last_insert();
3410 if (ptr == NULL)
3411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003412 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 return FAIL;
3414 }
3415
3416 /* may want to stuff the command character, to start Insert mode */
3417 if (c != NUL)
3418 stuffcharReadbuff(c);
3419 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
3420 *esc_ptr = NUL; /* remove the ESC */
3421
3422 /* when the last char is either "0" or "^" it will be quoted if no ESC
3423 * comes after it OR if it will inserted more than once and "ptr"
3424 * starts with ^D. -- Acevedo
3425 */
3426 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3427 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3428 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3429 {
3430 last = *last_ptr;
3431 *last_ptr = NUL;
3432 }
3433
3434 do
3435 {
3436 stuffReadbuff(ptr);
3437 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
3438 if (last)
3439 stuffReadbuff((char_u *)(last == '0'
3440 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3441 : IF_EB("\026^", CTRL_V_STR "^")));
3442 }
3443 while (--count > 0);
3444
3445 if (last)
3446 *last_ptr = last;
3447
3448 if (esc_ptr != NULL)
3449 *esc_ptr = ESC; /* put the ESC back */
3450
3451 /* may want to stuff a trailing ESC, to get out of Insert mode */
3452 if (!no_esc)
3453 stuffcharReadbuff(ESC);
3454
3455 return OK;
3456}
3457
3458 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003459get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
3461 if (last_insert == NULL)
3462 return NULL;
3463 return last_insert + last_insert_skip;
3464}
3465
3466/*
3467 * Get last inserted string, and remove trailing <Esc>.
3468 * Returns pointer to allocated memory (must be freed) or NULL.
3469 */
3470 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003471get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 char_u *s;
3474 int len;
3475
3476 if (last_insert == NULL)
3477 return NULL;
3478 s = vim_strsave(last_insert + last_insert_skip);
3479 if (s != NULL)
3480 {
3481 len = (int)STRLEN(s);
3482 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
3483 s[len - 1] = NUL;
3484 }
3485 return s;
3486}
3487
3488/*
3489 * Check the word in front of the cursor for an abbreviation.
3490 * Called when the non-id character "c" has been entered.
3491 * When an abbreviation is recognized it is removed from the text and
3492 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3493 */
3494 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003495echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 /* Don't check for abbreviation in paste mode, when disabled and just
3498 * after moving around with cursor keys. */
3499 if (p_paste || no_abbr || arrow_used)
3500 return FALSE;
3501
3502 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3503 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3504}
3505
3506/*
3507 * replace-stack functions
3508 *
3509 * When replacing characters, the replaced characters are remembered for each
3510 * new character. This is used to re-insert the old text when backspacing.
3511 *
3512 * There is a NUL headed list of characters for each character that is
3513 * currently in the file after the insertion point. When BS is used, one NUL
3514 * headed list is put back for the deleted character.
3515 *
3516 * For a newline, there are two NUL headed lists. One contains the characters
3517 * that the NL replaced. The extra one stores the characters after the cursor
3518 * that were deleted (always white space).
3519 *
3520 * Replace_offset is normally 0, in which case replace_push will add a new
3521 * character at the end of the stack. If replace_offset is not 0, that many
3522 * characters will be left on the stack above the newly inserted character.
3523 */
3524
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003525static char_u *replace_stack = NULL;
3526static long replace_stack_nr = 0; /* next entry in replace stack */
3527static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003530replace_push(
3531 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
3533 char_u *p;
3534
3535 if (replace_stack_nr < replace_offset) /* nothing to do */
3536 return;
3537 if (replace_stack_len <= replace_stack_nr)
3538 {
3539 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003540 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (p == NULL) /* out of memory */
3542 {
3543 replace_stack_len -= 50;
3544 return;
3545 }
3546 if (replace_stack != NULL)
3547 {
3548 mch_memmove(p, replace_stack,
3549 (size_t)(replace_stack_nr * sizeof(char_u)));
3550 vim_free(replace_stack);
3551 }
3552 replace_stack = p;
3553 }
3554 p = replace_stack + replace_stack_nr - replace_offset;
3555 if (replace_offset)
3556 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3557 *p = c;
3558 ++replace_stack_nr;
3559}
3560
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003561/*
3562 * Push a character onto the replace stack. Handles a multi-byte character in
3563 * reverse byte order, so that the first byte is popped off first.
3564 * Return the number of bytes done (includes composing characters).
3565 */
3566 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003567replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003568{
3569 int l = (*mb_ptr2len)(p);
3570 int j;
3571
3572 for (j = l - 1; j >= 0; --j)
3573 replace_push(p[j]);
3574 return l;
3575}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577/*
3578 * Pop one item from the replace stack.
3579 * return -1 if stack empty
3580 * return replaced character or NUL otherwise
3581 */
3582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
3585 if (replace_stack_nr == 0)
3586 return -1;
3587 return (int)replace_stack[--replace_stack_nr];
3588}
3589
3590/*
3591 * Join the top two items on the replace stack. This removes to "off"'th NUL
3592 * encountered.
3593 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003595replace_join(
3596 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597{
3598 int i;
3599
3600 for (i = replace_stack_nr; --i >= 0; )
3601 if (replace_stack[i] == NUL && off-- <= 0)
3602 {
3603 --replace_stack_nr;
3604 mch_memmove(replace_stack + i, replace_stack + i + 1,
3605 (size_t)(replace_stack_nr - i));
3606 return;
3607 }
3608}
3609
3610/*
3611 * Pop bytes from the replace stack until a NUL is found, and insert them
3612 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3613 */
3614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003615replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
3617 int cc;
3618 int oldState = State;
3619
3620 State = NORMAL; /* don't want REPLACE here */
3621 while ((cc = replace_pop()) > 0)
3622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 dec_cursor();
3625 }
3626 State = oldState;
3627}
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629/*
3630 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3631 * indicates a multi-byte char, pop the other bytes too.
3632 */
3633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003634mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635{
3636 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003637 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 int i;
3639 int c;
3640
3641 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3642 {
3643 buf[0] = cc;
3644 for (i = 1; i < n; ++i)
3645 buf[i] = replace_pop();
3646 ins_bytes_len(buf, n);
3647 }
3648 else
3649 ins_char(cc);
3650
3651 if (enc_utf8)
3652 /* Handle composing chars. */
3653 for (;;)
3654 {
3655 c = replace_pop();
3656 if (c == -1) /* stack empty */
3657 break;
3658 if ((n = MB_BYTE2LEN(c)) == 1)
3659 {
3660 /* Not a multi-byte char, put it back. */
3661 replace_push(c);
3662 break;
3663 }
3664 else
3665 {
3666 buf[0] = c;
3667 for (i = 1; i < n; ++i)
3668 buf[i] = replace_pop();
3669 if (utf_iscomposing(utf_ptr2char(buf)))
3670 ins_bytes_len(buf, n);
3671 else
3672 {
3673 /* Not a composing char, put it back. */
3674 for (i = n - 1; i >= 0; --i)
3675 replace_push(buf[i]);
3676 break;
3677 }
3678 }
3679 }
3680}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
3682/*
3683 * make the replace stack empty
3684 * (called when exiting replace mode)
3685 */
3686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003687replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003689 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 replace_stack_len = 0;
3691 replace_stack_nr = 0;
3692}
3693
3694/*
3695 * Handle doing a BS for one character.
3696 * cc < 0: replace stack empty, just move cursor
3697 * cc == 0: character was inserted, delete it
3698 * cc > 0: character was replaced, put cc (first byte of original char) back
3699 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003700 * When "limit_col" is >= 0, don't delete before this column. Matters when
3701 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 */
3703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003704replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 int orig_len = 0;
3708 int ins_len;
3709 int orig_vcols = 0;
3710 colnr_T start_vcol;
3711 char_u *p;
3712 int i;
3713 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
3715 cc = replace_pop();
3716 if (cc > 0)
3717 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01003718#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003719 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003720
3721 if (curbuf->b_has_textprop)
3722 {
3723 // Do not adjust text properties for individual delete and insert
3724 // operations, do it afterwards on the resulting text.
3725 len_before = STRLEN(ml_get_curline());
3726 ++text_prop_frozen;
3727 }
3728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (State & VREPLACE_FLAG)
3730 {
3731 /* Get the number of screen cells used by the character we are
3732 * going to delete. */
3733 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3734 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (has_mbyte)
3737 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003738 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003740 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 replace_push(cc);
3742 }
3743 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
3745 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003747 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749 replace_pop_ins();
3750
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (State & VREPLACE_FLAG)
3752 {
3753 /* Get the number of screen cells used by the inserted characters */
3754 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003755 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 vcol = start_vcol;
3757 for (i = 0; i < ins_len; ++i)
3758 {
3759 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003760 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 vcol -= start_vcol;
3763
3764 /* Delete spaces that were inserted after the cursor to keep the
3765 * text aligned. */
3766 curwin->w_cursor.col += ins_len;
3767 while (vcol > orig_vcols && gchar_cursor() == ' ')
3768 {
3769 del_char(FALSE);
3770 ++orig_vcols;
3771 }
3772 curwin->w_cursor.col -= ins_len;
3773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
Bram Moolenaar196d1572019-01-02 23:47:18 +01003775 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003777
3778#ifdef FEAT_TEXT_PROP
3779 if (curbuf->b_has_textprop)
3780 {
3781 size_t len_now = STRLEN(ml_get_curline());
3782
3783 --text_prop_frozen;
3784 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003785 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003786 }
3787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003790 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791}
3792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3794/*
3795 * Map Hebrew keyboard when in hkmap mode.
3796 */
3797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003798hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
3800 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
3801 {
3802 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3803 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3804 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3805 static char_u map[26] =
3806 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3807 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3808 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3809 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3810 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3811 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3812 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3813 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3814 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3815
3816 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3817 return (int)(map[CharOrd(c)] - 1 + p_aleph);
3818 /* '-1'='sofit' */
3819 else if (c == 'x')
3820 return 'X';
3821 else if (c == 'q')
3822 return '\''; /* {geresh}={'} */
3823 else if (c == 246)
3824 return ' '; /* \"o --> ' ' for a german keyboard */
3825 else if (c == 228)
3826 return ' '; /* \"a --> ' ' -- / -- */
3827 else if (c == 252)
3828 return ' '; /* \"u --> ' ' -- / -- */
3829#ifdef EBCDIC
3830 else if (islower(c))
3831#else
3832 /* NOTE: islower() does not do the right thing for us on Linux so we
3833 * do this the same was as 5.7 and previous, so it works correctly on
3834 * all systems. Specifically, the e.g. Delete and Arrow keys are
3835 * munged and won't work if e.g. searching for Hebrew text.
3836 */
3837 else if (c >= 'a' && c <= 'z')
3838#endif
3839 return (int)(map[CharOrdLow(c)] + p_aleph);
3840 else
3841 return c;
3842 }
3843 else
3844 {
3845 switch (c)
3846 {
3847 case '`': return ';';
3848 case '/': return '.';
3849 case '\'': return ',';
3850 case 'q': return '/';
3851 case 'w': return '\'';
3852
3853 /* Hebrew letters - set offset from 'a' */
3854 case ',': c = '{'; break;
3855 case '.': c = 'v'; break;
3856 case ';': c = 't'; break;
3857 default: {
3858 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3859
3860#ifdef EBCDIC
3861 /* see note about islower() above */
3862 if (!islower(c))
3863#else
3864 if (c < 'a' || c > 'z')
3865#endif
3866 return c;
3867 c = str[CharOrdLow(c)];
3868 break;
3869 }
3870 }
3871
3872 return (int)(CharOrdLow(c) + p_aleph);
3873 }
3874}
3875#endif
3876
3877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003878ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879{
3880 int need_redraw = FALSE;
3881 int regname;
3882 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003883 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
3885 /*
3886 * If we are going to wait for a character, show a '"'.
3887 */
3888 pc_status = PC_STATUS_UNSET;
3889 if (redrawing() && !char_avail())
3890 {
3891 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00003892 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
3894 edit_putchar('"', TRUE);
3895#ifdef FEAT_CMDL_INFO
3896 add_to_showcmd_c(Ctrl_R);
3897#endif
3898 }
3899
3900#ifdef USE_ON_FLY_SCROLL
3901 dont_scroll = TRUE; /* disallow scrolling here */
3902#endif
3903
3904 /*
3905 * Don't map the register name. This also prevents the mode message to be
3906 * deleted when ESC is hit.
3907 */
3908 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003909 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3912 {
3913 /* Get a third key for literal register insertion */
3914 literally = regname;
3915#ifdef FEAT_CMDL_INFO
3916 add_to_showcmd_c(literally);
3917#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003918 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921 --no_mapping;
3922
3923#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02003924 /* Don't call u_sync() while typing the expression or giving an error
3925 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 ++no_u_sync;
3927 if (regname == '=')
3928 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003929 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003930# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003932# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003933 /* Sync undo when evaluating the expression calls setline() or
3934 * append(), so that it can be undone separately. */
3935 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003936
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003938
3939 // Cursor may be moved back a column.
3940 curwin->w_cursor = curpos;
3941 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003942# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003943 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (im_on)
3945 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003946# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00003948 if (regname == NUL || !valid_yank_reg(regname, FALSE))
3949 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003950 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00003952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 else
3954 {
3955#endif
3956 if (literally == Ctrl_O || literally == Ctrl_P)
3957 {
3958 /* Append the command to the redo buffer. */
3959 AppendCharToRedobuff(Ctrl_R);
3960 AppendCharToRedobuff(literally);
3961 AppendCharToRedobuff(regname);
3962
3963 do_put(regname, BACKWARD, 1L,
3964 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
3965 }
3966 else if (insert_reg(regname, literally) == FAIL)
3967 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003968 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 need_redraw = TRUE; /* remove the '"' */
3970 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003971 else if (stop_insert_mode)
3972 /* When the '=' register was used and a function was invoked that
3973 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
3974 * insert anything, need to remove the '"' */
3975 need_redraw = TRUE;
3976
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977#ifdef FEAT_EVAL
3978 }
3979 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003980 if (u_sync_once == 1)
3981 ins_need_undo = TRUE;
3982 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983#endif
3984#ifdef FEAT_CMDL_INFO
3985 clear_showcmd();
3986#endif
3987
3988 /* If the inserted register is empty, we need to remove the '"' */
3989 if (need_redraw || stuff_empty())
3990 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003991
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003992 /* Disallow starting Visual mode here, would get a weird mode. */
3993 if (!vis_active && VIsual_active)
3994 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995}
3996
3997/*
3998 * CTRL-G commands in Insert mode.
3999 */
4000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 int c;
4004
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004005 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
4008 /*
4009 * Don't map the second key. This also prevents the mode message to be
4010 * deleted when ESC is hit.
4011 */
4012 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004013 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 --no_mapping;
4015 switch (c)
4016 {
4017 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
4018 case K_UP:
4019 case Ctrl_K:
4020 case 'k': ins_up(TRUE);
4021 break;
4022
4023 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
4024 case K_DOWN:
4025 case Ctrl_J:
4026 case 'j': ins_down(TRUE);
4027 break;
4028
4029 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004030 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004032
4033 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004034 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004035 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004036 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 break;
4038
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004039 /* CTRL-G U: do not break undo with the next char */
4040 case 'U':
4041 /* Allow one left/right cursor movement with the next char,
4042 * without breaking undo. */
4043 dont_sync_undo = MAYBE;
4044 break;
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02004047 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049}
4050
4051/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004052 * CTRL-^ in Insert mode.
4053 */
4054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004055ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004056{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004057 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 {
4059 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
4060 if (State & LANGMAP)
4061 {
4062 curbuf->b_p_iminsert = B_IMODE_NONE;
4063 State &= ~LANGMAP;
4064 }
4065 else
4066 {
4067 curbuf->b_p_iminsert = B_IMODE_LMAP;
4068 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004069#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070 im_set_active(FALSE);
4071#endif
4072 }
4073 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004074#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004075 else
4076 {
4077 /* There are no ":lmap" mappings, toggle IM */
4078 if (im_get_status())
4079 {
4080 curbuf->b_p_iminsert = B_IMODE_NONE;
4081 im_set_active(FALSE);
4082 }
4083 else
4084 {
4085 curbuf->b_p_iminsert = B_IMODE_IM;
4086 State &= ~LANGMAP;
4087 im_set_active(TRUE);
4088 }
4089 }
4090#endif
4091 set_iminsert_global();
4092 showmode();
4093#ifdef FEAT_GUI
4094 /* may show different cursor shape or color */
4095 if (gui.in_use)
4096 gui_update_cursor(TRUE, FALSE);
4097#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004098#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099 /* Show/unshow value of 'keymap' in status lines. */
4100 status_redraw_curbuf();
4101#endif
4102}
4103
4104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 * Handle ESC in insert mode.
4106 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4107 * insert.
4108 */
4109 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004110ins_esc(
4111 long *count,
4112 int cmdchar,
4113 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
4115 int temp;
4116 static int disabled_redraw = FALSE;
4117
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004118#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004119 check_spell_redraw();
4120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
4122 temp = curwin->w_cursor.col;
4123 if (disabled_redraw)
4124 {
4125 --RedrawingDisabled;
4126 disabled_redraw = FALSE;
4127 }
4128 if (!arrow_used)
4129 {
4130 /*
4131 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004132 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4133 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 */
4135 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004136 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 /*
4139 * Repeating insert may take a long time. Check for
4140 * interrupt now and then.
4141 */
4142 if (*count > 0)
4143 {
4144 line_breakcheck();
4145 if (got_int)
4146 *count = 0;
4147 }
4148
4149 if (--*count > 0) /* repeat what was typed */
4150 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004151 /* Vi repeats the insert without replacing characters. */
4152 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4153 State &= ~REPLACE_FLAG;
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 (void)start_redo_ins();
4156 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02004157 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 ++RedrawingDisabled;
4159 disabled_redraw = TRUE;
4160 return FALSE; /* repeat the insert */
4161 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004162 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 undisplay_dollar();
4164 }
4165
4166 /* When an autoindent was removed, curswant stays after the
4167 * indent */
4168 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4169 curwin->w_set_curswant = TRUE;
4170
4171 /* Remember the last Insert position in the '^ mark. */
4172 if (!cmdmod.keepjumps)
4173 curbuf->b_last_insert = curwin->w_cursor;
4174
4175 /*
4176 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004177 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004179 if (!nomove
4180 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004181 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004182 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004183 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#ifdef FEAT_RIGHTLEFT
4185 && !revins_on
4186#endif
4187 )
4188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4190 {
4191 oneleft();
4192 if (restart_edit != NUL)
4193 ++curwin->w_cursor.coladd;
4194 }
4195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 /* Correct cursor for multi-byte character. */
4199 if (has_mbyte)
4200 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202 }
4203
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004204#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 /* Disable IM to allow typing English directly for Normal mode commands.
4206 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4207 * well). */
4208 if (!(State & LANGMAP))
4209 im_save_status(&curbuf->b_p_iminsert);
4210 im_set_active(FALSE);
4211#endif
4212
4213 State = NORMAL;
4214 /* need to position cursor again (e.g. when on a TAB ) */
4215 changed_cline_bef_curs();
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218#ifdef CURSOR_SHAPE
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004219 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004221 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004222 {
4223 // Re-enable bracketed paste mode.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004224 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004226 // Re-enable modifyOtherKeys.
4227 out_str(T_CTI);
4228 }
4229
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004230 // When recording or for CTRL-O, need to display the new mode.
4231 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004232 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004234 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004235 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004237 return TRUE; // exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238}
4239
4240#ifdef FEAT_RIGHTLEFT
4241/*
4242 * Toggle language: hkmap and revins_on.
4243 * Move to end of reverse inserted text.
4244 */
4245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
4248 if (revins_on && revins_chars && revins_scol >= 0)
4249 {
4250 while (gchar_cursor() != NUL && revins_chars--)
4251 ++curwin->w_cursor.col;
4252 }
4253 p_ri = !p_ri;
4254 revins_on = (State == INSERT && p_ri);
4255 if (revins_on)
4256 {
4257 revins_scol = curwin->w_cursor.col;
4258 revins_legal++;
4259 revins_chars = 0;
4260 undisplay_dollar();
4261 }
4262 else
4263 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004264 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 showmode();
4266}
4267#endif
4268
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269/*
4270 * If 'keymodel' contains "startsel", may start selection.
4271 * Returns TRUE when a CTRL-O and other keys stuffed.
4272 */
4273 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004274ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275{
4276 if (km_startsel)
4277 switch (c)
4278 {
4279 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 case K_PAGEUP:
4282 case K_KPAGEUP:
4283 case K_PAGEDOWN:
4284 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004285# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 case K_LEFT:
4287 case K_RIGHT:
4288 case K_UP:
4289 case K_DOWN:
4290 case K_END:
4291 case K_HOME:
4292# endif
4293 if (!(mod_mask & MOD_MASK_SHIFT))
4294 break;
4295 /* FALLTHROUGH */
4296 case K_S_LEFT:
4297 case K_S_RIGHT:
4298 case K_S_UP:
4299 case K_S_DOWN:
4300 case K_S_END:
4301 case K_S_HOME:
4302 /* Start selection right away, the cursor can move with
4303 * CTRL-O when beyond the end of the line. */
4304 start_selection();
4305
4306 /* Execute the key in (insert) Select mode. */
4307 stuffcharReadbuff(Ctrl_O);
4308 if (mod_mask)
4309 {
4310 char_u buf[4];
4311
4312 buf[0] = K_SPECIAL;
4313 buf[1] = KS_MODIFIER;
4314 buf[2] = mod_mask;
4315 buf[3] = NUL;
4316 stuffReadbuff(buf);
4317 }
4318 stuffcharReadbuff(c);
4319 return TRUE;
4320 }
4321 return FALSE;
4322}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004325 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004326 */
4327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004328ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004330#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004331 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004332 (char_u *)((State & REPLACE_FLAG) ? "i"
4333 : replaceState == VREPLACE ? "v"
4334 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004335#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004336 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 if (State & REPLACE_FLAG)
4338 State = INSERT | (State & LANGMAP);
4339 else
4340 State = replaceState | (State & LANGMAP);
4341 AppendCharToRedobuff(K_INS);
4342 showmode();
4343#ifdef CURSOR_SHAPE
4344 ui_cursor_shape(); /* may show different cursor shape */
4345#endif
4346}
4347
4348/*
4349 * Pressed CTRL-O in Insert mode.
4350 */
4351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004352ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 if (State & VREPLACE_FLAG)
4355 restart_edit = 'V';
4356 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004357 if (State & REPLACE_FLAG)
4358 restart_edit = 'R';
4359 else
4360 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 if (virtual_active())
4362 ins_at_eol = FALSE; /* cursor always keeps its column */
4363 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004364 ins_at_eol = (gchar_cursor() == NUL);
4365}
4366
4367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 * If the cursor is on an indent, ^T/^D insert/delete one
4369 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004370 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 * with vi. But vi only supports ^T and ^D after an
4372 * autoindent, we support it everywhere.
4373 */
4374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004375ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376{
4377 if (stop_arrow() == FAIL)
4378 return;
4379 AppendCharToRedobuff(c);
4380
4381 /*
4382 * 0^D and ^^D: remove all indent.
4383 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004384 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4385 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
4387 --curwin->w_cursor.col;
4388 (void)del_char(FALSE); /* delete the '^' or '0' */
4389 /* In Replace mode, restore the characters that '^' or '0' replaced. */
4390 if (State & REPLACE_FLAG)
4391 replace_pop_ins();
4392 if (lastc == '^')
4393 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004394 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
4396 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004397 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4400 did_ai = FALSE;
4401#ifdef FEAT_SMARTINDENT
4402 did_si = FALSE;
4403 can_si = FALSE;
4404 can_si_back = FALSE;
4405#endif
4406#ifdef FEAT_CINDENT
4407 can_cindent = FALSE; /* no cindenting after ^D or ^T */
4408#endif
4409}
4410
4411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004412ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
4414 int temp;
4415
4416 if (stop_arrow() == FAIL)
4417 return;
4418 if (gchar_cursor() == NUL) /* delete newline */
4419 {
4420 temp = curwin->w_cursor.col;
4421 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004422 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004423 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004427 /* Adjust orig_line_count in case more lines have been deleted than
4428 * have been added. That makes sure, that open_line() later
4429 * can access all buffer lines correctly */
4430 if (State & VREPLACE_FLAG &&
4431 orig_line_count > curbuf->b_ml.ml_line_count)
4432 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02004435 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
4436 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 did_ai = FALSE;
4438#ifdef FEAT_SMARTINDENT
4439 did_si = FALSE;
4440 can_si = FALSE;
4441 can_si_back = FALSE;
4442#endif
4443 AppendCharToRedobuff(K_DEL);
4444}
4445
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004446/*
4447 * Delete one character for ins_bs().
4448 */
4449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004450ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004451{
4452 dec_cursor();
4453 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4454 if (State & REPLACE_FLAG)
4455 {
4456 /* Don't delete characters before the insert point when in
4457 * Replace mode */
4458 if (curwin->w_cursor.lnum != Insstart.lnum
4459 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004460 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004461 }
4462 else
4463 (void)del_char(FALSE);
4464}
4465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466/*
4467 * Handle Backspace, delete-word and delete-line in Insert mode.
4468 * Return TRUE when backspace was actually used.
4469 */
4470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004471ins_bs(
4472 int c,
4473 int mode,
4474 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475{
4476 linenr_T lnum;
4477 int cc;
4478 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004479 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 colnr_T mincol;
4481 int did_backspace = FALSE;
4482 int in_indent;
4483 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004484 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 /*
4487 * can't delete anything in an empty file
4488 * can't backup past first character in buffer
4489 * can't backup past starting point unless 'backspace' > 1
4490 * can backup to a previous line if 'backspace' == 0
4491 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004492 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 || (
4494#ifdef FEAT_RIGHTLEFT
4495 !revins_on &&
4496#endif
4497 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4498 || (!can_bs(BS_START)
4499 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004500 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4501 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4503 && curwin->w_cursor.col <= ai_col)
4504 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4505 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004506 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 return FALSE;
4508 }
4509
4510 if (stop_arrow() == FAIL)
4511 return FALSE;
4512 in_indent = inindent(0);
4513#ifdef FEAT_CINDENT
4514 if (in_indent)
4515 can_cindent = FALSE;
4516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 end_comment_pending = NUL; /* After BS, don't auto-end comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#ifdef FEAT_RIGHTLEFT
4519 if (revins_on) /* put cursor after last inserted char */
4520 inc_cursor();
4521#endif
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 /* Virtualedit:
4524 * BACKSPACE_CHAR eats a virtual space
4525 * BACKSPACE_WORD eats all coladd
4526 * BACKSPACE_LINE eats all coladd and keeps going
4527 */
4528 if (curwin->w_cursor.coladd > 0)
4529 {
4530 if (mode == BACKSPACE_CHAR)
4531 {
4532 --curwin->w_cursor.coladd;
4533 return TRUE;
4534 }
4535 if (mode == BACKSPACE_WORD)
4536 {
4537 curwin->w_cursor.coladd = 0;
4538 return TRUE;
4539 }
4540 curwin->w_cursor.coladd = 0;
4541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
4543 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004544 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 */
4546 if (curwin->w_cursor.col == 0)
4547 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004548 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004549 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550#ifdef FEAT_RIGHTLEFT
4551 || revins_on
4552#endif
4553 )
4554 {
4555 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4556 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4557 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004558 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004559 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561 /*
4562 * In replace mode:
4563 * cc < 0: NL was inserted, delete it
4564 * cc >= 0: NL was replaced, put original characters back
4565 */
4566 cc = -1;
4567 if (State & REPLACE_FLAG)
4568 cc = replace_pop(); /* returns -1 if NL was inserted */
4569 /*
4570 * In replace mode, in the line we started replacing, we only move the
4571 * cursor.
4572 */
4573 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4574 {
4575 dec_cursor();
4576 }
4577 else
4578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (!(State & VREPLACE_FLAG)
4580 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582 temp = gchar_cursor(); /* remember current char */
4583 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004584
4585 /* When "aw" is in 'formatoptions' we must delete the space at
4586 * the end of the line, otherwise the line will be broken
4587 * again when auto-formatting. */
4588 if (has_format_option(FO_AUTO)
4589 && has_format_option(FO_WHITE_PAR))
4590 {
4591 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4592 TRUE);
4593 int len;
4594
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004595 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004596 if (len > 0 && ptr[len - 1] == ' ')
4597 ptr[len - 1] = NUL;
4598 }
4599
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004600 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (temp == NUL && gchar_cursor() != NUL)
4602 inc_cursor();
4603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 else
4605 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 /*
4608 * In REPLACE mode we have to put back the text that was replaced
4609 * by the NL. On the replace stack is first a NUL-terminated
4610 * sequence of characters that were deleted and then the
4611 * characters that NL replaced.
4612 */
4613 if (State & REPLACE_FLAG)
4614 {
4615 /*
4616 * Do the next ins_char() in NORMAL state, to
4617 * prevent ins_char() from replacing characters and
4618 * avoiding showmatch().
4619 */
4620 oldState = State;
4621 State = NORMAL;
4622 /*
4623 * restore characters (blanks) deleted after cursor
4624 */
4625 while (cc > 0)
4626 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004627 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004629 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 cc = replace_pop();
4631 }
4632 /* restore the characters that NL replaced */
4633 replace_pop_ins();
4634 State = oldState;
4635 }
4636 }
4637 did_ai = FALSE;
4638 }
4639 else
4640 {
4641 /*
4642 * Delete character(s) before the cursor.
4643 */
4644#ifdef FEAT_RIGHTLEFT
4645 if (revins_on) /* put cursor on last inserted char */
4646 dec_cursor();
4647#endif
4648 mincol = 0;
4649 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004650 if (mode == BACKSPACE_LINE
4651 && (curbuf->b_p_ai
4652#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004653 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004654#endif
4655 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#ifdef FEAT_RIGHTLEFT
4657 && !revins_on
4658#endif
4659 )
4660 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004661 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004663 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004665 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667
4668 /*
4669 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4670 */
4671 if ( mode == BACKSPACE_CHAR
4672 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004673 || ((get_sts_value() != 0
4674#ifdef FEAT_VARTABS
4675 || tabstop_count(curbuf->b_p_vsts_array)
4676#endif
4677 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004678 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 && (*(ml_get_cursor() - 1) == TAB
4680 || (*(ml_get_cursor() - 1) == ' '
4681 && (!*inserted_space_p
4682 || arrow_used))))))
4683 {
4684 int ts;
4685 colnr_T vcol;
4686 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004687 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
4689 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 /* Compute the virtual column where we want to be. Since
4691 * 'showbreak' may get in the way, need to get the last column of
4692 * the previous character. */
4693 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004694 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 dec_cursor();
4696 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4697 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004698#ifdef FEAT_VARTABS
4699 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004700 {
4701 ts = (int)get_sw_value(curbuf);
4702 want_vcol = (want_vcol / ts) * ts;
4703 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004704 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004705 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004706 curbuf->b_p_vsts_array);
4707#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004708 if (p_sta && in_indent)
4709 ts = (int)get_sw_value(curbuf);
4710 else
4711 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714
4715 /* delete characters until we are at or before want_vcol */
4716 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004717 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004718 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720 /* insert extra spaces until we are at want_vcol */
4721 while (vcol < want_vcol)
4722 {
4723 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004724 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4725 && curwin->w_cursor.col < Insstart_orig.col)
4726 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (State & VREPLACE_FLAG)
4729 ins_char(' ');
4730 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
4732 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004733 if ((State & REPLACE_FLAG))
4734 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4737 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004738
4739 /* If we are now back where we started delete one character. Can
4740 * happen when using 'sts' and 'linebreak'. */
4741 if (vcol >= start_vcol)
4742 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
4744
4745 /*
4746 * Delete upto starting point, start of line or previous word.
4747 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004748 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004750 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004752 if (has_mbyte)
4753 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004754 do
4755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004757 if (!revins_on) /* put cursor on char to be deleted */
4758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004760
4761 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004762 /* look multi-byte character class */
4763 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004765 prev_cclass = cclass;
4766 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004768
4769 /* start of word? */
4770 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4771 {
4772 mode = BACKSPACE_WORD_NOT_SPACE;
4773 temp = vim_iswordc(cc);
4774 }
4775 /* end of word? */
4776 else if (mode == BACKSPACE_WORD_NOT_SPACE
4777 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004778 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004781 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004783 inc_cursor();
4784#ifdef FEAT_RIGHTLEFT
4785 else if (State & REPLACE_FLAG)
4786 dec_cursor();
4787#endif
4788 break;
4789 }
4790 if (State & REPLACE_FLAG)
4791 replace_do_bs(-1);
4792 else
4793 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004794 if (enc_utf8 && p_deco)
4795 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004796 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004797 /*
4798 * If there are combining characters and 'delcombine' is set
4799 * move the cursor back. Don't back up before the base
4800 * character.
4801 */
4802 if (enc_utf8 && p_deco && cpc[0] != NUL)
4803 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004804#ifdef FEAT_RIGHTLEFT
4805 if (revins_chars)
4806 {
4807 revins_chars--;
4808 revins_legal++;
4809 }
4810 if (revins_on && gchar_cursor() == NUL)
4811 break;
4812#endif
4813 }
4814 /* Just a single backspace?: */
4815 if (mode == BACKSPACE_CHAR)
4816 break;
4817 } while (
4818#ifdef FEAT_RIGHTLEFT
4819 revins_on ||
4820#endif
4821 (curwin->w_cursor.col > mincol
4822 && (curwin->w_cursor.lnum != Insstart_orig.lnum
4823 || curwin->w_cursor.col != Insstart_orig.col)));
4824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 did_backspace = TRUE;
4826 }
4827#ifdef FEAT_SMARTINDENT
4828 did_si = FALSE;
4829 can_si = FALSE;
4830 can_si_back = FALSE;
4831#endif
4832 if (curwin->w_cursor.col <= 1)
4833 did_ai = FALSE;
4834 /*
4835 * It's a little strange to put backspaces into the redo
4836 * buffer, but it makes auto-indent a lot easier to deal
4837 * with.
4838 */
4839 AppendCharToRedobuff(c);
4840
4841 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004842 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004843 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004844 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
4846 /* vi behaviour: the cursor moves backward but the character that
4847 * was there remains visible
4848 * Vim behaviour: the cursor moves backward and the character that
4849 * was there is erased from the screen.
4850 * We can emulate the vi behaviour by pretending there is a dollar
4851 * displayed even when there isn't.
4852 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004853 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 dollar_vcol = curwin->w_virtcol;
4855
Bram Moolenaarce3be472008-01-14 19:12:28 +00004856#ifdef FEAT_FOLDING
4857 /* When deleting a char the cursor line must never be in a closed fold.
4858 * E.g., when 'foldmethod' is indent and deleting the first non-white
4859 * char before a Tab. */
4860 if (did_backspace)
4861 foldOpenCursor();
4862#endif
4863
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 return did_backspace;
4865}
4866
Bram Moolenaarec2da362017-01-21 20:04:22 +01004867/*
4868 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4869 * P_PE literally.
4870 * When "drop" is TRUE then consume the text and drop it.
4871 */
4872 int
4873bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4874{
4875 int c;
4876 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4877 int idx = 0;
4878 char_u *end = find_termcode((char_u *)"PE");
4879 int ret_char = -1;
4880 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004881 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004882
4883 /* If the end code is too long we can't detect it, read everything. */
4884 if (STRLEN(end) >= NUMBUFLEN)
4885 end = NULL;
4886 ++no_mapping;
4887 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004888 if (!p_paste)
4889 // Also have the side effects of setting 'paste' to make it work much
4890 // faster.
4891 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004892
Bram Moolenaarec2da362017-01-21 20:04:22 +01004893 for (;;)
4894 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004895 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004896 if (end == NULL && vpeekc() == NUL)
4897 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004898 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004899 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004900 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004901 if (c == NUL || got_int)
4902 // When CTRL-C was encountered the typeahead will be flushed and we
4903 // won't get the end sequence.
4904 break;
4905
Bram Moolenaarec2da362017-01-21 20:04:22 +01004906 if (has_mbyte)
4907 idx += (*mb_char2bytes)(c, buf + idx);
4908 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004909 buf[idx++] = c;
4910 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004911 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004912 {
4913 if (end[idx] == NUL)
4914 break; /* Found the end of paste code. */
4915 continue;
4916 }
4917 if (!drop)
4918 {
4919 switch (mode)
4920 {
4921 case PASTE_CMDLINE:
4922 put_on_cmdline(buf, idx, TRUE);
4923 break;
4924
4925 case PASTE_EX:
4926 if (gap != NULL && ga_grow(gap, idx) == OK)
4927 {
4928 mch_memmove((char *)gap->ga_data + gap->ga_len,
4929 buf, (size_t)idx);
4930 gap->ga_len += idx;
4931 }
4932 break;
4933
4934 case PASTE_INSERT:
4935 if (stop_arrow() == OK)
4936 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004937 c = buf[0];
4938 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
4939 ins_eol(c);
4940 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01004941 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004942 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01004943 AppendToRedobuffLit(buf, idx);
4944 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01004945 }
4946 break;
4947
4948 case PASTE_ONE_CHAR:
4949 if (ret_char == -1)
4950 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01004951 if (has_mbyte)
4952 ret_char = (*mb_ptr2char)(buf);
4953 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004954 ret_char = buf[0];
4955 }
4956 break;
4957 }
4958 }
4959 idx = 0;
4960 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004961
Bram Moolenaarec2da362017-01-21 20:04:22 +01004962 --no_mapping;
4963 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004964 if (!save_paste)
4965 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01004966
4967 return ret_char;
4968}
4969
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004970#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004972ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004973{
4974 /* We will be leaving the current window, unless closing another tab. */
4975 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
4976 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
4977 {
4978 undisplay_dollar();
4979 start_arrow(&curwin->w_cursor);
4980# ifdef FEAT_CINDENT
4981 can_cindent = TRUE;
4982# endif
4983 }
4984
4985 if (c == K_TABLINE)
4986 goto_tabpage(current_tab);
4987 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004988 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004989 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004990 redraw_statuslines(); /* will redraw the tabline when needed */
4991 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004992}
4993#endif
4994
4995#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004997ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
4999 pos_T tpos;
5000
5001 undisplay_dollar();
5002 tpos = curwin->w_cursor;
5003 if (gui_do_scroll())
5004 {
5005 start_arrow(&tpos);
5006# ifdef FEAT_CINDENT
5007 can_cindent = TRUE;
5008# endif
5009 }
5010}
5011
5012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005013ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
5015 pos_T tpos;
5016
5017 undisplay_dollar();
5018 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005019 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
5021 start_arrow(&tpos);
5022# ifdef FEAT_CINDENT
5023 can_cindent = TRUE;
5024# endif
5025 }
5026}
5027#endif
5028
5029 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005030ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031{
5032 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005033 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
5035#ifdef FEAT_FOLDING
5036 if ((fdo_flags & FDO_HOR) && KeyTyped)
5037 foldOpenCursor();
5038#endif
5039 undisplay_dollar();
5040 tpos = curwin->w_cursor;
5041 if (oneleft() == OK)
5042 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005043#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5044 /* Only call start_arrow() when not busy with preediting, it will
5045 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005046 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005047#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005048 {
5049 start_arrow_with_change(&tpos, end_change);
5050 if (!end_change)
5051 AppendCharToRedobuff(K_LEFT);
5052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053#ifdef FEAT_RIGHTLEFT
5054 /* If exit reversed string, position is fixed */
5055 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5056 revins_legal++;
5057 revins_chars++;
5058#endif
5059 }
5060
5061 /*
5062 * if 'whichwrap' set for cursor in insert mode may go to
5063 * previous line
5064 */
5065 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5066 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005067 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 start_arrow(&tpos);
5069 --(curwin->w_cursor.lnum);
5070 coladvance((colnr_T)MAXCOL);
5071 curwin->w_set_curswant = TRUE; /* so we stay at the end */
5072 }
5073 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005074 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005075 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076}
5077
5078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005079ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 pos_T tpos;
5082
5083#ifdef FEAT_FOLDING
5084 if ((fdo_flags & FDO_HOR) && KeyTyped)
5085 foldOpenCursor();
5086#endif
5087 undisplay_dollar();
5088 tpos = curwin->w_cursor;
5089 if (c == K_C_HOME)
5090 curwin->w_cursor.lnum = 1;
5091 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 curwin->w_curswant = 0;
5094 start_arrow(&tpos);
5095}
5096
5097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005098ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
5100 pos_T tpos;
5101
5102#ifdef FEAT_FOLDING
5103 if ((fdo_flags & FDO_HOR) && KeyTyped)
5104 foldOpenCursor();
5105#endif
5106 undisplay_dollar();
5107 tpos = curwin->w_cursor;
5108 if (c == K_C_END)
5109 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5110 coladvance((colnr_T)MAXCOL);
5111 curwin->w_curswant = MAXCOL;
5112
5113 start_arrow(&tpos);
5114}
5115
5116 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005117ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005119 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#ifdef FEAT_FOLDING
5121 if ((fdo_flags & FDO_HOR) && KeyTyped)
5122 foldOpenCursor();
5123#endif
5124 undisplay_dollar();
5125 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5126 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005127 start_arrow_with_change(&curwin->w_cursor, end_change);
5128 if (!end_change)
5129 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 (void)bck_word(1L, FALSE, FALSE);
5131 curwin->w_set_curswant = TRUE;
5132 }
5133 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005134 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005135 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136}
5137
5138 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005139ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005141 int end_change = dont_sync_undo == FALSE; // end undoable change
5142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143#ifdef FEAT_FOLDING
5144 if ((fdo_flags & FDO_HOR) && KeyTyped)
5145 foldOpenCursor();
5146#endif
5147 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005148 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005150 start_arrow_with_change(&curwin->w_cursor, end_change);
5151 if (!end_change)
5152 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 if (virtual_active())
5155 oneright();
5156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005159 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 ++curwin->w_cursor.col;
5162 }
5163
5164#ifdef FEAT_RIGHTLEFT
5165 revins_legal++;
5166 if (revins_chars)
5167 revins_chars--;
5168#endif
5169 }
5170 /* if 'whichwrap' set for cursor in insert mode, may move the
5171 * cursor to the next line */
5172 else if (vim_strchr(p_ww, ']') != NULL
5173 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5174 {
5175 start_arrow(&curwin->w_cursor);
5176 curwin->w_set_curswant = TRUE;
5177 ++curwin->w_cursor.lnum;
5178 curwin->w_cursor.col = 0;
5179 }
5180 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005181 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005182 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183}
5184
5185 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005186ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005188 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189#ifdef FEAT_FOLDING
5190 if ((fdo_flags & FDO_HOR) && KeyTyped)
5191 foldOpenCursor();
5192#endif
5193 undisplay_dollar();
5194 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5195 || gchar_cursor() != NUL)
5196 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005197 start_arrow_with_change(&curwin->w_cursor, end_change);
5198 if (!end_change)
5199 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 (void)fwd_word(1L, FALSE, 0);
5201 curwin->w_set_curswant = TRUE;
5202 }
5203 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005204 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005205 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206}
5207
5208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005209ins_up(
5210 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211{
5212 pos_T tpos;
5213 linenr_T old_topline = curwin->w_topline;
5214#ifdef FEAT_DIFF
5215 int old_topfill = curwin->w_topfill;
5216#endif
5217
5218 undisplay_dollar();
5219 tpos = curwin->w_cursor;
5220 if (cursor_up(1L, TRUE) == OK)
5221 {
5222 if (startcol)
5223 coladvance(getvcol_nolist(&Insstart));
5224 if (old_topline != curwin->w_topline
5225#ifdef FEAT_DIFF
5226 || old_topfill != curwin->w_topfill
5227#endif
5228 )
5229 redraw_later(VALID);
5230 start_arrow(&tpos);
5231#ifdef FEAT_CINDENT
5232 can_cindent = TRUE;
5233#endif
5234 }
5235 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005236 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237}
5238
5239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005240ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241{
5242 pos_T tpos;
5243
5244 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005245
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005246 if (mod_mask & MOD_MASK_CTRL)
5247 {
5248 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005249 if (first_tabpage->tp_next != NULL)
5250 {
5251 start_arrow(&curwin->w_cursor);
5252 goto_tabpage(-1);
5253 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005254 return;
5255 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 tpos = curwin->w_cursor;
5258 if (onepage(BACKWARD, 1L) == OK)
5259 {
5260 start_arrow(&tpos);
5261#ifdef FEAT_CINDENT
5262 can_cindent = TRUE;
5263#endif
5264 }
5265 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005266 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267}
5268
5269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005270ins_down(
5271 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 pos_T tpos;
5274 linenr_T old_topline = curwin->w_topline;
5275#ifdef FEAT_DIFF
5276 int old_topfill = curwin->w_topfill;
5277#endif
5278
5279 undisplay_dollar();
5280 tpos = curwin->w_cursor;
5281 if (cursor_down(1L, TRUE) == OK)
5282 {
5283 if (startcol)
5284 coladvance(getvcol_nolist(&Insstart));
5285 if (old_topline != curwin->w_topline
5286#ifdef FEAT_DIFF
5287 || old_topfill != curwin->w_topfill
5288#endif
5289 )
5290 redraw_later(VALID);
5291 start_arrow(&tpos);
5292#ifdef FEAT_CINDENT
5293 can_cindent = TRUE;
5294#endif
5295 }
5296 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005297 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298}
5299
5300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005301ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
5303 pos_T tpos;
5304
5305 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005306
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005307 if (mod_mask & MOD_MASK_CTRL)
5308 {
5309 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00005310 if (first_tabpage->tp_next != NULL)
5311 {
5312 start_arrow(&curwin->w_cursor);
5313 goto_tabpage(0);
5314 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005315 return;
5316 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 tpos = curwin->w_cursor;
5319 if (onepage(FORWARD, 1L) == OK)
5320 {
5321 start_arrow(&tpos);
5322#ifdef FEAT_CINDENT
5323 can_cindent = TRUE;
5324#endif
5325 }
5326 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005327 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328}
5329
5330#ifdef FEAT_DND
5331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005332ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5335}
5336#endif
5337
5338/*
5339 * Handle TAB in Insert or Replace mode.
5340 * Return TRUE when the TAB needs to be inserted like a normal character.
5341 */
5342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005343ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
5345 int ind;
5346 int i;
5347 int temp;
5348
5349 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5350 Insstart_blank_vcol = get_nolist_virtcol();
5351 if (echeck_abbr(TAB + ABBR_OFF))
5352 return FALSE;
5353
5354 ind = inindent(0);
5355#ifdef FEAT_CINDENT
5356 if (ind)
5357 can_cindent = FALSE;
5358#endif
5359
5360 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005361 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 */
5363 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005364#ifdef FEAT_VARTABS
5365 && !(p_sta && ind
5366 /* These five lines mean 'tabstop' != 'shiftwidth' */
5367 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5368 || (tabstop_count(curbuf->b_p_vts_array) == 1
5369 && tabstop_first(curbuf->b_p_vts_array)
5370 != get_sw_value(curbuf))
5371 || (tabstop_count(curbuf->b_p_vts_array) == 0
5372 && curbuf->b_p_ts != get_sw_value(curbuf))))
5373 && tabstop_count(curbuf->b_p_vsts_array) == 0
5374#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005375 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005376#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005377 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return TRUE;
5379
5380 if (stop_arrow() == FAIL)
5381 return TRUE;
5382
5383 did_ai = FALSE;
5384#ifdef FEAT_SMARTINDENT
5385 did_si = FALSE;
5386 can_si = FALSE;
5387 can_si_back = FALSE;
5388#endif
5389 AppendToRedobuff((char_u *)"\t");
5390
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005391#ifdef FEAT_VARTABS
5392 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
5393 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005394 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005395 temp -= get_nolist_virtcol() % temp;
5396 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005397 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005398 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005399 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005400 curbuf->b_p_vsts_array);
5401 else /* otherwise use 'tabstop' */
5402 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5403 curbuf->b_p_vts_array);
5404#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005406 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005407 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
5408 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 else /* otherwise use 'tabstop' */
5410 temp = (int)curbuf->b_p_ts;
5411 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
5414 /*
5415 * Insert the first space with ins_char(). It will delete one char in
5416 * replace mode. Insert the rest with ins_str(); it will not delete any
5417 * chars. For VREPLACE mode, we use ins_char() for all characters.
5418 */
5419 ins_char(' ');
5420 while (--temp > 0)
5421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 if (State & VREPLACE_FLAG)
5423 ins_char(' ');
5424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
5426 ins_str((char_u *)" ");
5427 if (State & REPLACE_FLAG) /* no char replaced */
5428 replace_push(NUL);
5429 }
5430 }
5431
5432 /*
5433 * When 'expandtab' not set: Replace spaces by TABs where possible.
5434 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005435#ifdef FEAT_VARTABS
5436 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5437 || get_sts_value() > 0
5438 || (p_sta && ind)))
5439#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005440 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
5443 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 char_u *saved_line = NULL; /* init for GCC */
5445 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 pos_T fpos;
5447 pos_T *cursor;
5448 colnr_T want_vcol, vcol;
5449 int change_col = -1;
5450 int save_list = curwin->w_p_list;
5451
5452 /*
5453 * Get the current line. For VREPLACE mode, don't make real changes
5454 * yet, just work on a copy of the line.
5455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (State & VREPLACE_FLAG)
5457 {
5458 pos = curwin->w_cursor;
5459 cursor = &pos;
5460 saved_line = vim_strsave(ml_get_curline());
5461 if (saved_line == NULL)
5462 return FALSE;
5463 ptr = saved_line + pos.col;
5464 }
5465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 ptr = ml_get_cursor();
5468 cursor = &curwin->w_cursor;
5469 }
5470
5471 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
5472 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5473 curwin->w_p_list = FALSE;
5474
5475 /* Find first white before the cursor */
5476 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005477 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 {
5479 --fpos.col;
5480 --ptr;
5481 }
5482
5483 /* In Replace mode, don't change characters before the insert point. */
5484 if ((State & REPLACE_FLAG)
5485 && fpos.lnum == Insstart.lnum
5486 && fpos.col < Insstart.col)
5487 {
5488 ptr += Insstart.col - fpos.col;
5489 fpos.col = Insstart.col;
5490 }
5491
5492 /* compute virtual column numbers of first white and cursor */
5493 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5494 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5495
Bram Moolenaar597a4222014-06-25 14:39:50 +02005496 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5497 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01005498 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005500 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (vcol + i > want_vcol)
5502 break;
5503 if (*ptr != TAB)
5504 {
5505 *ptr = TAB;
5506 if (change_col < 0)
5507 {
5508 change_col = fpos.col; /* Column of first change */
5509 /* May have to adjust Insstart */
5510 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5511 Insstart.col = fpos.col;
5512 }
5513 }
5514 ++fpos.col;
5515 ++ptr;
5516 vcol += i;
5517 }
5518
5519 if (change_col >= 0)
5520 {
5521 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005522 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
5524 /* Skip over the spaces we need. */
5525 while (vcol < want_vcol && *ptr == ' ')
5526 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005527 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 ++ptr;
5529 ++repl_off;
5530 }
5531 if (vcol > want_vcol)
5532 {
5533 /* Must have a char with 'showbreak' just before it. */
5534 --ptr;
5535 --repl_off;
5536 }
5537 fpos.col += repl_off;
5538
5539 /* Delete following spaces. */
5540 i = cursor->col - fpos.col;
5541 if (i > 0)
5542 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005543 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005545 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 for (temp = i; --temp >= 0; )
5547 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005548#ifdef FEAT_TEXT_PROP
5549 curbuf->b_ml.ml_line_len -= i;
5550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005552#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005553 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005554 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005555 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005556 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5557 (char_u *)"\t", 1);
5558 }
5559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 cursor->col -= i;
5561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 /*
5563 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5564 * backspacing over the changed spacing and then inserting the new
5565 * spacing.
5566 */
5567 if (State & VREPLACE_FLAG)
5568 {
5569 /* Backspace from real cursor to change_col */
5570 backspace_until_column(change_col);
5571
5572 /* Insert each char in saved_line from changed_col to
5573 * ptr-cursor */
5574 ins_bytes_len(saved_line + change_col,
5575 cursor->col - change_col);
5576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 if (State & VREPLACE_FLAG)
5580 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 curwin->w_p_list = save_list;
5582 }
5583
5584 return FALSE;
5585}
5586
5587/*
5588 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005589 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005592ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 int i;
5595
5596 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005597 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 undisplay_dollar();
5601
5602 /*
5603 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5604 * character under the cursor. Only push a NUL on the replace stack,
5605 * nothing to put back when the NL is deleted.
5606 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005607 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 replace_push(NUL);
5609
5610 /*
5611 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5612 * replacing the next line, so we push all of the characters left on the
5613 * line onto the replace stack. This is not done here though, it is done
5614 * in open_line().
5615 */
5616
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005617 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
5618 * CTRL-O). */
5619 if (virtual_active() && curwin->w_cursor.coladd > 0)
5620 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 /* NL in reverse insert will always start in the end of
5624 * current line. */
5625 if (revins_on)
5626 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5627#endif
5628
5629 AppendToRedobuff(NL_STR);
5630 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005631 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 old_indent = 0;
5633#ifdef FEAT_CINDENT
5634 can_cindent = TRUE;
5635#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005636#ifdef FEAT_FOLDING
5637 /* When inserting a line the cursor line must never be in a closed fold. */
5638 foldOpenCursor();
5639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005641 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642}
5643
5644#ifdef FEAT_DIGRAPHS
5645/*
5646 * Handle digraph in insert mode.
5647 * Returns character still to be inserted, or NUL when nothing remaining to be
5648 * done.
5649 */
5650 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005651ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
5653 int c;
5654 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005655 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 pc_status = PC_STATUS_UNSET;
5658 if (redrawing() && !char_avail())
5659 {
5660 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005661 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005664 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665#ifdef FEAT_CMDL_INFO
5666 add_to_showcmd_c(Ctrl_K);
5667#endif
5668 }
5669
5670#ifdef USE_ON_FLY_SCROLL
5671 dont_scroll = TRUE; /* disallow scrolling here */
5672#endif
5673
5674 /* don't map the digraph chars. This also prevents the
5675 * mode message to be deleted when ESC is hit */
5676 ++no_mapping;
5677 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005678 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 --no_mapping;
5680 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005681 if (did_putchar)
5682 /* when the line fits in 'columns' the '?' is at the start of the next
5683 * line and will not be removed by the redraw */
5684 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (IS_SPECIAL(c) || mod_mask) /* special key */
5687 {
5688#ifdef FEAT_CMDL_INFO
5689 clear_showcmd();
5690#endif
5691 insert_special(c, TRUE, FALSE);
5692 return NUL;
5693 }
5694 if (c != ESC)
5695 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005696 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 if (redrawing() && !char_avail())
5698 {
5699 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005700 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
5702 if (char2cells(c) == 1)
5703 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005704 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005706 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708#ifdef FEAT_CMDL_INFO
5709 add_to_showcmd_c(c);
5710#endif
5711 }
5712 ++no_mapping;
5713 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005714 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 --no_mapping;
5716 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005717 if (did_putchar)
5718 /* when the line fits in 'columns' the '?' is at the start of the
5719 * next line and will not be removed by a redraw */
5720 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 if (cc != ESC)
5722 {
5723 AppendToRedobuff((char_u *)CTRL_V_STR);
5724 c = getdigraph(c, cc, TRUE);
5725#ifdef FEAT_CMDL_INFO
5726 clear_showcmd();
5727#endif
5728 return c;
5729 }
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731#ifdef FEAT_CMDL_INFO
5732 clear_showcmd();
5733#endif
5734 return NUL;
5735}
5736#endif
5737
5738/*
5739 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5740 * Returns the char to be inserted, or NUL if none found.
5741 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005743ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
5745 int c;
5746 int temp;
5747 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005748 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
5750 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5751 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005752 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 return NUL;
5754 }
5755
5756 /* try to advance to the cursor column */
5757 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005758 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 prev_ptr = ptr;
5760 validate_virtcol();
5761 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5762 {
5763 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005764 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766 if ((colnr_T)temp > curwin->w_virtcol)
5767 ptr = prev_ptr;
5768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005771 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 return c;
5773}
5774
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005775/*
5776 * CTRL-Y or CTRL-E typed in Insert mode.
5777 */
5778 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005779ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005780{
5781 int c = tc;
5782
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005783 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005784 {
5785 if (c == Ctrl_Y)
5786 scrolldown_clamp();
5787 else
5788 scrollup_clamp();
5789 redraw_later(VALID);
5790 }
5791 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005792 {
5793 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5794 if (c != NUL)
5795 {
5796 long tw_save;
5797
5798 /* The character must be taken literally, insert like it
5799 * was typed after a CTRL-V, and pretend 'textwidth'
5800 * wasn't set. Digits, 'o' and 'x' are special after a
5801 * CTRL-V, don't use it for these. */
5802 if (c < 256 && !isalnum(c))
5803 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
5804 tw_save = curbuf->b_p_tw;
5805 curbuf->b_p_tw = -1;
5806 insert_special(c, TRUE, FALSE);
5807 curbuf->b_p_tw = tw_save;
5808#ifdef FEAT_RIGHTLEFT
5809 revins_chars++;
5810 revins_legal++;
5811#endif
5812 c = Ctrl_V; /* pretend CTRL-V is last character */
5813 auto_format(FALSE, TRUE);
5814 }
5815 }
5816 return c;
5817}
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819/*
5820 * Get the value that w_virtcol would have when 'list' is off.
5821 * Unless 'cpo' contains the 'L' flag.
5822 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005823 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005824get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005826 // check validity of cursor in current buffer
5827 if (curwin->w_buffer == NULL
5828 || curwin->w_buffer->b_ml.ml_mfp == NULL
5829 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5830 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5832 return getvcol_nolist(&curwin->w_cursor);
5833 validate_virtcol();
5834 return curwin->w_virtcol;
5835}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005836
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005837#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005838/*
5839 * Handle the InsertCharPre autocommand.
5840 * "c" is the character that was typed.
5841 * Return a pointer to allocated memory with the replacement string.
5842 * Return NULL to continue inserting "c".
5843 */
5844 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005845do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005846{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005847 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005848 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005849 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005850
5851 /* Return quickly when there is nothing to do. */
5852 if (!has_insertcharpre())
5853 return NULL;
5854
Bram Moolenaar704984a2012-06-01 14:57:51 +02005855 if (has_mbyte)
5856 buf[(*mb_char2bytes)(c, buf)] = NUL;
5857 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005858 {
5859 buf[0] = c;
5860 buf[1] = NUL;
5861 }
5862
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005863 /* Lock the text to avoid weird things from happening. */
5864 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005865 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005866
Bram Moolenaar704984a2012-06-01 14:57:51 +02005867 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005868 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005869 {
5870 /* Get the value of v:char. It may be empty or more than one
5871 * character. Only use it when changed, otherwise continue with the
5872 * original character to avoid breaking autoindent. */
5873 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5874 res = vim_strsave(get_vim_var_str(VV_CHAR));
5875 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005876
5877 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
5878 --textlock;
5879
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005880 // Restore the State, it may have been changed.
5881 State = save_State;
5882
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005883 return res;
5884}
5885#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005886
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005887#if defined(FEAT_CINDENT) || defined(PROTO)
5888 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005889get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005890{
5891 return can_cindent;
5892}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005893
5894 void
5895set_can_cindent(int val)
5896{
5897 can_cindent = val;
5898}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005899#endif
5900
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005901/*
5902 * Trigger "event" and take care of fixing undo.
5903 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005904 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005905ins_apply_autocmds(event_T event)
5906{
5907 varnumber_T tick = CHANGEDTICK(curbuf);
5908 int r;
5909
5910 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5911
5912 // If u_savesub() was called then we are not prepared to start
5913 // a new line. Call u_save() with no contents to fix that.
5914 if (tick != CHANGEDTICK(curbuf))
5915 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5916
5917 return r;
5918}