blob: d92a9513981c0ae0f37e4baf67b532ac7587dd74 [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 Moolenaar5d18efe2019-12-01 21:11:22 +010021// Set when doing something for completion that may call edit() recursively,
22// which is not allowed.
Bram Moolenaar7591bb32019-03-30 13:53:47 +010023static 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 Moolenaarf28dbce2016-01-29 22:03:47 +010027static void insert_special(int, int, int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void redo_literal(int c);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010029static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +000030#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static void check_spell_redraw(void);
Bram Moolenaar217ad922005-03-20 22:37:15 +000032#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
34static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void replace_flush(void);
37static void replace_do_bs(int limit_col);
38static int del_char_after_col(int limit_col);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static void ins_reg(void);
40static void ins_ctrl_g(void);
41static void ins_ctrl_hat(void);
42static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static int ins_start_select(int c);
47static void ins_insert(int replaceState);
48static void ins_ctrl_o(void);
49static void ins_shift(int c, int lastc);
50static void ins_del(void);
51static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000052#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010053static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000054#endif
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010055static void ins_left(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010056static void ins_home(int c);
57static void ins_end(int c);
58static void ins_s_left(void);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010059static void ins_right(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010060static void ins_s_right(void);
61static void ins_up(int startcol);
62static void ins_pageup(void);
63static void ins_down(int startcol);
64static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static int ins_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static int ins_ctrl_ey(int tc);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010073#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +010075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010077static colnr_T Insstart_textlen; // length of line when insert started
78static colnr_T Insstart_blank_vcol; // vcol for first inserted blank
79static int update_Insstart_orig = TRUE; // set Insstart_orig to Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010081static char_u *last_insert = NULL; // the text of the previous insert,
82 // K_SPECIAL and CSI are escaped
83static int last_insert_skip; // nr of chars in front of previous insert
84static int new_insert_skip; // nr of chars in front of current insert
85static int did_restart_edit; // "restart_edit" when calling edit()
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
87#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010088static int can_cindent; // may do cindenting on this line
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#endif
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010092static int revins_on; // reverse insert mode on
93static int revins_chars; // how much to skip after edit
94static int revins_legal; // was the last char 'legal'?
95static int revins_scol; // start column of revins session
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#endif
97
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010098static int ins_need_undo; // call u_save() before inserting a
99 // char. Set when edit() is called.
100 // after that arrow_used is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100102static int dont_sync_undo = FALSE; // CTRL-G U prevents syncing undo for
103 // the next left/right cursor key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/*
106 * edit(): Start inserting text.
107 *
108 * "cmdchar" can be:
109 * 'i' normal insert command
110 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100111 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 * 'R' replace command
113 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
114 * but still only one <CR> is inserted. The <Esc> is not used for redo.
115 * 'g' "gI" command.
116 * 'V' "gR" command for Virtual Replace mode.
117 * 'v' "gr" command for single character Virtual Replace mode.
118 *
119 * This function is not called recursively. For CTRL-O commands, it returns
120 * and lets the caller handle the Normal-mode command.
121 *
122 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
123 */
124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100125edit(
126 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100127 int startln, // if set, insert at start of line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100128 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129{
130 int c = 0;
131 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100132 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000133 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 int i;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100136 int did_backspace = TRUE; // previous char was backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100138 int line_is_white = FALSE; // line is empty before insert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100140 linenr_T old_topline = 0; // topline before insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_DIFF
142 int old_topfill = -1;
143#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100144 int inserted_space = FALSE; // just inserted a space
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 int replaceState = REPLACE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100146 int nomove = FALSE; // don't move cursor on return
Bram Moolenaarf2732452018-06-03 14:47:35 +0200147#ifdef FEAT_JOB_CHANNEL
148 int cmdchar_todo = cmdchar;
149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100151 // Remember whether editing was restarted after CTRL-O.
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000152 did_restart_edit = restart_edit;
153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100154 // sleep before redrawing, needed for "CTRL-O :" that results in an
155 // error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 check_for_delay(TRUE);
157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100158 // set Insstart_orig to Insstart
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100159 update_Insstart_orig = TRUE;
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#ifdef HAVE_SANDBOX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100162 // Don't allow inserting in the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 if (sandbox != 0)
164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100165 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 return FALSE;
167 }
168#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100169 // Don't allow changes in the buffer while editing the cmdline. The
170 // caller of getcmdline() may get confused.
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100171 // Don't allow recursive insert mode when busy with completion.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200172 if (textwinlock != 0 || textlock != 0
173 || ins_compl_active() || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000174 {
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200175 emsg(_(e_textwinlock));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000176 return FALSE;
177 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100178 ins_compl_clear(); // clear stuff for CTRL-X mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaar843ee412004-06-30 16:16:41 +0000180 /*
181 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
182 */
183 if (cmdchar != 'r' && cmdchar != 'v')
184 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100185 pos_T save_cursor = curwin->w_cursor;
186
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100187#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000188 if (cmdchar == 'R')
189 ptr = (char_u *)"r";
190 else if (cmdchar == 'V')
191 ptr = (char_u *)"v";
192 else
193 ptr = (char_u *)"i";
194 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100195 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100196#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200197 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100199 // Make sure the cursor didn't move. Do call check_cursor_col() in
200 // case the text was modified. Since Insert mode was not started yet
201 // a call to check_cursor_col() may move the cursor, especially with
202 // the "A" command, thus set State to avoid that. Also check that the
203 // line number is still valid (lines may have been deleted).
204 // Do not restore if v:char was set to a non-empty string.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100205 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100206#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200207 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100208#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200209 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100210 {
211 int save_state = State;
212
213 curwin->w_cursor = save_cursor;
214 State = INSERT;
215 check_cursor_col();
216 State = save_state;
217 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000218 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000219
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200220#ifdef FEAT_CONCEAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100221 // Check if the cursor line needs redrawing before changing State. If
222 // 'concealcursor' is "n" it needs to be redrawn without concealing.
Bram Moolenaarb9464822018-05-10 15:09:49 +0200223 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200224#endif
225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 /*
227 * When doing a paste with the middle mouse button, Insstart is set to
228 * where the paste started.
229 */
230 if (where_paste_started.lnum != 0)
231 Insstart = where_paste_started;
232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {
234 Insstart = curwin->w_cursor;
235 if (startln)
236 Insstart.col = 0;
237 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000238 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 Insstart_blank_vcol = MAXCOL;
240 if (!did_ai)
241 ai_col = 0;
242
243 if (cmdchar != NUL && restart_edit == 0)
244 {
245 ResetRedobuff();
246 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 if (cmdchar == 'V' || cmdchar == 'v')
248 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100249 // "gR" or "gr" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 AppendCharToRedobuff('g');
251 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
252 }
253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100255 if (cmdchar == K_PS)
256 AppendCharToRedobuff('a');
257 else
258 AppendCharToRedobuff(cmdchar);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100259 if (cmdchar == 'g') // "gI" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 AppendCharToRedobuff('I');
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100261 else if (cmdchar == 'r') // "r<CR>" command
262 count = 1; // insert only one <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 }
264 }
265
266 if (cmdchar == 'R')
267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 State = REPLACE;
269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 else if (cmdchar == 'V' || cmdchar == 'v')
271 {
272 State = VREPLACE;
273 replaceState = VREPLACE;
274 orig_line_count = curbuf->b_ml.ml_line_count;
275 vr_lines_changed = 1;
276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 else
278 State = INSERT;
279
280 stop_insert_mode = FALSE;
281
282 /*
283 * Need to recompute the cursor position, it might move when the cursor is
284 * on a TAB or special character.
285 */
286 curs_columns(TRUE);
287
288 /*
289 * Enable langmap or IME, indicated by 'iminsert'.
290 * Note that IME may enabled/disabled without us noticing here, thus the
291 * 'iminsert' value may not reflect what is actually used. It is updated
292 * when hitting <Esc>.
293 */
294 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
295 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100296#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
298#endif
299
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#ifdef FEAT_CMDL_INFO
302 clear_showcmd();
303#endif
304#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100305 // there is no reverse replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 revins_on = (State == INSERT && p_ri);
307 if (revins_on)
308 undisplay_dollar();
309 revins_chars = 0;
310 revins_legal = 0;
311 revins_scol = -1;
312#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100313 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100314 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200315#ifdef FEAT_JOB_CHANNEL
316 ch_log_output = TRUE;
317#endif
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100318 // Disable bracketed paste mode, we won't recognize the escape
319 // sequences.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100320 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100322 // Disable modifyOtherKeys, keys with modifiers would cause exiting
323 // Insert mode.
324 out_str(T_CTE);
325 }
326
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 /*
328 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100329 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
330 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 */
332 if (restart_edit != 0 && stuff_empty())
333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 /*
335 * After a paste we consider text typed to be part of the insert for
336 * the pasted text. You can backspace over the pasted text too.
337 */
338 if (where_paste_started.lnum)
339 arrow_used = FALSE;
340 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 arrow_used = TRUE;
342 restart_edit = 0;
343
344 /*
345 * If the cursor was after the end-of-line before the CTRL-O and it is
346 * now at the end-of-line, put it after the end-of-line (this is not
347 * correct in very rare cases).
348 * Also do this if curswant is greater than the current virtual
349 * column. Eg after "^O$" or "^O80|".
350 */
351 validate_virtcol();
352 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000353 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 || curwin->w_curswant > curwin->w_virtcol)
355 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
356 {
357 if (ptr[1] == NUL)
358 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 else if (has_mbyte)
360 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000361 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (ptr[i] == NUL)
363 curwin->w_cursor.col += i;
364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000366 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 }
368 else
369 arrow_used = FALSE;
370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100371 // we are in insert mode now, don't need to start it anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 need_start_insertmode = FALSE;
373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100374 // Need to save the line for undo before inserting the first char.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 ins_need_undo = TRUE;
376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 where_paste_started.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#ifdef FEAT_CINDENT
379 can_cindent = TRUE;
380#endif
381#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100382 // The cursor line is not in a closed fold, unless 'insertmode' is set or
383 // restarting.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 if (!p_im && did_restart_edit == 0)
385 foldOpenCursor();
386#endif
387
388 /*
389 * If 'showmode' is set, show the current (insert/replace/..) mode.
390 * A warning message for changing a readonly file is given here, before
391 * actually changing anything. It's put after the mode, if any.
392 */
393 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000394 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 i = showmode();
396
397 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000398 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399
400#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100401 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#endif
403#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100404 do_digraph(-1); // clear digraphs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#endif
406
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000407 /*
408 * Get the current length of the redo buffer, those characters have to be
409 * skipped if we want to get to the inserted characters.
410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 ptr = get_inserted();
412 if (ptr == NULL)
413 new_insert_skip = 0;
414 else
415 {
416 new_insert_skip = (int)STRLEN(ptr);
417 vim_free(ptr);
418 }
419
420 old_indent = 0;
421
422 /*
423 * Main loop in Insert mode: repeat until Insert mode is left.
424 */
425 for (;;)
426 {
427#ifdef FEAT_RIGHTLEFT
428 if (!revins_legal)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100429 revins_scol = -1; // reset on illegal motions
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 else
431 revins_legal = 0;
432#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100433 if (arrow_used) // don't repeat insert when arrow key used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 count = 0;
435
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100436 if (update_Insstart_orig)
437 Insstart_orig = Insstart;
438
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200439 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100441 // ":stopinsert" used or 'insertmode' reset
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 count = 0;
443 goto doESCkey;
444 }
445
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100446 // set curwin->w_curswant for next K_DOWN or K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 if (!arrow_used)
448 curwin->w_set_curswant = TRUE;
449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100450 // If there is no typeahead may check for timestamps (e.g., for when a
451 // menu invoked a shell command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 if (stuff_empty())
453 {
454 did_check_timestamps = FALSE;
455 if (need_check_timestamps)
456 check_timestamps(FALSE);
457 }
458
459 /*
460 * When emsg() was called msg_scroll will have been set.
461 */
462 msg_scroll = FALSE;
463
464#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100465 // When 'mousefocus' is set a mouse movement may have taken us to
466 // another window. "need_mouse_correct" may then be set because of an
467 // autocommand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 if (need_mouse_correct)
469 gui_mouse_correct();
470#endif
471
472#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100473 // Open fold at the cursor line, according to 'foldopen'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 if (fdo_flags & FDO_INSERT)
475 foldOpenCursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100476 // Close folds where the cursor isn't, according to 'foldclose'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 if (!char_avail())
478 foldCheckClose();
479#endif
480
Bram Moolenaarf2732452018-06-03 14:47:35 +0200481#ifdef FEAT_JOB_CHANNEL
482 if (bt_prompt(curbuf))
483 {
484 init_prompt(cmdchar_todo);
485 cmdchar_todo = NUL;
486 }
487#endif
488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 /*
490 * If we inserted a character at the last position of the last line in
491 * the window, scroll the window one line up. This avoids an extra
492 * redraw.
493 * This is detected when the cursor column is smaller after inserting
494 * something.
495 * Don't do this when the topline changed already, it has
496 * already been adjusted (by insertchar() calling open_line())).
497 */
498 if (curbuf->b_mod_set
499 && curwin->w_p_wrap
500 && !did_backspace
501 && curwin->w_topline == old_topline
502#ifdef FEAT_DIFF
503 && curwin->w_topfill == old_topfill
504#endif
505 )
506 {
507 mincol = curwin->w_wcol;
508 validate_cursor_col();
509
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200510 if (
511#ifdef FEAT_VARTABS
512 (int)curwin->w_wcol < mincol - tabstop_at(
513 get_nolist_virtcol(), curbuf->b_p_ts,
514 curbuf->b_p_vts_array)
515#else
516 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100519 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 && (curwin->w_cursor.lnum != curwin->w_topline
521#ifdef FEAT_DIFF
522 || curwin->w_topfill > 0
523#endif
524 ))
525 {
526#ifdef FEAT_DIFF
527 if (curwin->w_topfill > 0)
528 --curwin->w_topfill;
529 else
530#endif
531#ifdef FEAT_FOLDING
532 if (hasFolding(curwin->w_topline, NULL, &old_topline))
533 set_topline(curwin, old_topline + 1);
534 else
535#endif
536 set_topline(curwin, curwin->w_topline + 1);
537 }
538 }
539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100540 // May need to adjust w_topline to show the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 update_topline();
542
543 did_backspace = FALSE;
544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100545 validate_cursor(); // may set must_redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
547 /*
548 * Redraw the display when no characters are waiting.
549 * Also shows mode, ruler and positions cursor.
550 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000551 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (curwin->w_p_scb)
554 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar860cae12010-06-05 23:22:07 +0200556 if (curwin->w_p_crb)
557 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 update_curswant();
559 old_topline = curwin->w_topline;
560#ifdef FEAT_DIFF
561 old_topfill = curwin->w_topfill;
562#endif
563
564#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100565 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566#endif
567
568 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100569 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100571 if (c != K_CURSORHOLD)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100572 lastc = c; // remember the previous char for CTRL-D
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100574 // After using CTRL-G U the next cursor key will not break undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200575 if (dont_sync_undo == MAYBE)
576 dont_sync_undo = TRUE;
577 else
578 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100579 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100580 // Got here from normal mode when bracketed paste started.
Bram Moolenaarec2da362017-01-21 20:04:22 +0100581 c = K_PS;
582 else
583 do
584 {
585 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200586
587 if (stop_insert_mode)
588 {
589 // Insert mode ended, possibly from a callback.
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200590 if (c != K_IGNORE && c != K_NOP)
591 vungetc(c);
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200592 count = 0;
593 nomove = TRUE;
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200594 ins_compl_prep(ESC);
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200595 goto doESCkey;
596 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100597 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100599 // Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V.
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000600 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#ifdef FEAT_RIGHTLEFT
603 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100604 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000607 /*
608 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000609 * and the cursor is still in the completed word. Only when there is
610 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000611 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100612 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000613 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100614 && curwin->w_cursor.col >= ins_compl_col()
615 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100617 // BS: Delete one character from "compl_leader".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000618 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100619 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000620 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000621 continue;
622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100623 // When no match was selected or it was edited.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100624 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100626 // CTRL-L: Add one character from the current match to
627 // "compl_leader". Except when at the original match and
628 // there is nothing to add, CTRL-L works like CTRL-P then.
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000629 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100630 && (!ctrl_x_mode_line_or_eval()
631 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000632 {
633 ins_compl_addfrommatch();
634 continue;
635 }
636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100637 // A non-white character that fits in with the current
638 // completion: Add to "compl_leader".
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000639 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000640 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100641#if defined(FEAT_EVAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100642 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100643 char_u *str = do_insert_char_pre(c);
644 char_u *p;
645
646 if (str != NULL)
647 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100648 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100649 ins_compl_addleader(PTR2CHAR(p));
650 vim_free(str);
651 }
652 else
653#endif
654 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000655 continue;
656 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100658 // Pressing CTRL-Y selects the current match. When
659 // ins_compl_enter_selects() is set the Enter key does the
660 // same.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100661 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200662 && (c == CAR || c == K_KENTER || c == NL)))
663 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000664 {
665 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200666 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000667 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000668 }
669 }
670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100671 // Prepare for or stop CTRL-X mode. This doesn't do completion, but
672 // it does fix up the text when finishing completion.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100673 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000674 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000675 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100677 // CTRL-\ CTRL-N goes to Normal mode,
678 // CTRL-\ CTRL-G goes to mode selected with 'insertmode',
679 // CTRL-\ CTRL-O is like CTRL-O but without moving the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (c == Ctrl_BSL)
681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100682 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +0000683 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 ++no_mapping;
685 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000686 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 --no_mapping;
688 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000689 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100691 // it's something else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 vungetc(c);
693 c = Ctrl_BSL;
694 }
695 else if (c == Ctrl_G && p_im)
696 continue;
697 else
698 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000699 if (c == Ctrl_O)
700 {
701 ins_ctrl_o();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100702 ins_at_eol = FALSE; // cursor keeps its column
Bram Moolenaar488c6512005-08-11 20:09:58 +0000703 nomove = TRUE;
704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 count = 0;
706 goto doESCkey;
707 }
708 }
709
710#ifdef FEAT_DIGRAPHS
711 c = do_digraph(c);
712#endif
713
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100714 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (c == Ctrl_V || c == Ctrl_Q)
717 {
718 ins_ctrl_v();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100719 c = Ctrl_V; // pretend CTRL-V is last typed character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 continue;
721 }
722
723#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200724 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100726 // A key name preceded by a bang means this key is not to be
727 // inserted. Skip ahead to the re-indenting below.
728 // A key name preceded by a star means that indenting has to be
729 // done before inserting the key.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 line_is_white = inindent(0);
731 if (in_cinkeys(c, '!', line_is_white))
732 goto force_cindent;
733 if (can_cindent && in_cinkeys(c, '*', line_is_white)
734 && stop_arrow() == OK)
735 do_c_expr_indent();
736 }
737#endif
738
739#ifdef FEAT_RIGHTLEFT
740 if (curwin->w_p_rl)
741 switch (c)
742 {
743 case K_LEFT: c = K_RIGHT; break;
744 case K_S_LEFT: c = K_S_RIGHT; break;
745 case K_C_LEFT: c = K_C_RIGHT; break;
746 case K_RIGHT: c = K_LEFT; break;
747 case K_S_RIGHT: c = K_S_LEFT; break;
748 case K_C_RIGHT: c = K_C_LEFT; break;
749 }
750#endif
751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /*
753 * If 'keymodel' contains "startsel", may start selection. If it
754 * does, a CTRL-O and c will be stuffed, we need to get these
755 * characters.
756 */
757 if (ins_start_select(c))
758 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 /*
761 * The big switch to handle a character in insert mode.
762 */
763 switch (c)
764 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100765 case ESC: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (echeck_abbr(ESC + ABBR_OFF))
767 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 case Ctrl_C: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#ifdef FEAT_CMDWIN
772 if (c == Ctrl_C && cmdwin_type != 0)
773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // Close the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 cmdwin_result = K_IGNORE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100776 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000777 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 goto doESCkey;
779 }
780#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200781#ifdef FEAT_JOB_CHANNEL
782 if (c == Ctrl_C && bt_prompt(curbuf))
783 {
784 if (invoke_prompt_interrupt())
785 {
786 if (!bt_prompt(curbuf))
787 // buffer changed to a non-prompt buffer, get out of
788 // Insert mode
789 goto doESCkey;
790 break;
791 }
792 }
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795#ifdef UNIX
796do_intr:
797#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100798 // when 'insertmode' set, and not halfway a mapping, don't leave
799 // Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (goto_im())
801 {
802 if (got_int)
803 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100804 (void)vgetc(); // flush all buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 got_int = FALSE;
806 }
807 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200808 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 break;
810 }
811doESCkey:
812 /*
813 * This is the ONLY return from edit()!
814 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100815 // Always update o_lnum, so that a "CTRL-O ." that adds a line
816 // still puts the cursor back after the inserted text.
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000817 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 o_lnum = curwin->w_cursor.lnum;
819
Bram Moolenaar488c6512005-08-11 20:09:58 +0000820 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000821 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100822 // When CTRL-C was typed got_int will be set, with the result
823 // that the autocommands won't be executed. When mapped got_int
824 // is not set, but let's keep the behavior the same.
825 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200826 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000827 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 continue;
831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 case Ctrl_Z: // suspend when 'insertmode' set
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000833 if (!p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100834 goto normalchar; // insert CTRL-Z as normal char
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100835 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100836#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100837 ui_cursor_shape(); // may need to update cursor shape
Bram Moolenaar74a47162017-02-26 19:09:05 +0100838#endif
839 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 case Ctrl_O: // execute one command
Bram Moolenaare344bea2005-09-01 20:46:49 +0000842#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000844 goto docomplete;
845#endif
846 if (echeck_abbr(Ctrl_O + ABBR_OFF))
847 break;
848 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // don't move the cursor left when 'virtualedit' has "onemore".
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000851 if (ve_flags & VE_ONEMORE)
852 {
853 ins_at_eol = FALSE;
854 nomove = TRUE;
855 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000856 count = 0;
857 goto doESCkey;
858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100859 case K_INS: // toggle insert/replace mode
Bram Moolenaar572cb562005-08-05 21:35:02 +0000860 case K_KINS:
861 ins_insert(replaceState);
862 break;
863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100864 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar572cb562005-08-05 21:35:02 +0000865 break;
866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100867 case K_HELP: // Help key works like <ESC> <Help>
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000868 case K_F1:
869 case K_XF1:
870 stuffcharReadbuff(K_HELP);
871 if (p_im)
872 need_start_insertmode = TRUE;
873 goto doESCkey;
874
875#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100876 case K_F21: // NetBeans command
877 ++no_mapping; // don't map the next key hits
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000878 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000879 --no_mapping;
880 netbeans_keycommand(i);
881 break;
882#endif
883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100884 case K_ZERO: // Insert the previously inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 case NUL:
886 case Ctrl_A:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100887 // For ^@ the trailing ESC will end the insert, unless there is an
888 // error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
890 && c != Ctrl_A && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100891 goto doESCkey; // quit insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 inserted_space = FALSE;
893 break;
894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100895 case Ctrl_R: // insert the contents of a register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ins_reg();
897 auto_format(FALSE, TRUE);
898 inserted_space = FALSE;
899 break;
900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100901 case Ctrl_G: // commands starting with CTRL-G
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ins_ctrl_g();
903 break;
904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 case Ctrl_HAT: // switch input mode and/or langmap
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000906 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 break;
908
909#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 case Ctrl__: // switch between languages
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (!p_ari)
912 goto normalchar;
913 ins_ctrl_();
914 break;
915#endif
916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100917 case Ctrl_D: // Make indent one shiftwidth smaller.
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200918#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100919 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 goto docomplete;
921#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100922 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100924 case Ctrl_T: // Make indent one shiftwidth greater.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000927 if (has_compl_option(FALSE))
928 goto docomplete;
929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 ins_shift(c, lastc);
933 auto_format(FALSE, TRUE);
934 inserted_space = FALSE;
935 break;
936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100937 case K_DEL: // delete character under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 case K_KDEL:
939 ins_del();
940 auto_format(FALSE, TRUE);
941 break;
942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 case K_BS: // delete character before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 case Ctrl_H:
945 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
946 auto_format(FALSE, TRUE);
947 break;
948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100949 case Ctrl_W: // delete word before the cursor
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200950#ifdef FEAT_JOB_CHANNEL
951 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
952 {
953 // In a prompt window CTRL-W is used for window commands.
954 // Use Shift-CTRL-W to delete a word.
955 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200956 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200957 nomove = TRUE;
958 count = 0;
959 goto doESCkey;
960 }
961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
963 auto_format(FALSE, TRUE);
964 break;
965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100966 case Ctrl_U: // delete all inserted text in current line
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000967# ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // CTRL-X CTRL-U completes with 'completefunc'.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100969 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000970 goto docomplete;
971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
973 auto_format(FALSE, TRUE);
974 inserted_space = FALSE;
975 break;
976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100977 case K_LEFTMOUSE: // mouse keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 case K_LEFTMOUSE_NM:
979 case K_LEFTDRAG:
980 case K_LEFTRELEASE:
981 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100982 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 case K_MIDDLEMOUSE:
984 case K_MIDDLEDRAG:
985 case K_MIDDLERELEASE:
986 case K_RIGHTMOUSE:
987 case K_RIGHTDRAG:
988 case K_RIGHTRELEASE:
989 case K_X1MOUSE:
990 case K_X1DRAG:
991 case K_X1RELEASE:
992 case K_X2MOUSE:
993 case K_X2DRAG:
994 case K_X2RELEASE:
995 ins_mouse(c);
996 break;
997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 case K_MOUSEDOWN: // Default action for scroll wheel up: scroll up
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200999 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 break;
1001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 case K_MOUSEUP: // Default action for scroll wheel down: scroll down
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001003 ins_mousescroll(MSCR_UP);
1004 break;
1005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001006 case K_MOUSELEFT: // Scroll wheel left
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001007 ins_mousescroll(MSCR_LEFT);
1008 break;
1009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 case K_MOUSERIGHT: // Scroll wheel right
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001011 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001013
Bram Moolenaarec2da362017-01-21 20:04:22 +01001014 case K_PS:
1015 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1016 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001017 // invoked from normal mode, bail out
Bram Moolenaarec2da362017-01-21 20:04:22 +01001018 goto doESCkey;
1019 break;
1020 case K_PE:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001021 // Got K_PE without K_PS, ignore.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001022 break;
1023
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001024#ifdef FEAT_GUI_TABLINE
1025 case K_TABLINE:
1026 case K_TABMENU:
1027 ins_tabline(c);
1028 break;
1029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001031 case K_IGNORE: // Something mapped to nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 break;
1033
Bram Moolenaar957cf672020-11-12 14:21:06 +01001034 case K_COMMAND: // <Cmd>command<CR>
1035 do_cmdline(NULL, getcmdkeycmd, NULL, 0);
1036 break;
1037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001038 case K_CURSORHOLD: // Didn't type something for a while.
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001039 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001040 did_cursorhold = TRUE;
1041 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001042
Bram Moolenaar4f974752019-02-17 17:44:42 +01001043#ifdef FEAT_GUI_MSWIN
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001044 // On MS-Windows ignore <M-F4>, we get it when closing the window
1045 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001046 case K_F4:
1047 if (mod_mask != MOD_MASK_ALT)
1048 goto normalchar;
1049 break;
1050#endif
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052#ifdef FEAT_GUI
1053 case K_VER_SCROLLBAR:
1054 ins_scroll();
1055 break;
1056
1057 case K_HOR_SCROLLBAR:
1058 ins_horscroll();
1059 break;
1060#endif
1061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001062 case K_HOME: // <Home>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 case K_S_HOME:
1065 case K_C_HOME:
1066 ins_home(c);
1067 break;
1068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001069 case K_END: // <End>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 case K_S_END:
1072 case K_C_END:
1073 ins_end(c);
1074 break;
1075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001076 case K_LEFT: // <Left>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001077 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1078 ins_s_left();
1079 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001080 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 break;
1082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001083 case K_S_LEFT: // <S-Left>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 case K_C_LEFT:
1085 ins_s_left();
1086 break;
1087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001088 case K_RIGHT: // <Right>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001089 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1090 ins_s_right();
1091 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001092 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 break;
1094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001095 case K_S_RIGHT: // <S-Right>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 case K_C_RIGHT:
1097 ins_s_right();
1098 break;
1099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001100 case K_UP: // <Up>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001101 if (pum_visible())
1102 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001103 if (mod_mask & MOD_MASK_SHIFT)
1104 ins_pageup();
1105 else
1106 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 break;
1108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001109 case K_S_UP: // <S-Up>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 case K_PAGEUP:
1111 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001112 if (pum_visible())
1113 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 ins_pageup();
1115 break;
1116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001117 case K_DOWN: // <Down>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001118 if (pum_visible())
1119 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001120 if (mod_mask & MOD_MASK_SHIFT)
1121 ins_pagedown();
1122 else
1123 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 break;
1125
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001126 case K_S_DOWN: // <S-Down>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 case K_PAGEDOWN:
1128 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001129 if (pum_visible())
1130 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 ins_pagedown();
1132 break;
1133
1134#ifdef FEAT_DND
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001135 case K_DROP: // drag-n-drop event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 ins_drop();
1137 break;
1138#endif
1139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001140 case K_S_TAB: // When not mapped, use like a normal TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 c = TAB;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001142 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001144 case TAB: // TAB or Complete patterns along path
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001145#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001146 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 goto docomplete;
1148#endif
1149 inserted_space = FALSE;
1150 if (ins_tab())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001151 goto normalchar; // insert TAB as a normal char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 auto_format(FALSE, TRUE);
1153 break;
1154
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001155 case K_KENTER: // <Enter>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 c = CAR;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001157 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 case CAR:
1159 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001160#if defined(FEAT_QUICKFIX)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001161 // In a quickfix window a <CR> jumps to the error under the
1162 // cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (bt_quickfix(curbuf) && c == CAR)
1164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001165 if (curwin->w_llist_ref == NULL) // quickfix window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001166 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001167 else // location list window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001168 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 break;
1170 }
1171#endif
1172#ifdef FEAT_CMDWIN
1173 if (cmdwin_type != 0)
1174 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001175 // Execute the command in the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 cmdwin_result = CAR;
1177 goto doESCkey;
1178 }
1179#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001180#ifdef FEAT_JOB_CHANNEL
1181 if (bt_prompt(curbuf))
1182 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001183 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001184 if (!bt_prompt(curbuf))
1185 // buffer changed to a non-prompt buffer, get out of
1186 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001187 goto doESCkey;
1188 break;
1189 }
1190#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001191 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001192 goto doESCkey; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 auto_format(FALSE, FALSE);
1194 inserted_space = FALSE;
1195 break;
1196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001197 case Ctrl_K: // digraph or keyword completion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001198 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 if (has_compl_option(TRUE))
1201 goto docomplete;
1202 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001204#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 c = ins_digraph();
1206 if (c == NUL)
1207 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001208#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001209 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001211 case Ctrl_X: // Enter CTRL-X mode
Bram Moolenaar572cb562005-08-05 21:35:02 +00001212 ins_ctrl_x();
1213 break;
1214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001215 case Ctrl_RSB: // Tag name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001216 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 goto normalchar;
1218 goto docomplete;
1219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001220 case Ctrl_F: // File name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001221 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 goto normalchar;
1223 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001224
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001225 case 's': // Spelling completion after ^X
Bram Moolenaar488c6512005-08-11 20:09:58 +00001226 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001227 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001228 goto normalchar;
1229 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001231 case Ctrl_L: // Whole line completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001232 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001234 // CTRL-L with 'insertmode' set: Leave Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (p_im)
1236 {
1237 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1238 break;
1239 goto doESCkey;
1240 }
1241 goto normalchar;
1242 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001243 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001245 case Ctrl_P: // Do previous/next pattern completion
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 case Ctrl_N:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001247 // if 'complete' is empty then plain ^P is no longer special,
1248 // but it is under other ^X modes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001250 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001251 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 goto normalchar;
1253
1254docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001255 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001256#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001257 disable_fold_update++; // don't redraw folds here
Bram Moolenaara6c07602017-03-05 21:18:27 +01001258#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001259 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001260 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001261#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001262 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001263#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001264 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001267 case Ctrl_Y: // copy from previous line or scroll down
1268 case Ctrl_E: // copy from next line or scroll up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 break;
1271
1272 default:
1273#ifdef UNIX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001274 if (c == intr_char) // special interrupt char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 goto do_intr;
1276#endif
1277
Bram Moolenaare659c952011-05-19 17:25:41 +02001278normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001280 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001282#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001283 if (!p_paste)
1284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001285 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001286 char_u *str = do_insert_char_pre(c);
1287 char_u *p;
1288
1289 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001290 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001291 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001292 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001293 // Insert the new value of v:char literally.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001294 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001295 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001296 c = PTR2CHAR(p);
1297 if (c == CAR || c == K_KENTER || c == NL)
1298 ins_eol(c);
1299 else
1300 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001301 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001302 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001303 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001304 vim_free(str);
1305 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001306 }
1307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001308 // If the new value is already inserted or an empty string
1309 // then don't insert any character.
Bram Moolenaare659c952011-05-19 17:25:41 +02001310 if (c == NUL)
1311 break;
1312 }
1313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#ifdef FEAT_SMARTINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001315 // Try to perform smart-indenting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 ins_try_si(c);
1317#endif
1318
1319 if (c == ' ')
1320 {
1321 inserted_space = TRUE;
1322#ifdef FEAT_CINDENT
1323 if (inindent(0))
1324 can_cindent = FALSE;
1325#endif
1326 if (Insstart_blank_vcol == MAXCOL
1327 && curwin->w_cursor.lnum == Insstart.lnum)
1328 Insstart_blank_vcol = get_nolist_virtcol();
1329 }
1330
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001331 // Insert a normal character and check for abbreviations on a
1332 // special character. Let CTRL-] expand abbreviations without
1333 // inserting it.
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001334 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001335 // Add ABBR_OFF for characters above 0x100, this is
1336 // what check_abbr() expects.
1337 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1338 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
1340 insert_special(c, FALSE, FALSE);
1341#ifdef FEAT_RIGHTLEFT
1342 revins_legal++;
1343 revins_chars++;
1344#endif
1345 }
1346
1347 auto_format(FALSE, TRUE);
1348
1349#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001350 // When inserting a character the cursor line must never be in a
1351 // closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 foldOpenCursor();
1353#endif
1354 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001355 } // end of switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001357 // If typed something may trigger CursorHoldI again.
Bram Moolenaar245c4102016-04-20 17:37:41 +02001358 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001359#ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001360 // but not in CTRL-X mode, a script can't restore the state
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001361 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001362#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001363 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001364 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001366 // If the cursor was moved we didn't just insert a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (arrow_used)
1368 inserted_space = FALSE;
1369
1370#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001371 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 {
1373force_cindent:
1374 /*
1375 * Indent now if a key was typed that is in 'cinkeys'.
1376 */
1377 if (in_cinkeys(c, ' ', line_is_white))
1378 {
1379 if (stop_arrow() == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001380 // re-indent the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 do_c_expr_indent();
1382 }
1383 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001384#endif // FEAT_CINDENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001386 } // for (;;)
1387 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388}
1389
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001390 int
1391ins_need_undo_get(void)
1392{
1393 return ins_need_undo;
1394}
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396/*
1397 * Redraw for Insert mode.
1398 * This is postponed until getting the next character to make '$' in the 'cpo'
1399 * option work correctly.
1400 * Only redraw when there are no characters available. This speeds up
1401 * inserting sequences of characters (e.g., for CTRL-R).
1402 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001403 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001404ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001406#ifdef FEAT_CONCEAL
1407 linenr_T conceal_old_cursor_line = 0;
1408 linenr_T conceal_new_cursor_line = 0;
1409 int conceal_update_lines = FALSE;
1410#endif
1411
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001412 if (char_avail())
1413 return;
1414
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001415 // Trigger CursorMoved if the cursor moved. Not when the popup menu is
1416 // visible, the command might delete it.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001417 if (ready && (has_cursormovedI()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001418# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001419 || popup_visible
1420# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001421# if defined(FEAT_CONCEAL)
1422 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001423# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001424 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001425 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001426 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001427 {
1428# ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001429 // Need to update the screen first, to make sure syntax
1430 // highlighting is correct after making a change (e.g., inserting
1431 // a "(". The autocommand may also require a redraw, so it's done
1432 // again below, unfortunately.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001433 if (syntax_present(curwin) && must_redraw)
1434 update_screen(0);
1435# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001436 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001437 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001438 // Make sure curswant is correct, an autocommand may call
1439 // getcurpos().
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001440 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001441 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001442 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001443#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001444 if (popup_visible)
1445 popup_check_cursor_pos();
1446#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001447# ifdef FEAT_CONCEAL
1448 if (curwin->w_p_cole > 0)
1449 {
1450 conceal_old_cursor_line = last_cursormoved.lnum;
1451 conceal_new_cursor_line = curwin->w_cursor.lnum;
1452 conceal_update_lines = TRUE;
1453 }
1454# endif
1455 last_cursormoved = curwin->w_cursor;
1456 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001458 // Trigger TextChangedI if b_changedtick differs.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001459 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001460 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001461 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001462 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001463 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001464 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001465
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001466 // save and restore curwin and curbuf, in case the autocmd changes them
1467 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001468 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001469 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001470 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001471 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1472 u_save(curwin->w_cursor.lnum,
1473 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001476 // Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1477 // TextChangedI will need to trigger for backwards compatibility, thus use
1478 // different b_last_changedtick* variables.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001479 if (ready && has_textchangedP()
1480 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1481 && pum_visible())
1482 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001483 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001484 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001485
1486 // save and restore curwin and curbuf, in case the autocmd changes them
1487 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001488 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001489 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001490 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001491 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1492 u_save(curwin->w_cursor.lnum,
1493 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001494 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001495
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001496 // Trigger SafeState if nothing is pending.
1497 may_trigger_safestate(ready
1498 && !ins_compl_active()
1499 && !pum_visible());
1500
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001501#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001502 if ((conceal_update_lines
1503 && (conceal_old_cursor_line != conceal_new_cursor_line
1504 || conceal_cursor_line(curwin)))
1505 || need_cursor_line_redraw)
1506 {
1507 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001508 redrawWinline(curwin, conceal_old_cursor_line);
1509 redrawWinline(curwin, conceal_new_cursor_line == 0
1510 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001511 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001512 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001513 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001514#endif
1515 if (must_redraw)
1516 update_screen(0);
1517 else if (clear_cmdline || redraw_cmdline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001518 showmode(); // clear cmdline and show mode
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001519 showruler(FALSE);
1520 setcursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001521 emsg_on_display = FALSE; // may remove error message now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522}
1523
1524/*
1525 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1526 */
1527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001528ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001531 int did_putchar = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001532 int prev_mod_mask = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001534 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00001535 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536
1537 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001540 did_putchar = TRUE;
1541 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001542 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544#ifdef FEAT_CMDL_INFO
1545 add_to_showcmd_c(Ctrl_V);
1546#endif
1547
1548 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001549 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001550 // when the line fits in 'columns' the '^' is at the start of the next
1551 // line and will not removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001552 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#ifdef FEAT_CMDL_INFO
1554 clear_showcmd();
1555#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001556
1557 if ((c == ESC || c == CSI) && !(prev_mod_mask & MOD_MASK_SHIFT))
1558 // Using CTRL-V: Change any modifyOtherKeys ESC sequence to a normal
1559 // key. Don't do this for CTRL-SHIFT-V.
1560 c = decodeModifyOtherKeys(c);
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 insert_special(c, FALSE, TRUE);
1563#ifdef FEAT_RIGHTLEFT
1564 revins_chars++;
1565 revins_legal++;
1566#endif
1567}
1568
1569/*
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001570 * After getting an ESC or CSI for a literal key: If the typeahead buffer
1571 * contains a modifyOtherKeys sequence then decode it and return the result.
1572 * Otherwise return "c".
1573 * Note that this doesn't wait for characters, they must be in the typeahead
1574 * buffer already.
1575 */
1576 int
1577decodeModifyOtherKeys(int c)
1578{
1579 char_u *p = typebuf.tb_buf + typebuf.tb_off;
1580 int idx;
1581 int form = 0;
1582 int argidx = 0;
1583 int arg[2] = {0, 0};
1584
1585 // Recognize:
1586 // form 0: {lead}{key};{modifier}u
1587 // form 1: {lead}27;{modifier};{key}~
1588 if ((c == CSI || (c == ESC && *p == '[')) && typebuf.tb_len >= 4)
1589 {
1590 idx = (*p == '[');
1591 if (p[idx] == '2' && p[idx + 1] == '7' && p[idx + 2] == ';')
1592 {
1593 form = 1;
1594 idx += 3;
1595 }
1596 while (idx < typebuf.tb_len && argidx < 2)
1597 {
1598 if (p[idx] == ';')
1599 ++argidx;
1600 else if (VIM_ISDIGIT(p[idx]))
1601 arg[argidx] = arg[argidx] * 10 + (p[idx] - '0');
1602 else
1603 break;
1604 ++idx;
1605 }
1606 if (idx < typebuf.tb_len
1607 && p[idx] == (form == 1 ? '~' : 'u')
1608 && argidx == 1)
1609 {
1610 // Match, consume the code.
1611 typebuf.tb_off += idx + 1;
1612 typebuf.tb_len -= idx + 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001613#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1614 if (typebuf.tb_len == 0)
1615 typebuf_was_filled = FALSE;
1616#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001617
1618 mod_mask = decode_modifiers(arg[!form]);
Bram Moolenaar975a8802020-06-06 22:36:24 +02001619 c = merge_modifyOtherKeys(arg[form], &mod_mask);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001620 }
1621 }
1622
1623 return c;
1624}
1625
1626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 * Put a character directly onto the screen. It's not stored in a buffer.
1628 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1629 */
1630static int pc_status;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001631#define PC_STATUS_UNSET 0 // pc_bytes was not set
1632#define PC_STATUS_RIGHT 1 // right halve of double-wide char
1633#define PC_STATUS_LEFT 2 // left halve of double-wide char
1634#define PC_STATUS_SET 3 // pc_bytes was filled
1635static char_u pc_bytes[MB_MAXBYTES + 1]; // saved bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636static int pc_attr;
1637static int pc_row;
1638static int pc_col;
1639
1640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001641edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643 int attr;
1644
1645 if (ScreenLines != NULL)
1646 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001647 update_topline(); // just in case w_topline isn't valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 validate_cursor();
1649 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001650 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 else
1652 attr = 0;
1653 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001654 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656#ifdef FEAT_RIGHTLEFT
1657 if (curwin->w_p_rl)
1658 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001659 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (has_mbyte)
1661 {
1662 int fix_col = mb_fix_col(pc_col, pc_row);
1663
1664 if (fix_col != pc_col)
1665 {
1666 screen_putchar(' ', pc_row, fix_col, attr);
1667 --curwin->w_wcol;
1668 pc_status = PC_STATUS_RIGHT;
1669 }
1670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
1672 else
1673#endif
1674 {
1675 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (mb_lefthalve(pc_row, pc_col))
1677 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001680 // save the character to be able to put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
1683 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1684 pc_status = PC_STATUS_SET;
1685 }
1686 screen_putchar(c, pc_row, pc_col, attr);
1687 }
1688}
1689
Bram Moolenaarf2732452018-06-03 14:47:35 +02001690/*
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001691 * Set the insert start position for when using a prompt buffer.
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001692 */
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001693 void
1694set_insstart(linenr_T lnum, int col)
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001695{
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001696 Insstart.lnum = lnum;
1697 Insstart.col = col;
1698 Insstart_orig = Insstart;
1699 Insstart_textlen = Insstart.col;
1700 Insstart_blank_vcol = MAXCOL;
1701 arrow_used = FALSE;
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001702}
1703
1704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 * Undo the previous edit_putchar().
1706 */
1707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001708edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 if (pc_status == PC_STATUS_RIGHT)
1713 ++curwin->w_wcol;
1714 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001715 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1718 }
1719}
1720
1721/*
1722 * Called when p_dollar is set: display a '$' at the end of the changed text
1723 * Only works when cursor is in the line that changes.
1724 */
1725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001726display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 colnr_T save_col;
1729
1730 if (!redrawing())
1731 return;
1732
1733 cursor_off();
1734 save_col = curwin->w_cursor.col;
1735 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 if (has_mbyte)
1737 {
1738 char_u *p;
1739
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001740 // If on the last byte of a multi-byte move to the first byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 p = ml_get_curline();
1742 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1743 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001744 curs_columns(FALSE); // recompute w_wrow and w_wcol
Bram Moolenaar02631462017-09-22 15:20:32 +02001745 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
1747 edit_putchar('$', FALSE);
1748 dollar_vcol = curwin->w_virtcol;
1749 }
1750 curwin->w_cursor.col = save_col;
1751}
1752
1753/*
1754 * Call this function before moving the cursor from the normal insert position
1755 * in insert mode.
1756 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001758undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001760 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001762 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001763 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
1765}
1766
1767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 * Truncate the space at the end of a line. This is to be used only in an
1769 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1770 * modes.
1771 */
1772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001773truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 int i;
1776
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001777 // find start of trailing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +01001778 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
1780 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001781 replace_join(0); // remove a NUL from the replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 }
1783 line[i + 1] = NUL;
1784}
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786/*
1787 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1788 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001789 * Will attempt not to go before "col" even when there is a composing
1790 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 */
1792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001793backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
1795 while ((int)curwin->w_cursor.col > col)
1796 {
1797 curwin->w_cursor.col--;
1798 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001799 replace_do_bs(col);
1800 else if (!del_char_after_col(col))
1801 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001805/*
1806 * Like del_char(), but make sure not to go before column "limit_col".
1807 * Only matters when there are composing characters.
1808 * Return TRUE when something was deleted.
1809 */
1810 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001812{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001813 if (enc_utf8 && limit_col >= 0)
1814 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001815 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001817 // Make sure the cursor is at the start of a character, but
1818 // skip forward again when going too far back because of a
1819 // composing character.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001820 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001821 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001822 {
1823 int l = utf_ptr2len(ml_get_cursor());
1824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001825 if (l == 0) // end of line
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001826 break;
1827 curwin->w_cursor.col += l;
1828 }
1829 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1830 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001831 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001832 }
1833 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001834 (void)del_char(FALSE);
1835 return TRUE;
1836}
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838/*
1839 * Next character is interpreted literally.
1840 * A one, two or three digit decimal number is interpreted as its byte value.
1841 * If one or two digits are entered, the next character is given to vungetc().
1842 * For Unicode a character > 255 may be returned.
1843 */
1844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001845get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
1847 int cc;
1848 int nc;
1849 int i;
1850 int hex = FALSE;
1851 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 if (got_int)
1855 return Ctrl_C;
1856
1857#ifdef FEAT_GUI
1858 /*
1859 * In GUI there is no point inserting the internal code for a special key.
1860 * It is more useful to insert the string "<KEY>" instead. This would
1861 * probably be useful in a text window too, but it would not be
1862 * vi-compatible (maybe there should be an option for it?) -- webb
1863 */
1864 if (gui.in_use)
1865 ++allow_keys;
1866#endif
1867#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001868 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001870 ++no_mapping; // don't map the next key hits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 cc = 0;
1872 i = 0;
1873 for (;;)
1874 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001875 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001877 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 add_to_showcmd(nc);
1879#endif
1880 if (nc == 'x' || nc == 'X')
1881 hex = TRUE;
1882 else if (nc == 'o' || nc == 'O')
1883 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 else if (nc == 'u' || nc == 'U')
1885 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 else
1887 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001888 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
1890 if (!vim_isxdigit(nc))
1891 break;
1892 cc = cc * 16 + hex2nr(nc);
1893 }
1894 else if (octal)
1895 {
1896 if (nc < '0' || nc > '7')
1897 break;
1898 cc = cc * 8 + nc - '0';
1899 }
1900 else
1901 {
1902 if (!VIM_ISDIGIT(nc))
1903 break;
1904 cc = cc * 10 + nc - '0';
1905 }
1906
1907 ++i;
1908 }
1909
Bram Moolenaar13505972019-01-24 15:04:48 +01001910 if (cc > 255 && unicode == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001911 cc = 255; // limit range to 0-255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 nc = 0;
1913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001914 if (hex) // hex: up to two chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
1916 if (i >= 2)
1917 break;
1918 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 else if (unicode) // Unicode: up to four or eight chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
1921 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1922 break;
1923 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001924 else if (i >= 3) // decimal or octal: up to three chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 break;
1926 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001927 if (i == 0) // no number entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 if (nc == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
1931 cc = '\n';
1932 nc = 0;
1933 }
1934 else
1935 {
1936 cc = nc;
1937 nc = 0;
1938 }
1939 }
1940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001941 if (cc == 0) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001943 if (enc_dbcs && (cc & 0xff) == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001944 cc = '?'; // don't accept an illegal DBCS char, the NUL in the
1945 // second byte will cause trouble!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 --no_mapping;
1948#ifdef FEAT_GUI
1949 if (gui.in_use)
1950 --allow_keys;
1951#endif
1952 if (nc)
1953 vungetc(nc);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001954 got_int = FALSE; // CTRL-C typed after CTRL-V is not an interrupt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 return cc;
1956}
1957
1958/*
1959 * Insert character, taking care of special keys and mod_mask
1960 */
1961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001962insert_special(
1963 int c,
1964 int allow_modmask,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001965 int ctrlv) // c was typed after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 char_u *p;
1968 int len;
1969
1970 /*
1971 * Special function key, translate into "<Key>". Up to the last '>' is
1972 * inserted with ins_str(), so as not to replace characters in replace
1973 * mode.
1974 * Only use mod_mask for special keys, to avoid things like <S-Space>,
1975 * unless 'allow_modmask' is TRUE.
1976 */
Bram Moolenaard0573012017-10-28 21:11:06 +02001977#ifdef MACOS_X
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001978 // Command-key never produces a normal key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 if (mod_mask & MOD_MASK_CMD)
1980 allow_modmask = TRUE;
1981#endif
1982 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
1983 {
1984 p = get_special_key_name(c, mod_mask);
1985 len = (int)STRLEN(p);
1986 c = p[len - 1];
1987 if (len > 2)
1988 {
1989 if (stop_arrow() == FAIL)
1990 return;
1991 p[len - 1] = NUL;
1992 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00001993 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 ctrlv = FALSE;
1995 }
1996 }
1997 if (stop_arrow() == OK)
1998 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
1999}
2000
2001/*
2002 * Special characters in this context are those that need processing other
2003 * than the simple insertion that can be performed here. This includes ESC
2004 * which terminates the insert, and CR/NL which need special processing to
2005 * open up a new line. This routine tries to optimize insertions performed by
2006 * the "redo", "undo" or "put" commands, so it needs to know when it should
2007 * stop and defer processing to the "normal" mechanism.
2008 * '0' and '^' are special, because they can be followed by CTRL-D.
2009 */
2010#ifdef EBCDIC
2011# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2012#else
2013# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2014#endif
2015
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002016/*
2017 * "flags": INSCHAR_FORMAT - force formatting
2018 * INSCHAR_CTRLV - char typed just after CTRL-V
2019 * INSCHAR_NO_FEX - don't use 'formatexpr'
2020 *
2021 * NOTE: passes the flags value straight through to internal_format() which,
2022 * beside INSCHAR_FORMAT (above), is also looking for these:
2023 * INSCHAR_DO_COM - format comments
2024 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002027insertchar(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002028 int c, // character to insert or NUL
2029 int flags, // INSCHAR_FORMAT, etc.
2030 int second_indent) // indent for second line if >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002035 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002037 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
2040 /*
2041 * Try to break the line in two or more pieces when:
2042 * - Always do this if we have been called to do formatting only.
2043 * - Always do this when 'formatoptions' has the 'a' flag and the line
2044 * ends in white space.
2045 * - Otherwise:
2046 * - Don't do this if inserting a blank
2047 * - Don't do this if an existing character is being replaced, unless
2048 * we're in VREPLACE mode.
2049 * - Do this if the cursor is not on the line where insert started
2050 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2051 * before the insert.
2052 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2053 * before 'textwidth'
2054 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002055 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002056 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002057 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 && *ml_get_cursor() != NUL)
2061 && (curwin->w_cursor.lnum != Insstart.lnum
2062 || ((!has_format_option(FO_INS_LONG)
2063 || Insstart_textlen <= (colnr_T)textwidth)
2064 && (!fo_ins_blank
2065 || Insstart_blank_vcol <= (colnr_T)textwidth
2066 ))))))
2067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002068 // Format with 'formatexpr' when it's set. Use internal formatting
2069 // when 'formatexpr' isn't set or it returns non-zero.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002070#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002071 int do_internal = TRUE;
2072 colnr_T virtcol = get_nolist_virtcol()
2073 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002074
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002075 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2076 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002077 {
2078 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002079 // It may be required to save for undo again, e.g. when setline()
2080 // was called.
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002081 ins_need_undo = TRUE;
2082 }
2083 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002085 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002088 if (c == NUL) // only formatting was wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 return;
2090
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002091 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 if (did_ai && (int)c == end_comment_pending)
2093 {
2094 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002095 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 int middle_len, end_len;
2097 int i;
2098
2099 /*
2100 * Need to remove existing (middle) comment leader and insert end
2101 * comment leader. First, check what comment leader we can find.
2102 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002103 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002104 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002106 // Skip middle-comment string
2107 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 ++p;
2109 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002110 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002111 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 --middle_len;
2113
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002114 // Find the end-comment string
2115 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 ++p;
2117 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2118
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002119 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002121 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 ;
2123 i++;
2124
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002125 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 i -= middle_len;
2127
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002128 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2130 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002131 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 backspace_until_column(i);
2133
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002134 // Insert the end-comment string, except for the last
2135 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 ins_bytes_len(lead_end, end_len - 1);
2137 }
2138 }
2139 }
2140 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 did_ai = FALSE;
2143#ifdef FEAT_SMARTINDENT
2144 did_si = FALSE;
2145 can_si = FALSE;
2146 can_si_back = FALSE;
2147#endif
2148
2149 /*
2150 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2151 * This speeds up normal text input considerably.
2152 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2153 * need to re-indent at a ':', or any other character (but not what
2154 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002155 * Don't do this when there an InsertCharPre autocommand is defined,
2156 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002157 * Do the check for InsertCharPre before the call to vpeekc() because the
2158 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 */
2160#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002161 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#endif
2163
2164 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002166 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 && vpeekc() != NUL
2168 && !(State & REPLACE_FLAG)
2169#ifdef FEAT_CINDENT
2170 && !cindent_on()
2171#endif
2172#ifdef FEAT_RIGHTLEFT
2173 && !p_ri
2174#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002175 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177#define INPUT_BUFLEN 100
2178 char_u buf[INPUT_BUFLEN + 1];
2179 int i;
2180 colnr_T virtcol = 0;
2181
2182 buf[0] = c;
2183 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002184 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 virtcol = get_nolist_virtcol();
2186 /*
2187 * Stop the string when:
2188 * - no more chars available
2189 * - finding a special character (command key)
2190 * - buffer is full
2191 * - running into the 'textwidth' boundary
2192 * - need to check for abbreviation: A non-word char after a word-char
2193 */
2194 while ( (c = vpeekc()) != NUL
2195 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 && i < INPUT_BUFLEN
2198 && (textwidth == 0
2199 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2200 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2201 {
2202#ifdef FEAT_RIGHTLEFT
2203 c = vgetc();
2204 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002205 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 buf[i++] = c;
2207#else
2208 buf[i++] = vgetc();
2209#endif
2210 }
2211
2212#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002213 do_digraph(-1); // clear digraphs
2214 do_digraph(buf[i-1]); // may be the start of a digraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#endif
2216 buf[i] = NUL;
2217 ins_str(buf);
2218 if (flags & INSCHAR_CTRLV)
2219 {
2220 redo_literal(*buf);
2221 i = 1;
2222 }
2223 else
2224 i = 0;
2225 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002226 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
2228 else
2229 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002230 int cc;
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2233 {
2234 char_u buf[MB_MAXBYTES + 1];
2235
2236 (*mb_char2bytes)(c, buf);
2237 buf[cc] = NUL;
2238 ins_char_bytes(buf, cc);
2239 AppendCharToRedobuff(c);
2240 }
2241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243 ins_char(c);
2244 if (flags & INSCHAR_CTRLV)
2245 redo_literal(c);
2246 else
2247 AppendCharToRedobuff(c);
2248 }
2249 }
2250}
2251
2252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 * Put a character in the redo buffer, for when just after a CTRL-V.
2254 */
2255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002256redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257{
2258 char_u buf[10];
2259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002260 // Only digits need special treatment. Translate them into a string of
2261 // three digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 if (VIM_ISDIGIT(c))
2263 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002264 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 AppendToRedobuff(buf);
2266 }
2267 else
2268 AppendCharToRedobuff(c);
2269}
2270
2271/*
2272 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002273 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002276start_arrow(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002277 pos_T *end_insert_pos) // can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002279 start_arrow_common(end_insert_pos, TRUE);
2280}
2281
2282/*
2283 * Like start_arrow() but with end_change argument.
2284 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2285 */
2286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002287start_arrow_with_change(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002288 pos_T *end_insert_pos, // can be NULL
2289 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002290{
2291 start_arrow_common(end_insert_pos, end_change);
2292 if (!end_change)
2293 {
2294 AppendCharToRedobuff(Ctrl_G);
2295 AppendCharToRedobuff('U');
2296 }
2297}
2298
2299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002300start_arrow_common(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002301 pos_T *end_insert_pos, // can be NULL
2302 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002303{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002304 if (!arrow_used && end_change) // something has been inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002307 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002308 arrow_used = TRUE; // this means we stopped the current insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002310#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002311 check_spell_redraw();
2312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313}
2314
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002315#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002316/*
2317 * If we skipped highlighting word at cursor, do it now.
2318 * It may be skipped again, thus reset spell_redraw_lnum first.
2319 */
2320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002321check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002322{
2323 if (spell_redraw_lnum != 0)
2324 {
2325 linenr_T lnum = spell_redraw_lnum;
2326
2327 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002328 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002329 }
2330}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002331
Bram Moolenaar217ad922005-03-20 22:37:15 +00002332#endif
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334/*
2335 * stop_arrow() is called before a change is made in insert mode.
2336 * If an arrow key has been used, start a new insertion.
2337 * Returns FAIL if undo is impossible, shouldn't insert then.
2338 */
2339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002340stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
2342 if (arrow_used)
2343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002344 Insstart = curwin->w_cursor; // new insertion starts here
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002345 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002346 // Don't update the original insert position when moved to the
2347 // right, except when nothing was inserted yet.
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002348 update_Insstart_orig = FALSE;
2349 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (u_save_cursor() == OK)
2352 {
2353 arrow_used = FALSE;
2354 ins_need_undo = FALSE;
2355 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (State & VREPLACE_FLAG)
2359 {
2360 orig_line_count = curbuf->b_ml.ml_line_count;
2361 vr_lines_changed = 1;
2362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 ResetRedobuff();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002364 AppendToRedobuff((char_u *)"1i"); // pretend we start an insertion
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002365 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367 else if (ins_need_undo)
2368 {
2369 if (u_save_cursor() == OK)
2370 ins_need_undo = FALSE;
2371 }
2372
2373#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002374 // Always open fold at the cursor line when inserting something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 foldOpenCursor();
2376#endif
2377
2378 return (arrow_used || ins_need_undo ? FAIL : OK);
2379}
2380
2381/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002382 * Do a few things to stop inserting.
2383 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2384 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 */
2386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002387stop_insert(
2388 pos_T *end_insert_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002389 int esc, // called by ins_esc()
2390 int nomove) // <c-\><c-o>, don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002392 int cc;
2393 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 stop_redo_ins();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002396 replace_flush(); // abandon replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002399 * Save the inserted text for later redo with ^@ and CTRL-A.
2400 * Don't do it when "restart_edit" was set and nothing was inserted,
2401 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002403 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002404 if (did_restart_edit == 0 || (ptr != NULL
2405 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002406 {
2407 vim_free(last_insert);
2408 last_insert = ptr;
2409 last_insert_skip = new_insert_skip;
2410 }
2411 else
2412 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002414 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002416 // Auto-format now. It may seem strange to do this when stopping an
2417 // insertion (or moving the cursor), but it's required when appending
2418 // a line and having it end in a space. But only do it when something
2419 // was actually inserted, otherwise undo won't work.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002420 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002422 pos_T tpos = curwin->w_cursor;
2423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002424 // When the cursor is at the end of the line after a space the
2425 // formatting will move it to the following word. Avoid that by
2426 // moving the cursor onto the space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 cc = 'x';
2428 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
2429 {
2430 dec_cursor();
2431 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002432 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002433 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435
2436 auto_format(TRUE, FALSE);
2437
Bram Moolenaar1c465442017-03-12 20:10:05 +01002438 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002439 {
2440 if (gchar_cursor() != NUL)
2441 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002442 // If the cursor is still at the same character, also keep
2443 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002444 if (gchar_cursor() == NUL
2445 && curwin->w_cursor.lnum == tpos.lnum
2446 && curwin->w_cursor.col == tpos.col)
2447 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002451 // If a space was inserted for auto-formatting, remove it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 check_auto_format(TRUE);
2453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002454 // If we just did an auto-indent, remove the white space from the end
2455 // of the line, and put the cursor back.
2456 // Do this when ESC was used or moving the cursor up/down.
2457 // Check for the old position still being valid, just in case the text
2458 // got changed unexpectedly.
Bram Moolenaarf332a652013-11-04 04:20:33 +01002459 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00002460 && curwin->w_cursor.lnum != end_insert_pos->lnum))
2461 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002463 pos_T tpos = curwin->w_cursor;
2464
2465 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002466 check_cursor_col(); // make sure it is not past the line
Bram Moolenaar39f05632006-03-19 22:15:26 +00002467 for (;;)
2468 {
2469 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
2470 --curwin->w_cursor.col;
2471 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002472 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00002473 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00002474 if (del_char(TRUE) == FAIL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002475 break; // should not happen
Bram Moolenaar39f05632006-03-19 22:15:26 +00002476 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002477 if (curwin->w_cursor.lnum != tpos.lnum)
2478 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01002479 else
2480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002481 // reset tpos, could have been invalidated in the loop above
Bram Moolenaaref6875b2014-11-12 18:59:25 +01002482 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01002483 tpos.col++;
2484 if (cc != NUL && gchar_pos(&tpos) == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002485 ++curwin->w_cursor.col; // put cursor back on the NUL
Bram Moolenaar2f31e392014-10-31 19:20:36 +01002486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002488 // <C-S-Right> may have started Visual mode, adjust the position for
2489 // deleted characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
2491 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002492 int len = (int)STRLEN(ml_get_curline());
2493
2494 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002496 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 }
2499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 }
2501 }
2502 did_ai = FALSE;
2503#ifdef FEAT_SMARTINDENT
2504 did_si = FALSE;
2505 can_si = FALSE;
2506 can_si_back = FALSE;
2507#endif
2508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002509 // Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
2510 // now in a different buffer.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002511 if (end_insert_pos != NULL)
2512 {
2513 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002514 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002515 curbuf->b_op_end = *end_insert_pos;
2516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517}
2518
2519/*
2520 * Set the last inserted text to a single character.
2521 * Used for the replace command.
2522 */
2523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002524set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
2526 char_u *s;
2527
2528 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 if (last_insert != NULL)
2531 {
2532 s = last_insert;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002533 // Use the CTRL-V only when entering a special char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (c < ' ' || c == DEL)
2535 *s++ = Ctrl_V;
2536 s = add_char2buf(c, s);
2537 *s++ = ESC;
2538 *s++ = NUL;
2539 last_insert_skip = 0;
2540 }
2541}
2542
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002543#if defined(EXITFREE) || defined(PROTO)
2544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002545free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002546{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002547 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002548}
2549#endif
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551/*
2552 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
2553 * and CSI. Handle multi-byte characters.
2554 * Returns a pointer to after the added bytes.
2555 */
2556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002557add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002559 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 int i;
2561 int len;
2562
2563 len = (*mb_char2bytes)(c, temp);
2564 for (i = 0; i < len; ++i)
2565 {
2566 c = temp[i];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002567 // Need to escape K_SPECIAL and CSI like in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (c == K_SPECIAL)
2569 {
2570 *s++ = K_SPECIAL;
2571 *s++ = KS_SPECIAL;
2572 *s++ = KE_FILLER;
2573 }
2574#ifdef FEAT_GUI
2575 else if (c == CSI)
2576 {
2577 *s++ = CSI;
2578 *s++ = KS_EXTRA;
2579 *s++ = (int)KE_CSI;
2580 }
2581#endif
2582 else
2583 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 return s;
2586}
2587
2588/*
2589 * move cursor to start of line
2590 * if flags & BL_WHITE move to first non-white
2591 * if flags & BL_SOL move to first non-white if startofline is set,
2592 * otherwise keep "curswant" column
2593 * if flags & BL_FIX don't leave the cursor on a NUL.
2594 */
2595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002596beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
2598 if ((flags & BL_SOL) && !p_sol)
2599 coladvance(curwin->w_curswant);
2600 else
2601 {
2602 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605 if (flags & (BL_WHITE | BL_SOL))
2606 {
2607 char_u *ptr;
2608
Bram Moolenaar1c465442017-03-12 20:10:05 +01002609 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
2611 ++curwin->w_cursor.col;
2612 }
2613 curwin->w_set_curswant = TRUE;
2614 }
2615}
2616
2617/*
2618 * oneright oneleft cursor_down cursor_up
2619 *
2620 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002621 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 * Return OK when successful, FAIL when we hit a line of file boundary.
2623 */
2624
2625 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002626oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (virtual_active())
2632 {
2633 pos_T prevpos = curwin->w_cursor;
2634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002635 // Adjust for multi-wide char (excluding TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01002637 coladvance(getviscol() + ((*ptr != TAB
2638 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 ? ptr2cells(ptr) : 1));
2640 curwin->w_set_curswant = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002641 // Return OK if the cursor moved, FAIL otherwise (at window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 return (prevpos.col != curwin->w_cursor.col
2643 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002647 if (*ptr == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002648 return FAIL; // already at the very end
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002649
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002650 if (has_mbyte)
2651 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002653 l = 1;
2654
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002655 // move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
2656 // contains "onemore".
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002657 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002658 return FAIL;
2659 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 curwin->w_set_curswant = TRUE;
2662 return OK;
2663}
2664
2665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002666oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (virtual_active())
2669 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002670#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 int v = getviscol();
2674
2675 if (v == 0)
2676 return FAIL;
2677
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002678#ifdef FEAT_LINEBREAK
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002679 // We might get stuck on 'showbreak', skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 width = 1;
2681 for (;;)
2682 {
2683 coladvance(v - width);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002684 // getviscol() is slow, skip it when 'showbreak' is empty,
2685 // 'breakindent' is not set and there are no multi-byte
2686 // characters
Bram Moolenaaree857022019-11-09 23:26:40 +01002687 if ((*get_showbreak_value(curwin) == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01002688 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 break;
2690 ++width;
2691 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002692#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 if (curwin->w_cursor.coladd == 1)
2697 {
2698 char_u *ptr;
2699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002700 // Adjust for multi-wide char (not a TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01002702 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
2703 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 curwin->w_cursor.coladd = 0;
2705 }
2706
2707 curwin->w_set_curswant = TRUE;
2708 return OK;
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 if (curwin->w_cursor.col == 0)
2712 return FAIL;
2713
2714 curwin->w_set_curswant = TRUE;
2715 --curwin->w_cursor.col;
2716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002717 // if the character on the left of the current cursor is a multi-byte
2718 // character, move to its first byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (has_mbyte)
2720 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 return OK;
2722}
2723
2724 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002725cursor_up(
2726 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002727 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 linenr_T lnum;
2730
2731 if (n > 0)
2732 {
2733 lnum = curwin->w_cursor.lnum;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002734 // This fails if the cursor is already in the first line or the count
2735 // is larger than the line number and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00002736 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 return FAIL;
2738 if (n >= lnum)
2739 lnum = 1;
2740 else
2741#ifdef FEAT_FOLDING
2742 if (hasAnyFolding(curwin))
2743 {
2744 /*
2745 * Count each sequence of folded lines as one logical line.
2746 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002747 // go to the start of the current fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 (void)hasFolding(lnum, &lnum, NULL);
2749
2750 while (n--)
2751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002752 // move up one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 --lnum;
2754 if (lnum <= 1)
2755 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002756 // If we entered a fold, move to the beginning, unless in
2757 // Insert mode or when 'foldopen' contains "all": it will open
2758 // in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
2760 (void)hasFolding(lnum, &lnum, NULL);
2761 }
2762 if (lnum < 1)
2763 lnum = 1;
2764 }
2765 else
2766#endif
2767 lnum -= n;
2768 curwin->w_cursor.lnum = lnum;
2769 }
2770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002771 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 coladvance(curwin->w_curswant);
2773
2774 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002775 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
2777 return OK;
2778}
2779
2780/*
2781 * Cursor down a number of logical lines.
2782 */
2783 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002784cursor_down(
2785 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002786 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788 linenr_T lnum;
2789
2790 if (n > 0)
2791 {
2792 lnum = curwin->w_cursor.lnum;
2793#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002794 // Move to last line of fold, will fail if it's the end-of-file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 (void)hasFolding(lnum, NULL, &lnum);
2796#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002797 // This fails if the cursor is already in the last line or would move
2798 // beyond the last line and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00002799 if (lnum >= curbuf->b_ml.ml_line_count
2800 || (lnum + n > curbuf->b_ml.ml_line_count
2801 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return FAIL;
2803 if (lnum + n >= curbuf->b_ml.ml_line_count)
2804 lnum = curbuf->b_ml.ml_line_count;
2805 else
2806#ifdef FEAT_FOLDING
2807 if (hasAnyFolding(curwin))
2808 {
2809 linenr_T last;
2810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002811 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 while (n--)
2813 {
2814 if (hasFolding(lnum, NULL, &last))
2815 lnum = last + 1;
2816 else
2817 ++lnum;
2818 if (lnum >= curbuf->b_ml.ml_line_count)
2819 break;
2820 }
2821 if (lnum > curbuf->b_ml.ml_line_count)
2822 lnum = curbuf->b_ml.ml_line_count;
2823 }
2824 else
2825#endif
2826 lnum += n;
2827 curwin->w_cursor.lnum = lnum;
2828 }
2829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002830 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 coladvance(curwin->w_curswant);
2832
2833 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002834 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
2836 return OK;
2837}
2838
2839/*
2840 * Stuff the last inserted text in the read buffer.
2841 * Last_insert actually is a copy of the redo buffer, so we
2842 * first have to remove the command.
2843 */
2844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845stuff_inserted(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002846 int c, // Command character to be inserted
2847 long count, // Repeat this many times
2848 int no_esc) // Don't add an ESC at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 char_u *esc_ptr;
2851 char_u *ptr;
2852 char_u *last_ptr;
2853 char_u last = NUL;
2854
2855 ptr = get_last_insert();
2856 if (ptr == NULL)
2857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002858 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 return FAIL;
2860 }
2861
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002862 // may want to stuff the command character, to start Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 if (c != NUL)
2864 stuffcharReadbuff(c);
2865 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002866 *esc_ptr = NUL; // remove the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002868 // when the last char is either "0" or "^" it will be quoted if no ESC
2869 // comes after it OR if it will inserted more than once and "ptr"
2870 // starts with ^D. -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
2872 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
2873 && (no_esc || (*ptr == Ctrl_D && count > 1)))
2874 {
2875 last = *last_ptr;
2876 *last_ptr = NUL;
2877 }
2878
2879 do
2880 {
2881 stuffReadbuff(ptr);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002882 // a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 if (last)
2884 stuffReadbuff((char_u *)(last == '0'
2885 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
2886 : IF_EB("\026^", CTRL_V_STR "^")));
2887 }
2888 while (--count > 0);
2889
2890 if (last)
2891 *last_ptr = last;
2892
2893 if (esc_ptr != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002894 *esc_ptr = ESC; // put the ESC back
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002896 // may want to stuff a trailing ESC, to get out of Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 if (!no_esc)
2898 stuffcharReadbuff(ESC);
2899
2900 return OK;
2901}
2902
2903 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002904get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 if (last_insert == NULL)
2907 return NULL;
2908 return last_insert + last_insert_skip;
2909}
2910
2911/*
2912 * Get last inserted string, and remove trailing <Esc>.
2913 * Returns pointer to allocated memory (must be freed) or NULL.
2914 */
2915 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002916get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 char_u *s;
2919 int len;
2920
2921 if (last_insert == NULL)
2922 return NULL;
2923 s = vim_strsave(last_insert + last_insert_skip);
2924 if (s != NULL)
2925 {
2926 len = (int)STRLEN(s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002927 if (len > 0 && s[len - 1] == ESC) // remove trailing ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 s[len - 1] = NUL;
2929 }
2930 return s;
2931}
2932
2933/*
2934 * Check the word in front of the cursor for an abbreviation.
2935 * Called when the non-id character "c" has been entered.
2936 * When an abbreviation is recognized it is removed from the text and
2937 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
2938 */
2939 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002940echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002942 // Don't check for abbreviation in paste mode, when disabled and just
2943 // after moving around with cursor keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 if (p_paste || no_abbr || arrow_used)
2945 return FALSE;
2946
2947 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
2948 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
2949}
2950
2951/*
2952 * replace-stack functions
2953 *
2954 * When replacing characters, the replaced characters are remembered for each
2955 * new character. This is used to re-insert the old text when backspacing.
2956 *
2957 * There is a NUL headed list of characters for each character that is
2958 * currently in the file after the insertion point. When BS is used, one NUL
2959 * headed list is put back for the deleted character.
2960 *
2961 * For a newline, there are two NUL headed lists. One contains the characters
2962 * that the NL replaced. The extra one stores the characters after the cursor
2963 * that were deleted (always white space).
2964 *
2965 * Replace_offset is normally 0, in which case replace_push will add a new
2966 * character at the end of the stack. If replace_offset is not 0, that many
2967 * characters will be left on the stack above the newly inserted character.
2968 */
2969
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002970static char_u *replace_stack = NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002971static long replace_stack_nr = 0; // next entry in replace stack
2972static long replace_stack_len = 0; // max. number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002975replace_push(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002976 int c) // character that is replaced (NUL is none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
2978 char_u *p;
2979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002980 if (replace_stack_nr < replace_offset) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 return;
2982 if (replace_stack_len <= replace_stack_nr)
2983 {
2984 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002985 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002986 if (p == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 replace_stack_len -= 50;
2989 return;
2990 }
2991 if (replace_stack != NULL)
2992 {
2993 mch_memmove(p, replace_stack,
2994 (size_t)(replace_stack_nr * sizeof(char_u)));
2995 vim_free(replace_stack);
2996 }
2997 replace_stack = p;
2998 }
2999 p = replace_stack + replace_stack_nr - replace_offset;
3000 if (replace_offset)
3001 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3002 *p = c;
3003 ++replace_stack_nr;
3004}
3005
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003006/*
3007 * Push a character onto the replace stack. Handles a multi-byte character in
3008 * reverse byte order, so that the first byte is popped off first.
3009 * Return the number of bytes done (includes composing characters).
3010 */
3011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003012replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003013{
3014 int l = (*mb_ptr2len)(p);
3015 int j;
3016
3017 for (j = l - 1; j >= 0; --j)
3018 replace_push(p[j]);
3019 return l;
3020}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022/*
3023 * Pop one item from the replace stack.
3024 * return -1 if stack empty
3025 * return replaced character or NUL otherwise
3026 */
3027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003028replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 if (replace_stack_nr == 0)
3031 return -1;
3032 return (int)replace_stack[--replace_stack_nr];
3033}
3034
3035/*
3036 * Join the top two items on the replace stack. This removes to "off"'th NUL
3037 * encountered.
3038 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003040replace_join(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003041 int off) // offset for which NUL to remove
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
3043 int i;
3044
3045 for (i = replace_stack_nr; --i >= 0; )
3046 if (replace_stack[i] == NUL && off-- <= 0)
3047 {
3048 --replace_stack_nr;
3049 mch_memmove(replace_stack + i, replace_stack + i + 1,
3050 (size_t)(replace_stack_nr - i));
3051 return;
3052 }
3053}
3054
3055/*
3056 * Pop bytes from the replace stack until a NUL is found, and insert them
3057 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3058 */
3059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003060replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 int cc;
3063 int oldState = State;
3064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003065 State = NORMAL; // don't want REPLACE here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 while ((cc = replace_pop()) > 0)
3067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 dec_cursor();
3070 }
3071 State = oldState;
3072}
3073
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074/*
3075 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3076 * indicates a multi-byte char, pop the other bytes too.
3077 */
3078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003082 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 int i;
3084 int c;
3085
3086 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3087 {
3088 buf[0] = cc;
3089 for (i = 1; i < n; ++i)
3090 buf[i] = replace_pop();
3091 ins_bytes_len(buf, n);
3092 }
3093 else
3094 ins_char(cc);
3095
3096 if (enc_utf8)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003097 // Handle composing chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 for (;;)
3099 {
3100 c = replace_pop();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003101 if (c == -1) // stack empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 break;
3103 if ((n = MB_BYTE2LEN(c)) == 1)
3104 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003105 // Not a multi-byte char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 replace_push(c);
3107 break;
3108 }
3109 else
3110 {
3111 buf[0] = c;
3112 for (i = 1; i < n; ++i)
3113 buf[i] = replace_pop();
3114 if (utf_iscomposing(utf_ptr2char(buf)))
3115 ins_bytes_len(buf, n);
3116 else
3117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003118 // Not a composing char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 for (i = n - 1; i >= 0; --i)
3120 replace_push(buf[i]);
3121 break;
3122 }
3123 }
3124 }
3125}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127/*
3128 * make the replace stack empty
3129 * (called when exiting replace mode)
3130 */
3131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003132replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003134 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 replace_stack_len = 0;
3136 replace_stack_nr = 0;
3137}
3138
3139/*
3140 * Handle doing a BS for one character.
3141 * cc < 0: replace stack empty, just move cursor
3142 * cc == 0: character was inserted, delete it
3143 * cc > 0: character was replaced, put cc (first byte of original char) back
3144 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003145 * When "limit_col" is >= 0, don't delete before this column. Matters when
3146 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 */
3148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003149replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
3151 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 int orig_len = 0;
3153 int ins_len;
3154 int orig_vcols = 0;
3155 colnr_T start_vcol;
3156 char_u *p;
3157 int i;
3158 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
3160 cc = replace_pop();
3161 if (cc > 0)
3162 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003163#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003164 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003165
3166 if (curbuf->b_has_textprop)
3167 {
3168 // Do not adjust text properties for individual delete and insert
3169 // operations, do it afterwards on the resulting text.
3170 len_before = STRLEN(ml_get_curline());
3171 ++text_prop_frozen;
3172 }
3173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (State & VREPLACE_FLAG)
3175 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003176 // Get the number of screen cells used by the character we are
3177 // going to delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3179 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (has_mbyte)
3182 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003183 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003185 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 replace_push(cc);
3187 }
3188 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
3190 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003192 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
3194 replace_pop_ins();
3195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 if (State & VREPLACE_FLAG)
3197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003198 // Get the number of screen cells used by the inserted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003200 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 vcol = start_vcol;
3202 for (i = 0; i < ins_len; ++i)
3203 {
3204 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003205 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 }
3207 vcol -= start_vcol;
3208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003209 // Delete spaces that were inserted after the cursor to keep the
3210 // text aligned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 curwin->w_cursor.col += ins_len;
3212 while (vcol > orig_vcols && gchar_cursor() == ' ')
3213 {
3214 del_char(FALSE);
3215 ++orig_vcols;
3216 }
3217 curwin->w_cursor.col -= ins_len;
3218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
Bram Moolenaar196d1572019-01-02 23:47:18 +01003220 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003222
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003223#ifdef FEAT_PROP_POPUP
Bram Moolenaar196d1572019-01-02 23:47:18 +01003224 if (curbuf->b_has_textprop)
3225 {
3226 size_t len_now = STRLEN(ml_get_curline());
3227
3228 --text_prop_frozen;
3229 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003230 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003231 }
3232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003235 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236}
3237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3239/*
3240 * Map Hebrew keyboard when in hkmap mode.
3241 */
3242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003245 if (p_hkmapp) // phonetic mapping, by Ilya Dogolazky
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3248 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3249 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3250 static char_u map[26] =
3251 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3252 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3253 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3254 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3255 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3256 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3257 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3258 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3259 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3260
3261 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3262 return (int)(map[CharOrd(c)] - 1 + p_aleph);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003263 // '-1'='sofit'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 else if (c == 'x')
3265 return 'X';
3266 else if (c == 'q')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003267 return '\''; // {geresh}={'}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 else if (c == 246)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003269 return ' '; // \"o --> ' ' for a german keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 else if (c == 228)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003271 return ' '; // \"a --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 else if (c == 252)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003273 return ' '; // \"u --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274#ifdef EBCDIC
3275 else if (islower(c))
3276#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003277 // NOTE: islower() does not do the right thing for us on Linux so we
3278 // do this the same was as 5.7 and previous, so it works correctly on
3279 // all systems. Specifically, the e.g. Delete and Arrow keys are
3280 // munged and won't work if e.g. searching for Hebrew text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 else if (c >= 'a' && c <= 'z')
3282#endif
3283 return (int)(map[CharOrdLow(c)] + p_aleph);
3284 else
3285 return c;
3286 }
3287 else
3288 {
3289 switch (c)
3290 {
3291 case '`': return ';';
3292 case '/': return '.';
3293 case '\'': return ',';
3294 case 'q': return '/';
3295 case 'w': return '\'';
3296
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003297 // Hebrew letters - set offset from 'a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 case ',': c = '{'; break;
3299 case '.': c = 'v'; break;
3300 case ';': c = 't'; break;
3301 default: {
3302 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3303
3304#ifdef EBCDIC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003305 // see note about islower() above
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (!islower(c))
3307#else
3308 if (c < 'a' || c > 'z')
3309#endif
3310 return c;
3311 c = str[CharOrdLow(c)];
3312 break;
3313 }
3314 }
3315
3316 return (int)(CharOrdLow(c) + p_aleph);
3317 }
3318}
3319#endif
3320
3321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003322ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 int need_redraw = FALSE;
3325 int regname;
3326 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003327 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329 /*
3330 * If we are going to wait for a character, show a '"'.
3331 */
3332 pc_status = PC_STATUS_UNSET;
3333 if (redrawing() && !char_avail())
3334 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003335 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00003336 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338 edit_putchar('"', TRUE);
3339#ifdef FEAT_CMDL_INFO
3340 add_to_showcmd_c(Ctrl_R);
3341#endif
3342 }
3343
3344#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003345 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#endif
3347
3348 /*
3349 * Don't map the register name. This also prevents the mode message to be
3350 * deleted when ESC is hit.
3351 */
3352 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003353 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003354 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3357 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003358 // Get a third key for literal register insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 literally = regname;
3360#ifdef FEAT_CMDL_INFO
3361 add_to_showcmd_c(literally);
3362#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003363 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003367 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369#ifdef FEAT_EVAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003370 // Don't call u_sync() while typing the expression or giving an error
3371 // message for it. Only call it explicitly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 ++no_u_sync;
3373 if (regname == '=')
3374 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003375 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003376# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003378# endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003379 // Sync undo when evaluating the expression calls setline() or
3380 // append(), so that it can be undone separately.
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003381 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003382
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003384
3385 // Cursor may be moved back a column.
3386 curwin->w_cursor = curpos;
3387 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003388# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003389 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (im_on)
3391 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00003394 if (regname == NUL || !valid_yank_reg(regname, FALSE))
3395 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003396 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003397 need_redraw = TRUE; // remove the '"'
Bram Moolenaar677ee682005-01-27 14:41:15 +00003398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 else
3400 {
3401#endif
3402 if (literally == Ctrl_O || literally == Ctrl_P)
3403 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003404 // Append the command to the redo buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 AppendCharToRedobuff(Ctrl_R);
3406 AppendCharToRedobuff(literally);
3407 AppendCharToRedobuff(regname);
3408
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003409 do_put(regname, NULL, BACKWARD, 1L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
3411 }
3412 else if (insert_reg(regname, literally) == FAIL)
3413 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003414 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003415 need_redraw = TRUE; // remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003417 else if (stop_insert_mode)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003418 // When the '=' register was used and a function was invoked that
3419 // did ":stopinsert" then stuff_empty() returns FALSE but we won't
3420 // insert anything, need to remove the '"'
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003421 need_redraw = TRUE;
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#ifdef FEAT_EVAL
3424 }
3425 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003426 if (u_sync_once == 1)
3427 ins_need_undo = TRUE;
3428 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#endif
3430#ifdef FEAT_CMDL_INFO
3431 clear_showcmd();
3432#endif
3433
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003434 // If the inserted register is empty, we need to remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (need_redraw || stuff_empty())
3436 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003438 // Disallow starting Visual mode here, would get a weird mode.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003439 if (!vis_active && VIsual_active)
3440 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441}
3442
3443/*
3444 * CTRL-G commands in Insert mode.
3445 */
3446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003447ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 int c;
3450
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003451 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 /*
3455 * Don't map the second key. This also prevents the mode message to be
3456 * deleted when ESC is hit.
3457 */
3458 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003459 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003460 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003462 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 switch (c)
3464 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003465 // CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 case K_UP:
3467 case Ctrl_K:
3468 case 'k': ins_up(TRUE);
3469 break;
3470
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003471 // CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 case K_DOWN:
3473 case Ctrl_J:
3474 case 'j': ins_down(TRUE);
3475 break;
3476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003477 // CTRL-G u: start new undoable edit
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003478 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003481 // Need to reset Insstart, esp. because a BS that joins
3482 // a line to the previous one must save for undo.
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003483 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003484 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 break;
3486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003487 // CTRL-G U: do not break undo with the next char
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003488 case 'U':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003489 // Allow one left/right cursor movement with the next char,
3490 // without breaking undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003491 dont_sync_undo = MAYBE;
3492 break;
3493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003494 // Unknown CTRL-G command, reserved for future expansion.
Bram Moolenaar165bc692015-07-21 17:53:25 +02003495 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497}
3498
3499/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003500 * CTRL-^ in Insert mode.
3501 */
3502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003503ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003505 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003506 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003507 // ":lmap" mappings exists, Toggle use of ":lmap" mappings.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003508 if (State & LANGMAP)
3509 {
3510 curbuf->b_p_iminsert = B_IMODE_NONE;
3511 State &= ~LANGMAP;
3512 }
3513 else
3514 {
3515 curbuf->b_p_iminsert = B_IMODE_LMAP;
3516 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003517#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003518 im_set_active(FALSE);
3519#endif
3520 }
3521 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003522#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003523 else
3524 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003525 // There are no ":lmap" mappings, toggle IM
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003526 if (im_get_status())
3527 {
3528 curbuf->b_p_iminsert = B_IMODE_NONE;
3529 im_set_active(FALSE);
3530 }
3531 else
3532 {
3533 curbuf->b_p_iminsert = B_IMODE_IM;
3534 State &= ~LANGMAP;
3535 im_set_active(TRUE);
3536 }
3537 }
3538#endif
3539 set_iminsert_global();
3540 showmode();
3541#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003542 // may show different cursor shape or color
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543 if (gui.in_use)
3544 gui_update_cursor(TRUE, FALSE);
3545#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003546#if defined(FEAT_KEYMAP)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003547 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003548 status_redraw_curbuf();
3549#endif
3550}
3551
3552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 * Handle ESC in insert mode.
3554 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
3555 * insert.
3556 */
3557 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003558ins_esc(
3559 long *count,
3560 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003561 int nomove) // don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
3563 int temp;
3564 static int disabled_redraw = FALSE;
3565
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003566#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003567 check_spell_redraw();
3568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
3570 temp = curwin->w_cursor.col;
3571 if (disabled_redraw)
3572 {
3573 --RedrawingDisabled;
3574 disabled_redraw = FALSE;
3575 }
3576 if (!arrow_used)
3577 {
3578 /*
3579 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00003580 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
3581 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 */
3583 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00003584 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 /*
3587 * Repeating insert may take a long time. Check for
3588 * interrupt now and then.
3589 */
3590 if (*count > 0)
3591 {
3592 line_breakcheck();
3593 if (got_int)
3594 *count = 0;
3595 }
3596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003597 if (--*count > 0) // repeat what was typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003599 // Vi repeats the insert without replacing characters.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003600 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
3601 State &= ~REPLACE_FLAG;
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 (void)start_redo_ins();
3604 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003605 stuffRedoReadbuff(ESC_STR); // no ESC in redo buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 ++RedrawingDisabled;
3607 disabled_redraw = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003608 return FALSE; // repeat the insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01003610 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 undisplay_dollar();
3612 }
3613
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02003614 if (cmdchar != 'r' && cmdchar != 'v')
3615 ins_apply_autocmds(EVENT_INSERTLEAVEPRE);
3616
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003617 // When an autoindent was removed, curswant stays after the
3618 // indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
3620 curwin->w_set_curswant = TRUE;
3621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003622 // Remember the last Insert position in the '^ mark.
Bram Moolenaare1004402020-10-24 20:49:43 +02003623 if ((cmdmod.cmod_flags & CMOD_KEEPJUMPS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 curbuf->b_last_insert = curwin->w_cursor;
3625
3626 /*
3627 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00003628 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00003630 if (!nomove
3631 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003632 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003633 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003634 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635#ifdef FEAT_RIGHTLEFT
3636 && !revins_on
3637#endif
3638 )
3639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
3641 {
3642 oneleft();
3643 if (restart_edit != NUL)
3644 ++curwin->w_cursor.coladd;
3645 }
3646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
3648 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003649 // Correct cursor for multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (has_mbyte)
3651 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653 }
3654
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003655#ifdef HAVE_INPUT_METHOD
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003656 // Disable IM to allow typing English directly for Normal mode commands.
3657 // When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
3658 // well).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (!(State & LANGMAP))
3660 im_save_status(&curbuf->b_p_iminsert);
3661 im_set_active(FALSE);
3662#endif
3663
3664 State = NORMAL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003665 // need to position cursor again (e.g. when on a TAB )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 changed_cline_bef_curs();
3667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669#ifdef CURSOR_SHAPE
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003670 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01003672 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003673 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003674#ifdef FEAT_JOB_CHANNEL
3675 ch_log_output = TRUE;
3676#endif
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003677 // Re-enable bracketed paste mode.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01003678 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003680 // Re-enable modifyOtherKeys.
3681 out_str(T_CTI);
3682 }
3683
Bram Moolenaarad3ec762019-04-21 00:00:13 +02003684 // When recording or for CTRL-O, need to display the new mode.
3685 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02003686 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02003688 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003689 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003691 return TRUE; // exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692}
3693
3694#ifdef FEAT_RIGHTLEFT
3695/*
3696 * Toggle language: hkmap and revins_on.
3697 * Move to end of reverse inserted text.
3698 */
3699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003700ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
3702 if (revins_on && revins_chars && revins_scol >= 0)
3703 {
3704 while (gchar_cursor() != NUL && revins_chars--)
3705 ++curwin->w_cursor.col;
3706 }
3707 p_ri = !p_ri;
3708 revins_on = (State == INSERT && p_ri);
3709 if (revins_on)
3710 {
3711 revins_scol = curwin->w_cursor.col;
3712 revins_legal++;
3713 revins_chars = 0;
3714 undisplay_dollar();
3715 }
3716 else
3717 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003718 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 showmode();
3720}
3721#endif
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723/*
3724 * If 'keymodel' contains "startsel", may start selection.
3725 * Returns TRUE when a CTRL-O and other keys stuffed.
3726 */
3727 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003728ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
3730 if (km_startsel)
3731 switch (c)
3732 {
3733 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 case K_PAGEUP:
3736 case K_KPAGEUP:
3737 case K_PAGEDOWN:
3738 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02003739# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 case K_LEFT:
3741 case K_RIGHT:
3742 case K_UP:
3743 case K_DOWN:
3744 case K_END:
3745 case K_HOME:
3746# endif
3747 if (!(mod_mask & MOD_MASK_SHIFT))
3748 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003749 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 case K_S_LEFT:
3751 case K_S_RIGHT:
3752 case K_S_UP:
3753 case K_S_DOWN:
3754 case K_S_END:
3755 case K_S_HOME:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003756 // Start selection right away, the cursor can move with
3757 // CTRL-O when beyond the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 start_selection();
3759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003760 // Execute the key in (insert) Select mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 stuffcharReadbuff(Ctrl_O);
3762 if (mod_mask)
3763 {
3764 char_u buf[4];
3765
3766 buf[0] = K_SPECIAL;
3767 buf[1] = KS_MODIFIER;
3768 buf[2] = mod_mask;
3769 buf[3] = NUL;
3770 stuffReadbuff(buf);
3771 }
3772 stuffcharReadbuff(c);
3773 return TRUE;
3774 }
3775 return FALSE;
3776}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
3778/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003779 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003780 */
3781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003783{
Bram Moolenaar14184a32019-02-16 15:10:30 +01003784#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003785 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02003786 (char_u *)((State & REPLACE_FLAG) ? "i"
3787 : replaceState == VREPLACE ? "v"
3788 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01003789#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02003790 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 if (State & REPLACE_FLAG)
3792 State = INSERT | (State & LANGMAP);
3793 else
3794 State = replaceState | (State & LANGMAP);
3795 AppendCharToRedobuff(K_INS);
3796 showmode();
3797#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003798 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003799#endif
3800}
3801
3802/*
3803 * Pressed CTRL-O in Insert mode.
3804 */
3805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003806ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808 if (State & VREPLACE_FLAG)
3809 restart_edit = 'V';
3810 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003811 if (State & REPLACE_FLAG)
3812 restart_edit = 'R';
3813 else
3814 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 if (virtual_active())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003816 ins_at_eol = FALSE; // cursor always keeps its column
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003818 ins_at_eol = (gchar_cursor() == NUL);
3819}
3820
3821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 * If the cursor is on an indent, ^T/^D insert/delete one
3823 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00003824 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 * with vi. But vi only supports ^T and ^D after an
3826 * autoindent, we support it everywhere.
3827 */
3828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831 if (stop_arrow() == FAIL)
3832 return;
3833 AppendCharToRedobuff(c);
3834
3835 /*
3836 * 0^D and ^^D: remove all indent.
3837 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00003838 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
3839 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 {
3841 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003842 (void)del_char(FALSE); // delete the '^' or '0'
3843 // In Replace mode, restore the characters that '^' or '0' replaced.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (State & REPLACE_FLAG)
3845 replace_pop_ins();
3846 if (lastc == '^')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003847 old_indent = get_indent(); // remember curr. indent
Bram Moolenaar21b17e72008-01-16 19:03:13 +00003848 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
3850 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00003851 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
3853 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
3854 did_ai = FALSE;
3855#ifdef FEAT_SMARTINDENT
3856 did_si = FALSE;
3857 can_si = FALSE;
3858 can_si_back = FALSE;
3859#endif
3860#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003861 can_cindent = FALSE; // no cindenting after ^D or ^T
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862#endif
3863}
3864
3865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003866ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
3868 int temp;
3869
3870 if (stop_arrow() == FAIL)
3871 return;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003872 if (gchar_cursor() == NUL) // delete newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
3874 temp = curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003875 if (!can_bs(BS_EOL) // only if "eol" included
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02003876 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003877 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01003879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 curwin->w_cursor.col = temp;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 // Adjust orig_line_count in case more lines have been deleted than
3882 // have been added. That makes sure, that open_line() later
3883 // can access all buffer lines correctly
Bram Moolenaar63e82db2018-03-06 12:10:48 +01003884 if (State & VREPLACE_FLAG &&
3885 orig_line_count > curbuf->b_ml.ml_line_count)
3886 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01003887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003889 else if (del_char(FALSE) == FAIL) // delete char under cursor
Bram Moolenaar165bc692015-07-21 17:53:25 +02003890 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 did_ai = FALSE;
3892#ifdef FEAT_SMARTINDENT
3893 did_si = FALSE;
3894 can_si = FALSE;
3895 can_si_back = FALSE;
3896#endif
3897 AppendCharToRedobuff(K_DEL);
3898}
3899
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003900/*
3901 * Delete one character for ins_bs().
3902 */
3903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003904ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003905{
3906 dec_cursor();
3907 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
3908 if (State & REPLACE_FLAG)
3909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003910 // Don't delete characters before the insert point when in
3911 // Replace mode
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003912 if (curwin->w_cursor.lnum != Insstart.lnum
3913 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003914 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003915 }
3916 else
3917 (void)del_char(FALSE);
3918}
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920/*
3921 * Handle Backspace, delete-word and delete-line in Insert mode.
3922 * Return TRUE when backspace was actually used.
3923 */
3924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003925ins_bs(
3926 int c,
3927 int mode,
3928 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930 linenr_T lnum;
3931 int cc;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003932 int temp = 0; // init for GCC
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003933 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 colnr_T mincol;
3935 int did_backspace = FALSE;
3936 int in_indent;
3937 int oldState;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003938 int cpc[MAX_MCO]; // composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
3940 /*
3941 * can't delete anything in an empty file
3942 * can't backup past first character in buffer
3943 * can't backup past starting point unless 'backspace' > 1
3944 * can backup to a previous line if 'backspace' == 0
3945 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003946 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 || (
3948#ifdef FEAT_RIGHTLEFT
3949 !revins_on &&
3950#endif
3951 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
3952 || (!can_bs(BS_START)
Bram Moolenaar6f624482020-11-11 20:52:40 +01003953 && ((arrow_used
3954#ifdef FEAT_JOB_CHANNEL
3955 && !bt_prompt(curbuf)
3956#endif
3957 ) || (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02003958 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
3960 && curwin->w_cursor.col <= ai_col)
3961 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
3962 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003963 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 return FALSE;
3965 }
3966
3967 if (stop_arrow() == FAIL)
3968 return FALSE;
3969 in_indent = inindent(0);
3970#ifdef FEAT_CINDENT
3971 if (in_indent)
3972 can_cindent = FALSE;
3973#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003974 end_comment_pending = NUL; // After BS, don't auto-end comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003976 if (revins_on) // put cursor after last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 inc_cursor();
3978#endif
3979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003980 // Virtualedit:
3981 // BACKSPACE_CHAR eats a virtual space
3982 // BACKSPACE_WORD eats all coladd
3983 // BACKSPACE_LINE eats all coladd and keeps going
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 if (curwin->w_cursor.coladd > 0)
3985 {
3986 if (mode == BACKSPACE_CHAR)
3987 {
3988 --curwin->w_cursor.coladd;
3989 return TRUE;
3990 }
3991 if (mode == BACKSPACE_WORD)
3992 {
3993 curwin->w_cursor.coladd = 0;
3994 return TRUE;
3995 }
3996 curwin->w_cursor.coladd = 0;
3997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004000 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 */
4002 if (curwin->w_cursor.col == 0)
4003 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004004 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004005 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#ifdef FEAT_RIGHTLEFT
4007 || revins_on
4008#endif
4009 )
4010 {
4011 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4012 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4013 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004014 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004015 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 /*
4018 * In replace mode:
4019 * cc < 0: NL was inserted, delete it
4020 * cc >= 0: NL was replaced, put original characters back
4021 */
4022 cc = -1;
4023 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004024 cc = replace_pop(); // returns -1 if NL was inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 /*
4026 * In replace mode, in the line we started replacing, we only move the
4027 * cursor.
4028 */
4029 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4030 {
4031 dec_cursor();
4032 }
4033 else
4034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (!(State & VREPLACE_FLAG)
4036 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004038 temp = gchar_cursor(); // remember current char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004041 // When "aw" is in 'formatoptions' we must delete the space at
4042 // the end of the line, otherwise the line will be broken
4043 // again when auto-formatting.
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004044 if (has_format_option(FO_AUTO)
4045 && has_format_option(FO_WHITE_PAR))
4046 {
4047 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4048 TRUE);
4049 int len;
4050
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004051 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004052 if (len > 0 && ptr[len - 1] == ' ')
4053 ptr[len - 1] = NUL;
4054 }
4055
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004056 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (temp == NUL && gchar_cursor() != NUL)
4058 inc_cursor();
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 else
4061 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 /*
4064 * In REPLACE mode we have to put back the text that was replaced
4065 * by the NL. On the replace stack is first a NUL-terminated
4066 * sequence of characters that were deleted and then the
4067 * characters that NL replaced.
4068 */
4069 if (State & REPLACE_FLAG)
4070 {
4071 /*
4072 * Do the next ins_char() in NORMAL state, to
4073 * prevent ins_char() from replacing characters and
4074 * avoiding showmatch().
4075 */
4076 oldState = State;
4077 State = NORMAL;
4078 /*
4079 * restore characters (blanks) deleted after cursor
4080 */
4081 while (cc > 0)
4082 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004083 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004085 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 cc = replace_pop();
4087 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004088 // restore the characters that NL replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 replace_pop_ins();
4090 State = oldState;
4091 }
4092 }
4093 did_ai = FALSE;
4094 }
4095 else
4096 {
4097 /*
4098 * Delete character(s) before the cursor.
4099 */
4100#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 if (revins_on) // put cursor on last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 dec_cursor();
4103#endif
4104 mincol = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004105 // keep indent
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004106 if (mode == BACKSPACE_LINE
4107 && (curbuf->b_p_ai
4108#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004109 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004110#endif
4111 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112#ifdef FEAT_RIGHTLEFT
4113 && !revins_on
4114#endif
4115 )
4116 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004117 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004119 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004121 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123
4124 /*
4125 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4126 */
4127 if ( mode == BACKSPACE_CHAR
4128 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004129 || ((get_sts_value() != 0
4130#ifdef FEAT_VARTABS
4131 || tabstop_count(curbuf->b_p_vsts_array)
4132#endif
4133 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004134 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 && (*(ml_get_cursor() - 1) == TAB
4136 || (*(ml_get_cursor() - 1) == ' '
4137 && (!*inserted_space_p
4138 || arrow_used))))))
4139 {
4140 int ts;
4141 colnr_T vcol;
4142 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004143 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 *inserted_space_p = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004146 // Compute the virtual column where we want to be. Since
4147 // 'showbreak' may get in the way, need to get the last column of
4148 // the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004150 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 dec_cursor();
4152 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4153 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004154#ifdef FEAT_VARTABS
4155 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004156 {
4157 ts = (int)get_sw_value(curbuf);
4158 want_vcol = (want_vcol / ts) * ts;
4159 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004160 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004161 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004162 curbuf->b_p_vsts_array);
4163#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004164 if (p_sta && in_indent)
4165 ts = (int)get_sw_value(curbuf);
4166 else
4167 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004171 // delete characters until we are at or before want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004173 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004174 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004176 // insert extra spaces until we are at want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 while (vcol < want_vcol)
4178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004179 // Remember the first char we inserted
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004180 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4181 && curwin->w_cursor.col < Insstart_orig.col)
4182 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 if (State & VREPLACE_FLAG)
4185 ins_char(' ');
4186 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
4188 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004189 if ((State & REPLACE_FLAG))
4190 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4193 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004195 // If we are now back where we started delete one character. Can
4196 // happen when using 'sts' and 'linebreak'.
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004197 if (vcol >= start_vcol)
4198 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200
4201 /*
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004202 * Delete up to starting point, start of line or previous word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004204 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004206 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004208 if (has_mbyte)
4209 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004210 do
4211 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004213 if (!revins_on) // put cursor on char to be deleted
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004216
4217 cc = gchar_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004218 // look multi-byte character class
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004219 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004221 prev_cclass = cclass;
4222 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004224
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004225 // start of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004226 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4227 {
4228 mode = BACKSPACE_WORD_NOT_SPACE;
4229 temp = vim_iswordc(cc);
4230 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004231 // end of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004232 else if (mode == BACKSPACE_WORD_NOT_SPACE
4233 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004234 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004237 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004239 inc_cursor();
4240#ifdef FEAT_RIGHTLEFT
4241 else if (State & REPLACE_FLAG)
4242 dec_cursor();
4243#endif
4244 break;
4245 }
4246 if (State & REPLACE_FLAG)
4247 replace_do_bs(-1);
4248 else
4249 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004250 if (enc_utf8 && p_deco)
4251 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004252 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004253 /*
4254 * If there are combining characters and 'delcombine' is set
4255 * move the cursor back. Don't back up before the base
4256 * character.
4257 */
4258 if (enc_utf8 && p_deco && cpc[0] != NUL)
4259 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004260#ifdef FEAT_RIGHTLEFT
4261 if (revins_chars)
4262 {
4263 revins_chars--;
4264 revins_legal++;
4265 }
4266 if (revins_on && gchar_cursor() == NUL)
4267 break;
4268#endif
4269 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004270 // Just a single backspace?:
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004271 if (mode == BACKSPACE_CHAR)
4272 break;
4273 } while (
4274#ifdef FEAT_RIGHTLEFT
4275 revins_on ||
4276#endif
4277 (curwin->w_cursor.col > mincol
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02004278 && (can_bs(BS_NOSTOP)
4279 || (curwin->w_cursor.lnum != Insstart_orig.lnum
4280 || curwin->w_cursor.col != Insstart_orig.col)
4281 )));
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 did_backspace = TRUE;
4284 }
4285#ifdef FEAT_SMARTINDENT
4286 did_si = FALSE;
4287 can_si = FALSE;
4288 can_si_back = FALSE;
4289#endif
4290 if (curwin->w_cursor.col <= 1)
4291 did_ai = FALSE;
4292 /*
4293 * It's a little strange to put backspaces into the redo
4294 * buffer, but it makes auto-indent a lot easier to deal
4295 * with.
4296 */
4297 AppendCharToRedobuff(c);
4298
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004299 // If deleted before the insertion point, adjust it
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004300 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004301 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004302 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004304 // vi behaviour: the cursor moves backward but the character that
4305 // was there remains visible
4306 // Vim behaviour: the cursor moves backward and the character that
4307 // was there is erased from the screen.
4308 // We can emulate the vi behaviour by pretending there is a dollar
4309 // displayed even when there isn't.
4310 // --pkv Sun Jan 19 01:56:40 EST 2003
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004311 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 dollar_vcol = curwin->w_virtcol;
4313
Bram Moolenaarce3be472008-01-14 19:12:28 +00004314#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004315 // When deleting a char the cursor line must never be in a closed fold.
4316 // E.g., when 'foldmethod' is indent and deleting the first non-white
4317 // char before a Tab.
Bram Moolenaarce3be472008-01-14 19:12:28 +00004318 if (did_backspace)
4319 foldOpenCursor();
4320#endif
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 return did_backspace;
4323}
4324
Bram Moolenaarec2da362017-01-21 20:04:22 +01004325/*
4326 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4327 * P_PE literally.
4328 * When "drop" is TRUE then consume the text and drop it.
4329 */
4330 int
4331bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4332{
4333 int c;
4334 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4335 int idx = 0;
4336 char_u *end = find_termcode((char_u *)"PE");
4337 int ret_char = -1;
4338 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004339 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004341 // If the end code is too long we can't detect it, read everything.
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01004342 if (end != NULL && STRLEN(end) >= NUMBUFLEN)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004343 end = NULL;
4344 ++no_mapping;
4345 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004346 if (!p_paste)
4347 // Also have the side effects of setting 'paste' to make it work much
4348 // faster.
4349 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004350
Bram Moolenaarec2da362017-01-21 20:04:22 +01004351 for (;;)
4352 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004353 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004354 if (end == NULL && vpeekc() == NUL)
4355 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004356 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004357 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004358 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004359 if (c == NUL || got_int || (ex_normal_busy > 0 && c == Ctrl_C))
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004360 // When CTRL-C was encountered the typeahead will be flushed and we
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004361 // won't get the end sequence. Except when using ":normal".
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004362 break;
4363
Bram Moolenaarec2da362017-01-21 20:04:22 +01004364 if (has_mbyte)
4365 idx += (*mb_char2bytes)(c, buf + idx);
4366 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004367 buf[idx++] = c;
4368 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004369 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004370 {
4371 if (end[idx] == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 break; // Found the end of paste code.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004373 continue;
4374 }
4375 if (!drop)
4376 {
4377 switch (mode)
4378 {
4379 case PASTE_CMDLINE:
4380 put_on_cmdline(buf, idx, TRUE);
4381 break;
4382
4383 case PASTE_EX:
4384 if (gap != NULL && ga_grow(gap, idx) == OK)
4385 {
4386 mch_memmove((char *)gap->ga_data + gap->ga_len,
4387 buf, (size_t)idx);
4388 gap->ga_len += idx;
4389 }
4390 break;
4391
4392 case PASTE_INSERT:
4393 if (stop_arrow() == OK)
4394 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004395 c = buf[0];
4396 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
4397 ins_eol(c);
4398 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01004399 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004400 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01004401 AppendToRedobuffLit(buf, idx);
4402 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01004403 }
4404 break;
4405
4406 case PASTE_ONE_CHAR:
4407 if (ret_char == -1)
4408 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01004409 if (has_mbyte)
4410 ret_char = (*mb_ptr2char)(buf);
4411 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004412 ret_char = buf[0];
4413 }
4414 break;
4415 }
4416 }
4417 idx = 0;
4418 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004419
Bram Moolenaarec2da362017-01-21 20:04:22 +01004420 --no_mapping;
4421 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004422 if (!save_paste)
4423 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01004424
4425 return ret_char;
4426}
4427
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004428#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004430ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004431{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004432 // We will be leaving the current window, unless closing another tab.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004433 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
4434 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
4435 {
4436 undisplay_dollar();
4437 start_arrow(&curwin->w_cursor);
4438# ifdef FEAT_CINDENT
4439 can_cindent = TRUE;
4440# endif
4441 }
4442
4443 if (c == K_TABLINE)
4444 goto_tabpage(current_tab);
4445 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004446 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004447 handle_tabmenu();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004448 redraw_statuslines(); // will redraw the tabline when needed
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004449 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004450}
4451#endif
4452
4453#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004455ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456{
4457 pos_T tpos;
4458
4459 undisplay_dollar();
4460 tpos = curwin->w_cursor;
4461 if (gui_do_scroll())
4462 {
4463 start_arrow(&tpos);
4464# ifdef FEAT_CINDENT
4465 can_cindent = TRUE;
4466# endif
4467 }
4468}
4469
4470 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004471ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
4473 pos_T tpos;
4474
4475 undisplay_dollar();
4476 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004477 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
4479 start_arrow(&tpos);
4480# ifdef FEAT_CINDENT
4481 can_cindent = TRUE;
4482# endif
4483 }
4484}
4485#endif
4486
4487 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004488ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489{
4490 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004491 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493#ifdef FEAT_FOLDING
4494 if ((fdo_flags & FDO_HOR) && KeyTyped)
4495 foldOpenCursor();
4496#endif
4497 undisplay_dollar();
4498 tpos = curwin->w_cursor;
4499 if (oneleft() == OK)
4500 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004501#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004502 // Only call start_arrow() when not busy with preediting, it will
4503 // break undo. K_LEFT is inserted in im_correct_cursor().
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004504 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004505#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004506 {
4507 start_arrow_with_change(&tpos, end_change);
4508 if (!end_change)
4509 AppendCharToRedobuff(K_LEFT);
4510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004512 // If exit reversed string, position is fixed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
4514 revins_legal++;
4515 revins_chars++;
4516#endif
4517 }
4518
4519 /*
4520 * if 'whichwrap' set for cursor in insert mode may go to
4521 * previous line
4522 */
4523 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
4524 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004525 // always break undo when moving upwards/downwards, else undo may break
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 start_arrow(&tpos);
4527 --(curwin->w_cursor.lnum);
4528 coladvance((colnr_T)MAXCOL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004529 curwin->w_set_curswant = TRUE; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004532 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004533 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534}
4535
4536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004537ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538{
4539 pos_T tpos;
4540
4541#ifdef FEAT_FOLDING
4542 if ((fdo_flags & FDO_HOR) && KeyTyped)
4543 foldOpenCursor();
4544#endif
4545 undisplay_dollar();
4546 tpos = curwin->w_cursor;
4547 if (c == K_C_HOME)
4548 curwin->w_cursor.lnum = 1;
4549 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 curwin->w_curswant = 0;
4552 start_arrow(&tpos);
4553}
4554
4555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004556ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
4558 pos_T tpos;
4559
4560#ifdef FEAT_FOLDING
4561 if ((fdo_flags & FDO_HOR) && KeyTyped)
4562 foldOpenCursor();
4563#endif
4564 undisplay_dollar();
4565 tpos = curwin->w_cursor;
4566 if (c == K_C_END)
4567 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4568 coladvance((colnr_T)MAXCOL);
4569 curwin->w_curswant = MAXCOL;
4570
4571 start_arrow(&tpos);
4572}
4573
4574 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004575ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004577 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#ifdef FEAT_FOLDING
4579 if ((fdo_flags & FDO_HOR) && KeyTyped)
4580 foldOpenCursor();
4581#endif
4582 undisplay_dollar();
4583 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
4584 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004585 start_arrow_with_change(&curwin->w_cursor, end_change);
4586 if (!end_change)
4587 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 (void)bck_word(1L, FALSE, FALSE);
4589 curwin->w_set_curswant = TRUE;
4590 }
4591 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004592 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004593 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594}
4595
4596 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004597ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004599 int end_change = dont_sync_undo == FALSE; // end undoable change
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601#ifdef FEAT_FOLDING
4602 if ((fdo_flags & FDO_HOR) && KeyTyped)
4603 foldOpenCursor();
4604#endif
4605 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004606 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004608 start_arrow_with_change(&curwin->w_cursor, end_change);
4609 if (!end_change)
4610 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (virtual_active())
4613 oneright();
4614 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004617 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 ++curwin->w_cursor.col;
4620 }
4621
4622#ifdef FEAT_RIGHTLEFT
4623 revins_legal++;
4624 if (revins_chars)
4625 revins_chars--;
4626#endif
4627 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 // if 'whichwrap' set for cursor in insert mode, may move the
4629 // cursor to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 else if (vim_strchr(p_ww, ']') != NULL
4631 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4632 {
4633 start_arrow(&curwin->w_cursor);
4634 curwin->w_set_curswant = TRUE;
4635 ++curwin->w_cursor.lnum;
4636 curwin->w_cursor.col = 0;
4637 }
4638 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004639 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004640 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641}
4642
4643 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004644ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004646 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647#ifdef FEAT_FOLDING
4648 if ((fdo_flags & FDO_HOR) && KeyTyped)
4649 foldOpenCursor();
4650#endif
4651 undisplay_dollar();
4652 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4653 || gchar_cursor() != NUL)
4654 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004655 start_arrow_with_change(&curwin->w_cursor, end_change);
4656 if (!end_change)
4657 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 (void)fwd_word(1L, FALSE, 0);
4659 curwin->w_set_curswant = TRUE;
4660 }
4661 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004662 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004663 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664}
4665
4666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004667ins_up(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669{
4670 pos_T tpos;
4671 linenr_T old_topline = curwin->w_topline;
4672#ifdef FEAT_DIFF
4673 int old_topfill = curwin->w_topfill;
4674#endif
4675
4676 undisplay_dollar();
4677 tpos = curwin->w_cursor;
4678 if (cursor_up(1L, TRUE) == OK)
4679 {
4680 if (startcol)
4681 coladvance(getvcol_nolist(&Insstart));
4682 if (old_topline != curwin->w_topline
4683#ifdef FEAT_DIFF
4684 || old_topfill != curwin->w_topfill
4685#endif
4686 )
4687 redraw_later(VALID);
4688 start_arrow(&tpos);
4689#ifdef FEAT_CINDENT
4690 can_cindent = TRUE;
4691#endif
4692 }
4693 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004694 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695}
4696
4697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004698ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 pos_T tpos;
4701
4702 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004703
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004704 if (mod_mask & MOD_MASK_CTRL)
4705 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004706 // <C-PageUp>: tab page back
Bram Moolenaarbc444822006-10-17 11:37:50 +00004707 if (first_tabpage->tp_next != NULL)
4708 {
4709 start_arrow(&curwin->w_cursor);
4710 goto_tabpage(-1);
4711 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004712 return;
4713 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004714
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 tpos = curwin->w_cursor;
4716 if (onepage(BACKWARD, 1L) == OK)
4717 {
4718 start_arrow(&tpos);
4719#ifdef FEAT_CINDENT
4720 can_cindent = TRUE;
4721#endif
4722 }
4723 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004724 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725}
4726
4727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004728ins_down(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004729 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731 pos_T tpos;
4732 linenr_T old_topline = curwin->w_topline;
4733#ifdef FEAT_DIFF
4734 int old_topfill = curwin->w_topfill;
4735#endif
4736
4737 undisplay_dollar();
4738 tpos = curwin->w_cursor;
4739 if (cursor_down(1L, TRUE) == OK)
4740 {
4741 if (startcol)
4742 coladvance(getvcol_nolist(&Insstart));
4743 if (old_topline != curwin->w_topline
4744#ifdef FEAT_DIFF
4745 || old_topfill != curwin->w_topfill
4746#endif
4747 )
4748 redraw_later(VALID);
4749 start_arrow(&tpos);
4750#ifdef FEAT_CINDENT
4751 can_cindent = TRUE;
4752#endif
4753 }
4754 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004755 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756}
4757
4758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004759ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761 pos_T tpos;
4762
4763 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004764
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004765 if (mod_mask & MOD_MASK_CTRL)
4766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004767 // <C-PageDown>: tab page forward
Bram Moolenaarbc444822006-10-17 11:37:50 +00004768 if (first_tabpage->tp_next != NULL)
4769 {
4770 start_arrow(&curwin->w_cursor);
4771 goto_tabpage(0);
4772 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004773 return;
4774 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004775
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 tpos = curwin->w_cursor;
4777 if (onepage(FORWARD, 1L) == OK)
4778 {
4779 start_arrow(&tpos);
4780#ifdef FEAT_CINDENT
4781 can_cindent = TRUE;
4782#endif
4783 }
4784 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004785 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786}
4787
4788#ifdef FEAT_DND
4789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004790ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004792 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793}
4794#endif
4795
4796/*
4797 * Handle TAB in Insert or Replace mode.
4798 * Return TRUE when the TAB needs to be inserted like a normal character.
4799 */
4800 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004801ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802{
4803 int ind;
4804 int i;
4805 int temp;
4806
4807 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
4808 Insstart_blank_vcol = get_nolist_virtcol();
4809 if (echeck_abbr(TAB + ABBR_OFF))
4810 return FALSE;
4811
4812 ind = inindent(0);
4813#ifdef FEAT_CINDENT
4814 if (ind)
4815 can_cindent = FALSE;
4816#endif
4817
4818 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004819 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 */
4821 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004822#ifdef FEAT_VARTABS
4823 && !(p_sta && ind
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004824 // These five lines mean 'tabstop' != 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004825 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
4826 || (tabstop_count(curbuf->b_p_vts_array) == 1
4827 && tabstop_first(curbuf->b_p_vts_array)
4828 != get_sw_value(curbuf))
4829 || (tabstop_count(curbuf->b_p_vts_array) == 0
4830 && curbuf->b_p_ts != get_sw_value(curbuf))))
4831 && tabstop_count(curbuf->b_p_vsts_array) == 0
4832#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004833 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004834#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02004835 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 return TRUE;
4837
4838 if (stop_arrow() == FAIL)
4839 return TRUE;
4840
4841 did_ai = FALSE;
4842#ifdef FEAT_SMARTINDENT
4843 did_si = FALSE;
4844 can_si = FALSE;
4845 can_si_back = FALSE;
4846#endif
4847 AppendToRedobuff((char_u *)"\t");
4848
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004849#ifdef FEAT_VARTABS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004850 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004851 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004852 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004853 temp -= get_nolist_virtcol() % temp;
4854 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004855 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004856 // use 'softtabstop' when set
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004857 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004858 curbuf->b_p_vsts_array);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004859 else // otherwise use 'tabstop'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004860 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
4861 curbuf->b_p_vts_array);
4862#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004863 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004864 temp = (int)get_sw_value(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004865 else if (curbuf->b_p_sts != 0) // use 'softtabstop' when set
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02004866 temp = (int)get_sts_value();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004867 else // otherwise use 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 temp = (int)curbuf->b_p_ts;
4869 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 /*
4873 * Insert the first space with ins_char(). It will delete one char in
4874 * replace mode. Insert the rest with ins_str(); it will not delete any
4875 * chars. For VREPLACE mode, we use ins_char() for all characters.
4876 */
4877 ins_char(' ');
4878 while (--temp > 0)
4879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (State & VREPLACE_FLAG)
4881 ins_char(' ');
4882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
4884 ins_str((char_u *)" ");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004885 if (State & REPLACE_FLAG) // no char replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 replace_push(NUL);
4887 }
4888 }
4889
4890 /*
4891 * When 'expandtab' not set: Replace spaces by TABs where possible.
4892 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004893#ifdef FEAT_VARTABS
4894 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
4895 || get_sts_value() > 0
4896 || (p_sta && ind)))
4897#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02004898 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
4901 char_u *ptr;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004902 char_u *saved_line = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 pos_T fpos;
4905 pos_T *cursor;
4906 colnr_T want_vcol, vcol;
4907 int change_col = -1;
4908 int save_list = curwin->w_p_list;
4909
4910 /*
4911 * Get the current line. For VREPLACE mode, don't make real changes
4912 * yet, just work on a copy of the line.
4913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (State & VREPLACE_FLAG)
4915 {
4916 pos = curwin->w_cursor;
4917 cursor = &pos;
4918 saved_line = vim_strsave(ml_get_curline());
4919 if (saved_line == NULL)
4920 return FALSE;
4921 ptr = saved_line + pos.col;
4922 }
4923 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
4925 ptr = ml_get_cursor();
4926 cursor = &curwin->w_cursor;
4927 }
4928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004929 // When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
4931 curwin->w_p_list = FALSE;
4932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004933 // Find first white before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004935 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
4937 --fpos.col;
4938 --ptr;
4939 }
4940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004941 // In Replace mode, don't change characters before the insert point.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if ((State & REPLACE_FLAG)
4943 && fpos.lnum == Insstart.lnum
4944 && fpos.col < Insstart.col)
4945 {
4946 ptr += Insstart.col - fpos.col;
4947 fpos.col = Insstart.col;
4948 }
4949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 // compute virtual column numbers of first white and cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 getvcol(curwin, &fpos, &vcol, NULL, NULL);
4952 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
4953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 // Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
4955 // and 'linebreak' adding extra virtual columns.
Bram Moolenaar1c465442017-03-12 20:10:05 +01004956 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004958 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (vcol + i > want_vcol)
4960 break;
4961 if (*ptr != TAB)
4962 {
4963 *ptr = TAB;
4964 if (change_col < 0)
4965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004966 change_col = fpos.col; // Column of first change
4967 // May have to adjust Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
4969 Insstart.col = fpos.col;
4970 }
4971 }
4972 ++fpos.col;
4973 ++ptr;
4974 vcol += i;
4975 }
4976
4977 if (change_col >= 0)
4978 {
4979 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004980 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004982 // Skip over the spaces we need.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 while (vcol < want_vcol && *ptr == ' ')
4984 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004985 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 ++ptr;
4987 ++repl_off;
4988 }
4989 if (vcol > want_vcol)
4990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // Must have a char with 'showbreak' just before it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 --ptr;
4993 --repl_off;
4994 }
4995 fpos.col += repl_off;
4996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 // Delete following spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 i = cursor->col - fpos.col;
4999 if (i > 0)
5000 {
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005001#ifdef FEAT_PROP_POPUP
5002 if (!(State & VREPLACE_FLAG))
5003 {
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005004 char_u *newp;
5005 int col;
5006
5007 newp = alloc(curbuf->b_ml.ml_line_len - i);
5008 if (newp == NULL)
5009 return FALSE;
5010
5011 col = ptr - curbuf->b_ml.ml_line_ptr;
5012 if (col > 0)
5013 mch_memmove(newp, ptr - col, col);
5014 mch_memmove(newp + col, ptr + i,
5015 curbuf->b_ml.ml_line_len - col - i);
5016
5017 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
5018 vim_free(curbuf->b_ml.ml_line_ptr);
5019 curbuf->b_ml.ml_line_ptr = newp;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005020 curbuf->b_ml.ml_line_len -= i;
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005021 curbuf->b_ml.ml_flags =
5022 (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005023 }
5024 else
5025#endif
5026 STRMOVE(ptr, ptr + i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005027 // correct replace stack.
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005028 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 for (temp = i; --temp >= 0; )
5030 replace_join(repl_off);
5031 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005032#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005033 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005034 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005035 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005036 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5037 (char_u *)"\t", 1);
5038 }
5039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 cursor->col -= i;
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /*
5043 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5044 * backspacing over the changed spacing and then inserting the new
5045 * spacing.
5046 */
5047 if (State & VREPLACE_FLAG)
5048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005049 // Backspace from real cursor to change_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 backspace_until_column(change_col);
5051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005052 // Insert each char in saved_line from changed_col to
5053 // ptr-cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 ins_bytes_len(saved_line + change_col,
5055 cursor->col - change_col);
5056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (State & VREPLACE_FLAG)
5060 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 curwin->w_p_list = save_list;
5062 }
5063
5064 return FALSE;
5065}
5066
5067/*
5068 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005069 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005072ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
5074 int i;
5075
5076 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005077 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005079 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 undisplay_dollar();
5081
5082 /*
5083 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5084 * character under the cursor. Only push a NUL on the replace stack,
5085 * nothing to put back when the NL is deleted.
5086 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005087 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 replace_push(NUL);
5089
5090 /*
5091 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5092 * replacing the next line, so we push all of the characters left on the
5093 * line onto the replace stack. This is not done here though, it is done
5094 * in open_line().
5095 */
5096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005097 // Put cursor on NUL if on the last char and coladd is 1 (happens after
5098 // CTRL-O).
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005099 if (virtual_active() && curwin->w_cursor.coladd > 0)
5100 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005101
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005103 // NL in reverse insert will always start in the end of
5104 // current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (revins_on)
5106 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5107#endif
5108
5109 AppendToRedobuff(NL_STR);
5110 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005111 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 old_indent = 0;
5113#ifdef FEAT_CINDENT
5114 can_cindent = TRUE;
5115#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005116#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005117 // When inserting a line the cursor line must never be in a closed fold.
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005118 foldOpenCursor();
5119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005121 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122}
5123
5124#ifdef FEAT_DIGRAPHS
5125/*
5126 * Handle digraph in insert mode.
5127 * Returns character still to be inserted, or NUL when nothing remaining to be
5128 * done.
5129 */
5130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005131ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 int c;
5134 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005135 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136
5137 pc_status = PC_STATUS_UNSET;
5138 if (redrawing() && !char_avail())
5139 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005140 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005141 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005144 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145#ifdef FEAT_CMDL_INFO
5146 add_to_showcmd_c(Ctrl_K);
5147#endif
5148 }
5149
5150#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005151 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152#endif
5153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005154 // don't map the digraph chars. This also prevents the
5155 // mode message to be deleted when ESC is hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 ++no_mapping;
5157 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005158 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 --no_mapping;
5160 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005161 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005162 // when the line fits in 'columns' the '?' is at the start of the next
5163 // line and will not be removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005164 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005165
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005166 if (IS_SPECIAL(c) || mod_mask) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
5168#ifdef FEAT_CMDL_INFO
5169 clear_showcmd();
5170#endif
5171 insert_special(c, TRUE, FALSE);
5172 return NUL;
5173 }
5174 if (c != ESC)
5175 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005176 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 if (redrawing() && !char_avail())
5178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005179 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005180 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
5182 if (char2cells(c) == 1)
5183 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005184 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005186 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
5188#ifdef FEAT_CMDL_INFO
5189 add_to_showcmd_c(c);
5190#endif
5191 }
5192 ++no_mapping;
5193 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005194 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 --no_mapping;
5196 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005197 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005198 // when the line fits in 'columns' the '?' is at the start of the
5199 // next line and will not be removed by a redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005200 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (cc != ESC)
5202 {
5203 AppendToRedobuff((char_u *)CTRL_V_STR);
5204 c = getdigraph(c, cc, TRUE);
5205#ifdef FEAT_CMDL_INFO
5206 clear_showcmd();
5207#endif
5208 return c;
5209 }
5210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#ifdef FEAT_CMDL_INFO
5212 clear_showcmd();
5213#endif
5214 return NUL;
5215}
5216#endif
5217
5218/*
5219 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5220 * Returns the char to be inserted, or NUL if none found.
5221 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005222 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005223ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 int c;
5226 int temp;
5227 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005228 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
5230 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5231 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005232 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 return NUL;
5234 }
5235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // try to advance to the cursor column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005238 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 prev_ptr = ptr;
5240 validate_virtcol();
5241 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5242 {
5243 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005244 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
5246 if ((colnr_T)temp > curwin->w_virtcol)
5247 ptr = prev_ptr;
5248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005251 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 return c;
5253}
5254
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005255/*
5256 * CTRL-Y or CTRL-E typed in Insert mode.
5257 */
5258 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005259ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260{
5261 int c = tc;
5262
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005263 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 {
5265 if (c == Ctrl_Y)
5266 scrolldown_clamp();
5267 else
5268 scrollup_clamp();
5269 redraw_later(VALID);
5270 }
5271 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 {
5273 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5274 if (c != NUL)
5275 {
5276 long tw_save;
5277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005278 // The character must be taken literally, insert like it
5279 // was typed after a CTRL-V, and pretend 'textwidth'
5280 // wasn't set. Digits, 'o' and 'x' are special after a
5281 // CTRL-V, don't use it for these.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 if (c < 256 && !isalnum(c))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005283 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 tw_save = curbuf->b_p_tw;
5285 curbuf->b_p_tw = -1;
5286 insert_special(c, TRUE, FALSE);
5287 curbuf->b_p_tw = tw_save;
5288#ifdef FEAT_RIGHTLEFT
5289 revins_chars++;
5290 revins_legal++;
5291#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005292 c = Ctrl_V; // pretend CTRL-V is last character
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 auto_format(FALSE, TRUE);
5294 }
5295 }
5296 return c;
5297}
5298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299/*
5300 * Get the value that w_virtcol would have when 'list' is off.
5301 * Unless 'cpo' contains the 'L' flag.
5302 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005303 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005304get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005306 // check validity of cursor in current buffer
5307 if (curwin->w_buffer == NULL
5308 || curwin->w_buffer->b_ml.ml_mfp == NULL
5309 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5310 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5312 return getvcol_nolist(&curwin->w_cursor);
5313 validate_virtcol();
5314 return curwin->w_virtcol;
5315}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005316
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005317#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005318/*
5319 * Handle the InsertCharPre autocommand.
5320 * "c" is the character that was typed.
5321 * Return a pointer to allocated memory with the replacement string.
5322 * Return NULL to continue inserting "c".
5323 */
5324 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005325do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005326{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005327 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005328 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005329 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005330
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005331 // Return quickly when there is nothing to do.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005332 if (!has_insertcharpre())
5333 return NULL;
5334
Bram Moolenaar704984a2012-06-01 14:57:51 +02005335 if (has_mbyte)
5336 buf[(*mb_char2bytes)(c, buf)] = NUL;
5337 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005338 {
5339 buf[0] = c;
5340 buf[1] = NUL;
5341 }
5342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005343 // Lock the text to avoid weird things from happening.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005344 ++textwinlock;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005345 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005346
Bram Moolenaar704984a2012-06-01 14:57:51 +02005347 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005348 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005349 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005350 // Get the value of v:char. It may be empty or more than one
5351 // character. Only use it when changed, otherwise continue with the
5352 // original character to avoid breaking autoindent.
Bram Moolenaar704984a2012-06-01 14:57:51 +02005353 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5354 res = vim_strsave(get_vim_var_str(VV_CHAR));
5355 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005357 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005358 --textwinlock;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005359
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005360 // Restore the State, it may have been changed.
5361 State = save_State;
5362
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005363 return res;
5364}
5365#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005366
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005367#if defined(FEAT_CINDENT) || defined(PROTO)
5368 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005369get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005370{
5371 return can_cindent;
5372}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005373
5374 void
5375set_can_cindent(int val)
5376{
5377 can_cindent = val;
5378}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005379#endif
5380
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005381/*
5382 * Trigger "event" and take care of fixing undo.
5383 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005384 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005385ins_apply_autocmds(event_T event)
5386{
5387 varnumber_T tick = CHANGEDTICK(curbuf);
5388 int r;
5389
5390 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5391
5392 // If u_savesub() was called then we are not prepared to start
5393 // a new line. Call u_save() with no contents to fix that.
Bram Moolenaardb934952020-04-27 20:18:31 +02005394 // Except when leaving Insert mode.
5395 if (event != EVENT_INSERTLEAVE && tick != CHANGEDTICK(curbuf))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005396 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5397
5398 return r;
5399}