blob: 3f0803f685b9d836a69432bc2ac4069703025f78 [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;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001615#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1616 if (typebuf.tb_len == 0)
1617 typebuf_was_filled = FALSE;
1618#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001619
1620 mod_mask = decode_modifiers(arg[!form]);
1621 c = merge_modifyOtherKeys(arg[form]);
1622 }
1623 }
1624
1625 return c;
1626}
1627
1628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 * Put a character directly onto the screen. It's not stored in a buffer.
1630 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1631 */
1632static int pc_status;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001633#define PC_STATUS_UNSET 0 // pc_bytes was not set
1634#define PC_STATUS_RIGHT 1 // right halve of double-wide char
1635#define PC_STATUS_LEFT 2 // left halve of double-wide char
1636#define PC_STATUS_SET 3 // pc_bytes was filled
1637static char_u pc_bytes[MB_MAXBYTES + 1]; // saved bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638static int pc_attr;
1639static int pc_row;
1640static int pc_col;
1641
1642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001643edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644{
1645 int attr;
1646
1647 if (ScreenLines != NULL)
1648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001649 update_topline(); // just in case w_topline isn't valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 validate_cursor();
1651 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001652 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 else
1654 attr = 0;
1655 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001656 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658#ifdef FEAT_RIGHTLEFT
1659 if (curwin->w_p_rl)
1660 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001661 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (has_mbyte)
1663 {
1664 int fix_col = mb_fix_col(pc_col, pc_row);
1665
1666 if (fix_col != pc_col)
1667 {
1668 screen_putchar(' ', pc_row, fix_col, attr);
1669 --curwin->w_wcol;
1670 pc_status = PC_STATUS_RIGHT;
1671 }
1672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 }
1674 else
1675#endif
1676 {
1677 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (mb_lefthalve(pc_row, pc_col))
1679 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001682 // save the character to be able to put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 {
1685 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1686 pc_status = PC_STATUS_SET;
1687 }
1688 screen_putchar(c, pc_row, pc_col, attr);
1689 }
1690}
1691
Bram Moolenaarf2732452018-06-03 14:47:35 +02001692#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1693/*
1694 * Return the effective prompt for the current buffer.
1695 */
1696 char_u *
1697prompt_text(void)
1698{
1699 if (curbuf->b_prompt_text == NULL)
1700 return (char_u *)"% ";
1701 return curbuf->b_prompt_text;
1702}
1703
1704/*
1705 * Prepare for prompt mode: Make sure the last line has the prompt text.
1706 * Move the cursor to this line.
1707 */
1708 static void
1709init_prompt(int cmdchar_todo)
1710{
1711 char_u *prompt = prompt_text();
1712 char_u *text;
1713
1714 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1715 text = ml_get_curline();
1716 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1717 {
1718 // prompt is missing, insert it or append a line with it
1719 if (*text == NUL)
1720 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1721 else
1722 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1723 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1724 coladvance((colnr_T)MAXCOL);
1725 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1726 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001727
1728 // Insert always starts after the prompt, allow editing text after it.
1729 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1730 || Insstart_orig.col != (int)STRLEN(prompt))
1731 {
1732 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001733 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001734 Insstart_orig = Insstart;
1735 Insstart_textlen = Insstart.col;
1736 Insstart_blank_vcol = MAXCOL;
1737 arrow_used = FALSE;
1738 }
1739
Bram Moolenaarf2732452018-06-03 14:47:35 +02001740 if (cmdchar_todo == 'A')
1741 coladvance((colnr_T)MAXCOL);
1742 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001743 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001744 // Make sure the cursor is in a valid position.
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001745 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001746}
1747
1748/*
1749 * Return TRUE if the cursor is in the editable position of the prompt line.
1750 */
1751 int
1752prompt_curpos_editable()
1753{
1754 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1755 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1756}
1757#endif
1758
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759/*
1760 * Undo the previous edit_putchar().
1761 */
1762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 if (pc_status == PC_STATUS_RIGHT)
1768 ++curwin->w_wcol;
1769 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001770 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1773 }
1774}
1775
1776/*
1777 * Called when p_dollar is set: display a '$' at the end of the changed text
1778 * Only works when cursor is in the line that changes.
1779 */
1780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 colnr_T save_col;
1784
1785 if (!redrawing())
1786 return;
1787
1788 cursor_off();
1789 save_col = curwin->w_cursor.col;
1790 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (has_mbyte)
1792 {
1793 char_u *p;
1794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001795 // If on the last byte of a multi-byte move to the first byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 p = ml_get_curline();
1797 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1798 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001799 curs_columns(FALSE); // recompute w_wrow and w_wcol
Bram Moolenaar02631462017-09-22 15:20:32 +02001800 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 edit_putchar('$', FALSE);
1803 dollar_vcol = curwin->w_virtcol;
1804 }
1805 curwin->w_cursor.col = save_col;
1806}
1807
1808/*
1809 * Call this function before moving the cursor from the normal insert position
1810 * in insert mode.
1811 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001815 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001817 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001818 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820}
1821
1822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 * Truncate the space at the end of a line. This is to be used only in an
1824 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1825 * modes.
1826 */
1827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 int i;
1831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001832 // find start of trailing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +01001833 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
1835 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001836 replace_join(0); // remove a NUL from the replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838 line[i + 1] = NUL;
1839}
1840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841/*
1842 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1843 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001844 * Will attempt not to go before "col" even when there is a composing
1845 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 */
1847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001848backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
1850 while ((int)curwin->w_cursor.col > col)
1851 {
1852 curwin->w_cursor.col--;
1853 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001854 replace_do_bs(col);
1855 else if (!del_char_after_col(col))
1856 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001860/*
1861 * Like del_char(), but make sure not to go before column "limit_col".
1862 * Only matters when there are composing characters.
1863 * Return TRUE when something was deleted.
1864 */
1865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001866del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001867{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001868 if (enc_utf8 && limit_col >= 0)
1869 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001870 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001872 // Make sure the cursor is at the start of a character, but
1873 // skip forward again when going too far back because of a
1874 // composing character.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001875 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001876 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001877 {
1878 int l = utf_ptr2len(ml_get_cursor());
1879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 if (l == 0) // end of line
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001881 break;
1882 curwin->w_cursor.col += l;
1883 }
1884 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1885 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001886 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001887 }
1888 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001889 (void)del_char(FALSE);
1890 return TRUE;
1891}
1892
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893/*
1894 * Next character is interpreted literally.
1895 * A one, two or three digit decimal number is interpreted as its byte value.
1896 * If one or two digits are entered, the next character is given to vungetc().
1897 * For Unicode a character > 255 may be returned.
1898 */
1899 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001900get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902 int cc;
1903 int nc;
1904 int i;
1905 int hex = FALSE;
1906 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
1909 if (got_int)
1910 return Ctrl_C;
1911
1912#ifdef FEAT_GUI
1913 /*
1914 * In GUI there is no point inserting the internal code for a special key.
1915 * It is more useful to insert the string "<KEY>" instead. This would
1916 * probably be useful in a text window too, but it would not be
1917 * vi-compatible (maybe there should be an option for it?) -- webb
1918 */
1919 if (gui.in_use)
1920 ++allow_keys;
1921#endif
1922#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001923 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 ++no_mapping; // don't map the next key hits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 cc = 0;
1927 i = 0;
1928 for (;;)
1929 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001930 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001932 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 add_to_showcmd(nc);
1934#endif
1935 if (nc == 'x' || nc == 'X')
1936 hex = TRUE;
1937 else if (nc == 'o' || nc == 'O')
1938 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 else if (nc == 'u' || nc == 'U')
1940 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 else
1942 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001943 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 if (!vim_isxdigit(nc))
1946 break;
1947 cc = cc * 16 + hex2nr(nc);
1948 }
1949 else if (octal)
1950 {
1951 if (nc < '0' || nc > '7')
1952 break;
1953 cc = cc * 8 + nc - '0';
1954 }
1955 else
1956 {
1957 if (!VIM_ISDIGIT(nc))
1958 break;
1959 cc = cc * 10 + nc - '0';
1960 }
1961
1962 ++i;
1963 }
1964
Bram Moolenaar13505972019-01-24 15:04:48 +01001965 if (cc > 255 && unicode == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001966 cc = 255; // limit range to 0-255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 nc = 0;
1968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 if (hex) // hex: up to two chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 if (i >= 2)
1972 break;
1973 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001974 else if (unicode) // Unicode: up to four or eight chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 {
1976 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1977 break;
1978 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001979 else if (i >= 3) // decimal or octal: up to three chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 break;
1981 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001982 if (i == 0) // no number entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001984 if (nc == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
1986 cc = '\n';
1987 nc = 0;
1988 }
1989 else
1990 {
1991 cc = nc;
1992 nc = 0;
1993 }
1994 }
1995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001996 if (cc == 0) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001998 if (enc_dbcs && (cc & 0xff) == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001999 cc = '?'; // don't accept an illegal DBCS char, the NUL in the
2000 // second byte will cause trouble!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002 --no_mapping;
2003#ifdef FEAT_GUI
2004 if (gui.in_use)
2005 --allow_keys;
2006#endif
2007 if (nc)
2008 vungetc(nc);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002009 got_int = FALSE; // CTRL-C typed after CTRL-V is not an interrupt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 return cc;
2011}
2012
2013/*
2014 * Insert character, taking care of special keys and mod_mask
2015 */
2016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002017insert_special(
2018 int c,
2019 int allow_modmask,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002020 int ctrlv) // c was typed after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021{
2022 char_u *p;
2023 int len;
2024
2025 /*
2026 * Special function key, translate into "<Key>". Up to the last '>' is
2027 * inserted with ins_str(), so as not to replace characters in replace
2028 * mode.
2029 * Only use mod_mask for special keys, to avoid things like <S-Space>,
2030 * unless 'allow_modmask' is TRUE.
2031 */
Bram Moolenaard0573012017-10-28 21:11:06 +02002032#ifdef MACOS_X
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002033 // Command-key never produces a normal key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (mod_mask & MOD_MASK_CMD)
2035 allow_modmask = TRUE;
2036#endif
2037 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
2038 {
2039 p = get_special_key_name(c, mod_mask);
2040 len = (int)STRLEN(p);
2041 c = p[len - 1];
2042 if (len > 2)
2043 {
2044 if (stop_arrow() == FAIL)
2045 return;
2046 p[len - 1] = NUL;
2047 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002048 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 ctrlv = FALSE;
2050 }
2051 }
2052 if (stop_arrow() == OK)
2053 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2054}
2055
2056/*
2057 * Special characters in this context are those that need processing other
2058 * than the simple insertion that can be performed here. This includes ESC
2059 * which terminates the insert, and CR/NL which need special processing to
2060 * open up a new line. This routine tries to optimize insertions performed by
2061 * the "redo", "undo" or "put" commands, so it needs to know when it should
2062 * stop and defer processing to the "normal" mechanism.
2063 * '0' and '^' are special, because they can be followed by CTRL-D.
2064 */
2065#ifdef EBCDIC
2066# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2067#else
2068# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2069#endif
2070
Bram Moolenaar13505972019-01-24 15:04:48 +01002071#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002073/*
2074 * "flags": INSCHAR_FORMAT - force formatting
2075 * INSCHAR_CTRLV - char typed just after CTRL-V
2076 * INSCHAR_NO_FEX - don't use 'formatexpr'
2077 *
2078 * NOTE: passes the flags value straight through to internal_format() which,
2079 * beside INSCHAR_FORMAT (above), is also looking for these:
2080 * INSCHAR_DO_COM - format comments
2081 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002084insertchar(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002085 int c, // character to insert or NUL
2086 int flags, // INSCHAR_FORMAT, etc.
2087 int second_indent) // indent for second line if >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002092 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002094 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
2097 /*
2098 * Try to break the line in two or more pieces when:
2099 * - Always do this if we have been called to do formatting only.
2100 * - Always do this when 'formatoptions' has the 'a' flag and the line
2101 * ends in white space.
2102 * - Otherwise:
2103 * - Don't do this if inserting a blank
2104 * - Don't do this if an existing character is being replaced, unless
2105 * we're in VREPLACE mode.
2106 * - Do this if the cursor is not on the line where insert started
2107 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2108 * before the insert.
2109 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2110 * before 'textwidth'
2111 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002112 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002113 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002114 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 && *ml_get_cursor() != NUL)
2118 && (curwin->w_cursor.lnum != Insstart.lnum
2119 || ((!has_format_option(FO_INS_LONG)
2120 || Insstart_textlen <= (colnr_T)textwidth)
2121 && (!fo_ins_blank
2122 || Insstart_blank_vcol <= (colnr_T)textwidth
2123 ))))))
2124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002125 // Format with 'formatexpr' when it's set. Use internal formatting
2126 // when 'formatexpr' isn't set or it returns non-zero.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002127#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002128 int do_internal = TRUE;
2129 colnr_T virtcol = get_nolist_virtcol()
2130 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002131
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002132 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2133 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002134 {
2135 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002136 // It may be required to save for undo again, e.g. when setline()
2137 // was called.
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002138 ins_need_undo = TRUE;
2139 }
2140 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002142 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002144
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002145 if (c == NUL) // only formatting was wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 return;
2147
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002148 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (did_ai && (int)c == end_comment_pending)
2150 {
2151 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002152 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 int middle_len, end_len;
2154 int i;
2155
2156 /*
2157 * Need to remove existing (middle) comment leader and insert end
2158 * comment leader. First, check what comment leader we can find.
2159 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002160 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002161 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002163 // Skip middle-comment string
2164 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 ++p;
2166 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002167 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002168 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 --middle_len;
2170
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002171 // Find the end-comment string
2172 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 ++p;
2174 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2175
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002176 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002178 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 ;
2180 i++;
2181
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002182 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 i -= middle_len;
2184
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002185 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2187 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002188 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 backspace_until_column(i);
2190
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002191 // Insert the end-comment string, except for the last
2192 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 ins_bytes_len(lead_end, end_len - 1);
2194 }
2195 }
2196 }
2197 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199 did_ai = FALSE;
2200#ifdef FEAT_SMARTINDENT
2201 did_si = FALSE;
2202 can_si = FALSE;
2203 can_si_back = FALSE;
2204#endif
2205
2206 /*
2207 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2208 * This speeds up normal text input considerably.
2209 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2210 * need to re-indent at a ':', or any other character (but not what
2211 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002212 * Don't do this when there an InsertCharPre autocommand is defined,
2213 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002214 * Do the check for InsertCharPre before the call to vpeekc() because the
2215 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 */
2217#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002218 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219#endif
2220
2221 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002223 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 && vpeekc() != NUL
2225 && !(State & REPLACE_FLAG)
2226#ifdef FEAT_CINDENT
2227 && !cindent_on()
2228#endif
2229#ifdef FEAT_RIGHTLEFT
2230 && !p_ri
2231#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002232 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
2234#define INPUT_BUFLEN 100
2235 char_u buf[INPUT_BUFLEN + 1];
2236 int i;
2237 colnr_T virtcol = 0;
2238
2239 buf[0] = c;
2240 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002241 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 virtcol = get_nolist_virtcol();
2243 /*
2244 * Stop the string when:
2245 * - no more chars available
2246 * - finding a special character (command key)
2247 * - buffer is full
2248 * - running into the 'textwidth' boundary
2249 * - need to check for abbreviation: A non-word char after a word-char
2250 */
2251 while ( (c = vpeekc()) != NUL
2252 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 && i < INPUT_BUFLEN
2255 && (textwidth == 0
2256 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2257 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2258 {
2259#ifdef FEAT_RIGHTLEFT
2260 c = vgetc();
2261 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002262 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 buf[i++] = c;
2264#else
2265 buf[i++] = vgetc();
2266#endif
2267 }
2268
2269#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002270 do_digraph(-1); // clear digraphs
2271 do_digraph(buf[i-1]); // may be the start of a digraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#endif
2273 buf[i] = NUL;
2274 ins_str(buf);
2275 if (flags & INSCHAR_CTRLV)
2276 {
2277 redo_literal(*buf);
2278 i = 1;
2279 }
2280 else
2281 i = 0;
2282 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002283 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285 else
2286 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002287 int cc;
2288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2290 {
2291 char_u buf[MB_MAXBYTES + 1];
2292
2293 (*mb_char2bytes)(c, buf);
2294 buf[cc] = NUL;
2295 ins_char_bytes(buf, cc);
2296 AppendCharToRedobuff(c);
2297 }
2298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 ins_char(c);
2301 if (flags & INSCHAR_CTRLV)
2302 redo_literal(c);
2303 else
2304 AppendCharToRedobuff(c);
2305 }
2306 }
2307}
2308
2309/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002310 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002311 *
2312 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2313 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002314 */
2315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002316internal_format(
2317 int textwidth,
2318 int second_indent,
2319 int flags,
2320 int format_only,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002321 int c) // character to be inserted (can be NUL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002322{
2323 int cc;
2324 int save_char = NUL;
2325 int haveto_redraw = FALSE;
2326 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002327 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002328 int fo_white_par = has_format_option(FO_WHITE_PAR);
2329 int first_line = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002330 colnr_T leader_len;
2331 int no_leader = FALSE;
2332 int do_comments = (flags & INSCHAR_DO_COM);
Bram Moolenaar0026d472014-09-09 16:32:39 +02002333#ifdef FEAT_LINEBREAK
2334 int has_lbr = curwin->w_p_lbr;
2335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002336 // make sure win_lbr_chartabsize() counts correctly
Bram Moolenaar0026d472014-09-09 16:32:39 +02002337 curwin->w_p_lbr = FALSE;
2338#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002339
2340 /*
2341 * When 'ai' is off we don't want a space under the cursor to be
2342 * deleted. Replace it with an 'x' temporarily.
2343 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002344 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002345 {
2346 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002347 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002348 {
2349 save_char = cc;
2350 pchar_cursor('x');
2351 }
2352 }
2353
2354 /*
2355 * Repeat breaking lines, until the current line is not too long.
2356 */
2357 while (!got_int)
2358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002359 int startcol; // Cursor column at entry
2360 int wantcol; // column at textwidth border
2361 int foundcol; // column for start of spaces
2362 int end_foundcol = 0; // column for start of word
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002363 colnr_T len;
2364 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002365 int orig_col = 0;
2366 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002367 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002368 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002369 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002370
Bram Moolenaar97b98102009-11-17 16:41:01 +00002371 virtcol = get_nolist_virtcol()
2372 + char2cells(c != NUL ? c : gchar_cursor());
2373 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002374 break;
2375
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002376 if (no_leader)
2377 do_comments = FALSE;
2378 else if (!(flags & INSCHAR_FORMAT)
2379 && has_format_option(FO_WRAP_COMS))
2380 do_comments = TRUE;
2381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002382 // Don't break until after the comment leader
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002383 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002384 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002385 else
2386 leader_len = 0;
2387
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002388 // If the line doesn't start with a comment leader, then don't
2389 // start one in a following broken line. Avoids that a %word
2390 // moved to the start of the next line causes all following lines
2391 // to start with %.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002392 if (leader_len == 0)
2393 no_leader = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002394 if (!(flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002395 && leader_len == 0
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002396 && !has_format_option(FO_WRAP))
2397
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002398 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002399 if ((startcol = curwin->w_cursor.col) == 0)
2400 break;
2401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002402 // find column of textwidth border
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002403 coladvance((colnr_T)textwidth);
2404 wantcol = curwin->w_cursor.col;
2405
Bram Moolenaar97b98102009-11-17 16:41:01 +00002406 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002407 foundcol = 0;
2408
2409 /*
2410 * Find position to break at.
2411 * Stop at first entered white when 'formatoptions' has 'v'
2412 */
2413 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002414 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002415 || curwin->w_cursor.lnum != Insstart.lnum
2416 || curwin->w_cursor.col >= Insstart.col)
2417 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002418 if (curwin->w_cursor.col == startcol && c != NUL)
2419 cc = c;
2420 else
2421 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002422 if (WHITECHAR(cc))
2423 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002424 // remember position of blank just before text
Bram Moolenaar97b98102009-11-17 16:41:01 +00002425 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002426
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002427 // find start of sequence of blanks
2428 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002429 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2430 {
2431 dec_cursor();
2432 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002433
2434 // Increment count of how many whitespace chars in this
2435 // group; we only need to know if it's more than one.
2436 if (wcc < 2)
2437 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002438 }
2439 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002440 break; // only spaces in front of text
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002441
2442 // Don't break after a period when 'formatoptions' has 'p' and
2443 // there are less than two spaces.
2444 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2445 continue;
2446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002447 // Don't break until after the comment leader
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002448 if (curwin->w_cursor.col < leader_len)
2449 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002450 if (has_format_option(FO_ONE_LETTER))
2451 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002452 // do not break after one-letter words
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002453 if (curwin->w_cursor.col == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002454 break; // one-letter word at begin
2455 // do not break "#a b" when 'tw' is 2
Bram Moolenaar97b98102009-11-17 16:41:01 +00002456 if (curwin->w_cursor.col <= leader_len)
2457 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002458 col = curwin->w_cursor.col;
2459 dec_cursor();
2460 cc = gchar_cursor();
2461
2462 if (WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002463 continue; // one-letter, continue
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002464 curwin->w_cursor.col = col;
2465 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002466
2467 inc_cursor();
2468
2469 end_foundcol = end_col + 1;
2470 foundcol = curwin->w_cursor.col;
2471 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002472 break;
2473 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002474 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002475 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002476 // Break after or before a multi-byte character.
Bram Moolenaar97b98102009-11-17 16:41:01 +00002477 if (curwin->w_cursor.col != startcol)
2478 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002479 // Don't break until after the comment leader
Bram Moolenaar97b98102009-11-17 16:41:01 +00002480 if (curwin->w_cursor.col < leader_len)
2481 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002482 col = curwin->w_cursor.col;
2483 inc_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002484 // Don't change end_foundcol if already set.
Bram Moolenaar97b98102009-11-17 16:41:01 +00002485 if (foundcol != curwin->w_cursor.col)
2486 {
2487 foundcol = curwin->w_cursor.col;
2488 end_foundcol = foundcol;
2489 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2490 break;
2491 }
2492 curwin->w_cursor.col = col;
2493 }
2494
2495 if (curwin->w_cursor.col == 0)
2496 break;
2497
2498 col = curwin->w_cursor.col;
2499
2500 dec_cursor();
2501 cc = gchar_cursor();
2502
2503 if (WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002504 continue; // break with space
2505 // Don't break until after the comment leader
Bram Moolenaar97b98102009-11-17 16:41:01 +00002506 if (curwin->w_cursor.col < leader_len)
2507 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002508
2509 curwin->w_cursor.col = col;
2510
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002511 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002512 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002513 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2514 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002515 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002516 if (curwin->w_cursor.col == 0)
2517 break;
2518 dec_cursor();
2519 }
2520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002521 if (foundcol == 0) // no spaces, cannot break line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002522 {
2523 curwin->w_cursor.col = startcol;
2524 break;
2525 }
2526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002527 // Going to break the line, remove any "$" now.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002528 undisplay_dollar();
2529
2530 /*
2531 * Offset between cursor position and line break is used by replace
2532 * stack functions. VREPLACE does not use this, and backspaces
2533 * over the text instead.
2534 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002535 if (State & VREPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002536 orig_col = startcol; // Will start backspacing from here
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002537 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002538 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002539
2540 /*
2541 * adjust startcol for spaces that will be deleted and
2542 * characters that will remain on top line
2543 */
2544 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002545 while ((cc = gchar_cursor(), WHITECHAR(cc))
2546 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002547 inc_cursor();
2548 startcol -= curwin->w_cursor.col;
2549 if (startcol < 0)
2550 startcol = 0;
2551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002552 if (State & VREPLACE_FLAG)
2553 {
2554 /*
2555 * In VREPLACE mode, we will backspace over the text to be
2556 * wrapped, so save a copy now to put on the next line.
2557 */
2558 saved_text = vim_strsave(ml_get_cursor());
2559 curwin->w_cursor.col = orig_col;
2560 if (saved_text == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002561 break; // Can't do it, out of memory
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002562 saved_text[startcol] = NUL;
2563
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002564 // Backspace over characters that will move to the next line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002565 if (!fo_white_par)
2566 backspace_until_column(foundcol);
2567 }
2568 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002569 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002570 // put cursor after pos. to break line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002571 if (!fo_white_par)
2572 curwin->w_cursor.col = foundcol;
2573 }
2574
2575 /*
2576 * Split the line just before the margin.
2577 * Only insert/delete lines, but don't really redraw the window.
2578 */
2579 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2580 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002581 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002582 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002583 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2584 if (!(flags & INSCHAR_COM_LIST))
2585 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002586
2587 replace_offset = 0;
2588 if (first_line)
2589 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002590 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002591 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002592 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002593 * This section is for auto-wrap of numeric lists. When not
2594 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2595 * flag will be set and open_line() will handle it (as seen
2596 * above). The code here (and in get_number_indent()) will
2597 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002598 */
2599 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002600 second_indent =
2601 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002602 if (second_indent >= 0)
2603 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002604 if (State & VREPLACE_FLAG)
2605 change_indent(INDENT_SET, second_indent,
2606 FALSE, NUL, TRUE);
2607 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002608 if (leader_len > 0 && second_indent - leader_len > 0)
2609 {
2610 int i;
2611 int padding = second_indent - leader_len;
2612
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002613 // We started at the first_line of a numbered list
2614 // that has a comment. the open_line() function has
2615 // inserted the proper comment leader and positioned
2616 // the cursor at the end of the split line. Now we
2617 // add the additional whitespace needed after the
2618 // comment leader for the numbered list.
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002619 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002620 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002621 }
2622 else
2623 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002624 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002625 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002626 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002627 }
2628 first_line = FALSE;
2629 }
2630
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002631 if (State & VREPLACE_FLAG)
2632 {
2633 /*
2634 * In VREPLACE mode we have backspaced over the text to be
2635 * moved, now we re-insert it into the new line.
2636 */
2637 ins_bytes(saved_text);
2638 vim_free(saved_text);
2639 }
2640 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002641 {
2642 /*
2643 * Check if cursor is not past the NUL off the line, cindent
2644 * may have added or removed indent.
2645 */
2646 curwin->w_cursor.col += startcol;
2647 len = (colnr_T)STRLEN(ml_get_curline());
2648 if (curwin->w_cursor.col > len)
2649 curwin->w_cursor.col = len;
2650 }
2651
2652 haveto_redraw = TRUE;
2653#ifdef FEAT_CINDENT
2654 can_cindent = TRUE;
2655#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002656 // moved the cursor, don't autoindent or cindent now
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002657 did_ai = FALSE;
2658#ifdef FEAT_SMARTINDENT
2659 did_si = FALSE;
2660 can_si = FALSE;
2661 can_si_back = FALSE;
2662#endif
2663 line_breakcheck();
2664 }
2665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002666 if (save_char != NUL) // put back space after cursor
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002667 pchar_cursor(save_char);
2668
Bram Moolenaar0026d472014-09-09 16:32:39 +02002669#ifdef FEAT_LINEBREAK
2670 curwin->w_p_lbr = has_lbr;
2671#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002672 if (!format_only && haveto_redraw)
2673 {
2674 update_topline();
2675 redraw_curbuf_later(VALID);
2676 }
2677}
2678
2679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 * Called after inserting or deleting text: When 'formatoptions' includes the
2681 * 'a' flag format from the current line until the end of the paragraph.
2682 * Keep the cursor at the same position relative to the text.
2683 * The caller must have saved the cursor line for undo, following ones will be
2684 * saved here.
2685 */
2686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002687auto_format(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002688 int trailblank, // when TRUE also format with trailing blank
2689 int prev_line) // may start in previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
2691 pos_T pos;
2692 colnr_T len;
2693 char_u *old;
2694 char_u *new, *pnew;
2695 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002696 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698 if (!has_format_option(FO_AUTO))
2699 return;
2700
2701 pos = curwin->w_cursor;
2702 old = ml_get_curline();
2703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002704 // may remove added space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 check_auto_format(FALSE);
2706
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002707 // Don't format in Insert mode when the cursor is on a trailing blank, the
2708 // user might insert normal text next. Also skip formatting when "1" is
2709 // in 'formatoptions' and there is a single character before the cursor.
2710 // Otherwise the line would be broken and when typing another non-white
2711 // next they are not joined back together.
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002712 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (*old != NUL && !trailblank && wasatend)
2714 {
2715 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002716 cc = gchar_cursor();
2717 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2718 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002720 cc = gchar_cursor();
2721 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
2723 curwin->w_cursor = pos;
2724 return;
2725 }
2726 curwin->w_cursor = pos;
2727 }
2728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002729 // With the 'c' flag in 'formatoptions' and 't' missing: only format
2730 // comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002732 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
2735 /*
2736 * May start formatting in a previous line, so that after "x" a word is
2737 * moved to the previous line if it fits there now. Only when this is not
2738 * the start of a paragraph.
2739 */
2740 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2741 {
2742 --curwin->w_cursor.lnum;
2743 if (u_save_cursor() == FAIL)
2744 return;
2745 }
2746
2747 /*
2748 * Do the formatting and restore the cursor position. "saved_cursor" will
2749 * be adjusted for the text formatting.
2750 */
2751 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002752 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 curwin->w_cursor = saved_cursor;
2754 saved_cursor.lnum = 0;
2755
2756 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002758 // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2760 coladvance((colnr_T)MAXCOL);
2761 }
2762 else
2763 check_cursor_col();
2764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002765 // Insert mode: If the cursor is now after the end of the line while it
2766 // previously wasn't, the line was broken. Because of the rule above we
2767 // need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2768 // formatted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (!wasatend && has_format_option(FO_WHITE_PAR))
2770 {
2771 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002772 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 if (curwin->w_cursor.col == len)
2774 {
2775 pnew = vim_strnsave(new, len + 2);
2776 pnew[len] = ' ';
2777 pnew[len + 1] = NUL;
2778 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002779 // remove the space later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 did_add_space = TRUE;
2781 }
2782 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002783 // may remove added space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 check_auto_format(FALSE);
2785 }
2786
2787 check_cursor();
2788}
2789
2790/*
2791 * When an extra space was added to continue a paragraph for auto-formatting,
2792 * delete it now. The space must be under the cursor, just after the insert
2793 * position.
2794 */
2795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002796check_auto_format(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002797 int end_insert) // TRUE when ending Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002800 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802 if (did_add_space)
2803 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002804 cc = gchar_cursor();
2805 if (!WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002806 // Somehow the space was removed already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 did_add_space = FALSE;
2808 else
2809 {
2810 if (!end_insert)
2811 {
2812 inc_cursor();
2813 c = gchar_cursor();
2814 dec_cursor();
2815 }
2816 if (c != NUL)
2817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // The space is no longer at the end of the line, delete it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 del_char(FALSE);
2820 did_add_space = FALSE;
2821 }
2822 }
2823 }
2824}
2825
2826/*
2827 * Find out textwidth to be used for formatting:
2828 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02002829 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 * if invalid value, use 0.
2831 * Set default to window width (maximum 79) for "gq" operator.
2832 */
2833 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002834comp_textwidth(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002835 int ff) // force formatting (for "gq" command)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837 int textwidth;
2838
2839 textwidth = curbuf->b_p_tw;
2840 if (textwidth == 0 && curbuf->b_p_wm)
2841 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002842 // The width is the window width minus 'wrapmargin' minus all the
2843 // things that add to the margin.
Bram Moolenaar02631462017-09-22 15:20:32 +02002844 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845#ifdef FEAT_CMDWIN
2846 if (cmdwin_type != 0)
2847 textwidth -= 1;
2848#endif
2849#ifdef FEAT_FOLDING
2850 textwidth -= curwin->w_p_fdc;
2851#endif
2852#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002853 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 textwidth -= 1;
2855#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02002856 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 textwidth -= 8;
2858 }
2859 if (textwidth < 0)
2860 textwidth = 0;
2861 if (ff && textwidth == 0)
2862 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002863 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 if (textwidth > 79)
2865 textwidth = 79;
2866 }
2867 return textwidth;
2868}
2869
2870/*
2871 * Put a character in the redo buffer, for when just after a CTRL-V.
2872 */
2873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002874redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 char_u buf[10];
2877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002878 // Only digits need special treatment. Translate them into a string of
2879 // three digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 if (VIM_ISDIGIT(c))
2881 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002882 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 AppendToRedobuff(buf);
2884 }
2885 else
2886 AppendCharToRedobuff(c);
2887}
2888
2889/*
2890 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002891 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002894start_arrow(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002895 pos_T *end_insert_pos) // can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002897 start_arrow_common(end_insert_pos, TRUE);
2898}
2899
2900/*
2901 * Like start_arrow() but with end_change argument.
2902 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2903 */
2904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905start_arrow_with_change(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002906 pos_T *end_insert_pos, // can be NULL
2907 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002908{
2909 start_arrow_common(end_insert_pos, end_change);
2910 if (!end_change)
2911 {
2912 AppendCharToRedobuff(Ctrl_G);
2913 AppendCharToRedobuff('U');
2914 }
2915}
2916
2917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002918start_arrow_common(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002919 pos_T *end_insert_pos, // can be NULL
2920 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002921{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002922 if (!arrow_used && end_change) // something has been inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
2924 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002925 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002926 arrow_used = TRUE; // this means we stopped the current insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002928#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002929 check_spell_redraw();
2930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931}
2932
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002933#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002934/*
2935 * If we skipped highlighting word at cursor, do it now.
2936 * It may be skipped again, thus reset spell_redraw_lnum first.
2937 */
2938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002940{
2941 if (spell_redraw_lnum != 0)
2942 {
2943 linenr_T lnum = spell_redraw_lnum;
2944
2945 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002946 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002947 }
2948}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002949
Bram Moolenaar217ad922005-03-20 22:37:15 +00002950#endif
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952/*
2953 * stop_arrow() is called before a change is made in insert mode.
2954 * If an arrow key has been used, start a new insertion.
2955 * Returns FAIL if undo is impossible, shouldn't insert then.
2956 */
2957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002958stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
2960 if (arrow_used)
2961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002962 Insstart = curwin->w_cursor; // new insertion starts here
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002963 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002964 // Don't update the original insert position when moved to the
2965 // right, except when nothing was inserted yet.
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002966 update_Insstart_orig = FALSE;
2967 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2968
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 if (u_save_cursor() == OK)
2970 {
2971 arrow_used = FALSE;
2972 ins_need_undo = FALSE;
2973 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 if (State & VREPLACE_FLAG)
2977 {
2978 orig_line_count = curbuf->b_ml.ml_line_count;
2979 vr_lines_changed = 1;
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 ResetRedobuff();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002982 AppendToRedobuff((char_u *)"1i"); // pretend we start an insertion
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002983 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
2985 else if (ins_need_undo)
2986 {
2987 if (u_save_cursor() == OK)
2988 ins_need_undo = FALSE;
2989 }
2990
2991#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002992 // Always open fold at the cursor line when inserting something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 foldOpenCursor();
2994#endif
2995
2996 return (arrow_used || ins_need_undo ? FAIL : OK);
2997}
2998
2999/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003000 * Do a few things to stop inserting.
3001 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
3002 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 */
3004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003005stop_insert(
3006 pos_T *end_insert_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003007 int esc, // called by ins_esc()
3008 int nomove) // <c-\><c-o>, don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003010 int cc;
3011 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 stop_redo_ins();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003014 replace_flush(); // abandon replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
3016 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003017 * Save the inserted text for later redo with ^@ and CTRL-A.
3018 * Don't do it when "restart_edit" was set and nothing was inserted,
3019 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003021 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003022 if (did_restart_edit == 0 || (ptr != NULL
3023 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003024 {
3025 vim_free(last_insert);
3026 last_insert = ptr;
3027 last_insert_skip = new_insert_skip;
3028 }
3029 else
3030 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003032 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003034 // Auto-format now. It may seem strange to do this when stopping an
3035 // insertion (or moving the cursor), but it's required when appending
3036 // a line and having it end in a space. But only do it when something
3037 // was actually inserted, otherwise undo won't work.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003038 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003040 pos_T tpos = curwin->w_cursor;
3041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003042 // When the cursor is at the end of the line after a space the
3043 // formatting will move it to the following word. Avoid that by
3044 // moving the cursor onto the space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 cc = 'x';
3046 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
3047 {
3048 dec_cursor();
3049 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003050 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003051 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
3053
3054 auto_format(TRUE, FALSE);
3055
Bram Moolenaar1c465442017-03-12 20:10:05 +01003056 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003057 {
3058 if (gchar_cursor() != NUL)
3059 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003060 // If the cursor is still at the same character, also keep
3061 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003062 if (gchar_cursor() == NUL
3063 && curwin->w_cursor.lnum == tpos.lnum
3064 && curwin->w_cursor.col == tpos.col)
3065 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 }
3068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003069 // If a space was inserted for auto-formatting, remove it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 check_auto_format(TRUE);
3071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003072 // If we just did an auto-indent, remove the white space from the end
3073 // of the line, and put the cursor back.
3074 // Do this when ESC was used or moving the cursor up/down.
3075 // Check for the old position still being valid, just in case the text
3076 // got changed unexpectedly.
Bram Moolenaarf332a652013-11-04 04:20:33 +01003077 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003078 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3079 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003081 pos_T tpos = curwin->w_cursor;
3082
3083 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003084 check_cursor_col(); // make sure it is not past the line
Bram Moolenaar39f05632006-03-19 22:15:26 +00003085 for (;;)
3086 {
3087 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3088 --curwin->w_cursor.col;
3089 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003090 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003091 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003092 if (del_char(TRUE) == FAIL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003093 break; // should not happen
Bram Moolenaar39f05632006-03-19 22:15:26 +00003094 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003095 if (curwin->w_cursor.lnum != tpos.lnum)
3096 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003097 else
3098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003099 // reset tpos, could have been invalidated in the loop above
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003100 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003101 tpos.col++;
3102 if (cc != NUL && gchar_pos(&tpos) == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003103 ++curwin->w_cursor.col; // put cursor back on the NUL
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003106 // <C-S-Right> may have started Visual mode, adjust the position for
3107 // deleted characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3109 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003110 int len = (int)STRLEN(ml_get_curline());
3111
3112 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003114 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119 }
3120 did_ai = FALSE;
3121#ifdef FEAT_SMARTINDENT
3122 did_si = FALSE;
3123 can_si = FALSE;
3124 can_si_back = FALSE;
3125#endif
3126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003127 // Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3128 // now in a different buffer.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003129 if (end_insert_pos != NULL)
3130 {
3131 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003132 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003133 curbuf->b_op_end = *end_insert_pos;
3134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135}
3136
3137/*
3138 * Set the last inserted text to a single character.
3139 * Used for the replace command.
3140 */
3141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003142set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
3144 char_u *s;
3145
3146 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (last_insert != NULL)
3149 {
3150 s = last_insert;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003151 // Use the CTRL-V only when entering a special char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (c < ' ' || c == DEL)
3153 *s++ = Ctrl_V;
3154 s = add_char2buf(c, s);
3155 *s++ = ESC;
3156 *s++ = NUL;
3157 last_insert_skip = 0;
3158 }
3159}
3160
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003161#if defined(EXITFREE) || defined(PROTO)
3162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003163free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003164{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003165 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003166}
3167#endif
3168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169/*
3170 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3171 * and CSI. Handle multi-byte characters.
3172 * Returns a pointer to after the added bytes.
3173 */
3174 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003175add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003177 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 int i;
3179 int len;
3180
3181 len = (*mb_char2bytes)(c, temp);
3182 for (i = 0; i < len; ++i)
3183 {
3184 c = temp[i];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 // Need to escape K_SPECIAL and CSI like in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (c == K_SPECIAL)
3187 {
3188 *s++ = K_SPECIAL;
3189 *s++ = KS_SPECIAL;
3190 *s++ = KE_FILLER;
3191 }
3192#ifdef FEAT_GUI
3193 else if (c == CSI)
3194 {
3195 *s++ = CSI;
3196 *s++ = KS_EXTRA;
3197 *s++ = (int)KE_CSI;
3198 }
3199#endif
3200 else
3201 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 return s;
3204}
3205
3206/*
3207 * move cursor to start of line
3208 * if flags & BL_WHITE move to first non-white
3209 * if flags & BL_SOL move to first non-white if startofline is set,
3210 * otherwise keep "curswant" column
3211 * if flags & BL_FIX don't leave the cursor on a NUL.
3212 */
3213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 if ((flags & BL_SOL) && !p_sol)
3217 coladvance(curwin->w_curswant);
3218 else
3219 {
3220 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 if (flags & (BL_WHITE | BL_SOL))
3224 {
3225 char_u *ptr;
3226
Bram Moolenaar1c465442017-03-12 20:10:05 +01003227 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3229 ++curwin->w_cursor.col;
3230 }
3231 curwin->w_set_curswant = TRUE;
3232 }
3233}
3234
3235/*
3236 * oneright oneleft cursor_down cursor_up
3237 *
3238 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003239 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * Return OK when successful, FAIL when we hit a line of file boundary.
3241 */
3242
3243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003244oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 if (virtual_active())
3250 {
3251 pos_T prevpos = curwin->w_cursor;
3252
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003253 // Adjust for multi-wide char (excluding TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003255 coladvance(getviscol() + ((*ptr != TAB
3256 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 ? ptr2cells(ptr) : 1));
3258 curwin->w_set_curswant = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003259 // Return OK if the cursor moved, FAIL otherwise (at window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 return (prevpos.col != curwin->w_cursor.col
3261 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
3264 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003265 if (*ptr == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003266 return FAIL; // already at the very end
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003267
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003268 if (has_mbyte)
3269 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003271 l = 1;
3272
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003273 // move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3274 // contains "onemore".
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003275 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003276 return FAIL;
3277 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
3279 curwin->w_set_curswant = TRUE;
3280 return OK;
3281}
3282
3283 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003284oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (virtual_active())
3287 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003288#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 int v = getviscol();
3292
3293 if (v == 0)
3294 return FAIL;
3295
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003296#ifdef FEAT_LINEBREAK
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003297 // We might get stuck on 'showbreak', skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 width = 1;
3299 for (;;)
3300 {
3301 coladvance(v - width);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003302 // getviscol() is slow, skip it when 'showbreak' is empty,
3303 // 'breakindent' is not set and there are no multi-byte
3304 // characters
Bram Moolenaaree857022019-11-09 23:26:40 +01003305 if ((*get_showbreak_value(curwin) == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003306 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 break;
3308 ++width;
3309 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003310#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
3314 if (curwin->w_cursor.coladd == 1)
3315 {
3316 char_u *ptr;
3317
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003318 // Adjust for multi-wide char (not a TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003320 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3321 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 curwin->w_cursor.coladd = 0;
3323 }
3324
3325 curwin->w_set_curswant = TRUE;
3326 return OK;
3327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329 if (curwin->w_cursor.col == 0)
3330 return FAIL;
3331
3332 curwin->w_set_curswant = TRUE;
3333 --curwin->w_cursor.col;
3334
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003335 // if the character on the left of the current cursor is a multi-byte
3336 // character, move to its first byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (has_mbyte)
3338 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 return OK;
3340}
3341
3342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343cursor_up(
3344 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003345 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 linenr_T lnum;
3348
3349 if (n > 0)
3350 {
3351 lnum = curwin->w_cursor.lnum;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003352 // This fails if the cursor is already in the first line or the count
3353 // is larger than the line number and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00003354 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 return FAIL;
3356 if (n >= lnum)
3357 lnum = 1;
3358 else
3359#ifdef FEAT_FOLDING
3360 if (hasAnyFolding(curwin))
3361 {
3362 /*
3363 * Count each sequence of folded lines as one logical line.
3364 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003365 // go to the start of the current fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 (void)hasFolding(lnum, &lnum, NULL);
3367
3368 while (n--)
3369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003370 // move up one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 --lnum;
3372 if (lnum <= 1)
3373 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003374 // If we entered a fold, move to the beginning, unless in
3375 // Insert mode or when 'foldopen' contains "all": it will open
3376 // in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3378 (void)hasFolding(lnum, &lnum, NULL);
3379 }
3380 if (lnum < 1)
3381 lnum = 1;
3382 }
3383 else
3384#endif
3385 lnum -= n;
3386 curwin->w_cursor.lnum = lnum;
3387 }
3388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003389 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 coladvance(curwin->w_curswant);
3391
3392 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003393 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 return OK;
3396}
3397
3398/*
3399 * Cursor down a number of logical lines.
3400 */
3401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003402cursor_down(
3403 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003404 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
3406 linenr_T lnum;
3407
3408 if (n > 0)
3409 {
3410 lnum = curwin->w_cursor.lnum;
3411#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003412 // Move to last line of fold, will fail if it's the end-of-file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 (void)hasFolding(lnum, NULL, &lnum);
3414#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003415 // This fails if the cursor is already in the last line or would move
3416 // beyond the last line and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00003417 if (lnum >= curbuf->b_ml.ml_line_count
3418 || (lnum + n > curbuf->b_ml.ml_line_count
3419 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 return FAIL;
3421 if (lnum + n >= curbuf->b_ml.ml_line_count)
3422 lnum = curbuf->b_ml.ml_line_count;
3423 else
3424#ifdef FEAT_FOLDING
3425 if (hasAnyFolding(curwin))
3426 {
3427 linenr_T last;
3428
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003429 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 while (n--)
3431 {
3432 if (hasFolding(lnum, NULL, &last))
3433 lnum = last + 1;
3434 else
3435 ++lnum;
3436 if (lnum >= curbuf->b_ml.ml_line_count)
3437 break;
3438 }
3439 if (lnum > curbuf->b_ml.ml_line_count)
3440 lnum = curbuf->b_ml.ml_line_count;
3441 }
3442 else
3443#endif
3444 lnum += n;
3445 curwin->w_cursor.lnum = lnum;
3446 }
3447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003448 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 coladvance(curwin->w_curswant);
3450
3451 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003452 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 return OK;
3455}
3456
3457/*
3458 * Stuff the last inserted text in the read buffer.
3459 * Last_insert actually is a copy of the redo buffer, so we
3460 * first have to remove the command.
3461 */
3462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003463stuff_inserted(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003464 int c, // Command character to be inserted
3465 long count, // Repeat this many times
3466 int no_esc) // Don't add an ESC at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
3468 char_u *esc_ptr;
3469 char_u *ptr;
3470 char_u *last_ptr;
3471 char_u last = NUL;
3472
3473 ptr = get_last_insert();
3474 if (ptr == NULL)
3475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003476 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 return FAIL;
3478 }
3479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003480 // may want to stuff the command character, to start Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (c != NUL)
3482 stuffcharReadbuff(c);
3483 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003484 *esc_ptr = NUL; // remove the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003486 // when the last char is either "0" or "^" it will be quoted if no ESC
3487 // comes after it OR if it will inserted more than once and "ptr"
3488 // starts with ^D. -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3490 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3491 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3492 {
3493 last = *last_ptr;
3494 *last_ptr = NUL;
3495 }
3496
3497 do
3498 {
3499 stuffReadbuff(ptr);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003500 // a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (last)
3502 stuffReadbuff((char_u *)(last == '0'
3503 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3504 : IF_EB("\026^", CTRL_V_STR "^")));
3505 }
3506 while (--count > 0);
3507
3508 if (last)
3509 *last_ptr = last;
3510
3511 if (esc_ptr != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003512 *esc_ptr = ESC; // put the ESC back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003514 // may want to stuff a trailing ESC, to get out of Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 if (!no_esc)
3516 stuffcharReadbuff(ESC);
3517
3518 return OK;
3519}
3520
3521 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003522get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
3524 if (last_insert == NULL)
3525 return NULL;
3526 return last_insert + last_insert_skip;
3527}
3528
3529/*
3530 * Get last inserted string, and remove trailing <Esc>.
3531 * Returns pointer to allocated memory (must be freed) or NULL.
3532 */
3533 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535{
3536 char_u *s;
3537 int len;
3538
3539 if (last_insert == NULL)
3540 return NULL;
3541 s = vim_strsave(last_insert + last_insert_skip);
3542 if (s != NULL)
3543 {
3544 len = (int)STRLEN(s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003545 if (len > 0 && s[len - 1] == ESC) // remove trailing ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 s[len - 1] = NUL;
3547 }
3548 return s;
3549}
3550
3551/*
3552 * Check the word in front of the cursor for an abbreviation.
3553 * Called when the non-id character "c" has been entered.
3554 * When an abbreviation is recognized it is removed from the text and
3555 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3556 */
3557 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003558echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003560 // Don't check for abbreviation in paste mode, when disabled and just
3561 // after moving around with cursor keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (p_paste || no_abbr || arrow_used)
3563 return FALSE;
3564
3565 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3566 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3567}
3568
3569/*
3570 * replace-stack functions
3571 *
3572 * When replacing characters, the replaced characters are remembered for each
3573 * new character. This is used to re-insert the old text when backspacing.
3574 *
3575 * There is a NUL headed list of characters for each character that is
3576 * currently in the file after the insertion point. When BS is used, one NUL
3577 * headed list is put back for the deleted character.
3578 *
3579 * For a newline, there are two NUL headed lists. One contains the characters
3580 * that the NL replaced. The extra one stores the characters after the cursor
3581 * that were deleted (always white space).
3582 *
3583 * Replace_offset is normally 0, in which case replace_push will add a new
3584 * character at the end of the stack. If replace_offset is not 0, that many
3585 * characters will be left on the stack above the newly inserted character.
3586 */
3587
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003588static char_u *replace_stack = NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003589static long replace_stack_nr = 0; // next entry in replace stack
3590static long replace_stack_len = 0; // max. number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003593replace_push(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003594 int c) // character that is replaced (NUL is none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
3596 char_u *p;
3597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003598 if (replace_stack_nr < replace_offset) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 return;
3600 if (replace_stack_len <= replace_stack_nr)
3601 {
3602 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003603 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003604 if (p == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 replace_stack_len -= 50;
3607 return;
3608 }
3609 if (replace_stack != NULL)
3610 {
3611 mch_memmove(p, replace_stack,
3612 (size_t)(replace_stack_nr * sizeof(char_u)));
3613 vim_free(replace_stack);
3614 }
3615 replace_stack = p;
3616 }
3617 p = replace_stack + replace_stack_nr - replace_offset;
3618 if (replace_offset)
3619 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3620 *p = c;
3621 ++replace_stack_nr;
3622}
3623
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003624/*
3625 * Push a character onto the replace stack. Handles a multi-byte character in
3626 * reverse byte order, so that the first byte is popped off first.
3627 * Return the number of bytes done (includes composing characters).
3628 */
3629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003630replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003631{
3632 int l = (*mb_ptr2len)(p);
3633 int j;
3634
3635 for (j = l - 1; j >= 0; --j)
3636 replace_push(p[j]);
3637 return l;
3638}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640/*
3641 * Pop one item from the replace stack.
3642 * return -1 if stack empty
3643 * return replaced character or NUL otherwise
3644 */
3645 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003646replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 if (replace_stack_nr == 0)
3649 return -1;
3650 return (int)replace_stack[--replace_stack_nr];
3651}
3652
3653/*
3654 * Join the top two items on the replace stack. This removes to "off"'th NUL
3655 * encountered.
3656 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003657 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003658replace_join(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003659 int off) // offset for which NUL to remove
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660{
3661 int i;
3662
3663 for (i = replace_stack_nr; --i >= 0; )
3664 if (replace_stack[i] == NUL && off-- <= 0)
3665 {
3666 --replace_stack_nr;
3667 mch_memmove(replace_stack + i, replace_stack + i + 1,
3668 (size_t)(replace_stack_nr - i));
3669 return;
3670 }
3671}
3672
3673/*
3674 * Pop bytes from the replace stack until a NUL is found, and insert them
3675 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3676 */
3677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003678replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 int cc;
3681 int oldState = State;
3682
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003683 State = NORMAL; // don't want REPLACE here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 while ((cc = replace_pop()) > 0)
3685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 dec_cursor();
3688 }
3689 State = oldState;
3690}
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692/*
3693 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3694 * indicates a multi-byte char, pop the other bytes too.
3695 */
3696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003697mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
3699 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003700 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 int i;
3702 int c;
3703
3704 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3705 {
3706 buf[0] = cc;
3707 for (i = 1; i < n; ++i)
3708 buf[i] = replace_pop();
3709 ins_bytes_len(buf, n);
3710 }
3711 else
3712 ins_char(cc);
3713
3714 if (enc_utf8)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003715 // Handle composing chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 for (;;)
3717 {
3718 c = replace_pop();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 if (c == -1) // stack empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 break;
3721 if ((n = MB_BYTE2LEN(c)) == 1)
3722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003723 // Not a multi-byte char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 replace_push(c);
3725 break;
3726 }
3727 else
3728 {
3729 buf[0] = c;
3730 for (i = 1; i < n; ++i)
3731 buf[i] = replace_pop();
3732 if (utf_iscomposing(utf_ptr2char(buf)))
3733 ins_bytes_len(buf, n);
3734 else
3735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003736 // Not a composing char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 for (i = n - 1; i >= 0; --i)
3738 replace_push(buf[i]);
3739 break;
3740 }
3741 }
3742 }
3743}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745/*
3746 * make the replace stack empty
3747 * (called when exiting replace mode)
3748 */
3749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003750replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003752 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 replace_stack_len = 0;
3754 replace_stack_nr = 0;
3755}
3756
3757/*
3758 * Handle doing a BS for one character.
3759 * cc < 0: replace stack empty, just move cursor
3760 * cc == 0: character was inserted, delete it
3761 * cc > 0: character was replaced, put cc (first byte of original char) back
3762 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003763 * When "limit_col" is >= 0, don't delete before this column. Matters when
3764 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 */
3766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003767replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
3769 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 int orig_len = 0;
3771 int ins_len;
3772 int orig_vcols = 0;
3773 colnr_T start_vcol;
3774 char_u *p;
3775 int i;
3776 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
3778 cc = replace_pop();
3779 if (cc > 0)
3780 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003781#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003782 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003783
3784 if (curbuf->b_has_textprop)
3785 {
3786 // Do not adjust text properties for individual delete and insert
3787 // operations, do it afterwards on the resulting text.
3788 len_before = STRLEN(ml_get_curline());
3789 ++text_prop_frozen;
3790 }
3791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (State & VREPLACE_FLAG)
3793 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003794 // Get the number of screen cells used by the character we are
3795 // going to delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3797 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 if (has_mbyte)
3800 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003801 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003803 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 replace_push(cc);
3805 }
3806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 {
3808 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003810 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812 replace_pop_ins();
3813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (State & VREPLACE_FLAG)
3815 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003816 // Get the number of screen cells used by the inserted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003818 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 vcol = start_vcol;
3820 for (i = 0; i < ins_len; ++i)
3821 {
3822 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003823 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825 vcol -= start_vcol;
3826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003827 // Delete spaces that were inserted after the cursor to keep the
3828 // text aligned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 curwin->w_cursor.col += ins_len;
3830 while (vcol > orig_vcols && gchar_cursor() == ' ')
3831 {
3832 del_char(FALSE);
3833 ++orig_vcols;
3834 }
3835 curwin->w_cursor.col -= ins_len;
3836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
Bram Moolenaar196d1572019-01-02 23:47:18 +01003838 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003840
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003841#ifdef FEAT_PROP_POPUP
Bram Moolenaar196d1572019-01-02 23:47:18 +01003842 if (curbuf->b_has_textprop)
3843 {
3844 size_t len_now = STRLEN(ml_get_curline());
3845
3846 --text_prop_frozen;
3847 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003848 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003849 }
3850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003853 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854}
3855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3857/*
3858 * Map Hebrew keyboard when in hkmap mode.
3859 */
3860 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003861hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003863 if (p_hkmapp) // phonetic mapping, by Ilya Dogolazky
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 {
3865 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3866 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3867 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3868 static char_u map[26] =
3869 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3870 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3871 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3872 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3873 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3874 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3875 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3876 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3877 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3878
3879 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3880 return (int)(map[CharOrd(c)] - 1 + p_aleph);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 // '-1'='sofit'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 else if (c == 'x')
3883 return 'X';
3884 else if (c == 'q')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003885 return '\''; // {geresh}={'}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 else if (c == 246)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003887 return ' '; // \"o --> ' ' for a german keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 else if (c == 228)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003889 return ' '; // \"a --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 else if (c == 252)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003891 return ' '; // \"u --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892#ifdef EBCDIC
3893 else if (islower(c))
3894#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003895 // NOTE: islower() does not do the right thing for us on Linux so we
3896 // do this the same was as 5.7 and previous, so it works correctly on
3897 // all systems. Specifically, the e.g. Delete and Arrow keys are
3898 // munged and won't work if e.g. searching for Hebrew text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 else if (c >= 'a' && c <= 'z')
3900#endif
3901 return (int)(map[CharOrdLow(c)] + p_aleph);
3902 else
3903 return c;
3904 }
3905 else
3906 {
3907 switch (c)
3908 {
3909 case '`': return ';';
3910 case '/': return '.';
3911 case '\'': return ',';
3912 case 'q': return '/';
3913 case 'w': return '\'';
3914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003915 // Hebrew letters - set offset from 'a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 case ',': c = '{'; break;
3917 case '.': c = 'v'; break;
3918 case ';': c = 't'; break;
3919 default: {
3920 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3921
3922#ifdef EBCDIC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003923 // see note about islower() above
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (!islower(c))
3925#else
3926 if (c < 'a' || c > 'z')
3927#endif
3928 return c;
3929 c = str[CharOrdLow(c)];
3930 break;
3931 }
3932 }
3933
3934 return (int)(CharOrdLow(c) + p_aleph);
3935 }
3936}
3937#endif
3938
3939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003940ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
3942 int need_redraw = FALSE;
3943 int regname;
3944 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003945 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
3947 /*
3948 * If we are going to wait for a character, show a '"'.
3949 */
3950 pc_status = PC_STATUS_UNSET;
3951 if (redrawing() && !char_avail())
3952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003953 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00003954 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
3956 edit_putchar('"', TRUE);
3957#ifdef FEAT_CMDL_INFO
3958 add_to_showcmd_c(Ctrl_R);
3959#endif
3960 }
3961
3962#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003963 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964#endif
3965
3966 /*
3967 * Don't map the register name. This also prevents the mode message to be
3968 * deleted when ESC is hit.
3969 */
3970 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003971 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003972 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3975 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003976 // Get a third key for literal register insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 literally = regname;
3978#ifdef FEAT_CMDL_INFO
3979 add_to_showcmd_c(literally);
3980#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003981 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003985 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
3987#ifdef FEAT_EVAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003988 // Don't call u_sync() while typing the expression or giving an error
3989 // message for it. Only call it explicitly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 ++no_u_sync;
3991 if (regname == '=')
3992 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003993 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003994# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003996# endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003997 // Sync undo when evaluating the expression calls setline() or
3998 // append(), so that it can be undone separately.
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003999 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02004000
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004002
4003 // Cursor may be moved back a column.
4004 curwin->w_cursor = curpos;
4005 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004006# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004007 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 if (im_on)
4009 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00004012 if (regname == NUL || !valid_yank_reg(regname, FALSE))
4013 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004014 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004015 need_redraw = TRUE; // remove the '"'
Bram Moolenaar677ee682005-01-27 14:41:15 +00004016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 else
4018 {
4019#endif
4020 if (literally == Ctrl_O || literally == Ctrl_P)
4021 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004022 // Append the command to the redo buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 AppendCharToRedobuff(Ctrl_R);
4024 AppendCharToRedobuff(literally);
4025 AppendCharToRedobuff(regname);
4026
4027 do_put(regname, BACKWARD, 1L,
4028 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
4029 }
4030 else if (insert_reg(regname, literally) == FAIL)
4031 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004032 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004033 need_redraw = TRUE; // remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004035 else if (stop_insert_mode)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004036 // When the '=' register was used and a function was invoked that
4037 // did ":stopinsert" then stuff_empty() returns FALSE but we won't
4038 // insert anything, need to remove the '"'
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004039 need_redraw = TRUE;
4040
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041#ifdef FEAT_EVAL
4042 }
4043 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004044 if (u_sync_once == 1)
4045 ins_need_undo = TRUE;
4046 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047#endif
4048#ifdef FEAT_CMDL_INFO
4049 clear_showcmd();
4050#endif
4051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004052 // If the inserted register is empty, we need to remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (need_redraw || stuff_empty())
4054 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004055
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004056 // Disallow starting Visual mode here, would get a weird mode.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004057 if (!vis_active && VIsual_active)
4058 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059}
4060
4061/*
4062 * CTRL-G commands in Insert mode.
4063 */
4064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004065ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066{
4067 int c;
4068
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004069 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 /*
4073 * Don't map the second key. This also prevents the mode message to be
4074 * deleted when ESC is hit.
4075 */
4076 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004077 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004078 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004080 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 switch (c)
4082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004083 // CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 case K_UP:
4085 case Ctrl_K:
4086 case 'k': ins_up(TRUE);
4087 break;
4088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004089 // CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 case K_DOWN:
4091 case Ctrl_J:
4092 case 'j': ins_down(TRUE);
4093 break;
4094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004095 // CTRL-G u: start new undoable edit
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004096 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004099 // Need to reset Insstart, esp. because a BS that joins
4100 // a line to the previous one must save for undo.
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004101 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004102 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 break;
4104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004105 // CTRL-G U: do not break undo with the next char
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004106 case 'U':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004107 // Allow one left/right cursor movement with the next char,
4108 // without breaking undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004109 dont_sync_undo = MAYBE;
4110 break;
4111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004112 // Unknown CTRL-G command, reserved for future expansion.
Bram Moolenaar165bc692015-07-21 17:53:25 +02004113 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115}
4116
4117/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004118 * CTRL-^ in Insert mode.
4119 */
4120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004121ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004123 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004125 // ":lmap" mappings exists, Toggle use of ":lmap" mappings.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004126 if (State & LANGMAP)
4127 {
4128 curbuf->b_p_iminsert = B_IMODE_NONE;
4129 State &= ~LANGMAP;
4130 }
4131 else
4132 {
4133 curbuf->b_p_iminsert = B_IMODE_LMAP;
4134 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004135#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004136 im_set_active(FALSE);
4137#endif
4138 }
4139 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004140#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 else
4142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004143 // There are no ":lmap" mappings, toggle IM
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144 if (im_get_status())
4145 {
4146 curbuf->b_p_iminsert = B_IMODE_NONE;
4147 im_set_active(FALSE);
4148 }
4149 else
4150 {
4151 curbuf->b_p_iminsert = B_IMODE_IM;
4152 State &= ~LANGMAP;
4153 im_set_active(TRUE);
4154 }
4155 }
4156#endif
4157 set_iminsert_global();
4158 showmode();
4159#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004160 // may show different cursor shape or color
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004161 if (gui.in_use)
4162 gui_update_cursor(TRUE, FALSE);
4163#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004164#if defined(FEAT_KEYMAP)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004165 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004166 status_redraw_curbuf();
4167#endif
4168}
4169
4170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 * Handle ESC in insert mode.
4172 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4173 * insert.
4174 */
4175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004176ins_esc(
4177 long *count,
4178 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004179 int nomove) // don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180{
4181 int temp;
4182 static int disabled_redraw = FALSE;
4183
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004184#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 check_spell_redraw();
4186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 temp = curwin->w_cursor.col;
4189 if (disabled_redraw)
4190 {
4191 --RedrawingDisabled;
4192 disabled_redraw = FALSE;
4193 }
4194 if (!arrow_used)
4195 {
4196 /*
4197 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004198 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4199 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 */
4201 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004202 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204 /*
4205 * Repeating insert may take a long time. Check for
4206 * interrupt now and then.
4207 */
4208 if (*count > 0)
4209 {
4210 line_breakcheck();
4211 if (got_int)
4212 *count = 0;
4213 }
4214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004215 if (--*count > 0) // repeat what was typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004217 // Vi repeats the insert without replacing characters.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004218 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4219 State &= ~REPLACE_FLAG;
4220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 (void)start_redo_ins();
4222 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004223 stuffRedoReadbuff(ESC_STR); // no ESC in redo buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 ++RedrawingDisabled;
4225 disabled_redraw = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004226 return FALSE; // repeat the insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004228 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 undisplay_dollar();
4230 }
4231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004232 // When an autoindent was removed, curswant stays after the
4233 // indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4235 curwin->w_set_curswant = TRUE;
4236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004237 // Remember the last Insert position in the '^ mark.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 if (!cmdmod.keepjumps)
4239 curbuf->b_last_insert = curwin->w_cursor;
4240
4241 /*
4242 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004243 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004245 if (!nomove
4246 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004247 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004248 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004249 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250#ifdef FEAT_RIGHTLEFT
4251 && !revins_on
4252#endif
4253 )
4254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4256 {
4257 oneleft();
4258 if (restart_edit != NUL)
4259 ++curwin->w_cursor.coladd;
4260 }
4261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
4263 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004264 // Correct cursor for multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if (has_mbyte)
4266 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268 }
4269
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004270#ifdef HAVE_INPUT_METHOD
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004271 // Disable IM to allow typing English directly for Normal mode commands.
4272 // When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4273 // well).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if (!(State & LANGMAP))
4275 im_save_status(&curbuf->b_p_iminsert);
4276 im_set_active(FALSE);
4277#endif
4278
4279 State = NORMAL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004280 // need to position cursor again (e.g. when on a TAB )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 changed_cline_bef_curs();
4282
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284#ifdef CURSOR_SHAPE
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004285 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004287 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004288 {
4289 // Re-enable bracketed paste mode.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004290 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004292 // Re-enable modifyOtherKeys.
4293 out_str(T_CTI);
4294 }
4295
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004296 // When recording or for CTRL-O, need to display the new mode.
4297 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004298 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004300 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004301 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004303 return TRUE; // exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304}
4305
4306#ifdef FEAT_RIGHTLEFT
4307/*
4308 * Toggle language: hkmap and revins_on.
4309 * Move to end of reverse inserted text.
4310 */
4311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004312ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
4314 if (revins_on && revins_chars && revins_scol >= 0)
4315 {
4316 while (gchar_cursor() != NUL && revins_chars--)
4317 ++curwin->w_cursor.col;
4318 }
4319 p_ri = !p_ri;
4320 revins_on = (State == INSERT && p_ri);
4321 if (revins_on)
4322 {
4323 revins_scol = curwin->w_cursor.col;
4324 revins_legal++;
4325 revins_chars = 0;
4326 undisplay_dollar();
4327 }
4328 else
4329 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004330 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 showmode();
4332}
4333#endif
4334
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335/*
4336 * If 'keymodel' contains "startsel", may start selection.
4337 * Returns TRUE when a CTRL-O and other keys stuffed.
4338 */
4339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004340ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
4342 if (km_startsel)
4343 switch (c)
4344 {
4345 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 case K_PAGEUP:
4348 case K_KPAGEUP:
4349 case K_PAGEDOWN:
4350 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004351# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 case K_LEFT:
4353 case K_RIGHT:
4354 case K_UP:
4355 case K_DOWN:
4356 case K_END:
4357 case K_HOME:
4358# endif
4359 if (!(mod_mask & MOD_MASK_SHIFT))
4360 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004361 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 case K_S_LEFT:
4363 case K_S_RIGHT:
4364 case K_S_UP:
4365 case K_S_DOWN:
4366 case K_S_END:
4367 case K_S_HOME:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // Start selection right away, the cursor can move with
4369 // CTRL-O when beyond the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 start_selection();
4371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 // Execute the key in (insert) Select mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 stuffcharReadbuff(Ctrl_O);
4374 if (mod_mask)
4375 {
4376 char_u buf[4];
4377
4378 buf[0] = K_SPECIAL;
4379 buf[1] = KS_MODIFIER;
4380 buf[2] = mod_mask;
4381 buf[3] = NUL;
4382 stuffReadbuff(buf);
4383 }
4384 stuffcharReadbuff(c);
4385 return TRUE;
4386 }
4387 return FALSE;
4388}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004391 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 */
4393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004394ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004395{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004396#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004397 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004398 (char_u *)((State & REPLACE_FLAG) ? "i"
4399 : replaceState == VREPLACE ? "v"
4400 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004401#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004402 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 if (State & REPLACE_FLAG)
4404 State = INSERT | (State & LANGMAP);
4405 else
4406 State = replaceState | (State & LANGMAP);
4407 AppendCharToRedobuff(K_INS);
4408 showmode();
4409#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004410 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004411#endif
4412}
4413
4414/*
4415 * Pressed CTRL-O in Insert mode.
4416 */
4417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004418ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004419{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 if (State & VREPLACE_FLAG)
4421 restart_edit = 'V';
4422 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423 if (State & REPLACE_FLAG)
4424 restart_edit = 'R';
4425 else
4426 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 if (virtual_active())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004428 ins_at_eol = FALSE; // cursor always keeps its column
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004429 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 ins_at_eol = (gchar_cursor() == NUL);
4431}
4432
4433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 * If the cursor is on an indent, ^T/^D insert/delete one
4435 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004436 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 * with vi. But vi only supports ^T and ^D after an
4438 * autoindent, we support it everywhere.
4439 */
4440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004441ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
4443 if (stop_arrow() == FAIL)
4444 return;
4445 AppendCharToRedobuff(c);
4446
4447 /*
4448 * 0^D and ^^D: remove all indent.
4449 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004450 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4451 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004454 (void)del_char(FALSE); // delete the '^' or '0'
4455 // In Replace mode, restore the characters that '^' or '0' replaced.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 if (State & REPLACE_FLAG)
4457 replace_pop_ins();
4458 if (lastc == '^')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004459 old_indent = get_indent(); // remember curr. indent
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004460 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004463 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
4465 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4466 did_ai = FALSE;
4467#ifdef FEAT_SMARTINDENT
4468 did_si = FALSE;
4469 can_si = FALSE;
4470 can_si_back = FALSE;
4471#endif
4472#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004473 can_cindent = FALSE; // no cindenting after ^D or ^T
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474#endif
4475}
4476
4477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004478ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479{
4480 int temp;
4481
4482 if (stop_arrow() == FAIL)
4483 return;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004484 if (gchar_cursor() == NUL) // delete newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 temp = curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004487 if (!can_bs(BS_EOL) // only if "eol" included
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004488 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004489 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 curwin->w_cursor.col = temp;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004493 // Adjust orig_line_count in case more lines have been deleted than
4494 // have been added. That makes sure, that open_line() later
4495 // can access all buffer lines correctly
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004496 if (State & VREPLACE_FLAG &&
4497 orig_line_count > curbuf->b_ml.ml_line_count)
4498 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004501 else if (del_char(FALSE) == FAIL) // delete char under cursor
Bram Moolenaar165bc692015-07-21 17:53:25 +02004502 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 did_ai = FALSE;
4504#ifdef FEAT_SMARTINDENT
4505 did_si = FALSE;
4506 can_si = FALSE;
4507 can_si_back = FALSE;
4508#endif
4509 AppendCharToRedobuff(K_DEL);
4510}
4511
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004512/*
4513 * Delete one character for ins_bs().
4514 */
4515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004516ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004517{
4518 dec_cursor();
4519 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4520 if (State & REPLACE_FLAG)
4521 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004522 // Don't delete characters before the insert point when in
4523 // Replace mode
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004524 if (curwin->w_cursor.lnum != Insstart.lnum
4525 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004526 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004527 }
4528 else
4529 (void)del_char(FALSE);
4530}
4531
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532/*
4533 * Handle Backspace, delete-word and delete-line in Insert mode.
4534 * Return TRUE when backspace was actually used.
4535 */
4536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004537ins_bs(
4538 int c,
4539 int mode,
4540 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541{
4542 linenr_T lnum;
4543 int cc;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004544 int temp = 0; // init for GCC
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004545 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 colnr_T mincol;
4547 int did_backspace = FALSE;
4548 int in_indent;
4549 int oldState;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 int cpc[MAX_MCO]; // composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 /*
4553 * can't delete anything in an empty file
4554 * can't backup past first character in buffer
4555 * can't backup past starting point unless 'backspace' > 1
4556 * can backup to a previous line if 'backspace' == 0
4557 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004558 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 || (
4560#ifdef FEAT_RIGHTLEFT
4561 !revins_on &&
4562#endif
4563 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4564 || (!can_bs(BS_START)
4565 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004566 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4567 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4569 && curwin->w_cursor.col <= ai_col)
4570 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4571 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004572 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 return FALSE;
4574 }
4575
4576 if (stop_arrow() == FAIL)
4577 return FALSE;
4578 in_indent = inindent(0);
4579#ifdef FEAT_CINDENT
4580 if (in_indent)
4581 can_cindent = FALSE;
4582#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004583 end_comment_pending = NUL; // After BS, don't auto-end comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004585 if (revins_on) // put cursor after last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 inc_cursor();
4587#endif
4588
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004589 // Virtualedit:
4590 // BACKSPACE_CHAR eats a virtual space
4591 // BACKSPACE_WORD eats all coladd
4592 // BACKSPACE_LINE eats all coladd and keeps going
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (curwin->w_cursor.coladd > 0)
4594 {
4595 if (mode == BACKSPACE_CHAR)
4596 {
4597 --curwin->w_cursor.coladd;
4598 return TRUE;
4599 }
4600 if (mode == BACKSPACE_WORD)
4601 {
4602 curwin->w_cursor.coladd = 0;
4603 return TRUE;
4604 }
4605 curwin->w_cursor.coladd = 0;
4606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
4608 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004609 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 */
4611 if (curwin->w_cursor.col == 0)
4612 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004613 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004614 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#ifdef FEAT_RIGHTLEFT
4616 || revins_on
4617#endif
4618 )
4619 {
4620 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4621 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4622 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004623 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004624 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 /*
4627 * In replace mode:
4628 * cc < 0: NL was inserted, delete it
4629 * cc >= 0: NL was replaced, put original characters back
4630 */
4631 cc = -1;
4632 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004633 cc = replace_pop(); // returns -1 if NL was inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 /*
4635 * In replace mode, in the line we started replacing, we only move the
4636 * cursor.
4637 */
4638 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4639 {
4640 dec_cursor();
4641 }
4642 else
4643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (!(State & VREPLACE_FLAG)
4645 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004647 temp = gchar_cursor(); // remember current char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004650 // When "aw" is in 'formatoptions' we must delete the space at
4651 // the end of the line, otherwise the line will be broken
4652 // again when auto-formatting.
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004653 if (has_format_option(FO_AUTO)
4654 && has_format_option(FO_WHITE_PAR))
4655 {
4656 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4657 TRUE);
4658 int len;
4659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004660 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004661 if (len > 0 && ptr[len - 1] == ' ')
4662 ptr[len - 1] = NUL;
4663 }
4664
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004665 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (temp == NUL && gchar_cursor() != NUL)
4667 inc_cursor();
4668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 else
4670 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 /*
4673 * In REPLACE mode we have to put back the text that was replaced
4674 * by the NL. On the replace stack is first a NUL-terminated
4675 * sequence of characters that were deleted and then the
4676 * characters that NL replaced.
4677 */
4678 if (State & REPLACE_FLAG)
4679 {
4680 /*
4681 * Do the next ins_char() in NORMAL state, to
4682 * prevent ins_char() from replacing characters and
4683 * avoiding showmatch().
4684 */
4685 oldState = State;
4686 State = NORMAL;
4687 /*
4688 * restore characters (blanks) deleted after cursor
4689 */
4690 while (cc > 0)
4691 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004692 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004694 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 cc = replace_pop();
4696 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004697 // restore the characters that NL replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 replace_pop_ins();
4699 State = oldState;
4700 }
4701 }
4702 did_ai = FALSE;
4703 }
4704 else
4705 {
4706 /*
4707 * Delete character(s) before the cursor.
4708 */
4709#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004710 if (revins_on) // put cursor on last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 dec_cursor();
4712#endif
4713 mincol = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004714 // keep indent
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004715 if (mode == BACKSPACE_LINE
4716 && (curbuf->b_p_ai
4717#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004718 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004719#endif
4720 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#ifdef FEAT_RIGHTLEFT
4722 && !revins_on
4723#endif
4724 )
4725 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004726 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004728 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004730 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732
4733 /*
4734 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4735 */
4736 if ( mode == BACKSPACE_CHAR
4737 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004738 || ((get_sts_value() != 0
4739#ifdef FEAT_VARTABS
4740 || tabstop_count(curbuf->b_p_vsts_array)
4741#endif
4742 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004743 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 && (*(ml_get_cursor() - 1) == TAB
4745 || (*(ml_get_cursor() - 1) == ' '
4746 && (!*inserted_space_p
4747 || arrow_used))))))
4748 {
4749 int ts;
4750 colnr_T vcol;
4751 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004752 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753
4754 *inserted_space_p = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004755 // Compute the virtual column where we want to be. Since
4756 // 'showbreak' may get in the way, need to get the last column of
4757 // the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004759 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 dec_cursor();
4761 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4762 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004763#ifdef FEAT_VARTABS
4764 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004765 {
4766 ts = (int)get_sw_value(curbuf);
4767 want_vcol = (want_vcol / ts) * ts;
4768 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004769 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004770 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004771 curbuf->b_p_vsts_array);
4772#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004773 if (p_sta && in_indent)
4774 ts = (int)get_sw_value(curbuf);
4775 else
4776 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 // delete characters until we are at or before want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004782 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004783 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 // insert extra spaces until we are at want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 while (vcol < want_vcol)
4787 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004788 // Remember the first char we inserted
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004789 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4790 && curwin->w_cursor.col < Insstart_orig.col)
4791 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if (State & VREPLACE_FLAG)
4794 ins_char(' ');
4795 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
4797 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004798 if ((State & REPLACE_FLAG))
4799 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
4801 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4802 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004804 // If we are now back where we started delete one character. Can
4805 // happen when using 'sts' and 'linebreak'.
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004806 if (vcol >= start_vcol)
4807 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809
4810 /*
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004811 * Delete up to starting point, start of line or previous word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004815 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004817 if (has_mbyte)
4818 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004819 do
4820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 if (!revins_on) // put cursor on char to be deleted
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004825
4826 cc = gchar_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004827 // look multi-byte character class
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004828 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004830 prev_cclass = cclass;
4831 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004834 // start of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004835 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4836 {
4837 mode = BACKSPACE_WORD_NOT_SPACE;
4838 temp = vim_iswordc(cc);
4839 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004840 // end of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004841 else if (mode == BACKSPACE_WORD_NOT_SPACE
4842 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004843 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004846 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004848 inc_cursor();
4849#ifdef FEAT_RIGHTLEFT
4850 else if (State & REPLACE_FLAG)
4851 dec_cursor();
4852#endif
4853 break;
4854 }
4855 if (State & REPLACE_FLAG)
4856 replace_do_bs(-1);
4857 else
4858 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004859 if (enc_utf8 && p_deco)
4860 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004861 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004862 /*
4863 * If there are combining characters and 'delcombine' is set
4864 * move the cursor back. Don't back up before the base
4865 * character.
4866 */
4867 if (enc_utf8 && p_deco && cpc[0] != NUL)
4868 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004869#ifdef FEAT_RIGHTLEFT
4870 if (revins_chars)
4871 {
4872 revins_chars--;
4873 revins_legal++;
4874 }
4875 if (revins_on && gchar_cursor() == NUL)
4876 break;
4877#endif
4878 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004879 // Just a single backspace?:
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004880 if (mode == BACKSPACE_CHAR)
4881 break;
4882 } while (
4883#ifdef FEAT_RIGHTLEFT
4884 revins_on ||
4885#endif
4886 (curwin->w_cursor.col > mincol
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02004887 && (can_bs(BS_NOSTOP)
4888 || (curwin->w_cursor.lnum != Insstart_orig.lnum
4889 || curwin->w_cursor.col != Insstart_orig.col)
4890 )));
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 did_backspace = TRUE;
4893 }
4894#ifdef FEAT_SMARTINDENT
4895 did_si = FALSE;
4896 can_si = FALSE;
4897 can_si_back = FALSE;
4898#endif
4899 if (curwin->w_cursor.col <= 1)
4900 did_ai = FALSE;
4901 /*
4902 * It's a little strange to put backspaces into the redo
4903 * buffer, but it makes auto-indent a lot easier to deal
4904 * with.
4905 */
4906 AppendCharToRedobuff(c);
4907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004908 // If deleted before the insertion point, adjust it
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004909 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004910 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004911 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004913 // vi behaviour: the cursor moves backward but the character that
4914 // was there remains visible
4915 // Vim behaviour: the cursor moves backward and the character that
4916 // was there is erased from the screen.
4917 // We can emulate the vi behaviour by pretending there is a dollar
4918 // displayed even when there isn't.
4919 // --pkv Sun Jan 19 01:56:40 EST 2003
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004920 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 dollar_vcol = curwin->w_virtcol;
4922
Bram Moolenaarce3be472008-01-14 19:12:28 +00004923#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004924 // When deleting a char the cursor line must never be in a closed fold.
4925 // E.g., when 'foldmethod' is indent and deleting the first non-white
4926 // char before a Tab.
Bram Moolenaarce3be472008-01-14 19:12:28 +00004927 if (did_backspace)
4928 foldOpenCursor();
4929#endif
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 return did_backspace;
4932}
4933
Bram Moolenaarec2da362017-01-21 20:04:22 +01004934/*
4935 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4936 * P_PE literally.
4937 * When "drop" is TRUE then consume the text and drop it.
4938 */
4939 int
4940bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4941{
4942 int c;
4943 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4944 int idx = 0;
4945 char_u *end = find_termcode((char_u *)"PE");
4946 int ret_char = -1;
4947 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004948 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 // If the end code is too long we can't detect it, read everything.
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01004951 if (end != NULL && STRLEN(end) >= NUMBUFLEN)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004952 end = NULL;
4953 ++no_mapping;
4954 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004955 if (!p_paste)
4956 // Also have the side effects of setting 'paste' to make it work much
4957 // faster.
4958 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004959
Bram Moolenaarec2da362017-01-21 20:04:22 +01004960 for (;;)
4961 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004962 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004963 if (end == NULL && vpeekc() == NUL)
4964 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004965 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004966 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004967 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004968 if (c == NUL || got_int || (ex_normal_busy > 0 && c == Ctrl_C))
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004969 // When CTRL-C was encountered the typeahead will be flushed and we
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004970 // won't get the end sequence. Except when using ":normal".
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004971 break;
4972
Bram Moolenaarec2da362017-01-21 20:04:22 +01004973 if (has_mbyte)
4974 idx += (*mb_char2bytes)(c, buf + idx);
4975 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004976 buf[idx++] = c;
4977 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004978 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004979 {
4980 if (end[idx] == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004981 break; // Found the end of paste code.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004982 continue;
4983 }
4984 if (!drop)
4985 {
4986 switch (mode)
4987 {
4988 case PASTE_CMDLINE:
4989 put_on_cmdline(buf, idx, TRUE);
4990 break;
4991
4992 case PASTE_EX:
4993 if (gap != NULL && ga_grow(gap, idx) == OK)
4994 {
4995 mch_memmove((char *)gap->ga_data + gap->ga_len,
4996 buf, (size_t)idx);
4997 gap->ga_len += idx;
4998 }
4999 break;
5000
5001 case PASTE_INSERT:
5002 if (stop_arrow() == OK)
5003 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005004 c = buf[0];
5005 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
5006 ins_eol(c);
5007 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01005008 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005009 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01005010 AppendToRedobuffLit(buf, idx);
5011 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01005012 }
5013 break;
5014
5015 case PASTE_ONE_CHAR:
5016 if (ret_char == -1)
5017 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01005018 if (has_mbyte)
5019 ret_char = (*mb_ptr2char)(buf);
5020 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005021 ret_char = buf[0];
5022 }
5023 break;
5024 }
5025 }
5026 idx = 0;
5027 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005028
Bram Moolenaarec2da362017-01-21 20:04:22 +01005029 --no_mapping;
5030 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005031 if (!save_paste)
5032 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01005033
5034 return ret_char;
5035}
5036
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005037#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00005038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005039ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005040{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 // We will be leaving the current window, unless closing another tab.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005042 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
5043 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
5044 {
5045 undisplay_dollar();
5046 start_arrow(&curwin->w_cursor);
5047# ifdef FEAT_CINDENT
5048 can_cindent = TRUE;
5049# endif
5050 }
5051
5052 if (c == K_TABLINE)
5053 goto_tabpage(current_tab);
5054 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005055 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005056 handle_tabmenu();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 redraw_statuslines(); // will redraw the tabline when needed
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005058 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005059}
5060#endif
5061
5062#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005064ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 pos_T tpos;
5067
5068 undisplay_dollar();
5069 tpos = curwin->w_cursor;
5070 if (gui_do_scroll())
5071 {
5072 start_arrow(&tpos);
5073# ifdef FEAT_CINDENT
5074 can_cindent = TRUE;
5075# endif
5076 }
5077}
5078
5079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005080ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
5082 pos_T tpos;
5083
5084 undisplay_dollar();
5085 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005086 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 start_arrow(&tpos);
5089# ifdef FEAT_CINDENT
5090 can_cindent = TRUE;
5091# endif
5092 }
5093}
5094#endif
5095
5096 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005097ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
5099 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005100 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
5102#ifdef FEAT_FOLDING
5103 if ((fdo_flags & FDO_HOR) && KeyTyped)
5104 foldOpenCursor();
5105#endif
5106 undisplay_dollar();
5107 tpos = curwin->w_cursor;
5108 if (oneleft() == OK)
5109 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005110#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005111 // Only call start_arrow() when not busy with preediting, it will
5112 // break undo. K_LEFT is inserted in im_correct_cursor().
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005113 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005114#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005115 {
5116 start_arrow_with_change(&tpos, end_change);
5117 if (!end_change)
5118 AppendCharToRedobuff(K_LEFT);
5119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005121 // If exit reversed string, position is fixed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5123 revins_legal++;
5124 revins_chars++;
5125#endif
5126 }
5127
5128 /*
5129 * if 'whichwrap' set for cursor in insert mode may go to
5130 * previous line
5131 */
5132 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005134 // always break undo when moving upwards/downwards, else undo may break
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 start_arrow(&tpos);
5136 --(curwin->w_cursor.lnum);
5137 coladvance((colnr_T)MAXCOL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005138 curwin->w_set_curswant = TRUE; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005141 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005142 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143}
5144
5145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005146ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147{
5148 pos_T tpos;
5149
5150#ifdef FEAT_FOLDING
5151 if ((fdo_flags & FDO_HOR) && KeyTyped)
5152 foldOpenCursor();
5153#endif
5154 undisplay_dollar();
5155 tpos = curwin->w_cursor;
5156 if (c == K_C_HOME)
5157 curwin->w_cursor.lnum = 1;
5158 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 curwin->w_curswant = 0;
5161 start_arrow(&tpos);
5162}
5163
5164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005165ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 pos_T tpos;
5168
5169#ifdef FEAT_FOLDING
5170 if ((fdo_flags & FDO_HOR) && KeyTyped)
5171 foldOpenCursor();
5172#endif
5173 undisplay_dollar();
5174 tpos = curwin->w_cursor;
5175 if (c == K_C_END)
5176 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5177 coladvance((colnr_T)MAXCOL);
5178 curwin->w_curswant = MAXCOL;
5179
5180 start_arrow(&tpos);
5181}
5182
5183 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005184ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005186 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187#ifdef FEAT_FOLDING
5188 if ((fdo_flags & FDO_HOR) && KeyTyped)
5189 foldOpenCursor();
5190#endif
5191 undisplay_dollar();
5192 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5193 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005194 start_arrow_with_change(&curwin->w_cursor, end_change);
5195 if (!end_change)
5196 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 (void)bck_word(1L, FALSE, FALSE);
5198 curwin->w_set_curswant = TRUE;
5199 }
5200 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005201 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005202 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203}
5204
5205 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005206ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005208 int end_change = dont_sync_undo == FALSE; // end undoable change
5209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#ifdef FEAT_FOLDING
5211 if ((fdo_flags & FDO_HOR) && KeyTyped)
5212 foldOpenCursor();
5213#endif
5214 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005215 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005217 start_arrow_with_change(&curwin->w_cursor, end_change);
5218 if (!end_change)
5219 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 if (virtual_active())
5222 oneright();
5223 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005226 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 ++curwin->w_cursor.col;
5229 }
5230
5231#ifdef FEAT_RIGHTLEFT
5232 revins_legal++;
5233 if (revins_chars)
5234 revins_chars--;
5235#endif
5236 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 // if 'whichwrap' set for cursor in insert mode, may move the
5238 // cursor to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 else if (vim_strchr(p_ww, ']') != NULL
5240 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5241 {
5242 start_arrow(&curwin->w_cursor);
5243 curwin->w_set_curswant = TRUE;
5244 ++curwin->w_cursor.lnum;
5245 curwin->w_cursor.col = 0;
5246 }
5247 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005248 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005249 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250}
5251
5252 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005253ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005255 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#ifdef FEAT_FOLDING
5257 if ((fdo_flags & FDO_HOR) && KeyTyped)
5258 foldOpenCursor();
5259#endif
5260 undisplay_dollar();
5261 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5262 || gchar_cursor() != NUL)
5263 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005264 start_arrow_with_change(&curwin->w_cursor, end_change);
5265 if (!end_change)
5266 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 (void)fwd_word(1L, FALSE, 0);
5268 curwin->w_set_curswant = TRUE;
5269 }
5270 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005271 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005272 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273}
5274
5275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005276ins_up(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005277 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
5279 pos_T tpos;
5280 linenr_T old_topline = curwin->w_topline;
5281#ifdef FEAT_DIFF
5282 int old_topfill = curwin->w_topfill;
5283#endif
5284
5285 undisplay_dollar();
5286 tpos = curwin->w_cursor;
5287 if (cursor_up(1L, TRUE) == OK)
5288 {
5289 if (startcol)
5290 coladvance(getvcol_nolist(&Insstart));
5291 if (old_topline != curwin->w_topline
5292#ifdef FEAT_DIFF
5293 || old_topfill != curwin->w_topfill
5294#endif
5295 )
5296 redraw_later(VALID);
5297 start_arrow(&tpos);
5298#ifdef FEAT_CINDENT
5299 can_cindent = TRUE;
5300#endif
5301 }
5302 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005303 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304}
5305
5306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005307ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309 pos_T tpos;
5310
5311 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005312
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005313 if (mod_mask & MOD_MASK_CTRL)
5314 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005315 // <C-PageUp>: tab page back
Bram Moolenaarbc444822006-10-17 11:37:50 +00005316 if (first_tabpage->tp_next != NULL)
5317 {
5318 start_arrow(&curwin->w_cursor);
5319 goto_tabpage(-1);
5320 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005321 return;
5322 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 tpos = curwin->w_cursor;
5325 if (onepage(BACKWARD, 1L) == OK)
5326 {
5327 start_arrow(&tpos);
5328#ifdef FEAT_CINDENT
5329 can_cindent = TRUE;
5330#endif
5331 }
5332 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005333 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334}
5335
5336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005337ins_down(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
5340 pos_T tpos;
5341 linenr_T old_topline = curwin->w_topline;
5342#ifdef FEAT_DIFF
5343 int old_topfill = curwin->w_topfill;
5344#endif
5345
5346 undisplay_dollar();
5347 tpos = curwin->w_cursor;
5348 if (cursor_down(1L, TRUE) == OK)
5349 {
5350 if (startcol)
5351 coladvance(getvcol_nolist(&Insstart));
5352 if (old_topline != curwin->w_topline
5353#ifdef FEAT_DIFF
5354 || old_topfill != curwin->w_topfill
5355#endif
5356 )
5357 redraw_later(VALID);
5358 start_arrow(&tpos);
5359#ifdef FEAT_CINDENT
5360 can_cindent = TRUE;
5361#endif
5362 }
5363 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005364 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365}
5366
5367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005368ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370 pos_T tpos;
5371
5372 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005373
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005374 if (mod_mask & MOD_MASK_CTRL)
5375 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005376 // <C-PageDown>: tab page forward
Bram Moolenaarbc444822006-10-17 11:37:50 +00005377 if (first_tabpage->tp_next != NULL)
5378 {
5379 start_arrow(&curwin->w_cursor);
5380 goto_tabpage(0);
5381 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005382 return;
5383 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005384
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 tpos = curwin->w_cursor;
5386 if (onepage(FORWARD, 1L) == OK)
5387 {
5388 start_arrow(&tpos);
5389#ifdef FEAT_CINDENT
5390 can_cindent = TRUE;
5391#endif
5392 }
5393 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005394 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395}
5396
5397#ifdef FEAT_DND
5398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005399ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5402}
5403#endif
5404
5405/*
5406 * Handle TAB in Insert or Replace mode.
5407 * Return TRUE when the TAB needs to be inserted like a normal character.
5408 */
5409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005410ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 int ind;
5413 int i;
5414 int temp;
5415
5416 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5417 Insstart_blank_vcol = get_nolist_virtcol();
5418 if (echeck_abbr(TAB + ABBR_OFF))
5419 return FALSE;
5420
5421 ind = inindent(0);
5422#ifdef FEAT_CINDENT
5423 if (ind)
5424 can_cindent = FALSE;
5425#endif
5426
5427 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005428 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 */
5430 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005431#ifdef FEAT_VARTABS
5432 && !(p_sta && ind
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005433 // These five lines mean 'tabstop' != 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005434 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5435 || (tabstop_count(curbuf->b_p_vts_array) == 1
5436 && tabstop_first(curbuf->b_p_vts_array)
5437 != get_sw_value(curbuf))
5438 || (tabstop_count(curbuf->b_p_vts_array) == 0
5439 && curbuf->b_p_ts != get_sw_value(curbuf))))
5440 && tabstop_count(curbuf->b_p_vsts_array) == 0
5441#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005442 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005443#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005444 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 return TRUE;
5446
5447 if (stop_arrow() == FAIL)
5448 return TRUE;
5449
5450 did_ai = FALSE;
5451#ifdef FEAT_SMARTINDENT
5452 did_si = FALSE;
5453 can_si = FALSE;
5454 can_si_back = FALSE;
5455#endif
5456 AppendToRedobuff((char_u *)"\t");
5457
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005458#ifdef FEAT_VARTABS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005460 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005461 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005462 temp -= get_nolist_virtcol() % temp;
5463 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005464 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 // use 'softtabstop' when set
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005466 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005467 curbuf->b_p_vsts_array);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005468 else // otherwise use 'tabstop'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005469 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5470 curbuf->b_p_vts_array);
5471#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005472 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005473 temp = (int)get_sw_value(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005474 else if (curbuf->b_p_sts != 0) // use 'softtabstop' when set
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005475 temp = (int)get_sts_value();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005476 else // otherwise use 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 temp = (int)curbuf->b_p_ts;
5478 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
5481 /*
5482 * Insert the first space with ins_char(). It will delete one char in
5483 * replace mode. Insert the rest with ins_str(); it will not delete any
5484 * chars. For VREPLACE mode, we use ins_char() for all characters.
5485 */
5486 ins_char(' ');
5487 while (--temp > 0)
5488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 if (State & VREPLACE_FLAG)
5490 ins_char(' ');
5491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {
5493 ins_str((char_u *)" ");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005494 if (State & REPLACE_FLAG) // no char replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 replace_push(NUL);
5496 }
5497 }
5498
5499 /*
5500 * When 'expandtab' not set: Replace spaces by TABs where possible.
5501 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005502#ifdef FEAT_VARTABS
5503 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5504 || get_sts_value() > 0
5505 || (p_sta && ind)))
5506#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005507 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
5510 char_u *ptr;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005511 char_u *saved_line = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 pos_T fpos;
5514 pos_T *cursor;
5515 colnr_T want_vcol, vcol;
5516 int change_col = -1;
5517 int save_list = curwin->w_p_list;
5518
5519 /*
5520 * Get the current line. For VREPLACE mode, don't make real changes
5521 * yet, just work on a copy of the line.
5522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 if (State & VREPLACE_FLAG)
5524 {
5525 pos = curwin->w_cursor;
5526 cursor = &pos;
5527 saved_line = vim_strsave(ml_get_curline());
5528 if (saved_line == NULL)
5529 return FALSE;
5530 ptr = saved_line + pos.col;
5531 }
5532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
5534 ptr = ml_get_cursor();
5535 cursor = &curwin->w_cursor;
5536 }
5537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005538 // When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5540 curwin->w_p_list = FALSE;
5541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005542 // Find first white before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005544 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 {
5546 --fpos.col;
5547 --ptr;
5548 }
5549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // In Replace mode, don't change characters before the insert point.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 if ((State & REPLACE_FLAG)
5552 && fpos.lnum == Insstart.lnum
5553 && fpos.col < Insstart.col)
5554 {
5555 ptr += Insstart.col - fpos.col;
5556 fpos.col = Insstart.col;
5557 }
5558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005559 // compute virtual column numbers of first white and cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5561 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005563 // Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5564 // and 'linebreak' adding extra virtual columns.
Bram Moolenaar1c465442017-03-12 20:10:05 +01005565 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005567 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 if (vcol + i > want_vcol)
5569 break;
5570 if (*ptr != TAB)
5571 {
5572 *ptr = TAB;
5573 if (change_col < 0)
5574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005575 change_col = fpos.col; // Column of first change
5576 // May have to adjust Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5578 Insstart.col = fpos.col;
5579 }
5580 }
5581 ++fpos.col;
5582 ++ptr;
5583 vcol += i;
5584 }
5585
5586 if (change_col >= 0)
5587 {
5588 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005589 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005591 // Skip over the spaces we need.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 while (vcol < want_vcol && *ptr == ' ')
5593 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005594 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 ++ptr;
5596 ++repl_off;
5597 }
5598 if (vcol > want_vcol)
5599 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005600 // Must have a char with 'showbreak' just before it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 --ptr;
5602 --repl_off;
5603 }
5604 fpos.col += repl_off;
5605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005606 // Delete following spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 i = cursor->col - fpos.col;
5608 if (i > 0)
5609 {
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005610#ifdef FEAT_PROP_POPUP
5611 if (!(State & VREPLACE_FLAG))
5612 {
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005613 char_u *newp;
5614 int col;
5615
5616 newp = alloc(curbuf->b_ml.ml_line_len - i);
5617 if (newp == NULL)
5618 return FALSE;
5619
5620 col = ptr - curbuf->b_ml.ml_line_ptr;
5621 if (col > 0)
5622 mch_memmove(newp, ptr - col, col);
5623 mch_memmove(newp + col, ptr + i,
5624 curbuf->b_ml.ml_line_len - col - i);
5625
5626 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
5627 vim_free(curbuf->b_ml.ml_line_ptr);
5628 curbuf->b_ml.ml_line_ptr = newp;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005629 curbuf->b_ml.ml_line_len -= i;
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005630 curbuf->b_ml.ml_flags =
5631 (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005632 }
5633 else
5634#endif
5635 STRMOVE(ptr, ptr + i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005636 // correct replace stack.
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005637 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 for (temp = i; --temp >= 0; )
5639 replace_join(repl_off);
5640 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005641#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005642 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005643 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005644 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005645 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5646 (char_u *)"\t", 1);
5647 }
5648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 cursor->col -= i;
5650
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 /*
5652 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5653 * backspacing over the changed spacing and then inserting the new
5654 * spacing.
5655 */
5656 if (State & VREPLACE_FLAG)
5657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005658 // Backspace from real cursor to change_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 backspace_until_column(change_col);
5660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005661 // Insert each char in saved_line from changed_col to
5662 // ptr-cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 ins_bytes_len(saved_line + change_col,
5664 cursor->col - change_col);
5665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
5667
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 if (State & VREPLACE_FLAG)
5669 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 curwin->w_p_list = save_list;
5671 }
5672
5673 return FALSE;
5674}
5675
5676/*
5677 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005678 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005680 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005681ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 int i;
5684
5685 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005686 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005688 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 undisplay_dollar();
5690
5691 /*
5692 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5693 * character under the cursor. Only push a NUL on the replace stack,
5694 * nothing to put back when the NL is deleted.
5695 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005696 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 replace_push(NUL);
5698
5699 /*
5700 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5701 * replacing the next line, so we push all of the characters left on the
5702 * line onto the replace stack. This is not done here though, it is done
5703 * in open_line().
5704 */
5705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005706 // Put cursor on NUL if on the last char and coladd is 1 (happens after
5707 // CTRL-O).
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005708 if (virtual_active() && curwin->w_cursor.coladd > 0)
5709 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005712 // NL in reverse insert will always start in the end of
5713 // current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 if (revins_on)
5715 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5716#endif
5717
5718 AppendToRedobuff(NL_STR);
5719 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005720 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 old_indent = 0;
5722#ifdef FEAT_CINDENT
5723 can_cindent = TRUE;
5724#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005725#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005726 // When inserting a line the cursor line must never be in a closed fold.
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005727 foldOpenCursor();
5728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005730 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731}
5732
5733#ifdef FEAT_DIGRAPHS
5734/*
5735 * Handle digraph in insert mode.
5736 * Returns character still to be inserted, or NUL when nothing remaining to be
5737 * done.
5738 */
5739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005740ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 int c;
5743 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005744 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
5746 pc_status = PC_STATUS_UNSET;
5747 if (redrawing() && !char_avail())
5748 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005749 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005750 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
5752 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005753 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754#ifdef FEAT_CMDL_INFO
5755 add_to_showcmd_c(Ctrl_K);
5756#endif
5757 }
5758
5759#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005760 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761#endif
5762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005763 // don't map the digraph chars. This also prevents the
5764 // mode message to be deleted when ESC is hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 ++no_mapping;
5766 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005767 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 --no_mapping;
5769 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005770 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005771 // when the line fits in 'columns' the '?' is at the start of the next
5772 // line and will not be removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005773 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005775 if (IS_SPECIAL(c) || mod_mask) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 {
5777#ifdef FEAT_CMDL_INFO
5778 clear_showcmd();
5779#endif
5780 insert_special(c, TRUE, FALSE);
5781 return NUL;
5782 }
5783 if (c != ESC)
5784 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005785 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 if (redrawing() && !char_avail())
5787 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005788 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005789 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
5791 if (char2cells(c) == 1)
5792 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005793 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005795 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
5797#ifdef FEAT_CMDL_INFO
5798 add_to_showcmd_c(c);
5799#endif
5800 }
5801 ++no_mapping;
5802 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005803 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 --no_mapping;
5805 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005806 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005807 // when the line fits in 'columns' the '?' is at the start of the
5808 // next line and will not be removed by a redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005809 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 if (cc != ESC)
5811 {
5812 AppendToRedobuff((char_u *)CTRL_V_STR);
5813 c = getdigraph(c, cc, TRUE);
5814#ifdef FEAT_CMDL_INFO
5815 clear_showcmd();
5816#endif
5817 return c;
5818 }
5819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820#ifdef FEAT_CMDL_INFO
5821 clear_showcmd();
5822#endif
5823 return NUL;
5824}
5825#endif
5826
5827/*
5828 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5829 * Returns the char to be inserted, or NUL if none found.
5830 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005831 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005832ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
5834 int c;
5835 int temp;
5836 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005837 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
5839 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5840 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005841 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 return NUL;
5843 }
5844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005845 // try to advance to the cursor column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005847 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 prev_ptr = ptr;
5849 validate_virtcol();
5850 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5851 {
5852 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005853 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 }
5855 if ((colnr_T)temp > curwin->w_virtcol)
5856 ptr = prev_ptr;
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005860 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 return c;
5862}
5863
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005864/*
5865 * CTRL-Y or CTRL-E typed in Insert mode.
5866 */
5867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005868ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005869{
5870 int c = tc;
5871
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005872 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005873 {
5874 if (c == Ctrl_Y)
5875 scrolldown_clamp();
5876 else
5877 scrollup_clamp();
5878 redraw_later(VALID);
5879 }
5880 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005881 {
5882 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5883 if (c != NUL)
5884 {
5885 long tw_save;
5886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005887 // The character must be taken literally, insert like it
5888 // was typed after a CTRL-V, and pretend 'textwidth'
5889 // wasn't set. Digits, 'o' and 'x' are special after a
5890 // CTRL-V, don't use it for these.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005891 if (c < 256 && !isalnum(c))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005892 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005893 tw_save = curbuf->b_p_tw;
5894 curbuf->b_p_tw = -1;
5895 insert_special(c, TRUE, FALSE);
5896 curbuf->b_p_tw = tw_save;
5897#ifdef FEAT_RIGHTLEFT
5898 revins_chars++;
5899 revins_legal++;
5900#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005901 c = Ctrl_V; // pretend CTRL-V is last character
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005902 auto_format(FALSE, TRUE);
5903 }
5904 }
5905 return c;
5906}
5907
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908/*
5909 * Get the value that w_virtcol would have when 'list' is off.
5910 * Unless 'cpo' contains the 'L' flag.
5911 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005912 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005913get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005915 // check validity of cursor in current buffer
5916 if (curwin->w_buffer == NULL
5917 || curwin->w_buffer->b_ml.ml_mfp == NULL
5918 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5919 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5921 return getvcol_nolist(&curwin->w_cursor);
5922 validate_virtcol();
5923 return curwin->w_virtcol;
5924}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005925
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005926#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005927/*
5928 * Handle the InsertCharPre autocommand.
5929 * "c" is the character that was typed.
5930 * Return a pointer to allocated memory with the replacement string.
5931 * Return NULL to continue inserting "c".
5932 */
5933 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005934do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005935{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005936 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005937 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005938 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005939
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005940 // Return quickly when there is nothing to do.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005941 if (!has_insertcharpre())
5942 return NULL;
5943
Bram Moolenaar704984a2012-06-01 14:57:51 +02005944 if (has_mbyte)
5945 buf[(*mb_char2bytes)(c, buf)] = NUL;
5946 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005947 {
5948 buf[0] = c;
5949 buf[1] = NUL;
5950 }
5951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005952 // Lock the text to avoid weird things from happening.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005953 ++textlock;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005954 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005955
Bram Moolenaar704984a2012-06-01 14:57:51 +02005956 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005957 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005959 // Get the value of v:char. It may be empty or more than one
5960 // character. Only use it when changed, otherwise continue with the
5961 // original character to avoid breaking autoindent.
Bram Moolenaar704984a2012-06-01 14:57:51 +02005962 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5963 res = vim_strsave(get_vim_var_str(VV_CHAR));
5964 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005966 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005967 --textlock;
5968
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005969 // Restore the State, it may have been changed.
5970 State = save_State;
5971
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005972 return res;
5973}
5974#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005975
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005976#if defined(FEAT_CINDENT) || defined(PROTO)
5977 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005978get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005979{
5980 return can_cindent;
5981}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005982
5983 void
5984set_can_cindent(int val)
5985{
5986 can_cindent = val;
5987}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005988#endif
5989
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005990/*
5991 * Trigger "event" and take care of fixing undo.
5992 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005993 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005994ins_apply_autocmds(event_T event)
5995{
5996 varnumber_T tick = CHANGEDTICK(curbuf);
5997 int r;
5998
5999 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
6000
6001 // If u_savesub() was called then we are not prepared to start
6002 // a new line. Call u_save() with no contents to fix that.
6003 if (tick != CHANGEDTICK(curbuf))
6004 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
6005
6006 return r;
6007}