blob: 0ac63ec8fd5986c6a5a19d26eae86c38c502cfe1 [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 Moolenaarf2732452018-06-03 14:47:35 +020027#ifdef FEAT_JOB_CHANNEL
28static void init_prompt(int cmdchar_todo);
29#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static void insert_special(int, int, int);
31static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
32static void check_auto_format(int);
33static void redo_literal(int c);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010034static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +000035#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void check_spell_redraw(void);
Bram Moolenaar217ad922005-03-20 22:37:15 +000037#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
39static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010040static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010041static void replace_flush(void);
42static void replace_do_bs(int limit_col);
43static int del_char_after_col(int limit_col);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static void ins_reg(void);
45static void ins_ctrl_g(void);
46static void ins_ctrl_hat(void);
47static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010049static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010051static int ins_start_select(int c);
52static void ins_insert(int replaceState);
53static void ins_ctrl_o(void);
54static void ins_shift(int c, int lastc);
55static void ins_del(void);
56static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000057#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010058static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000059#endif
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010060static void ins_left(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010061static void ins_home(int c);
62static void ins_end(int c);
63static void ins_s_left(void);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010064static void ins_right(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010065static void ins_s_right(void);
66static void ins_up(int startcol);
67static void ins_pageup(void);
68static void ins_down(int startcol);
69static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010073static int ins_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010075static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010077static int ins_ctrl_ey(int tc);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010078#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +010080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010082static colnr_T Insstart_textlen; // length of line when insert started
83static colnr_T Insstart_blank_vcol; // vcol for first inserted blank
84static int update_Insstart_orig = TRUE; // set Insstart_orig to Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010086static char_u *last_insert = NULL; // the text of the previous insert,
87 // K_SPECIAL and CSI are escaped
88static int last_insert_skip; // nr of chars in front of previous insert
89static int new_insert_skip; // nr of chars in front of current insert
90static int did_restart_edit; // "restart_edit" when calling edit()
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010093static int can_cindent; // may do cindenting on this line
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010097static int revins_on; // reverse insert mode on
98static int revins_chars; // how much to skip after edit
99static int revins_legal; // was the last char 'legal'?
100static int revins_scol; // start column of revins session
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101#endif
102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100103static int ins_need_undo; // call u_save() before inserting a
104 // char. Set when edit() is called.
105 // after that arrow_used is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100107static int did_add_space = FALSE; // auto_format() added an extra space
108 // under the cursor
109static int dont_sync_undo = FALSE; // CTRL-G U prevents syncing undo for
110 // the next left/right cursor key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
112/*
113 * edit(): Start inserting text.
114 *
115 * "cmdchar" can be:
116 * 'i' normal insert command
117 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100118 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 * 'R' replace command
120 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
121 * but still only one <CR> is inserted. The <Esc> is not used for redo.
122 * 'g' "gI" command.
123 * 'V' "gR" command for Virtual Replace mode.
124 * 'v' "gr" command for single character Virtual Replace mode.
125 *
126 * This function is not called recursively. For CTRL-O commands, it returns
127 * and lets the caller handle the Normal-mode command.
128 *
129 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
130 */
131 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100132edit(
133 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100134 int startln, // if set, insert at start of line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100135 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136{
137 int c = 0;
138 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100139 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000140 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 int i;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100143 int did_backspace = TRUE; // previous char was backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100145 int line_is_white = FALSE; // line is empty before insert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100147 linenr_T old_topline = 0; // topline before insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#ifdef FEAT_DIFF
149 int old_topfill = -1;
150#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100151 int inserted_space = FALSE; // just inserted a space
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 int replaceState = REPLACE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100153 int nomove = FALSE; // don't move cursor on return
Bram Moolenaarf2732452018-06-03 14:47:35 +0200154#ifdef FEAT_JOB_CHANNEL
155 int cmdchar_todo = cmdchar;
156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100158 // Remember whether editing was restarted after CTRL-O.
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000159 did_restart_edit = restart_edit;
160
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100161 // sleep before redrawing, needed for "CTRL-O :" that results in an
162 // error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 check_for_delay(TRUE);
164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100165 // set Insstart_orig to Insstart
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100166 update_Insstart_orig = TRUE;
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#ifdef HAVE_SANDBOX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100169 // Don't allow inserting in the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (sandbox != 0)
171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100172 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 return FALSE;
174 }
175#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 // Don't allow changes in the buffer while editing the cmdline. The
177 // caller of getcmdline() may get confused.
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100178 // Don't allow recursive insert mode when busy with completion.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200179 if (textwinlock != 0 || textlock != 0
180 || ins_compl_active() || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000181 {
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200182 emsg(_(e_textwinlock));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000183 return FALSE;
184 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100185 ins_compl_clear(); // clear stuff for CTRL-X mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar843ee412004-06-30 16:16:41 +0000187 /*
188 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
189 */
190 if (cmdchar != 'r' && cmdchar != 'v')
191 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100192 pos_T save_cursor = curwin->w_cursor;
193
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100194#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000195 if (cmdchar == 'R')
196 ptr = (char_u *)"r";
197 else if (cmdchar == 'V')
198 ptr = (char_u *)"v";
199 else
200 ptr = (char_u *)"i";
201 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100202 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100203#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200204 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100206 // Make sure the cursor didn't move. Do call check_cursor_col() in
207 // case the text was modified. Since Insert mode was not started yet
208 // a call to check_cursor_col() may move the cursor, especially with
209 // the "A" command, thus set State to avoid that. Also check that the
210 // line number is still valid (lines may have been deleted).
211 // Do not restore if v:char was set to a non-empty string.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100212 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100213#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200214 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100215#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200216 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100217 {
218 int save_state = State;
219
220 curwin->w_cursor = save_cursor;
221 State = INSERT;
222 check_cursor_col();
223 State = save_state;
224 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000226
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200227#ifdef FEAT_CONCEAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100228 // Check if the cursor line needs redrawing before changing State. If
229 // 'concealcursor' is "n" it needs to be redrawn without concealing.
Bram Moolenaarb9464822018-05-10 15:09:49 +0200230 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200231#endif
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 /*
234 * When doing a paste with the middle mouse button, Insstart is set to
235 * where the paste started.
236 */
237 if (where_paste_started.lnum != 0)
238 Insstart = where_paste_started;
239 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 {
241 Insstart = curwin->w_cursor;
242 if (startln)
243 Insstart.col = 0;
244 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000245 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 Insstart_blank_vcol = MAXCOL;
247 if (!did_ai)
248 ai_col = 0;
249
250 if (cmdchar != NUL && restart_edit == 0)
251 {
252 ResetRedobuff();
253 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 if (cmdchar == 'V' || cmdchar == 'v')
255 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100256 // "gR" or "gr" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 AppendCharToRedobuff('g');
258 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
259 }
260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100262 if (cmdchar == K_PS)
263 AppendCharToRedobuff('a');
264 else
265 AppendCharToRedobuff(cmdchar);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100266 if (cmdchar == 'g') // "gI" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 AppendCharToRedobuff('I');
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100268 else if (cmdchar == 'r') // "r<CR>" command
269 count = 1; // insert only one <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 }
271 }
272
273 if (cmdchar == 'R')
274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 State = REPLACE;
276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 else if (cmdchar == 'V' || cmdchar == 'v')
278 {
279 State = VREPLACE;
280 replaceState = VREPLACE;
281 orig_line_count = curbuf->b_ml.ml_line_count;
282 vr_lines_changed = 1;
283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 else
285 State = INSERT;
286
287 stop_insert_mode = FALSE;
288
289 /*
290 * Need to recompute the cursor position, it might move when the cursor is
291 * on a TAB or special character.
292 */
293 curs_columns(TRUE);
294
295 /*
296 * Enable langmap or IME, indicated by 'iminsert'.
297 * Note that IME may enabled/disabled without us noticing here, thus the
298 * 'iminsert' value may not reflect what is actually used. It is updated
299 * when hitting <Esc>.
300 */
301 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
302 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100303#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
305#endif
306
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#ifdef FEAT_CMDL_INFO
309 clear_showcmd();
310#endif
311#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100312 // there is no reverse replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 revins_on = (State == INSERT && p_ri);
314 if (revins_on)
315 undisplay_dollar();
316 revins_chars = 0;
317 revins_legal = 0;
318 revins_scol = -1;
319#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100320 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100321 {
322 // 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.
594 count = 0;
595 nomove = TRUE;
596 goto doESCkey;
597 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100598 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100600 // Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V.
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000601 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000602
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603#ifdef FEAT_RIGHTLEFT
604 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100605 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000608 /*
609 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000610 * and the cursor is still in the completed word. Only when there is
611 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000612 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100613 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000614 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100615 && curwin->w_cursor.col >= ins_compl_col()
616 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000617 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100618 // BS: Delete one character from "compl_leader".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000619 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100620 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000621 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000622 continue;
623
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100624 // When no match was selected or it was edited.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100625 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000626 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100627 // CTRL-L: Add one character from the current match to
628 // "compl_leader". Except when at the original match and
629 // there is nothing to add, CTRL-L works like CTRL-P then.
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000630 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100631 && (!ctrl_x_mode_line_or_eval()
632 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000633 {
634 ins_compl_addfrommatch();
635 continue;
636 }
637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100638 // A non-white character that fits in with the current
639 // completion: Add to "compl_leader".
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000640 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000641 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100642#if defined(FEAT_EVAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100643 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100644 char_u *str = do_insert_char_pre(c);
645 char_u *p;
646
647 if (str != NULL)
648 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100649 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100650 ins_compl_addleader(PTR2CHAR(p));
651 vim_free(str);
652 }
653 else
654#endif
655 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000656 continue;
657 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000658
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100659 // Pressing CTRL-Y selects the current match. When
660 // ins_compl_enter_selects() is set the Enter key does the
661 // same.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100662 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200663 && (c == CAR || c == K_KENTER || c == NL)))
664 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000665 {
666 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200667 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000668 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000669 }
670 }
671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100672 // Prepare for or stop CTRL-X mode. This doesn't do completion, but
673 // it does fix up the text when finishing completion.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100674 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000675 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000676 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100678 // CTRL-\ CTRL-N goes to Normal mode,
679 // CTRL-\ CTRL-G goes to mode selected with 'insertmode',
680 // CTRL-\ CTRL-O is like CTRL-O but without moving the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (c == Ctrl_BSL)
682 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100683 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +0000684 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 ++no_mapping;
686 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000687 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 --no_mapping;
689 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000690 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100692 // it's something else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 vungetc(c);
694 c = Ctrl_BSL;
695 }
696 else if (c == Ctrl_G && p_im)
697 continue;
698 else
699 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000700 if (c == Ctrl_O)
701 {
702 ins_ctrl_o();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100703 ins_at_eol = FALSE; // cursor keeps its column
Bram Moolenaar488c6512005-08-11 20:09:58 +0000704 nomove = TRUE;
705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 count = 0;
707 goto doESCkey;
708 }
709 }
710
711#ifdef FEAT_DIGRAPHS
712 c = do_digraph(c);
713#endif
714
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100715 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (c == Ctrl_V || c == Ctrl_Q)
718 {
719 ins_ctrl_v();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100720 c = Ctrl_V; // pretend CTRL-V is last typed character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 continue;
722 }
723
724#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200725 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100727 // A key name preceded by a bang means this key is not to be
728 // inserted. Skip ahead to the re-indenting below.
729 // A key name preceded by a star means that indenting has to be
730 // done before inserting the key.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 line_is_white = inindent(0);
732 if (in_cinkeys(c, '!', line_is_white))
733 goto force_cindent;
734 if (can_cindent && in_cinkeys(c, '*', line_is_white)
735 && stop_arrow() == OK)
736 do_c_expr_indent();
737 }
738#endif
739
740#ifdef FEAT_RIGHTLEFT
741 if (curwin->w_p_rl)
742 switch (c)
743 {
744 case K_LEFT: c = K_RIGHT; break;
745 case K_S_LEFT: c = K_S_RIGHT; break;
746 case K_C_LEFT: c = K_C_RIGHT; break;
747 case K_RIGHT: c = K_LEFT; break;
748 case K_S_RIGHT: c = K_S_LEFT; break;
749 case K_C_RIGHT: c = K_C_LEFT; break;
750 }
751#endif
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 /*
754 * If 'keymodel' contains "startsel", may start selection. If it
755 * does, a CTRL-O and c will be stuffed, we need to get these
756 * characters.
757 */
758 if (ins_start_select(c))
759 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
761 /*
762 * The big switch to handle a character in insert mode.
763 */
764 switch (c)
765 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100766 case ESC: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (echeck_abbr(ESC + ABBR_OFF))
768 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100769 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100771 case Ctrl_C: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#ifdef FEAT_CMDWIN
773 if (c == Ctrl_C && cmdwin_type != 0)
774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100775 // Close the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 cmdwin_result = K_IGNORE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100777 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000778 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 goto doESCkey;
780 }
781#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200782#ifdef FEAT_JOB_CHANNEL
783 if (c == Ctrl_C && bt_prompt(curbuf))
784 {
785 if (invoke_prompt_interrupt())
786 {
787 if (!bt_prompt(curbuf))
788 // buffer changed to a non-prompt buffer, get out of
789 // Insert mode
790 goto doESCkey;
791 break;
792 }
793 }
794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796#ifdef UNIX
797do_intr:
798#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100799 // when 'insertmode' set, and not halfway a mapping, don't leave
800 // Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 if (goto_im())
802 {
803 if (got_int)
804 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100805 (void)vgetc(); // flush all buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 got_int = FALSE;
807 }
808 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200809 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 break;
811 }
812doESCkey:
813 /*
814 * This is the ONLY return from edit()!
815 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100816 // Always update o_lnum, so that a "CTRL-O ." that adds a line
817 // still puts the cursor back after the inserted text.
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000818 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 o_lnum = curwin->w_cursor.lnum;
820
Bram Moolenaar488c6512005-08-11 20:09:58 +0000821 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000822 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100823 // When CTRL-C was typed got_int will be set, with the result
824 // that the autocommands won't be executed. When mapped got_int
825 // is not set, but let's keep the behavior the same.
826 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200827 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000828 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 continue;
832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100833 case Ctrl_Z: // suspend when 'insertmode' set
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000834 if (!p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100835 goto normalchar; // insert CTRL-Z as normal char
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100836 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100837#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100838 ui_cursor_shape(); // may need to update cursor shape
Bram Moolenaar74a47162017-02-26 19:09:05 +0100839#endif
840 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100842 case Ctrl_O: // execute one command
Bram Moolenaare344bea2005-09-01 20:46:49 +0000843#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100844 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000845 goto docomplete;
846#endif
847 if (echeck_abbr(Ctrl_O + ABBR_OFF))
848 break;
849 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100851 // don't move the cursor left when 'virtualedit' has "onemore".
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000852 if (ve_flags & VE_ONEMORE)
853 {
854 ins_at_eol = FALSE;
855 nomove = TRUE;
856 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000857 count = 0;
858 goto doESCkey;
859
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100860 case K_INS: // toggle insert/replace mode
Bram Moolenaar572cb562005-08-05 21:35:02 +0000861 case K_KINS:
862 ins_insert(replaceState);
863 break;
864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100865 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar572cb562005-08-05 21:35:02 +0000866 break;
867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 case K_HELP: // Help key works like <ESC> <Help>
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000869 case K_F1:
870 case K_XF1:
871 stuffcharReadbuff(K_HELP);
872 if (p_im)
873 need_start_insertmode = TRUE;
874 goto doESCkey;
875
876#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100877 case K_F21: // NetBeans command
878 ++no_mapping; // don't map the next key hits
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000879 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000880 --no_mapping;
881 netbeans_keycommand(i);
882 break;
883#endif
884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 case K_ZERO: // Insert the previously inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 case NUL:
887 case Ctrl_A:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100888 // For ^@ the trailing ESC will end the insert, unless there is an
889 // error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
891 && c != Ctrl_A && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100892 goto doESCkey; // quit insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 inserted_space = FALSE;
894 break;
895
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100896 case Ctrl_R: // insert the contents of a register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 ins_reg();
898 auto_format(FALSE, TRUE);
899 inserted_space = FALSE;
900 break;
901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100902 case Ctrl_G: // commands starting with CTRL-G
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 ins_ctrl_g();
904 break;
905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100906 case Ctrl_HAT: // switch input mode and/or langmap
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000907 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 break;
909
910#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100911 case Ctrl__: // switch between languages
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (!p_ari)
913 goto normalchar;
914 ins_ctrl_();
915 break;
916#endif
917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100918 case Ctrl_D: // Make indent one shiftwidth smaller.
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200919#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100920 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 goto docomplete;
922#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100923 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100925 case Ctrl_T: // Make indent one shiftwidth greater.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100926 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000928 if (has_compl_option(FALSE))
929 goto docomplete;
930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200932
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 ins_shift(c, lastc);
934 auto_format(FALSE, TRUE);
935 inserted_space = FALSE;
936 break;
937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100938 case K_DEL: // delete character under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 case K_KDEL:
940 ins_del();
941 auto_format(FALSE, TRUE);
942 break;
943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100944 case K_BS: // delete character before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 case Ctrl_H:
946 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
947 auto_format(FALSE, TRUE);
948 break;
949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100950 case Ctrl_W: // delete word before the cursor
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200951#ifdef FEAT_JOB_CHANNEL
952 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
953 {
954 // In a prompt window CTRL-W is used for window commands.
955 // Use Shift-CTRL-W to delete a word.
956 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200957 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200958 nomove = TRUE;
959 count = 0;
960 goto doESCkey;
961 }
962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
964 auto_format(FALSE, TRUE);
965 break;
966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100967 case Ctrl_U: // delete all inserted text in current line
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000968# ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100969 // CTRL-X CTRL-U completes with 'completefunc'.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100970 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000971 goto docomplete;
972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
974 auto_format(FALSE, TRUE);
975 inserted_space = FALSE;
976 break;
977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100978 case K_LEFTMOUSE: // mouse keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 case K_LEFTMOUSE_NM:
980 case K_LEFTDRAG:
981 case K_LEFTRELEASE:
982 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100983 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 case K_MIDDLEMOUSE:
985 case K_MIDDLEDRAG:
986 case K_MIDDLERELEASE:
987 case K_RIGHTMOUSE:
988 case K_RIGHTDRAG:
989 case K_RIGHTRELEASE:
990 case K_X1MOUSE:
991 case K_X1DRAG:
992 case K_X1RELEASE:
993 case K_X2MOUSE:
994 case K_X2DRAG:
995 case K_X2RELEASE:
996 ins_mouse(c);
997 break;
998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100999 case K_MOUSEDOWN: // Default action for scroll wheel up: scroll up
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001000 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 break;
1002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001003 case K_MOUSEUP: // Default action for scroll wheel down: scroll down
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001004 ins_mousescroll(MSCR_UP);
1005 break;
1006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001007 case K_MOUSELEFT: // Scroll wheel left
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001008 ins_mousescroll(MSCR_LEFT);
1009 break;
1010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001011 case K_MOUSERIGHT: // Scroll wheel right
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001012 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001014
Bram Moolenaarec2da362017-01-21 20:04:22 +01001015 case K_PS:
1016 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1017 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001018 // invoked from normal mode, bail out
Bram Moolenaarec2da362017-01-21 20:04:22 +01001019 goto doESCkey;
1020 break;
1021 case K_PE:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001022 // Got K_PE without K_PS, ignore.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001023 break;
1024
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001025#ifdef FEAT_GUI_TABLINE
1026 case K_TABLINE:
1027 case K_TABMENU:
1028 ins_tabline(c);
1029 break;
1030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001032 case K_IGNORE: // Something mapped to nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 break;
1034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001035 case K_CURSORHOLD: // Didn't type something for a while.
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001036 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001037 did_cursorhold = TRUE;
1038 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001039
Bram Moolenaar4f974752019-02-17 17:44:42 +01001040#ifdef FEAT_GUI_MSWIN
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001041 // On MS-Windows ignore <M-F4>, we get it when closing the window
1042 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001043 case K_F4:
1044 if (mod_mask != MOD_MASK_ALT)
1045 goto normalchar;
1046 break;
1047#endif
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#ifdef FEAT_GUI
1050 case K_VER_SCROLLBAR:
1051 ins_scroll();
1052 break;
1053
1054 case K_HOR_SCROLLBAR:
1055 ins_horscroll();
1056 break;
1057#endif
1058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 case K_HOME: // <Home>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 case K_S_HOME:
1062 case K_C_HOME:
1063 ins_home(c);
1064 break;
1065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001066 case K_END: // <End>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 case K_S_END:
1069 case K_C_END:
1070 ins_end(c);
1071 break;
1072
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001073 case K_LEFT: // <Left>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001074 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1075 ins_s_left();
1076 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001077 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 break;
1079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001080 case K_S_LEFT: // <S-Left>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 case K_C_LEFT:
1082 ins_s_left();
1083 break;
1084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001085 case K_RIGHT: // <Right>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001086 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1087 ins_s_right();
1088 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001089 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 break;
1091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 case K_S_RIGHT: // <S-Right>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 case K_C_RIGHT:
1094 ins_s_right();
1095 break;
1096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001097 case K_UP: // <Up>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001098 if (pum_visible())
1099 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001100 if (mod_mask & MOD_MASK_SHIFT)
1101 ins_pageup();
1102 else
1103 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 break;
1105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001106 case K_S_UP: // <S-Up>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 case K_PAGEUP:
1108 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001109 if (pum_visible())
1110 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 ins_pageup();
1112 break;
1113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001114 case K_DOWN: // <Down>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001115 if (pum_visible())
1116 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001117 if (mod_mask & MOD_MASK_SHIFT)
1118 ins_pagedown();
1119 else
1120 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 break;
1122
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001123 case K_S_DOWN: // <S-Down>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 case K_PAGEDOWN:
1125 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001126 if (pum_visible())
1127 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 ins_pagedown();
1129 break;
1130
1131#ifdef FEAT_DND
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001132 case K_DROP: // drag-n-drop event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 ins_drop();
1134 break;
1135#endif
1136
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001137 case K_S_TAB: // When not mapped, use like a normal TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 c = TAB;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001139 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001141 case TAB: // TAB or Complete patterns along path
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001142#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001143 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 goto docomplete;
1145#endif
1146 inserted_space = FALSE;
1147 if (ins_tab())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001148 goto normalchar; // insert TAB as a normal char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 auto_format(FALSE, TRUE);
1150 break;
1151
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 case K_KENTER: // <Enter>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 c = CAR;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001154 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 case CAR:
1156 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001157#if defined(FEAT_QUICKFIX)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001158 // In a quickfix window a <CR> jumps to the error under the
1159 // cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (bt_quickfix(curbuf) && c == CAR)
1161 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001162 if (curwin->w_llist_ref == NULL) // quickfix window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001163 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001164 else // location list window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001165 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 break;
1167 }
1168#endif
1169#ifdef FEAT_CMDWIN
1170 if (cmdwin_type != 0)
1171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001172 // Execute the command in the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 cmdwin_result = CAR;
1174 goto doESCkey;
1175 }
1176#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001177#ifdef FEAT_JOB_CHANNEL
1178 if (bt_prompt(curbuf))
1179 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001180 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001181 if (!bt_prompt(curbuf))
1182 // buffer changed to a non-prompt buffer, get out of
1183 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001184 goto doESCkey;
1185 break;
1186 }
1187#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001188 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001189 goto doESCkey; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 auto_format(FALSE, FALSE);
1191 inserted_space = FALSE;
1192 break;
1193
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001194 case Ctrl_K: // digraph or keyword completion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001195 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001197 if (has_compl_option(TRUE))
1198 goto docomplete;
1199 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001201#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 c = ins_digraph();
1203 if (c == NUL)
1204 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001206 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001208 case Ctrl_X: // Enter CTRL-X mode
Bram Moolenaar572cb562005-08-05 21:35:02 +00001209 ins_ctrl_x();
1210 break;
1211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001212 case Ctrl_RSB: // Tag name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001213 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 goto normalchar;
1215 goto docomplete;
1216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001217 case Ctrl_F: // File name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001218 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 goto normalchar;
1220 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001221
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001222 case 's': // Spelling completion after ^X
Bram Moolenaar488c6512005-08-11 20:09:58 +00001223 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001224 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001225 goto normalchar;
1226 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001228 case Ctrl_L: // Whole line completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001229 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001231 // CTRL-L with 'insertmode' set: Leave Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (p_im)
1233 {
1234 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1235 break;
1236 goto doESCkey;
1237 }
1238 goto normalchar;
1239 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001240 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001242 case Ctrl_P: // Do previous/next pattern completion
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 case Ctrl_N:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001244 // if 'complete' is empty then plain ^P is no longer special,
1245 // but it is under other ^X modes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001247 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 goto normalchar;
1250
1251docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001252 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001253#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001254 disable_fold_update++; // don't redraw folds here
Bram Moolenaara6c07602017-03-05 21:18:27 +01001255#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001256 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001257 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001258#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001259 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001260#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001261 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001264 case Ctrl_Y: // copy from previous line or scroll down
1265 case Ctrl_E: // copy from next line or scroll up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001266 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 break;
1268
1269 default:
1270#ifdef UNIX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001271 if (c == intr_char) // special interrupt char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 goto do_intr;
1273#endif
1274
Bram Moolenaare659c952011-05-19 17:25:41 +02001275normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001277 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001279#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001280 if (!p_paste)
1281 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001282 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001283 char_u *str = do_insert_char_pre(c);
1284 char_u *p;
1285
1286 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001287 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001288 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001290 // Insert the new value of v:char literally.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001291 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001292 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001293 c = PTR2CHAR(p);
1294 if (c == CAR || c == K_KENTER || c == NL)
1295 ins_eol(c);
1296 else
1297 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001298 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001299 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001300 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001301 vim_free(str);
1302 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001303 }
1304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001305 // If the new value is already inserted or an empty string
1306 // then don't insert any character.
Bram Moolenaare659c952011-05-19 17:25:41 +02001307 if (c == NUL)
1308 break;
1309 }
1310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311#ifdef FEAT_SMARTINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001312 // Try to perform smart-indenting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 ins_try_si(c);
1314#endif
1315
1316 if (c == ' ')
1317 {
1318 inserted_space = TRUE;
1319#ifdef FEAT_CINDENT
1320 if (inindent(0))
1321 can_cindent = FALSE;
1322#endif
1323 if (Insstart_blank_vcol == MAXCOL
1324 && curwin->w_cursor.lnum == Insstart.lnum)
1325 Insstart_blank_vcol = get_nolist_virtcol();
1326 }
1327
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001328 // Insert a normal character and check for abbreviations on a
1329 // special character. Let CTRL-] expand abbreviations without
1330 // inserting it.
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001331 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001332 // Add ABBR_OFF for characters above 0x100, this is
1333 // what check_abbr() expects.
1334 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1335 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {
1337 insert_special(c, FALSE, FALSE);
1338#ifdef FEAT_RIGHTLEFT
1339 revins_legal++;
1340 revins_chars++;
1341#endif
1342 }
1343
1344 auto_format(FALSE, TRUE);
1345
1346#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001347 // When inserting a character the cursor line must never be in a
1348 // closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 foldOpenCursor();
1350#endif
1351 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001352 } // end of switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001354 // If typed something may trigger CursorHoldI again.
Bram Moolenaar245c4102016-04-20 17:37:41 +02001355 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001356#ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001357 // but not in CTRL-X mode, a script can't restore the state
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001358 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001359#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001360 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001361 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001363 // If the cursor was moved we didn't just insert a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (arrow_used)
1365 inserted_space = FALSE;
1366
1367#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001368 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
1370force_cindent:
1371 /*
1372 * Indent now if a key was typed that is in 'cinkeys'.
1373 */
1374 if (in_cinkeys(c, ' ', line_is_white))
1375 {
1376 if (stop_arrow() == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001377 // re-indent the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 do_c_expr_indent();
1379 }
1380 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001381#endif // FEAT_CINDENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001383 } // for (;;)
1384 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385}
1386
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001387 int
1388ins_need_undo_get(void)
1389{
1390 return ins_need_undo;
1391}
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393/*
1394 * Redraw for Insert mode.
1395 * This is postponed until getting the next character to make '$' in the 'cpo'
1396 * option work correctly.
1397 * Only redraw when there are no characters available. This speeds up
1398 * inserting sequences of characters (e.g., for CTRL-R).
1399 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001400 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001401ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001403#ifdef FEAT_CONCEAL
1404 linenr_T conceal_old_cursor_line = 0;
1405 linenr_T conceal_new_cursor_line = 0;
1406 int conceal_update_lines = FALSE;
1407#endif
1408
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001409 if (char_avail())
1410 return;
1411
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001412 // Trigger CursorMoved if the cursor moved. Not when the popup menu is
1413 // visible, the command might delete it.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001414 if (ready && (has_cursormovedI()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001415# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001416 || popup_visible
1417# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001418# if defined(FEAT_CONCEAL)
1419 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001420# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001421 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001422 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001423 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001424 {
1425# ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001426 // Need to update the screen first, to make sure syntax
1427 // highlighting is correct after making a change (e.g., inserting
1428 // a "(". The autocommand may also require a redraw, so it's done
1429 // again below, unfortunately.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001430 if (syntax_present(curwin) && must_redraw)
1431 update_screen(0);
1432# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001433 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001434 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001435 // Make sure curswant is correct, an autocommand may call
1436 // getcurpos().
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001437 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001438 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001439 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001440#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001441 if (popup_visible)
1442 popup_check_cursor_pos();
1443#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001444# ifdef FEAT_CONCEAL
1445 if (curwin->w_p_cole > 0)
1446 {
1447 conceal_old_cursor_line = last_cursormoved.lnum;
1448 conceal_new_cursor_line = curwin->w_cursor.lnum;
1449 conceal_update_lines = TRUE;
1450 }
1451# endif
1452 last_cursormoved = curwin->w_cursor;
1453 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001455 // Trigger TextChangedI if b_changedtick differs.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001456 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001457 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001458 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001459 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001460 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001461 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001462
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001463 // save and restore curwin and curbuf, in case the autocmd changes them
1464 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001465 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001466 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001467 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001468 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1469 u_save(curwin->w_cursor.lnum,
1470 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001473 // Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1474 // TextChangedI will need to trigger for backwards compatibility, thus use
1475 // different b_last_changedtick* variables.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001476 if (ready && has_textchangedP()
1477 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1478 && pum_visible())
1479 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001480 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001481 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001482
1483 // save and restore curwin and curbuf, in case the autocmd changes them
1484 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001485 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001486 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001487 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001488 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1489 u_save(curwin->w_cursor.lnum,
1490 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001491 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001492
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001493 // Trigger SafeState if nothing is pending.
1494 may_trigger_safestate(ready
1495 && !ins_compl_active()
1496 && !pum_visible());
1497
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001498#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001499 if ((conceal_update_lines
1500 && (conceal_old_cursor_line != conceal_new_cursor_line
1501 || conceal_cursor_line(curwin)))
1502 || need_cursor_line_redraw)
1503 {
1504 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001505 redrawWinline(curwin, conceal_old_cursor_line);
1506 redrawWinline(curwin, conceal_new_cursor_line == 0
1507 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001508 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001509 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001510 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001511#endif
1512 if (must_redraw)
1513 update_screen(0);
1514 else if (clear_cmdline || redraw_cmdline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001515 showmode(); // clear cmdline and show mode
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001516 showruler(FALSE);
1517 setcursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001518 emsg_on_display = FALSE; // may remove error message now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519}
1520
1521/*
1522 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1523 */
1524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001525ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001528 int did_putchar = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001529 int prev_mod_mask = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001531 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00001532 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
1534 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001537 did_putchar = TRUE;
1538 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001539 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541#ifdef FEAT_CMDL_INFO
1542 add_to_showcmd_c(Ctrl_V);
1543#endif
1544
1545 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001546 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001547 // when the line fits in 'columns' the '^' is at the start of the next
1548 // line and will not removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001549 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#ifdef FEAT_CMDL_INFO
1551 clear_showcmd();
1552#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001553
1554 if ((c == ESC || c == CSI) && !(prev_mod_mask & MOD_MASK_SHIFT))
1555 // Using CTRL-V: Change any modifyOtherKeys ESC sequence to a normal
1556 // key. Don't do this for CTRL-SHIFT-V.
1557 c = decodeModifyOtherKeys(c);
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 insert_special(c, FALSE, TRUE);
1560#ifdef FEAT_RIGHTLEFT
1561 revins_chars++;
1562 revins_legal++;
1563#endif
1564}
1565
1566/*
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001567 * After getting an ESC or CSI for a literal key: If the typeahead buffer
1568 * contains a modifyOtherKeys sequence then decode it and return the result.
1569 * Otherwise return "c".
1570 * Note that this doesn't wait for characters, they must be in the typeahead
1571 * buffer already.
1572 */
1573 int
1574decodeModifyOtherKeys(int c)
1575{
1576 char_u *p = typebuf.tb_buf + typebuf.tb_off;
1577 int idx;
1578 int form = 0;
1579 int argidx = 0;
1580 int arg[2] = {0, 0};
1581
1582 // Recognize:
1583 // form 0: {lead}{key};{modifier}u
1584 // form 1: {lead}27;{modifier};{key}~
1585 if ((c == CSI || (c == ESC && *p == '[')) && typebuf.tb_len >= 4)
1586 {
1587 idx = (*p == '[');
1588 if (p[idx] == '2' && p[idx + 1] == '7' && p[idx + 2] == ';')
1589 {
1590 form = 1;
1591 idx += 3;
1592 }
1593 while (idx < typebuf.tb_len && argidx < 2)
1594 {
1595 if (p[idx] == ';')
1596 ++argidx;
1597 else if (VIM_ISDIGIT(p[idx]))
1598 arg[argidx] = arg[argidx] * 10 + (p[idx] - '0');
1599 else
1600 break;
1601 ++idx;
1602 }
1603 if (idx < typebuf.tb_len
1604 && p[idx] == (form == 1 ? '~' : 'u')
1605 && argidx == 1)
1606 {
1607 // Match, consume the code.
1608 typebuf.tb_off += idx + 1;
1609 typebuf.tb_len -= idx + 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001610#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1611 if (typebuf.tb_len == 0)
1612 typebuf_was_filled = FALSE;
1613#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001614
1615 mod_mask = decode_modifiers(arg[!form]);
1616 c = merge_modifyOtherKeys(arg[form]);
1617 }
1618 }
1619
1620 return c;
1621}
1622
1623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 * Put a character directly onto the screen. It's not stored in a buffer.
1625 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1626 */
1627static int pc_status;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001628#define PC_STATUS_UNSET 0 // pc_bytes was not set
1629#define PC_STATUS_RIGHT 1 // right halve of double-wide char
1630#define PC_STATUS_LEFT 2 // left halve of double-wide char
1631#define PC_STATUS_SET 3 // pc_bytes was filled
1632static char_u pc_bytes[MB_MAXBYTES + 1]; // saved bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633static int pc_attr;
1634static int pc_row;
1635static int pc_col;
1636
1637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001638edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
1640 int attr;
1641
1642 if (ScreenLines != NULL)
1643 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001644 update_topline(); // just in case w_topline isn't valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 validate_cursor();
1646 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001647 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 else
1649 attr = 0;
1650 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001651 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653#ifdef FEAT_RIGHTLEFT
1654 if (curwin->w_p_rl)
1655 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001656 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (has_mbyte)
1658 {
1659 int fix_col = mb_fix_col(pc_col, pc_row);
1660
1661 if (fix_col != pc_col)
1662 {
1663 screen_putchar(' ', pc_row, fix_col, attr);
1664 --curwin->w_wcol;
1665 pc_status = PC_STATUS_RIGHT;
1666 }
1667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 else
1670#endif
1671 {
1672 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (mb_lefthalve(pc_row, pc_col))
1674 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
1676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001677 // save the character to be able to put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1681 pc_status = PC_STATUS_SET;
1682 }
1683 screen_putchar(c, pc_row, pc_col, attr);
1684 }
1685}
1686
Bram Moolenaarf2732452018-06-03 14:47:35 +02001687#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1688/*
1689 * Return the effective prompt for the current buffer.
1690 */
1691 char_u *
1692prompt_text(void)
1693{
1694 if (curbuf->b_prompt_text == NULL)
1695 return (char_u *)"% ";
1696 return curbuf->b_prompt_text;
1697}
1698
1699/*
1700 * Prepare for prompt mode: Make sure the last line has the prompt text.
1701 * Move the cursor to this line.
1702 */
1703 static void
1704init_prompt(int cmdchar_todo)
1705{
1706 char_u *prompt = prompt_text();
1707 char_u *text;
1708
1709 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1710 text = ml_get_curline();
1711 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1712 {
1713 // prompt is missing, insert it or append a line with it
1714 if (*text == NUL)
1715 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1716 else
1717 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1718 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1719 coladvance((colnr_T)MAXCOL);
1720 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1721 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001722
1723 // Insert always starts after the prompt, allow editing text after it.
1724 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1725 || Insstart_orig.col != (int)STRLEN(prompt))
1726 {
1727 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001728 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001729 Insstart_orig = Insstart;
1730 Insstart_textlen = Insstart.col;
1731 Insstart_blank_vcol = MAXCOL;
1732 arrow_used = FALSE;
1733 }
1734
Bram Moolenaarf2732452018-06-03 14:47:35 +02001735 if (cmdchar_todo == 'A')
1736 coladvance((colnr_T)MAXCOL);
1737 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001738 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001739 // Make sure the cursor is in a valid position.
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001740 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001741}
1742
1743/*
1744 * Return TRUE if the cursor is in the editable position of the prompt line.
1745 */
1746 int
1747prompt_curpos_editable()
1748{
1749 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1750 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1751}
1752#endif
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754/*
1755 * Undo the previous edit_putchar().
1756 */
1757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001758edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (pc_status == PC_STATUS_RIGHT)
1763 ++curwin->w_wcol;
1764 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001765 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1768 }
1769}
1770
1771/*
1772 * Called when p_dollar is set: display a '$' at the end of the changed text
1773 * Only works when cursor is in the line that changes.
1774 */
1775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 colnr_T save_col;
1779
1780 if (!redrawing())
1781 return;
1782
1783 cursor_off();
1784 save_col = curwin->w_cursor.col;
1785 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 if (has_mbyte)
1787 {
1788 char_u *p;
1789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001790 // If on the last byte of a multi-byte move to the first byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 p = ml_get_curline();
1792 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1793 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001794 curs_columns(FALSE); // recompute w_wrow and w_wcol
Bram Moolenaar02631462017-09-22 15:20:32 +02001795 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
1797 edit_putchar('$', FALSE);
1798 dollar_vcol = curwin->w_virtcol;
1799 }
1800 curwin->w_cursor.col = save_col;
1801}
1802
1803/*
1804 * Call this function before moving the cursor from the normal insert position
1805 * in insert mode.
1806 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001810 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001812 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001813 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815}
1816
1817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 * Truncate the space at the end of a line. This is to be used only in an
1819 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1820 * modes.
1821 */
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 int i;
1826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001827 // find start of trailing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +01001828 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
1830 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001831 replace_join(0); // remove a NUL from the replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833 line[i + 1] = NUL;
1834}
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
1837 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1838 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001839 * Will attempt not to go before "col" even when there is a composing
1840 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 */
1842 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
1845 while ((int)curwin->w_cursor.col > col)
1846 {
1847 curwin->w_cursor.col--;
1848 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001849 replace_do_bs(col);
1850 else if (!del_char_after_col(col))
1851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 }
1853}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001855/*
1856 * Like del_char(), but make sure not to go before column "limit_col".
1857 * Only matters when there are composing characters.
1858 * Return TRUE when something was deleted.
1859 */
1860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001861del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001862{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001863 if (enc_utf8 && limit_col >= 0)
1864 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001865 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001867 // Make sure the cursor is at the start of a character, but
1868 // skip forward again when going too far back because of a
1869 // composing character.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001870 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001871 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001872 {
1873 int l = utf_ptr2len(ml_get_cursor());
1874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001875 if (l == 0) // end of line
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001876 break;
1877 curwin->w_cursor.col += l;
1878 }
1879 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1880 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001881 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001882 }
1883 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001884 (void)del_char(FALSE);
1885 return TRUE;
1886}
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888/*
1889 * Next character is interpreted literally.
1890 * A one, two or three digit decimal number is interpreted as its byte value.
1891 * If one or two digits are entered, the next character is given to vungetc().
1892 * For Unicode a character > 255 may be returned.
1893 */
1894 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001895get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 int cc;
1898 int nc;
1899 int i;
1900 int hex = FALSE;
1901 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
1904 if (got_int)
1905 return Ctrl_C;
1906
1907#ifdef FEAT_GUI
1908 /*
1909 * In GUI there is no point inserting the internal code for a special key.
1910 * It is more useful to insert the string "<KEY>" instead. This would
1911 * probably be useful in a text window too, but it would not be
1912 * vi-compatible (maybe there should be an option for it?) -- webb
1913 */
1914 if (gui.in_use)
1915 ++allow_keys;
1916#endif
1917#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001918 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001920 ++no_mapping; // don't map the next key hits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 cc = 0;
1922 i = 0;
1923 for (;;)
1924 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001925 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001927 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 add_to_showcmd(nc);
1929#endif
1930 if (nc == 'x' || nc == 'X')
1931 hex = TRUE;
1932 else if (nc == 'o' || nc == 'O')
1933 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 else if (nc == 'u' || nc == 'U')
1935 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 else
1937 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001938 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
1940 if (!vim_isxdigit(nc))
1941 break;
1942 cc = cc * 16 + hex2nr(nc);
1943 }
1944 else if (octal)
1945 {
1946 if (nc < '0' || nc > '7')
1947 break;
1948 cc = cc * 8 + nc - '0';
1949 }
1950 else
1951 {
1952 if (!VIM_ISDIGIT(nc))
1953 break;
1954 cc = cc * 10 + nc - '0';
1955 }
1956
1957 ++i;
1958 }
1959
Bram Moolenaar13505972019-01-24 15:04:48 +01001960 if (cc > 255 && unicode == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001961 cc = 255; // limit range to 0-255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 nc = 0;
1963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001964 if (hex) // hex: up to two chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 if (i >= 2)
1967 break;
1968 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 else if (unicode) // Unicode: up to four or eight chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1972 break;
1973 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001974 else if (i >= 3) // decimal or octal: up to three chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 break;
1976 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 if (i == 0) // no number entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001979 if (nc == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
1981 cc = '\n';
1982 nc = 0;
1983 }
1984 else
1985 {
1986 cc = nc;
1987 nc = 0;
1988 }
1989 }
1990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001991 if (cc == 0) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001993 if (enc_dbcs && (cc & 0xff) == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001994 cc = '?'; // don't accept an illegal DBCS char, the NUL in the
1995 // second byte will cause trouble!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
1997 --no_mapping;
1998#ifdef FEAT_GUI
1999 if (gui.in_use)
2000 --allow_keys;
2001#endif
2002 if (nc)
2003 vungetc(nc);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002004 got_int = FALSE; // CTRL-C typed after CTRL-V is not an interrupt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 return cc;
2006}
2007
2008/*
2009 * Insert character, taking care of special keys and mod_mask
2010 */
2011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002012insert_special(
2013 int c,
2014 int allow_modmask,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002015 int ctrlv) // c was typed after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 char_u *p;
2018 int len;
2019
2020 /*
2021 * Special function key, translate into "<Key>". Up to the last '>' is
2022 * inserted with ins_str(), so as not to replace characters in replace
2023 * mode.
2024 * Only use mod_mask for special keys, to avoid things like <S-Space>,
2025 * unless 'allow_modmask' is TRUE.
2026 */
Bram Moolenaard0573012017-10-28 21:11:06 +02002027#ifdef MACOS_X
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002028 // Command-key never produces a normal key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (mod_mask & MOD_MASK_CMD)
2030 allow_modmask = TRUE;
2031#endif
2032 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
2033 {
2034 p = get_special_key_name(c, mod_mask);
2035 len = (int)STRLEN(p);
2036 c = p[len - 1];
2037 if (len > 2)
2038 {
2039 if (stop_arrow() == FAIL)
2040 return;
2041 p[len - 1] = NUL;
2042 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002043 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 ctrlv = FALSE;
2045 }
2046 }
2047 if (stop_arrow() == OK)
2048 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2049}
2050
2051/*
2052 * Special characters in this context are those that need processing other
2053 * than the simple insertion that can be performed here. This includes ESC
2054 * which terminates the insert, and CR/NL which need special processing to
2055 * open up a new line. This routine tries to optimize insertions performed by
2056 * the "redo", "undo" or "put" commands, so it needs to know when it should
2057 * stop and defer processing to the "normal" mechanism.
2058 * '0' and '^' are special, because they can be followed by CTRL-D.
2059 */
2060#ifdef EBCDIC
2061# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2062#else
2063# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2064#endif
2065
Bram Moolenaar13505972019-01-24 15:04:48 +01002066#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002068/*
2069 * "flags": INSCHAR_FORMAT - force formatting
2070 * INSCHAR_CTRLV - char typed just after CTRL-V
2071 * INSCHAR_NO_FEX - don't use 'formatexpr'
2072 *
2073 * NOTE: passes the flags value straight through to internal_format() which,
2074 * beside INSCHAR_FORMAT (above), is also looking for these:
2075 * INSCHAR_DO_COM - format comments
2076 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002079insertchar(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002080 int c, // character to insert or NUL
2081 int flags, // INSCHAR_FORMAT, etc.
2082 int second_indent) // indent for second line if >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002087 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002089 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
2092 /*
2093 * Try to break the line in two or more pieces when:
2094 * - Always do this if we have been called to do formatting only.
2095 * - Always do this when 'formatoptions' has the 'a' flag and the line
2096 * ends in white space.
2097 * - Otherwise:
2098 * - Don't do this if inserting a blank
2099 * - Don't do this if an existing character is being replaced, unless
2100 * we're in VREPLACE mode.
2101 * - Do this if the cursor is not on the line where insert started
2102 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2103 * before the insert.
2104 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2105 * before 'textwidth'
2106 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002107 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002108 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002109 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 && *ml_get_cursor() != NUL)
2113 && (curwin->w_cursor.lnum != Insstart.lnum
2114 || ((!has_format_option(FO_INS_LONG)
2115 || Insstart_textlen <= (colnr_T)textwidth)
2116 && (!fo_ins_blank
2117 || Insstart_blank_vcol <= (colnr_T)textwidth
2118 ))))))
2119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002120 // Format with 'formatexpr' when it's set. Use internal formatting
2121 // when 'formatexpr' isn't set or it returns non-zero.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002122#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002123 int do_internal = TRUE;
2124 colnr_T virtcol = get_nolist_virtcol()
2125 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002126
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002127 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2128 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002129 {
2130 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002131 // It may be required to save for undo again, e.g. when setline()
2132 // was called.
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002133 ins_need_undo = TRUE;
2134 }
2135 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002137 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002140 if (c == NUL) // only formatting was wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 return;
2142
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002143 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (did_ai && (int)c == end_comment_pending)
2145 {
2146 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002147 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 int middle_len, end_len;
2149 int i;
2150
2151 /*
2152 * Need to remove existing (middle) comment leader and insert end
2153 * comment leader. First, check what comment leader we can find.
2154 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002155 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002156 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002158 // Skip middle-comment string
2159 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 ++p;
2161 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002162 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002163 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 --middle_len;
2165
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002166 // Find the end-comment string
2167 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 ++p;
2169 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2170
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002171 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002173 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 ;
2175 i++;
2176
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002177 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 i -= middle_len;
2179
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002180 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2182 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002183 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 backspace_until_column(i);
2185
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002186 // Insert the end-comment string, except for the last
2187 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 ins_bytes_len(lead_end, end_len - 1);
2189 }
2190 }
2191 }
2192 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 did_ai = FALSE;
2195#ifdef FEAT_SMARTINDENT
2196 did_si = FALSE;
2197 can_si = FALSE;
2198 can_si_back = FALSE;
2199#endif
2200
2201 /*
2202 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2203 * This speeds up normal text input considerably.
2204 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2205 * need to re-indent at a ':', or any other character (but not what
2206 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002207 * Don't do this when there an InsertCharPre autocommand is defined,
2208 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002209 * Do the check for InsertCharPre before the call to vpeekc() because the
2210 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 */
2212#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002213 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214#endif
2215
2216 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002218 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 && vpeekc() != NUL
2220 && !(State & REPLACE_FLAG)
2221#ifdef FEAT_CINDENT
2222 && !cindent_on()
2223#endif
2224#ifdef FEAT_RIGHTLEFT
2225 && !p_ri
2226#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002227 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
2229#define INPUT_BUFLEN 100
2230 char_u buf[INPUT_BUFLEN + 1];
2231 int i;
2232 colnr_T virtcol = 0;
2233
2234 buf[0] = c;
2235 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002236 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 virtcol = get_nolist_virtcol();
2238 /*
2239 * Stop the string when:
2240 * - no more chars available
2241 * - finding a special character (command key)
2242 * - buffer is full
2243 * - running into the 'textwidth' boundary
2244 * - need to check for abbreviation: A non-word char after a word-char
2245 */
2246 while ( (c = vpeekc()) != NUL
2247 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 && i < INPUT_BUFLEN
2250 && (textwidth == 0
2251 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2252 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2253 {
2254#ifdef FEAT_RIGHTLEFT
2255 c = vgetc();
2256 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002257 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 buf[i++] = c;
2259#else
2260 buf[i++] = vgetc();
2261#endif
2262 }
2263
2264#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002265 do_digraph(-1); // clear digraphs
2266 do_digraph(buf[i-1]); // may be the start of a digraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#endif
2268 buf[i] = NUL;
2269 ins_str(buf);
2270 if (flags & INSCHAR_CTRLV)
2271 {
2272 redo_literal(*buf);
2273 i = 1;
2274 }
2275 else
2276 i = 0;
2277 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002278 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280 else
2281 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002282 int cc;
2283
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2285 {
2286 char_u buf[MB_MAXBYTES + 1];
2287
2288 (*mb_char2bytes)(c, buf);
2289 buf[cc] = NUL;
2290 ins_char_bytes(buf, cc);
2291 AppendCharToRedobuff(c);
2292 }
2293 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 ins_char(c);
2296 if (flags & INSCHAR_CTRLV)
2297 redo_literal(c);
2298 else
2299 AppendCharToRedobuff(c);
2300 }
2301 }
2302}
2303
2304/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002305 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002306 *
2307 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2308 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002309 */
2310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002311internal_format(
2312 int textwidth,
2313 int second_indent,
2314 int flags,
2315 int format_only,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002316 int c) // character to be inserted (can be NUL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002317{
2318 int cc;
2319 int save_char = NUL;
2320 int haveto_redraw = FALSE;
2321 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002322 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002323 int fo_white_par = has_format_option(FO_WHITE_PAR);
2324 int first_line = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002325 colnr_T leader_len;
2326 int no_leader = FALSE;
2327 int do_comments = (flags & INSCHAR_DO_COM);
Bram Moolenaar0026d472014-09-09 16:32:39 +02002328#ifdef FEAT_LINEBREAK
2329 int has_lbr = curwin->w_p_lbr;
2330
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002331 // make sure win_lbr_chartabsize() counts correctly
Bram Moolenaar0026d472014-09-09 16:32:39 +02002332 curwin->w_p_lbr = FALSE;
2333#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002334
2335 /*
2336 * When 'ai' is off we don't want a space under the cursor to be
2337 * deleted. Replace it with an 'x' temporarily.
2338 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002339 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002340 {
2341 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002342 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002343 {
2344 save_char = cc;
2345 pchar_cursor('x');
2346 }
2347 }
2348
2349 /*
2350 * Repeat breaking lines, until the current line is not too long.
2351 */
2352 while (!got_int)
2353 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002354 int startcol; // Cursor column at entry
2355 int wantcol; // column at textwidth border
2356 int foundcol; // column for start of spaces
2357 int end_foundcol = 0; // column for start of word
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002358 colnr_T len;
2359 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002360 int orig_col = 0;
2361 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002362 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002363 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002364 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002365
Bram Moolenaar97b98102009-11-17 16:41:01 +00002366 virtcol = get_nolist_virtcol()
2367 + char2cells(c != NUL ? c : gchar_cursor());
2368 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002369 break;
2370
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002371 if (no_leader)
2372 do_comments = FALSE;
2373 else if (!(flags & INSCHAR_FORMAT)
2374 && has_format_option(FO_WRAP_COMS))
2375 do_comments = TRUE;
2376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002377 // Don't break until after the comment leader
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002378 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002379 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002380 else
2381 leader_len = 0;
2382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002383 // If the line doesn't start with a comment leader, then don't
2384 // start one in a following broken line. Avoids that a %word
2385 // moved to the start of the next line causes all following lines
2386 // to start with %.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002387 if (leader_len == 0)
2388 no_leader = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002389 if (!(flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002390 && leader_len == 0
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002391 && !has_format_option(FO_WRAP))
2392
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002393 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002394 if ((startcol = curwin->w_cursor.col) == 0)
2395 break;
2396
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002397 // find column of textwidth border
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002398 coladvance((colnr_T)textwidth);
2399 wantcol = curwin->w_cursor.col;
2400
Bram Moolenaar97b98102009-11-17 16:41:01 +00002401 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002402 foundcol = 0;
2403
2404 /*
2405 * Find position to break at.
2406 * Stop at first entered white when 'formatoptions' has 'v'
2407 */
2408 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002409 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002410 || curwin->w_cursor.lnum != Insstart.lnum
2411 || curwin->w_cursor.col >= Insstart.col)
2412 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002413 if (curwin->w_cursor.col == startcol && c != NUL)
2414 cc = c;
2415 else
2416 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002417 if (WHITECHAR(cc))
2418 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002419 // remember position of blank just before text
Bram Moolenaar97b98102009-11-17 16:41:01 +00002420 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002421
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002422 // find start of sequence of blanks
2423 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002424 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2425 {
2426 dec_cursor();
2427 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002428
2429 // Increment count of how many whitespace chars in this
2430 // group; we only need to know if it's more than one.
2431 if (wcc < 2)
2432 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002433 }
2434 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002435 break; // only spaces in front of text
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002436
2437 // Don't break after a period when 'formatoptions' has 'p' and
2438 // there are less than two spaces.
2439 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2440 continue;
2441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002442 // Don't break until after the comment leader
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002443 if (curwin->w_cursor.col < leader_len)
2444 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002445 if (has_format_option(FO_ONE_LETTER))
2446 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002447 // do not break after one-letter words
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002448 if (curwin->w_cursor.col == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002449 break; // one-letter word at begin
2450 // do not break "#a b" when 'tw' is 2
Bram Moolenaar97b98102009-11-17 16:41:01 +00002451 if (curwin->w_cursor.col <= leader_len)
2452 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002453 col = curwin->w_cursor.col;
2454 dec_cursor();
2455 cc = gchar_cursor();
2456
2457 if (WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002458 continue; // one-letter, continue
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002459 curwin->w_cursor.col = col;
2460 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002461
2462 inc_cursor();
2463
2464 end_foundcol = end_col + 1;
2465 foundcol = curwin->w_cursor.col;
2466 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002467 break;
2468 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002469 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002471 // Break after or before a multi-byte character.
Bram Moolenaar97b98102009-11-17 16:41:01 +00002472 if (curwin->w_cursor.col != startcol)
2473 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002474 // Don't break until after the comment leader
Bram Moolenaar97b98102009-11-17 16:41:01 +00002475 if (curwin->w_cursor.col < leader_len)
2476 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002477 col = curwin->w_cursor.col;
2478 inc_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002479 // Don't change end_foundcol if already set.
Bram Moolenaar97b98102009-11-17 16:41:01 +00002480 if (foundcol != curwin->w_cursor.col)
2481 {
2482 foundcol = curwin->w_cursor.col;
2483 end_foundcol = foundcol;
2484 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2485 break;
2486 }
2487 curwin->w_cursor.col = col;
2488 }
2489
2490 if (curwin->w_cursor.col == 0)
2491 break;
2492
2493 col = curwin->w_cursor.col;
2494
2495 dec_cursor();
2496 cc = gchar_cursor();
2497
2498 if (WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002499 continue; // break with space
2500 // Don't break until after the comment leader
Bram Moolenaar97b98102009-11-17 16:41:01 +00002501 if (curwin->w_cursor.col < leader_len)
2502 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002503
2504 curwin->w_cursor.col = col;
2505
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002506 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002507 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002508 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2509 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002510 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002511 if (curwin->w_cursor.col == 0)
2512 break;
2513 dec_cursor();
2514 }
2515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002516 if (foundcol == 0) // no spaces, cannot break line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002517 {
2518 curwin->w_cursor.col = startcol;
2519 break;
2520 }
2521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002522 // Going to break the line, remove any "$" now.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002523 undisplay_dollar();
2524
2525 /*
2526 * Offset between cursor position and line break is used by replace
2527 * stack functions. VREPLACE does not use this, and backspaces
2528 * over the text instead.
2529 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002530 if (State & VREPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002531 orig_col = startcol; // Will start backspacing from here
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002532 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002533 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002534
2535 /*
2536 * adjust startcol for spaces that will be deleted and
2537 * characters that will remain on top line
2538 */
2539 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002540 while ((cc = gchar_cursor(), WHITECHAR(cc))
2541 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002542 inc_cursor();
2543 startcol -= curwin->w_cursor.col;
2544 if (startcol < 0)
2545 startcol = 0;
2546
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002547 if (State & VREPLACE_FLAG)
2548 {
2549 /*
2550 * In VREPLACE mode, we will backspace over the text to be
2551 * wrapped, so save a copy now to put on the next line.
2552 */
2553 saved_text = vim_strsave(ml_get_cursor());
2554 curwin->w_cursor.col = orig_col;
2555 if (saved_text == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002556 break; // Can't do it, out of memory
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002557 saved_text[startcol] = NUL;
2558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002559 // Backspace over characters that will move to the next line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002560 if (!fo_white_par)
2561 backspace_until_column(foundcol);
2562 }
2563 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002564 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002565 // put cursor after pos. to break line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002566 if (!fo_white_par)
2567 curwin->w_cursor.col = foundcol;
2568 }
2569
2570 /*
2571 * Split the line just before the margin.
2572 * Only insert/delete lines, but don't really redraw the window.
2573 */
2574 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2575 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002576 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002577 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002578 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2579 if (!(flags & INSCHAR_COM_LIST))
2580 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002581
2582 replace_offset = 0;
2583 if (first_line)
2584 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002585 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002586 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002587 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002588 * This section is for auto-wrap of numeric lists. When not
2589 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2590 * flag will be set and open_line() will handle it (as seen
2591 * above). The code here (and in get_number_indent()) will
2592 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002593 */
2594 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002595 second_indent =
2596 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002597 if (second_indent >= 0)
2598 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002599 if (State & VREPLACE_FLAG)
2600 change_indent(INDENT_SET, second_indent,
2601 FALSE, NUL, TRUE);
2602 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002603 if (leader_len > 0 && second_indent - leader_len > 0)
2604 {
2605 int i;
2606 int padding = second_indent - leader_len;
2607
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002608 // We started at the first_line of a numbered list
2609 // that has a comment. the open_line() function has
2610 // inserted the proper comment leader and positioned
2611 // the cursor at the end of the split line. Now we
2612 // add the additional whitespace needed after the
2613 // comment leader for the numbered list.
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002614 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002615 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002616 }
2617 else
2618 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002619 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002620 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002621 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002622 }
2623 first_line = FALSE;
2624 }
2625
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002626 if (State & VREPLACE_FLAG)
2627 {
2628 /*
2629 * In VREPLACE mode we have backspaced over the text to be
2630 * moved, now we re-insert it into the new line.
2631 */
2632 ins_bytes(saved_text);
2633 vim_free(saved_text);
2634 }
2635 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002636 {
2637 /*
2638 * Check if cursor is not past the NUL off the line, cindent
2639 * may have added or removed indent.
2640 */
2641 curwin->w_cursor.col += startcol;
2642 len = (colnr_T)STRLEN(ml_get_curline());
2643 if (curwin->w_cursor.col > len)
2644 curwin->w_cursor.col = len;
2645 }
2646
2647 haveto_redraw = TRUE;
2648#ifdef FEAT_CINDENT
2649 can_cindent = TRUE;
2650#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002651 // moved the cursor, don't autoindent or cindent now
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002652 did_ai = FALSE;
2653#ifdef FEAT_SMARTINDENT
2654 did_si = FALSE;
2655 can_si = FALSE;
2656 can_si_back = FALSE;
2657#endif
2658 line_breakcheck();
2659 }
2660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002661 if (save_char != NUL) // put back space after cursor
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002662 pchar_cursor(save_char);
2663
Bram Moolenaar0026d472014-09-09 16:32:39 +02002664#ifdef FEAT_LINEBREAK
2665 curwin->w_p_lbr = has_lbr;
2666#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002667 if (!format_only && haveto_redraw)
2668 {
2669 update_topline();
2670 redraw_curbuf_later(VALID);
2671 }
2672}
2673
2674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 * Called after inserting or deleting text: When 'formatoptions' includes the
2676 * 'a' flag format from the current line until the end of the paragraph.
2677 * Keep the cursor at the same position relative to the text.
2678 * The caller must have saved the cursor line for undo, following ones will be
2679 * saved here.
2680 */
2681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002682auto_format(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002683 int trailblank, // when TRUE also format with trailing blank
2684 int prev_line) // may start in previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
2686 pos_T pos;
2687 colnr_T len;
2688 char_u *old;
2689 char_u *new, *pnew;
2690 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002691 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 if (!has_format_option(FO_AUTO))
2694 return;
2695
2696 pos = curwin->w_cursor;
2697 old = ml_get_curline();
2698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002699 // may remove added space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 check_auto_format(FALSE);
2701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002702 // Don't format in Insert mode when the cursor is on a trailing blank, the
2703 // user might insert normal text next. Also skip formatting when "1" is
2704 // in 'formatoptions' and there is a single character before the cursor.
2705 // Otherwise the line would be broken and when typing another non-white
2706 // next they are not joined back together.
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002707 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (*old != NUL && !trailblank && wasatend)
2709 {
2710 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002711 cc = gchar_cursor();
2712 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2713 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002715 cc = gchar_cursor();
2716 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
2718 curwin->w_cursor = pos;
2719 return;
2720 }
2721 curwin->w_cursor = pos;
2722 }
2723
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002724 // With the 'c' flag in 'formatoptions' and 't' missing: only format
2725 // comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002727 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730 /*
2731 * May start formatting in a previous line, so that after "x" a word is
2732 * moved to the previous line if it fits there now. Only when this is not
2733 * the start of a paragraph.
2734 */
2735 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2736 {
2737 --curwin->w_cursor.lnum;
2738 if (u_save_cursor() == FAIL)
2739 return;
2740 }
2741
2742 /*
2743 * Do the formatting and restore the cursor position. "saved_cursor" will
2744 * be adjusted for the text formatting.
2745 */
2746 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002747 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 curwin->w_cursor = saved_cursor;
2749 saved_cursor.lnum = 0;
2750
2751 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002753 // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2755 coladvance((colnr_T)MAXCOL);
2756 }
2757 else
2758 check_cursor_col();
2759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002760 // Insert mode: If the cursor is now after the end of the line while it
2761 // previously wasn't, the line was broken. Because of the rule above we
2762 // need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2763 // formatted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (!wasatend && has_format_option(FO_WHITE_PAR))
2765 {
2766 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002767 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (curwin->w_cursor.col == len)
2769 {
2770 pnew = vim_strnsave(new, len + 2);
2771 pnew[len] = ' ';
2772 pnew[len + 1] = NUL;
2773 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002774 // remove the space later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 did_add_space = TRUE;
2776 }
2777 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002778 // may remove added space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 check_auto_format(FALSE);
2780 }
2781
2782 check_cursor();
2783}
2784
2785/*
2786 * When an extra space was added to continue a paragraph for auto-formatting,
2787 * delete it now. The space must be under the cursor, just after the insert
2788 * position.
2789 */
2790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002791check_auto_format(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002792 int end_insert) // TRUE when ending Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
2794 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002795 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
2797 if (did_add_space)
2798 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002799 cc = gchar_cursor();
2800 if (!WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002801 // Somehow the space was removed already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 did_add_space = FALSE;
2803 else
2804 {
2805 if (!end_insert)
2806 {
2807 inc_cursor();
2808 c = gchar_cursor();
2809 dec_cursor();
2810 }
2811 if (c != NUL)
2812 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002813 // The space is no longer at the end of the line, delete it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 del_char(FALSE);
2815 did_add_space = FALSE;
2816 }
2817 }
2818 }
2819}
2820
2821/*
2822 * Find out textwidth to be used for formatting:
2823 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02002824 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 * if invalid value, use 0.
2826 * Set default to window width (maximum 79) for "gq" operator.
2827 */
2828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002829comp_textwidth(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002830 int ff) // force formatting (for "gq" command)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 int textwidth;
2833
2834 textwidth = curbuf->b_p_tw;
2835 if (textwidth == 0 && curbuf->b_p_wm)
2836 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002837 // The width is the window width minus 'wrapmargin' minus all the
2838 // things that add to the margin.
Bram Moolenaar02631462017-09-22 15:20:32 +02002839 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840#ifdef FEAT_CMDWIN
2841 if (cmdwin_type != 0)
2842 textwidth -= 1;
2843#endif
2844#ifdef FEAT_FOLDING
2845 textwidth -= curwin->w_p_fdc;
2846#endif
2847#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002848 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 textwidth -= 1;
2850#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02002851 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 textwidth -= 8;
2853 }
2854 if (textwidth < 0)
2855 textwidth = 0;
2856 if (ff && textwidth == 0)
2857 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002858 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (textwidth > 79)
2860 textwidth = 79;
2861 }
2862 return textwidth;
2863}
2864
2865/*
2866 * Put a character in the redo buffer, for when just after a CTRL-V.
2867 */
2868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002869redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
2871 char_u buf[10];
2872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002873 // Only digits need special treatment. Translate them into a string of
2874 // three digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 if (VIM_ISDIGIT(c))
2876 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002877 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 AppendToRedobuff(buf);
2879 }
2880 else
2881 AppendCharToRedobuff(c);
2882}
2883
2884/*
2885 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002886 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002889start_arrow(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002890 pos_T *end_insert_pos) // can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002892 start_arrow_common(end_insert_pos, TRUE);
2893}
2894
2895/*
2896 * Like start_arrow() but with end_change argument.
2897 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2898 */
2899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002900start_arrow_with_change(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002901 pos_T *end_insert_pos, // can be NULL
2902 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002903{
2904 start_arrow_common(end_insert_pos, end_change);
2905 if (!end_change)
2906 {
2907 AppendCharToRedobuff(Ctrl_G);
2908 AppendCharToRedobuff('U');
2909 }
2910}
2911
2912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002913start_arrow_common(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002914 pos_T *end_insert_pos, // can be NULL
2915 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002916{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002917 if (!arrow_used && end_change) // something has been inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002920 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002921 arrow_used = TRUE; // this means we stopped the current insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002923#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002924 check_spell_redraw();
2925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926}
2927
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002928#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002929/*
2930 * If we skipped highlighting word at cursor, do it now.
2931 * It may be skipped again, thus reset spell_redraw_lnum first.
2932 */
2933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002934check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002935{
2936 if (spell_redraw_lnum != 0)
2937 {
2938 linenr_T lnum = spell_redraw_lnum;
2939
2940 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002941 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002942 }
2943}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002944
Bram Moolenaar217ad922005-03-20 22:37:15 +00002945#endif
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947/*
2948 * stop_arrow() is called before a change is made in insert mode.
2949 * If an arrow key has been used, start a new insertion.
2950 * Returns FAIL if undo is impossible, shouldn't insert then.
2951 */
2952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002953stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 if (arrow_used)
2956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002957 Insstart = curwin->w_cursor; // new insertion starts here
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002958 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002959 // Don't update the original insert position when moved to the
2960 // right, except when nothing was inserted yet.
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002961 update_Insstart_orig = FALSE;
2962 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (u_save_cursor() == OK)
2965 {
2966 arrow_used = FALSE;
2967 ins_need_undo = FALSE;
2968 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (State & VREPLACE_FLAG)
2972 {
2973 orig_line_count = curbuf->b_ml.ml_line_count;
2974 vr_lines_changed = 1;
2975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 ResetRedobuff();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002977 AppendToRedobuff((char_u *)"1i"); // pretend we start an insertion
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002978 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980 else if (ins_need_undo)
2981 {
2982 if (u_save_cursor() == OK)
2983 ins_need_undo = FALSE;
2984 }
2985
2986#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002987 // Always open fold at the cursor line when inserting something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 foldOpenCursor();
2989#endif
2990
2991 return (arrow_used || ins_need_undo ? FAIL : OK);
2992}
2993
2994/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002995 * Do a few things to stop inserting.
2996 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2997 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 */
2999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003000stop_insert(
3001 pos_T *end_insert_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003002 int esc, // called by ins_esc()
3003 int nomove) // <c-\><c-o>, don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003005 int cc;
3006 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
3008 stop_redo_ins();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003009 replace_flush(); // abandon replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
3011 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003012 * Save the inserted text for later redo with ^@ and CTRL-A.
3013 * Don't do it when "restart_edit" was set and nothing was inserted,
3014 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003016 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003017 if (did_restart_edit == 0 || (ptr != NULL
3018 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003019 {
3020 vim_free(last_insert);
3021 last_insert = ptr;
3022 last_insert_skip = new_insert_skip;
3023 }
3024 else
3025 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003027 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003029 // Auto-format now. It may seem strange to do this when stopping an
3030 // insertion (or moving the cursor), but it's required when appending
3031 // a line and having it end in a space. But only do it when something
3032 // was actually inserted, otherwise undo won't work.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003033 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003035 pos_T tpos = curwin->w_cursor;
3036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003037 // When the cursor is at the end of the line after a space the
3038 // formatting will move it to the following word. Avoid that by
3039 // moving the cursor onto the space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 cc = 'x';
3041 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
3042 {
3043 dec_cursor();
3044 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003045 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003046 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
3048
3049 auto_format(TRUE, FALSE);
3050
Bram Moolenaar1c465442017-03-12 20:10:05 +01003051 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003052 {
3053 if (gchar_cursor() != NUL)
3054 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003055 // If the cursor is still at the same character, also keep
3056 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003057 if (gchar_cursor() == NUL
3058 && curwin->w_cursor.lnum == tpos.lnum
3059 && curwin->w_cursor.col == tpos.col)
3060 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 }
3063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003064 // If a space was inserted for auto-formatting, remove it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 check_auto_format(TRUE);
3066
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003067 // If we just did an auto-indent, remove the white space from the end
3068 // of the line, and put the cursor back.
3069 // Do this when ESC was used or moving the cursor up/down.
3070 // Check for the old position still being valid, just in case the text
3071 // got changed unexpectedly.
Bram Moolenaarf332a652013-11-04 04:20:33 +01003072 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003073 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3074 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003076 pos_T tpos = curwin->w_cursor;
3077
3078 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003079 check_cursor_col(); // make sure it is not past the line
Bram Moolenaar39f05632006-03-19 22:15:26 +00003080 for (;;)
3081 {
3082 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3083 --curwin->w_cursor.col;
3084 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003085 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003086 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003087 if (del_char(TRUE) == FAIL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 break; // should not happen
Bram Moolenaar39f05632006-03-19 22:15:26 +00003089 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003090 if (curwin->w_cursor.lnum != tpos.lnum)
3091 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003092 else
3093 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003094 // reset tpos, could have been invalidated in the loop above
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003095 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003096 tpos.col++;
3097 if (cc != NUL && gchar_pos(&tpos) == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003098 ++curwin->w_cursor.col; // put cursor back on the NUL
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003101 // <C-S-Right> may have started Visual mode, adjust the position for
3102 // deleted characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3104 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003105 int len = (int)STRLEN(ml_get_curline());
3106
3107 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003109 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 }
3114 }
3115 did_ai = FALSE;
3116#ifdef FEAT_SMARTINDENT
3117 did_si = FALSE;
3118 can_si = FALSE;
3119 can_si_back = FALSE;
3120#endif
3121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003122 // Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3123 // now in a different buffer.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003124 if (end_insert_pos != NULL)
3125 {
3126 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003127 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003128 curbuf->b_op_end = *end_insert_pos;
3129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130}
3131
3132/*
3133 * Set the last inserted text to a single character.
3134 * Used for the replace command.
3135 */
3136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003137set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138{
3139 char_u *s;
3140
3141 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (last_insert != NULL)
3144 {
3145 s = last_insert;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003146 // Use the CTRL-V only when entering a special char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 if (c < ' ' || c == DEL)
3148 *s++ = Ctrl_V;
3149 s = add_char2buf(c, s);
3150 *s++ = ESC;
3151 *s++ = NUL;
3152 last_insert_skip = 0;
3153 }
3154}
3155
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003156#if defined(EXITFREE) || defined(PROTO)
3157 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003158free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003159{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003160 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003161}
3162#endif
3163
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164/*
3165 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3166 * and CSI. Handle multi-byte characters.
3167 * Returns a pointer to after the added bytes.
3168 */
3169 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003170add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003172 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 int i;
3174 int len;
3175
3176 len = (*mb_char2bytes)(c, temp);
3177 for (i = 0; i < len; ++i)
3178 {
3179 c = temp[i];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003180 // Need to escape K_SPECIAL and CSI like in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (c == K_SPECIAL)
3182 {
3183 *s++ = K_SPECIAL;
3184 *s++ = KS_SPECIAL;
3185 *s++ = KE_FILLER;
3186 }
3187#ifdef FEAT_GUI
3188 else if (c == CSI)
3189 {
3190 *s++ = CSI;
3191 *s++ = KS_EXTRA;
3192 *s++ = (int)KE_CSI;
3193 }
3194#endif
3195 else
3196 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 return s;
3199}
3200
3201/*
3202 * move cursor to start of line
3203 * if flags & BL_WHITE move to first non-white
3204 * if flags & BL_SOL move to first non-white if startofline is set,
3205 * otherwise keep "curswant" column
3206 * if flags & BL_FIX don't leave the cursor on a NUL.
3207 */
3208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 if ((flags & BL_SOL) && !p_sol)
3212 coladvance(curwin->w_curswant);
3213 else
3214 {
3215 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 if (flags & (BL_WHITE | BL_SOL))
3219 {
3220 char_u *ptr;
3221
Bram Moolenaar1c465442017-03-12 20:10:05 +01003222 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3224 ++curwin->w_cursor.col;
3225 }
3226 curwin->w_set_curswant = TRUE;
3227 }
3228}
3229
3230/*
3231 * oneright oneleft cursor_down cursor_up
3232 *
3233 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003234 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 * Return OK when successful, FAIL when we hit a line of file boundary.
3236 */
3237
3238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003239oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240{
3241 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (virtual_active())
3245 {
3246 pos_T prevpos = curwin->w_cursor;
3247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003248 // Adjust for multi-wide char (excluding TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003250 coladvance(getviscol() + ((*ptr != TAB
3251 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 ? ptr2cells(ptr) : 1));
3253 curwin->w_set_curswant = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003254 // Return OK if the cursor moved, FAIL otherwise (at window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 return (prevpos.col != curwin->w_cursor.col
3256 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003260 if (*ptr == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003261 return FAIL; // already at the very end
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003262
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003263 if (has_mbyte)
3264 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003266 l = 1;
3267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003268 // move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3269 // contains "onemore".
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003270 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003271 return FAIL;
3272 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274 curwin->w_set_curswant = TRUE;
3275 return OK;
3276}
3277
3278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003279oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (virtual_active())
3282 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003283#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 int v = getviscol();
3287
3288 if (v == 0)
3289 return FAIL;
3290
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003291#ifdef FEAT_LINEBREAK
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003292 // We might get stuck on 'showbreak', skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 width = 1;
3294 for (;;)
3295 {
3296 coladvance(v - width);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003297 // getviscol() is slow, skip it when 'showbreak' is empty,
3298 // 'breakindent' is not set and there are no multi-byte
3299 // characters
Bram Moolenaaree857022019-11-09 23:26:40 +01003300 if ((*get_showbreak_value(curwin) == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003301 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 break;
3303 ++width;
3304 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003305#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308
3309 if (curwin->w_cursor.coladd == 1)
3310 {
3311 char_u *ptr;
3312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003313 // Adjust for multi-wide char (not a TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003315 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3316 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 curwin->w_cursor.coladd = 0;
3318 }
3319
3320 curwin->w_set_curswant = TRUE;
3321 return OK;
3322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
3324 if (curwin->w_cursor.col == 0)
3325 return FAIL;
3326
3327 curwin->w_set_curswant = TRUE;
3328 --curwin->w_cursor.col;
3329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003330 // if the character on the left of the current cursor is a multi-byte
3331 // character, move to its first byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 if (has_mbyte)
3333 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 return OK;
3335}
3336
3337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003338cursor_up(
3339 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003340 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 linenr_T lnum;
3343
3344 if (n > 0)
3345 {
3346 lnum = curwin->w_cursor.lnum;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003347 // This fails if the cursor is already in the first line or the count
3348 // is larger than the line number and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00003349 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 return FAIL;
3351 if (n >= lnum)
3352 lnum = 1;
3353 else
3354#ifdef FEAT_FOLDING
3355 if (hasAnyFolding(curwin))
3356 {
3357 /*
3358 * Count each sequence of folded lines as one logical line.
3359 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003360 // go to the start of the current fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 (void)hasFolding(lnum, &lnum, NULL);
3362
3363 while (n--)
3364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003365 // move up one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 --lnum;
3367 if (lnum <= 1)
3368 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003369 // If we entered a fold, move to the beginning, unless in
3370 // Insert mode or when 'foldopen' contains "all": it will open
3371 // in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3373 (void)hasFolding(lnum, &lnum, NULL);
3374 }
3375 if (lnum < 1)
3376 lnum = 1;
3377 }
3378 else
3379#endif
3380 lnum -= n;
3381 curwin->w_cursor.lnum = lnum;
3382 }
3383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003384 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 coladvance(curwin->w_curswant);
3386
3387 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003388 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
3390 return OK;
3391}
3392
3393/*
3394 * Cursor down a number of logical lines.
3395 */
3396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003397cursor_down(
3398 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003399 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 linenr_T lnum;
3402
3403 if (n > 0)
3404 {
3405 lnum = curwin->w_cursor.lnum;
3406#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003407 // Move to last line of fold, will fail if it's the end-of-file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 (void)hasFolding(lnum, NULL, &lnum);
3409#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003410 // This fails if the cursor is already in the last line or would move
3411 // beyond the last line and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00003412 if (lnum >= curbuf->b_ml.ml_line_count
3413 || (lnum + n > curbuf->b_ml.ml_line_count
3414 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 return FAIL;
3416 if (lnum + n >= curbuf->b_ml.ml_line_count)
3417 lnum = curbuf->b_ml.ml_line_count;
3418 else
3419#ifdef FEAT_FOLDING
3420 if (hasAnyFolding(curwin))
3421 {
3422 linenr_T last;
3423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003424 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 while (n--)
3426 {
3427 if (hasFolding(lnum, NULL, &last))
3428 lnum = last + 1;
3429 else
3430 ++lnum;
3431 if (lnum >= curbuf->b_ml.ml_line_count)
3432 break;
3433 }
3434 if (lnum > curbuf->b_ml.ml_line_count)
3435 lnum = curbuf->b_ml.ml_line_count;
3436 }
3437 else
3438#endif
3439 lnum += n;
3440 curwin->w_cursor.lnum = lnum;
3441 }
3442
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003443 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 coladvance(curwin->w_curswant);
3445
3446 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003447 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 return OK;
3450}
3451
3452/*
3453 * Stuff the last inserted text in the read buffer.
3454 * Last_insert actually is a copy of the redo buffer, so we
3455 * first have to remove the command.
3456 */
3457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003458stuff_inserted(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003459 int c, // Command character to be inserted
3460 long count, // Repeat this many times
3461 int no_esc) // Don't add an ESC at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
3463 char_u *esc_ptr;
3464 char_u *ptr;
3465 char_u *last_ptr;
3466 char_u last = NUL;
3467
3468 ptr = get_last_insert();
3469 if (ptr == NULL)
3470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003471 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 return FAIL;
3473 }
3474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003475 // may want to stuff the command character, to start Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (c != NUL)
3477 stuffcharReadbuff(c);
3478 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003479 *esc_ptr = NUL; // remove the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003481 // when the last char is either "0" or "^" it will be quoted if no ESC
3482 // comes after it OR if it will inserted more than once and "ptr"
3483 // starts with ^D. -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3485 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3486 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3487 {
3488 last = *last_ptr;
3489 *last_ptr = NUL;
3490 }
3491
3492 do
3493 {
3494 stuffReadbuff(ptr);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003495 // a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (last)
3497 stuffReadbuff((char_u *)(last == '0'
3498 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3499 : IF_EB("\026^", CTRL_V_STR "^")));
3500 }
3501 while (--count > 0);
3502
3503 if (last)
3504 *last_ptr = last;
3505
3506 if (esc_ptr != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003507 *esc_ptr = ESC; // put the ESC back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003509 // may want to stuff a trailing ESC, to get out of Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (!no_esc)
3511 stuffcharReadbuff(ESC);
3512
3513 return OK;
3514}
3515
3516 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003517get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
3519 if (last_insert == NULL)
3520 return NULL;
3521 return last_insert + last_insert_skip;
3522}
3523
3524/*
3525 * Get last inserted string, and remove trailing <Esc>.
3526 * Returns pointer to allocated memory (must be freed) or NULL.
3527 */
3528 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003529get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
3531 char_u *s;
3532 int len;
3533
3534 if (last_insert == NULL)
3535 return NULL;
3536 s = vim_strsave(last_insert + last_insert_skip);
3537 if (s != NULL)
3538 {
3539 len = (int)STRLEN(s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003540 if (len > 0 && s[len - 1] == ESC) // remove trailing ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 s[len - 1] = NUL;
3542 }
3543 return s;
3544}
3545
3546/*
3547 * Check the word in front of the cursor for an abbreviation.
3548 * Called when the non-id character "c" has been entered.
3549 * When an abbreviation is recognized it is removed from the text and
3550 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3551 */
3552 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003555 // Don't check for abbreviation in paste mode, when disabled and just
3556 // after moving around with cursor keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (p_paste || no_abbr || arrow_used)
3558 return FALSE;
3559
3560 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3561 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3562}
3563
3564/*
3565 * replace-stack functions
3566 *
3567 * When replacing characters, the replaced characters are remembered for each
3568 * new character. This is used to re-insert the old text when backspacing.
3569 *
3570 * There is a NUL headed list of characters for each character that is
3571 * currently in the file after the insertion point. When BS is used, one NUL
3572 * headed list is put back for the deleted character.
3573 *
3574 * For a newline, there are two NUL headed lists. One contains the characters
3575 * that the NL replaced. The extra one stores the characters after the cursor
3576 * that were deleted (always white space).
3577 *
3578 * Replace_offset is normally 0, in which case replace_push will add a new
3579 * character at the end of the stack. If replace_offset is not 0, that many
3580 * characters will be left on the stack above the newly inserted character.
3581 */
3582
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003583static char_u *replace_stack = NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003584static long replace_stack_nr = 0; // next entry in replace stack
3585static long replace_stack_len = 0; // max. number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
3587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003588replace_push(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003589 int c) // character that is replaced (NUL is none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
3591 char_u *p;
3592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003593 if (replace_stack_nr < replace_offset) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 return;
3595 if (replace_stack_len <= replace_stack_nr)
3596 {
3597 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003598 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003599 if (p == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601 replace_stack_len -= 50;
3602 return;
3603 }
3604 if (replace_stack != NULL)
3605 {
3606 mch_memmove(p, replace_stack,
3607 (size_t)(replace_stack_nr * sizeof(char_u)));
3608 vim_free(replace_stack);
3609 }
3610 replace_stack = p;
3611 }
3612 p = replace_stack + replace_stack_nr - replace_offset;
3613 if (replace_offset)
3614 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3615 *p = c;
3616 ++replace_stack_nr;
3617}
3618
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003619/*
3620 * Push a character onto the replace stack. Handles a multi-byte character in
3621 * reverse byte order, so that the first byte is popped off first.
3622 * Return the number of bytes done (includes composing characters).
3623 */
3624 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003625replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003626{
3627 int l = (*mb_ptr2len)(p);
3628 int j;
3629
3630 for (j = l - 1; j >= 0; --j)
3631 replace_push(p[j]);
3632 return l;
3633}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003634
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635/*
3636 * Pop one item from the replace stack.
3637 * return -1 if stack empty
3638 * return replaced character or NUL otherwise
3639 */
3640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 if (replace_stack_nr == 0)
3644 return -1;
3645 return (int)replace_stack[--replace_stack_nr];
3646}
3647
3648/*
3649 * Join the top two items on the replace stack. This removes to "off"'th NUL
3650 * encountered.
3651 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003652 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003653replace_join(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003654 int off) // offset for which NUL to remove
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 int i;
3657
3658 for (i = replace_stack_nr; --i >= 0; )
3659 if (replace_stack[i] == NUL && off-- <= 0)
3660 {
3661 --replace_stack_nr;
3662 mch_memmove(replace_stack + i, replace_stack + i + 1,
3663 (size_t)(replace_stack_nr - i));
3664 return;
3665 }
3666}
3667
3668/*
3669 * Pop bytes from the replace stack until a NUL is found, and insert them
3670 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3671 */
3672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003673replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 int cc;
3676 int oldState = State;
3677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003678 State = NORMAL; // don't want REPLACE here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 while ((cc = replace_pop()) > 0)
3680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 dec_cursor();
3683 }
3684 State = oldState;
3685}
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687/*
3688 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3689 * indicates a multi-byte char, pop the other bytes too.
3690 */
3691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003692mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003695 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 int i;
3697 int c;
3698
3699 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3700 {
3701 buf[0] = cc;
3702 for (i = 1; i < n; ++i)
3703 buf[i] = replace_pop();
3704 ins_bytes_len(buf, n);
3705 }
3706 else
3707 ins_char(cc);
3708
3709 if (enc_utf8)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003710 // Handle composing chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 for (;;)
3712 {
3713 c = replace_pop();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003714 if (c == -1) // stack empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 break;
3716 if ((n = MB_BYTE2LEN(c)) == 1)
3717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003718 // Not a multi-byte char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 replace_push(c);
3720 break;
3721 }
3722 else
3723 {
3724 buf[0] = c;
3725 for (i = 1; i < n; ++i)
3726 buf[i] = replace_pop();
3727 if (utf_iscomposing(utf_ptr2char(buf)))
3728 ins_bytes_len(buf, n);
3729 else
3730 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003731 // Not a composing char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 for (i = n - 1; i >= 0; --i)
3733 replace_push(buf[i]);
3734 break;
3735 }
3736 }
3737 }
3738}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739
3740/*
3741 * make the replace stack empty
3742 * (called when exiting replace mode)
3743 */
3744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003745replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003747 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 replace_stack_len = 0;
3749 replace_stack_nr = 0;
3750}
3751
3752/*
3753 * Handle doing a BS for one character.
3754 * cc < 0: replace stack empty, just move cursor
3755 * cc == 0: character was inserted, delete it
3756 * cc > 0: character was replaced, put cc (first byte of original char) back
3757 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003758 * When "limit_col" is >= 0, don't delete before this column. Matters when
3759 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 */
3761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003762replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
3764 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 int orig_len = 0;
3766 int ins_len;
3767 int orig_vcols = 0;
3768 colnr_T start_vcol;
3769 char_u *p;
3770 int i;
3771 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 cc = replace_pop();
3774 if (cc > 0)
3775 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003776#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003777 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003778
3779 if (curbuf->b_has_textprop)
3780 {
3781 // Do not adjust text properties for individual delete and insert
3782 // operations, do it afterwards on the resulting text.
3783 len_before = STRLEN(ml_get_curline());
3784 ++text_prop_frozen;
3785 }
3786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (State & VREPLACE_FLAG)
3788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003789 // Get the number of screen cells used by the character we are
3790 // going to delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3792 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (has_mbyte)
3795 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003796 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003798 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 replace_push(cc);
3800 }
3801 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
3803 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003805 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807 replace_pop_ins();
3808
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (State & VREPLACE_FLAG)
3810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003811 // Get the number of screen cells used by the inserted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003813 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 vcol = start_vcol;
3815 for (i = 0; i < ins_len; ++i)
3816 {
3817 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003818 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820 vcol -= start_vcol;
3821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003822 // Delete spaces that were inserted after the cursor to keep the
3823 // text aligned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 curwin->w_cursor.col += ins_len;
3825 while (vcol > orig_vcols && gchar_cursor() == ' ')
3826 {
3827 del_char(FALSE);
3828 ++orig_vcols;
3829 }
3830 curwin->w_cursor.col -= ins_len;
3831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
Bram Moolenaar196d1572019-01-02 23:47:18 +01003833 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003835
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003836#ifdef FEAT_PROP_POPUP
Bram Moolenaar196d1572019-01-02 23:47:18 +01003837 if (curbuf->b_has_textprop)
3838 {
3839 size_t len_now = STRLEN(ml_get_curline());
3840
3841 --text_prop_frozen;
3842 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003843 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003844 }
3845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003848 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849}
3850
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3852/*
3853 * Map Hebrew keyboard when in hkmap mode.
3854 */
3855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003856hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003858 if (p_hkmapp) // phonetic mapping, by Ilya Dogolazky
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3861 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3862 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3863 static char_u map[26] =
3864 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3865 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3866 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3867 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3868 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3869 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3870 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3871 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3872 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3873
3874 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3875 return (int)(map[CharOrd(c)] - 1 + p_aleph);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003876 // '-1'='sofit'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 else if (c == 'x')
3878 return 'X';
3879 else if (c == 'q')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003880 return '\''; // {geresh}={'}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 else if (c == 246)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003882 return ' '; // \"o --> ' ' for a german keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 else if (c == 228)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003884 return ' '; // \"a --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 else if (c == 252)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003886 return ' '; // \"u --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#ifdef EBCDIC
3888 else if (islower(c))
3889#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003890 // NOTE: islower() does not do the right thing for us on Linux so we
3891 // do this the same was as 5.7 and previous, so it works correctly on
3892 // all systems. Specifically, the e.g. Delete and Arrow keys are
3893 // munged and won't work if e.g. searching for Hebrew text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 else if (c >= 'a' && c <= 'z')
3895#endif
3896 return (int)(map[CharOrdLow(c)] + p_aleph);
3897 else
3898 return c;
3899 }
3900 else
3901 {
3902 switch (c)
3903 {
3904 case '`': return ';';
3905 case '/': return '.';
3906 case '\'': return ',';
3907 case 'q': return '/';
3908 case 'w': return '\'';
3909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003910 // Hebrew letters - set offset from 'a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 case ',': c = '{'; break;
3912 case '.': c = 'v'; break;
3913 case ';': c = 't'; break;
3914 default: {
3915 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3916
3917#ifdef EBCDIC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003918 // see note about islower() above
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (!islower(c))
3920#else
3921 if (c < 'a' || c > 'z')
3922#endif
3923 return c;
3924 c = str[CharOrdLow(c)];
3925 break;
3926 }
3927 }
3928
3929 return (int)(CharOrdLow(c) + p_aleph);
3930 }
3931}
3932#endif
3933
3934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003935ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937 int need_redraw = FALSE;
3938 int regname;
3939 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003940 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
3942 /*
3943 * If we are going to wait for a character, show a '"'.
3944 */
3945 pc_status = PC_STATUS_UNSET;
3946 if (redrawing() && !char_avail())
3947 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003948 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00003949 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
3951 edit_putchar('"', TRUE);
3952#ifdef FEAT_CMDL_INFO
3953 add_to_showcmd_c(Ctrl_R);
3954#endif
3955 }
3956
3957#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003958 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#endif
3960
3961 /*
3962 * Don't map the register name. This also prevents the mode message to be
3963 * deleted when ESC is hit.
3964 */
3965 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003966 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003967 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003971 // Get a third key for literal register insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 literally = regname;
3973#ifdef FEAT_CMDL_INFO
3974 add_to_showcmd_c(literally);
3975#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003976 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
3979 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003980 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
3982#ifdef FEAT_EVAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003983 // Don't call u_sync() while typing the expression or giving an error
3984 // message for it. Only call it explicitly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 ++no_u_sync;
3986 if (regname == '=')
3987 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003988 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003989# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003991# endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003992 // Sync undo when evaluating the expression calls setline() or
3993 // append(), so that it can be undone separately.
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003994 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003995
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003997
3998 // Cursor may be moved back a column.
3999 curwin->w_cursor = curpos;
4000 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004001# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004002 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 if (im_on)
4004 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00004007 if (regname == NUL || !valid_yank_reg(regname, FALSE))
4008 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004009 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004010 need_redraw = TRUE; // remove the '"'
Bram Moolenaar677ee682005-01-27 14:41:15 +00004011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 else
4013 {
4014#endif
4015 if (literally == Ctrl_O || literally == Ctrl_P)
4016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004017 // Append the command to the redo buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 AppendCharToRedobuff(Ctrl_R);
4019 AppendCharToRedobuff(literally);
4020 AppendCharToRedobuff(regname);
4021
4022 do_put(regname, BACKWARD, 1L,
4023 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
4024 }
4025 else if (insert_reg(regname, literally) == FAIL)
4026 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004027 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004028 need_redraw = TRUE; // remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004030 else if (stop_insert_mode)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004031 // When the '=' register was used and a function was invoked that
4032 // did ":stopinsert" then stuff_empty() returns FALSE but we won't
4033 // insert anything, need to remove the '"'
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004034 need_redraw = TRUE;
4035
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036#ifdef FEAT_EVAL
4037 }
4038 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004039 if (u_sync_once == 1)
4040 ins_need_undo = TRUE;
4041 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042#endif
4043#ifdef FEAT_CMDL_INFO
4044 clear_showcmd();
4045#endif
4046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004047 // If the inserted register is empty, we need to remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (need_redraw || stuff_empty())
4049 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004051 // Disallow starting Visual mode here, would get a weird mode.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004052 if (!vis_active && VIsual_active)
4053 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054}
4055
4056/*
4057 * CTRL-G commands in Insert mode.
4058 */
4059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004060ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
4062 int c;
4063
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004064 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 /*
4068 * Don't map the second key. This also prevents the mode message to be
4069 * deleted when ESC is hit.
4070 */
4071 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004072 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004073 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004075 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 switch (c)
4077 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004078 // CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 case K_UP:
4080 case Ctrl_K:
4081 case 'k': ins_up(TRUE);
4082 break;
4083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004084 // CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 case K_DOWN:
4086 case Ctrl_J:
4087 case 'j': ins_down(TRUE);
4088 break;
4089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004090 // CTRL-G u: start new undoable edit
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004091 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004094 // Need to reset Insstart, esp. because a BS that joins
4095 // a line to the previous one must save for undo.
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004096 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004097 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 break;
4099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004100 // CTRL-G U: do not break undo with the next char
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004101 case 'U':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004102 // Allow one left/right cursor movement with the next char,
4103 // without breaking undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004104 dont_sync_undo = MAYBE;
4105 break;
4106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004107 // Unknown CTRL-G command, reserved for future expansion.
Bram Moolenaar165bc692015-07-21 17:53:25 +02004108 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110}
4111
4112/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004113 * CTRL-^ in Insert mode.
4114 */
4115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004116ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004117{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004118 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004120 // ":lmap" mappings exists, Toggle use of ":lmap" mappings.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004121 if (State & LANGMAP)
4122 {
4123 curbuf->b_p_iminsert = B_IMODE_NONE;
4124 State &= ~LANGMAP;
4125 }
4126 else
4127 {
4128 curbuf->b_p_iminsert = B_IMODE_LMAP;
4129 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004130#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004131 im_set_active(FALSE);
4132#endif
4133 }
4134 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004135#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004136 else
4137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004138 // There are no ":lmap" mappings, toggle IM
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 if (im_get_status())
4140 {
4141 curbuf->b_p_iminsert = B_IMODE_NONE;
4142 im_set_active(FALSE);
4143 }
4144 else
4145 {
4146 curbuf->b_p_iminsert = B_IMODE_IM;
4147 State &= ~LANGMAP;
4148 im_set_active(TRUE);
4149 }
4150 }
4151#endif
4152 set_iminsert_global();
4153 showmode();
4154#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004155 // may show different cursor shape or color
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156 if (gui.in_use)
4157 gui_update_cursor(TRUE, FALSE);
4158#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004159#if defined(FEAT_KEYMAP)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004160 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004161 status_redraw_curbuf();
4162#endif
4163}
4164
4165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 * Handle ESC in insert mode.
4167 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4168 * insert.
4169 */
4170 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004171ins_esc(
4172 long *count,
4173 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004174 int nomove) // don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 int temp;
4177 static int disabled_redraw = FALSE;
4178
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004179#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180 check_spell_redraw();
4181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 temp = curwin->w_cursor.col;
4184 if (disabled_redraw)
4185 {
4186 --RedrawingDisabled;
4187 disabled_redraw = FALSE;
4188 }
4189 if (!arrow_used)
4190 {
4191 /*
4192 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004193 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4194 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 */
4196 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004197 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
4199 /*
4200 * Repeating insert may take a long time. Check for
4201 * interrupt now and then.
4202 */
4203 if (*count > 0)
4204 {
4205 line_breakcheck();
4206 if (got_int)
4207 *count = 0;
4208 }
4209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004210 if (--*count > 0) // repeat what was typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004212 // Vi repeats the insert without replacing characters.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004213 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4214 State &= ~REPLACE_FLAG;
4215
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 (void)start_redo_ins();
4217 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004218 stuffRedoReadbuff(ESC_STR); // no ESC in redo buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 ++RedrawingDisabled;
4220 disabled_redraw = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004221 return FALSE; // repeat the insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004223 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 undisplay_dollar();
4225 }
4226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004227 // When an autoindent was removed, curswant stays after the
4228 // indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4230 curwin->w_set_curswant = TRUE;
4231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004232 // Remember the last Insert position in the '^ mark.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (!cmdmod.keepjumps)
4234 curbuf->b_last_insert = curwin->w_cursor;
4235
4236 /*
4237 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004238 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004240 if (!nomove
4241 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004242 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004243 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004244 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#ifdef FEAT_RIGHTLEFT
4246 && !revins_on
4247#endif
4248 )
4249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4251 {
4252 oneleft();
4253 if (restart_edit != NUL)
4254 ++curwin->w_cursor.coladd;
4255 }
4256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
4258 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004259 // Correct cursor for multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (has_mbyte)
4261 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 }
4264
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004265#ifdef HAVE_INPUT_METHOD
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004266 // Disable IM to allow typing English directly for Normal mode commands.
4267 // When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4268 // well).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if (!(State & LANGMAP))
4270 im_save_status(&curbuf->b_p_iminsert);
4271 im_set_active(FALSE);
4272#endif
4273
4274 State = NORMAL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004275 // need to position cursor again (e.g. when on a TAB )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 changed_cline_bef_curs();
4277
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279#ifdef CURSOR_SHAPE
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004280 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004282 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004283 {
4284 // Re-enable bracketed paste mode.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004285 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004287 // Re-enable modifyOtherKeys.
4288 out_str(T_CTI);
4289 }
4290
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004291 // When recording or for CTRL-O, need to display the new mode.
4292 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004293 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004295 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004296 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004298 return TRUE; // exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299}
4300
4301#ifdef FEAT_RIGHTLEFT
4302/*
4303 * Toggle language: hkmap and revins_on.
4304 * Move to end of reverse inserted text.
4305 */
4306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004307ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
4309 if (revins_on && revins_chars && revins_scol >= 0)
4310 {
4311 while (gchar_cursor() != NUL && revins_chars--)
4312 ++curwin->w_cursor.col;
4313 }
4314 p_ri = !p_ri;
4315 revins_on = (State == INSERT && p_ri);
4316 if (revins_on)
4317 {
4318 revins_scol = curwin->w_cursor.col;
4319 revins_legal++;
4320 revins_chars = 0;
4321 undisplay_dollar();
4322 }
4323 else
4324 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004325 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 showmode();
4327}
4328#endif
4329
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330/*
4331 * If 'keymodel' contains "startsel", may start selection.
4332 * Returns TRUE when a CTRL-O and other keys stuffed.
4333 */
4334 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004335ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
4337 if (km_startsel)
4338 switch (c)
4339 {
4340 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 case K_PAGEUP:
4343 case K_KPAGEUP:
4344 case K_PAGEDOWN:
4345 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004346# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 case K_LEFT:
4348 case K_RIGHT:
4349 case K_UP:
4350 case K_DOWN:
4351 case K_END:
4352 case K_HOME:
4353# endif
4354 if (!(mod_mask & MOD_MASK_SHIFT))
4355 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004356 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 case K_S_LEFT:
4358 case K_S_RIGHT:
4359 case K_S_UP:
4360 case K_S_DOWN:
4361 case K_S_END:
4362 case K_S_HOME:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004363 // Start selection right away, the cursor can move with
4364 // CTRL-O when beyond the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 start_selection();
4366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 // Execute the key in (insert) Select mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 stuffcharReadbuff(Ctrl_O);
4369 if (mod_mask)
4370 {
4371 char_u buf[4];
4372
4373 buf[0] = K_SPECIAL;
4374 buf[1] = KS_MODIFIER;
4375 buf[2] = mod_mask;
4376 buf[3] = NUL;
4377 stuffReadbuff(buf);
4378 }
4379 stuffcharReadbuff(c);
4380 return TRUE;
4381 }
4382 return FALSE;
4383}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004386 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 */
4388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004389ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004391#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004393 (char_u *)((State & REPLACE_FLAG) ? "i"
4394 : replaceState == VREPLACE ? "v"
4395 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004396#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004397 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 if (State & REPLACE_FLAG)
4399 State = INSERT | (State & LANGMAP);
4400 else
4401 State = replaceState | (State & LANGMAP);
4402 AppendCharToRedobuff(K_INS);
4403 showmode();
4404#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004405 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004406#endif
4407}
4408
4409/*
4410 * Pressed CTRL-O in Insert mode.
4411 */
4412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004413ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004414{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415 if (State & VREPLACE_FLAG)
4416 restart_edit = 'V';
4417 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 if (State & REPLACE_FLAG)
4419 restart_edit = 'R';
4420 else
4421 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004422 if (virtual_active())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004423 ins_at_eol = FALSE; // cursor always keeps its column
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 ins_at_eol = (gchar_cursor() == NUL);
4426}
4427
4428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 * If the cursor is on an indent, ^T/^D insert/delete one
4430 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004431 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 * with vi. But vi only supports ^T and ^D after an
4433 * autoindent, we support it everywhere.
4434 */
4435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004436ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
4438 if (stop_arrow() == FAIL)
4439 return;
4440 AppendCharToRedobuff(c);
4441
4442 /*
4443 * 0^D and ^^D: remove all indent.
4444 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004445 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4446 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004449 (void)del_char(FALSE); // delete the '^' or '0'
4450 // In Replace mode, restore the characters that '^' or '0' replaced.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 if (State & REPLACE_FLAG)
4452 replace_pop_ins();
4453 if (lastc == '^')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004454 old_indent = get_indent(); // remember curr. indent
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004455 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004458 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
4460 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4461 did_ai = FALSE;
4462#ifdef FEAT_SMARTINDENT
4463 did_si = FALSE;
4464 can_si = FALSE;
4465 can_si_back = FALSE;
4466#endif
4467#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004468 can_cindent = FALSE; // no cindenting after ^D or ^T
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469#endif
4470}
4471
4472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004473ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
4475 int temp;
4476
4477 if (stop_arrow() == FAIL)
4478 return;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 if (gchar_cursor() == NUL) // delete newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
4481 temp = curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 if (!can_bs(BS_EOL) // only if "eol" included
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004483 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004484 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 curwin->w_cursor.col = temp;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 // Adjust orig_line_count in case more lines have been deleted than
4489 // have been added. That makes sure, that open_line() later
4490 // can access all buffer lines correctly
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004491 if (State & VREPLACE_FLAG &&
4492 orig_line_count > curbuf->b_ml.ml_line_count)
4493 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004496 else if (del_char(FALSE) == FAIL) // delete char under cursor
Bram Moolenaar165bc692015-07-21 17:53:25 +02004497 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 did_ai = FALSE;
4499#ifdef FEAT_SMARTINDENT
4500 did_si = FALSE;
4501 can_si = FALSE;
4502 can_si_back = FALSE;
4503#endif
4504 AppendCharToRedobuff(K_DEL);
4505}
4506
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004507/*
4508 * Delete one character for ins_bs().
4509 */
4510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004511ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004512{
4513 dec_cursor();
4514 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4515 if (State & REPLACE_FLAG)
4516 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004517 // Don't delete characters before the insert point when in
4518 // Replace mode
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004519 if (curwin->w_cursor.lnum != Insstart.lnum
4520 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004521 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004522 }
4523 else
4524 (void)del_char(FALSE);
4525}
4526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527/*
4528 * Handle Backspace, delete-word and delete-line in Insert mode.
4529 * Return TRUE when backspace was actually used.
4530 */
4531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004532ins_bs(
4533 int c,
4534 int mode,
4535 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536{
4537 linenr_T lnum;
4538 int cc;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004539 int temp = 0; // init for GCC
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004540 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 colnr_T mincol;
4542 int did_backspace = FALSE;
4543 int in_indent;
4544 int oldState;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004545 int cpc[MAX_MCO]; // composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
4547 /*
4548 * can't delete anything in an empty file
4549 * can't backup past first character in buffer
4550 * can't backup past starting point unless 'backspace' > 1
4551 * can backup to a previous line if 'backspace' == 0
4552 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004553 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 || (
4555#ifdef FEAT_RIGHTLEFT
4556 !revins_on &&
4557#endif
4558 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4559 || (!can_bs(BS_START)
4560 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004561 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4562 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4564 && curwin->w_cursor.col <= ai_col)
4565 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4566 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004567 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 return FALSE;
4569 }
4570
4571 if (stop_arrow() == FAIL)
4572 return FALSE;
4573 in_indent = inindent(0);
4574#ifdef FEAT_CINDENT
4575 if (in_indent)
4576 can_cindent = FALSE;
4577#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004578 end_comment_pending = NUL; // After BS, don't auto-end comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004580 if (revins_on) // put cursor after last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 inc_cursor();
4582#endif
4583
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004584 // Virtualedit:
4585 // BACKSPACE_CHAR eats a virtual space
4586 // BACKSPACE_WORD eats all coladd
4587 // BACKSPACE_LINE eats all coladd and keeps going
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (curwin->w_cursor.coladd > 0)
4589 {
4590 if (mode == BACKSPACE_CHAR)
4591 {
4592 --curwin->w_cursor.coladd;
4593 return TRUE;
4594 }
4595 if (mode == BACKSPACE_WORD)
4596 {
4597 curwin->w_cursor.coladd = 0;
4598 return TRUE;
4599 }
4600 curwin->w_cursor.coladd = 0;
4601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004604 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 */
4606 if (curwin->w_cursor.col == 0)
4607 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004608 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004609 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#ifdef FEAT_RIGHTLEFT
4611 || revins_on
4612#endif
4613 )
4614 {
4615 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4616 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4617 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004618 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004619 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621 /*
4622 * In replace mode:
4623 * cc < 0: NL was inserted, delete it
4624 * cc >= 0: NL was replaced, put original characters back
4625 */
4626 cc = -1;
4627 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 cc = replace_pop(); // returns -1 if NL was inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 /*
4630 * In replace mode, in the line we started replacing, we only move the
4631 * cursor.
4632 */
4633 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4634 {
4635 dec_cursor();
4636 }
4637 else
4638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (!(State & VREPLACE_FLAG)
4640 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004642 temp = gchar_cursor(); // remember current char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004645 // When "aw" is in 'formatoptions' we must delete the space at
4646 // the end of the line, otherwise the line will be broken
4647 // again when auto-formatting.
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004648 if (has_format_option(FO_AUTO)
4649 && has_format_option(FO_WHITE_PAR))
4650 {
4651 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4652 TRUE);
4653 int len;
4654
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004655 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004656 if (len > 0 && ptr[len - 1] == ' ')
4657 ptr[len - 1] = NUL;
4658 }
4659
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004660 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (temp == NUL && gchar_cursor() != NUL)
4662 inc_cursor();
4663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 else
4665 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
4667 /*
4668 * In REPLACE mode we have to put back the text that was replaced
4669 * by the NL. On the replace stack is first a NUL-terminated
4670 * sequence of characters that were deleted and then the
4671 * characters that NL replaced.
4672 */
4673 if (State & REPLACE_FLAG)
4674 {
4675 /*
4676 * Do the next ins_char() in NORMAL state, to
4677 * prevent ins_char() from replacing characters and
4678 * avoiding showmatch().
4679 */
4680 oldState = State;
4681 State = NORMAL;
4682 /*
4683 * restore characters (blanks) deleted after cursor
4684 */
4685 while (cc > 0)
4686 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004687 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004689 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 cc = replace_pop();
4691 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004692 // restore the characters that NL replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 replace_pop_ins();
4694 State = oldState;
4695 }
4696 }
4697 did_ai = FALSE;
4698 }
4699 else
4700 {
4701 /*
4702 * Delete character(s) before the cursor.
4703 */
4704#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004705 if (revins_on) // put cursor on last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 dec_cursor();
4707#endif
4708 mincol = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004709 // keep indent
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004710 if (mode == BACKSPACE_LINE
4711 && (curbuf->b_p_ai
4712#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004713 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004714#endif
4715 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716#ifdef FEAT_RIGHTLEFT
4717 && !revins_on
4718#endif
4719 )
4720 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004721 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004723 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004725 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
4727
4728 /*
4729 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4730 */
4731 if ( mode == BACKSPACE_CHAR
4732 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004733 || ((get_sts_value() != 0
4734#ifdef FEAT_VARTABS
4735 || tabstop_count(curbuf->b_p_vsts_array)
4736#endif
4737 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004738 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 && (*(ml_get_cursor() - 1) == TAB
4740 || (*(ml_get_cursor() - 1) == ' '
4741 && (!*inserted_space_p
4742 || arrow_used))))))
4743 {
4744 int ts;
4745 colnr_T vcol;
4746 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004747 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
4749 *inserted_space_p = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004750 // Compute the virtual column where we want to be. Since
4751 // 'showbreak' may get in the way, need to get the last column of
4752 // the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004754 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 dec_cursor();
4756 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4757 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004758#ifdef FEAT_VARTABS
4759 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004760 {
4761 ts = (int)get_sw_value(curbuf);
4762 want_vcol = (want_vcol / ts) * ts;
4763 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004764 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004765 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004766 curbuf->b_p_vsts_array);
4767#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004768 if (p_sta && in_indent)
4769 ts = (int)get_sw_value(curbuf);
4770 else
4771 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004775 // delete characters until we are at or before want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004777 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004778 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 // insert extra spaces until we are at want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 while (vcol < want_vcol)
4782 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004783 // Remember the first char we inserted
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004784 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4785 && curwin->w_cursor.col < Insstart_orig.col)
4786 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (State & VREPLACE_FLAG)
4789 ins_char(' ');
4790 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
4792 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004793 if ((State & REPLACE_FLAG))
4794 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4797 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004799 // If we are now back where we started delete one character. Can
4800 // happen when using 'sts' and 'linebreak'.
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004801 if (vcol >= start_vcol)
4802 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804
4805 /*
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004806 * Delete up to starting point, start of line or previous word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004808 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004810 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004812 if (has_mbyte)
4813 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004814 do
4815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004817 if (!revins_on) // put cursor on char to be deleted
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004820
4821 cc = gchar_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // look multi-byte character class
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004823 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004825 prev_cclass = cclass;
4826 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004829 // start of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004830 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4831 {
4832 mode = BACKSPACE_WORD_NOT_SPACE;
4833 temp = vim_iswordc(cc);
4834 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004835 // end of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004836 else if (mode == BACKSPACE_WORD_NOT_SPACE
4837 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004838 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004841 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004843 inc_cursor();
4844#ifdef FEAT_RIGHTLEFT
4845 else if (State & REPLACE_FLAG)
4846 dec_cursor();
4847#endif
4848 break;
4849 }
4850 if (State & REPLACE_FLAG)
4851 replace_do_bs(-1);
4852 else
4853 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004854 if (enc_utf8 && p_deco)
4855 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004856 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004857 /*
4858 * If there are combining characters and 'delcombine' is set
4859 * move the cursor back. Don't back up before the base
4860 * character.
4861 */
4862 if (enc_utf8 && p_deco && cpc[0] != NUL)
4863 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004864#ifdef FEAT_RIGHTLEFT
4865 if (revins_chars)
4866 {
4867 revins_chars--;
4868 revins_legal++;
4869 }
4870 if (revins_on && gchar_cursor() == NUL)
4871 break;
4872#endif
4873 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004874 // Just a single backspace?:
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004875 if (mode == BACKSPACE_CHAR)
4876 break;
4877 } while (
4878#ifdef FEAT_RIGHTLEFT
4879 revins_on ||
4880#endif
4881 (curwin->w_cursor.col > mincol
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02004882 && (can_bs(BS_NOSTOP)
4883 || (curwin->w_cursor.lnum != Insstart_orig.lnum
4884 || curwin->w_cursor.col != Insstart_orig.col)
4885 )));
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 did_backspace = TRUE;
4888 }
4889#ifdef FEAT_SMARTINDENT
4890 did_si = FALSE;
4891 can_si = FALSE;
4892 can_si_back = FALSE;
4893#endif
4894 if (curwin->w_cursor.col <= 1)
4895 did_ai = FALSE;
4896 /*
4897 * It's a little strange to put backspaces into the redo
4898 * buffer, but it makes auto-indent a lot easier to deal
4899 * with.
4900 */
4901 AppendCharToRedobuff(c);
4902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 // If deleted before the insertion point, adjust it
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004904 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004905 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004906 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004908 // vi behaviour: the cursor moves backward but the character that
4909 // was there remains visible
4910 // Vim behaviour: the cursor moves backward and the character that
4911 // was there is erased from the screen.
4912 // We can emulate the vi behaviour by pretending there is a dollar
4913 // displayed even when there isn't.
4914 // --pkv Sun Jan 19 01:56:40 EST 2003
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004915 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 dollar_vcol = curwin->w_virtcol;
4917
Bram Moolenaarce3be472008-01-14 19:12:28 +00004918#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004919 // When deleting a char the cursor line must never be in a closed fold.
4920 // E.g., when 'foldmethod' is indent and deleting the first non-white
4921 // char before a Tab.
Bram Moolenaarce3be472008-01-14 19:12:28 +00004922 if (did_backspace)
4923 foldOpenCursor();
4924#endif
4925
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return did_backspace;
4927}
4928
Bram Moolenaarec2da362017-01-21 20:04:22 +01004929/*
4930 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4931 * P_PE literally.
4932 * When "drop" is TRUE then consume the text and drop it.
4933 */
4934 int
4935bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4936{
4937 int c;
4938 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4939 int idx = 0;
4940 char_u *end = find_termcode((char_u *)"PE");
4941 int ret_char = -1;
4942 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004943 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004945 // If the end code is too long we can't detect it, read everything.
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01004946 if (end != NULL && STRLEN(end) >= NUMBUFLEN)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004947 end = NULL;
4948 ++no_mapping;
4949 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004950 if (!p_paste)
4951 // Also have the side effects of setting 'paste' to make it work much
4952 // faster.
4953 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004954
Bram Moolenaarec2da362017-01-21 20:04:22 +01004955 for (;;)
4956 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004957 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004958 if (end == NULL && vpeekc() == NUL)
4959 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004960 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004961 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004962 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004963 if (c == NUL || got_int || (ex_normal_busy > 0 && c == Ctrl_C))
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004964 // When CTRL-C was encountered the typeahead will be flushed and we
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004965 // won't get the end sequence. Except when using ":normal".
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004966 break;
4967
Bram Moolenaarec2da362017-01-21 20:04:22 +01004968 if (has_mbyte)
4969 idx += (*mb_char2bytes)(c, buf + idx);
4970 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004971 buf[idx++] = c;
4972 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004973 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004974 {
4975 if (end[idx] == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004976 break; // Found the end of paste code.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004977 continue;
4978 }
4979 if (!drop)
4980 {
4981 switch (mode)
4982 {
4983 case PASTE_CMDLINE:
4984 put_on_cmdline(buf, idx, TRUE);
4985 break;
4986
4987 case PASTE_EX:
4988 if (gap != NULL && ga_grow(gap, idx) == OK)
4989 {
4990 mch_memmove((char *)gap->ga_data + gap->ga_len,
4991 buf, (size_t)idx);
4992 gap->ga_len += idx;
4993 }
4994 break;
4995
4996 case PASTE_INSERT:
4997 if (stop_arrow() == OK)
4998 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004999 c = buf[0];
5000 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
5001 ins_eol(c);
5002 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01005003 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005004 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01005005 AppendToRedobuffLit(buf, idx);
5006 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01005007 }
5008 break;
5009
5010 case PASTE_ONE_CHAR:
5011 if (ret_char == -1)
5012 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01005013 if (has_mbyte)
5014 ret_char = (*mb_ptr2char)(buf);
5015 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005016 ret_char = buf[0];
5017 }
5018 break;
5019 }
5020 }
5021 idx = 0;
5022 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005023
Bram Moolenaarec2da362017-01-21 20:04:22 +01005024 --no_mapping;
5025 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005026 if (!save_paste)
5027 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01005028
5029 return ret_char;
5030}
5031
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005032#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00005033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005034ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005035{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005036 // We will be leaving the current window, unless closing another tab.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005037 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
5038 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
5039 {
5040 undisplay_dollar();
5041 start_arrow(&curwin->w_cursor);
5042# ifdef FEAT_CINDENT
5043 can_cindent = TRUE;
5044# endif
5045 }
5046
5047 if (c == K_TABLINE)
5048 goto_tabpage(current_tab);
5049 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005050 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005051 handle_tabmenu();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005052 redraw_statuslines(); // will redraw the tabline when needed
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005053 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005054}
5055#endif
5056
5057#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005059ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061 pos_T tpos;
5062
5063 undisplay_dollar();
5064 tpos = curwin->w_cursor;
5065 if (gui_do_scroll())
5066 {
5067 start_arrow(&tpos);
5068# ifdef FEAT_CINDENT
5069 can_cindent = TRUE;
5070# endif
5071 }
5072}
5073
5074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005075ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076{
5077 pos_T tpos;
5078
5079 undisplay_dollar();
5080 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005081 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
5083 start_arrow(&tpos);
5084# ifdef FEAT_CINDENT
5085 can_cindent = TRUE;
5086# endif
5087 }
5088}
5089#endif
5090
5091 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005092ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
5094 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005095 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096
5097#ifdef FEAT_FOLDING
5098 if ((fdo_flags & FDO_HOR) && KeyTyped)
5099 foldOpenCursor();
5100#endif
5101 undisplay_dollar();
5102 tpos = curwin->w_cursor;
5103 if (oneleft() == OK)
5104 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005105#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005106 // Only call start_arrow() when not busy with preediting, it will
5107 // break undo. K_LEFT is inserted in im_correct_cursor().
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005108 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005109#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005110 {
5111 start_arrow_with_change(&tpos, end_change);
5112 if (!end_change)
5113 AppendCharToRedobuff(K_LEFT);
5114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // If exit reversed string, position is fixed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5118 revins_legal++;
5119 revins_chars++;
5120#endif
5121 }
5122
5123 /*
5124 * if 'whichwrap' set for cursor in insert mode may go to
5125 * previous line
5126 */
5127 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005129 // always break undo when moving upwards/downwards, else undo may break
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 start_arrow(&tpos);
5131 --(curwin->w_cursor.lnum);
5132 coladvance((colnr_T)MAXCOL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005133 curwin->w_set_curswant = TRUE; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005136 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005137 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138}
5139
5140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005141ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142{
5143 pos_T tpos;
5144
5145#ifdef FEAT_FOLDING
5146 if ((fdo_flags & FDO_HOR) && KeyTyped)
5147 foldOpenCursor();
5148#endif
5149 undisplay_dollar();
5150 tpos = curwin->w_cursor;
5151 if (c == K_C_HOME)
5152 curwin->w_cursor.lnum = 1;
5153 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 curwin->w_curswant = 0;
5156 start_arrow(&tpos);
5157}
5158
5159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005160ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162 pos_T tpos;
5163
5164#ifdef FEAT_FOLDING
5165 if ((fdo_flags & FDO_HOR) && KeyTyped)
5166 foldOpenCursor();
5167#endif
5168 undisplay_dollar();
5169 tpos = curwin->w_cursor;
5170 if (c == K_C_END)
5171 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5172 coladvance((colnr_T)MAXCOL);
5173 curwin->w_curswant = MAXCOL;
5174
5175 start_arrow(&tpos);
5176}
5177
5178 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005179ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005181 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182#ifdef FEAT_FOLDING
5183 if ((fdo_flags & FDO_HOR) && KeyTyped)
5184 foldOpenCursor();
5185#endif
5186 undisplay_dollar();
5187 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5188 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005189 start_arrow_with_change(&curwin->w_cursor, end_change);
5190 if (!end_change)
5191 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 (void)bck_word(1L, FALSE, FALSE);
5193 curwin->w_set_curswant = TRUE;
5194 }
5195 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005196 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005197 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198}
5199
5200 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005201ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005203 int end_change = dont_sync_undo == FALSE; // end undoable change
5204
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#ifdef FEAT_FOLDING
5206 if ((fdo_flags & FDO_HOR) && KeyTyped)
5207 foldOpenCursor();
5208#endif
5209 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005210 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005212 start_arrow_with_change(&curwin->w_cursor, end_change);
5213 if (!end_change)
5214 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (virtual_active())
5217 oneright();
5218 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005221 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 ++curwin->w_cursor.col;
5224 }
5225
5226#ifdef FEAT_RIGHTLEFT
5227 revins_legal++;
5228 if (revins_chars)
5229 revins_chars--;
5230#endif
5231 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // if 'whichwrap' set for cursor in insert mode, may move the
5233 // cursor to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 else if (vim_strchr(p_ww, ']') != NULL
5235 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5236 {
5237 start_arrow(&curwin->w_cursor);
5238 curwin->w_set_curswant = TRUE;
5239 ++curwin->w_cursor.lnum;
5240 curwin->w_cursor.col = 0;
5241 }
5242 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005243 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005244 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245}
5246
5247 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005248ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005250 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#ifdef FEAT_FOLDING
5252 if ((fdo_flags & FDO_HOR) && KeyTyped)
5253 foldOpenCursor();
5254#endif
5255 undisplay_dollar();
5256 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5257 || gchar_cursor() != NUL)
5258 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005259 start_arrow_with_change(&curwin->w_cursor, end_change);
5260 if (!end_change)
5261 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 (void)fwd_word(1L, FALSE, 0);
5263 curwin->w_set_curswant = TRUE;
5264 }
5265 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005266 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005267 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268}
5269
5270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005271ins_up(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273{
5274 pos_T tpos;
5275 linenr_T old_topline = curwin->w_topline;
5276#ifdef FEAT_DIFF
5277 int old_topfill = curwin->w_topfill;
5278#endif
5279
5280 undisplay_dollar();
5281 tpos = curwin->w_cursor;
5282 if (cursor_up(1L, TRUE) == OK)
5283 {
5284 if (startcol)
5285 coladvance(getvcol_nolist(&Insstart));
5286 if (old_topline != curwin->w_topline
5287#ifdef FEAT_DIFF
5288 || old_topfill != curwin->w_topfill
5289#endif
5290 )
5291 redraw_later(VALID);
5292 start_arrow(&tpos);
5293#ifdef FEAT_CINDENT
5294 can_cindent = TRUE;
5295#endif
5296 }
5297 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005298 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299}
5300
5301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005302ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
5304 pos_T tpos;
5305
5306 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005307
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005308 if (mod_mask & MOD_MASK_CTRL)
5309 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005310 // <C-PageUp>: tab page back
Bram Moolenaarbc444822006-10-17 11:37:50 +00005311 if (first_tabpage->tp_next != NULL)
5312 {
5313 start_arrow(&curwin->w_cursor);
5314 goto_tabpage(-1);
5315 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005316 return;
5317 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 tpos = curwin->w_cursor;
5320 if (onepage(BACKWARD, 1L) == OK)
5321 {
5322 start_arrow(&tpos);
5323#ifdef FEAT_CINDENT
5324 can_cindent = TRUE;
5325#endif
5326 }
5327 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005328 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329}
5330
5331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005332ins_down(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005333 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334{
5335 pos_T tpos;
5336 linenr_T old_topline = curwin->w_topline;
5337#ifdef FEAT_DIFF
5338 int old_topfill = curwin->w_topfill;
5339#endif
5340
5341 undisplay_dollar();
5342 tpos = curwin->w_cursor;
5343 if (cursor_down(1L, TRUE) == OK)
5344 {
5345 if (startcol)
5346 coladvance(getvcol_nolist(&Insstart));
5347 if (old_topline != curwin->w_topline
5348#ifdef FEAT_DIFF
5349 || old_topfill != curwin->w_topfill
5350#endif
5351 )
5352 redraw_later(VALID);
5353 start_arrow(&tpos);
5354#ifdef FEAT_CINDENT
5355 can_cindent = TRUE;
5356#endif
5357 }
5358 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005359 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360}
5361
5362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005363ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
5365 pos_T tpos;
5366
5367 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005368
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005369 if (mod_mask & MOD_MASK_CTRL)
5370 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005371 // <C-PageDown>: tab page forward
Bram Moolenaarbc444822006-10-17 11:37:50 +00005372 if (first_tabpage->tp_next != NULL)
5373 {
5374 start_arrow(&curwin->w_cursor);
5375 goto_tabpage(0);
5376 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005377 return;
5378 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 tpos = curwin->w_cursor;
5381 if (onepage(FORWARD, 1L) == OK)
5382 {
5383 start_arrow(&tpos);
5384#ifdef FEAT_CINDENT
5385 can_cindent = TRUE;
5386#endif
5387 }
5388 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005389 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390}
5391
5392#ifdef FEAT_DND
5393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005394ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
5396 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5397}
5398#endif
5399
5400/*
5401 * Handle TAB in Insert or Replace mode.
5402 * Return TRUE when the TAB needs to be inserted like a normal character.
5403 */
5404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005405ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 int ind;
5408 int i;
5409 int temp;
5410
5411 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5412 Insstart_blank_vcol = get_nolist_virtcol();
5413 if (echeck_abbr(TAB + ABBR_OFF))
5414 return FALSE;
5415
5416 ind = inindent(0);
5417#ifdef FEAT_CINDENT
5418 if (ind)
5419 can_cindent = FALSE;
5420#endif
5421
5422 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005423 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 */
5425 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005426#ifdef FEAT_VARTABS
5427 && !(p_sta && ind
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005428 // These five lines mean 'tabstop' != 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005429 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5430 || (tabstop_count(curbuf->b_p_vts_array) == 1
5431 && tabstop_first(curbuf->b_p_vts_array)
5432 != get_sw_value(curbuf))
5433 || (tabstop_count(curbuf->b_p_vts_array) == 0
5434 && curbuf->b_p_ts != get_sw_value(curbuf))))
5435 && tabstop_count(curbuf->b_p_vsts_array) == 0
5436#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005437 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005438#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005439 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 return TRUE;
5441
5442 if (stop_arrow() == FAIL)
5443 return TRUE;
5444
5445 did_ai = FALSE;
5446#ifdef FEAT_SMARTINDENT
5447 did_si = FALSE;
5448 can_si = FALSE;
5449 can_si_back = FALSE;
5450#endif
5451 AppendToRedobuff((char_u *)"\t");
5452
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005453#ifdef FEAT_VARTABS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005454 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005455 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005456 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005457 temp -= get_nolist_virtcol() % temp;
5458 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005459 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005460 // use 'softtabstop' when set
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005461 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005462 curbuf->b_p_vsts_array);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 else // otherwise use 'tabstop'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005464 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5465 curbuf->b_p_vts_array);
5466#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005467 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005468 temp = (int)get_sw_value(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 else if (curbuf->b_p_sts != 0) // use 'softtabstop' when set
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005470 temp = (int)get_sts_value();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005471 else // otherwise use 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 temp = (int)curbuf->b_p_ts;
5473 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475
5476 /*
5477 * Insert the first space with ins_char(). It will delete one char in
5478 * replace mode. Insert the rest with ins_str(); it will not delete any
5479 * chars. For VREPLACE mode, we use ins_char() for all characters.
5480 */
5481 ins_char(' ');
5482 while (--temp > 0)
5483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 if (State & VREPLACE_FLAG)
5485 ins_char(' ');
5486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
5488 ins_str((char_u *)" ");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005489 if (State & REPLACE_FLAG) // no char replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 replace_push(NUL);
5491 }
5492 }
5493
5494 /*
5495 * When 'expandtab' not set: Replace spaces by TABs where possible.
5496 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005497#ifdef FEAT_VARTABS
5498 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5499 || get_sts_value() > 0
5500 || (p_sta && ind)))
5501#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005502 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
5505 char_u *ptr;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005506 char_u *saved_line = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 pos_T fpos;
5509 pos_T *cursor;
5510 colnr_T want_vcol, vcol;
5511 int change_col = -1;
5512 int save_list = curwin->w_p_list;
5513
5514 /*
5515 * Get the current line. For VREPLACE mode, don't make real changes
5516 * yet, just work on a copy of the line.
5517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (State & VREPLACE_FLAG)
5519 {
5520 pos = curwin->w_cursor;
5521 cursor = &pos;
5522 saved_line = vim_strsave(ml_get_curline());
5523 if (saved_line == NULL)
5524 return FALSE;
5525 ptr = saved_line + pos.col;
5526 }
5527 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
5529 ptr = ml_get_cursor();
5530 cursor = &curwin->w_cursor;
5531 }
5532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005533 // When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5535 curwin->w_p_list = FALSE;
5536
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005537 // Find first white before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005539 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 {
5541 --fpos.col;
5542 --ptr;
5543 }
5544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005545 // In Replace mode, don't change characters before the insert point.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 if ((State & REPLACE_FLAG)
5547 && fpos.lnum == Insstart.lnum
5548 && fpos.col < Insstart.col)
5549 {
5550 ptr += Insstart.col - fpos.col;
5551 fpos.col = Insstart.col;
5552 }
5553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005554 // compute virtual column numbers of first white and cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5556 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5557
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005558 // Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5559 // and 'linebreak' adding extra virtual columns.
Bram Moolenaar1c465442017-03-12 20:10:05 +01005560 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005562 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 if (vcol + i > want_vcol)
5564 break;
5565 if (*ptr != TAB)
5566 {
5567 *ptr = TAB;
5568 if (change_col < 0)
5569 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005570 change_col = fpos.col; // Column of first change
5571 // May have to adjust Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5573 Insstart.col = fpos.col;
5574 }
5575 }
5576 ++fpos.col;
5577 ++ptr;
5578 vcol += i;
5579 }
5580
5581 if (change_col >= 0)
5582 {
5583 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005584 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005586 // Skip over the spaces we need.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 while (vcol < want_vcol && *ptr == ' ')
5588 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005589 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 ++ptr;
5591 ++repl_off;
5592 }
5593 if (vcol > want_vcol)
5594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005595 // Must have a char with 'showbreak' just before it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 --ptr;
5597 --repl_off;
5598 }
5599 fpos.col += repl_off;
5600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005601 // Delete following spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 i = cursor->col - fpos.col;
5603 if (i > 0)
5604 {
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005605#ifdef FEAT_PROP_POPUP
5606 if (!(State & VREPLACE_FLAG))
5607 {
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005608 char_u *newp;
5609 int col;
5610
5611 newp = alloc(curbuf->b_ml.ml_line_len - i);
5612 if (newp == NULL)
5613 return FALSE;
5614
5615 col = ptr - curbuf->b_ml.ml_line_ptr;
5616 if (col > 0)
5617 mch_memmove(newp, ptr - col, col);
5618 mch_memmove(newp + col, ptr + i,
5619 curbuf->b_ml.ml_line_len - col - i);
5620
5621 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
5622 vim_free(curbuf->b_ml.ml_line_ptr);
5623 curbuf->b_ml.ml_line_ptr = newp;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005624 curbuf->b_ml.ml_line_len -= i;
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005625 curbuf->b_ml.ml_flags =
5626 (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005627 }
5628 else
5629#endif
5630 STRMOVE(ptr, ptr + i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005631 // correct replace stack.
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005632 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 for (temp = i; --temp >= 0; )
5634 replace_join(repl_off);
5635 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005636#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005637 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005638 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005639 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005640 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5641 (char_u *)"\t", 1);
5642 }
5643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 cursor->col -= i;
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 /*
5647 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5648 * backspacing over the changed spacing and then inserting the new
5649 * spacing.
5650 */
5651 if (State & VREPLACE_FLAG)
5652 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005653 // Backspace from real cursor to change_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 backspace_until_column(change_col);
5655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005656 // Insert each char in saved_line from changed_col to
5657 // ptr-cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 ins_bytes_len(saved_line + change_col,
5659 cursor->col - change_col);
5660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 }
5662
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 if (State & VREPLACE_FLAG)
5664 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 curwin->w_p_list = save_list;
5666 }
5667
5668 return FALSE;
5669}
5670
5671/*
5672 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005673 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005676ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 int i;
5679
5680 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005681 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005683 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 undisplay_dollar();
5685
5686 /*
5687 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5688 * character under the cursor. Only push a NUL on the replace stack,
5689 * nothing to put back when the NL is deleted.
5690 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005691 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 replace_push(NUL);
5693
5694 /*
5695 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5696 * replacing the next line, so we push all of the characters left on the
5697 * line onto the replace stack. This is not done here though, it is done
5698 * in open_line().
5699 */
5700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005701 // Put cursor on NUL if on the last char and coladd is 1 (happens after
5702 // CTRL-O).
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005703 if (virtual_active() && curwin->w_cursor.coladd > 0)
5704 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005705
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005707 // NL in reverse insert will always start in the end of
5708 // current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (revins_on)
5710 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5711#endif
5712
5713 AppendToRedobuff(NL_STR);
5714 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005715 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 old_indent = 0;
5717#ifdef FEAT_CINDENT
5718 can_cindent = TRUE;
5719#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005720#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005721 // When inserting a line the cursor line must never be in a closed fold.
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005722 foldOpenCursor();
5723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005725 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726}
5727
5728#ifdef FEAT_DIGRAPHS
5729/*
5730 * Handle digraph in insert mode.
5731 * Returns character still to be inserted, or NUL when nothing remaining to be
5732 * done.
5733 */
5734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005735ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737 int c;
5738 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005739 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
5741 pc_status = PC_STATUS_UNSET;
5742 if (redrawing() && !char_avail())
5743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005744 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005745 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
5747 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005748 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749#ifdef FEAT_CMDL_INFO
5750 add_to_showcmd_c(Ctrl_K);
5751#endif
5752 }
5753
5754#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005755 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756#endif
5757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005758 // don't map the digraph chars. This also prevents the
5759 // mode message to be deleted when ESC is hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 ++no_mapping;
5761 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005762 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 --no_mapping;
5764 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005765 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005766 // when the line fits in 'columns' the '?' is at the start of the next
5767 // line and will not be removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005768 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005770 if (IS_SPECIAL(c) || mod_mask) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
5772#ifdef FEAT_CMDL_INFO
5773 clear_showcmd();
5774#endif
5775 insert_special(c, TRUE, FALSE);
5776 return NUL;
5777 }
5778 if (c != ESC)
5779 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005780 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 if (redrawing() && !char_avail())
5782 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005783 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005784 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
5786 if (char2cells(c) == 1)
5787 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005788 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005790 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 }
5792#ifdef FEAT_CMDL_INFO
5793 add_to_showcmd_c(c);
5794#endif
5795 }
5796 ++no_mapping;
5797 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005798 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 --no_mapping;
5800 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005801 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005802 // when the line fits in 'columns' the '?' is at the start of the
5803 // next line and will not be removed by a redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005804 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 if (cc != ESC)
5806 {
5807 AppendToRedobuff((char_u *)CTRL_V_STR);
5808 c = getdigraph(c, cc, TRUE);
5809#ifdef FEAT_CMDL_INFO
5810 clear_showcmd();
5811#endif
5812 return c;
5813 }
5814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815#ifdef FEAT_CMDL_INFO
5816 clear_showcmd();
5817#endif
5818 return NUL;
5819}
5820#endif
5821
5822/*
5823 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5824 * Returns the char to be inserted, or NUL if none found.
5825 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005826 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005827ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
5829 int c;
5830 int temp;
5831 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005832 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
5834 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5835 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005836 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 return NUL;
5838 }
5839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005840 // try to advance to the cursor column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005842 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 prev_ptr = ptr;
5844 validate_virtcol();
5845 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5846 {
5847 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005848 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
5850 if ((colnr_T)temp > curwin->w_virtcol)
5851 ptr = prev_ptr;
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005855 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 return c;
5857}
5858
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005859/*
5860 * CTRL-Y or CTRL-E typed in Insert mode.
5861 */
5862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005863ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005864{
5865 int c = tc;
5866
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005867 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005868 {
5869 if (c == Ctrl_Y)
5870 scrolldown_clamp();
5871 else
5872 scrollup_clamp();
5873 redraw_later(VALID);
5874 }
5875 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005876 {
5877 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5878 if (c != NUL)
5879 {
5880 long tw_save;
5881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005882 // The character must be taken literally, insert like it
5883 // was typed after a CTRL-V, and pretend 'textwidth'
5884 // wasn't set. Digits, 'o' and 'x' are special after a
5885 // CTRL-V, don't use it for these.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005886 if (c < 256 && !isalnum(c))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005887 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005888 tw_save = curbuf->b_p_tw;
5889 curbuf->b_p_tw = -1;
5890 insert_special(c, TRUE, FALSE);
5891 curbuf->b_p_tw = tw_save;
5892#ifdef FEAT_RIGHTLEFT
5893 revins_chars++;
5894 revins_legal++;
5895#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005896 c = Ctrl_V; // pretend CTRL-V is last character
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005897 auto_format(FALSE, TRUE);
5898 }
5899 }
5900 return c;
5901}
5902
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903/*
5904 * Get the value that w_virtcol would have when 'list' is off.
5905 * Unless 'cpo' contains the 'L' flag.
5906 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005907 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005908get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005910 // check validity of cursor in current buffer
5911 if (curwin->w_buffer == NULL
5912 || curwin->w_buffer->b_ml.ml_mfp == NULL
5913 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5914 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5916 return getvcol_nolist(&curwin->w_cursor);
5917 validate_virtcol();
5918 return curwin->w_virtcol;
5919}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005920
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005921#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005922/*
5923 * Handle the InsertCharPre autocommand.
5924 * "c" is the character that was typed.
5925 * Return a pointer to allocated memory with the replacement string.
5926 * Return NULL to continue inserting "c".
5927 */
5928 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005929do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005930{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005931 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005932 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005933 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005935 // Return quickly when there is nothing to do.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005936 if (!has_insertcharpre())
5937 return NULL;
5938
Bram Moolenaar704984a2012-06-01 14:57:51 +02005939 if (has_mbyte)
5940 buf[(*mb_char2bytes)(c, buf)] = NUL;
5941 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005942 {
5943 buf[0] = c;
5944 buf[1] = NUL;
5945 }
5946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005947 // Lock the text to avoid weird things from happening.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005948 ++textwinlock;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005949 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005950
Bram Moolenaar704984a2012-06-01 14:57:51 +02005951 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005952 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005954 // Get the value of v:char. It may be empty or more than one
5955 // character. Only use it when changed, otherwise continue with the
5956 // original character to avoid breaking autoindent.
Bram Moolenaar704984a2012-06-01 14:57:51 +02005957 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5958 res = vim_strsave(get_vim_var_str(VV_CHAR));
5959 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005960
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005961 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005962 --textwinlock;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005963
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005964 // Restore the State, it may have been changed.
5965 State = save_State;
5966
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005967 return res;
5968}
5969#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005970
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005971#if defined(FEAT_CINDENT) || defined(PROTO)
5972 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005973get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005974{
5975 return can_cindent;
5976}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005977
5978 void
5979set_can_cindent(int val)
5980{
5981 can_cindent = val;
5982}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005983#endif
5984
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005985/*
5986 * Trigger "event" and take care of fixing undo.
5987 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005988 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005989ins_apply_autocmds(event_T event)
5990{
5991 varnumber_T tick = CHANGEDTICK(curbuf);
5992 int r;
5993
5994 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5995
5996 // If u_savesub() was called then we are not prepared to start
5997 // a new line. Call u_save() with no contents to fix that.
Bram Moolenaardb934952020-04-27 20:18:31 +02005998 // Except when leaving Insert mode.
5999 if (event != EVENT_INSERTLEAVE && tick != CHANGEDTICK(curbuf))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02006000 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
6001
6002 return r;
6003}