blob: 9162fe629894580e097fe3c86adcc66785c4125b [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 Moolenaar2e6cdb92021-01-28 11:07:44 +0100199 // Check for changed highlighting, e.g. for ModeMsg.
200 if (need_highlight_changed)
201 highlight_changed();
202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100203 // Make sure the cursor didn't move. Do call check_cursor_col() in
204 // case the text was modified. Since Insert mode was not started yet
205 // a call to check_cursor_col() may move the cursor, especially with
206 // the "A" command, thus set State to avoid that. Also check that the
207 // line number is still valid (lines may have been deleted).
208 // Do not restore if v:char was set to a non-empty string.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100209 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100210#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200211 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100212#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200213 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100214 {
215 int save_state = State;
216
217 curwin->w_cursor = save_cursor;
218 State = INSERT;
219 check_cursor_col();
220 State = save_state;
221 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000222 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000223
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200224#ifdef FEAT_CONCEAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100225 // Check if the cursor line needs redrawing before changing State. If
226 // 'concealcursor' is "n" it needs to be redrawn without concealing.
Bram Moolenaarb9464822018-05-10 15:09:49 +0200227 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200228#endif
229
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 /*
231 * When doing a paste with the middle mouse button, Insstart is set to
232 * where the paste started.
233 */
234 if (where_paste_started.lnum != 0)
235 Insstart = where_paste_started;
236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {
238 Insstart = curwin->w_cursor;
239 if (startln)
240 Insstart.col = 0;
241 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000242 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 Insstart_blank_vcol = MAXCOL;
244 if (!did_ai)
245 ai_col = 0;
246
247 if (cmdchar != NUL && restart_edit == 0)
248 {
249 ResetRedobuff();
250 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 if (cmdchar == 'V' || cmdchar == 'v')
252 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100253 // "gR" or "gr" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 AppendCharToRedobuff('g');
255 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
256 }
257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100259 if (cmdchar == K_PS)
260 AppendCharToRedobuff('a');
261 else
262 AppendCharToRedobuff(cmdchar);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100263 if (cmdchar == 'g') // "gI" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 AppendCharToRedobuff('I');
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100265 else if (cmdchar == 'r') // "r<CR>" command
266 count = 1; // insert only one <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 }
268 }
269
270 if (cmdchar == 'R')
271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 State = REPLACE;
273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 else if (cmdchar == 'V' || cmdchar == 'v')
275 {
276 State = VREPLACE;
277 replaceState = VREPLACE;
278 orig_line_count = curbuf->b_ml.ml_line_count;
279 vr_lines_changed = 1;
280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 else
282 State = INSERT;
283
284 stop_insert_mode = FALSE;
285
286 /*
287 * Need to recompute the cursor position, it might move when the cursor is
288 * on a TAB or special character.
289 */
290 curs_columns(TRUE);
291
292 /*
293 * Enable langmap or IME, indicated by 'iminsert'.
294 * Note that IME may enabled/disabled without us noticing here, thus the
295 * 'iminsert' value may not reflect what is actually used. It is updated
296 * when hitting <Esc>.
297 */
298 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
299 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100300#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
302#endif
303
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#ifdef FEAT_CMDL_INFO
306 clear_showcmd();
307#endif
308#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100309 // there is no reverse replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 revins_on = (State == INSERT && p_ri);
311 if (revins_on)
312 undisplay_dollar();
313 revins_chars = 0;
314 revins_legal = 0;
315 revins_scol = -1;
316#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100317 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100318 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200319#ifdef FEAT_JOB_CHANNEL
320 ch_log_output = TRUE;
321#endif
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100322 // Disable bracketed paste mode, we won't recognize the escape
323 // sequences.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100324 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100326 // Disable modifyOtherKeys, keys with modifiers would cause exiting
327 // Insert mode.
328 out_str(T_CTE);
329 }
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 /*
332 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100333 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
334 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 */
336 if (restart_edit != 0 && stuff_empty())
337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 /*
339 * After a paste we consider text typed to be part of the insert for
340 * the pasted text. You can backspace over the pasted text too.
341 */
342 if (where_paste_started.lnum)
343 arrow_used = FALSE;
344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 arrow_used = TRUE;
346 restart_edit = 0;
347
348 /*
349 * If the cursor was after the end-of-line before the CTRL-O and it is
350 * now at the end-of-line, put it after the end-of-line (this is not
351 * correct in very rare cases).
352 * Also do this if curswant is greater than the current virtual
353 * column. Eg after "^O$" or "^O80|".
354 */
355 validate_virtcol();
356 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000357 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 || curwin->w_curswant > curwin->w_virtcol)
359 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
360 {
361 if (ptr[1] == NUL)
362 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 else if (has_mbyte)
364 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000365 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 if (ptr[i] == NUL)
367 curwin->w_cursor.col += i;
368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000370 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372 else
373 arrow_used = FALSE;
374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100375 // we are in insert mode now, don't need to start it anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 need_start_insertmode = FALSE;
377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100378 // Need to save the line for undo before inserting the first char.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 ins_need_undo = TRUE;
380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 where_paste_started.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382#ifdef FEAT_CINDENT
383 can_cindent = TRUE;
384#endif
385#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100386 // The cursor line is not in a closed fold, unless 'insertmode' is set or
387 // restarting.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 if (!p_im && did_restart_edit == 0)
389 foldOpenCursor();
390#endif
391
392 /*
393 * If 'showmode' is set, show the current (insert/replace/..) mode.
394 * A warning message for changing a readonly file is given here, before
395 * actually changing anything. It's put after the mode, if any.
396 */
397 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000398 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 i = showmode();
400
401 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000402 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
404#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100405 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406#endif
407#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100408 do_digraph(-1); // clear digraphs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#endif
410
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000411 /*
412 * Get the current length of the redo buffer, those characters have to be
413 * skipped if we want to get to the inserted characters.
414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 ptr = get_inserted();
416 if (ptr == NULL)
417 new_insert_skip = 0;
418 else
419 {
420 new_insert_skip = (int)STRLEN(ptr);
421 vim_free(ptr);
422 }
423
424 old_indent = 0;
425
426 /*
427 * Main loop in Insert mode: repeat until Insert mode is left.
428 */
429 for (;;)
430 {
431#ifdef FEAT_RIGHTLEFT
432 if (!revins_legal)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100433 revins_scol = -1; // reset on illegal motions
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 else
435 revins_legal = 0;
436#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100437 if (arrow_used) // don't repeat insert when arrow key used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 count = 0;
439
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100440 if (update_Insstart_orig)
441 Insstart_orig = Insstart;
442
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200443 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100445 // ":stopinsert" used or 'insertmode' reset
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 count = 0;
447 goto doESCkey;
448 }
449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100450 // set curwin->w_curswant for next K_DOWN or K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 if (!arrow_used)
452 curwin->w_set_curswant = TRUE;
453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100454 // If there is no typeahead may check for timestamps (e.g., for when a
455 // menu invoked a shell command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (stuff_empty())
457 {
458 did_check_timestamps = FALSE;
459 if (need_check_timestamps)
460 check_timestamps(FALSE);
461 }
462
463 /*
464 * When emsg() was called msg_scroll will have been set.
465 */
466 msg_scroll = FALSE;
467
468#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100469 // When 'mousefocus' is set a mouse movement may have taken us to
470 // another window. "need_mouse_correct" may then be set because of an
471 // autocommand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 if (need_mouse_correct)
473 gui_mouse_correct();
474#endif
475
476#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100477 // Open fold at the cursor line, according to 'foldopen'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (fdo_flags & FDO_INSERT)
479 foldOpenCursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100480 // Close folds where the cursor isn't, according to 'foldclose'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 if (!char_avail())
482 foldCheckClose();
483#endif
484
Bram Moolenaarf2732452018-06-03 14:47:35 +0200485#ifdef FEAT_JOB_CHANNEL
486 if (bt_prompt(curbuf))
487 {
488 init_prompt(cmdchar_todo);
489 cmdchar_todo = NUL;
490 }
491#endif
492
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 /*
494 * If we inserted a character at the last position of the last line in
495 * the window, scroll the window one line up. This avoids an extra
496 * redraw.
497 * This is detected when the cursor column is smaller after inserting
498 * something.
499 * Don't do this when the topline changed already, it has
500 * already been adjusted (by insertchar() calling open_line())).
501 */
502 if (curbuf->b_mod_set
503 && curwin->w_p_wrap
504 && !did_backspace
505 && curwin->w_topline == old_topline
506#ifdef FEAT_DIFF
507 && curwin->w_topfill == old_topfill
508#endif
509 )
510 {
511 mincol = curwin->w_wcol;
512 validate_cursor_col();
513
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200514 if (
515#ifdef FEAT_VARTABS
516 (int)curwin->w_wcol < mincol - tabstop_at(
517 get_nolist_virtcol(), curbuf->b_p_ts,
518 curbuf->b_p_vts_array)
519#else
520 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100523 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 && (curwin->w_cursor.lnum != curwin->w_topline
525#ifdef FEAT_DIFF
526 || curwin->w_topfill > 0
527#endif
528 ))
529 {
530#ifdef FEAT_DIFF
531 if (curwin->w_topfill > 0)
532 --curwin->w_topfill;
533 else
534#endif
535#ifdef FEAT_FOLDING
536 if (hasFolding(curwin->w_topline, NULL, &old_topline))
537 set_topline(curwin, old_topline + 1);
538 else
539#endif
540 set_topline(curwin, curwin->w_topline + 1);
541 }
542 }
543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100544 // May need to adjust w_topline to show the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 update_topline();
546
547 did_backspace = FALSE;
548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100549 validate_cursor(); // may set must_redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
551 /*
552 * Redraw the display when no characters are waiting.
553 * Also shows mode, ruler and positions cursor.
554 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000555 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (curwin->w_p_scb)
558 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaar860cae12010-06-05 23:22:07 +0200560 if (curwin->w_p_crb)
561 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 update_curswant();
563 old_topline = curwin->w_topline;
564#ifdef FEAT_DIFF
565 old_topfill = curwin->w_topfill;
566#endif
567
568#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100569 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570#endif
571
572 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100573 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100575 if (c != K_CURSORHOLD)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100576 lastc = c; // remember the previous char for CTRL-D
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100578 // After using CTRL-G U the next cursor key will not break undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200579 if (dont_sync_undo == MAYBE)
580 dont_sync_undo = TRUE;
581 else
582 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100583 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100584 // Got here from normal mode when bracketed paste started.
Bram Moolenaarec2da362017-01-21 20:04:22 +0100585 c = K_PS;
586 else
587 do
588 {
589 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200590
591 if (stop_insert_mode)
592 {
593 // Insert mode ended, possibly from a callback.
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200594 if (c != K_IGNORE && c != K_NOP)
595 vungetc(c);
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200596 count = 0;
597 nomove = TRUE;
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200598 ins_compl_prep(ESC);
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200599 goto doESCkey;
600 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100601 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100603 // Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V.
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000604 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606#ifdef FEAT_RIGHTLEFT
607 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100608 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000611 /*
612 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000613 * and the cursor is still in the completed word. Only when there is
614 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000615 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100616 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000617 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100618 && curwin->w_cursor.col >= ins_compl_col()
619 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000620 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100621 // BS: Delete one character from "compl_leader".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000622 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100623 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000624 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000625 continue;
626
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100627 // When no match was selected or it was edited.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100628 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100630 // CTRL-L: Add one character from the current match to
631 // "compl_leader". Except when at the original match and
632 // there is nothing to add, CTRL-L works like CTRL-P then.
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000633 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100634 && (!ctrl_x_mode_line_or_eval()
635 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000636 {
637 ins_compl_addfrommatch();
638 continue;
639 }
640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100641 // A non-white character that fits in with the current
642 // completion: Add to "compl_leader".
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000643 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000644 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100645#if defined(FEAT_EVAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100646 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100647 char_u *str = do_insert_char_pre(c);
648 char_u *p;
649
650 if (str != NULL)
651 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100652 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100653 ins_compl_addleader(PTR2CHAR(p));
654 vim_free(str);
655 }
656 else
657#endif
658 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000659 continue;
660 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100662 // Pressing CTRL-Y selects the current match. When
663 // ins_compl_enter_selects() is set the Enter key does the
664 // same.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100665 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200666 && (c == CAR || c == K_KENTER || c == NL)))
667 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000668 {
669 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200670 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000671 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000672 }
673 }
674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100675 // Prepare for or stop CTRL-X mode. This doesn't do completion, but
676 // it does fix up the text when finishing completion.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100677 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000678 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000679 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100681 // CTRL-\ CTRL-N goes to Normal mode,
682 // CTRL-\ CTRL-G goes to mode selected with 'insertmode',
683 // CTRL-\ CTRL-O is like CTRL-O but without moving the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (c == Ctrl_BSL)
685 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100686 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +0000687 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 ++no_mapping;
689 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000690 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 --no_mapping;
692 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000693 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100695 // it's something else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 vungetc(c);
697 c = Ctrl_BSL;
698 }
699 else if (c == Ctrl_G && p_im)
700 continue;
701 else
702 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000703 if (c == Ctrl_O)
704 {
705 ins_ctrl_o();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100706 ins_at_eol = FALSE; // cursor keeps its column
Bram Moolenaar488c6512005-08-11 20:09:58 +0000707 nomove = TRUE;
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 count = 0;
710 goto doESCkey;
711 }
712 }
713
714#ifdef FEAT_DIGRAPHS
715 c = do_digraph(c);
716#endif
717
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100718 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 if (c == Ctrl_V || c == Ctrl_Q)
721 {
722 ins_ctrl_v();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100723 c = Ctrl_V; // pretend CTRL-V is last typed character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 continue;
725 }
726
727#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200728 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100730 // A key name preceded by a bang means this key is not to be
731 // inserted. Skip ahead to the re-indenting below.
732 // A key name preceded by a star means that indenting has to be
733 // done before inserting the key.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 line_is_white = inindent(0);
735 if (in_cinkeys(c, '!', line_is_white))
736 goto force_cindent;
737 if (can_cindent && in_cinkeys(c, '*', line_is_white)
738 && stop_arrow() == OK)
739 do_c_expr_indent();
740 }
741#endif
742
743#ifdef FEAT_RIGHTLEFT
744 if (curwin->w_p_rl)
745 switch (c)
746 {
747 case K_LEFT: c = K_RIGHT; break;
748 case K_S_LEFT: c = K_S_RIGHT; break;
749 case K_C_LEFT: c = K_C_RIGHT; break;
750 case K_RIGHT: c = K_LEFT; break;
751 case K_S_RIGHT: c = K_S_LEFT; break;
752 case K_C_RIGHT: c = K_C_LEFT; break;
753 }
754#endif
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 /*
757 * If 'keymodel' contains "startsel", may start selection. If it
758 * does, a CTRL-O and c will be stuffed, we need to get these
759 * characters.
760 */
761 if (ins_start_select(c))
762 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763
764 /*
765 * The big switch to handle a character in insert mode.
766 */
767 switch (c)
768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100769 case ESC: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (echeck_abbr(ESC + ABBR_OFF))
771 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100772 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 case Ctrl_C: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_CMDWIN
776 if (c == Ctrl_C && cmdwin_type != 0)
777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100778 // Close the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 cmdwin_result = K_IGNORE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100780 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000781 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 goto doESCkey;
783 }
784#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200785#ifdef FEAT_JOB_CHANNEL
786 if (c == Ctrl_C && bt_prompt(curbuf))
787 {
788 if (invoke_prompt_interrupt())
789 {
790 if (!bt_prompt(curbuf))
791 // buffer changed to a non-prompt buffer, get out of
792 // Insert mode
793 goto doESCkey;
794 break;
795 }
796 }
797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
799#ifdef UNIX
800do_intr:
801#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100802 // when 'insertmode' set, and not halfway a mapping, don't leave
803 // Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (goto_im())
805 {
806 if (got_int)
807 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100808 (void)vgetc(); // flush all buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 got_int = FALSE;
810 }
811 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200812 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 break;
814 }
815doESCkey:
816 /*
817 * This is the ONLY return from edit()!
818 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100819 // Always update o_lnum, so that a "CTRL-O ." that adds a line
820 // still puts the cursor back after the inserted text.
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000821 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 o_lnum = curwin->w_cursor.lnum;
823
Bram Moolenaar488c6512005-08-11 20:09:58 +0000824 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000825 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100826 // When CTRL-C was typed got_int will be set, with the result
827 // that the autocommands won't be executed. When mapped got_int
828 // is not set, but let's keep the behavior the same.
829 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200830 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000831 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 continue;
835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100836 case Ctrl_Z: // suspend when 'insertmode' set
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000837 if (!p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100838 goto normalchar; // insert CTRL-Z as normal char
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100839 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100840#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 ui_cursor_shape(); // may need to update cursor shape
Bram Moolenaar74a47162017-02-26 19:09:05 +0100842#endif
843 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100845 case Ctrl_O: // execute one command
Bram Moolenaare344bea2005-09-01 20:46:49 +0000846#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100847 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000848 goto docomplete;
849#endif
850 if (echeck_abbr(Ctrl_O + ABBR_OFF))
851 break;
852 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100854 // don't move the cursor left when 'virtualedit' has "onemore".
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000855 if (ve_flags & VE_ONEMORE)
856 {
857 ins_at_eol = FALSE;
858 nomove = TRUE;
859 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000860 count = 0;
861 goto doESCkey;
862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100863 case K_INS: // toggle insert/replace mode
Bram Moolenaar572cb562005-08-05 21:35:02 +0000864 case K_KINS:
865 ins_insert(replaceState);
866 break;
867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar572cb562005-08-05 21:35:02 +0000869 break;
870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100871 case K_HELP: // Help key works like <ESC> <Help>
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000872 case K_F1:
873 case K_XF1:
874 stuffcharReadbuff(K_HELP);
875 if (p_im)
876 need_start_insertmode = TRUE;
877 goto doESCkey;
878
879#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100880 case K_F21: // NetBeans command
881 ++no_mapping; // don't map the next key hits
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000882 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000883 --no_mapping;
884 netbeans_keycommand(i);
885 break;
886#endif
887
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100888 case K_ZERO: // Insert the previously inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 case NUL:
890 case Ctrl_A:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100891 // For ^@ the trailing ESC will end the insert, unless there is an
892 // error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
894 && c != Ctrl_A && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100895 goto doESCkey; // quit insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 inserted_space = FALSE;
897 break;
898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100899 case Ctrl_R: // insert the contents of a register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 ins_reg();
901 auto_format(FALSE, TRUE);
902 inserted_space = FALSE;
903 break;
904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 case Ctrl_G: // commands starting with CTRL-G
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 ins_ctrl_g();
907 break;
908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100909 case Ctrl_HAT: // switch input mode and/or langmap
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000910 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 break;
912
913#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100914 case Ctrl__: // switch between languages
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (!p_ari)
916 goto normalchar;
917 ins_ctrl_();
918 break;
919#endif
920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100921 case Ctrl_D: // Make indent one shiftwidth smaller.
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200922#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100923 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 goto docomplete;
925#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100926 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100928 case Ctrl_T: // Make indent one shiftwidth greater.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100929 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000931 if (has_compl_option(FALSE))
932 goto docomplete;
933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200935
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 ins_shift(c, lastc);
937 auto_format(FALSE, TRUE);
938 inserted_space = FALSE;
939 break;
940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100941 case K_DEL: // delete character under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 case K_KDEL:
943 ins_del();
944 auto_format(FALSE, TRUE);
945 break;
946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100947 case K_BS: // delete character before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 case Ctrl_H:
949 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
950 auto_format(FALSE, TRUE);
951 break;
952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100953 case Ctrl_W: // delete word before the cursor
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200954#ifdef FEAT_JOB_CHANNEL
955 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
956 {
957 // In a prompt window CTRL-W is used for window commands.
958 // Use Shift-CTRL-W to delete a word.
959 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200960 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200961 nomove = TRUE;
962 count = 0;
963 goto doESCkey;
964 }
965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
967 auto_format(FALSE, TRUE);
968 break;
969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100970 case Ctrl_U: // delete all inserted text in current line
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000971# ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100972 // CTRL-X CTRL-U completes with 'completefunc'.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100973 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000974 goto docomplete;
975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
977 auto_format(FALSE, TRUE);
978 inserted_space = FALSE;
979 break;
980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100981 case K_LEFTMOUSE: // mouse keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 case K_LEFTMOUSE_NM:
983 case K_LEFTDRAG:
984 case K_LEFTRELEASE:
985 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100986 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 case K_MIDDLEMOUSE:
988 case K_MIDDLEDRAG:
989 case K_MIDDLERELEASE:
990 case K_RIGHTMOUSE:
991 case K_RIGHTDRAG:
992 case K_RIGHTRELEASE:
993 case K_X1MOUSE:
994 case K_X1DRAG:
995 case K_X1RELEASE:
996 case K_X2MOUSE:
997 case K_X2DRAG:
998 case K_X2RELEASE:
999 ins_mouse(c);
1000 break;
1001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 case K_MOUSEDOWN: // Default action for scroll wheel up: scroll up
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001003 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 break;
1005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001006 case K_MOUSEUP: // Default action for scroll wheel down: scroll down
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001007 ins_mousescroll(MSCR_UP);
1008 break;
1009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 case K_MOUSELEFT: // Scroll wheel left
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001011 ins_mousescroll(MSCR_LEFT);
1012 break;
1013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 case K_MOUSERIGHT: // Scroll wheel right
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001015 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001017
Bram Moolenaarec2da362017-01-21 20:04:22 +01001018 case K_PS:
1019 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1020 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001021 // invoked from normal mode, bail out
Bram Moolenaarec2da362017-01-21 20:04:22 +01001022 goto doESCkey;
1023 break;
1024 case K_PE:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001025 // Got K_PE without K_PS, ignore.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001026 break;
1027
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001028#ifdef FEAT_GUI_TABLINE
1029 case K_TABLINE:
1030 case K_TABMENU:
1031 ins_tabline(c);
1032 break;
1033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001035 case K_IGNORE: // Something mapped to nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 break;
1037
Bram Moolenaar957cf672020-11-12 14:21:06 +01001038 case K_COMMAND: // <Cmd>command<CR>
1039 do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaare41decc2020-11-14 21:34:59 +01001040#ifdef FEAT_TERMINAL
1041 if (term_use_loop())
1042 // Started a terminal that gets the input, exit Insert mode.
1043 goto doESCkey;
1044#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01001045 break;
1046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001047 case K_CURSORHOLD: // Didn't type something for a while.
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001048 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001049 did_cursorhold = TRUE;
1050 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001051
Bram Moolenaar4f974752019-02-17 17:44:42 +01001052#ifdef FEAT_GUI_MSWIN
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001053 // On MS-Windows ignore <M-F4>, we get it when closing the window
1054 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001055 case K_F4:
1056 if (mod_mask != MOD_MASK_ALT)
1057 goto normalchar;
1058 break;
1059#endif
1060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#ifdef FEAT_GUI
1062 case K_VER_SCROLLBAR:
1063 ins_scroll();
1064 break;
1065
1066 case K_HOR_SCROLLBAR:
1067 ins_horscroll();
1068 break;
1069#endif
1070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 case K_HOME: // <Home>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case K_S_HOME:
1074 case K_C_HOME:
1075 ins_home(c);
1076 break;
1077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001078 case K_END: // <End>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 case K_S_END:
1081 case K_C_END:
1082 ins_end(c);
1083 break;
1084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001085 case K_LEFT: // <Left>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001086 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1087 ins_s_left();
1088 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001089 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 break;
1091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 case K_S_LEFT: // <S-Left>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 case K_C_LEFT:
1094 ins_s_left();
1095 break;
1096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001097 case K_RIGHT: // <Right>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001098 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1099 ins_s_right();
1100 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001101 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 break;
1103
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001104 case K_S_RIGHT: // <S-Right>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 case K_C_RIGHT:
1106 ins_s_right();
1107 break;
1108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001109 case K_UP: // <Up>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001110 if (pum_visible())
1111 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001112 if (mod_mask & MOD_MASK_SHIFT)
1113 ins_pageup();
1114 else
1115 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 break;
1117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001118 case K_S_UP: // <S-Up>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 case K_PAGEUP:
1120 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001121 if (pum_visible())
1122 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 ins_pageup();
1124 break;
1125
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001126 case K_DOWN: // <Down>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001127 if (pum_visible())
1128 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001129 if (mod_mask & MOD_MASK_SHIFT)
1130 ins_pagedown();
1131 else
1132 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 break;
1134
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001135 case K_S_DOWN: // <S-Down>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 case K_PAGEDOWN:
1137 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001138 if (pum_visible())
1139 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 ins_pagedown();
1141 break;
1142
1143#ifdef FEAT_DND
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001144 case K_DROP: // drag-n-drop event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 ins_drop();
1146 break;
1147#endif
1148
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001149 case K_S_TAB: // When not mapped, use like a normal TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 c = TAB;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001151 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001153 case TAB: // TAB or Complete patterns along path
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001154#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001155 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 goto docomplete;
1157#endif
1158 inserted_space = FALSE;
1159 if (ins_tab())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001160 goto normalchar; // insert TAB as a normal char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 auto_format(FALSE, TRUE);
1162 break;
1163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001164 case K_KENTER: // <Enter>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 c = CAR;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001166 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 case CAR:
1168 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001169#if defined(FEAT_QUICKFIX)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001170 // In a quickfix window a <CR> jumps to the error under the
1171 // cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (bt_quickfix(curbuf) && c == CAR)
1173 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001174 if (curwin->w_llist_ref == NULL) // quickfix window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001175 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001176 else // location list window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001177 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 break;
1179 }
1180#endif
1181#ifdef FEAT_CMDWIN
1182 if (cmdwin_type != 0)
1183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001184 // Execute the command in the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 cmdwin_result = CAR;
1186 goto doESCkey;
1187 }
1188#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001189#ifdef FEAT_JOB_CHANNEL
1190 if (bt_prompt(curbuf))
1191 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001192 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001193 if (!bt_prompt(curbuf))
1194 // buffer changed to a non-prompt buffer, get out of
1195 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001196 goto doESCkey;
1197 break;
1198 }
1199#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001200 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001201 goto doESCkey; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 auto_format(FALSE, FALSE);
1203 inserted_space = FALSE;
1204 break;
1205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001206 case Ctrl_K: // digraph or keyword completion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001207 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 if (has_compl_option(TRUE))
1210 goto docomplete;
1211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001213#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 c = ins_digraph();
1215 if (c == NUL)
1216 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001218 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001220 case Ctrl_X: // Enter CTRL-X mode
Bram Moolenaar572cb562005-08-05 21:35:02 +00001221 ins_ctrl_x();
1222 break;
1223
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001224 case Ctrl_RSB: // Tag name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001225 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 goto normalchar;
1227 goto docomplete;
1228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001229 case Ctrl_F: // File name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001230 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 goto normalchar;
1232 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001233
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001234 case 's': // Spelling completion after ^X
Bram Moolenaar488c6512005-08-11 20:09:58 +00001235 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001236 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001237 goto normalchar;
1238 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001240 case Ctrl_L: // Whole line completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001241 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001243 // CTRL-L with 'insertmode' set: Leave Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (p_im)
1245 {
1246 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1247 break;
1248 goto doESCkey;
1249 }
1250 goto normalchar;
1251 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001252 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001254 case Ctrl_P: // Do previous/next pattern completion
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 case Ctrl_N:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001256 // if 'complete' is empty then plain ^P is no longer special,
1257 // but it is under other ^X modes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001259 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001260 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 goto normalchar;
1262
1263docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001264 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001265#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001266 disable_fold_update++; // don't redraw folds here
Bram Moolenaara6c07602017-03-05 21:18:27 +01001267#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001268 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001270#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001271 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001272#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001273 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001276 case Ctrl_Y: // copy from previous line or scroll down
1277 case Ctrl_E: // copy from next line or scroll up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001278 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 break;
1280
1281 default:
1282#ifdef UNIX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001283 if (c == intr_char) // special interrupt char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 goto do_intr;
1285#endif
1286
Bram Moolenaare659c952011-05-19 17:25:41 +02001287normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001289 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001291#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001292 if (!p_paste)
1293 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001294 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001295 char_u *str = do_insert_char_pre(c);
1296 char_u *p;
1297
1298 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001299 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001300 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001301 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001302 // Insert the new value of v:char literally.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001303 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001304 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001305 c = PTR2CHAR(p);
1306 if (c == CAR || c == K_KENTER || c == NL)
1307 ins_eol(c);
1308 else
1309 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001310 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001311 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001312 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001313 vim_free(str);
1314 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001315 }
1316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001317 // If the new value is already inserted or an empty string
1318 // then don't insert any character.
Bram Moolenaare659c952011-05-19 17:25:41 +02001319 if (c == NUL)
1320 break;
1321 }
1322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#ifdef FEAT_SMARTINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001324 // Try to perform smart-indenting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 ins_try_si(c);
1326#endif
1327
1328 if (c == ' ')
1329 {
1330 inserted_space = TRUE;
1331#ifdef FEAT_CINDENT
1332 if (inindent(0))
1333 can_cindent = FALSE;
1334#endif
1335 if (Insstart_blank_vcol == MAXCOL
1336 && curwin->w_cursor.lnum == Insstart.lnum)
1337 Insstart_blank_vcol = get_nolist_virtcol();
1338 }
1339
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001340 // Insert a normal character and check for abbreviations on a
1341 // special character. Let CTRL-] expand abbreviations without
1342 // inserting it.
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001343 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001344 // Add ABBR_OFF for characters above 0x100, this is
1345 // what check_abbr() expects.
1346 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1347 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
1349 insert_special(c, FALSE, FALSE);
1350#ifdef FEAT_RIGHTLEFT
1351 revins_legal++;
1352 revins_chars++;
1353#endif
1354 }
1355
1356 auto_format(FALSE, TRUE);
1357
1358#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001359 // When inserting a character the cursor line must never be in a
1360 // closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 foldOpenCursor();
1362#endif
1363 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001364 } // end of switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001366 // If typed something may trigger CursorHoldI again.
Bram Moolenaar245c4102016-04-20 17:37:41 +02001367 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001368#ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001369 // but not in CTRL-X mode, a script can't restore the state
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001370 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001371#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001372 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001373 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001375 // If the cursor was moved we didn't just insert a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (arrow_used)
1377 inserted_space = FALSE;
1378
1379#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001380 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
1382force_cindent:
1383 /*
1384 * Indent now if a key was typed that is in 'cinkeys'.
1385 */
1386 if (in_cinkeys(c, ' ', line_is_white))
1387 {
1388 if (stop_arrow() == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001389 // re-indent the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 do_c_expr_indent();
1391 }
1392 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001393#endif // FEAT_CINDENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001395 } // for (;;)
1396 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397}
1398
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001399 int
1400ins_need_undo_get(void)
1401{
1402 return ins_need_undo;
1403}
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405/*
1406 * Redraw for Insert mode.
1407 * This is postponed until getting the next character to make '$' in the 'cpo'
1408 * option work correctly.
1409 * Only redraw when there are no characters available. This speeds up
1410 * inserting sequences of characters (e.g., for CTRL-R).
1411 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001412 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001413ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001415#ifdef FEAT_CONCEAL
1416 linenr_T conceal_old_cursor_line = 0;
1417 linenr_T conceal_new_cursor_line = 0;
1418 int conceal_update_lines = FALSE;
1419#endif
1420
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001421 if (char_avail())
1422 return;
1423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001424 // Trigger CursorMoved if the cursor moved. Not when the popup menu is
1425 // visible, the command might delete it.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001426 if (ready && (has_cursormovedI()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001427# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001428 || popup_visible
1429# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001430# if defined(FEAT_CONCEAL)
1431 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001432# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001433 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001434 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001435 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001436 {
1437# ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001438 // Need to update the screen first, to make sure syntax
1439 // highlighting is correct after making a change (e.g., inserting
1440 // a "(". The autocommand may also require a redraw, so it's done
1441 // again below, unfortunately.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001442 if (syntax_present(curwin) && must_redraw)
1443 update_screen(0);
1444# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001445 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001446 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001447 // Make sure curswant is correct, an autocommand may call
1448 // getcurpos().
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001449 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001450 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001451 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001452#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001453 if (popup_visible)
1454 popup_check_cursor_pos();
1455#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001456# ifdef FEAT_CONCEAL
1457 if (curwin->w_p_cole > 0)
1458 {
1459 conceal_old_cursor_line = last_cursormoved.lnum;
1460 conceal_new_cursor_line = curwin->w_cursor.lnum;
1461 conceal_update_lines = TRUE;
1462 }
1463# endif
1464 last_cursormoved = curwin->w_cursor;
1465 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001467 // Trigger TextChangedI if b_changedtick differs.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001468 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001469 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001470 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001471 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001472 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001473 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001474
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001475 // save and restore curwin and curbuf, in case the autocmd changes them
1476 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001477 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001478 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001479 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001480 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1481 u_save(curwin->w_cursor.lnum,
1482 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001485 // Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1486 // TextChangedI will need to trigger for backwards compatibility, thus use
1487 // different b_last_changedtick* variables.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001488 if (ready && has_textchangedP()
1489 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1490 && pum_visible())
1491 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001492 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001493 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001494
1495 // save and restore curwin and curbuf, in case the autocmd changes them
1496 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001497 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001498 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001499 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001500 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1501 u_save(curwin->w_cursor.lnum,
1502 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001503 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001504
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001505 // Trigger SafeState if nothing is pending.
1506 may_trigger_safestate(ready
1507 && !ins_compl_active()
1508 && !pum_visible());
1509
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001510#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001511 if ((conceal_update_lines
1512 && (conceal_old_cursor_line != conceal_new_cursor_line
1513 || conceal_cursor_line(curwin)))
1514 || need_cursor_line_redraw)
1515 {
1516 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001517 redrawWinline(curwin, conceal_old_cursor_line);
1518 redrawWinline(curwin, conceal_new_cursor_line == 0
1519 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001520 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001521 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001522 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001523#endif
1524 if (must_redraw)
1525 update_screen(0);
1526 else if (clear_cmdline || redraw_cmdline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001527 showmode(); // clear cmdline and show mode
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001528 showruler(FALSE);
1529 setcursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001530 emsg_on_display = FALSE; // may remove error message now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531}
1532
1533/*
1534 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1535 */
1536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001537ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001540 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001542 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00001543 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
1545 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001548 did_putchar = TRUE;
1549 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001550 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552#ifdef FEAT_CMDL_INFO
1553 add_to_showcmd_c(Ctrl_V);
1554#endif
1555
Bram Moolenaar0684e362020-12-03 19:54:42 +01001556 // Do not change any modifyOtherKeys ESC sequence to a normal key for
1557 // CTRL-SHIFT-V.
1558 c = get_literal(mod_mask & MOD_MASK_SHIFT);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001559 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001560 // when the line fits in 'columns' the '^' is at the start of the next
1561 // line and will not removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001562 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563#ifdef FEAT_CMDL_INFO
1564 clear_showcmd();
1565#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 insert_special(c, FALSE, TRUE);
1568#ifdef FEAT_RIGHTLEFT
1569 revins_chars++;
1570 revins_legal++;
1571#endif
1572}
1573
1574/*
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001575 * After getting an ESC or CSI for a literal key: If the typeahead buffer
1576 * contains a modifyOtherKeys sequence then decode it and return the result.
1577 * Otherwise return "c".
1578 * Note that this doesn't wait for characters, they must be in the typeahead
1579 * buffer already.
1580 */
1581 int
1582decodeModifyOtherKeys(int c)
1583{
1584 char_u *p = typebuf.tb_buf + typebuf.tb_off;
1585 int idx;
1586 int form = 0;
1587 int argidx = 0;
1588 int arg[2] = {0, 0};
1589
1590 // Recognize:
1591 // form 0: {lead}{key};{modifier}u
1592 // form 1: {lead}27;{modifier};{key}~
Bram Moolenaar82aa6e02021-01-17 22:04:02 +01001593 if (typebuf.tb_len >= 4 && (c == CSI || (c == ESC && *p == '[')))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001594 {
1595 idx = (*p == '[');
1596 if (p[idx] == '2' && p[idx + 1] == '7' && p[idx + 2] == ';')
1597 {
1598 form = 1;
1599 idx += 3;
1600 }
1601 while (idx < typebuf.tb_len && argidx < 2)
1602 {
1603 if (p[idx] == ';')
1604 ++argidx;
1605 else if (VIM_ISDIGIT(p[idx]))
1606 arg[argidx] = arg[argidx] * 10 + (p[idx] - '0');
1607 else
1608 break;
1609 ++idx;
1610 }
1611 if (idx < typebuf.tb_len
1612 && p[idx] == (form == 1 ? '~' : 'u')
1613 && argidx == 1)
1614 {
1615 // Match, consume the code.
1616 typebuf.tb_off += idx + 1;
1617 typebuf.tb_len -= idx + 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001618#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1619 if (typebuf.tb_len == 0)
1620 typebuf_was_filled = FALSE;
1621#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001622
1623 mod_mask = decode_modifiers(arg[!form]);
Bram Moolenaar975a8802020-06-06 22:36:24 +02001624 c = merge_modifyOtherKeys(arg[form], &mod_mask);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001625 }
1626 }
1627
1628 return c;
1629}
1630
1631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 * Put a character directly onto the screen. It's not stored in a buffer.
1633 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1634 */
1635static int pc_status;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001636#define PC_STATUS_UNSET 0 // pc_bytes was not set
1637#define PC_STATUS_RIGHT 1 // right halve of double-wide char
1638#define PC_STATUS_LEFT 2 // left halve of double-wide char
1639#define PC_STATUS_SET 3 // pc_bytes was filled
1640static char_u pc_bytes[MB_MAXBYTES + 1]; // saved bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641static int pc_attr;
1642static int pc_row;
1643static int pc_col;
1644
1645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648 int attr;
1649
1650 if (ScreenLines != NULL)
1651 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001652 update_topline(); // just in case w_topline isn't valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 validate_cursor();
1654 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001655 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 else
1657 attr = 0;
1658 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001659 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661#ifdef FEAT_RIGHTLEFT
1662 if (curwin->w_p_rl)
1663 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001664 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (has_mbyte)
1666 {
1667 int fix_col = mb_fix_col(pc_col, pc_row);
1668
1669 if (fix_col != pc_col)
1670 {
1671 screen_putchar(' ', pc_row, fix_col, attr);
1672 --curwin->w_wcol;
1673 pc_status = PC_STATUS_RIGHT;
1674 }
1675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
1677 else
1678#endif
1679 {
1680 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (mb_lefthalve(pc_row, pc_col))
1682 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001685 // save the character to be able to put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1689 pc_status = PC_STATUS_SET;
1690 }
1691 screen_putchar(c, pc_row, pc_col, attr);
1692 }
1693}
1694
Bram Moolenaarf2732452018-06-03 14:47:35 +02001695/*
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001696 * Set the insert start position for when using a prompt buffer.
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001697 */
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001698 void
1699set_insstart(linenr_T lnum, int col)
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001700{
Bram Moolenaar8b5866d2020-09-05 15:48:51 +02001701 Insstart.lnum = lnum;
1702 Insstart.col = col;
1703 Insstart_orig = Insstart;
1704 Insstart_textlen = Insstart.col;
1705 Insstart_blank_vcol = MAXCOL;
1706 arrow_used = FALSE;
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001707}
1708
1709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 * Undo the previous edit_putchar().
1711 */
1712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001713edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (pc_status == PC_STATUS_RIGHT)
1718 ++curwin->w_wcol;
1719 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001720 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1723 }
1724}
1725
1726/*
1727 * Called when p_dollar is set: display a '$' at the end of the changed text
1728 * Only works when cursor is in the line that changes.
1729 */
1730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001731display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 colnr_T save_col;
1734
1735 if (!redrawing())
1736 return;
1737
1738 cursor_off();
1739 save_col = curwin->w_cursor.col;
1740 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 if (has_mbyte)
1742 {
1743 char_u *p;
1744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001745 // If on the last byte of a multi-byte move to the first byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 p = ml_get_curline();
1747 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1748 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001749 curs_columns(FALSE); // recompute w_wrow and w_wcol
Bram Moolenaar02631462017-09-22 15:20:32 +02001750 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
1752 edit_putchar('$', FALSE);
1753 dollar_vcol = curwin->w_virtcol;
1754 }
1755 curwin->w_cursor.col = save_col;
1756}
1757
1758/*
1759 * Call this function before moving the cursor from the normal insert position
1760 * in insert mode.
1761 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001765 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001767 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001768 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
1770}
1771
1772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 * Truncate the space at the end of a line. This is to be used only in an
1774 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1775 * modes.
1776 */
1777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
1780 int i;
1781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001782 // find start of trailing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +01001783 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
1785 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001786 replace_join(0); // remove a NUL from the replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788 line[i + 1] = NUL;
1789}
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791/*
1792 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1793 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001794 * Will attempt not to go before "col" even when there is a composing
1795 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
1797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001798backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
1800 while ((int)curwin->w_cursor.col > col)
1801 {
1802 curwin->w_cursor.col--;
1803 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001804 replace_do_bs(col);
1805 else if (!del_char_after_col(col))
1806 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001810/*
1811 * Like del_char(), but make sure not to go before column "limit_col".
1812 * Only matters when there are composing characters.
1813 * Return TRUE when something was deleted.
1814 */
1815 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001817{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001818 if (enc_utf8 && limit_col >= 0)
1819 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001820 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001822 // Make sure the cursor is at the start of a character, but
1823 // skip forward again when going too far back because of a
1824 // composing character.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001825 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001826 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001827 {
1828 int l = utf_ptr2len(ml_get_cursor());
1829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001830 if (l == 0) // end of line
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001831 break;
1832 curwin->w_cursor.col += l;
1833 }
1834 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1835 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001836 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001837 }
1838 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001839 (void)del_char(FALSE);
1840 return TRUE;
1841}
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
1844 * Next character is interpreted literally.
1845 * A one, two or three digit decimal number is interpreted as its byte value.
1846 * If one or two digits are entered, the next character is given to vungetc().
1847 * For Unicode a character > 255 may be returned.
Bram Moolenaar0684e362020-12-03 19:54:42 +01001848 * If "noReduceKeys" is TRUE do not change any modifyOtherKeys ESC sequence
1849 * into a normal key, return ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 */
1851 int
Bram Moolenaar0684e362020-12-03 19:54:42 +01001852get_literal(int noReduceKeys)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 int cc;
1855 int nc;
1856 int i;
1857 int hex = FALSE;
1858 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
1861 if (got_int)
1862 return Ctrl_C;
1863
1864#ifdef FEAT_GUI
1865 /*
1866 * In GUI there is no point inserting the internal code for a special key.
1867 * It is more useful to insert the string "<KEY>" instead. This would
1868 * probably be useful in a text window too, but it would not be
1869 * vi-compatible (maybe there should be an option for it?) -- webb
1870 */
1871 if (gui.in_use)
1872 ++allow_keys;
1873#endif
1874#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001875 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001877 ++no_mapping; // don't map the next key hits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 cc = 0;
1879 i = 0;
1880 for (;;)
1881 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001882 nc = plain_vgetc();
Bram Moolenaar0684e362020-12-03 19:54:42 +01001883 if ((nc == ESC || nc == CSI) && !noReduceKeys)
1884 nc = decodeModifyOtherKeys(nc);
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001887 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 add_to_showcmd(nc);
1889#endif
1890 if (nc == 'x' || nc == 'X')
1891 hex = TRUE;
1892 else if (nc == 'o' || nc == 'O')
1893 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 else if (nc == 'u' || nc == 'U')
1895 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 else
1897 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001898 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
1900 if (!vim_isxdigit(nc))
1901 break;
1902 cc = cc * 16 + hex2nr(nc);
1903 }
1904 else if (octal)
1905 {
1906 if (nc < '0' || nc > '7')
1907 break;
1908 cc = cc * 8 + nc - '0';
1909 }
1910 else
1911 {
1912 if (!VIM_ISDIGIT(nc))
1913 break;
1914 cc = cc * 10 + nc - '0';
1915 }
1916
1917 ++i;
1918 }
1919
Bram Moolenaar13505972019-01-24 15:04:48 +01001920 if (cc > 255 && unicode == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001921 cc = 255; // limit range to 0-255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 nc = 0;
1923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001924 if (hex) // hex: up to two chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
1926 if (i >= 2)
1927 break;
1928 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 else if (unicode) // Unicode: up to four or eight chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
1931 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1932 break;
1933 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001934 else if (i >= 3) // decimal or octal: up to three chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 break;
1936 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001937 if (i == 0) // no number entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001939 if (nc == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 cc = '\n';
1942 nc = 0;
1943 }
1944 else
1945 {
1946 cc = nc;
1947 nc = 0;
1948 }
1949 }
1950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001951 if (cc == 0) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001953 if (enc_dbcs && (cc & 0xff) == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001954 cc = '?'; // don't accept an illegal DBCS char, the NUL in the
1955 // second byte will cause trouble!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
1957 --no_mapping;
1958#ifdef FEAT_GUI
1959 if (gui.in_use)
1960 --allow_keys;
1961#endif
1962 if (nc)
1963 vungetc(nc);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001964 got_int = FALSE; // CTRL-C typed after CTRL-V is not an interrupt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 return cc;
1966}
1967
1968/*
1969 * Insert character, taking care of special keys and mod_mask
1970 */
1971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001972insert_special(
1973 int c,
1974 int allow_modmask,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001975 int ctrlv) // c was typed after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977 char_u *p;
1978 int len;
1979
1980 /*
1981 * Special function key, translate into "<Key>". Up to the last '>' is
1982 * inserted with ins_str(), so as not to replace characters in replace
1983 * mode.
1984 * Only use mod_mask for special keys, to avoid things like <S-Space>,
1985 * unless 'allow_modmask' is TRUE.
1986 */
Bram Moolenaard0573012017-10-28 21:11:06 +02001987#ifdef MACOS_X
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001988 // Command-key never produces a normal key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (mod_mask & MOD_MASK_CMD)
1990 allow_modmask = TRUE;
1991#endif
1992 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
1993 {
1994 p = get_special_key_name(c, mod_mask);
1995 len = (int)STRLEN(p);
1996 c = p[len - 1];
1997 if (len > 2)
1998 {
1999 if (stop_arrow() == FAIL)
2000 return;
2001 p[len - 1] = NUL;
2002 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002003 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 ctrlv = FALSE;
2005 }
2006 }
2007 if (stop_arrow() == OK)
2008 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2009}
2010
2011/*
2012 * Special characters in this context are those that need processing other
2013 * than the simple insertion that can be performed here. This includes ESC
2014 * which terminates the insert, and CR/NL which need special processing to
2015 * open up a new line. This routine tries to optimize insertions performed by
2016 * the "redo", "undo" or "put" commands, so it needs to know when it should
2017 * stop and defer processing to the "normal" mechanism.
2018 * '0' and '^' are special, because they can be followed by CTRL-D.
2019 */
2020#ifdef EBCDIC
2021# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2022#else
2023# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2024#endif
2025
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002026/*
2027 * "flags": INSCHAR_FORMAT - force formatting
2028 * INSCHAR_CTRLV - char typed just after CTRL-V
2029 * INSCHAR_NO_FEX - don't use 'formatexpr'
2030 *
2031 * NOTE: passes the flags value straight through to internal_format() which,
2032 * beside INSCHAR_FORMAT (above), is also looking for these:
2033 * INSCHAR_DO_COM - format comments
2034 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002037insertchar(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002038 int c, // character to insert or NUL
2039 int flags, // INSCHAR_FORMAT, etc.
2040 int second_indent) // indent for second line if >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002045 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002047 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 /*
2051 * Try to break the line in two or more pieces when:
2052 * - Always do this if we have been called to do formatting only.
2053 * - Always do this when 'formatoptions' has the 'a' flag and the line
2054 * ends in white space.
2055 * - Otherwise:
2056 * - Don't do this if inserting a blank
2057 * - Don't do this if an existing character is being replaced, unless
2058 * we're in VREPLACE mode.
2059 * - Do this if the cursor is not on the line where insert started
2060 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2061 * before the insert.
2062 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2063 * before 'textwidth'
2064 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002065 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002066 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002067 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 && *ml_get_cursor() != NUL)
2071 && (curwin->w_cursor.lnum != Insstart.lnum
2072 || ((!has_format_option(FO_INS_LONG)
2073 || Insstart_textlen <= (colnr_T)textwidth)
2074 && (!fo_ins_blank
2075 || Insstart_blank_vcol <= (colnr_T)textwidth
2076 ))))))
2077 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002078 // Format with 'formatexpr' when it's set. Use internal formatting
2079 // when 'formatexpr' isn't set or it returns non-zero.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002080#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002081 int do_internal = TRUE;
2082 colnr_T virtcol = get_nolist_virtcol()
2083 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002084
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002085 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2086 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002087 {
2088 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002089 // It may be required to save for undo again, e.g. when setline()
2090 // was called.
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002091 ins_need_undo = TRUE;
2092 }
2093 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002095 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002097
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002098 if (c == NUL) // only formatting was wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 return;
2100
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002101 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 if (did_ai && (int)c == end_comment_pending)
2103 {
2104 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002105 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 int middle_len, end_len;
2107 int i;
2108
2109 /*
2110 * Need to remove existing (middle) comment leader and insert end
2111 * comment leader. First, check what comment leader we can find.
2112 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002113 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002114 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002116 // Skip middle-comment string
2117 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 ++p;
2119 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002120 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002121 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 --middle_len;
2123
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002124 // Find the end-comment string
2125 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 ++p;
2127 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2128
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002129 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002131 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 ;
2133 i++;
2134
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002135 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 i -= middle_len;
2137
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002138 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2140 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002141 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 backspace_until_column(i);
2143
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002144 // Insert the end-comment string, except for the last
2145 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 ins_bytes_len(lead_end, end_len - 1);
2147 }
2148 }
2149 }
2150 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 did_ai = FALSE;
2153#ifdef FEAT_SMARTINDENT
2154 did_si = FALSE;
2155 can_si = FALSE;
2156 can_si_back = FALSE;
2157#endif
2158
2159 /*
2160 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2161 * This speeds up normal text input considerably.
2162 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2163 * need to re-indent at a ':', or any other character (but not what
2164 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002165 * Don't do this when there an InsertCharPre autocommand is defined,
2166 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002167 * Do the check for InsertCharPre before the call to vpeekc() because the
2168 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 */
2170#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002171 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172#endif
2173
2174 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002176 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 && vpeekc() != NUL
2178 && !(State & REPLACE_FLAG)
2179#ifdef FEAT_CINDENT
2180 && !cindent_on()
2181#endif
2182#ifdef FEAT_RIGHTLEFT
2183 && !p_ri
2184#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002185 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187#define INPUT_BUFLEN 100
2188 char_u buf[INPUT_BUFLEN + 1];
2189 int i;
2190 colnr_T virtcol = 0;
2191
2192 buf[0] = c;
2193 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002194 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 virtcol = get_nolist_virtcol();
2196 /*
2197 * Stop the string when:
2198 * - no more chars available
2199 * - finding a special character (command key)
2200 * - buffer is full
2201 * - running into the 'textwidth' boundary
2202 * - need to check for abbreviation: A non-word char after a word-char
2203 */
2204 while ( (c = vpeekc()) != NUL
2205 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 && i < INPUT_BUFLEN
2208 && (textwidth == 0
2209 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2210 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2211 {
2212#ifdef FEAT_RIGHTLEFT
2213 c = vgetc();
2214 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002215 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 buf[i++] = c;
2217#else
2218 buf[i++] = vgetc();
2219#endif
2220 }
2221
2222#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002223 do_digraph(-1); // clear digraphs
2224 do_digraph(buf[i-1]); // may be the start of a digraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225#endif
2226 buf[i] = NUL;
2227 ins_str(buf);
2228 if (flags & INSCHAR_CTRLV)
2229 {
2230 redo_literal(*buf);
2231 i = 1;
2232 }
2233 else
2234 i = 0;
2235 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002236 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
2238 else
2239 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002240 int cc;
2241
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2243 {
2244 char_u buf[MB_MAXBYTES + 1];
2245
2246 (*mb_char2bytes)(c, buf);
2247 buf[cc] = NUL;
2248 ins_char_bytes(buf, cc);
2249 AppendCharToRedobuff(c);
2250 }
2251 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
2253 ins_char(c);
2254 if (flags & INSCHAR_CTRLV)
2255 redo_literal(c);
2256 else
2257 AppendCharToRedobuff(c);
2258 }
2259 }
2260}
2261
2262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 * Put a character in the redo buffer, for when just after a CTRL-V.
2264 */
2265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002266redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 char_u buf[10];
2269
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002270 // Only digits need special treatment. Translate them into a string of
2271 // three digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (VIM_ISDIGIT(c))
2273 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002274 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 AppendToRedobuff(buf);
2276 }
2277 else
2278 AppendCharToRedobuff(c);
2279}
2280
2281/*
2282 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002283 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002286start_arrow(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002287 pos_T *end_insert_pos) // can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002289 start_arrow_common(end_insert_pos, TRUE);
2290}
2291
2292/*
2293 * Like start_arrow() but with end_change argument.
2294 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2295 */
2296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002297start_arrow_with_change(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002298 pos_T *end_insert_pos, // can be NULL
2299 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002300{
2301 start_arrow_common(end_insert_pos, end_change);
2302 if (!end_change)
2303 {
2304 AppendCharToRedobuff(Ctrl_G);
2305 AppendCharToRedobuff('U');
2306 }
2307}
2308
2309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002310start_arrow_common(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002311 pos_T *end_insert_pos, // can be NULL
2312 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002313{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002314 if (!arrow_used && end_change) // something has been inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002317 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002318 arrow_used = TRUE; // this means we stopped the current insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002320#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002321 check_spell_redraw();
2322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323}
2324
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002325#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002326/*
2327 * If we skipped highlighting word at cursor, do it now.
2328 * It may be skipped again, thus reset spell_redraw_lnum first.
2329 */
2330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002331check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002332{
2333 if (spell_redraw_lnum != 0)
2334 {
2335 linenr_T lnum = spell_redraw_lnum;
2336
2337 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002338 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002339 }
2340}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002341
Bram Moolenaar217ad922005-03-20 22:37:15 +00002342#endif
2343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344/*
2345 * stop_arrow() is called before a change is made in insert mode.
2346 * If an arrow key has been used, start a new insertion.
2347 * Returns FAIL if undo is impossible, shouldn't insert then.
2348 */
2349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002350stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
2352 if (arrow_used)
2353 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002354 Insstart = curwin->w_cursor; // new insertion starts here
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002355 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002356 // Don't update the original insert position when moved to the
2357 // right, except when nothing was inserted yet.
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002358 update_Insstart_orig = FALSE;
2359 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2360
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (u_save_cursor() == OK)
2362 {
2363 arrow_used = FALSE;
2364 ins_need_undo = FALSE;
2365 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (State & VREPLACE_FLAG)
2369 {
2370 orig_line_count = curbuf->b_ml.ml_line_count;
2371 vr_lines_changed = 1;
2372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 ResetRedobuff();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002374 AppendToRedobuff((char_u *)"1i"); // pretend we start an insertion
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002375 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377 else if (ins_need_undo)
2378 {
2379 if (u_save_cursor() == OK)
2380 ins_need_undo = FALSE;
2381 }
2382
2383#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002384 // Always open fold at the cursor line when inserting something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 foldOpenCursor();
2386#endif
2387
2388 return (arrow_used || ins_need_undo ? FAIL : OK);
2389}
2390
2391/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002392 * Do a few things to stop inserting.
2393 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2394 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 */
2396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002397stop_insert(
2398 pos_T *end_insert_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002399 int esc, // called by ins_esc()
2400 int nomove) // <c-\><c-o>, don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002402 int cc;
2403 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
2405 stop_redo_ins();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002406 replace_flush(); // abandon replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002409 * Save the inserted text for later redo with ^@ and CTRL-A.
2410 * Don't do it when "restart_edit" was set and nothing was inserted,
2411 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002413 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002414 if (did_restart_edit == 0 || (ptr != NULL
2415 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00002416 {
2417 vim_free(last_insert);
2418 last_insert = ptr;
2419 last_insert_skip = new_insert_skip;
2420 }
2421 else
2422 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002424 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002426 // Auto-format now. It may seem strange to do this when stopping an
2427 // insertion (or moving the cursor), but it's required when appending
2428 // a line and having it end in a space. But only do it when something
2429 // was actually inserted, otherwise undo won't work.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002430 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002432 pos_T tpos = curwin->w_cursor;
2433
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002434 // When the cursor is at the end of the line after a space the
2435 // formatting will move it to the following word. Avoid that by
2436 // moving the cursor onto the space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 cc = 'x';
2438 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
2439 {
2440 dec_cursor();
2441 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002442 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002443 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
2445
2446 auto_format(TRUE, FALSE);
2447
Bram Moolenaar1c465442017-03-12 20:10:05 +01002448 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002449 {
2450 if (gchar_cursor() != NUL)
2451 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002452 // If the cursor is still at the same character, also keep
2453 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002454 if (gchar_cursor() == NUL
2455 && curwin->w_cursor.lnum == tpos.lnum
2456 && curwin->w_cursor.col == tpos.col)
2457 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002461 // If a space was inserted for auto-formatting, remove it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 check_auto_format(TRUE);
2463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002464 // If we just did an auto-indent, remove the white space from the end
2465 // of the line, and put the cursor back.
2466 // Do this when ESC was used or moving the cursor up/down.
2467 // Check for the old position still being valid, just in case the text
2468 // got changed unexpectedly.
Bram Moolenaarf332a652013-11-04 04:20:33 +01002469 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00002470 && curwin->w_cursor.lnum != end_insert_pos->lnum))
2471 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002473 pos_T tpos = curwin->w_cursor;
2474
2475 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002476 check_cursor_col(); // make sure it is not past the line
Bram Moolenaar39f05632006-03-19 22:15:26 +00002477 for (;;)
2478 {
2479 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
2480 --curwin->w_cursor.col;
2481 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002482 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00002483 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00002484 if (del_char(TRUE) == FAIL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002485 break; // should not happen
Bram Moolenaar39f05632006-03-19 22:15:26 +00002486 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002487 if (curwin->w_cursor.lnum != tpos.lnum)
2488 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01002489 else
2490 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002491 // reset tpos, could have been invalidated in the loop above
Bram Moolenaaref6875b2014-11-12 18:59:25 +01002492 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01002493 tpos.col++;
2494 if (cc != NUL && gchar_pos(&tpos) == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002495 ++curwin->w_cursor.col; // put cursor back on the NUL
Bram Moolenaar2f31e392014-10-31 19:20:36 +01002496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002498 // <C-S-Right> may have started Visual mode, adjust the position for
2499 // deleted characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
2501 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002502 int len = (int)STRLEN(ml_get_curline());
2503
2504 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002506 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 }
2509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 }
2511 }
2512 did_ai = FALSE;
2513#ifdef FEAT_SMARTINDENT
2514 did_si = FALSE;
2515 can_si = FALSE;
2516 can_si_back = FALSE;
2517#endif
2518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002519 // Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
2520 // now in a different buffer.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002521 if (end_insert_pos != NULL)
2522 {
2523 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002524 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002525 curbuf->b_op_end = *end_insert_pos;
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527}
2528
2529/*
2530 * Set the last inserted text to a single character.
2531 * Used for the replace command.
2532 */
2533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002534set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 char_u *s;
2537
2538 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (last_insert != NULL)
2541 {
2542 s = last_insert;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002543 // Use the CTRL-V only when entering a special char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (c < ' ' || c == DEL)
2545 *s++ = Ctrl_V;
2546 s = add_char2buf(c, s);
2547 *s++ = ESC;
2548 *s++ = NUL;
2549 last_insert_skip = 0;
2550 }
2551}
2552
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002553#if defined(EXITFREE) || defined(PROTO)
2554 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002555free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002556{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002557 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002558}
2559#endif
2560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561/*
2562 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
2563 * and CSI. Handle multi-byte characters.
2564 * Returns a pointer to after the added bytes.
2565 */
2566 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002567add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002569 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 int i;
2571 int len;
2572
2573 len = (*mb_char2bytes)(c, temp);
2574 for (i = 0; i < len; ++i)
2575 {
2576 c = temp[i];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002577 // Need to escape K_SPECIAL and CSI like in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (c == K_SPECIAL)
2579 {
2580 *s++ = K_SPECIAL;
2581 *s++ = KS_SPECIAL;
2582 *s++ = KE_FILLER;
2583 }
2584#ifdef FEAT_GUI
2585 else if (c == CSI)
2586 {
2587 *s++ = CSI;
2588 *s++ = KS_EXTRA;
2589 *s++ = (int)KE_CSI;
2590 }
2591#endif
2592 else
2593 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 return s;
2596}
2597
2598/*
2599 * move cursor to start of line
2600 * if flags & BL_WHITE move to first non-white
2601 * if flags & BL_SOL move to first non-white if startofline is set,
2602 * otherwise keep "curswant" column
2603 * if flags & BL_FIX don't leave the cursor on a NUL.
2604 */
2605 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002606beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 if ((flags & BL_SOL) && !p_sol)
2609 coladvance(curwin->w_curswant);
2610 else
2611 {
2612 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
2615 if (flags & (BL_WHITE | BL_SOL))
2616 {
2617 char_u *ptr;
2618
Bram Moolenaar1c465442017-03-12 20:10:05 +01002619 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
2621 ++curwin->w_cursor.col;
2622 }
2623 curwin->w_set_curswant = TRUE;
2624 }
2625}
2626
2627/*
2628 * oneright oneleft cursor_down cursor_up
2629 *
2630 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002631 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 * Return OK when successful, FAIL when we hit a line of file boundary.
2633 */
2634
2635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002636oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (virtual_active())
2642 {
2643 pos_T prevpos = curwin->w_cursor;
2644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002645 // Adjust for multi-wide char (excluding TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01002647 coladvance(getviscol() + ((*ptr != TAB
2648 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 ? ptr2cells(ptr) : 1));
2650 curwin->w_set_curswant = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002651 // Return OK if the cursor moved, FAIL otherwise (at window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 return (prevpos.col != curwin->w_cursor.col
2653 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
2654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002657 if (*ptr == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002658 return FAIL; // already at the very end
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002659
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002660 if (has_mbyte)
2661 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002663 l = 1;
2664
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002665 // move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
2666 // contains "onemore".
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002667 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00002668 return FAIL;
2669 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
2671 curwin->w_set_curswant = TRUE;
2672 return OK;
2673}
2674
2675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002676oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (virtual_active())
2679 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002680#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 int v = getviscol();
2684
2685 if (v == 0)
2686 return FAIL;
2687
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002688#ifdef FEAT_LINEBREAK
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002689 // We might get stuck on 'showbreak', skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 width = 1;
2691 for (;;)
2692 {
2693 coladvance(v - width);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002694 // getviscol() is slow, skip it when 'showbreak' is empty,
2695 // 'breakindent' is not set and there are no multi-byte
2696 // characters
Bram Moolenaaree857022019-11-09 23:26:40 +01002697 if ((*get_showbreak_value(curwin) == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01002698 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 break;
2700 ++width;
2701 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002702#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
2706 if (curwin->w_cursor.coladd == 1)
2707 {
2708 char_u *ptr;
2709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002710 // Adjust for multi-wide char (not a TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01002712 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
2713 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 curwin->w_cursor.coladd = 0;
2715 }
2716
2717 curwin->w_set_curswant = TRUE;
2718 return OK;
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 if (curwin->w_cursor.col == 0)
2722 return FAIL;
2723
2724 curwin->w_set_curswant = TRUE;
2725 --curwin->w_cursor.col;
2726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002727 // if the character on the left of the current cursor is a multi-byte
2728 // character, move to its first byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if (has_mbyte)
2730 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 return OK;
2732}
2733
2734 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002735cursor_up(
2736 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002737 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738{
2739 linenr_T lnum;
2740
2741 if (n > 0)
2742 {
2743 lnum = curwin->w_cursor.lnum;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002744 // This fails if the cursor is already in the first line or the count
2745 // is larger than the line number and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00002746 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 return FAIL;
2748 if (n >= lnum)
2749 lnum = 1;
2750 else
2751#ifdef FEAT_FOLDING
2752 if (hasAnyFolding(curwin))
2753 {
2754 /*
2755 * Count each sequence of folded lines as one logical line.
2756 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002757 // go to the start of the current fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 (void)hasFolding(lnum, &lnum, NULL);
2759
2760 while (n--)
2761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002762 // move up one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 --lnum;
2764 if (lnum <= 1)
2765 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002766 // If we entered a fold, move to the beginning, unless in
2767 // Insert mode or when 'foldopen' contains "all": it will open
2768 // in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
2770 (void)hasFolding(lnum, &lnum, NULL);
2771 }
2772 if (lnum < 1)
2773 lnum = 1;
2774 }
2775 else
2776#endif
2777 lnum -= n;
2778 curwin->w_cursor.lnum = lnum;
2779 }
2780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002781 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 coladvance(curwin->w_curswant);
2783
2784 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002785 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 return OK;
2788}
2789
2790/*
2791 * Cursor down a number of logical lines.
2792 */
2793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794cursor_down(
2795 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002796 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 linenr_T lnum;
2799
2800 if (n > 0)
2801 {
2802 lnum = curwin->w_cursor.lnum;
2803#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002804 // Move to last line of fold, will fail if it's the end-of-file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 (void)hasFolding(lnum, NULL, &lnum);
2806#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002807 // This fails if the cursor is already in the last line or would move
2808 // beyond the last line and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00002809 if (lnum >= curbuf->b_ml.ml_line_count
2810 || (lnum + n > curbuf->b_ml.ml_line_count
2811 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 return FAIL;
2813 if (lnum + n >= curbuf->b_ml.ml_line_count)
2814 lnum = curbuf->b_ml.ml_line_count;
2815 else
2816#ifdef FEAT_FOLDING
2817 if (hasAnyFolding(curwin))
2818 {
2819 linenr_T last;
2820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002821 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 while (n--)
2823 {
2824 if (hasFolding(lnum, NULL, &last))
2825 lnum = last + 1;
2826 else
2827 ++lnum;
2828 if (lnum >= curbuf->b_ml.ml_line_count)
2829 break;
2830 }
2831 if (lnum > curbuf->b_ml.ml_line_count)
2832 lnum = curbuf->b_ml.ml_line_count;
2833 }
2834 else
2835#endif
2836 lnum += n;
2837 curwin->w_cursor.lnum = lnum;
2838 }
2839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002840 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 coladvance(curwin->w_curswant);
2842
2843 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002844 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
2846 return OK;
2847}
2848
2849/*
2850 * Stuff the last inserted text in the read buffer.
2851 * Last_insert actually is a copy of the redo buffer, so we
2852 * first have to remove the command.
2853 */
2854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002855stuff_inserted(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002856 int c, // Command character to be inserted
2857 long count, // Repeat this many times
2858 int no_esc) // Don't add an ESC at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860 char_u *esc_ptr;
2861 char_u *ptr;
2862 char_u *last_ptr;
2863 char_u last = NUL;
2864
2865 ptr = get_last_insert();
2866 if (ptr == NULL)
2867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002868 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 return FAIL;
2870 }
2871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002872 // may want to stuff the command character, to start Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 if (c != NUL)
2874 stuffcharReadbuff(c);
2875 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002876 *esc_ptr = NUL; // remove the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002878 // when the last char is either "0" or "^" it will be quoted if no ESC
2879 // comes after it OR if it will inserted more than once and "ptr"
2880 // starts with ^D. -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
2882 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
2883 && (no_esc || (*ptr == Ctrl_D && count > 1)))
2884 {
2885 last = *last_ptr;
2886 *last_ptr = NUL;
2887 }
2888
2889 do
2890 {
2891 stuffReadbuff(ptr);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002892 // a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 if (last)
2894 stuffReadbuff((char_u *)(last == '0'
2895 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
2896 : IF_EB("\026^", CTRL_V_STR "^")));
2897 }
2898 while (--count > 0);
2899
2900 if (last)
2901 *last_ptr = last;
2902
2903 if (esc_ptr != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002904 *esc_ptr = ESC; // put the ESC back
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002906 // may want to stuff a trailing ESC, to get out of Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 if (!no_esc)
2908 stuffcharReadbuff(ESC);
2909
2910 return OK;
2911}
2912
2913 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
2916 if (last_insert == NULL)
2917 return NULL;
2918 return last_insert + last_insert_skip;
2919}
2920
2921/*
2922 * Get last inserted string, and remove trailing <Esc>.
2923 * Returns pointer to allocated memory (must be freed) or NULL.
2924 */
2925 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 char_u *s;
2929 int len;
2930
2931 if (last_insert == NULL)
2932 return NULL;
2933 s = vim_strsave(last_insert + last_insert_skip);
2934 if (s != NULL)
2935 {
2936 len = (int)STRLEN(s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002937 if (len > 0 && s[len - 1] == ESC) // remove trailing ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 s[len - 1] = NUL;
2939 }
2940 return s;
2941}
2942
2943/*
2944 * Check the word in front of the cursor for an abbreviation.
2945 * Called when the non-id character "c" has been entered.
2946 * When an abbreviation is recognized it is removed from the text and
2947 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
2948 */
2949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002950echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002952 // Don't check for abbreviation in paste mode, when disabled and just
2953 // after moving around with cursor keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 if (p_paste || no_abbr || arrow_used)
2955 return FALSE;
2956
2957 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
2958 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
2959}
2960
2961/*
2962 * replace-stack functions
2963 *
2964 * When replacing characters, the replaced characters are remembered for each
2965 * new character. This is used to re-insert the old text when backspacing.
2966 *
2967 * There is a NUL headed list of characters for each character that is
2968 * currently in the file after the insertion point. When BS is used, one NUL
2969 * headed list is put back for the deleted character.
2970 *
2971 * For a newline, there are two NUL headed lists. One contains the characters
2972 * that the NL replaced. The extra one stores the characters after the cursor
2973 * that were deleted (always white space).
2974 *
2975 * Replace_offset is normally 0, in which case replace_push will add a new
2976 * character at the end of the stack. If replace_offset is not 0, that many
2977 * characters will be left on the stack above the newly inserted character.
2978 */
2979
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002980static char_u *replace_stack = NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002981static long replace_stack_nr = 0; // next entry in replace stack
2982static long replace_stack_len = 0; // max. number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
2984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002985replace_push(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002986 int c) // character that is replaced (NUL is none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 char_u *p;
2989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002990 if (replace_stack_nr < replace_offset) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 return;
2992 if (replace_stack_len <= replace_stack_nr)
2993 {
2994 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002995 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002996 if (p == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 {
2998 replace_stack_len -= 50;
2999 return;
3000 }
3001 if (replace_stack != NULL)
3002 {
3003 mch_memmove(p, replace_stack,
3004 (size_t)(replace_stack_nr * sizeof(char_u)));
3005 vim_free(replace_stack);
3006 }
3007 replace_stack = p;
3008 }
3009 p = replace_stack + replace_stack_nr - replace_offset;
3010 if (replace_offset)
3011 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3012 *p = c;
3013 ++replace_stack_nr;
3014}
3015
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003016/*
3017 * Push a character onto the replace stack. Handles a multi-byte character in
3018 * reverse byte order, so that the first byte is popped off first.
3019 * Return the number of bytes done (includes composing characters).
3020 */
3021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003022replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003023{
3024 int l = (*mb_ptr2len)(p);
3025 int j;
3026
3027 for (j = l - 1; j >= 0; --j)
3028 replace_push(p[j]);
3029 return l;
3030}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003031
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032/*
3033 * Pop one item from the replace stack.
3034 * return -1 if stack empty
3035 * return replaced character or NUL otherwise
3036 */
3037 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003038replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 if (replace_stack_nr == 0)
3041 return -1;
3042 return (int)replace_stack[--replace_stack_nr];
3043}
3044
3045/*
3046 * Join the top two items on the replace stack. This removes to "off"'th NUL
3047 * encountered.
3048 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003050replace_join(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003051 int off) // offset for which NUL to remove
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 int i;
3054
3055 for (i = replace_stack_nr; --i >= 0; )
3056 if (replace_stack[i] == NUL && off-- <= 0)
3057 {
3058 --replace_stack_nr;
3059 mch_memmove(replace_stack + i, replace_stack + i + 1,
3060 (size_t)(replace_stack_nr - i));
3061 return;
3062 }
3063}
3064
3065/*
3066 * Pop bytes from the replace stack until a NUL is found, and insert them
3067 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3068 */
3069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003070replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 int cc;
3073 int oldState = State;
3074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003075 State = NORMAL; // don't want REPLACE here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 while ((cc = replace_pop()) > 0)
3077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 dec_cursor();
3080 }
3081 State = oldState;
3082}
3083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084/*
3085 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3086 * indicates a multi-byte char, pop the other bytes too.
3087 */
3088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003089mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003092 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 int i;
3094 int c;
3095
3096 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3097 {
3098 buf[0] = cc;
3099 for (i = 1; i < n; ++i)
3100 buf[i] = replace_pop();
3101 ins_bytes_len(buf, n);
3102 }
3103 else
3104 ins_char(cc);
3105
3106 if (enc_utf8)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003107 // Handle composing chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 for (;;)
3109 {
3110 c = replace_pop();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003111 if (c == -1) // stack empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 break;
3113 if ((n = MB_BYTE2LEN(c)) == 1)
3114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003115 // Not a multi-byte char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 replace_push(c);
3117 break;
3118 }
3119 else
3120 {
3121 buf[0] = c;
3122 for (i = 1; i < n; ++i)
3123 buf[i] = replace_pop();
3124 if (utf_iscomposing(utf_ptr2char(buf)))
3125 ins_bytes_len(buf, n);
3126 else
3127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003128 // Not a composing char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 for (i = n - 1; i >= 0; --i)
3130 replace_push(buf[i]);
3131 break;
3132 }
3133 }
3134 }
3135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
3137/*
3138 * make the replace stack empty
3139 * (called when exiting replace mode)
3140 */
3141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003142replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003144 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 replace_stack_len = 0;
3146 replace_stack_nr = 0;
3147}
3148
3149/*
3150 * Handle doing a BS for one character.
3151 * cc < 0: replace stack empty, just move cursor
3152 * cc == 0: character was inserted, delete it
3153 * cc > 0: character was replaced, put cc (first byte of original char) back
3154 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003155 * When "limit_col" is >= 0, don't delete before this column. Matters when
3156 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 */
3158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003159replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
3161 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 int orig_len = 0;
3163 int ins_len;
3164 int orig_vcols = 0;
3165 colnr_T start_vcol;
3166 char_u *p;
3167 int i;
3168 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 cc = replace_pop();
3171 if (cc > 0)
3172 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003173#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003174 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003175
3176 if (curbuf->b_has_textprop)
3177 {
3178 // Do not adjust text properties for individual delete and insert
3179 // operations, do it afterwards on the resulting text.
3180 len_before = STRLEN(ml_get_curline());
3181 ++text_prop_frozen;
3182 }
3183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (State & VREPLACE_FLAG)
3185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003186 // Get the number of screen cells used by the character we are
3187 // going to delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3189 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (has_mbyte)
3192 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003193 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003195 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 replace_push(cc);
3197 }
3198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
3200 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003202 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204 replace_pop_ins();
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (State & VREPLACE_FLAG)
3207 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003208 // Get the number of screen cells used by the inserted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003210 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 vcol = start_vcol;
3212 for (i = 0; i < ins_len; ++i)
3213 {
3214 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003215 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
3217 vcol -= start_vcol;
3218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003219 // Delete spaces that were inserted after the cursor to keep the
3220 // text aligned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 curwin->w_cursor.col += ins_len;
3222 while (vcol > orig_vcols && gchar_cursor() == ' ')
3223 {
3224 del_char(FALSE);
3225 ++orig_vcols;
3226 }
3227 curwin->w_cursor.col -= ins_len;
3228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaar196d1572019-01-02 23:47:18 +01003230 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003232
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003233#ifdef FEAT_PROP_POPUP
Bram Moolenaar196d1572019-01-02 23:47:18 +01003234 if (curbuf->b_has_textprop)
3235 {
3236 size_t len_now = STRLEN(ml_get_curline());
3237
3238 --text_prop_frozen;
3239 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003240 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003241 }
3242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003245 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246}
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3249/*
3250 * Map Hebrew keyboard when in hkmap mode.
3251 */
3252 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003255 if (p_hkmapp) // phonetic mapping, by Ilya Dogolazky
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
3257 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3258 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3259 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3260 static char_u map[26] =
3261 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3262 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3263 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3264 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3265 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3266 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3267 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3268 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3269 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3270
3271 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3272 return (int)(map[CharOrd(c)] - 1 + p_aleph);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003273 // '-1'='sofit'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 else if (c == 'x')
3275 return 'X';
3276 else if (c == 'q')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003277 return '\''; // {geresh}={'}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 else if (c == 246)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003279 return ' '; // \"o --> ' ' for a german keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 else if (c == 228)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003281 return ' '; // \"a --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 else if (c == 252)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003283 return ' '; // \"u --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284#ifdef EBCDIC
3285 else if (islower(c))
3286#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003287 // NOTE: islower() does not do the right thing for us on Linux so we
3288 // do this the same was as 5.7 and previous, so it works correctly on
3289 // all systems. Specifically, the e.g. Delete and Arrow keys are
3290 // munged and won't work if e.g. searching for Hebrew text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 else if (c >= 'a' && c <= 'z')
3292#endif
3293 return (int)(map[CharOrdLow(c)] + p_aleph);
3294 else
3295 return c;
3296 }
3297 else
3298 {
3299 switch (c)
3300 {
3301 case '`': return ';';
3302 case '/': return '.';
3303 case '\'': return ',';
3304 case 'q': return '/';
3305 case 'w': return '\'';
3306
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003307 // Hebrew letters - set offset from 'a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 case ',': c = '{'; break;
3309 case '.': c = 'v'; break;
3310 case ';': c = 't'; break;
3311 default: {
3312 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3313
3314#ifdef EBCDIC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003315 // see note about islower() above
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 if (!islower(c))
3317#else
3318 if (c < 'a' || c > 'z')
3319#endif
3320 return c;
3321 c = str[CharOrdLow(c)];
3322 break;
3323 }
3324 }
3325
3326 return (int)(CharOrdLow(c) + p_aleph);
3327 }
3328}
3329#endif
3330
3331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003332ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 int need_redraw = FALSE;
3335 int regname;
3336 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003337 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
3339 /*
3340 * If we are going to wait for a character, show a '"'.
3341 */
3342 pc_status = PC_STATUS_UNSET;
3343 if (redrawing() && !char_avail())
3344 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003345 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00003346 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348 edit_putchar('"', TRUE);
3349#ifdef FEAT_CMDL_INFO
3350 add_to_showcmd_c(Ctrl_R);
3351#endif
3352 }
3353
3354#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003355 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#endif
3357
3358 /*
3359 * Don't map the register name. This also prevents the mode message to be
3360 * deleted when ESC is hit.
3361 */
3362 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003363 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003364 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3367 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003368 // Get a third key for literal register insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 literally = regname;
3370#ifdef FEAT_CMDL_INFO
3371 add_to_showcmd_c(literally);
3372#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003373 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003377 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
3379#ifdef FEAT_EVAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003380 // Don't call u_sync() while typing the expression or giving an error
3381 // message for it. Only call it explicitly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 ++no_u_sync;
3383 if (regname == '=')
3384 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003385 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003386# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003388# endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003389 // Sync undo when evaluating the expression calls setline() or
3390 // append(), so that it can be undone separately.
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003391 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003394
3395 // Cursor may be moved back a column.
3396 curwin->w_cursor = curpos;
3397 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003398# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003399 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (im_on)
3401 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00003404 if (regname == NUL || !valid_yank_reg(regname, FALSE))
3405 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003406 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003407 need_redraw = TRUE; // remove the '"'
Bram Moolenaar677ee682005-01-27 14:41:15 +00003408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 else
3410 {
3411#endif
3412 if (literally == Ctrl_O || literally == Ctrl_P)
3413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003414 // Append the command to the redo buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 AppendCharToRedobuff(Ctrl_R);
3416 AppendCharToRedobuff(literally);
3417 AppendCharToRedobuff(regname);
3418
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003419 do_put(regname, NULL, BACKWARD, 1L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
3421 }
3422 else if (insert_reg(regname, literally) == FAIL)
3423 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003424 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003425 need_redraw = TRUE; // remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003427 else if (stop_insert_mode)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003428 // When the '=' register was used and a function was invoked that
3429 // did ":stopinsert" then stuff_empty() returns FALSE but we won't
3430 // insert anything, need to remove the '"'
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003431 need_redraw = TRUE;
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433#ifdef FEAT_EVAL
3434 }
3435 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003436 if (u_sync_once == 1)
3437 ins_need_undo = TRUE;
3438 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439#endif
3440#ifdef FEAT_CMDL_INFO
3441 clear_showcmd();
3442#endif
3443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003444 // If the inserted register is empty, we need to remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (need_redraw || stuff_empty())
3446 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003448 // Disallow starting Visual mode here, would get a weird mode.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003449 if (!vis_active && VIsual_active)
3450 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451}
3452
3453/*
3454 * CTRL-G commands in Insert mode.
3455 */
3456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003457ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 int c;
3460
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003461 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 /*
3465 * Don't map the second key. This also prevents the mode message to be
3466 * deleted when ESC is hit.
3467 */
3468 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003469 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003470 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003472 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 switch (c)
3474 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003475 // CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 case K_UP:
3477 case Ctrl_K:
3478 case 'k': ins_up(TRUE);
3479 break;
3480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003481 // CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 case K_DOWN:
3483 case Ctrl_J:
3484 case 'j': ins_down(TRUE);
3485 break;
3486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003487 // CTRL-G u: start new undoable edit
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003488 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003490
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003491 // Need to reset Insstart, esp. because a BS that joins
3492 // a line to the previous one must save for undo.
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003493 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003494 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 break;
3496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003497 // CTRL-G U: do not break undo with the next char
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003498 case 'U':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003499 // Allow one left/right cursor movement with the next char,
3500 // without breaking undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02003501 dont_sync_undo = MAYBE;
3502 break;
3503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003504 // Unknown CTRL-G command, reserved for future expansion.
Bram Moolenaar165bc692015-07-21 17:53:25 +02003505 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507}
3508
3509/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003510 * CTRL-^ in Insert mode.
3511 */
3512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003513ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003514{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003515 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003516 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003517 // ":lmap" mappings exists, Toggle use of ":lmap" mappings.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003518 if (State & LANGMAP)
3519 {
3520 curbuf->b_p_iminsert = B_IMODE_NONE;
3521 State &= ~LANGMAP;
3522 }
3523 else
3524 {
3525 curbuf->b_p_iminsert = B_IMODE_LMAP;
3526 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003527#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003528 im_set_active(FALSE);
3529#endif
3530 }
3531 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003532#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003533 else
3534 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003535 // There are no ":lmap" mappings, toggle IM
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003536 if (im_get_status())
3537 {
3538 curbuf->b_p_iminsert = B_IMODE_NONE;
3539 im_set_active(FALSE);
3540 }
3541 else
3542 {
3543 curbuf->b_p_iminsert = B_IMODE_IM;
3544 State &= ~LANGMAP;
3545 im_set_active(TRUE);
3546 }
3547 }
3548#endif
3549 set_iminsert_global();
3550 showmode();
3551#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003552 // may show different cursor shape or color
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 if (gui.in_use)
3554 gui_update_cursor(TRUE, FALSE);
3555#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003556#if defined(FEAT_KEYMAP)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003557 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003558 status_redraw_curbuf();
3559#endif
3560}
3561
3562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 * Handle ESC in insert mode.
3564 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
3565 * insert.
3566 */
3567 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568ins_esc(
3569 long *count,
3570 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003571 int nomove) // don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
3573 int temp;
3574 static int disabled_redraw = FALSE;
3575
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003576#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003577 check_spell_redraw();
3578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
3580 temp = curwin->w_cursor.col;
3581 if (disabled_redraw)
3582 {
3583 --RedrawingDisabled;
3584 disabled_redraw = FALSE;
3585 }
3586 if (!arrow_used)
3587 {
3588 /*
3589 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00003590 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
3591 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 */
3593 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00003594 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 /*
3597 * Repeating insert may take a long time. Check for
3598 * interrupt now and then.
3599 */
3600 if (*count > 0)
3601 {
3602 line_breakcheck();
3603 if (got_int)
3604 *count = 0;
3605 }
3606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003607 if (--*count > 0) // repeat what was typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003609 // Vi repeats the insert without replacing characters.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003610 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
3611 State &= ~REPLACE_FLAG;
3612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 (void)start_redo_ins();
3614 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003615 stuffRedoReadbuff(ESC_STR); // no ESC in redo buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 ++RedrawingDisabled;
3617 disabled_redraw = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003618 return FALSE; // repeat the insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01003620 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 undisplay_dollar();
3622 }
3623
Bram Moolenaarb53e13a2020-10-21 12:19:53 +02003624 if (cmdchar != 'r' && cmdchar != 'v')
3625 ins_apply_autocmds(EVENT_INSERTLEAVEPRE);
3626
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003627 // When an autoindent was removed, curswant stays after the
3628 // indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
3630 curwin->w_set_curswant = TRUE;
3631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003632 // Remember the last Insert position in the '^ mark.
Bram Moolenaare1004402020-10-24 20:49:43 +02003633 if ((cmdmod.cmod_flags & CMOD_KEEPJUMPS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 curbuf->b_last_insert = curwin->w_cursor;
3635
3636 /*
3637 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00003638 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00003640 if (!nomove
3641 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003642 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003643 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003644 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645#ifdef FEAT_RIGHTLEFT
3646 && !revins_on
3647#endif
3648 )
3649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
3651 {
3652 oneleft();
3653 if (restart_edit != NUL)
3654 ++curwin->w_cursor.coladd;
3655 }
3656 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003659 // Correct cursor for multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 if (has_mbyte)
3661 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663 }
3664
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003665#ifdef HAVE_INPUT_METHOD
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003666 // Disable IM to allow typing English directly for Normal mode commands.
3667 // When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
3668 // well).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (!(State & LANGMAP))
3670 im_save_status(&curbuf->b_p_iminsert);
3671 im_set_active(FALSE);
3672#endif
3673
3674 State = NORMAL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003675 // need to position cursor again (e.g. when on a TAB )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 changed_cline_bef_curs();
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679#ifdef CURSOR_SHAPE
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003680 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01003682 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003683 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003684#ifdef FEAT_JOB_CHANNEL
3685 ch_log_output = TRUE;
3686#endif
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003687 // Re-enable bracketed paste mode.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01003688 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003690 // Re-enable modifyOtherKeys.
3691 out_str(T_CTI);
3692 }
3693
Bram Moolenaarad3ec762019-04-21 00:00:13 +02003694 // When recording or for CTRL-O, need to display the new mode.
3695 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02003696 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02003698 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003699 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
Bram Moolenaar177c9f22019-11-06 13:59:16 +01003701 return TRUE; // exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702}
3703
3704#ifdef FEAT_RIGHTLEFT
3705/*
3706 * Toggle language: hkmap and revins_on.
3707 * Move to end of reverse inserted text.
3708 */
3709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 if (revins_on && revins_chars && revins_scol >= 0)
3713 {
3714 while (gchar_cursor() != NUL && revins_chars--)
3715 ++curwin->w_cursor.col;
3716 }
3717 p_ri = !p_ri;
3718 revins_on = (State == INSERT && p_ri);
3719 if (revins_on)
3720 {
3721 revins_scol = curwin->w_cursor.col;
3722 revins_legal++;
3723 revins_chars = 0;
3724 undisplay_dollar();
3725 }
3726 else
3727 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003728 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 showmode();
3730}
3731#endif
3732
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733/*
3734 * If 'keymodel' contains "startsel", may start selection.
3735 * Returns TRUE when a CTRL-O and other keys stuffed.
3736 */
3737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003738ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 if (km_startsel)
3741 switch (c)
3742 {
3743 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 case K_PAGEUP:
3746 case K_KPAGEUP:
3747 case K_PAGEDOWN:
3748 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02003749# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 case K_LEFT:
3751 case K_RIGHT:
3752 case K_UP:
3753 case K_DOWN:
3754 case K_END:
3755 case K_HOME:
3756# endif
3757 if (!(mod_mask & MOD_MASK_SHIFT))
3758 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003759 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 case K_S_LEFT:
3761 case K_S_RIGHT:
3762 case K_S_UP:
3763 case K_S_DOWN:
3764 case K_S_END:
3765 case K_S_HOME:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003766 // Start selection right away, the cursor can move with
3767 // CTRL-O when beyond the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 start_selection();
3769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003770 // Execute the key in (insert) Select mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 stuffcharReadbuff(Ctrl_O);
3772 if (mod_mask)
3773 {
3774 char_u buf[4];
3775
3776 buf[0] = K_SPECIAL;
3777 buf[1] = KS_MODIFIER;
3778 buf[2] = mod_mask;
3779 buf[3] = NUL;
3780 stuffReadbuff(buf);
3781 }
3782 stuffcharReadbuff(c);
3783 return TRUE;
3784 }
3785 return FALSE;
3786}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
3788/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003789 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003790 */
3791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003792ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003793{
Bram Moolenaar14184a32019-02-16 15:10:30 +01003794#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003795 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02003796 (char_u *)((State & REPLACE_FLAG) ? "i"
3797 : replaceState == VREPLACE ? "v"
3798 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01003799#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02003800 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003801 if (State & REPLACE_FLAG)
3802 State = INSERT | (State & LANGMAP);
3803 else
3804 State = replaceState | (State & LANGMAP);
3805 AppendCharToRedobuff(K_INS);
3806 showmode();
3807#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003808 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003809#endif
3810}
3811
3812/*
3813 * Pressed CTRL-O in Insert mode.
3814 */
3815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003816ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003818 if (State & VREPLACE_FLAG)
3819 restart_edit = 'V';
Bram Moolenaar0684e362020-12-03 19:54:42 +01003820 else if (State & REPLACE_FLAG)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003821 restart_edit = 'R';
3822 else
3823 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003824 if (virtual_active())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003825 ins_at_eol = FALSE; // cursor always keeps its column
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003827 ins_at_eol = (gchar_cursor() == NUL);
3828}
3829
3830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 * If the cursor is on an indent, ^T/^D insert/delete one
3832 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00003833 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 * with vi. But vi only supports ^T and ^D after an
3835 * autoindent, we support it everywhere.
3836 */
3837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003838ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
3840 if (stop_arrow() == FAIL)
3841 return;
3842 AppendCharToRedobuff(c);
3843
3844 /*
3845 * 0^D and ^^D: remove all indent.
3846 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00003847 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
3848 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
3850 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003851 (void)del_char(FALSE); // delete the '^' or '0'
3852 // In Replace mode, restore the characters that '^' or '0' replaced.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (State & REPLACE_FLAG)
3854 replace_pop_ins();
3855 if (lastc == '^')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003856 old_indent = get_indent(); // remember curr. indent
Bram Moolenaar21b17e72008-01-16 19:03:13 +00003857 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00003860 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
3863 did_ai = FALSE;
3864#ifdef FEAT_SMARTINDENT
3865 did_si = FALSE;
3866 can_si = FALSE;
3867 can_si_back = FALSE;
3868#endif
3869#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003870 can_cindent = FALSE; // no cindenting after ^D or ^T
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871#endif
3872}
3873
3874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003875ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
3877 int temp;
3878
3879 if (stop_arrow() == FAIL)
3880 return;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 if (gchar_cursor() == NUL) // delete newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 temp = curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003884 if (!can_bs(BS_EOL) // only if "eol" included
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02003885 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003886 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01003888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 curwin->w_cursor.col = temp;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003890 // Adjust orig_line_count in case more lines have been deleted than
3891 // have been added. That makes sure, that open_line() later
3892 // can access all buffer lines correctly
Bram Moolenaar63e82db2018-03-06 12:10:48 +01003893 if (State & VREPLACE_FLAG &&
3894 orig_line_count > curbuf->b_ml.ml_line_count)
3895 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01003896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003898 else if (del_char(FALSE) == FAIL) // delete char under cursor
Bram Moolenaar165bc692015-07-21 17:53:25 +02003899 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 did_ai = FALSE;
3901#ifdef FEAT_SMARTINDENT
3902 did_si = FALSE;
3903 can_si = FALSE;
3904 can_si_back = FALSE;
3905#endif
3906 AppendCharToRedobuff(K_DEL);
3907}
3908
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003909/*
3910 * Delete one character for ins_bs().
3911 */
3912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003913ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003914{
3915 dec_cursor();
3916 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
3917 if (State & REPLACE_FLAG)
3918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003919 // Don't delete characters before the insert point when in
3920 // Replace mode
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003921 if (curwin->w_cursor.lnum != Insstart.lnum
3922 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003923 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00003924 }
3925 else
3926 (void)del_char(FALSE);
3927}
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929/*
3930 * Handle Backspace, delete-word and delete-line in Insert mode.
3931 * Return TRUE when backspace was actually used.
3932 */
3933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003934ins_bs(
3935 int c,
3936 int mode,
3937 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939 linenr_T lnum;
3940 int cc;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003941 int temp = 0; // init for GCC
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003942 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 colnr_T mincol;
3944 int did_backspace = FALSE;
3945 int in_indent;
3946 int oldState;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003947 int cpc[MAX_MCO]; // composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
3949 /*
3950 * can't delete anything in an empty file
3951 * can't backup past first character in buffer
3952 * can't backup past starting point unless 'backspace' > 1
3953 * can backup to a previous line if 'backspace' == 0
3954 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003955 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 || (
3957#ifdef FEAT_RIGHTLEFT
3958 !revins_on &&
3959#endif
3960 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
3961 || (!can_bs(BS_START)
Bram Moolenaar6f624482020-11-11 20:52:40 +01003962 && ((arrow_used
3963#ifdef FEAT_JOB_CHANNEL
3964 && !bt_prompt(curbuf)
3965#endif
3966 ) || (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02003967 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
3969 && curwin->w_cursor.col <= ai_col)
3970 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
3971 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003972 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 return FALSE;
3974 }
3975
3976 if (stop_arrow() == FAIL)
3977 return FALSE;
3978 in_indent = inindent(0);
3979#ifdef FEAT_CINDENT
3980 if (in_indent)
3981 can_cindent = FALSE;
3982#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003983 end_comment_pending = NUL; // After BS, don't auto-end comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003985 if (revins_on) // put cursor after last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 inc_cursor();
3987#endif
3988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003989 // Virtualedit:
3990 // BACKSPACE_CHAR eats a virtual space
3991 // BACKSPACE_WORD eats all coladd
3992 // BACKSPACE_LINE eats all coladd and keeps going
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (curwin->w_cursor.coladd > 0)
3994 {
3995 if (mode == BACKSPACE_CHAR)
3996 {
3997 --curwin->w_cursor.coladd;
3998 return TRUE;
3999 }
4000 if (mode == BACKSPACE_WORD)
4001 {
4002 curwin->w_cursor.coladd = 0;
4003 return TRUE;
4004 }
4005 curwin->w_cursor.coladd = 0;
4006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
4008 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004009 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 */
4011 if (curwin->w_cursor.col == 0)
4012 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004013 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004014 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#ifdef FEAT_RIGHTLEFT
4016 || revins_on
4017#endif
4018 )
4019 {
4020 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4021 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4022 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004023 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004024 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026 /*
4027 * In replace mode:
4028 * cc < 0: NL was inserted, delete it
4029 * cc >= 0: NL was replaced, put original characters back
4030 */
4031 cc = -1;
4032 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004033 cc = replace_pop(); // returns -1 if NL was inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 /*
4035 * In replace mode, in the line we started replacing, we only move the
4036 * cursor.
4037 */
4038 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4039 {
4040 dec_cursor();
4041 }
4042 else
4043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (!(State & VREPLACE_FLAG)
4045 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004047 temp = gchar_cursor(); // remember current char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004050 // When "aw" is in 'formatoptions' we must delete the space at
4051 // the end of the line, otherwise the line will be broken
4052 // again when auto-formatting.
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004053 if (has_format_option(FO_AUTO)
4054 && has_format_option(FO_WHITE_PAR))
4055 {
4056 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4057 TRUE);
4058 int len;
4059
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004060 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004061 if (len > 0 && ptr[len - 1] == ' ')
4062 ptr[len - 1] = NUL;
4063 }
4064
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004065 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (temp == NUL && gchar_cursor() != NUL)
4067 inc_cursor();
4068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 else
4070 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 /*
4073 * In REPLACE mode we have to put back the text that was replaced
4074 * by the NL. On the replace stack is first a NUL-terminated
4075 * sequence of characters that were deleted and then the
4076 * characters that NL replaced.
4077 */
4078 if (State & REPLACE_FLAG)
4079 {
4080 /*
4081 * Do the next ins_char() in NORMAL state, to
4082 * prevent ins_char() from replacing characters and
4083 * avoiding showmatch().
4084 */
4085 oldState = State;
4086 State = NORMAL;
4087 /*
4088 * restore characters (blanks) deleted after cursor
4089 */
4090 while (cc > 0)
4091 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004092 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004094 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 cc = replace_pop();
4096 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004097 // restore the characters that NL replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 replace_pop_ins();
4099 State = oldState;
4100 }
4101 }
4102 did_ai = FALSE;
4103 }
4104 else
4105 {
4106 /*
4107 * Delete character(s) before the cursor.
4108 */
4109#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004110 if (revins_on) // put cursor on last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 dec_cursor();
4112#endif
4113 mincol = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004114 // keep indent
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004115 if (mode == BACKSPACE_LINE
4116 && (curbuf->b_p_ai
4117#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004118 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004119#endif
4120 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121#ifdef FEAT_RIGHTLEFT
4122 && !revins_on
4123#endif
4124 )
4125 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004126 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004128 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004130 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132
4133 /*
4134 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4135 */
4136 if ( mode == BACKSPACE_CHAR
4137 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004138 || ((get_sts_value() != 0
4139#ifdef FEAT_VARTABS
4140 || tabstop_count(curbuf->b_p_vsts_array)
4141#endif
4142 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004143 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 && (*(ml_get_cursor() - 1) == TAB
4145 || (*(ml_get_cursor() - 1) == ' '
4146 && (!*inserted_space_p
4147 || arrow_used))))))
4148 {
4149 int ts;
4150 colnr_T vcol;
4151 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004152 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154 *inserted_space_p = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004155 // Compute the virtual column where we want to be. Since
4156 // 'showbreak' may get in the way, need to get the last column of
4157 // the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004159 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 dec_cursor();
4161 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4162 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004163#ifdef FEAT_VARTABS
4164 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004165 {
4166 ts = (int)get_sw_value(curbuf);
4167 want_vcol = (want_vcol / ts) * ts;
4168 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004169 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004170 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004171 curbuf->b_p_vsts_array);
4172#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004173 if (p_sta && in_indent)
4174 ts = (int)get_sw_value(curbuf);
4175 else
4176 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004180 // delete characters until we are at or before want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004182 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004183 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004185 // insert extra spaces until we are at want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 while (vcol < want_vcol)
4187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004188 // Remember the first char we inserted
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004189 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4190 && curwin->w_cursor.col < Insstart_orig.col)
4191 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (State & VREPLACE_FLAG)
4194 ins_char(' ');
4195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004198 if ((State & REPLACE_FLAG))
4199 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4202 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004204 // If we are now back where we started delete one character. Can
4205 // happen when using 'sts' and 'linebreak'.
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004206 if (vcol >= start_vcol)
4207 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209
4210 /*
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004211 * Delete up to starting point, start of line or previous word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004215 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004217 if (has_mbyte)
4218 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004219 do
4220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004222 if (!revins_on) // put cursor on char to be deleted
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004225
4226 cc = gchar_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004227 // look multi-byte character class
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004228 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004230 prev_cclass = cclass;
4231 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004233
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004234 // start of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004235 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4236 {
4237 mode = BACKSPACE_WORD_NOT_SPACE;
4238 temp = vim_iswordc(cc);
4239 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004240 // end of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004241 else if (mode == BACKSPACE_WORD_NOT_SPACE
4242 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004243 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004244 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004246 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004248 inc_cursor();
4249#ifdef FEAT_RIGHTLEFT
4250 else if (State & REPLACE_FLAG)
4251 dec_cursor();
4252#endif
4253 break;
4254 }
4255 if (State & REPLACE_FLAG)
4256 replace_do_bs(-1);
4257 else
4258 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004259 if (enc_utf8 && p_deco)
4260 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004261 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004262 /*
4263 * If there are combining characters and 'delcombine' is set
4264 * move the cursor back. Don't back up before the base
4265 * character.
4266 */
4267 if (enc_utf8 && p_deco && cpc[0] != NUL)
4268 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004269#ifdef FEAT_RIGHTLEFT
4270 if (revins_chars)
4271 {
4272 revins_chars--;
4273 revins_legal++;
4274 }
4275 if (revins_on && gchar_cursor() == NUL)
4276 break;
4277#endif
4278 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004279 // Just a single backspace?:
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004280 if (mode == BACKSPACE_CHAR)
4281 break;
4282 } while (
4283#ifdef FEAT_RIGHTLEFT
4284 revins_on ||
4285#endif
4286 (curwin->w_cursor.col > mincol
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02004287 && (can_bs(BS_NOSTOP)
4288 || (curwin->w_cursor.lnum != Insstart_orig.lnum
4289 || curwin->w_cursor.col != Insstart_orig.col)
4290 )));
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 did_backspace = TRUE;
4293 }
4294#ifdef FEAT_SMARTINDENT
4295 did_si = FALSE;
4296 can_si = FALSE;
4297 can_si_back = FALSE;
4298#endif
4299 if (curwin->w_cursor.col <= 1)
4300 did_ai = FALSE;
4301 /*
4302 * It's a little strange to put backspaces into the redo
4303 * buffer, but it makes auto-indent a lot easier to deal
4304 * with.
4305 */
4306 AppendCharToRedobuff(c);
4307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004308 // If deleted before the insertion point, adjust it
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004309 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004310 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004311 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004313 // vi behaviour: the cursor moves backward but the character that
4314 // was there remains visible
4315 // Vim behaviour: the cursor moves backward and the character that
4316 // was there is erased from the screen.
4317 // We can emulate the vi behaviour by pretending there is a dollar
4318 // displayed even when there isn't.
4319 // --pkv Sun Jan 19 01:56:40 EST 2003
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004320 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 dollar_vcol = curwin->w_virtcol;
4322
Bram Moolenaarce3be472008-01-14 19:12:28 +00004323#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004324 // When deleting a char the cursor line must never be in a closed fold.
4325 // E.g., when 'foldmethod' is indent and deleting the first non-white
4326 // char before a Tab.
Bram Moolenaarce3be472008-01-14 19:12:28 +00004327 if (did_backspace)
4328 foldOpenCursor();
4329#endif
4330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 return did_backspace;
4332}
4333
Bram Moolenaarec2da362017-01-21 20:04:22 +01004334/*
4335 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4336 * P_PE literally.
4337 * When "drop" is TRUE then consume the text and drop it.
4338 */
4339 int
4340bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4341{
4342 int c;
4343 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4344 int idx = 0;
4345 char_u *end = find_termcode((char_u *)"PE");
4346 int ret_char = -1;
4347 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004348 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004349
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004350 // If the end code is too long we can't detect it, read everything.
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01004351 if (end != NULL && STRLEN(end) >= NUMBUFLEN)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004352 end = NULL;
4353 ++no_mapping;
4354 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004355 if (!p_paste)
4356 // Also have the side effects of setting 'paste' to make it work much
4357 // faster.
4358 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004359
Bram Moolenaarec2da362017-01-21 20:04:22 +01004360 for (;;)
4361 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004362 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004363 if (end == NULL && vpeekc() == NUL)
4364 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004365 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004366 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004367 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004368 if (c == NUL || got_int || (ex_normal_busy > 0 && c == Ctrl_C))
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004369 // When CTRL-C was encountered the typeahead will be flushed and we
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004370 // won't get the end sequence. Except when using ":normal".
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004371 break;
4372
Bram Moolenaarec2da362017-01-21 20:04:22 +01004373 if (has_mbyte)
4374 idx += (*mb_char2bytes)(c, buf + idx);
4375 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004376 buf[idx++] = c;
4377 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004378 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004379 {
4380 if (end[idx] == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004381 break; // Found the end of paste code.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004382 continue;
4383 }
4384 if (!drop)
4385 {
4386 switch (mode)
4387 {
4388 case PASTE_CMDLINE:
4389 put_on_cmdline(buf, idx, TRUE);
4390 break;
4391
4392 case PASTE_EX:
4393 if (gap != NULL && ga_grow(gap, idx) == OK)
4394 {
4395 mch_memmove((char *)gap->ga_data + gap->ga_len,
4396 buf, (size_t)idx);
4397 gap->ga_len += idx;
4398 }
4399 break;
4400
4401 case PASTE_INSERT:
4402 if (stop_arrow() == OK)
4403 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004404 c = buf[0];
4405 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
4406 ins_eol(c);
4407 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01004408 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004409 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01004410 AppendToRedobuffLit(buf, idx);
4411 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01004412 }
4413 break;
4414
4415 case PASTE_ONE_CHAR:
4416 if (ret_char == -1)
4417 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01004418 if (has_mbyte)
4419 ret_char = (*mb_ptr2char)(buf);
4420 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004421 ret_char = buf[0];
4422 }
4423 break;
4424 }
4425 }
4426 idx = 0;
4427 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004428
Bram Moolenaarec2da362017-01-21 20:04:22 +01004429 --no_mapping;
4430 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004431 if (!save_paste)
4432 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01004433
4434 return ret_char;
4435}
4436
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004437#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004439ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004440{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004441 // We will be leaving the current window, unless closing another tab.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004442 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
4443 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
4444 {
4445 undisplay_dollar();
4446 start_arrow(&curwin->w_cursor);
4447# ifdef FEAT_CINDENT
4448 can_cindent = TRUE;
4449# endif
4450 }
4451
4452 if (c == K_TABLINE)
4453 goto_tabpage(current_tab);
4454 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004455 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004456 handle_tabmenu();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004457 redraw_statuslines(); // will redraw the tabline when needed
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004458 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004459}
4460#endif
4461
4462#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004464ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465{
4466 pos_T tpos;
4467
4468 undisplay_dollar();
4469 tpos = curwin->w_cursor;
4470 if (gui_do_scroll())
4471 {
4472 start_arrow(&tpos);
4473# ifdef FEAT_CINDENT
4474 can_cindent = TRUE;
4475# endif
4476 }
4477}
4478
4479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004480ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481{
4482 pos_T tpos;
4483
4484 undisplay_dollar();
4485 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004486 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 start_arrow(&tpos);
4489# ifdef FEAT_CINDENT
4490 can_cindent = TRUE;
4491# endif
4492 }
4493}
4494#endif
4495
4496 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004497ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498{
4499 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004500 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502#ifdef FEAT_FOLDING
4503 if ((fdo_flags & FDO_HOR) && KeyTyped)
4504 foldOpenCursor();
4505#endif
4506 undisplay_dollar();
4507 tpos = curwin->w_cursor;
4508 if (oneleft() == OK)
4509 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004510#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004511 // Only call start_arrow() when not busy with preediting, it will
4512 // break undo. K_LEFT is inserted in im_correct_cursor().
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004513 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00004514#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004515 {
4516 start_arrow_with_change(&tpos, end_change);
4517 if (!end_change)
4518 AppendCharToRedobuff(K_LEFT);
4519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004521 // If exit reversed string, position is fixed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
4523 revins_legal++;
4524 revins_chars++;
4525#endif
4526 }
4527
4528 /*
4529 * if 'whichwrap' set for cursor in insert mode may go to
4530 * previous line
4531 */
4532 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
4533 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004534 // always break undo when moving upwards/downwards, else undo may break
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 start_arrow(&tpos);
4536 --(curwin->w_cursor.lnum);
4537 coladvance((colnr_T)MAXCOL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004538 curwin->w_set_curswant = TRUE; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004541 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004542 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543}
4544
4545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004546ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547{
4548 pos_T tpos;
4549
4550#ifdef FEAT_FOLDING
4551 if ((fdo_flags & FDO_HOR) && KeyTyped)
4552 foldOpenCursor();
4553#endif
4554 undisplay_dollar();
4555 tpos = curwin->w_cursor;
4556 if (c == K_C_HOME)
4557 curwin->w_cursor.lnum = 1;
4558 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 curwin->w_curswant = 0;
4561 start_arrow(&tpos);
4562}
4563
4564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004565ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566{
4567 pos_T tpos;
4568
4569#ifdef FEAT_FOLDING
4570 if ((fdo_flags & FDO_HOR) && KeyTyped)
4571 foldOpenCursor();
4572#endif
4573 undisplay_dollar();
4574 tpos = curwin->w_cursor;
4575 if (c == K_C_END)
4576 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4577 coladvance((colnr_T)MAXCOL);
4578 curwin->w_curswant = MAXCOL;
4579
4580 start_arrow(&tpos);
4581}
4582
4583 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004584ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004586 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#ifdef FEAT_FOLDING
4588 if ((fdo_flags & FDO_HOR) && KeyTyped)
4589 foldOpenCursor();
4590#endif
4591 undisplay_dollar();
4592 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
4593 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004594 start_arrow_with_change(&curwin->w_cursor, end_change);
4595 if (!end_change)
4596 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 (void)bck_word(1L, FALSE, FALSE);
4598 curwin->w_set_curswant = TRUE;
4599 }
4600 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004601 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004602 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603}
4604
4605 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004606ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004608 int end_change = dont_sync_undo == FALSE; // end undoable change
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#ifdef FEAT_FOLDING
4611 if ((fdo_flags & FDO_HOR) && KeyTyped)
4612 foldOpenCursor();
4613#endif
4614 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004615 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004617 start_arrow_with_change(&curwin->w_cursor, end_change);
4618 if (!end_change)
4619 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (virtual_active())
4622 oneright();
4623 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004626 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 ++curwin->w_cursor.col;
4629 }
4630
4631#ifdef FEAT_RIGHTLEFT
4632 revins_legal++;
4633 if (revins_chars)
4634 revins_chars--;
4635#endif
4636 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004637 // if 'whichwrap' set for cursor in insert mode, may move the
4638 // cursor to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 else if (vim_strchr(p_ww, ']') != NULL
4640 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4641 {
4642 start_arrow(&curwin->w_cursor);
4643 curwin->w_set_curswant = TRUE;
4644 ++curwin->w_cursor.lnum;
4645 curwin->w_cursor.col = 0;
4646 }
4647 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004648 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004649 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650}
4651
4652 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004653ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004655 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#ifdef FEAT_FOLDING
4657 if ((fdo_flags & FDO_HOR) && KeyTyped)
4658 foldOpenCursor();
4659#endif
4660 undisplay_dollar();
4661 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4662 || gchar_cursor() != NUL)
4663 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004664 start_arrow_with_change(&curwin->w_cursor, end_change);
4665 if (!end_change)
4666 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 (void)fwd_word(1L, FALSE, 0);
4668 curwin->w_set_curswant = TRUE;
4669 }
4670 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004671 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01004672 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673}
4674
4675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004676ins_up(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004677 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679 pos_T tpos;
4680 linenr_T old_topline = curwin->w_topline;
4681#ifdef FEAT_DIFF
4682 int old_topfill = curwin->w_topfill;
4683#endif
4684
4685 undisplay_dollar();
4686 tpos = curwin->w_cursor;
4687 if (cursor_up(1L, TRUE) == OK)
4688 {
4689 if (startcol)
4690 coladvance(getvcol_nolist(&Insstart));
4691 if (old_topline != curwin->w_topline
4692#ifdef FEAT_DIFF
4693 || old_topfill != curwin->w_topfill
4694#endif
4695 )
4696 redraw_later(VALID);
4697 start_arrow(&tpos);
4698#ifdef FEAT_CINDENT
4699 can_cindent = TRUE;
4700#endif
4701 }
4702 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004703 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704}
4705
4706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004707ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
4709 pos_T tpos;
4710
4711 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004712
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004713 if (mod_mask & MOD_MASK_CTRL)
4714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004715 // <C-PageUp>: tab page back
Bram Moolenaarbc444822006-10-17 11:37:50 +00004716 if (first_tabpage->tp_next != NULL)
4717 {
4718 start_arrow(&curwin->w_cursor);
4719 goto_tabpage(-1);
4720 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004721 return;
4722 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 tpos = curwin->w_cursor;
4725 if (onepage(BACKWARD, 1L) == OK)
4726 {
4727 start_arrow(&tpos);
4728#ifdef FEAT_CINDENT
4729 can_cindent = TRUE;
4730#endif
4731 }
4732 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004733 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734}
4735
4736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004737ins_down(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004738 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
4740 pos_T tpos;
4741 linenr_T old_topline = curwin->w_topline;
4742#ifdef FEAT_DIFF
4743 int old_topfill = curwin->w_topfill;
4744#endif
4745
4746 undisplay_dollar();
4747 tpos = curwin->w_cursor;
4748 if (cursor_down(1L, TRUE) == OK)
4749 {
4750 if (startcol)
4751 coladvance(getvcol_nolist(&Insstart));
4752 if (old_topline != curwin->w_topline
4753#ifdef FEAT_DIFF
4754 || old_topfill != curwin->w_topfill
4755#endif
4756 )
4757 redraw_later(VALID);
4758 start_arrow(&tpos);
4759#ifdef FEAT_CINDENT
4760 can_cindent = TRUE;
4761#endif
4762 }
4763 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004764 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765}
4766
4767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004768ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
4770 pos_T tpos;
4771
4772 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004773
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004774 if (mod_mask & MOD_MASK_CTRL)
4775 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004776 // <C-PageDown>: tab page forward
Bram Moolenaarbc444822006-10-17 11:37:50 +00004777 if (first_tabpage->tp_next != NULL)
4778 {
4779 start_arrow(&curwin->w_cursor);
4780 goto_tabpage(0);
4781 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004782 return;
4783 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004784
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 tpos = curwin->w_cursor;
4786 if (onepage(FORWARD, 1L) == OK)
4787 {
4788 start_arrow(&tpos);
4789#ifdef FEAT_CINDENT
4790 can_cindent = TRUE;
4791#endif
4792 }
4793 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02004794 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795}
4796
4797#ifdef FEAT_DND
4798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004799ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004801 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802}
4803#endif
4804
4805/*
4806 * Handle TAB in Insert or Replace mode.
4807 * Return TRUE when the TAB needs to be inserted like a normal character.
4808 */
4809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004810ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812 int ind;
4813 int i;
4814 int temp;
4815
4816 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
4817 Insstart_blank_vcol = get_nolist_virtcol();
4818 if (echeck_abbr(TAB + ABBR_OFF))
4819 return FALSE;
4820
4821 ind = inindent(0);
4822#ifdef FEAT_CINDENT
4823 if (ind)
4824 can_cindent = FALSE;
4825#endif
4826
4827 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004828 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 */
4830 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004831#ifdef FEAT_VARTABS
4832 && !(p_sta && ind
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004833 // These five lines mean 'tabstop' != 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004834 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
4835 || (tabstop_count(curbuf->b_p_vts_array) == 1
4836 && tabstop_first(curbuf->b_p_vts_array)
4837 != get_sw_value(curbuf))
4838 || (tabstop_count(curbuf->b_p_vts_array) == 0
4839 && curbuf->b_p_ts != get_sw_value(curbuf))))
4840 && tabstop_count(curbuf->b_p_vsts_array) == 0
4841#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004842 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004843#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02004844 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 return TRUE;
4846
4847 if (stop_arrow() == FAIL)
4848 return TRUE;
4849
4850 did_ai = FALSE;
4851#ifdef FEAT_SMARTINDENT
4852 did_si = FALSE;
4853 can_si = FALSE;
4854 can_si_back = FALSE;
4855#endif
4856 AppendToRedobuff((char_u *)"\t");
4857
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004858#ifdef FEAT_VARTABS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004859 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004860 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004861 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004862 temp -= get_nolist_virtcol() % temp;
4863 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004864 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004865 // use 'softtabstop' when set
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004866 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004867 curbuf->b_p_vsts_array);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004868 else // otherwise use 'tabstop'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004869 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
4870 curbuf->b_p_vts_array);
4871#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004872 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004873 temp = (int)get_sw_value(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004874 else if (curbuf->b_p_sts != 0) // use 'softtabstop' when set
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02004875 temp = (int)get_sts_value();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004876 else // otherwise use 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 temp = (int)curbuf->b_p_ts;
4878 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 /*
4882 * Insert the first space with ins_char(). It will delete one char in
4883 * replace mode. Insert the rest with ins_str(); it will not delete any
4884 * chars. For VREPLACE mode, we use ins_char() for all characters.
4885 */
4886 ins_char(' ');
4887 while (--temp > 0)
4888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 if (State & VREPLACE_FLAG)
4890 ins_char(' ');
4891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
4893 ins_str((char_u *)" ");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004894 if (State & REPLACE_FLAG) // no char replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 replace_push(NUL);
4896 }
4897 }
4898
4899 /*
4900 * When 'expandtab' not set: Replace spaces by TABs where possible.
4901 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004902#ifdef FEAT_VARTABS
4903 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
4904 || get_sts_value() > 0
4905 || (p_sta && ind)))
4906#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02004907 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
4910 char_u *ptr;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004911 char_u *saved_line = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 pos_T fpos;
4914 pos_T *cursor;
4915 colnr_T want_vcol, vcol;
4916 int change_col = -1;
4917 int save_list = curwin->w_p_list;
4918
4919 /*
4920 * Get the current line. For VREPLACE mode, don't make real changes
4921 * yet, just work on a copy of the line.
4922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (State & VREPLACE_FLAG)
4924 {
4925 pos = curwin->w_cursor;
4926 cursor = &pos;
4927 saved_line = vim_strsave(ml_get_curline());
4928 if (saved_line == NULL)
4929 return FALSE;
4930 ptr = saved_line + pos.col;
4931 }
4932 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
4934 ptr = ml_get_cursor();
4935 cursor = &curwin->w_cursor;
4936 }
4937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004938 // When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
4940 curwin->w_p_list = FALSE;
4941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004942 // Find first white before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004944 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
4946 --fpos.col;
4947 --ptr;
4948 }
4949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 // In Replace mode, don't change characters before the insert point.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 if ((State & REPLACE_FLAG)
4952 && fpos.lnum == Insstart.lnum
4953 && fpos.col < Insstart.col)
4954 {
4955 ptr += Insstart.col - fpos.col;
4956 fpos.col = Insstart.col;
4957 }
4958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004959 // compute virtual column numbers of first white and cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 getvcol(curwin, &fpos, &vcol, NULL, NULL);
4961 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
4962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004963 // Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
4964 // and 'linebreak' adding extra virtual columns.
Bram Moolenaar1c465442017-03-12 20:10:05 +01004965 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004967 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (vcol + i > want_vcol)
4969 break;
4970 if (*ptr != TAB)
4971 {
4972 *ptr = TAB;
4973 if (change_col < 0)
4974 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004975 change_col = fpos.col; // Column of first change
4976 // May have to adjust Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
4978 Insstart.col = fpos.col;
4979 }
4980 }
4981 ++fpos.col;
4982 ++ptr;
4983 vcol += i;
4984 }
4985
4986 if (change_col >= 0)
4987 {
4988 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004989 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // Skip over the spaces we need.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 while (vcol < want_vcol && *ptr == ' ')
4993 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004994 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 ++ptr;
4996 ++repl_off;
4997 }
4998 if (vcol > want_vcol)
4999 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005000 // Must have a char with 'showbreak' just before it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 --ptr;
5002 --repl_off;
5003 }
5004 fpos.col += repl_off;
5005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005006 // Delete following spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 i = cursor->col - fpos.col;
5008 if (i > 0)
5009 {
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005010#ifdef FEAT_PROP_POPUP
5011 if (!(State & VREPLACE_FLAG))
5012 {
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005013 char_u *newp;
5014 int col;
5015
5016 newp = alloc(curbuf->b_ml.ml_line_len - i);
5017 if (newp == NULL)
5018 return FALSE;
5019
5020 col = ptr - curbuf->b_ml.ml_line_ptr;
5021 if (col > 0)
5022 mch_memmove(newp, ptr - col, col);
5023 mch_memmove(newp + col, ptr + i,
5024 curbuf->b_ml.ml_line_len - col - i);
5025
5026 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
5027 vim_free(curbuf->b_ml.ml_line_ptr);
5028 curbuf->b_ml.ml_line_ptr = newp;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005029 curbuf->b_ml.ml_line_len -= i;
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005030 curbuf->b_ml.ml_flags =
5031 (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005032 }
5033 else
5034#endif
5035 STRMOVE(ptr, ptr + i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005036 // correct replace stack.
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005037 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 for (temp = i; --temp >= 0; )
5039 replace_join(repl_off);
5040 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005041#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005042 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005043 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005044 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005045 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5046 (char_u *)"\t", 1);
5047 }
5048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 cursor->col -= i;
5050
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 /*
5052 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5053 * backspacing over the changed spacing and then inserting the new
5054 * spacing.
5055 */
5056 if (State & VREPLACE_FLAG)
5057 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005058 // Backspace from real cursor to change_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 backspace_until_column(change_col);
5060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005061 // Insert each char in saved_line from changed_col to
5062 // ptr-cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 ins_bytes_len(saved_line + change_col,
5064 cursor->col - change_col);
5065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (State & VREPLACE_FLAG)
5069 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 curwin->w_p_list = save_list;
5071 }
5072
5073 return FALSE;
5074}
5075
5076/*
5077 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005078 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005081ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
5083 int i;
5084
5085 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005086 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 undisplay_dollar();
5090
5091 /*
5092 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5093 * character under the cursor. Only push a NUL on the replace stack,
5094 * nothing to put back when the NL is deleted.
5095 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005096 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 replace_push(NUL);
5098
5099 /*
5100 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5101 * replacing the next line, so we push all of the characters left on the
5102 * line onto the replace stack. This is not done here though, it is done
5103 * in open_line().
5104 */
5105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005106 // Put cursor on NUL if on the last char and coladd is 1 (happens after
5107 // CTRL-O).
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005108 if (virtual_active() && curwin->w_cursor.coladd > 0)
5109 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005112 // NL in reverse insert will always start in the end of
5113 // current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (revins_on)
5115 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5116#endif
5117
5118 AppendToRedobuff(NL_STR);
5119 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005120 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 old_indent = 0;
5122#ifdef FEAT_CINDENT
5123 can_cindent = TRUE;
5124#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005125#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005126 // When inserting a line the cursor line must never be in a closed fold.
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005127 foldOpenCursor();
5128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005130 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131}
5132
5133#ifdef FEAT_DIGRAPHS
5134/*
5135 * Handle digraph in insert mode.
5136 * Returns character still to be inserted, or NUL when nothing remaining to be
5137 * done.
5138 */
5139 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005140ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 int c;
5143 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005144 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
5146 pc_status = PC_STATUS_UNSET;
5147 if (redrawing() && !char_avail())
5148 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005149 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005150 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151
5152 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005153 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#ifdef FEAT_CMDL_INFO
5155 add_to_showcmd_c(Ctrl_K);
5156#endif
5157 }
5158
5159#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005160 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161#endif
5162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005163 // don't map the digraph chars. This also prevents the
5164 // mode message to be deleted when ESC is hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 ++no_mapping;
5166 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005167 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 --no_mapping;
5169 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005170 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005171 // when the line fits in 'columns' the '?' is at the start of the next
5172 // line and will not be removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005173 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005174
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005175 if (IS_SPECIAL(c) || mod_mask) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177#ifdef FEAT_CMDL_INFO
5178 clear_showcmd();
5179#endif
5180 insert_special(c, TRUE, FALSE);
5181 return NUL;
5182 }
5183 if (c != ESC)
5184 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005185 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (redrawing() && !char_avail())
5187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005188 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005189 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
5191 if (char2cells(c) == 1)
5192 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005193 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005195 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
5197#ifdef FEAT_CMDL_INFO
5198 add_to_showcmd_c(c);
5199#endif
5200 }
5201 ++no_mapping;
5202 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005203 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 --no_mapping;
5205 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005206 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005207 // when the line fits in 'columns' the '?' is at the start of the
5208 // next line and will not be removed by a redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005209 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (cc != ESC)
5211 {
5212 AppendToRedobuff((char_u *)CTRL_V_STR);
5213 c = getdigraph(c, cc, TRUE);
5214#ifdef FEAT_CMDL_INFO
5215 clear_showcmd();
5216#endif
5217 return c;
5218 }
5219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#ifdef FEAT_CMDL_INFO
5221 clear_showcmd();
5222#endif
5223 return NUL;
5224}
5225#endif
5226
5227/*
5228 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5229 * Returns the char to be inserted, or NUL if none found.
5230 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005231 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005232ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233{
5234 int c;
5235 int temp;
5236 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005237 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5240 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005241 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 return NUL;
5243 }
5244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005245 // try to advance to the cursor column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005247 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 prev_ptr = ptr;
5249 validate_virtcol();
5250 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5251 {
5252 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005253 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255 if ((colnr_T)temp > curwin->w_virtcol)
5256 ptr = prev_ptr;
5257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005260 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 return c;
5262}
5263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264/*
5265 * CTRL-Y or CTRL-E typed in Insert mode.
5266 */
5267 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005268ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005269{
5270 int c = tc;
5271
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005272 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005273 {
5274 if (c == Ctrl_Y)
5275 scrolldown_clamp();
5276 else
5277 scrollup_clamp();
5278 redraw_later(VALID);
5279 }
5280 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 {
5282 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5283 if (c != NUL)
5284 {
5285 long tw_save;
5286
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005287 // The character must be taken literally, insert like it
5288 // was typed after a CTRL-V, and pretend 'textwidth'
5289 // wasn't set. Digits, 'o' and 'x' are special after a
5290 // CTRL-V, don't use it for these.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 if (c < 256 && !isalnum(c))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005292 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 tw_save = curbuf->b_p_tw;
5294 curbuf->b_p_tw = -1;
5295 insert_special(c, TRUE, FALSE);
5296 curbuf->b_p_tw = tw_save;
5297#ifdef FEAT_RIGHTLEFT
5298 revins_chars++;
5299 revins_legal++;
5300#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005301 c = Ctrl_V; // pretend CTRL-V is last character
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 auto_format(FALSE, TRUE);
5303 }
5304 }
5305 return c;
5306}
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308/*
5309 * Get the value that w_virtcol would have when 'list' is off.
5310 * Unless 'cpo' contains the 'L' flag.
5311 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005312 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005315 // check validity of cursor in current buffer
5316 if (curwin->w_buffer == NULL
5317 || curwin->w_buffer->b_ml.ml_mfp == NULL
5318 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5319 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5321 return getvcol_nolist(&curwin->w_cursor);
5322 validate_virtcol();
5323 return curwin->w_virtcol;
5324}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005325
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005326#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005327/*
5328 * Handle the InsertCharPre autocommand.
5329 * "c" is the character that was typed.
5330 * Return a pointer to allocated memory with the replacement string.
5331 * Return NULL to continue inserting "c".
5332 */
5333 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005334do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005335{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005336 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005337 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005338 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005339
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005340 // Return quickly when there is nothing to do.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005341 if (!has_insertcharpre())
5342 return NULL;
5343
Bram Moolenaar704984a2012-06-01 14:57:51 +02005344 if (has_mbyte)
5345 buf[(*mb_char2bytes)(c, buf)] = NUL;
5346 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005347 {
5348 buf[0] = c;
5349 buf[1] = NUL;
5350 }
5351
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 // Lock the text to avoid weird things from happening.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005353 ++textwinlock;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005354 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005355
Bram Moolenaar704984a2012-06-01 14:57:51 +02005356 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005357 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 // Get the value of v:char. It may be empty or more than one
5360 // character. Only use it when changed, otherwise continue with the
5361 // original character to avoid breaking autoindent.
Bram Moolenaar704984a2012-06-01 14:57:51 +02005362 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5363 res = vim_strsave(get_vim_var_str(VV_CHAR));
5364 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005366 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005367 --textwinlock;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005368
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005369 // Restore the State, it may have been changed.
5370 State = save_State;
5371
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005372 return res;
5373}
5374#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005375
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005376#if defined(FEAT_CINDENT) || defined(PROTO)
5377 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005378get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005379{
5380 return can_cindent;
5381}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005382
5383 void
5384set_can_cindent(int val)
5385{
5386 can_cindent = val;
5387}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005388#endif
5389
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005390/*
5391 * Trigger "event" and take care of fixing undo.
5392 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005393 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005394ins_apply_autocmds(event_T event)
5395{
5396 varnumber_T tick = CHANGEDTICK(curbuf);
5397 int r;
5398
5399 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5400
5401 // If u_savesub() was called then we are not prepared to start
5402 // a new line. Call u_save() with no contents to fix that.
Bram Moolenaardb934952020-04-27 20:18:31 +02005403 // Except when leaving Insert mode.
5404 if (event != EVENT_INSERTLEAVE && tick != CHANGEDTICK(curbuf))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005405 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5406
5407 return r;
5408}