blob: d2e45dd2f0e19f803394bd5042e4b98ec7f67e32 [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 Moolenaarb71eaae2006-01-20 23:10:18 +0000178 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100180 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000181 return FALSE;
182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100184 // Don't allow recursive insert mode when busy with completion.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100185 if (ins_compl_active() || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100187 emsg(_(e_secure));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000188 return FALSE;
189 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100190 ins_compl_clear(); // clear stuff for CTRL-X mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar843ee412004-06-30 16:16:41 +0000192 /*
193 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
194 */
195 if (cmdchar != 'r' && cmdchar != 'v')
196 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100197 pos_T save_cursor = curwin->w_cursor;
198
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100199#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000200 if (cmdchar == 'R')
201 ptr = (char_u *)"r";
202 else if (cmdchar == 'V')
203 ptr = (char_u *)"v";
204 else
205 ptr = (char_u *)"i";
206 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100207 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100208#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200209 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100211 // Make sure the cursor didn't move. Do call check_cursor_col() in
212 // case the text was modified. Since Insert mode was not started yet
213 // a call to check_cursor_col() may move the cursor, especially with
214 // the "A" command, thus set State to avoid that. Also check that the
215 // line number is still valid (lines may have been deleted).
216 // Do not restore if v:char was set to a non-empty string.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100217 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100218#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200219 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100220#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200221 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100222 {
223 int save_state = State;
224
225 curwin->w_cursor = save_cursor;
226 State = INSERT;
227 check_cursor_col();
228 State = save_state;
229 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000230 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000231
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200232#ifdef FEAT_CONCEAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100233 // Check if the cursor line needs redrawing before changing State. If
234 // 'concealcursor' is "n" it needs to be redrawn without concealing.
Bram Moolenaarb9464822018-05-10 15:09:49 +0200235 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200236#endif
237
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 /*
239 * When doing a paste with the middle mouse button, Insstart is set to
240 * where the paste started.
241 */
242 if (where_paste_started.lnum != 0)
243 Insstart = where_paste_started;
244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {
246 Insstart = curwin->w_cursor;
247 if (startln)
248 Insstart.col = 0;
249 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000250 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 Insstart_blank_vcol = MAXCOL;
252 if (!did_ai)
253 ai_col = 0;
254
255 if (cmdchar != NUL && restart_edit == 0)
256 {
257 ResetRedobuff();
258 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (cmdchar == 'V' || cmdchar == 'v')
260 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100261 // "gR" or "gr" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 AppendCharToRedobuff('g');
263 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
264 }
265 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100267 if (cmdchar == K_PS)
268 AppendCharToRedobuff('a');
269 else
270 AppendCharToRedobuff(cmdchar);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100271 if (cmdchar == 'g') // "gI" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 AppendCharToRedobuff('I');
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100273 else if (cmdchar == 'r') // "r<CR>" command
274 count = 1; // insert only one <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 }
276 }
277
278 if (cmdchar == 'R')
279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 State = REPLACE;
281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 else if (cmdchar == 'V' || cmdchar == 'v')
283 {
284 State = VREPLACE;
285 replaceState = VREPLACE;
286 orig_line_count = curbuf->b_ml.ml_line_count;
287 vr_lines_changed = 1;
288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 else
290 State = INSERT;
291
292 stop_insert_mode = FALSE;
293
294 /*
295 * Need to recompute the cursor position, it might move when the cursor is
296 * on a TAB or special character.
297 */
298 curs_columns(TRUE);
299
300 /*
301 * Enable langmap or IME, indicated by 'iminsert'.
302 * Note that IME may enabled/disabled without us noticing here, thus the
303 * 'iminsert' value may not reflect what is actually used. It is updated
304 * when hitting <Esc>.
305 */
306 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
307 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100308#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
310#endif
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#ifdef FEAT_CMDL_INFO
314 clear_showcmd();
315#endif
316#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100317 // there is no reverse replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 revins_on = (State == INSERT && p_ri);
319 if (revins_on)
320 undisplay_dollar();
321 revins_chars = 0;
322 revins_legal = 0;
323 revins_scol = -1;
324#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100325 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100326 {
327 // Disable bracketed paste mode, we won't recognize the escape
328 // sequences.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100329 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100331 // Disable modifyOtherKeys, keys with modifiers would cause exiting
332 // Insert mode.
333 out_str(T_CTE);
334 }
335
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 /*
337 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100338 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
339 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 */
341 if (restart_edit != 0 && stuff_empty())
342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 /*
344 * After a paste we consider text typed to be part of the insert for
345 * the pasted text. You can backspace over the pasted text too.
346 */
347 if (where_paste_started.lnum)
348 arrow_used = FALSE;
349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 arrow_used = TRUE;
351 restart_edit = 0;
352
353 /*
354 * If the cursor was after the end-of-line before the CTRL-O and it is
355 * now at the end-of-line, put it after the end-of-line (this is not
356 * correct in very rare cases).
357 * Also do this if curswant is greater than the current virtual
358 * column. Eg after "^O$" or "^O80|".
359 */
360 validate_virtcol();
361 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000362 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 || curwin->w_curswant > curwin->w_virtcol)
364 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
365 {
366 if (ptr[1] == NUL)
367 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 else if (has_mbyte)
369 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000370 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 if (ptr[i] == NUL)
372 curwin->w_cursor.col += i;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000375 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 }
377 else
378 arrow_used = FALSE;
379
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100380 // we are in insert mode now, don't need to start it anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 need_start_insertmode = FALSE;
382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100383 // Need to save the line for undo before inserting the first char.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 ins_need_undo = TRUE;
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 where_paste_started.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_CINDENT
388 can_cindent = TRUE;
389#endif
390#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100391 // The cursor line is not in a closed fold, unless 'insertmode' is set or
392 // restarting.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if (!p_im && did_restart_edit == 0)
394 foldOpenCursor();
395#endif
396
397 /*
398 * If 'showmode' is set, show the current (insert/replace/..) mode.
399 * A warning message for changing a readonly file is given here, before
400 * actually changing anything. It's put after the mode, if any.
401 */
402 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000403 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 i = showmode();
405
406 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000407 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
409#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100410 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411#endif
412#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100413 do_digraph(-1); // clear digraphs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#endif
415
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000416 /*
417 * Get the current length of the redo buffer, those characters have to be
418 * skipped if we want to get to the inserted characters.
419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 ptr = get_inserted();
421 if (ptr == NULL)
422 new_insert_skip = 0;
423 else
424 {
425 new_insert_skip = (int)STRLEN(ptr);
426 vim_free(ptr);
427 }
428
429 old_indent = 0;
430
431 /*
432 * Main loop in Insert mode: repeat until Insert mode is left.
433 */
434 for (;;)
435 {
436#ifdef FEAT_RIGHTLEFT
437 if (!revins_legal)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100438 revins_scol = -1; // reset on illegal motions
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 else
440 revins_legal = 0;
441#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100442 if (arrow_used) // don't repeat insert when arrow key used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 count = 0;
444
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100445 if (update_Insstart_orig)
446 Insstart_orig = Insstart;
447
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200448 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100450 // ":stopinsert" used or 'insertmode' reset
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 count = 0;
452 goto doESCkey;
453 }
454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100455 // set curwin->w_curswant for next K_DOWN or K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (!arrow_used)
457 curwin->w_set_curswant = TRUE;
458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100459 // If there is no typeahead may check for timestamps (e.g., for when a
460 // menu invoked a shell command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 if (stuff_empty())
462 {
463 did_check_timestamps = FALSE;
464 if (need_check_timestamps)
465 check_timestamps(FALSE);
466 }
467
468 /*
469 * When emsg() was called msg_scroll will have been set.
470 */
471 msg_scroll = FALSE;
472
473#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100474 // When 'mousefocus' is set a mouse movement may have taken us to
475 // another window. "need_mouse_correct" may then be set because of an
476 // autocommand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 if (need_mouse_correct)
478 gui_mouse_correct();
479#endif
480
481#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100482 // Open fold at the cursor line, according to 'foldopen'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (fdo_flags & FDO_INSERT)
484 foldOpenCursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100485 // Close folds where the cursor isn't, according to 'foldclose'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 if (!char_avail())
487 foldCheckClose();
488#endif
489
Bram Moolenaarf2732452018-06-03 14:47:35 +0200490#ifdef FEAT_JOB_CHANNEL
491 if (bt_prompt(curbuf))
492 {
493 init_prompt(cmdchar_todo);
494 cmdchar_todo = NUL;
495 }
496#endif
497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 /*
499 * If we inserted a character at the last position of the last line in
500 * the window, scroll the window one line up. This avoids an extra
501 * redraw.
502 * This is detected when the cursor column is smaller after inserting
503 * something.
504 * Don't do this when the topline changed already, it has
505 * already been adjusted (by insertchar() calling open_line())).
506 */
507 if (curbuf->b_mod_set
508 && curwin->w_p_wrap
509 && !did_backspace
510 && curwin->w_topline == old_topline
511#ifdef FEAT_DIFF
512 && curwin->w_topfill == old_topfill
513#endif
514 )
515 {
516 mincol = curwin->w_wcol;
517 validate_cursor_col();
518
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200519 if (
520#ifdef FEAT_VARTABS
521 (int)curwin->w_wcol < mincol - tabstop_at(
522 get_nolist_virtcol(), curbuf->b_p_ts,
523 curbuf->b_p_vts_array)
524#else
525 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100528 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 && (curwin->w_cursor.lnum != curwin->w_topline
530#ifdef FEAT_DIFF
531 || curwin->w_topfill > 0
532#endif
533 ))
534 {
535#ifdef FEAT_DIFF
536 if (curwin->w_topfill > 0)
537 --curwin->w_topfill;
538 else
539#endif
540#ifdef FEAT_FOLDING
541 if (hasFolding(curwin->w_topline, NULL, &old_topline))
542 set_topline(curwin, old_topline + 1);
543 else
544#endif
545 set_topline(curwin, curwin->w_topline + 1);
546 }
547 }
548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100549 // May need to adjust w_topline to show the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 update_topline();
551
552 did_backspace = FALSE;
553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100554 validate_cursor(); // may set must_redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 /*
557 * Redraw the display when no characters are waiting.
558 * Also shows mode, ruler and positions cursor.
559 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000560 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (curwin->w_p_scb)
563 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar860cae12010-06-05 23:22:07 +0200565 if (curwin->w_p_crb)
566 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 update_curswant();
568 old_topline = curwin->w_topline;
569#ifdef FEAT_DIFF
570 old_topfill = curwin->w_topfill;
571#endif
572
573#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100574 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575#endif
576
577 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100578 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100580 if (c != K_CURSORHOLD)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100581 lastc = c; // remember the previous char for CTRL-D
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100583 // After using CTRL-G U the next cursor key will not break undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200584 if (dont_sync_undo == MAYBE)
585 dont_sync_undo = TRUE;
586 else
587 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100588 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100589 // Got here from normal mode when bracketed paste started.
Bram Moolenaarec2da362017-01-21 20:04:22 +0100590 c = K_PS;
591 else
592 do
593 {
594 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200595
596 if (stop_insert_mode)
597 {
598 // Insert mode ended, possibly from a callback.
599 count = 0;
600 nomove = TRUE;
601 goto doESCkey;
602 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100603 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100605 // Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V.
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000606 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#ifdef FEAT_RIGHTLEFT
609 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100610 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000613 /*
614 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000615 * and the cursor is still in the completed word. Only when there is
616 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000617 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100618 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000619 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100620 && curwin->w_cursor.col >= ins_compl_col()
621 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000622 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100623 // BS: Delete one character from "compl_leader".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000624 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100625 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000626 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000627 continue;
628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100629 // When no match was selected or it was edited.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100630 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000631 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100632 // CTRL-L: Add one character from the current match to
633 // "compl_leader". Except when at the original match and
634 // there is nothing to add, CTRL-L works like CTRL-P then.
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000635 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100636 && (!ctrl_x_mode_line_or_eval()
637 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000638 {
639 ins_compl_addfrommatch();
640 continue;
641 }
642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100643 // A non-white character that fits in with the current
644 // completion: Add to "compl_leader".
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000645 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000646 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100647#if defined(FEAT_EVAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100648 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100649 char_u *str = do_insert_char_pre(c);
650 char_u *p;
651
652 if (str != NULL)
653 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100654 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100655 ins_compl_addleader(PTR2CHAR(p));
656 vim_free(str);
657 }
658 else
659#endif
660 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000661 continue;
662 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100664 // Pressing CTRL-Y selects the current match. When
665 // ins_compl_enter_selects() is set the Enter key does the
666 // same.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100667 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200668 && (c == CAR || c == K_KENTER || c == NL)))
669 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000670 {
671 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200672 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000673 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000674 }
675 }
676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100677 // Prepare for or stop CTRL-X mode. This doesn't do completion, but
678 // it does fix up the text when finishing completion.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100679 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000680 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000681 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100683 // CTRL-\ CTRL-N goes to Normal mode,
684 // CTRL-\ CTRL-G goes to mode selected with 'insertmode',
685 // CTRL-\ CTRL-O is like CTRL-O but without moving the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if (c == Ctrl_BSL)
687 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100688 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +0000689 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 ++no_mapping;
691 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000692 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 --no_mapping;
694 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000695 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100697 // it's something else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 vungetc(c);
699 c = Ctrl_BSL;
700 }
701 else if (c == Ctrl_G && p_im)
702 continue;
703 else
704 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000705 if (c == Ctrl_O)
706 {
707 ins_ctrl_o();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100708 ins_at_eol = FALSE; // cursor keeps its column
Bram Moolenaar488c6512005-08-11 20:09:58 +0000709 nomove = TRUE;
710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 count = 0;
712 goto doESCkey;
713 }
714 }
715
716#ifdef FEAT_DIGRAPHS
717 c = do_digraph(c);
718#endif
719
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100720 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (c == Ctrl_V || c == Ctrl_Q)
723 {
724 ins_ctrl_v();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100725 c = Ctrl_V; // pretend CTRL-V is last typed character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 continue;
727 }
728
729#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200730 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100732 // A key name preceded by a bang means this key is not to be
733 // inserted. Skip ahead to the re-indenting below.
734 // A key name preceded by a star means that indenting has to be
735 // done before inserting the key.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 line_is_white = inindent(0);
737 if (in_cinkeys(c, '!', line_is_white))
738 goto force_cindent;
739 if (can_cindent && in_cinkeys(c, '*', line_is_white)
740 && stop_arrow() == OK)
741 do_c_expr_indent();
742 }
743#endif
744
745#ifdef FEAT_RIGHTLEFT
746 if (curwin->w_p_rl)
747 switch (c)
748 {
749 case K_LEFT: c = K_RIGHT; break;
750 case K_S_LEFT: c = K_S_RIGHT; break;
751 case K_C_LEFT: c = K_C_RIGHT; break;
752 case K_RIGHT: c = K_LEFT; break;
753 case K_S_RIGHT: c = K_S_LEFT; break;
754 case K_C_RIGHT: c = K_C_LEFT; break;
755 }
756#endif
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /*
759 * If 'keymodel' contains "startsel", may start selection. If it
760 * does, a CTRL-O and c will be stuffed, we need to get these
761 * characters.
762 */
763 if (ins_start_select(c))
764 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 /*
767 * The big switch to handle a character in insert mode.
768 */
769 switch (c)
770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100771 case ESC: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (echeck_abbr(ESC + ABBR_OFF))
773 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100776 case Ctrl_C: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#ifdef FEAT_CMDWIN
778 if (c == Ctrl_C && cmdwin_type != 0)
779 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100780 // Close the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 cmdwin_result = K_IGNORE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100782 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000783 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 goto doESCkey;
785 }
786#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200787#ifdef FEAT_JOB_CHANNEL
788 if (c == Ctrl_C && bt_prompt(curbuf))
789 {
790 if (invoke_prompt_interrupt())
791 {
792 if (!bt_prompt(curbuf))
793 // buffer changed to a non-prompt buffer, get out of
794 // Insert mode
795 goto doESCkey;
796 break;
797 }
798 }
799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801#ifdef UNIX
802do_intr:
803#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100804 // when 'insertmode' set, and not halfway a mapping, don't leave
805 // Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (goto_im())
807 {
808 if (got_int)
809 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100810 (void)vgetc(); // flush all buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 got_int = FALSE;
812 }
813 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200814 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 break;
816 }
817doESCkey:
818 /*
819 * This is the ONLY return from edit()!
820 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100821 // Always update o_lnum, so that a "CTRL-O ." that adds a line
822 // still puts the cursor back after the inserted text.
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000823 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 o_lnum = curwin->w_cursor.lnum;
825
Bram Moolenaar488c6512005-08-11 20:09:58 +0000826 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000827 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100828 // When CTRL-C was typed got_int will be set, with the result
829 // that the autocommands won't be executed. When mapped got_int
830 // is not set, but let's keep the behavior the same.
831 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200832 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000833 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 continue;
837
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100838 case Ctrl_Z: // suspend when 'insertmode' set
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000839 if (!p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100840 goto normalchar; // insert CTRL-Z as normal char
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100841 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100842#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100843 ui_cursor_shape(); // may need to update cursor shape
Bram Moolenaar74a47162017-02-26 19:09:05 +0100844#endif
845 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100847 case Ctrl_O: // execute one command
Bram Moolenaare344bea2005-09-01 20:46:49 +0000848#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100849 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000850 goto docomplete;
851#endif
852 if (echeck_abbr(Ctrl_O + ABBR_OFF))
853 break;
854 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100856 // don't move the cursor left when 'virtualedit' has "onemore".
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000857 if (ve_flags & VE_ONEMORE)
858 {
859 ins_at_eol = FALSE;
860 nomove = TRUE;
861 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000862 count = 0;
863 goto doESCkey;
864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100865 case K_INS: // toggle insert/replace mode
Bram Moolenaar572cb562005-08-05 21:35:02 +0000866 case K_KINS:
867 ins_insert(replaceState);
868 break;
869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100870 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar572cb562005-08-05 21:35:02 +0000871 break;
872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100873 case K_HELP: // Help key works like <ESC> <Help>
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000874 case K_F1:
875 case K_XF1:
876 stuffcharReadbuff(K_HELP);
877 if (p_im)
878 need_start_insertmode = TRUE;
879 goto doESCkey;
880
881#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100882 case K_F21: // NetBeans command
883 ++no_mapping; // don't map the next key hits
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000884 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 --no_mapping;
886 netbeans_keycommand(i);
887 break;
888#endif
889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100890 case K_ZERO: // Insert the previously inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 case NUL:
892 case Ctrl_A:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100893 // For ^@ the trailing ESC will end the insert, unless there is an
894 // error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
896 && c != Ctrl_A && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100897 goto doESCkey; // quit insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 inserted_space = FALSE;
899 break;
900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100901 case Ctrl_R: // insert the contents of a register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ins_reg();
903 auto_format(FALSE, TRUE);
904 inserted_space = FALSE;
905 break;
906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100907 case Ctrl_G: // commands starting with CTRL-G
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 ins_ctrl_g();
909 break;
910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100911 case Ctrl_HAT: // switch input mode and/or langmap
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000912 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 break;
914
915#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100916 case Ctrl__: // switch between languages
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (!p_ari)
918 goto normalchar;
919 ins_ctrl_();
920 break;
921#endif
922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100923 case Ctrl_D: // Make indent one shiftwidth smaller.
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200924#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 goto docomplete;
927#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100928 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100930 case Ctrl_T: // Make indent one shiftwidth greater.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100931 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000933 if (has_compl_option(FALSE))
934 goto docomplete;
935 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ins_shift(c, lastc);
939 auto_format(FALSE, TRUE);
940 inserted_space = FALSE;
941 break;
942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 case K_DEL: // delete character under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 case K_KDEL:
945 ins_del();
946 auto_format(FALSE, TRUE);
947 break;
948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100949 case K_BS: // delete character before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 case Ctrl_H:
951 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
952 auto_format(FALSE, TRUE);
953 break;
954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100955 case Ctrl_W: // delete word before the cursor
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200956#ifdef FEAT_JOB_CHANNEL
957 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
958 {
959 // In a prompt window CTRL-W is used for window commands.
960 // Use Shift-CTRL-W to delete a word.
961 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200962 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200963 nomove = TRUE;
964 count = 0;
965 goto doESCkey;
966 }
967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
969 auto_format(FALSE, TRUE);
970 break;
971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100972 case Ctrl_U: // delete all inserted text in current line
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000973# ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100974 // CTRL-X CTRL-U completes with 'completefunc'.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100975 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000976 goto docomplete;
977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
979 auto_format(FALSE, TRUE);
980 inserted_space = FALSE;
981 break;
982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100983 case K_LEFTMOUSE: // mouse keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 case K_LEFTMOUSE_NM:
985 case K_LEFTDRAG:
986 case K_LEFTRELEASE:
987 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100988 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 case K_MIDDLEMOUSE:
990 case K_MIDDLEDRAG:
991 case K_MIDDLERELEASE:
992 case K_RIGHTMOUSE:
993 case K_RIGHTDRAG:
994 case K_RIGHTRELEASE:
995 case K_X1MOUSE:
996 case K_X1DRAG:
997 case K_X1RELEASE:
998 case K_X2MOUSE:
999 case K_X2DRAG:
1000 case K_X2RELEASE:
1001 ins_mouse(c);
1002 break;
1003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 case K_MOUSEDOWN: // Default action for scroll wheel up: scroll up
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001005 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 break;
1007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001008 case K_MOUSEUP: // Default action for scroll wheel down: scroll down
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001009 ins_mousescroll(MSCR_UP);
1010 break;
1011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001012 case K_MOUSELEFT: // Scroll wheel left
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001013 ins_mousescroll(MSCR_LEFT);
1014 break;
1015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001016 case K_MOUSERIGHT: // Scroll wheel right
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001017 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001019
Bram Moolenaarec2da362017-01-21 20:04:22 +01001020 case K_PS:
1021 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1022 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001023 // invoked from normal mode, bail out
Bram Moolenaarec2da362017-01-21 20:04:22 +01001024 goto doESCkey;
1025 break;
1026 case K_PE:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001027 // Got K_PE without K_PS, ignore.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001028 break;
1029
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001030#ifdef FEAT_GUI_TABLINE
1031 case K_TABLINE:
1032 case K_TABMENU:
1033 ins_tabline(c);
1034 break;
1035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001037 case K_IGNORE: // Something mapped to nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 break;
1039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001040 case K_CURSORHOLD: // Didn't type something for a while.
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001041 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001042 did_cursorhold = TRUE;
1043 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001044
Bram Moolenaar4f974752019-02-17 17:44:42 +01001045#ifdef FEAT_GUI_MSWIN
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001046 // On MS-Windows ignore <M-F4>, we get it when closing the window
1047 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001048 case K_F4:
1049 if (mod_mask != MOD_MASK_ALT)
1050 goto normalchar;
1051 break;
1052#endif
1053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054#ifdef FEAT_GUI
1055 case K_VER_SCROLLBAR:
1056 ins_scroll();
1057 break;
1058
1059 case K_HOR_SCROLLBAR:
1060 ins_horscroll();
1061 break;
1062#endif
1063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001064 case K_HOME: // <Home>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 case K_S_HOME:
1067 case K_C_HOME:
1068 ins_home(c);
1069 break;
1070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 case K_END: // <End>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case K_S_END:
1074 case K_C_END:
1075 ins_end(c);
1076 break;
1077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001078 case K_LEFT: // <Left>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001079 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1080 ins_s_left();
1081 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001082 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 break;
1084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001085 case K_S_LEFT: // <S-Left>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 case K_C_LEFT:
1087 ins_s_left();
1088 break;
1089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001090 case K_RIGHT: // <Right>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001091 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1092 ins_s_right();
1093 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001094 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 break;
1096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001097 case K_S_RIGHT: // <S-Right>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 case K_C_RIGHT:
1099 ins_s_right();
1100 break;
1101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001102 case K_UP: // <Up>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001103 if (pum_visible())
1104 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001105 if (mod_mask & MOD_MASK_SHIFT)
1106 ins_pageup();
1107 else
1108 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 break;
1110
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001111 case K_S_UP: // <S-Up>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 case K_PAGEUP:
1113 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001114 if (pum_visible())
1115 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 ins_pageup();
1117 break;
1118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001119 case K_DOWN: // <Down>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001120 if (pum_visible())
1121 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001122 if (mod_mask & MOD_MASK_SHIFT)
1123 ins_pagedown();
1124 else
1125 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 break;
1127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001128 case K_S_DOWN: // <S-Down>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 case K_PAGEDOWN:
1130 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001131 if (pum_visible())
1132 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 ins_pagedown();
1134 break;
1135
1136#ifdef FEAT_DND
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001137 case K_DROP: // drag-n-drop event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 ins_drop();
1139 break;
1140#endif
1141
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001142 case K_S_TAB: // When not mapped, use like a normal TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 c = TAB;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001144 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001146 case TAB: // TAB or Complete patterns along path
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001147#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001148 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 goto docomplete;
1150#endif
1151 inserted_space = FALSE;
1152 if (ins_tab())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001153 goto normalchar; // insert TAB as a normal char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 auto_format(FALSE, TRUE);
1155 break;
1156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001157 case K_KENTER: // <Enter>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 c = CAR;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001159 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 case CAR:
1161 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001162#if defined(FEAT_QUICKFIX)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001163 // In a quickfix window a <CR> jumps to the error under the
1164 // cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (bt_quickfix(curbuf) && c == CAR)
1166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001167 if (curwin->w_llist_ref == NULL) // quickfix window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001168 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001169 else // location list window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001170 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 break;
1172 }
1173#endif
1174#ifdef FEAT_CMDWIN
1175 if (cmdwin_type != 0)
1176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001177 // Execute the command in the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 cmdwin_result = CAR;
1179 goto doESCkey;
1180 }
1181#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001182#ifdef FEAT_JOB_CHANNEL
1183 if (bt_prompt(curbuf))
1184 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001185 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001186 if (!bt_prompt(curbuf))
1187 // buffer changed to a non-prompt buffer, get out of
1188 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001189 goto doESCkey;
1190 break;
1191 }
1192#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001193 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001194 goto doESCkey; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 auto_format(FALSE, FALSE);
1196 inserted_space = FALSE;
1197 break;
1198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001199 case Ctrl_K: // digraph or keyword completion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001200 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001202 if (has_compl_option(TRUE))
1203 goto docomplete;
1204 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001206#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 c = ins_digraph();
1208 if (c == NUL)
1209 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001210#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001211 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001213 case Ctrl_X: // Enter CTRL-X mode
Bram Moolenaar572cb562005-08-05 21:35:02 +00001214 ins_ctrl_x();
1215 break;
1216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001217 case Ctrl_RSB: // Tag name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001218 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 goto normalchar;
1220 goto docomplete;
1221
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001222 case Ctrl_F: // File name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001223 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 goto normalchar;
1225 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 case 's': // Spelling completion after ^X
Bram Moolenaar488c6512005-08-11 20:09:58 +00001228 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001229 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001230 goto normalchar;
1231 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001233 case Ctrl_L: // Whole line completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001234 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001236 // CTRL-L with 'insertmode' set: Leave Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (p_im)
1238 {
1239 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1240 break;
1241 goto doESCkey;
1242 }
1243 goto normalchar;
1244 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001245 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001247 case Ctrl_P: // Do previous/next pattern completion
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 case Ctrl_N:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001249 // if 'complete' is empty then plain ^P is no longer special,
1250 // but it is under other ^X modes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001252 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 goto normalchar;
1255
1256docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001257 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001258#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001259 disable_fold_update++; // don't redraw folds here
Bram Moolenaara6c07602017-03-05 21:18:27 +01001260#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001261 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001262 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001263#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001264 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001265#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001266 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001269 case Ctrl_Y: // copy from previous line or scroll down
1270 case Ctrl_E: // copy from next line or scroll up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001271 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 break;
1273
1274 default:
1275#ifdef UNIX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001276 if (c == intr_char) // special interrupt char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 goto do_intr;
1278#endif
1279
Bram Moolenaare659c952011-05-19 17:25:41 +02001280normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001282 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001284#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001285 if (!p_paste)
1286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001287 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001288 char_u *str = do_insert_char_pre(c);
1289 char_u *p;
1290
1291 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001292 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001293 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001294 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001295 // Insert the new value of v:char literally.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001296 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001297 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001298 c = PTR2CHAR(p);
1299 if (c == CAR || c == K_KENTER || c == NL)
1300 ins_eol(c);
1301 else
1302 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001303 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001304 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001305 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001306 vim_free(str);
1307 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001308 }
1309
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001310 // If the new value is already inserted or an empty string
1311 // then don't insert any character.
Bram Moolenaare659c952011-05-19 17:25:41 +02001312 if (c == NUL)
1313 break;
1314 }
1315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316#ifdef FEAT_SMARTINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001317 // Try to perform smart-indenting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 ins_try_si(c);
1319#endif
1320
1321 if (c == ' ')
1322 {
1323 inserted_space = TRUE;
1324#ifdef FEAT_CINDENT
1325 if (inindent(0))
1326 can_cindent = FALSE;
1327#endif
1328 if (Insstart_blank_vcol == MAXCOL
1329 && curwin->w_cursor.lnum == Insstart.lnum)
1330 Insstart_blank_vcol = get_nolist_virtcol();
1331 }
1332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001333 // Insert a normal character and check for abbreviations on a
1334 // special character. Let CTRL-] expand abbreviations without
1335 // inserting it.
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001336 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001337 // Add ABBR_OFF for characters above 0x100, this is
1338 // what check_abbr() expects.
1339 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1340 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
1342 insert_special(c, FALSE, FALSE);
1343#ifdef FEAT_RIGHTLEFT
1344 revins_legal++;
1345 revins_chars++;
1346#endif
1347 }
1348
1349 auto_format(FALSE, TRUE);
1350
1351#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001352 // When inserting a character the cursor line must never be in a
1353 // closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 foldOpenCursor();
1355#endif
1356 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001357 } // end of switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001359 // If typed something may trigger CursorHoldI again.
Bram Moolenaar245c4102016-04-20 17:37:41 +02001360 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001361#ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001362 // but not in CTRL-X mode, a script can't restore the state
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001363 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001364#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001365 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001366 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001368 // If the cursor was moved we didn't just insert a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (arrow_used)
1370 inserted_space = FALSE;
1371
1372#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001373 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
1375force_cindent:
1376 /*
1377 * Indent now if a key was typed that is in 'cinkeys'.
1378 */
1379 if (in_cinkeys(c, ' ', line_is_white))
1380 {
1381 if (stop_arrow() == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001382 // re-indent the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 do_c_expr_indent();
1384 }
1385 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001386#endif // FEAT_CINDENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001388 } // for (;;)
1389 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390}
1391
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001392 int
1393ins_need_undo_get(void)
1394{
1395 return ins_need_undo;
1396}
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398/*
1399 * Redraw for Insert mode.
1400 * This is postponed until getting the next character to make '$' in the 'cpo'
1401 * option work correctly.
1402 * Only redraw when there are no characters available. This speeds up
1403 * inserting sequences of characters (e.g., for CTRL-R).
1404 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001405 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001406ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001408#ifdef FEAT_CONCEAL
1409 linenr_T conceal_old_cursor_line = 0;
1410 linenr_T conceal_new_cursor_line = 0;
1411 int conceal_update_lines = FALSE;
1412#endif
1413
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001414 if (char_avail())
1415 return;
1416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001417 // Trigger CursorMoved if the cursor moved. Not when the popup menu is
1418 // visible, the command might delete it.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001419 if (ready && (has_cursormovedI()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001420# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001421 || popup_visible
1422# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001423# if defined(FEAT_CONCEAL)
1424 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001425# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001426 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001427 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001428 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001429 {
1430# ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001431 // Need to update the screen first, to make sure syntax
1432 // highlighting is correct after making a change (e.g., inserting
1433 // a "(". The autocommand may also require a redraw, so it's done
1434 // again below, unfortunately.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001435 if (syntax_present(curwin) && must_redraw)
1436 update_screen(0);
1437# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001438 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001439 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001440 // Make sure curswant is correct, an autocommand may call
1441 // getcurpos().
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001442 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001443 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001444 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001445#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001446 if (popup_visible)
1447 popup_check_cursor_pos();
1448#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001449# ifdef FEAT_CONCEAL
1450 if (curwin->w_p_cole > 0)
1451 {
1452 conceal_old_cursor_line = last_cursormoved.lnum;
1453 conceal_new_cursor_line = curwin->w_cursor.lnum;
1454 conceal_update_lines = TRUE;
1455 }
1456# endif
1457 last_cursormoved = curwin->w_cursor;
1458 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001459
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001460 // Trigger TextChangedI if b_changedtick differs.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001461 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001462 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001463 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001464 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001465 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001466 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001467
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001468 // save and restore curwin and curbuf, in case the autocmd changes them
1469 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001470 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001471 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001472 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001473 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1474 u_save(curwin->w_cursor.lnum,
1475 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001478 // Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1479 // TextChangedI will need to trigger for backwards compatibility, thus use
1480 // different b_last_changedtick* variables.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001481 if (ready && has_textchangedP()
1482 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1483 && pum_visible())
1484 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001485 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001486 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001487
1488 // save and restore curwin and curbuf, in case the autocmd changes them
1489 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001490 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001491 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001492 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001493 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1494 u_save(curwin->w_cursor.lnum,
1495 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001496 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001497
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001498 // Trigger SafeState if nothing is pending.
1499 may_trigger_safestate(ready
1500 && !ins_compl_active()
1501 && !pum_visible());
1502
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001503#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001504 if ((conceal_update_lines
1505 && (conceal_old_cursor_line != conceal_new_cursor_line
1506 || conceal_cursor_line(curwin)))
1507 || need_cursor_line_redraw)
1508 {
1509 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001510 redrawWinline(curwin, conceal_old_cursor_line);
1511 redrawWinline(curwin, conceal_new_cursor_line == 0
1512 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001513 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001514 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001515 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001516#endif
1517 if (must_redraw)
1518 update_screen(0);
1519 else if (clear_cmdline || redraw_cmdline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001520 showmode(); // clear cmdline and show mode
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001521 showruler(FALSE);
1522 setcursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001523 emsg_on_display = FALSE; // may remove error message now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524}
1525
1526/*
1527 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1528 */
1529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001530ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001533 int did_putchar = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001534 int prev_mod_mask = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001536 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00001537 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
1539 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001542 did_putchar = TRUE;
1543 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001544 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546#ifdef FEAT_CMDL_INFO
1547 add_to_showcmd_c(Ctrl_V);
1548#endif
1549
1550 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001551 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001552 // when the line fits in 'columns' the '^' is at the start of the next
1553 // line and will not removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001554 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#ifdef FEAT_CMDL_INFO
1556 clear_showcmd();
1557#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001558
1559 if ((c == ESC || c == CSI) && !(prev_mod_mask & MOD_MASK_SHIFT))
1560 // Using CTRL-V: Change any modifyOtherKeys ESC sequence to a normal
1561 // key. Don't do this for CTRL-SHIFT-V.
1562 c = decodeModifyOtherKeys(c);
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 insert_special(c, FALSE, TRUE);
1565#ifdef FEAT_RIGHTLEFT
1566 revins_chars++;
1567 revins_legal++;
1568#endif
1569}
1570
1571/*
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001572 * After getting an ESC or CSI for a literal key: If the typeahead buffer
1573 * contains a modifyOtherKeys sequence then decode it and return the result.
1574 * Otherwise return "c".
1575 * Note that this doesn't wait for characters, they must be in the typeahead
1576 * buffer already.
1577 */
1578 int
1579decodeModifyOtherKeys(int c)
1580{
1581 char_u *p = typebuf.tb_buf + typebuf.tb_off;
1582 int idx;
1583 int form = 0;
1584 int argidx = 0;
1585 int arg[2] = {0, 0};
1586
1587 // Recognize:
1588 // form 0: {lead}{key};{modifier}u
1589 // form 1: {lead}27;{modifier};{key}~
1590 if ((c == CSI || (c == ESC && *p == '[')) && typebuf.tb_len >= 4)
1591 {
1592 idx = (*p == '[');
1593 if (p[idx] == '2' && p[idx + 1] == '7' && p[idx + 2] == ';')
1594 {
1595 form = 1;
1596 idx += 3;
1597 }
1598 while (idx < typebuf.tb_len && argidx < 2)
1599 {
1600 if (p[idx] == ';')
1601 ++argidx;
1602 else if (VIM_ISDIGIT(p[idx]))
1603 arg[argidx] = arg[argidx] * 10 + (p[idx] - '0');
1604 else
1605 break;
1606 ++idx;
1607 }
1608 if (idx < typebuf.tb_len
1609 && p[idx] == (form == 1 ? '~' : 'u')
1610 && argidx == 1)
1611 {
1612 // Match, consume the code.
1613 typebuf.tb_off += idx + 1;
1614 typebuf.tb_len -= idx + 1;
1615
1616 mod_mask = decode_modifiers(arg[!form]);
1617 c = merge_modifyOtherKeys(arg[form]);
1618 }
1619 }
1620
1621 return c;
1622}
1623
1624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 * Put a character directly onto the screen. It's not stored in a buffer.
1626 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1627 */
1628static int pc_status;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001629#define PC_STATUS_UNSET 0 // pc_bytes was not set
1630#define PC_STATUS_RIGHT 1 // right halve of double-wide char
1631#define PC_STATUS_LEFT 2 // left halve of double-wide char
1632#define PC_STATUS_SET 3 // pc_bytes was filled
1633static char_u pc_bytes[MB_MAXBYTES + 1]; // saved bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634static int pc_attr;
1635static int pc_row;
1636static int pc_col;
1637
1638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
1641 int attr;
1642
1643 if (ScreenLines != NULL)
1644 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001645 update_topline(); // just in case w_topline isn't valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 validate_cursor();
1647 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001648 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 else
1650 attr = 0;
1651 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001652 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#ifdef FEAT_RIGHTLEFT
1655 if (curwin->w_p_rl)
1656 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001657 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (has_mbyte)
1659 {
1660 int fix_col = mb_fix_col(pc_col, pc_row);
1661
1662 if (fix_col != pc_col)
1663 {
1664 screen_putchar(' ', pc_row, fix_col, attr);
1665 --curwin->w_wcol;
1666 pc_status = PC_STATUS_RIGHT;
1667 }
1668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 else
1671#endif
1672 {
1673 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (mb_lefthalve(pc_row, pc_col))
1675 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
1677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001678 // save the character to be able to put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1682 pc_status = PC_STATUS_SET;
1683 }
1684 screen_putchar(c, pc_row, pc_col, attr);
1685 }
1686}
1687
Bram Moolenaarf2732452018-06-03 14:47:35 +02001688#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1689/*
1690 * Return the effective prompt for the current buffer.
1691 */
1692 char_u *
1693prompt_text(void)
1694{
1695 if (curbuf->b_prompt_text == NULL)
1696 return (char_u *)"% ";
1697 return curbuf->b_prompt_text;
1698}
1699
1700/*
1701 * Prepare for prompt mode: Make sure the last line has the prompt text.
1702 * Move the cursor to this line.
1703 */
1704 static void
1705init_prompt(int cmdchar_todo)
1706{
1707 char_u *prompt = prompt_text();
1708 char_u *text;
1709
1710 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1711 text = ml_get_curline();
1712 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1713 {
1714 // prompt is missing, insert it or append a line with it
1715 if (*text == NUL)
1716 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1717 else
1718 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1719 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1720 coladvance((colnr_T)MAXCOL);
1721 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1722 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001723
1724 // Insert always starts after the prompt, allow editing text after it.
1725 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1726 || Insstart_orig.col != (int)STRLEN(prompt))
1727 {
1728 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001729 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001730 Insstart_orig = Insstart;
1731 Insstart_textlen = Insstart.col;
1732 Insstart_blank_vcol = MAXCOL;
1733 arrow_used = FALSE;
1734 }
1735
Bram Moolenaarf2732452018-06-03 14:47:35 +02001736 if (cmdchar_todo == 'A')
1737 coladvance((colnr_T)MAXCOL);
1738 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001739 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001740 // Make sure the cursor is in a valid position.
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001741 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001742}
1743
1744/*
1745 * Return TRUE if the cursor is in the editable position of the prompt line.
1746 */
1747 int
1748prompt_curpos_editable()
1749{
1750 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1751 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1752}
1753#endif
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755/*
1756 * Undo the previous edit_putchar().
1757 */
1758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001759edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (pc_status == PC_STATUS_RIGHT)
1764 ++curwin->w_wcol;
1765 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001766 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1769 }
1770}
1771
1772/*
1773 * Called when p_dollar is set: display a '$' at the end of the changed text
1774 * Only works when cursor is in the line that changes.
1775 */
1776 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001777display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 colnr_T save_col;
1780
1781 if (!redrawing())
1782 return;
1783
1784 cursor_off();
1785 save_col = curwin->w_cursor.col;
1786 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (has_mbyte)
1788 {
1789 char_u *p;
1790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001791 // If on the last byte of a multi-byte move to the first byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 p = ml_get_curline();
1793 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1794 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001795 curs_columns(FALSE); // recompute w_wrow and w_wcol
Bram Moolenaar02631462017-09-22 15:20:32 +02001796 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
1798 edit_putchar('$', FALSE);
1799 dollar_vcol = curwin->w_virtcol;
1800 }
1801 curwin->w_cursor.col = save_col;
1802}
1803
1804/*
1805 * Call this function before moving the cursor from the normal insert position
1806 * in insert mode.
1807 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001809undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001811 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001813 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001814 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816}
1817
1818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 * Truncate the space at the end of a line. This is to be used only in an
1820 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1821 * modes.
1822 */
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 int i;
1827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001828 // find start of trailing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +01001829 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
1831 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001832 replace_join(0); // remove a NUL from the replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 line[i + 1] = NUL;
1835}
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837/*
1838 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1839 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001840 * Will attempt not to go before "col" even when there is a composing
1841 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 */
1843 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001844backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845{
1846 while ((int)curwin->w_cursor.col > col)
1847 {
1848 curwin->w_cursor.col--;
1849 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001850 replace_do_bs(col);
1851 else if (!del_char_after_col(col))
1852 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001856/*
1857 * Like del_char(), but make sure not to go before column "limit_col".
1858 * Only matters when there are composing characters.
1859 * Return TRUE when something was deleted.
1860 */
1861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001862del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001863{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001864 if (enc_utf8 && limit_col >= 0)
1865 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001866 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001868 // Make sure the cursor is at the start of a character, but
1869 // skip forward again when going too far back because of a
1870 // composing character.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001871 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001872 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001873 {
1874 int l = utf_ptr2len(ml_get_cursor());
1875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001876 if (l == 0) // end of line
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001877 break;
1878 curwin->w_cursor.col += l;
1879 }
1880 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1881 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001882 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001883 }
1884 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001885 (void)del_char(FALSE);
1886 return TRUE;
1887}
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889/*
1890 * Next character is interpreted literally.
1891 * A one, two or three digit decimal number is interpreted as its byte value.
1892 * If one or two digits are entered, the next character is given to vungetc().
1893 * For Unicode a character > 255 may be returned.
1894 */
1895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001896get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
1898 int cc;
1899 int nc;
1900 int i;
1901 int hex = FALSE;
1902 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 if (got_int)
1906 return Ctrl_C;
1907
1908#ifdef FEAT_GUI
1909 /*
1910 * In GUI there is no point inserting the internal code for a special key.
1911 * It is more useful to insert the string "<KEY>" instead. This would
1912 * probably be useful in a text window too, but it would not be
1913 * vi-compatible (maybe there should be an option for it?) -- webb
1914 */
1915 if (gui.in_use)
1916 ++allow_keys;
1917#endif
1918#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001921 ++no_mapping; // don't map the next key hits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 cc = 0;
1923 i = 0;
1924 for (;;)
1925 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001926 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001928 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 add_to_showcmd(nc);
1930#endif
1931 if (nc == 'x' || nc == 'X')
1932 hex = TRUE;
1933 else if (nc == 'o' || nc == 'O')
1934 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 else if (nc == 'u' || nc == 'U')
1936 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 else
1938 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001939 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 if (!vim_isxdigit(nc))
1942 break;
1943 cc = cc * 16 + hex2nr(nc);
1944 }
1945 else if (octal)
1946 {
1947 if (nc < '0' || nc > '7')
1948 break;
1949 cc = cc * 8 + nc - '0';
1950 }
1951 else
1952 {
1953 if (!VIM_ISDIGIT(nc))
1954 break;
1955 cc = cc * 10 + nc - '0';
1956 }
1957
1958 ++i;
1959 }
1960
Bram Moolenaar13505972019-01-24 15:04:48 +01001961 if (cc > 255 && unicode == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001962 cc = 255; // limit range to 0-255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 nc = 0;
1964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001965 if (hex) // hex: up to two chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 if (i >= 2)
1968 break;
1969 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001970 else if (unicode) // Unicode: up to four or eight chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1973 break;
1974 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001975 else if (i >= 3) // decimal or octal: up to three chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 break;
1977 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001978 if (i == 0) // no number entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001980 if (nc == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
1982 cc = '\n';
1983 nc = 0;
1984 }
1985 else
1986 {
1987 cc = nc;
1988 nc = 0;
1989 }
1990 }
1991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001992 if (cc == 0) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001994 if (enc_dbcs && (cc & 0xff) == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001995 cc = '?'; // don't accept an illegal DBCS char, the NUL in the
1996 // second byte will cause trouble!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
1998 --no_mapping;
1999#ifdef FEAT_GUI
2000 if (gui.in_use)
2001 --allow_keys;
2002#endif
2003 if (nc)
2004 vungetc(nc);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002005 got_int = FALSE; // CTRL-C typed after CTRL-V is not an interrupt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 return cc;
2007}
2008
2009/*
2010 * Insert character, taking care of special keys and mod_mask
2011 */
2012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002013insert_special(
2014 int c,
2015 int allow_modmask,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002016 int ctrlv) // c was typed after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 char_u *p;
2019 int len;
2020
2021 /*
2022 * Special function key, translate into "<Key>". Up to the last '>' is
2023 * inserted with ins_str(), so as not to replace characters in replace
2024 * mode.
2025 * Only use mod_mask for special keys, to avoid things like <S-Space>,
2026 * unless 'allow_modmask' is TRUE.
2027 */
Bram Moolenaard0573012017-10-28 21:11:06 +02002028#ifdef MACOS_X
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002029 // Command-key never produces a normal key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (mod_mask & MOD_MASK_CMD)
2031 allow_modmask = TRUE;
2032#endif
2033 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
2034 {
2035 p = get_special_key_name(c, mod_mask);
2036 len = (int)STRLEN(p);
2037 c = p[len - 1];
2038 if (len > 2)
2039 {
2040 if (stop_arrow() == FAIL)
2041 return;
2042 p[len - 1] = NUL;
2043 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002044 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 ctrlv = FALSE;
2046 }
2047 }
2048 if (stop_arrow() == OK)
2049 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2050}
2051
2052/*
2053 * Special characters in this context are those that need processing other
2054 * than the simple insertion that can be performed here. This includes ESC
2055 * which terminates the insert, and CR/NL which need special processing to
2056 * open up a new line. This routine tries to optimize insertions performed by
2057 * the "redo", "undo" or "put" commands, so it needs to know when it should
2058 * stop and defer processing to the "normal" mechanism.
2059 * '0' and '^' are special, because they can be followed by CTRL-D.
2060 */
2061#ifdef EBCDIC
2062# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2063#else
2064# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2065#endif
2066
Bram Moolenaar13505972019-01-24 15:04:48 +01002067#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002069/*
2070 * "flags": INSCHAR_FORMAT - force formatting
2071 * INSCHAR_CTRLV - char typed just after CTRL-V
2072 * INSCHAR_NO_FEX - don't use 'formatexpr'
2073 *
2074 * NOTE: passes the flags value straight through to internal_format() which,
2075 * beside INSCHAR_FORMAT (above), is also looking for these:
2076 * INSCHAR_DO_COM - format comments
2077 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002080insertchar(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002081 int c, // character to insert or NUL
2082 int flags, // INSCHAR_FORMAT, etc.
2083 int second_indent) // indent for second line if >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002088 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002090 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093 /*
2094 * Try to break the line in two or more pieces when:
2095 * - Always do this if we have been called to do formatting only.
2096 * - Always do this when 'formatoptions' has the 'a' flag and the line
2097 * ends in white space.
2098 * - Otherwise:
2099 * - Don't do this if inserting a blank
2100 * - Don't do this if an existing character is being replaced, unless
2101 * we're in VREPLACE mode.
2102 * - Do this if the cursor is not on the line where insert started
2103 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2104 * before the insert.
2105 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2106 * before 'textwidth'
2107 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002108 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002109 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002110 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 && *ml_get_cursor() != NUL)
2114 && (curwin->w_cursor.lnum != Insstart.lnum
2115 || ((!has_format_option(FO_INS_LONG)
2116 || Insstart_textlen <= (colnr_T)textwidth)
2117 && (!fo_ins_blank
2118 || Insstart_blank_vcol <= (colnr_T)textwidth
2119 ))))))
2120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002121 // Format with 'formatexpr' when it's set. Use internal formatting
2122 // when 'formatexpr' isn't set or it returns non-zero.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002123#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002124 int do_internal = TRUE;
2125 colnr_T virtcol = get_nolist_virtcol()
2126 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002127
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002128 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2129 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002130 {
2131 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002132 // It may be required to save for undo again, e.g. when setline()
2133 // was called.
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002134 ins_need_undo = TRUE;
2135 }
2136 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002138 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002140
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002141 if (c == NUL) // only formatting was wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 return;
2143
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002144 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (did_ai && (int)c == end_comment_pending)
2146 {
2147 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002148 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 int middle_len, end_len;
2150 int i;
2151
2152 /*
2153 * Need to remove existing (middle) comment leader and insert end
2154 * comment leader. First, check what comment leader we can find.
2155 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002156 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002157 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002159 // Skip middle-comment string
2160 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 ++p;
2162 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002163 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002164 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 --middle_len;
2166
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002167 // Find the end-comment string
2168 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 ++p;
2170 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2171
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002172 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002174 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 ;
2176 i++;
2177
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002178 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 i -= middle_len;
2180
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002181 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2183 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002184 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 backspace_until_column(i);
2186
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002187 // Insert the end-comment string, except for the last
2188 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 ins_bytes_len(lead_end, end_len - 1);
2190 }
2191 }
2192 }
2193 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 did_ai = FALSE;
2196#ifdef FEAT_SMARTINDENT
2197 did_si = FALSE;
2198 can_si = FALSE;
2199 can_si_back = FALSE;
2200#endif
2201
2202 /*
2203 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2204 * This speeds up normal text input considerably.
2205 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2206 * need to re-indent at a ':', or any other character (but not what
2207 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002208 * Don't do this when there an InsertCharPre autocommand is defined,
2209 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002210 * Do the check for InsertCharPre before the call to vpeekc() because the
2211 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 */
2213#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002214 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#endif
2216
2217 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002219 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 && vpeekc() != NUL
2221 && !(State & REPLACE_FLAG)
2222#ifdef FEAT_CINDENT
2223 && !cindent_on()
2224#endif
2225#ifdef FEAT_RIGHTLEFT
2226 && !p_ri
2227#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002228 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230#define INPUT_BUFLEN 100
2231 char_u buf[INPUT_BUFLEN + 1];
2232 int i;
2233 colnr_T virtcol = 0;
2234
2235 buf[0] = c;
2236 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002237 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 virtcol = get_nolist_virtcol();
2239 /*
2240 * Stop the string when:
2241 * - no more chars available
2242 * - finding a special character (command key)
2243 * - buffer is full
2244 * - running into the 'textwidth' boundary
2245 * - need to check for abbreviation: A non-word char after a word-char
2246 */
2247 while ( (c = vpeekc()) != NUL
2248 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 && i < INPUT_BUFLEN
2251 && (textwidth == 0
2252 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2253 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2254 {
2255#ifdef FEAT_RIGHTLEFT
2256 c = vgetc();
2257 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002258 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 buf[i++] = c;
2260#else
2261 buf[i++] = vgetc();
2262#endif
2263 }
2264
2265#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002266 do_digraph(-1); // clear digraphs
2267 do_digraph(buf[i-1]); // may be the start of a digraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#endif
2269 buf[i] = NUL;
2270 ins_str(buf);
2271 if (flags & INSCHAR_CTRLV)
2272 {
2273 redo_literal(*buf);
2274 i = 1;
2275 }
2276 else
2277 i = 0;
2278 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002279 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281 else
2282 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002283 int cc;
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2286 {
2287 char_u buf[MB_MAXBYTES + 1];
2288
2289 (*mb_char2bytes)(c, buf);
2290 buf[cc] = NUL;
2291 ins_char_bytes(buf, cc);
2292 AppendCharToRedobuff(c);
2293 }
2294 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 ins_char(c);
2297 if (flags & INSCHAR_CTRLV)
2298 redo_literal(c);
2299 else
2300 AppendCharToRedobuff(c);
2301 }
2302 }
2303}
2304
2305/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002306 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002307 *
2308 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2309 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002310 */
2311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002312internal_format(
2313 int textwidth,
2314 int second_indent,
2315 int flags,
2316 int format_only,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002317 int c) // character to be inserted (can be NUL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002318{
2319 int cc;
2320 int save_char = NUL;
2321 int haveto_redraw = FALSE;
2322 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002323 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002324 int fo_white_par = has_format_option(FO_WHITE_PAR);
2325 int first_line = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002326 colnr_T leader_len;
2327 int no_leader = FALSE;
2328 int do_comments = (flags & INSCHAR_DO_COM);
Bram Moolenaar0026d472014-09-09 16:32:39 +02002329#ifdef FEAT_LINEBREAK
2330 int has_lbr = curwin->w_p_lbr;
2331
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002332 // make sure win_lbr_chartabsize() counts correctly
Bram Moolenaar0026d472014-09-09 16:32:39 +02002333 curwin->w_p_lbr = FALSE;
2334#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002335
2336 /*
2337 * When 'ai' is off we don't want a space under the cursor to be
2338 * deleted. Replace it with an 'x' temporarily.
2339 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002340 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002341 {
2342 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002343 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002344 {
2345 save_char = cc;
2346 pchar_cursor('x');
2347 }
2348 }
2349
2350 /*
2351 * Repeat breaking lines, until the current line is not too long.
2352 */
2353 while (!got_int)
2354 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002355 int startcol; // Cursor column at entry
2356 int wantcol; // column at textwidth border
2357 int foundcol; // column for start of spaces
2358 int end_foundcol = 0; // column for start of word
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002359 colnr_T len;
2360 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002361 int orig_col = 0;
2362 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002363 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002364 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002365 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002366
Bram Moolenaar97b98102009-11-17 16:41:01 +00002367 virtcol = get_nolist_virtcol()
2368 + char2cells(c != NUL ? c : gchar_cursor());
2369 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002370 break;
2371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002372 if (no_leader)
2373 do_comments = FALSE;
2374 else if (!(flags & INSCHAR_FORMAT)
2375 && has_format_option(FO_WRAP_COMS))
2376 do_comments = TRUE;
2377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002378 // Don't break until after the comment leader
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002379 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002380 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002381 else
2382 leader_len = 0;
2383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002384 // If the line doesn't start with a comment leader, then don't
2385 // start one in a following broken line. Avoids that a %word
2386 // moved to the start of the next line causes all following lines
2387 // to start with %.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002388 if (leader_len == 0)
2389 no_leader = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002390 if (!(flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002391 && leader_len == 0
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002392 && !has_format_option(FO_WRAP))
2393
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002394 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002395 if ((startcol = curwin->w_cursor.col) == 0)
2396 break;
2397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002398 // find column of textwidth border
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002399 coladvance((colnr_T)textwidth);
2400 wantcol = curwin->w_cursor.col;
2401
Bram Moolenaar97b98102009-11-17 16:41:01 +00002402 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002403 foundcol = 0;
2404
2405 /*
2406 * Find position to break at.
2407 * Stop at first entered white when 'formatoptions' has 'v'
2408 */
2409 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002410 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002411 || curwin->w_cursor.lnum != Insstart.lnum
2412 || curwin->w_cursor.col >= Insstart.col)
2413 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002414 if (curwin->w_cursor.col == startcol && c != NUL)
2415 cc = c;
2416 else
2417 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002418 if (WHITECHAR(cc))
2419 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002420 // remember position of blank just before text
Bram Moolenaar97b98102009-11-17 16:41:01 +00002421 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002422
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002423 // find start of sequence of blanks
2424 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002425 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2426 {
2427 dec_cursor();
2428 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002429
2430 // Increment count of how many whitespace chars in this
2431 // group; we only need to know if it's more than one.
2432 if (wcc < 2)
2433 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002434 }
2435 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002436 break; // only spaces in front of text
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002437
2438 // Don't break after a period when 'formatoptions' has 'p' and
2439 // there are less than two spaces.
2440 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2441 continue;
2442
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002443 // Don't break until after the comment leader
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002444 if (curwin->w_cursor.col < leader_len)
2445 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002446 if (has_format_option(FO_ONE_LETTER))
2447 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002448 // do not break after one-letter words
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002449 if (curwin->w_cursor.col == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002450 break; // one-letter word at begin
2451 // do not break "#a b" when 'tw' is 2
Bram Moolenaar97b98102009-11-17 16:41:01 +00002452 if (curwin->w_cursor.col <= leader_len)
2453 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002454 col = curwin->w_cursor.col;
2455 dec_cursor();
2456 cc = gchar_cursor();
2457
2458 if (WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002459 continue; // one-letter, continue
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002460 curwin->w_cursor.col = col;
2461 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002462
2463 inc_cursor();
2464
2465 end_foundcol = end_col + 1;
2466 foundcol = curwin->w_cursor.col;
2467 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002468 break;
2469 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002470 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002471 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002472 // Break after or before a multi-byte character.
Bram Moolenaar97b98102009-11-17 16:41:01 +00002473 if (curwin->w_cursor.col != startcol)
2474 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002475 // Don't break until after the comment leader
Bram Moolenaar97b98102009-11-17 16:41:01 +00002476 if (curwin->w_cursor.col < leader_len)
2477 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002478 col = curwin->w_cursor.col;
2479 inc_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002480 // Don't change end_foundcol if already set.
Bram Moolenaar97b98102009-11-17 16:41:01 +00002481 if (foundcol != curwin->w_cursor.col)
2482 {
2483 foundcol = curwin->w_cursor.col;
2484 end_foundcol = foundcol;
2485 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2486 break;
2487 }
2488 curwin->w_cursor.col = col;
2489 }
2490
2491 if (curwin->w_cursor.col == 0)
2492 break;
2493
2494 col = curwin->w_cursor.col;
2495
2496 dec_cursor();
2497 cc = gchar_cursor();
2498
2499 if (WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002500 continue; // break with space
2501 // Don't break until after the comment leader
Bram Moolenaar97b98102009-11-17 16:41:01 +00002502 if (curwin->w_cursor.col < leader_len)
2503 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002504
2505 curwin->w_cursor.col = col;
2506
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002507 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002508 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002509 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2510 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002511 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002512 if (curwin->w_cursor.col == 0)
2513 break;
2514 dec_cursor();
2515 }
2516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002517 if (foundcol == 0) // no spaces, cannot break line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002518 {
2519 curwin->w_cursor.col = startcol;
2520 break;
2521 }
2522
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002523 // Going to break the line, remove any "$" now.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002524 undisplay_dollar();
2525
2526 /*
2527 * Offset between cursor position and line break is used by replace
2528 * stack functions. VREPLACE does not use this, and backspaces
2529 * over the text instead.
2530 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002531 if (State & VREPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002532 orig_col = startcol; // Will start backspacing from here
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002533 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002534 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002535
2536 /*
2537 * adjust startcol for spaces that will be deleted and
2538 * characters that will remain on top line
2539 */
2540 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002541 while ((cc = gchar_cursor(), WHITECHAR(cc))
2542 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002543 inc_cursor();
2544 startcol -= curwin->w_cursor.col;
2545 if (startcol < 0)
2546 startcol = 0;
2547
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002548 if (State & VREPLACE_FLAG)
2549 {
2550 /*
2551 * In VREPLACE mode, we will backspace over the text to be
2552 * wrapped, so save a copy now to put on the next line.
2553 */
2554 saved_text = vim_strsave(ml_get_cursor());
2555 curwin->w_cursor.col = orig_col;
2556 if (saved_text == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002557 break; // Can't do it, out of memory
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002558 saved_text[startcol] = NUL;
2559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002560 // Backspace over characters that will move to the next line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002561 if (!fo_white_par)
2562 backspace_until_column(foundcol);
2563 }
2564 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002566 // put cursor after pos. to break line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002567 if (!fo_white_par)
2568 curwin->w_cursor.col = foundcol;
2569 }
2570
2571 /*
2572 * Split the line just before the margin.
2573 * Only insert/delete lines, but don't really redraw the window.
2574 */
2575 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2576 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002577 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002578 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002579 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2580 if (!(flags & INSCHAR_COM_LIST))
2581 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002582
2583 replace_offset = 0;
2584 if (first_line)
2585 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002586 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002587 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002588 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002589 * This section is for auto-wrap of numeric lists. When not
2590 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2591 * flag will be set and open_line() will handle it (as seen
2592 * above). The code here (and in get_number_indent()) will
2593 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002594 */
2595 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002596 second_indent =
2597 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002598 if (second_indent >= 0)
2599 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002600 if (State & VREPLACE_FLAG)
2601 change_indent(INDENT_SET, second_indent,
2602 FALSE, NUL, TRUE);
2603 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002604 if (leader_len > 0 && second_indent - leader_len > 0)
2605 {
2606 int i;
2607 int padding = second_indent - leader_len;
2608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002609 // We started at the first_line of a numbered list
2610 // that has a comment. the open_line() function has
2611 // inserted the proper comment leader and positioned
2612 // the cursor at the end of the split line. Now we
2613 // add the additional whitespace needed after the
2614 // comment leader for the numbered list.
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002615 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002616 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002617 }
2618 else
2619 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002620 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002621 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002622 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002623 }
2624 first_line = FALSE;
2625 }
2626
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002627 if (State & VREPLACE_FLAG)
2628 {
2629 /*
2630 * In VREPLACE mode we have backspaced over the text to be
2631 * moved, now we re-insert it into the new line.
2632 */
2633 ins_bytes(saved_text);
2634 vim_free(saved_text);
2635 }
2636 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002637 {
2638 /*
2639 * Check if cursor is not past the NUL off the line, cindent
2640 * may have added or removed indent.
2641 */
2642 curwin->w_cursor.col += startcol;
2643 len = (colnr_T)STRLEN(ml_get_curline());
2644 if (curwin->w_cursor.col > len)
2645 curwin->w_cursor.col = len;
2646 }
2647
2648 haveto_redraw = TRUE;
2649#ifdef FEAT_CINDENT
2650 can_cindent = TRUE;
2651#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 // moved the cursor, don't autoindent or cindent now
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002653 did_ai = FALSE;
2654#ifdef FEAT_SMARTINDENT
2655 did_si = FALSE;
2656 can_si = FALSE;
2657 can_si_back = FALSE;
2658#endif
2659 line_breakcheck();
2660 }
2661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002662 if (save_char != NUL) // put back space after cursor
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002663 pchar_cursor(save_char);
2664
Bram Moolenaar0026d472014-09-09 16:32:39 +02002665#ifdef FEAT_LINEBREAK
2666 curwin->w_p_lbr = has_lbr;
2667#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002668 if (!format_only && haveto_redraw)
2669 {
2670 update_topline();
2671 redraw_curbuf_later(VALID);
2672 }
2673}
2674
2675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 * Called after inserting or deleting text: When 'formatoptions' includes the
2677 * 'a' flag format from the current line until the end of the paragraph.
2678 * Keep the cursor at the same position relative to the text.
2679 * The caller must have saved the cursor line for undo, following ones will be
2680 * saved here.
2681 */
2682 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002683auto_format(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002684 int trailblank, // when TRUE also format with trailing blank
2685 int prev_line) // may start in previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 pos_T pos;
2688 colnr_T len;
2689 char_u *old;
2690 char_u *new, *pnew;
2691 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002692 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 if (!has_format_option(FO_AUTO))
2695 return;
2696
2697 pos = curwin->w_cursor;
2698 old = ml_get_curline();
2699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002700 // may remove added space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 check_auto_format(FALSE);
2702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002703 // Don't format in Insert mode when the cursor is on a trailing blank, the
2704 // user might insert normal text next. Also skip formatting when "1" is
2705 // in 'formatoptions' and there is a single character before the cursor.
2706 // Otherwise the line would be broken and when typing another non-white
2707 // next they are not joined back together.
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002708 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (*old != NUL && !trailblank && wasatend)
2710 {
2711 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002712 cc = gchar_cursor();
2713 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2714 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002716 cc = gchar_cursor();
2717 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
2719 curwin->w_cursor = pos;
2720 return;
2721 }
2722 curwin->w_cursor = pos;
2723 }
2724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002725 // With the 'c' flag in 'formatoptions' and 't' missing: only format
2726 // comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002728 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 /*
2732 * May start formatting in a previous line, so that after "x" a word is
2733 * moved to the previous line if it fits there now. Only when this is not
2734 * the start of a paragraph.
2735 */
2736 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2737 {
2738 --curwin->w_cursor.lnum;
2739 if (u_save_cursor() == FAIL)
2740 return;
2741 }
2742
2743 /*
2744 * Do the formatting and restore the cursor position. "saved_cursor" will
2745 * be adjusted for the text formatting.
2746 */
2747 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002748 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 curwin->w_cursor = saved_cursor;
2750 saved_cursor.lnum = 0;
2751
2752 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2753 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002754 // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2756 coladvance((colnr_T)MAXCOL);
2757 }
2758 else
2759 check_cursor_col();
2760
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002761 // Insert mode: If the cursor is now after the end of the line while it
2762 // previously wasn't, the line was broken. Because of the rule above we
2763 // need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2764 // formatted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if (!wasatend && has_format_option(FO_WHITE_PAR))
2766 {
2767 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002768 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (curwin->w_cursor.col == len)
2770 {
2771 pnew = vim_strnsave(new, len + 2);
2772 pnew[len] = ' ';
2773 pnew[len + 1] = NUL;
2774 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002775 // remove the space later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 did_add_space = TRUE;
2777 }
2778 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002779 // may remove added space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 check_auto_format(FALSE);
2781 }
2782
2783 check_cursor();
2784}
2785
2786/*
2787 * When an extra space was added to continue a paragraph for auto-formatting,
2788 * delete it now. The space must be under the cursor, just after the insert
2789 * position.
2790 */
2791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002792check_auto_format(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002793 int end_insert) // TRUE when ending Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002796 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 if (did_add_space)
2799 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002800 cc = gchar_cursor();
2801 if (!WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002802 // Somehow the space was removed already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 did_add_space = FALSE;
2804 else
2805 {
2806 if (!end_insert)
2807 {
2808 inc_cursor();
2809 c = gchar_cursor();
2810 dec_cursor();
2811 }
2812 if (c != NUL)
2813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002814 // The space is no longer at the end of the line, delete it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 del_char(FALSE);
2816 did_add_space = FALSE;
2817 }
2818 }
2819 }
2820}
2821
2822/*
2823 * Find out textwidth to be used for formatting:
2824 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02002825 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 * if invalid value, use 0.
2827 * Set default to window width (maximum 79) for "gq" operator.
2828 */
2829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002830comp_textwidth(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002831 int ff) // force formatting (for "gq" command)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
2833 int textwidth;
2834
2835 textwidth = curbuf->b_p_tw;
2836 if (textwidth == 0 && curbuf->b_p_wm)
2837 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002838 // The width is the window width minus 'wrapmargin' minus all the
2839 // things that add to the margin.
Bram Moolenaar02631462017-09-22 15:20:32 +02002840 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841#ifdef FEAT_CMDWIN
2842 if (cmdwin_type != 0)
2843 textwidth -= 1;
2844#endif
2845#ifdef FEAT_FOLDING
2846 textwidth -= curwin->w_p_fdc;
2847#endif
2848#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002849 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 textwidth -= 1;
2851#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02002852 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 textwidth -= 8;
2854 }
2855 if (textwidth < 0)
2856 textwidth = 0;
2857 if (ff && textwidth == 0)
2858 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002859 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (textwidth > 79)
2861 textwidth = 79;
2862 }
2863 return textwidth;
2864}
2865
2866/*
2867 * Put a character in the redo buffer, for when just after a CTRL-V.
2868 */
2869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002870redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
2872 char_u buf[10];
2873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002874 // Only digits need special treatment. Translate them into a string of
2875 // three digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (VIM_ISDIGIT(c))
2877 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002878 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 AppendToRedobuff(buf);
2880 }
2881 else
2882 AppendCharToRedobuff(c);
2883}
2884
2885/*
2886 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002887 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002890start_arrow(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002891 pos_T *end_insert_pos) // can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002893 start_arrow_common(end_insert_pos, TRUE);
2894}
2895
2896/*
2897 * Like start_arrow() but with end_change argument.
2898 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2899 */
2900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002901start_arrow_with_change(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002902 pos_T *end_insert_pos, // can be NULL
2903 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002904{
2905 start_arrow_common(end_insert_pos, end_change);
2906 if (!end_change)
2907 {
2908 AppendCharToRedobuff(Ctrl_G);
2909 AppendCharToRedobuff('U');
2910 }
2911}
2912
2913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914start_arrow_common(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002915 pos_T *end_insert_pos, // can be NULL
2916 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002917{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002918 if (!arrow_used && end_change) // something has been inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
2920 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002921 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002922 arrow_used = TRUE; // this means we stopped the current insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002924#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002925 check_spell_redraw();
2926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927}
2928
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002929#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002930/*
2931 * If we skipped highlighting word at cursor, do it now.
2932 * It may be skipped again, thus reset spell_redraw_lnum first.
2933 */
2934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002936{
2937 if (spell_redraw_lnum != 0)
2938 {
2939 linenr_T lnum = spell_redraw_lnum;
2940
2941 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002942 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002943 }
2944}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002945
Bram Moolenaar217ad922005-03-20 22:37:15 +00002946#endif
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948/*
2949 * stop_arrow() is called before a change is made in insert mode.
2950 * If an arrow key has been used, start a new insertion.
2951 * Returns FAIL if undo is impossible, shouldn't insert then.
2952 */
2953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 if (arrow_used)
2957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002958 Insstart = curwin->w_cursor; // new insertion starts here
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002959 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002960 // Don't update the original insert position when moved to the
2961 // right, except when nothing was inserted yet.
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002962 update_Insstart_orig = FALSE;
2963 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 if (u_save_cursor() == OK)
2966 {
2967 arrow_used = FALSE;
2968 ins_need_undo = FALSE;
2969 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002970
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if (State & VREPLACE_FLAG)
2973 {
2974 orig_line_count = curbuf->b_ml.ml_line_count;
2975 vr_lines_changed = 1;
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 ResetRedobuff();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002978 AppendToRedobuff((char_u *)"1i"); // pretend we start an insertion
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002979 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981 else if (ins_need_undo)
2982 {
2983 if (u_save_cursor() == OK)
2984 ins_need_undo = FALSE;
2985 }
2986
2987#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002988 // Always open fold at the cursor line when inserting something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 foldOpenCursor();
2990#endif
2991
2992 return (arrow_used || ins_need_undo ? FAIL : OK);
2993}
2994
2995/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002996 * Do a few things to stop inserting.
2997 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2998 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 */
3000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003001stop_insert(
3002 pos_T *end_insert_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003003 int esc, // called by ins_esc()
3004 int nomove) // <c-\><c-o>, don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003006 int cc;
3007 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
3009 stop_redo_ins();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003010 replace_flush(); // abandon replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003013 * Save the inserted text for later redo with ^@ and CTRL-A.
3014 * Don't do it when "restart_edit" was set and nothing was inserted,
3015 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003017 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003018 if (did_restart_edit == 0 || (ptr != NULL
3019 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003020 {
3021 vim_free(last_insert);
3022 last_insert = ptr;
3023 last_insert_skip = new_insert_skip;
3024 }
3025 else
3026 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003028 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003030 // Auto-format now. It may seem strange to do this when stopping an
3031 // insertion (or moving the cursor), but it's required when appending
3032 // a line and having it end in a space. But only do it when something
3033 // was actually inserted, otherwise undo won't work.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003034 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003036 pos_T tpos = curwin->w_cursor;
3037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003038 // When the cursor is at the end of the line after a space the
3039 // formatting will move it to the following word. Avoid that by
3040 // moving the cursor onto the space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 cc = 'x';
3042 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
3043 {
3044 dec_cursor();
3045 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003046 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003047 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
3049
3050 auto_format(TRUE, FALSE);
3051
Bram Moolenaar1c465442017-03-12 20:10:05 +01003052 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003053 {
3054 if (gchar_cursor() != NUL)
3055 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003056 // If the cursor is still at the same character, also keep
3057 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003058 if (gchar_cursor() == NUL
3059 && curwin->w_cursor.lnum == tpos.lnum
3060 && curwin->w_cursor.col == tpos.col)
3061 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003065 // If a space was inserted for auto-formatting, remove it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 check_auto_format(TRUE);
3067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003068 // If we just did an auto-indent, remove the white space from the end
3069 // of the line, and put the cursor back.
3070 // Do this when ESC was used or moving the cursor up/down.
3071 // Check for the old position still being valid, just in case the text
3072 // got changed unexpectedly.
Bram Moolenaarf332a652013-11-04 04:20:33 +01003073 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003074 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3075 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003077 pos_T tpos = curwin->w_cursor;
3078
3079 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003080 check_cursor_col(); // make sure it is not past the line
Bram Moolenaar39f05632006-03-19 22:15:26 +00003081 for (;;)
3082 {
3083 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3084 --curwin->w_cursor.col;
3085 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003086 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003087 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003088 if (del_char(TRUE) == FAIL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003089 break; // should not happen
Bram Moolenaar39f05632006-03-19 22:15:26 +00003090 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003091 if (curwin->w_cursor.lnum != tpos.lnum)
3092 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003093 else
3094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003095 // reset tpos, could have been invalidated in the loop above
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003096 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003097 tpos.col++;
3098 if (cc != NUL && gchar_pos(&tpos) == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003099 ++curwin->w_cursor.col; // put cursor back on the NUL
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003102 // <C-S-Right> may have started Visual mode, adjust the position for
3103 // deleted characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3105 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003106 int len = (int)STRLEN(ml_get_curline());
3107
3108 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003110 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
3115 }
3116 did_ai = FALSE;
3117#ifdef FEAT_SMARTINDENT
3118 did_si = FALSE;
3119 can_si = FALSE;
3120 can_si_back = FALSE;
3121#endif
3122
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003123 // Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3124 // now in a different buffer.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003125 if (end_insert_pos != NULL)
3126 {
3127 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003128 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003129 curbuf->b_op_end = *end_insert_pos;
3130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131}
3132
3133/*
3134 * Set the last inserted text to a single character.
3135 * Used for the replace command.
3136 */
3137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003138set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 char_u *s;
3141
3142 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (last_insert != NULL)
3145 {
3146 s = last_insert;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003147 // Use the CTRL-V only when entering a special char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (c < ' ' || c == DEL)
3149 *s++ = Ctrl_V;
3150 s = add_char2buf(c, s);
3151 *s++ = ESC;
3152 *s++ = NUL;
3153 last_insert_skip = 0;
3154 }
3155}
3156
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003157#if defined(EXITFREE) || defined(PROTO)
3158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003159free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003160{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003161 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003162}
3163#endif
3164
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165/*
3166 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3167 * and CSI. Handle multi-byte characters.
3168 * Returns a pointer to after the added bytes.
3169 */
3170 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003171add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003173 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 int i;
3175 int len;
3176
3177 len = (*mb_char2bytes)(c, temp);
3178 for (i = 0; i < len; ++i)
3179 {
3180 c = temp[i];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003181 // Need to escape K_SPECIAL and CSI like in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (c == K_SPECIAL)
3183 {
3184 *s++ = K_SPECIAL;
3185 *s++ = KS_SPECIAL;
3186 *s++ = KE_FILLER;
3187 }
3188#ifdef FEAT_GUI
3189 else if (c == CSI)
3190 {
3191 *s++ = CSI;
3192 *s++ = KS_EXTRA;
3193 *s++ = (int)KE_CSI;
3194 }
3195#endif
3196 else
3197 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 return s;
3200}
3201
3202/*
3203 * move cursor to start of line
3204 * if flags & BL_WHITE move to first non-white
3205 * if flags & BL_SOL move to first non-white if startofline is set,
3206 * otherwise keep "curswant" column
3207 * if flags & BL_FIX don't leave the cursor on a NUL.
3208 */
3209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003210beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 if ((flags & BL_SOL) && !p_sol)
3213 coladvance(curwin->w_curswant);
3214 else
3215 {
3216 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 if (flags & (BL_WHITE | BL_SOL))
3220 {
3221 char_u *ptr;
3222
Bram Moolenaar1c465442017-03-12 20:10:05 +01003223 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3225 ++curwin->w_cursor.col;
3226 }
3227 curwin->w_set_curswant = TRUE;
3228 }
3229}
3230
3231/*
3232 * oneright oneleft cursor_down cursor_up
3233 *
3234 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003235 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 * Return OK when successful, FAIL when we hit a line of file boundary.
3237 */
3238
3239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (virtual_active())
3246 {
3247 pos_T prevpos = curwin->w_cursor;
3248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003249 // Adjust for multi-wide char (excluding TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003251 coladvance(getviscol() + ((*ptr != TAB
3252 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 ? ptr2cells(ptr) : 1));
3254 curwin->w_set_curswant = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003255 // Return OK if the cursor moved, FAIL otherwise (at window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 return (prevpos.col != curwin->w_cursor.col
3257 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003261 if (*ptr == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003262 return FAIL; // already at the very end
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003263
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003264 if (has_mbyte)
3265 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003267 l = 1;
3268
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003269 // move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3270 // contains "onemore".
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003271 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003272 return FAIL;
3273 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275 curwin->w_set_curswant = TRUE;
3276 return OK;
3277}
3278
3279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003280oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (virtual_active())
3283 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003284#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 int v = getviscol();
3288
3289 if (v == 0)
3290 return FAIL;
3291
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003292#ifdef FEAT_LINEBREAK
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003293 // We might get stuck on 'showbreak', skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 width = 1;
3295 for (;;)
3296 {
3297 coladvance(v - width);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003298 // getviscol() is slow, skip it when 'showbreak' is empty,
3299 // 'breakindent' is not set and there are no multi-byte
3300 // characters
Bram Moolenaaree857022019-11-09 23:26:40 +01003301 if ((*get_showbreak_value(curwin) == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003302 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 break;
3304 ++width;
3305 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003306#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
3310 if (curwin->w_cursor.coladd == 1)
3311 {
3312 char_u *ptr;
3313
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003314 // Adjust for multi-wide char (not a TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003316 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3317 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 curwin->w_cursor.coladd = 0;
3319 }
3320
3321 curwin->w_set_curswant = TRUE;
3322 return OK;
3323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
3325 if (curwin->w_cursor.col == 0)
3326 return FAIL;
3327
3328 curwin->w_set_curswant = TRUE;
3329 --curwin->w_cursor.col;
3330
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003331 // if the character on the left of the current cursor is a multi-byte
3332 // character, move to its first byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (has_mbyte)
3334 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return OK;
3336}
3337
3338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339cursor_up(
3340 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003341 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 linenr_T lnum;
3344
3345 if (n > 0)
3346 {
3347 lnum = curwin->w_cursor.lnum;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003348 // This fails if the cursor is already in the first line or the count
3349 // is larger than the line number and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00003350 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 return FAIL;
3352 if (n >= lnum)
3353 lnum = 1;
3354 else
3355#ifdef FEAT_FOLDING
3356 if (hasAnyFolding(curwin))
3357 {
3358 /*
3359 * Count each sequence of folded lines as one logical line.
3360 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003361 // go to the start of the current fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 (void)hasFolding(lnum, &lnum, NULL);
3363
3364 while (n--)
3365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003366 // move up one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 --lnum;
3368 if (lnum <= 1)
3369 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003370 // If we entered a fold, move to the beginning, unless in
3371 // Insert mode or when 'foldopen' contains "all": it will open
3372 // in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3374 (void)hasFolding(lnum, &lnum, NULL);
3375 }
3376 if (lnum < 1)
3377 lnum = 1;
3378 }
3379 else
3380#endif
3381 lnum -= n;
3382 curwin->w_cursor.lnum = lnum;
3383 }
3384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003385 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 coladvance(curwin->w_curswant);
3387
3388 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003389 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391 return OK;
3392}
3393
3394/*
3395 * Cursor down a number of logical lines.
3396 */
3397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003398cursor_down(
3399 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003400 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 linenr_T lnum;
3403
3404 if (n > 0)
3405 {
3406 lnum = curwin->w_cursor.lnum;
3407#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003408 // Move to last line of fold, will fail if it's the end-of-file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 (void)hasFolding(lnum, NULL, &lnum);
3410#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003411 // This fails if the cursor is already in the last line or would move
3412 // beyond the last line and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00003413 if (lnum >= curbuf->b_ml.ml_line_count
3414 || (lnum + n > curbuf->b_ml.ml_line_count
3415 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 return FAIL;
3417 if (lnum + n >= curbuf->b_ml.ml_line_count)
3418 lnum = curbuf->b_ml.ml_line_count;
3419 else
3420#ifdef FEAT_FOLDING
3421 if (hasAnyFolding(curwin))
3422 {
3423 linenr_T last;
3424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003425 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 while (n--)
3427 {
3428 if (hasFolding(lnum, NULL, &last))
3429 lnum = last + 1;
3430 else
3431 ++lnum;
3432 if (lnum >= curbuf->b_ml.ml_line_count)
3433 break;
3434 }
3435 if (lnum > curbuf->b_ml.ml_line_count)
3436 lnum = curbuf->b_ml.ml_line_count;
3437 }
3438 else
3439#endif
3440 lnum += n;
3441 curwin->w_cursor.lnum = lnum;
3442 }
3443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003444 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 coladvance(curwin->w_curswant);
3446
3447 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003448 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
3450 return OK;
3451}
3452
3453/*
3454 * Stuff the last inserted text in the read buffer.
3455 * Last_insert actually is a copy of the redo buffer, so we
3456 * first have to remove the command.
3457 */
3458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003459stuff_inserted(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003460 int c, // Command character to be inserted
3461 long count, // Repeat this many times
3462 int no_esc) // Don't add an ESC at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
3464 char_u *esc_ptr;
3465 char_u *ptr;
3466 char_u *last_ptr;
3467 char_u last = NUL;
3468
3469 ptr = get_last_insert();
3470 if (ptr == NULL)
3471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003472 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 return FAIL;
3474 }
3475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003476 // may want to stuff the command character, to start Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (c != NUL)
3478 stuffcharReadbuff(c);
3479 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003480 *esc_ptr = NUL; // remove the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003482 // when the last char is either "0" or "^" it will be quoted if no ESC
3483 // comes after it OR if it will inserted more than once and "ptr"
3484 // starts with ^D. -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3486 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3487 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3488 {
3489 last = *last_ptr;
3490 *last_ptr = NUL;
3491 }
3492
3493 do
3494 {
3495 stuffReadbuff(ptr);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003496 // a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (last)
3498 stuffReadbuff((char_u *)(last == '0'
3499 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3500 : IF_EB("\026^", CTRL_V_STR "^")));
3501 }
3502 while (--count > 0);
3503
3504 if (last)
3505 *last_ptr = last;
3506
3507 if (esc_ptr != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003508 *esc_ptr = ESC; // put the ESC back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003510 // may want to stuff a trailing ESC, to get out of Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (!no_esc)
3512 stuffcharReadbuff(ESC);
3513
3514 return OK;
3515}
3516
3517 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003518get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 if (last_insert == NULL)
3521 return NULL;
3522 return last_insert + last_insert_skip;
3523}
3524
3525/*
3526 * Get last inserted string, and remove trailing <Esc>.
3527 * Returns pointer to allocated memory (must be freed) or NULL.
3528 */
3529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003530get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532 char_u *s;
3533 int len;
3534
3535 if (last_insert == NULL)
3536 return NULL;
3537 s = vim_strsave(last_insert + last_insert_skip);
3538 if (s != NULL)
3539 {
3540 len = (int)STRLEN(s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003541 if (len > 0 && s[len - 1] == ESC) // remove trailing ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 s[len - 1] = NUL;
3543 }
3544 return s;
3545}
3546
3547/*
3548 * Check the word in front of the cursor for an abbreviation.
3549 * Called when the non-id character "c" has been entered.
3550 * When an abbreviation is recognized it is removed from the text and
3551 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3552 */
3553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003554echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003556 // Don't check for abbreviation in paste mode, when disabled and just
3557 // after moving around with cursor keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (p_paste || no_abbr || arrow_used)
3559 return FALSE;
3560
3561 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3562 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3563}
3564
3565/*
3566 * replace-stack functions
3567 *
3568 * When replacing characters, the replaced characters are remembered for each
3569 * new character. This is used to re-insert the old text when backspacing.
3570 *
3571 * There is a NUL headed list of characters for each character that is
3572 * currently in the file after the insertion point. When BS is used, one NUL
3573 * headed list is put back for the deleted character.
3574 *
3575 * For a newline, there are two NUL headed lists. One contains the characters
3576 * that the NL replaced. The extra one stores the characters after the cursor
3577 * that were deleted (always white space).
3578 *
3579 * Replace_offset is normally 0, in which case replace_push will add a new
3580 * character at the end of the stack. If replace_offset is not 0, that many
3581 * characters will be left on the stack above the newly inserted character.
3582 */
3583
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003584static char_u *replace_stack = NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003585static long replace_stack_nr = 0; // next entry in replace stack
3586static long replace_stack_len = 0; // max. number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
3588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003589replace_push(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003590 int c) // character that is replaced (NUL is none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
3592 char_u *p;
3593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003594 if (replace_stack_nr < replace_offset) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 return;
3596 if (replace_stack_len <= replace_stack_nr)
3597 {
3598 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003599 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003600 if (p == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 replace_stack_len -= 50;
3603 return;
3604 }
3605 if (replace_stack != NULL)
3606 {
3607 mch_memmove(p, replace_stack,
3608 (size_t)(replace_stack_nr * sizeof(char_u)));
3609 vim_free(replace_stack);
3610 }
3611 replace_stack = p;
3612 }
3613 p = replace_stack + replace_stack_nr - replace_offset;
3614 if (replace_offset)
3615 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3616 *p = c;
3617 ++replace_stack_nr;
3618}
3619
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003620/*
3621 * Push a character onto the replace stack. Handles a multi-byte character in
3622 * reverse byte order, so that the first byte is popped off first.
3623 * Return the number of bytes done (includes composing characters).
3624 */
3625 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003627{
3628 int l = (*mb_ptr2len)(p);
3629 int j;
3630
3631 for (j = l - 1; j >= 0; --j)
3632 replace_push(p[j]);
3633 return l;
3634}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636/*
3637 * Pop one item from the replace stack.
3638 * return -1 if stack empty
3639 * return replaced character or NUL otherwise
3640 */
3641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 if (replace_stack_nr == 0)
3645 return -1;
3646 return (int)replace_stack[--replace_stack_nr];
3647}
3648
3649/*
3650 * Join the top two items on the replace stack. This removes to "off"'th NUL
3651 * encountered.
3652 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654replace_join(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003655 int off) // offset for which NUL to remove
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
3657 int i;
3658
3659 for (i = replace_stack_nr; --i >= 0; )
3660 if (replace_stack[i] == NUL && off-- <= 0)
3661 {
3662 --replace_stack_nr;
3663 mch_memmove(replace_stack + i, replace_stack + i + 1,
3664 (size_t)(replace_stack_nr - i));
3665 return;
3666 }
3667}
3668
3669/*
3670 * Pop bytes from the replace stack until a NUL is found, and insert them
3671 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3672 */
3673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003674replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 int cc;
3677 int oldState = State;
3678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003679 State = NORMAL; // don't want REPLACE here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 while ((cc = replace_pop()) > 0)
3681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 dec_cursor();
3684 }
3685 State = oldState;
3686}
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688/*
3689 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3690 * indicates a multi-byte char, pop the other bytes too.
3691 */
3692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003693mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
3695 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003696 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 int i;
3698 int c;
3699
3700 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3701 {
3702 buf[0] = cc;
3703 for (i = 1; i < n; ++i)
3704 buf[i] = replace_pop();
3705 ins_bytes_len(buf, n);
3706 }
3707 else
3708 ins_char(cc);
3709
3710 if (enc_utf8)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003711 // Handle composing chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 for (;;)
3713 {
3714 c = replace_pop();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003715 if (c == -1) // stack empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 break;
3717 if ((n = MB_BYTE2LEN(c)) == 1)
3718 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 // Not a multi-byte char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 replace_push(c);
3721 break;
3722 }
3723 else
3724 {
3725 buf[0] = c;
3726 for (i = 1; i < n; ++i)
3727 buf[i] = replace_pop();
3728 if (utf_iscomposing(utf_ptr2char(buf)))
3729 ins_bytes_len(buf, n);
3730 else
3731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003732 // Not a composing char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 for (i = n - 1; i >= 0; --i)
3734 replace_push(buf[i]);
3735 break;
3736 }
3737 }
3738 }
3739}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
3741/*
3742 * make the replace stack empty
3743 * (called when exiting replace mode)
3744 */
3745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003746replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003748 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 replace_stack_len = 0;
3750 replace_stack_nr = 0;
3751}
3752
3753/*
3754 * Handle doing a BS for one character.
3755 * cc < 0: replace stack empty, just move cursor
3756 * cc == 0: character was inserted, delete it
3757 * cc > 0: character was replaced, put cc (first byte of original char) back
3758 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003759 * When "limit_col" is >= 0, don't delete before this column. Matters when
3760 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 */
3762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003763replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
3765 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 int orig_len = 0;
3767 int ins_len;
3768 int orig_vcols = 0;
3769 colnr_T start_vcol;
3770 char_u *p;
3771 int i;
3772 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773
3774 cc = replace_pop();
3775 if (cc > 0)
3776 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003777#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003778 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003779
3780 if (curbuf->b_has_textprop)
3781 {
3782 // Do not adjust text properties for individual delete and insert
3783 // operations, do it afterwards on the resulting text.
3784 len_before = STRLEN(ml_get_curline());
3785 ++text_prop_frozen;
3786 }
3787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (State & VREPLACE_FLAG)
3789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003790 // Get the number of screen cells used by the character we are
3791 // going to delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3793 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (has_mbyte)
3796 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003797 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003799 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 replace_push(cc);
3801 }
3802 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
3804 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003806 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808 replace_pop_ins();
3809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (State & VREPLACE_FLAG)
3811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003812 // Get the number of screen cells used by the inserted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003814 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 vcol = start_vcol;
3816 for (i = 0; i < ins_len; ++i)
3817 {
3818 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003819 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 vcol -= start_vcol;
3822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003823 // Delete spaces that were inserted after the cursor to keep the
3824 // text aligned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 curwin->w_cursor.col += ins_len;
3826 while (vcol > orig_vcols && gchar_cursor() == ' ')
3827 {
3828 del_char(FALSE);
3829 ++orig_vcols;
3830 }
3831 curwin->w_cursor.col -= ins_len;
3832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
Bram Moolenaar196d1572019-01-02 23:47:18 +01003834 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003836
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003837#ifdef FEAT_PROP_POPUP
Bram Moolenaar196d1572019-01-02 23:47:18 +01003838 if (curbuf->b_has_textprop)
3839 {
3840 size_t len_now = STRLEN(ml_get_curline());
3841
3842 --text_prop_frozen;
3843 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003844 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003845 }
3846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003849 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850}
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3853/*
3854 * Map Hebrew keyboard when in hkmap mode.
3855 */
3856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003857hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003859 if (p_hkmapp) // phonetic mapping, by Ilya Dogolazky
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3862 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3863 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3864 static char_u map[26] =
3865 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3866 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3867 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3868 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3869 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3870 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3871 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3872 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3873 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3874
3875 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3876 return (int)(map[CharOrd(c)] - 1 + p_aleph);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003877 // '-1'='sofit'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else if (c == 'x')
3879 return 'X';
3880 else if (c == 'q')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 return '\''; // {geresh}={'}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 else if (c == 246)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003883 return ' '; // \"o --> ' ' for a german keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 else if (c == 228)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003885 return ' '; // \"a --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 else if (c == 252)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003887 return ' '; // \"u --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888#ifdef EBCDIC
3889 else if (islower(c))
3890#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003891 // NOTE: islower() does not do the right thing for us on Linux so we
3892 // do this the same was as 5.7 and previous, so it works correctly on
3893 // all systems. Specifically, the e.g. Delete and Arrow keys are
3894 // munged and won't work if e.g. searching for Hebrew text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 else if (c >= 'a' && c <= 'z')
3896#endif
3897 return (int)(map[CharOrdLow(c)] + p_aleph);
3898 else
3899 return c;
3900 }
3901 else
3902 {
3903 switch (c)
3904 {
3905 case '`': return ';';
3906 case '/': return '.';
3907 case '\'': return ',';
3908 case 'q': return '/';
3909 case 'w': return '\'';
3910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003911 // Hebrew letters - set offset from 'a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 case ',': c = '{'; break;
3913 case '.': c = 'v'; break;
3914 case ';': c = 't'; break;
3915 default: {
3916 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3917
3918#ifdef EBCDIC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003919 // see note about islower() above
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (!islower(c))
3921#else
3922 if (c < 'a' || c > 'z')
3923#endif
3924 return c;
3925 c = str[CharOrdLow(c)];
3926 break;
3927 }
3928 }
3929
3930 return (int)(CharOrdLow(c) + p_aleph);
3931 }
3932}
3933#endif
3934
3935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 int need_redraw = FALSE;
3939 int regname;
3940 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003941 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
3943 /*
3944 * If we are going to wait for a character, show a '"'.
3945 */
3946 pc_status = PC_STATUS_UNSET;
3947 if (redrawing() && !char_avail())
3948 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003949 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00003950 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
3952 edit_putchar('"', TRUE);
3953#ifdef FEAT_CMDL_INFO
3954 add_to_showcmd_c(Ctrl_R);
3955#endif
3956 }
3957
3958#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003959 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960#endif
3961
3962 /*
3963 * Don't map the register name. This also prevents the mode message to be
3964 * deleted when ESC is hit.
3965 */
3966 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003967 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003968 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003972 // Get a third key for literal register insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 literally = regname;
3974#ifdef FEAT_CMDL_INFO
3975 add_to_showcmd_c(literally);
3976#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003977 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
3980 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003981 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982
3983#ifdef FEAT_EVAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003984 // Don't call u_sync() while typing the expression or giving an error
3985 // message for it. Only call it explicitly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 ++no_u_sync;
3987 if (regname == '=')
3988 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003989 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003990# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003992# endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003993 // Sync undo when evaluating the expression calls setline() or
3994 // append(), so that it can be undone separately.
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003995 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003998
3999 // Cursor may be moved back a column.
4000 curwin->w_cursor = curpos;
4001 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004002# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004003 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (im_on)
4005 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00004008 if (regname == NUL || !valid_yank_reg(regname, FALSE))
4009 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004010 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004011 need_redraw = TRUE; // remove the '"'
Bram Moolenaar677ee682005-01-27 14:41:15 +00004012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 else
4014 {
4015#endif
4016 if (literally == Ctrl_O || literally == Ctrl_P)
4017 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004018 // Append the command to the redo buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 AppendCharToRedobuff(Ctrl_R);
4020 AppendCharToRedobuff(literally);
4021 AppendCharToRedobuff(regname);
4022
4023 do_put(regname, BACKWARD, 1L,
4024 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
4025 }
4026 else if (insert_reg(regname, literally) == FAIL)
4027 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004028 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 need_redraw = TRUE; // remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004031 else if (stop_insert_mode)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004032 // When the '=' register was used and a function was invoked that
4033 // did ":stopinsert" then stuff_empty() returns FALSE but we won't
4034 // insert anything, need to remove the '"'
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004035 need_redraw = TRUE;
4036
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037#ifdef FEAT_EVAL
4038 }
4039 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004040 if (u_sync_once == 1)
4041 ins_need_undo = TRUE;
4042 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043#endif
4044#ifdef FEAT_CMDL_INFO
4045 clear_showcmd();
4046#endif
4047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004048 // If the inserted register is empty, we need to remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (need_redraw || stuff_empty())
4050 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004052 // Disallow starting Visual mode here, would get a weird mode.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004053 if (!vis_active && VIsual_active)
4054 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055}
4056
4057/*
4058 * CTRL-G commands in Insert mode.
4059 */
4060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004061ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
4063 int c;
4064
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004065 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 /*
4069 * Don't map the second key. This also prevents the mode message to be
4070 * deleted when ESC is hit.
4071 */
4072 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004073 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004074 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004076 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 switch (c)
4078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004079 // CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 case K_UP:
4081 case Ctrl_K:
4082 case 'k': ins_up(TRUE);
4083 break;
4084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004085 // CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 case K_DOWN:
4087 case Ctrl_J:
4088 case 'j': ins_down(TRUE);
4089 break;
4090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004091 // CTRL-G u: start new undoable edit
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004092 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004095 // Need to reset Insstart, esp. because a BS that joins
4096 // a line to the previous one must save for undo.
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004097 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004098 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 break;
4100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 // CTRL-G U: do not break undo with the next char
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004102 case 'U':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004103 // Allow one left/right cursor movement with the next char,
4104 // without breaking undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004105 dont_sync_undo = MAYBE;
4106 break;
4107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004108 // Unknown CTRL-G command, reserved for future expansion.
Bram Moolenaar165bc692015-07-21 17:53:25 +02004109 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111}
4112
4113/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004114 * CTRL-^ in Insert mode.
4115 */
4116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004117ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004118{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004119 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004121 // ":lmap" mappings exists, Toggle use of ":lmap" mappings.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 if (State & LANGMAP)
4123 {
4124 curbuf->b_p_iminsert = B_IMODE_NONE;
4125 State &= ~LANGMAP;
4126 }
4127 else
4128 {
4129 curbuf->b_p_iminsert = B_IMODE_LMAP;
4130 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004131#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004132 im_set_active(FALSE);
4133#endif
4134 }
4135 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004136#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004137 else
4138 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004139 // There are no ":lmap" mappings, toggle IM
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004140 if (im_get_status())
4141 {
4142 curbuf->b_p_iminsert = B_IMODE_NONE;
4143 im_set_active(FALSE);
4144 }
4145 else
4146 {
4147 curbuf->b_p_iminsert = B_IMODE_IM;
4148 State &= ~LANGMAP;
4149 im_set_active(TRUE);
4150 }
4151 }
4152#endif
4153 set_iminsert_global();
4154 showmode();
4155#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004156 // may show different cursor shape or color
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157 if (gui.in_use)
4158 gui_update_cursor(TRUE, FALSE);
4159#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004160#if defined(FEAT_KEYMAP)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004161 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004162 status_redraw_curbuf();
4163#endif
4164}
4165
4166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * Handle ESC in insert mode.
4168 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4169 * insert.
4170 */
4171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004172ins_esc(
4173 long *count,
4174 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004175 int nomove) // don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
4177 int temp;
4178 static int disabled_redraw = FALSE;
4179
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004180#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004181 check_spell_redraw();
4182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 temp = curwin->w_cursor.col;
4185 if (disabled_redraw)
4186 {
4187 --RedrawingDisabled;
4188 disabled_redraw = FALSE;
4189 }
4190 if (!arrow_used)
4191 {
4192 /*
4193 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004194 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4195 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
4197 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004198 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
4200 /*
4201 * Repeating insert may take a long time. Check for
4202 * interrupt now and then.
4203 */
4204 if (*count > 0)
4205 {
4206 line_breakcheck();
4207 if (got_int)
4208 *count = 0;
4209 }
4210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004211 if (--*count > 0) // repeat what was typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004213 // Vi repeats the insert without replacing characters.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004214 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4215 State &= ~REPLACE_FLAG;
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 (void)start_redo_ins();
4218 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004219 stuffRedoReadbuff(ESC_STR); // no ESC in redo buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 ++RedrawingDisabled;
4221 disabled_redraw = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004222 return FALSE; // repeat the insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004224 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 undisplay_dollar();
4226 }
4227
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004228 // When an autoindent was removed, curswant stays after the
4229 // indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4231 curwin->w_set_curswant = TRUE;
4232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004233 // Remember the last Insert position in the '^ mark.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 if (!cmdmod.keepjumps)
4235 curbuf->b_last_insert = curwin->w_cursor;
4236
4237 /*
4238 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004239 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004241 if (!nomove
4242 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004243 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004244 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004245 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246#ifdef FEAT_RIGHTLEFT
4247 && !revins_on
4248#endif
4249 )
4250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4252 {
4253 oneleft();
4254 if (restart_edit != NUL)
4255 ++curwin->w_cursor.coladd;
4256 }
4257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
4259 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004260 // Correct cursor for multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (has_mbyte)
4262 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 }
4265
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004266#ifdef HAVE_INPUT_METHOD
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004267 // Disable IM to allow typing English directly for Normal mode commands.
4268 // When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4269 // well).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 if (!(State & LANGMAP))
4271 im_save_status(&curbuf->b_p_iminsert);
4272 im_set_active(FALSE);
4273#endif
4274
4275 State = NORMAL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004276 // need to position cursor again (e.g. when on a TAB )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 changed_cline_bef_curs();
4278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280#ifdef CURSOR_SHAPE
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004281 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004283 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004284 {
4285 // Re-enable bracketed paste mode.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004286 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004288 // Re-enable modifyOtherKeys.
4289 out_str(T_CTI);
4290 }
4291
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004292 // When recording or for CTRL-O, need to display the new mode.
4293 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004294 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004296 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004297 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004299 return TRUE; // exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300}
4301
4302#ifdef FEAT_RIGHTLEFT
4303/*
4304 * Toggle language: hkmap and revins_on.
4305 * Move to end of reverse inserted text.
4306 */
4307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004308ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309{
4310 if (revins_on && revins_chars && revins_scol >= 0)
4311 {
4312 while (gchar_cursor() != NUL && revins_chars--)
4313 ++curwin->w_cursor.col;
4314 }
4315 p_ri = !p_ri;
4316 revins_on = (State == INSERT && p_ri);
4317 if (revins_on)
4318 {
4319 revins_scol = curwin->w_cursor.col;
4320 revins_legal++;
4321 revins_chars = 0;
4322 undisplay_dollar();
4323 }
4324 else
4325 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004326 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 showmode();
4328}
4329#endif
4330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331/*
4332 * If 'keymodel' contains "startsel", may start selection.
4333 * Returns TRUE when a CTRL-O and other keys stuffed.
4334 */
4335 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004336ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337{
4338 if (km_startsel)
4339 switch (c)
4340 {
4341 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 case K_PAGEUP:
4344 case K_KPAGEUP:
4345 case K_PAGEDOWN:
4346 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004347# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 case K_LEFT:
4349 case K_RIGHT:
4350 case K_UP:
4351 case K_DOWN:
4352 case K_END:
4353 case K_HOME:
4354# endif
4355 if (!(mod_mask & MOD_MASK_SHIFT))
4356 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004357 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 case K_S_LEFT:
4359 case K_S_RIGHT:
4360 case K_S_UP:
4361 case K_S_DOWN:
4362 case K_S_END:
4363 case K_S_HOME:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004364 // Start selection right away, the cursor can move with
4365 // CTRL-O when beyond the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 start_selection();
4367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // Execute the key in (insert) Select mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 stuffcharReadbuff(Ctrl_O);
4370 if (mod_mask)
4371 {
4372 char_u buf[4];
4373
4374 buf[0] = K_SPECIAL;
4375 buf[1] = KS_MODIFIER;
4376 buf[2] = mod_mask;
4377 buf[3] = NUL;
4378 stuffReadbuff(buf);
4379 }
4380 stuffcharReadbuff(c);
4381 return TRUE;
4382 }
4383 return FALSE;
4384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004387 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388 */
4389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004390ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004392#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004394 (char_u *)((State & REPLACE_FLAG) ? "i"
4395 : replaceState == VREPLACE ? "v"
4396 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004397#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004398 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 if (State & REPLACE_FLAG)
4400 State = INSERT | (State & LANGMAP);
4401 else
4402 State = replaceState | (State & LANGMAP);
4403 AppendCharToRedobuff(K_INS);
4404 showmode();
4405#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004406 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004407#endif
4408}
4409
4410/*
4411 * Pressed CTRL-O in Insert mode.
4412 */
4413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004414ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004416 if (State & VREPLACE_FLAG)
4417 restart_edit = 'V';
4418 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004419 if (State & REPLACE_FLAG)
4420 restart_edit = 'R';
4421 else
4422 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423 if (virtual_active())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004424 ins_at_eol = FALSE; // cursor always keeps its column
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426 ins_at_eol = (gchar_cursor() == NUL);
4427}
4428
4429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 * If the cursor is on an indent, ^T/^D insert/delete one
4431 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004432 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 * with vi. But vi only supports ^T and ^D after an
4434 * autoindent, we support it everywhere.
4435 */
4436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004437ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438{
4439 if (stop_arrow() == FAIL)
4440 return;
4441 AppendCharToRedobuff(c);
4442
4443 /*
4444 * 0^D and ^^D: remove all indent.
4445 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004446 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4447 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
4449 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004450 (void)del_char(FALSE); // delete the '^' or '0'
4451 // In Replace mode, restore the characters that '^' or '0' replaced.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (State & REPLACE_FLAG)
4453 replace_pop_ins();
4454 if (lastc == '^')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 old_indent = get_indent(); // remember curr. indent
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004456 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004459 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
4461 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4462 did_ai = FALSE;
4463#ifdef FEAT_SMARTINDENT
4464 did_si = FALSE;
4465 can_si = FALSE;
4466 can_si_back = FALSE;
4467#endif
4468#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004469 can_cindent = FALSE; // no cindenting after ^D or ^T
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#endif
4471}
4472
4473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004474ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475{
4476 int temp;
4477
4478 if (stop_arrow() == FAIL)
4479 return;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004480 if (gchar_cursor() == NUL) // delete newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
4482 temp = curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004483 if (!can_bs(BS_EOL) // only if "eol" included
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004484 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004485 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 curwin->w_cursor.col = temp;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004489 // Adjust orig_line_count in case more lines have been deleted than
4490 // have been added. That makes sure, that open_line() later
4491 // can access all buffer lines correctly
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004492 if (State & VREPLACE_FLAG &&
4493 orig_line_count > curbuf->b_ml.ml_line_count)
4494 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004497 else if (del_char(FALSE) == FAIL) // delete char under cursor
Bram Moolenaar165bc692015-07-21 17:53:25 +02004498 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 did_ai = FALSE;
4500#ifdef FEAT_SMARTINDENT
4501 did_si = FALSE;
4502 can_si = FALSE;
4503 can_si_back = FALSE;
4504#endif
4505 AppendCharToRedobuff(K_DEL);
4506}
4507
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004508/*
4509 * Delete one character for ins_bs().
4510 */
4511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004512ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004513{
4514 dec_cursor();
4515 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4516 if (State & REPLACE_FLAG)
4517 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004518 // Don't delete characters before the insert point when in
4519 // Replace mode
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004520 if (curwin->w_cursor.lnum != Insstart.lnum
4521 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004522 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004523 }
4524 else
4525 (void)del_char(FALSE);
4526}
4527
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528/*
4529 * Handle Backspace, delete-word and delete-line in Insert mode.
4530 * Return TRUE when backspace was actually used.
4531 */
4532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004533ins_bs(
4534 int c,
4535 int mode,
4536 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537{
4538 linenr_T lnum;
4539 int cc;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004540 int temp = 0; // init for GCC
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004541 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 colnr_T mincol;
4543 int did_backspace = FALSE;
4544 int in_indent;
4545 int oldState;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004546 int cpc[MAX_MCO]; // composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 /*
4549 * can't delete anything in an empty file
4550 * can't backup past first character in buffer
4551 * can't backup past starting point unless 'backspace' > 1
4552 * can backup to a previous line if 'backspace' == 0
4553 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004554 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 || (
4556#ifdef FEAT_RIGHTLEFT
4557 !revins_on &&
4558#endif
4559 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4560 || (!can_bs(BS_START)
4561 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004562 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4563 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4565 && curwin->w_cursor.col <= ai_col)
4566 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4567 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004568 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 return FALSE;
4570 }
4571
4572 if (stop_arrow() == FAIL)
4573 return FALSE;
4574 in_indent = inindent(0);
4575#ifdef FEAT_CINDENT
4576 if (in_indent)
4577 can_cindent = FALSE;
4578#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004579 end_comment_pending = NUL; // After BS, don't auto-end comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004581 if (revins_on) // put cursor after last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 inc_cursor();
4583#endif
4584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004585 // Virtualedit:
4586 // BACKSPACE_CHAR eats a virtual space
4587 // BACKSPACE_WORD eats all coladd
4588 // BACKSPACE_LINE eats all coladd and keeps going
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (curwin->w_cursor.coladd > 0)
4590 {
4591 if (mode == BACKSPACE_CHAR)
4592 {
4593 --curwin->w_cursor.coladd;
4594 return TRUE;
4595 }
4596 if (mode == BACKSPACE_WORD)
4597 {
4598 curwin->w_cursor.coladd = 0;
4599 return TRUE;
4600 }
4601 curwin->w_cursor.coladd = 0;
4602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004605 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 */
4607 if (curwin->w_cursor.col == 0)
4608 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004609 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004610 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611#ifdef FEAT_RIGHTLEFT
4612 || revins_on
4613#endif
4614 )
4615 {
4616 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4617 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4618 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004619 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004620 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
4622 /*
4623 * In replace mode:
4624 * cc < 0: NL was inserted, delete it
4625 * cc >= 0: NL was replaced, put original characters back
4626 */
4627 cc = -1;
4628 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004629 cc = replace_pop(); // returns -1 if NL was inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 /*
4631 * In replace mode, in the line we started replacing, we only move the
4632 * cursor.
4633 */
4634 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4635 {
4636 dec_cursor();
4637 }
4638 else
4639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (!(State & VREPLACE_FLAG)
4641 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 temp = gchar_cursor(); // remember current char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004646 // When "aw" is in 'formatoptions' we must delete the space at
4647 // the end of the line, otherwise the line will be broken
4648 // again when auto-formatting.
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004649 if (has_format_option(FO_AUTO)
4650 && has_format_option(FO_WHITE_PAR))
4651 {
4652 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4653 TRUE);
4654 int len;
4655
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004656 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004657 if (len > 0 && ptr[len - 1] == ' ')
4658 ptr[len - 1] = NUL;
4659 }
4660
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004661 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (temp == NUL && gchar_cursor() != NUL)
4663 inc_cursor();
4664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 else
4666 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
4668 /*
4669 * In REPLACE mode we have to put back the text that was replaced
4670 * by the NL. On the replace stack is first a NUL-terminated
4671 * sequence of characters that were deleted and then the
4672 * characters that NL replaced.
4673 */
4674 if (State & REPLACE_FLAG)
4675 {
4676 /*
4677 * Do the next ins_char() in NORMAL state, to
4678 * prevent ins_char() from replacing characters and
4679 * avoiding showmatch().
4680 */
4681 oldState = State;
4682 State = NORMAL;
4683 /*
4684 * restore characters (blanks) deleted after cursor
4685 */
4686 while (cc > 0)
4687 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004688 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004690 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 cc = replace_pop();
4692 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004693 // restore the characters that NL replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 replace_pop_ins();
4695 State = oldState;
4696 }
4697 }
4698 did_ai = FALSE;
4699 }
4700 else
4701 {
4702 /*
4703 * Delete character(s) before the cursor.
4704 */
4705#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004706 if (revins_on) // put cursor on last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 dec_cursor();
4708#endif
4709 mincol = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004710 // keep indent
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004711 if (mode == BACKSPACE_LINE
4712 && (curbuf->b_p_ai
4713#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004714 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004715#endif
4716 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef FEAT_RIGHTLEFT
4718 && !revins_on
4719#endif
4720 )
4721 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004722 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004724 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004726 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
4728
4729 /*
4730 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4731 */
4732 if ( mode == BACKSPACE_CHAR
4733 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004734 || ((get_sts_value() != 0
4735#ifdef FEAT_VARTABS
4736 || tabstop_count(curbuf->b_p_vsts_array)
4737#endif
4738 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004739 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 && (*(ml_get_cursor() - 1) == TAB
4741 || (*(ml_get_cursor() - 1) == ' '
4742 && (!*inserted_space_p
4743 || arrow_used))))))
4744 {
4745 int ts;
4746 colnr_T vcol;
4747 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004748 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
4750 *inserted_space_p = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004751 // Compute the virtual column where we want to be. Since
4752 // 'showbreak' may get in the way, need to get the last column of
4753 // the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004755 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 dec_cursor();
4757 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4758 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004759#ifdef FEAT_VARTABS
4760 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004761 {
4762 ts = (int)get_sw_value(curbuf);
4763 want_vcol = (want_vcol / ts) * ts;
4764 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004765 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004766 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004767 curbuf->b_p_vsts_array);
4768#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004769 if (p_sta && in_indent)
4770 ts = (int)get_sw_value(curbuf);
4771 else
4772 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004776 // delete characters until we are at or before want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004778 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004779 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004781 // insert extra spaces until we are at want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 while (vcol < want_vcol)
4783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004784 // Remember the first char we inserted
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004785 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4786 && curwin->w_cursor.col < Insstart_orig.col)
4787 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (State & VREPLACE_FLAG)
4790 ins_char(' ');
4791 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
4793 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004794 if ((State & REPLACE_FLAG))
4795 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4798 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004800 // If we are now back where we started delete one character. Can
4801 // happen when using 'sts' and 'linebreak'.
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004802 if (vcol >= start_vcol)
4803 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805
4806 /*
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004807 * Delete up to starting point, start of line or previous word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004809 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004811 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004813 if (has_mbyte)
4814 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004815 do
4816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 if (!revins_on) // put cursor on char to be deleted
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004821
4822 cc = gchar_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004823 // look multi-byte character class
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004824 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004826 prev_cclass = cclass;
4827 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004830 // start of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004831 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4832 {
4833 mode = BACKSPACE_WORD_NOT_SPACE;
4834 temp = vim_iswordc(cc);
4835 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004836 // end of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004837 else if (mode == BACKSPACE_WORD_NOT_SPACE
4838 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004839 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004842 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004844 inc_cursor();
4845#ifdef FEAT_RIGHTLEFT
4846 else if (State & REPLACE_FLAG)
4847 dec_cursor();
4848#endif
4849 break;
4850 }
4851 if (State & REPLACE_FLAG)
4852 replace_do_bs(-1);
4853 else
4854 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004855 if (enc_utf8 && p_deco)
4856 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004857 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004858 /*
4859 * If there are combining characters and 'delcombine' is set
4860 * move the cursor back. Don't back up before the base
4861 * character.
4862 */
4863 if (enc_utf8 && p_deco && cpc[0] != NUL)
4864 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004865#ifdef FEAT_RIGHTLEFT
4866 if (revins_chars)
4867 {
4868 revins_chars--;
4869 revins_legal++;
4870 }
4871 if (revins_on && gchar_cursor() == NUL)
4872 break;
4873#endif
4874 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004875 // Just a single backspace?:
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004876 if (mode == BACKSPACE_CHAR)
4877 break;
4878 } while (
4879#ifdef FEAT_RIGHTLEFT
4880 revins_on ||
4881#endif
4882 (curwin->w_cursor.col > mincol
4883 && (curwin->w_cursor.lnum != Insstart_orig.lnum
4884 || curwin->w_cursor.col != Insstart_orig.col)));
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 did_backspace = TRUE;
4887 }
4888#ifdef FEAT_SMARTINDENT
4889 did_si = FALSE;
4890 can_si = FALSE;
4891 can_si_back = FALSE;
4892#endif
4893 if (curwin->w_cursor.col <= 1)
4894 did_ai = FALSE;
4895 /*
4896 * It's a little strange to put backspaces into the redo
4897 * buffer, but it makes auto-indent a lot easier to deal
4898 * with.
4899 */
4900 AppendCharToRedobuff(c);
4901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004902 // If deleted before the insertion point, adjust it
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004903 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004904 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004905 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004907 // vi behaviour: the cursor moves backward but the character that
4908 // was there remains visible
4909 // Vim behaviour: the cursor moves backward and the character that
4910 // was there is erased from the screen.
4911 // We can emulate the vi behaviour by pretending there is a dollar
4912 // displayed even when there isn't.
4913 // --pkv Sun Jan 19 01:56:40 EST 2003
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004914 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 dollar_vcol = curwin->w_virtcol;
4916
Bram Moolenaarce3be472008-01-14 19:12:28 +00004917#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 // When deleting a char the cursor line must never be in a closed fold.
4919 // E.g., when 'foldmethod' is indent and deleting the first non-white
4920 // char before a Tab.
Bram Moolenaarce3be472008-01-14 19:12:28 +00004921 if (did_backspace)
4922 foldOpenCursor();
4923#endif
4924
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return did_backspace;
4926}
4927
Bram Moolenaarec2da362017-01-21 20:04:22 +01004928/*
4929 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4930 * P_PE literally.
4931 * When "drop" is TRUE then consume the text and drop it.
4932 */
4933 int
4934bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4935{
4936 int c;
4937 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4938 int idx = 0;
4939 char_u *end = find_termcode((char_u *)"PE");
4940 int ret_char = -1;
4941 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004942 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004944 // If the end code is too long we can't detect it, read everything.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004945 if (STRLEN(end) >= NUMBUFLEN)
4946 end = NULL;
4947 ++no_mapping;
4948 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004949 if (!p_paste)
4950 // Also have the side effects of setting 'paste' to make it work much
4951 // faster.
4952 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004953
Bram Moolenaarec2da362017-01-21 20:04:22 +01004954 for (;;)
4955 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004956 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004957 if (end == NULL && vpeekc() == NUL)
4958 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004959 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004960 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004961 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004962 if (c == NUL || got_int)
4963 // When CTRL-C was encountered the typeahead will be flushed and we
4964 // won't get the end sequence.
4965 break;
4966
Bram Moolenaarec2da362017-01-21 20:04:22 +01004967 if (has_mbyte)
4968 idx += (*mb_char2bytes)(c, buf + idx);
4969 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004970 buf[idx++] = c;
4971 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004972 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004973 {
4974 if (end[idx] == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004975 break; // Found the end of paste code.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004976 continue;
4977 }
4978 if (!drop)
4979 {
4980 switch (mode)
4981 {
4982 case PASTE_CMDLINE:
4983 put_on_cmdline(buf, idx, TRUE);
4984 break;
4985
4986 case PASTE_EX:
4987 if (gap != NULL && ga_grow(gap, idx) == OK)
4988 {
4989 mch_memmove((char *)gap->ga_data + gap->ga_len,
4990 buf, (size_t)idx);
4991 gap->ga_len += idx;
4992 }
4993 break;
4994
4995 case PASTE_INSERT:
4996 if (stop_arrow() == OK)
4997 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004998 c = buf[0];
4999 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
5000 ins_eol(c);
5001 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01005002 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005003 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01005004 AppendToRedobuffLit(buf, idx);
5005 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01005006 }
5007 break;
5008
5009 case PASTE_ONE_CHAR:
5010 if (ret_char == -1)
5011 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01005012 if (has_mbyte)
5013 ret_char = (*mb_ptr2char)(buf);
5014 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005015 ret_char = buf[0];
5016 }
5017 break;
5018 }
5019 }
5020 idx = 0;
5021 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005022
Bram Moolenaarec2da362017-01-21 20:04:22 +01005023 --no_mapping;
5024 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005025 if (!save_paste)
5026 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01005027
5028 return ret_char;
5029}
5030
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005031#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00005032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005033ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005034{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005035 // We will be leaving the current window, unless closing another tab.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005036 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
5037 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
5038 {
5039 undisplay_dollar();
5040 start_arrow(&curwin->w_cursor);
5041# ifdef FEAT_CINDENT
5042 can_cindent = TRUE;
5043# endif
5044 }
5045
5046 if (c == K_TABLINE)
5047 goto_tabpage(current_tab);
5048 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005049 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005050 handle_tabmenu();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005051 redraw_statuslines(); // will redraw the tabline when needed
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005052 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005053}
5054#endif
5055
5056#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005058ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
5060 pos_T tpos;
5061
5062 undisplay_dollar();
5063 tpos = curwin->w_cursor;
5064 if (gui_do_scroll())
5065 {
5066 start_arrow(&tpos);
5067# ifdef FEAT_CINDENT
5068 can_cindent = TRUE;
5069# endif
5070 }
5071}
5072
5073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005074ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
5076 pos_T tpos;
5077
5078 undisplay_dollar();
5079 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005080 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 start_arrow(&tpos);
5083# ifdef FEAT_CINDENT
5084 can_cindent = TRUE;
5085# endif
5086 }
5087}
5088#endif
5089
5090 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005091ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
5093 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005094 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
5096#ifdef FEAT_FOLDING
5097 if ((fdo_flags & FDO_HOR) && KeyTyped)
5098 foldOpenCursor();
5099#endif
5100 undisplay_dollar();
5101 tpos = curwin->w_cursor;
5102 if (oneleft() == OK)
5103 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005104#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005105 // Only call start_arrow() when not busy with preediting, it will
5106 // break undo. K_LEFT is inserted in im_correct_cursor().
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005107 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005108#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005109 {
5110 start_arrow_with_change(&tpos, end_change);
5111 if (!end_change)
5112 AppendCharToRedobuff(K_LEFT);
5113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005115 // If exit reversed string, position is fixed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5117 revins_legal++;
5118 revins_chars++;
5119#endif
5120 }
5121
5122 /*
5123 * if 'whichwrap' set for cursor in insert mode may go to
5124 * previous line
5125 */
5126 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005128 // always break undo when moving upwards/downwards, else undo may break
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 start_arrow(&tpos);
5130 --(curwin->w_cursor.lnum);
5131 coladvance((colnr_T)MAXCOL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005132 curwin->w_set_curswant = TRUE; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005135 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005136 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137}
5138
5139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005140ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 pos_T tpos;
5143
5144#ifdef FEAT_FOLDING
5145 if ((fdo_flags & FDO_HOR) && KeyTyped)
5146 foldOpenCursor();
5147#endif
5148 undisplay_dollar();
5149 tpos = curwin->w_cursor;
5150 if (c == K_C_HOME)
5151 curwin->w_cursor.lnum = 1;
5152 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 curwin->w_curswant = 0;
5155 start_arrow(&tpos);
5156}
5157
5158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005159ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160{
5161 pos_T tpos;
5162
5163#ifdef FEAT_FOLDING
5164 if ((fdo_flags & FDO_HOR) && KeyTyped)
5165 foldOpenCursor();
5166#endif
5167 undisplay_dollar();
5168 tpos = curwin->w_cursor;
5169 if (c == K_C_END)
5170 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5171 coladvance((colnr_T)MAXCOL);
5172 curwin->w_curswant = MAXCOL;
5173
5174 start_arrow(&tpos);
5175}
5176
5177 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005178ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005180 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#ifdef FEAT_FOLDING
5182 if ((fdo_flags & FDO_HOR) && KeyTyped)
5183 foldOpenCursor();
5184#endif
5185 undisplay_dollar();
5186 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5187 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005188 start_arrow_with_change(&curwin->w_cursor, end_change);
5189 if (!end_change)
5190 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 (void)bck_word(1L, FALSE, FALSE);
5192 curwin->w_set_curswant = TRUE;
5193 }
5194 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005195 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005196 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197}
5198
5199 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005200ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005202 int end_change = dont_sync_undo == FALSE; // end undoable change
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204#ifdef FEAT_FOLDING
5205 if ((fdo_flags & FDO_HOR) && KeyTyped)
5206 foldOpenCursor();
5207#endif
5208 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005209 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005211 start_arrow_with_change(&curwin->w_cursor, end_change);
5212 if (!end_change)
5213 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (virtual_active())
5216 oneright();
5217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005220 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 ++curwin->w_cursor.col;
5223 }
5224
5225#ifdef FEAT_RIGHTLEFT
5226 revins_legal++;
5227 if (revins_chars)
5228 revins_chars--;
5229#endif
5230 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005231 // if 'whichwrap' set for cursor in insert mode, may move the
5232 // cursor to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 else if (vim_strchr(p_ww, ']') != NULL
5234 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5235 {
5236 start_arrow(&curwin->w_cursor);
5237 curwin->w_set_curswant = TRUE;
5238 ++curwin->w_cursor.lnum;
5239 curwin->w_cursor.col = 0;
5240 }
5241 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005242 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005243 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244}
5245
5246 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005247ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005249 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250#ifdef FEAT_FOLDING
5251 if ((fdo_flags & FDO_HOR) && KeyTyped)
5252 foldOpenCursor();
5253#endif
5254 undisplay_dollar();
5255 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5256 || gchar_cursor() != NUL)
5257 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005258 start_arrow_with_change(&curwin->w_cursor, end_change);
5259 if (!end_change)
5260 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 (void)fwd_word(1L, FALSE, 0);
5262 curwin->w_set_curswant = TRUE;
5263 }
5264 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005265 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005266 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267}
5268
5269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005270ins_up(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005271 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 pos_T tpos;
5274 linenr_T old_topline = curwin->w_topline;
5275#ifdef FEAT_DIFF
5276 int old_topfill = curwin->w_topfill;
5277#endif
5278
5279 undisplay_dollar();
5280 tpos = curwin->w_cursor;
5281 if (cursor_up(1L, TRUE) == OK)
5282 {
5283 if (startcol)
5284 coladvance(getvcol_nolist(&Insstart));
5285 if (old_topline != curwin->w_topline
5286#ifdef FEAT_DIFF
5287 || old_topfill != curwin->w_topfill
5288#endif
5289 )
5290 redraw_later(VALID);
5291 start_arrow(&tpos);
5292#ifdef FEAT_CINDENT
5293 can_cindent = TRUE;
5294#endif
5295 }
5296 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005297 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298}
5299
5300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005301ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
5303 pos_T tpos;
5304
5305 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005306
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005307 if (mod_mask & MOD_MASK_CTRL)
5308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 // <C-PageUp>: tab page back
Bram Moolenaarbc444822006-10-17 11:37:50 +00005310 if (first_tabpage->tp_next != NULL)
5311 {
5312 start_arrow(&curwin->w_cursor);
5313 goto_tabpage(-1);
5314 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005315 return;
5316 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 tpos = curwin->w_cursor;
5319 if (onepage(BACKWARD, 1L) == OK)
5320 {
5321 start_arrow(&tpos);
5322#ifdef FEAT_CINDENT
5323 can_cindent = TRUE;
5324#endif
5325 }
5326 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005327 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328}
5329
5330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005331ins_down(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005332 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 pos_T tpos;
5335 linenr_T old_topline = curwin->w_topline;
5336#ifdef FEAT_DIFF
5337 int old_topfill = curwin->w_topfill;
5338#endif
5339
5340 undisplay_dollar();
5341 tpos = curwin->w_cursor;
5342 if (cursor_down(1L, TRUE) == OK)
5343 {
5344 if (startcol)
5345 coladvance(getvcol_nolist(&Insstart));
5346 if (old_topline != curwin->w_topline
5347#ifdef FEAT_DIFF
5348 || old_topfill != curwin->w_topfill
5349#endif
5350 )
5351 redraw_later(VALID);
5352 start_arrow(&tpos);
5353#ifdef FEAT_CINDENT
5354 can_cindent = TRUE;
5355#endif
5356 }
5357 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005358 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359}
5360
5361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005362ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363{
5364 pos_T tpos;
5365
5366 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005367
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005368 if (mod_mask & MOD_MASK_CTRL)
5369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 // <C-PageDown>: tab page forward
Bram Moolenaarbc444822006-10-17 11:37:50 +00005371 if (first_tabpage->tp_next != NULL)
5372 {
5373 start_arrow(&curwin->w_cursor);
5374 goto_tabpage(0);
5375 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005376 return;
5377 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 tpos = curwin->w_cursor;
5380 if (onepage(FORWARD, 1L) == OK)
5381 {
5382 start_arrow(&tpos);
5383#ifdef FEAT_CINDENT
5384 can_cindent = TRUE;
5385#endif
5386 }
5387 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005388 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389}
5390
5391#ifdef FEAT_DND
5392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005393ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
5395 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5396}
5397#endif
5398
5399/*
5400 * Handle TAB in Insert or Replace mode.
5401 * Return TRUE when the TAB needs to be inserted like a normal character.
5402 */
5403 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005404ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 int ind;
5407 int i;
5408 int temp;
5409
5410 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5411 Insstart_blank_vcol = get_nolist_virtcol();
5412 if (echeck_abbr(TAB + ABBR_OFF))
5413 return FALSE;
5414
5415 ind = inindent(0);
5416#ifdef FEAT_CINDENT
5417 if (ind)
5418 can_cindent = FALSE;
5419#endif
5420
5421 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005422 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 */
5424 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005425#ifdef FEAT_VARTABS
5426 && !(p_sta && ind
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 // These five lines mean 'tabstop' != 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005428 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5429 || (tabstop_count(curbuf->b_p_vts_array) == 1
5430 && tabstop_first(curbuf->b_p_vts_array)
5431 != get_sw_value(curbuf))
5432 || (tabstop_count(curbuf->b_p_vts_array) == 0
5433 && curbuf->b_p_ts != get_sw_value(curbuf))))
5434 && tabstop_count(curbuf->b_p_vsts_array) == 0
5435#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005436 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005437#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005438 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 return TRUE;
5440
5441 if (stop_arrow() == FAIL)
5442 return TRUE;
5443
5444 did_ai = FALSE;
5445#ifdef FEAT_SMARTINDENT
5446 did_si = FALSE;
5447 can_si = FALSE;
5448 can_si_back = FALSE;
5449#endif
5450 AppendToRedobuff((char_u *)"\t");
5451
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005452#ifdef FEAT_VARTABS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005454 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005455 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005456 temp -= get_nolist_virtcol() % temp;
5457 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005458 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 // use 'softtabstop' when set
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005460 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005461 curbuf->b_p_vsts_array);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005462 else // otherwise use 'tabstop'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005463 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5464 curbuf->b_p_vts_array);
5465#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005467 temp = (int)get_sw_value(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005468 else if (curbuf->b_p_sts != 0) // use 'softtabstop' when set
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005469 temp = (int)get_sts_value();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 else // otherwise use 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 temp = (int)curbuf->b_p_ts;
5472 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 /*
5476 * Insert the first space with ins_char(). It will delete one char in
5477 * replace mode. Insert the rest with ins_str(); it will not delete any
5478 * chars. For VREPLACE mode, we use ins_char() for all characters.
5479 */
5480 ins_char(' ');
5481 while (--temp > 0)
5482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (State & VREPLACE_FLAG)
5484 ins_char(' ');
5485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
5487 ins_str((char_u *)" ");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005488 if (State & REPLACE_FLAG) // no char replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 replace_push(NUL);
5490 }
5491 }
5492
5493 /*
5494 * When 'expandtab' not set: Replace spaces by TABs where possible.
5495 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005496#ifdef FEAT_VARTABS
5497 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5498 || get_sts_value() > 0
5499 || (p_sta && ind)))
5500#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005501 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 char_u *ptr;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 char_u *saved_line = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 pos_T fpos;
5508 pos_T *cursor;
5509 colnr_T want_vcol, vcol;
5510 int change_col = -1;
5511 int save_list = curwin->w_p_list;
5512
5513 /*
5514 * Get the current line. For VREPLACE mode, don't make real changes
5515 * yet, just work on a copy of the line.
5516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (State & VREPLACE_FLAG)
5518 {
5519 pos = curwin->w_cursor;
5520 cursor = &pos;
5521 saved_line = vim_strsave(ml_get_curline());
5522 if (saved_line == NULL)
5523 return FALSE;
5524 ptr = saved_line + pos.col;
5525 }
5526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 {
5528 ptr = ml_get_cursor();
5529 cursor = &curwin->w_cursor;
5530 }
5531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005532 // When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5534 curwin->w_p_list = FALSE;
5535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005536 // Find first white before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005538 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 --fpos.col;
5541 --ptr;
5542 }
5543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005544 // In Replace mode, don't change characters before the insert point.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 if ((State & REPLACE_FLAG)
5546 && fpos.lnum == Insstart.lnum
5547 && fpos.col < Insstart.col)
5548 {
5549 ptr += Insstart.col - fpos.col;
5550 fpos.col = Insstart.col;
5551 }
5552
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005553 // compute virtual column numbers of first white and cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5555 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005557 // Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5558 // and 'linebreak' adding extra virtual columns.
Bram Moolenaar1c465442017-03-12 20:10:05 +01005559 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005561 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 if (vcol + i > want_vcol)
5563 break;
5564 if (*ptr != TAB)
5565 {
5566 *ptr = TAB;
5567 if (change_col < 0)
5568 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005569 change_col = fpos.col; // Column of first change
5570 // May have to adjust Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5572 Insstart.col = fpos.col;
5573 }
5574 }
5575 ++fpos.col;
5576 ++ptr;
5577 vcol += i;
5578 }
5579
5580 if (change_col >= 0)
5581 {
5582 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005583 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 // Skip over the spaces we need.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 while (vcol < want_vcol && *ptr == ' ')
5587 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005588 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 ++ptr;
5590 ++repl_off;
5591 }
5592 if (vcol > want_vcol)
5593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005594 // Must have a char with 'showbreak' just before it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 --ptr;
5596 --repl_off;
5597 }
5598 fpos.col += repl_off;
5599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005600 // Delete following spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 i = cursor->col - fpos.col;
5602 if (i > 0)
5603 {
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005604#ifdef FEAT_PROP_POPUP
5605 if (!(State & VREPLACE_FLAG))
5606 {
5607 mch_memmove(ptr, ptr + i, curbuf->b_ml.ml_line_len - i
5608 - (ptr - curbuf->b_ml.ml_line_ptr));
5609 curbuf->b_ml.ml_line_len -= i;
5610 }
5611 else
5612#endif
5613 STRMOVE(ptr, ptr + i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005614 // correct replace stack.
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005615 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 for (temp = i; --temp >= 0; )
5617 replace_join(repl_off);
5618 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005619#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005620 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005621 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005622 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005623 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5624 (char_u *)"\t", 1);
5625 }
5626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 cursor->col -= i;
5628
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 /*
5630 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5631 * backspacing over the changed spacing and then inserting the new
5632 * spacing.
5633 */
5634 if (State & VREPLACE_FLAG)
5635 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005636 // Backspace from real cursor to change_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 backspace_until_column(change_col);
5638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005639 // Insert each char in saved_line from changed_col to
5640 // ptr-cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 ins_bytes_len(saved_line + change_col,
5642 cursor->col - change_col);
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 if (State & VREPLACE_FLAG)
5647 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 curwin->w_p_list = save_list;
5649 }
5650
5651 return FALSE;
5652}
5653
5654/*
5655 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005656 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005658 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005659ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661 int i;
5662
5663 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005664 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005666 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 undisplay_dollar();
5668
5669 /*
5670 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5671 * character under the cursor. Only push a NUL on the replace stack,
5672 * nothing to put back when the NL is deleted.
5673 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005674 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 replace_push(NUL);
5676
5677 /*
5678 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5679 * replacing the next line, so we push all of the characters left on the
5680 * line onto the replace stack. This is not done here though, it is done
5681 * in open_line().
5682 */
5683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005684 // Put cursor on NUL if on the last char and coladd is 1 (happens after
5685 // CTRL-O).
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005686 if (virtual_active() && curwin->w_cursor.coladd > 0)
5687 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005690 // NL in reverse insert will always start in the end of
5691 // current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (revins_on)
5693 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5694#endif
5695
5696 AppendToRedobuff(NL_STR);
5697 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005698 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 old_indent = 0;
5700#ifdef FEAT_CINDENT
5701 can_cindent = TRUE;
5702#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005703#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005704 // When inserting a line the cursor line must never be in a closed fold.
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005705 foldOpenCursor();
5706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005708 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709}
5710
5711#ifdef FEAT_DIGRAPHS
5712/*
5713 * Handle digraph in insert mode.
5714 * Returns character still to be inserted, or NUL when nothing remaining to be
5715 * done.
5716 */
5717 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005718ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719{
5720 int c;
5721 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005722 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
5724 pc_status = PC_STATUS_UNSET;
5725 if (redrawing() && !char_avail())
5726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005727 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005728 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
5730 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005731 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732#ifdef FEAT_CMDL_INFO
5733 add_to_showcmd_c(Ctrl_K);
5734#endif
5735 }
5736
5737#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005738 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739#endif
5740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005741 // don't map the digraph chars. This also prevents the
5742 // mode message to be deleted when ESC is hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 ++no_mapping;
5744 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005745 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 --no_mapping;
5747 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005748 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005749 // when the line fits in 'columns' the '?' is at the start of the next
5750 // line and will not be removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005751 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005753 if (IS_SPECIAL(c) || mod_mask) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755#ifdef FEAT_CMDL_INFO
5756 clear_showcmd();
5757#endif
5758 insert_special(c, TRUE, FALSE);
5759 return NUL;
5760 }
5761 if (c != ESC)
5762 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005763 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 if (redrawing() && !char_avail())
5765 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005766 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005767 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769 if (char2cells(c) == 1)
5770 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005771 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005773 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 }
5775#ifdef FEAT_CMDL_INFO
5776 add_to_showcmd_c(c);
5777#endif
5778 }
5779 ++no_mapping;
5780 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005781 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 --no_mapping;
5783 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005784 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005785 // when the line fits in 'columns' the '?' is at the start of the
5786 // next line and will not be removed by a redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005787 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 if (cc != ESC)
5789 {
5790 AppendToRedobuff((char_u *)CTRL_V_STR);
5791 c = getdigraph(c, cc, TRUE);
5792#ifdef FEAT_CMDL_INFO
5793 clear_showcmd();
5794#endif
5795 return c;
5796 }
5797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798#ifdef FEAT_CMDL_INFO
5799 clear_showcmd();
5800#endif
5801 return NUL;
5802}
5803#endif
5804
5805/*
5806 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5807 * Returns the char to be inserted, or NUL if none found.
5808 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005810ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 int c;
5813 int temp;
5814 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005815 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
5817 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5818 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005819 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 return NUL;
5821 }
5822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005823 // try to advance to the cursor column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005825 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 prev_ptr = ptr;
5827 validate_virtcol();
5828 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5829 {
5830 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005831 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
5833 if ((colnr_T)temp > curwin->w_virtcol)
5834 ptr = prev_ptr;
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005838 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 return c;
5840}
5841
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005842/*
5843 * CTRL-Y or CTRL-E typed in Insert mode.
5844 */
5845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005846ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005847{
5848 int c = tc;
5849
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005850 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005851 {
5852 if (c == Ctrl_Y)
5853 scrolldown_clamp();
5854 else
5855 scrollup_clamp();
5856 redraw_later(VALID);
5857 }
5858 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005859 {
5860 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5861 if (c != NUL)
5862 {
5863 long tw_save;
5864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005865 // The character must be taken literally, insert like it
5866 // was typed after a CTRL-V, and pretend 'textwidth'
5867 // wasn't set. Digits, 'o' and 'x' are special after a
5868 // CTRL-V, don't use it for these.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005869 if (c < 256 && !isalnum(c))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005870 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005871 tw_save = curbuf->b_p_tw;
5872 curbuf->b_p_tw = -1;
5873 insert_special(c, TRUE, FALSE);
5874 curbuf->b_p_tw = tw_save;
5875#ifdef FEAT_RIGHTLEFT
5876 revins_chars++;
5877 revins_legal++;
5878#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005879 c = Ctrl_V; // pretend CTRL-V is last character
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005880 auto_format(FALSE, TRUE);
5881 }
5882 }
5883 return c;
5884}
5885
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886/*
5887 * Get the value that w_virtcol would have when 'list' is off.
5888 * Unless 'cpo' contains the 'L' flag.
5889 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005890 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005891get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005893 // check validity of cursor in current buffer
5894 if (curwin->w_buffer == NULL
5895 || curwin->w_buffer->b_ml.ml_mfp == NULL
5896 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5897 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5899 return getvcol_nolist(&curwin->w_cursor);
5900 validate_virtcol();
5901 return curwin->w_virtcol;
5902}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005903
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005904#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005905/*
5906 * Handle the InsertCharPre autocommand.
5907 * "c" is the character that was typed.
5908 * Return a pointer to allocated memory with the replacement string.
5909 * Return NULL to continue inserting "c".
5910 */
5911 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005912do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005913{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005914 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005915 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005916 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005918 // Return quickly when there is nothing to do.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005919 if (!has_insertcharpre())
5920 return NULL;
5921
Bram Moolenaar704984a2012-06-01 14:57:51 +02005922 if (has_mbyte)
5923 buf[(*mb_char2bytes)(c, buf)] = NUL;
5924 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005925 {
5926 buf[0] = c;
5927 buf[1] = NUL;
5928 }
5929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005930 // Lock the text to avoid weird things from happening.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005931 ++textlock;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005932 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005933
Bram Moolenaar704984a2012-06-01 14:57:51 +02005934 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005935 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005937 // Get the value of v:char. It may be empty or more than one
5938 // character. Only use it when changed, otherwise continue with the
5939 // original character to avoid breaking autoindent.
Bram Moolenaar704984a2012-06-01 14:57:51 +02005940 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5941 res = vim_strsave(get_vim_var_str(VV_CHAR));
5942 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005944 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005945 --textlock;
5946
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005947 // Restore the State, it may have been changed.
5948 State = save_State;
5949
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005950 return res;
5951}
5952#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005953
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005954#if defined(FEAT_CINDENT) || defined(PROTO)
5955 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005956get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005957{
5958 return can_cindent;
5959}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005960
5961 void
5962set_can_cindent(int val)
5963{
5964 can_cindent = val;
5965}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005966#endif
5967
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005968/*
5969 * Trigger "event" and take care of fixing undo.
5970 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005971 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005972ins_apply_autocmds(event_T event)
5973{
5974 varnumber_T tick = CHANGEDTICK(curbuf);
5975 int r;
5976
5977 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5978
5979 // If u_savesub() was called then we are not prepared to start
5980 // a new line. Call u_save() with no contents to fix that.
5981 if (tick != CHANGEDTICK(curbuf))
5982 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5983
5984 return r;
5985}