blob: 05518ceab8490ec3d581200205557c7452523873 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#define BACKSPACE_CHAR 1
17#define BACKSPACE_WORD 2
18#define BACKSPACE_WORD_NOT_SPACE 3
19#define BACKSPACE_LINE 4
20
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010021// Set when doing something for completion that may call edit() recursively,
22// which is not allowed.
Bram Moolenaar7591bb32019-03-30 13:53:47 +010023static int compl_busy = FALSE;
Bram Moolenaar7591bb32019-03-30 13:53:47 +010024
25
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010026static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +020027#ifdef FEAT_JOB_CHANNEL
28static void init_prompt(int cmdchar_todo);
29#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static void insert_special(int, int, int);
31static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
32static void check_auto_format(int);
33static void redo_literal(int c);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010034static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +000035#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void check_spell_redraw(void);
Bram Moolenaar217ad922005-03-20 22:37:15 +000037#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
39static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010040static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010041static void replace_flush(void);
42static void replace_do_bs(int limit_col);
43static int del_char_after_col(int limit_col);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static void ins_reg(void);
45static void ins_ctrl_g(void);
46static void ins_ctrl_hat(void);
47static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010049static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010051static int ins_start_select(int c);
52static void ins_insert(int replaceState);
53static void ins_ctrl_o(void);
54static void ins_shift(int c, int lastc);
55static void ins_del(void);
56static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000057#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010058static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +000059#endif
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010060static void ins_left(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010061static void ins_home(int c);
62static void ins_end(int c);
63static void ins_s_left(void);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +010064static void ins_right(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010065static void ins_s_right(void);
66static void ins_up(int startcol);
67static void ins_pageup(void);
68static void ins_down(int startcol);
69static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010073static int ins_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010075static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010077static int ins_ctrl_ey(int tc);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010078#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +010080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010082static colnr_T Insstart_textlen; // length of line when insert started
83static colnr_T Insstart_blank_vcol; // vcol for first inserted blank
84static int update_Insstart_orig = TRUE; // set Insstart_orig to Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010086static char_u *last_insert = NULL; // the text of the previous insert,
87 // K_SPECIAL and CSI are escaped
88static int last_insert_skip; // nr of chars in front of previous insert
89static int new_insert_skip; // nr of chars in front of current insert
90static int did_restart_edit; // "restart_edit" when calling edit()
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010093static int can_cindent; // may do cindenting on this line
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010097static int revins_on; // reverse insert mode on
98static int revins_chars; // how much to skip after edit
99static int revins_legal; // was the last char 'legal'?
100static int revins_scol; // start column of revins session
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101#endif
102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100103static int ins_need_undo; // call u_save() before inserting a
104 // char. Set when edit() is called.
105 // after that arrow_used is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
Bram Moolenaar75bf3d22019-03-26 22:46:05 +0100107static int did_add_space = FALSE; // auto_format() added an extra space
108 // under the cursor
109static int dont_sync_undo = FALSE; // CTRL-G U prevents syncing undo for
110 // the next left/right cursor key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
112/*
113 * edit(): Start inserting text.
114 *
115 * "cmdchar" can be:
116 * 'i' normal insert command
117 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100118 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 * 'R' replace command
120 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
121 * but still only one <CR> is inserted. The <Esc> is not used for redo.
122 * 'g' "gI" command.
123 * 'V' "gR" command for Virtual Replace mode.
124 * 'v' "gr" command for single character Virtual Replace mode.
125 *
126 * This function is not called recursively. For CTRL-O commands, it returns
127 * and lets the caller handle the Normal-mode command.
128 *
129 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
130 */
131 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100132edit(
133 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100134 int startln, // if set, insert at start of line
Bram Moolenaar7454a062016-01-30 15:14:10 +0100135 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136{
137 int c = 0;
138 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100139 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000140 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 int i;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100143 int did_backspace = TRUE; // previous char was backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100145 int line_is_white = FALSE; // line is empty before insert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100147 linenr_T old_topline = 0; // topline before insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#ifdef FEAT_DIFF
149 int old_topfill = -1;
150#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100151 int inserted_space = FALSE; // just inserted a space
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 int replaceState = REPLACE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100153 int nomove = FALSE; // don't move cursor on return
Bram Moolenaarf2732452018-06-03 14:47:35 +0200154#ifdef FEAT_JOB_CHANNEL
155 int cmdchar_todo = cmdchar;
156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100158 // Remember whether editing was restarted after CTRL-O.
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000159 did_restart_edit = restart_edit;
160
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100161 // sleep before redrawing, needed for "CTRL-O :" that results in an
162 // error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 check_for_delay(TRUE);
164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100165 // set Insstart_orig to Insstart
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100166 update_Insstart_orig = TRUE;
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#ifdef HAVE_SANDBOX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100169 // Don't allow inserting in the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (sandbox != 0)
171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100172 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 return FALSE;
174 }
175#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 // Don't allow changes in the buffer while editing the cmdline. The
177 // caller of getcmdline() may get confused.
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100178 // Don't allow recursive insert mode when busy with completion.
Bram Moolenaarff06f282020-04-21 22:01:14 +0200179 if (textlock != 0 || ins_compl_active() || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000180 {
Bram Moolenaarff06f282020-04-21 22:01:14 +0200181 emsg(_(e_textlock));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000182 return FALSE;
183 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100184 ins_compl_clear(); // clear stuff for CTRL-X mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar843ee412004-06-30 16:16:41 +0000186 /*
187 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
188 */
189 if (cmdchar != 'r' && cmdchar != 'v')
190 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100191 pos_T save_cursor = curwin->w_cursor;
192
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100193#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000194 if (cmdchar == 'R')
195 ptr = (char_u *)"r";
196 else if (cmdchar == 'V')
197 ptr = (char_u *)"v";
198 else
199 ptr = (char_u *)"i";
200 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100201 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100202#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200203 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100205 // Make sure the cursor didn't move. Do call check_cursor_col() in
206 // case the text was modified. Since Insert mode was not started yet
207 // a call to check_cursor_col() may move the cursor, especially with
208 // the "A" command, thus set State to avoid that. Also check that the
209 // line number is still valid (lines may have been deleted).
210 // Do not restore if v:char was set to a non-empty string.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100211 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100212#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200213 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100214#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200215 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100216 {
217 int save_state = State;
218
219 curwin->w_cursor = save_cursor;
220 State = INSERT;
221 check_cursor_col();
222 State = save_state;
223 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000224 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200226#ifdef FEAT_CONCEAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100227 // Check if the cursor line needs redrawing before changing State. If
228 // 'concealcursor' is "n" it needs to be redrawn without concealing.
Bram Moolenaarb9464822018-05-10 15:09:49 +0200229 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200230#endif
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 /*
233 * When doing a paste with the middle mouse button, Insstart is set to
234 * where the paste started.
235 */
236 if (where_paste_started.lnum != 0)
237 Insstart = where_paste_started;
238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {
240 Insstart = curwin->w_cursor;
241 if (startln)
242 Insstart.col = 0;
243 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000244 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 Insstart_blank_vcol = MAXCOL;
246 if (!did_ai)
247 ai_col = 0;
248
249 if (cmdchar != NUL && restart_edit == 0)
250 {
251 ResetRedobuff();
252 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 if (cmdchar == 'V' || cmdchar == 'v')
254 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100255 // "gR" or "gr" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 AppendCharToRedobuff('g');
257 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
258 }
259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100261 if (cmdchar == K_PS)
262 AppendCharToRedobuff('a');
263 else
264 AppendCharToRedobuff(cmdchar);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100265 if (cmdchar == 'g') // "gI" command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 AppendCharToRedobuff('I');
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100267 else if (cmdchar == 'r') // "r<CR>" command
268 count = 1; // insert only one <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 }
270 }
271
272 if (cmdchar == 'R')
273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 State = REPLACE;
275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 else if (cmdchar == 'V' || cmdchar == 'v')
277 {
278 State = VREPLACE;
279 replaceState = VREPLACE;
280 orig_line_count = curbuf->b_ml.ml_line_count;
281 vr_lines_changed = 1;
282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 else
284 State = INSERT;
285
286 stop_insert_mode = FALSE;
287
288 /*
289 * Need to recompute the cursor position, it might move when the cursor is
290 * on a TAB or special character.
291 */
292 curs_columns(TRUE);
293
294 /*
295 * Enable langmap or IME, indicated by 'iminsert'.
296 * Note that IME may enabled/disabled without us noticing here, thus the
297 * 'iminsert' value may not reflect what is actually used. It is updated
298 * when hitting <Esc>.
299 */
300 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
301 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100302#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
304#endif
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#ifdef FEAT_CMDL_INFO
308 clear_showcmd();
309#endif
310#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100311 // there is no reverse replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 revins_on = (State == INSERT && p_ri);
313 if (revins_on)
314 undisplay_dollar();
315 revins_chars = 0;
316 revins_legal = 0;
317 revins_scol = -1;
318#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100319 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100320 {
321 // Disable bracketed paste mode, we won't recognize the escape
322 // sequences.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100323 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
Bram Moolenaar177c9f22019-11-06 13:59:16 +0100325 // Disable modifyOtherKeys, keys with modifiers would cause exiting
326 // Insert mode.
327 out_str(T_CTE);
328 }
329
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 /*
331 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100332 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
333 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 */
335 if (restart_edit != 0 && stuff_empty())
336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 /*
338 * After a paste we consider text typed to be part of the insert for
339 * the pasted text. You can backspace over the pasted text too.
340 */
341 if (where_paste_started.lnum)
342 arrow_used = FALSE;
343 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 arrow_used = TRUE;
345 restart_edit = 0;
346
347 /*
348 * If the cursor was after the end-of-line before the CTRL-O and it is
349 * now at the end-of-line, put it after the end-of-line (this is not
350 * correct in very rare cases).
351 * Also do this if curswant is greater than the current virtual
352 * column. Eg after "^O$" or "^O80|".
353 */
354 validate_virtcol();
355 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000356 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 || curwin->w_curswant > curwin->w_virtcol)
358 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
359 {
360 if (ptr[1] == NUL)
361 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 else if (has_mbyte)
363 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000364 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (ptr[i] == NUL)
366 curwin->w_cursor.col += i;
367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000369 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371 else
372 arrow_used = FALSE;
373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100374 // we are in insert mode now, don't need to start it anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 need_start_insertmode = FALSE;
376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100377 // Need to save the line for undo before inserting the first char.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 ins_need_undo = TRUE;
379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 where_paste_started.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#ifdef FEAT_CINDENT
382 can_cindent = TRUE;
383#endif
384#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100385 // The cursor line is not in a closed fold, unless 'insertmode' is set or
386 // restarting.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 if (!p_im && did_restart_edit == 0)
388 foldOpenCursor();
389#endif
390
391 /*
392 * If 'showmode' is set, show the current (insert/replace/..) mode.
393 * A warning message for changing a readonly file is given here, before
394 * actually changing anything. It's put after the mode, if any.
395 */
396 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000397 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 i = showmode();
399
400 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000401 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
403#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100404 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#endif
406#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100407 do_digraph(-1); // clear digraphs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408#endif
409
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000410 /*
411 * Get the current length of the redo buffer, those characters have to be
412 * skipped if we want to get to the inserted characters.
413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 ptr = get_inserted();
415 if (ptr == NULL)
416 new_insert_skip = 0;
417 else
418 {
419 new_insert_skip = (int)STRLEN(ptr);
420 vim_free(ptr);
421 }
422
423 old_indent = 0;
424
425 /*
426 * Main loop in Insert mode: repeat until Insert mode is left.
427 */
428 for (;;)
429 {
430#ifdef FEAT_RIGHTLEFT
431 if (!revins_legal)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100432 revins_scol = -1; // reset on illegal motions
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 else
434 revins_legal = 0;
435#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100436 if (arrow_used) // don't repeat insert when arrow key used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 count = 0;
438
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100439 if (update_Insstart_orig)
440 Insstart_orig = Insstart;
441
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200442 if (stop_insert_mode && !pum_visible())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100444 // ":stopinsert" used or 'insertmode' reset
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 count = 0;
446 goto doESCkey;
447 }
448
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100449 // set curwin->w_curswant for next K_DOWN or K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (!arrow_used)
451 curwin->w_set_curswant = TRUE;
452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100453 // If there is no typeahead may check for timestamps (e.g., for when a
454 // menu invoked a shell command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (stuff_empty())
456 {
457 did_check_timestamps = FALSE;
458 if (need_check_timestamps)
459 check_timestamps(FALSE);
460 }
461
462 /*
463 * When emsg() was called msg_scroll will have been set.
464 */
465 msg_scroll = FALSE;
466
467#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100468 // When 'mousefocus' is set a mouse movement may have taken us to
469 // another window. "need_mouse_correct" may then be set because of an
470 // autocommand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 if (need_mouse_correct)
472 gui_mouse_correct();
473#endif
474
475#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100476 // Open fold at the cursor line, according to 'foldopen'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 if (fdo_flags & FDO_INSERT)
478 foldOpenCursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100479 // Close folds where the cursor isn't, according to 'foldclose'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 if (!char_avail())
481 foldCheckClose();
482#endif
483
Bram Moolenaarf2732452018-06-03 14:47:35 +0200484#ifdef FEAT_JOB_CHANNEL
485 if (bt_prompt(curbuf))
486 {
487 init_prompt(cmdchar_todo);
488 cmdchar_todo = NUL;
489 }
490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 /*
493 * If we inserted a character at the last position of the last line in
494 * the window, scroll the window one line up. This avoids an extra
495 * redraw.
496 * This is detected when the cursor column is smaller after inserting
497 * something.
498 * Don't do this when the topline changed already, it has
499 * already been adjusted (by insertchar() calling open_line())).
500 */
501 if (curbuf->b_mod_set
502 && curwin->w_p_wrap
503 && !did_backspace
504 && curwin->w_topline == old_topline
505#ifdef FEAT_DIFF
506 && curwin->w_topfill == old_topfill
507#endif
508 )
509 {
510 mincol = curwin->w_wcol;
511 validate_cursor_col();
512
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200513 if (
514#ifdef FEAT_VARTABS
515 (int)curwin->w_wcol < mincol - tabstop_at(
516 get_nolist_virtcol(), curbuf->b_p_ts,
517 curbuf->b_p_vts_array)
518#else
519 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100522 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 && (curwin->w_cursor.lnum != curwin->w_topline
524#ifdef FEAT_DIFF
525 || curwin->w_topfill > 0
526#endif
527 ))
528 {
529#ifdef FEAT_DIFF
530 if (curwin->w_topfill > 0)
531 --curwin->w_topfill;
532 else
533#endif
534#ifdef FEAT_FOLDING
535 if (hasFolding(curwin->w_topline, NULL, &old_topline))
536 set_topline(curwin, old_topline + 1);
537 else
538#endif
539 set_topline(curwin, curwin->w_topline + 1);
540 }
541 }
542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100543 // May need to adjust w_topline to show the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 update_topline();
545
546 did_backspace = FALSE;
547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100548 validate_cursor(); // may set must_redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550 /*
551 * Redraw the display when no characters are waiting.
552 * Also shows mode, ruler and positions cursor.
553 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000554 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if (curwin->w_p_scb)
557 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar860cae12010-06-05 23:22:07 +0200559 if (curwin->w_p_crb)
560 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 update_curswant();
562 old_topline = curwin->w_topline;
563#ifdef FEAT_DIFF
564 old_topfill = curwin->w_topfill;
565#endif
566
567#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100568 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#endif
570
571 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100572 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100574 if (c != K_CURSORHOLD)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100575 lastc = c; // remember the previous char for CTRL-D
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100577 // After using CTRL-G U the next cursor key will not break undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200578 if (dont_sync_undo == MAYBE)
579 dont_sync_undo = TRUE;
580 else
581 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100582 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100583 // Got here from normal mode when bracketed paste started.
Bram Moolenaarec2da362017-01-21 20:04:22 +0100584 c = K_PS;
585 else
586 do
587 {
588 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200589
590 if (stop_insert_mode)
591 {
592 // Insert mode ended, possibly from a callback.
593 count = 0;
594 nomove = TRUE;
595 goto doESCkey;
596 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100597 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100599 // Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V.
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000600 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#ifdef FEAT_RIGHTLEFT
603 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100604 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000607 /*
608 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000609 * and the cursor is still in the completed word. Only when there is
610 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000611 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100612 if (ins_compl_active()
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000613 && pum_wanted()
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100614 && curwin->w_cursor.col >= ins_compl_col()
615 && ins_compl_has_shown_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100617 // BS: Delete one character from "compl_leader".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000618 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100619 && curwin->w_cursor.col > ins_compl_col()
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000620 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000621 continue;
622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100623 // When no match was selected or it was edited.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100624 if (!ins_compl_used_match())
Bram Moolenaara6557602006-02-04 22:43:20 +0000625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100626 // CTRL-L: Add one character from the current match to
627 // "compl_leader". Except when at the original match and
628 // there is nothing to add, CTRL-L works like CTRL-P then.
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000629 if (c == Ctrl_L
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100630 && (!ctrl_x_mode_line_or_eval()
631 || ins_compl_long_shown_match()))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000632 {
633 ins_compl_addfrommatch();
634 continue;
635 }
636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100637 // A non-white character that fits in with the current
638 // completion: Add to "compl_leader".
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000639 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000640 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100641#if defined(FEAT_EVAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100642 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100643 char_u *str = do_insert_char_pre(c);
644 char_u *p;
645
646 if (str != NULL)
647 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100648 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100649 ins_compl_addleader(PTR2CHAR(p));
650 vim_free(str);
651 }
652 else
653#endif
654 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000655 continue;
656 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100658 // Pressing CTRL-Y selects the current match. When
659 // ins_compl_enter_selects() is set the Enter key does the
660 // same.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100661 if ((c == Ctrl_Y || (ins_compl_enter_selects()
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200662 && (c == CAR || c == K_KENTER || c == NL)))
663 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000664 {
665 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200666 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000667 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000668 }
669 }
670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100671 // Prepare for or stop CTRL-X mode. This doesn't do completion, but
672 // it does fix up the text when finishing completion.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100673 ins_compl_init_get_longest();
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000674 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000675 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100677 // CTRL-\ CTRL-N goes to Normal mode,
678 // CTRL-\ CTRL-G goes to mode selected with 'insertmode',
679 // CTRL-\ CTRL-O is like CTRL-O but without moving the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (c == Ctrl_BSL)
681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100682 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +0000683 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 ++no_mapping;
685 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000686 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 --no_mapping;
688 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000689 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100691 // it's something else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 vungetc(c);
693 c = Ctrl_BSL;
694 }
695 else if (c == Ctrl_G && p_im)
696 continue;
697 else
698 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000699 if (c == Ctrl_O)
700 {
701 ins_ctrl_o();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100702 ins_at_eol = FALSE; // cursor keeps its column
Bram Moolenaar488c6512005-08-11 20:09:58 +0000703 nomove = TRUE;
704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 count = 0;
706 goto doESCkey;
707 }
708 }
709
710#ifdef FEAT_DIGRAPHS
711 c = do_digraph(c);
712#endif
713
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100714 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode_cmdline())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (c == Ctrl_V || c == Ctrl_Q)
717 {
718 ins_ctrl_v();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100719 c = Ctrl_V; // pretend CTRL-V is last typed character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 continue;
721 }
722
723#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200724 if (cindent_on() && ctrl_x_mode_none())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100726 // A key name preceded by a bang means this key is not to be
727 // inserted. Skip ahead to the re-indenting below.
728 // A key name preceded by a star means that indenting has to be
729 // done before inserting the key.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 line_is_white = inindent(0);
731 if (in_cinkeys(c, '!', line_is_white))
732 goto force_cindent;
733 if (can_cindent && in_cinkeys(c, '*', line_is_white)
734 && stop_arrow() == OK)
735 do_c_expr_indent();
736 }
737#endif
738
739#ifdef FEAT_RIGHTLEFT
740 if (curwin->w_p_rl)
741 switch (c)
742 {
743 case K_LEFT: c = K_RIGHT; break;
744 case K_S_LEFT: c = K_S_RIGHT; break;
745 case K_C_LEFT: c = K_C_RIGHT; break;
746 case K_RIGHT: c = K_LEFT; break;
747 case K_S_RIGHT: c = K_S_LEFT; break;
748 case K_C_RIGHT: c = K_C_LEFT; break;
749 }
750#endif
751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /*
753 * If 'keymodel' contains "startsel", may start selection. If it
754 * does, a CTRL-O and c will be stuffed, we need to get these
755 * characters.
756 */
757 if (ins_start_select(c))
758 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 /*
761 * The big switch to handle a character in insert mode.
762 */
763 switch (c)
764 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100765 case ESC: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (echeck_abbr(ESC + ABBR_OFF))
767 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 case Ctrl_C: // End input mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#ifdef FEAT_CMDWIN
772 if (c == Ctrl_C && cmdwin_type != 0)
773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // Close the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 cmdwin_result = K_IGNORE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100776 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000777 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 goto doESCkey;
779 }
780#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200781#ifdef FEAT_JOB_CHANNEL
782 if (c == Ctrl_C && bt_prompt(curbuf))
783 {
784 if (invoke_prompt_interrupt())
785 {
786 if (!bt_prompt(curbuf))
787 // buffer changed to a non-prompt buffer, get out of
788 // Insert mode
789 goto doESCkey;
790 break;
791 }
792 }
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795#ifdef UNIX
796do_intr:
797#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100798 // when 'insertmode' set, and not halfway a mapping, don't leave
799 // Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (goto_im())
801 {
802 if (got_int)
803 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100804 (void)vgetc(); // flush all buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 got_int = FALSE;
806 }
807 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200808 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 break;
810 }
811doESCkey:
812 /*
813 * This is the ONLY return from edit()!
814 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100815 // Always update o_lnum, so that a "CTRL-O ." that adds a line
816 // still puts the cursor back after the inserted text.
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000817 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 o_lnum = curwin->w_cursor.lnum;
819
Bram Moolenaar488c6512005-08-11 20:09:58 +0000820 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000821 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +0100822 // When CTRL-C was typed got_int will be set, with the result
823 // that the autocommands won't be executed. When mapped got_int
824 // is not set, but let's keep the behavior the same.
825 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200826 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000827 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 continue;
831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 case Ctrl_Z: // suspend when 'insertmode' set
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000833 if (!p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100834 goto normalchar; // insert CTRL-Z as normal char
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +0100835 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +0100836#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100837 ui_cursor_shape(); // may need to update cursor shape
Bram Moolenaar74a47162017-02-26 19:09:05 +0100838#endif
839 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 case Ctrl_O: // execute one command
Bram Moolenaare344bea2005-09-01 20:46:49 +0000842#ifdef FEAT_COMPL_FUNC
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100843 if (ctrl_x_mode_omni())
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000844 goto docomplete;
845#endif
846 if (echeck_abbr(Ctrl_O + ABBR_OFF))
847 break;
848 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // don't move the cursor left when 'virtualedit' has "onemore".
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000851 if (ve_flags & VE_ONEMORE)
852 {
853 ins_at_eol = FALSE;
854 nomove = TRUE;
855 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000856 count = 0;
857 goto doESCkey;
858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100859 case K_INS: // toggle insert/replace mode
Bram Moolenaar572cb562005-08-05 21:35:02 +0000860 case K_KINS:
861 ins_insert(replaceState);
862 break;
863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100864 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar572cb562005-08-05 21:35:02 +0000865 break;
866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100867 case K_HELP: // Help key works like <ESC> <Help>
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000868 case K_F1:
869 case K_XF1:
870 stuffcharReadbuff(K_HELP);
871 if (p_im)
872 need_start_insertmode = TRUE;
873 goto doESCkey;
874
875#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100876 case K_F21: // NetBeans command
877 ++no_mapping; // don't map the next key hits
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000878 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000879 --no_mapping;
880 netbeans_keycommand(i);
881 break;
882#endif
883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100884 case K_ZERO: // Insert the previously inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 case NUL:
886 case Ctrl_A:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100887 // For ^@ the trailing ESC will end the insert, unless there is an
888 // error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
890 && c != Ctrl_A && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100891 goto doESCkey; // quit insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 inserted_space = FALSE;
893 break;
894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100895 case Ctrl_R: // insert the contents of a register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ins_reg();
897 auto_format(FALSE, TRUE);
898 inserted_space = FALSE;
899 break;
900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100901 case Ctrl_G: // commands starting with CTRL-G
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 ins_ctrl_g();
903 break;
904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 case Ctrl_HAT: // switch input mode and/or langmap
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000906 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 break;
908
909#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 case Ctrl__: // switch between languages
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (!p_ari)
912 goto normalchar;
913 ins_ctrl_();
914 break;
915#endif
916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100917 case Ctrl_D: // Make indent one shiftwidth smaller.
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200918#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100919 if (ctrl_x_mode_path_defines())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 goto docomplete;
921#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100922 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100924 case Ctrl_T: // Make indent one shiftwidth greater.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100925 if (c == Ctrl_T && ctrl_x_mode_thesaurus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000927 if (has_compl_option(FALSE))
928 goto docomplete;
929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 ins_shift(c, lastc);
933 auto_format(FALSE, TRUE);
934 inserted_space = FALSE;
935 break;
936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100937 case K_DEL: // delete character under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 case K_KDEL:
939 ins_del();
940 auto_format(FALSE, TRUE);
941 break;
942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 case K_BS: // delete character before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 case Ctrl_H:
945 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
946 auto_format(FALSE, TRUE);
947 break;
948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100949 case Ctrl_W: // delete word before the cursor
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200950#ifdef FEAT_JOB_CHANNEL
951 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
952 {
953 // In a prompt window CTRL-W is used for window commands.
954 // Use Shift-CTRL-W to delete a word.
955 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +0200956 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200957 nomove = TRUE;
958 count = 0;
959 goto doESCkey;
960 }
961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
963 auto_format(FALSE, TRUE);
964 break;
965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100966 case Ctrl_U: // delete all inserted text in current line
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000967# ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // CTRL-X CTRL-U completes with 'completefunc'.
Bram Moolenaar7591bb32019-03-30 13:53:47 +0100969 if (ctrl_x_mode_function())
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000970 goto docomplete;
971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
973 auto_format(FALSE, TRUE);
974 inserted_space = FALSE;
975 break;
976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100977 case K_LEFTMOUSE: // mouse keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 case K_LEFTMOUSE_NM:
979 case K_LEFTDRAG:
980 case K_LEFTRELEASE:
981 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100982 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 case K_MIDDLEMOUSE:
984 case K_MIDDLEDRAG:
985 case K_MIDDLERELEASE:
986 case K_RIGHTMOUSE:
987 case K_RIGHTDRAG:
988 case K_RIGHTRELEASE:
989 case K_X1MOUSE:
990 case K_X1DRAG:
991 case K_X1RELEASE:
992 case K_X2MOUSE:
993 case K_X2DRAG:
994 case K_X2RELEASE:
995 ins_mouse(c);
996 break;
997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 case K_MOUSEDOWN: // Default action for scroll wheel up: scroll up
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200999 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 break;
1001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 case K_MOUSEUP: // Default action for scroll wheel down: scroll down
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001003 ins_mousescroll(MSCR_UP);
1004 break;
1005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001006 case K_MOUSELEFT: // Scroll wheel left
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001007 ins_mousescroll(MSCR_LEFT);
1008 break;
1009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 case K_MOUSERIGHT: // Scroll wheel right
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001011 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001013
Bram Moolenaarec2da362017-01-21 20:04:22 +01001014 case K_PS:
1015 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1016 if (cmdchar == K_PS)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001017 // invoked from normal mode, bail out
Bram Moolenaarec2da362017-01-21 20:04:22 +01001018 goto doESCkey;
1019 break;
1020 case K_PE:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001021 // Got K_PE without K_PS, ignore.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001022 break;
1023
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001024#ifdef FEAT_GUI_TABLINE
1025 case K_TABLINE:
1026 case K_TABMENU:
1027 ins_tabline(c);
1028 break;
1029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001031 case K_IGNORE: // Something mapped to nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 break;
1033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001034 case K_CURSORHOLD: // Didn't type something for a while.
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001035 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001036 did_cursorhold = TRUE;
1037 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001038
Bram Moolenaar4f974752019-02-17 17:44:42 +01001039#ifdef FEAT_GUI_MSWIN
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001040 // On MS-Windows ignore <M-F4>, we get it when closing the window
1041 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001042 case K_F4:
1043 if (mod_mask != MOD_MASK_ALT)
1044 goto normalchar;
1045 break;
1046#endif
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#ifdef FEAT_GUI
1049 case K_VER_SCROLLBAR:
1050 ins_scroll();
1051 break;
1052
1053 case K_HOR_SCROLLBAR:
1054 ins_horscroll();
1055 break;
1056#endif
1057
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001058 case K_HOME: // <Home>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 case K_S_HOME:
1061 case K_C_HOME:
1062 ins_home(c);
1063 break;
1064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001065 case K_END: // <End>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 case K_S_END:
1068 case K_C_END:
1069 ins_end(c);
1070 break;
1071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001072 case K_LEFT: // <Left>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001073 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1074 ins_s_left();
1075 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001076 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 break;
1078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001079 case K_S_LEFT: // <S-Left>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 case K_C_LEFT:
1081 ins_s_left();
1082 break;
1083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001084 case K_RIGHT: // <Right>
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001085 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1086 ins_s_right();
1087 else
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01001088 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 break;
1090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001091 case K_S_RIGHT: // <S-Right>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 case K_C_RIGHT:
1093 ins_s_right();
1094 break;
1095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001096 case K_UP: // <Up>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001097 if (pum_visible())
1098 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001099 if (mod_mask & MOD_MASK_SHIFT)
1100 ins_pageup();
1101 else
1102 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 break;
1104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001105 case K_S_UP: // <S-Up>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 case K_PAGEUP:
1107 case K_KPAGEUP:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001108 if (pum_visible())
1109 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 ins_pageup();
1111 break;
1112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001113 case K_DOWN: // <Down>
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001114 if (pum_visible())
1115 goto docomplete;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001116 if (mod_mask & MOD_MASK_SHIFT)
1117 ins_pagedown();
1118 else
1119 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001122 case K_S_DOWN: // <S-Down>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 case K_PAGEDOWN:
1124 case K_KPAGEDOWN:
Bram Moolenaare3226be2005-12-18 22:10:00 +00001125 if (pum_visible())
1126 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ins_pagedown();
1128 break;
1129
1130#ifdef FEAT_DND
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001131 case K_DROP: // drag-n-drop event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ins_drop();
1133 break;
1134#endif
1135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001136 case K_S_TAB: // When not mapped, use like a normal TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 c = TAB;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001138 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001140 case TAB: // TAB or Complete patterns along path
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001141#if defined(FEAT_FIND_ID)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001142 if (ctrl_x_mode_path_patterns())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 goto docomplete;
1144#endif
1145 inserted_space = FALSE;
1146 if (ins_tab())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001147 goto normalchar; // insert TAB as a normal char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 auto_format(FALSE, TRUE);
1149 break;
1150
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001151 case K_KENTER: // <Enter>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 c = CAR;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001153 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 case CAR:
1155 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001156#if defined(FEAT_QUICKFIX)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001157 // In a quickfix window a <CR> jumps to the error under the
1158 // cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (bt_quickfix(curbuf) && c == CAR)
1160 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001161 if (curwin->w_llist_ref == NULL) // quickfix window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001162 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001163 else // location list window
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001164 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 break;
1166 }
1167#endif
1168#ifdef FEAT_CMDWIN
1169 if (cmdwin_type != 0)
1170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001171 // Execute the command in the cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 cmdwin_result = CAR;
1173 goto doESCkey;
1174 }
1175#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001176#ifdef FEAT_JOB_CHANNEL
1177 if (bt_prompt(curbuf))
1178 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001179 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001180 if (!bt_prompt(curbuf))
1181 // buffer changed to a non-prompt buffer, get out of
1182 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001183 goto doESCkey;
1184 break;
1185 }
1186#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001187 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001188 goto doESCkey; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 auto_format(FALSE, FALSE);
1190 inserted_space = FALSE;
1191 break;
1192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001193 case Ctrl_K: // digraph or keyword completion
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001194 if (ctrl_x_mode_dictionary())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 if (has_compl_option(TRUE))
1197 goto docomplete;
1198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001200#ifdef FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 c = ins_digraph();
1202 if (c == NUL)
1203 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204#endif
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001205 goto normalchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001207 case Ctrl_X: // Enter CTRL-X mode
Bram Moolenaar572cb562005-08-05 21:35:02 +00001208 ins_ctrl_x();
1209 break;
1210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001211 case Ctrl_RSB: // Tag name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001212 if (!ctrl_x_mode_tags())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 goto normalchar;
1214 goto docomplete;
1215
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001216 case Ctrl_F: // File name completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001217 if (!ctrl_x_mode_files())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 goto normalchar;
1219 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001221 case 's': // Spelling completion after ^X
Bram Moolenaar488c6512005-08-11 20:09:58 +00001222 case Ctrl_S:
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001223 if (!ctrl_x_mode_spell())
Bram Moolenaar488c6512005-08-11 20:09:58 +00001224 goto normalchar;
1225 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 case Ctrl_L: // Whole line completion after ^X
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001228 if (!ctrl_x_mode_whole_line())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001230 // CTRL-L with 'insertmode' set: Leave Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (p_im)
1232 {
1233 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1234 break;
1235 goto doESCkey;
1236 }
1237 goto normalchar;
1238 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001239 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001241 case Ctrl_P: // Do previous/next pattern completion
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 case Ctrl_N:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001243 // if 'complete' is empty then plain ^P is no longer special,
1244 // but it is under other ^X modes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001246 && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 goto normalchar;
1249
1250docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001251 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001252#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001253 disable_fold_update++; // don't redraw folds here
Bram Moolenaara6c07602017-03-05 21:18:27 +01001254#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001255 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001256 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001257#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001258 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001259#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001260 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001263 case Ctrl_Y: // copy from previous line or scroll down
1264 case Ctrl_E: // copy from next line or scroll up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001265 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 break;
1267
1268 default:
1269#ifdef UNIX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001270 if (c == intr_char) // special interrupt char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 goto do_intr;
1272#endif
1273
Bram Moolenaare659c952011-05-19 17:25:41 +02001274normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001276 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001278#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001279 if (!p_paste)
1280 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001281 // Trigger InsertCharPre.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001282 char_u *str = do_insert_char_pre(c);
1283 char_u *p;
1284
1285 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001286 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001287 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001288 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001289 // Insert the new value of v:char literally.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001290 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001291 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001292 c = PTR2CHAR(p);
1293 if (c == CAR || c == K_KENTER || c == NL)
1294 ins_eol(c);
1295 else
1296 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001297 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001298 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001299 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001300 vim_free(str);
1301 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001302 }
1303
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001304 // If the new value is already inserted or an empty string
1305 // then don't insert any character.
Bram Moolenaare659c952011-05-19 17:25:41 +02001306 if (c == NUL)
1307 break;
1308 }
1309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#ifdef FEAT_SMARTINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001311 // Try to perform smart-indenting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 ins_try_si(c);
1313#endif
1314
1315 if (c == ' ')
1316 {
1317 inserted_space = TRUE;
1318#ifdef FEAT_CINDENT
1319 if (inindent(0))
1320 can_cindent = FALSE;
1321#endif
1322 if (Insstart_blank_vcol == MAXCOL
1323 && curwin->w_cursor.lnum == Insstart.lnum)
1324 Insstart_blank_vcol = get_nolist_virtcol();
1325 }
1326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001327 // Insert a normal character and check for abbreviations on a
1328 // special character. Let CTRL-] expand abbreviations without
1329 // inserting it.
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001330 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001331 // Add ABBR_OFF for characters above 0x100, this is
1332 // what check_abbr() expects.
1333 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1334 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 insert_special(c, FALSE, FALSE);
1337#ifdef FEAT_RIGHTLEFT
1338 revins_legal++;
1339 revins_chars++;
1340#endif
1341 }
1342
1343 auto_format(FALSE, TRUE);
1344
1345#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001346 // When inserting a character the cursor line must never be in a
1347 // closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 foldOpenCursor();
1349#endif
1350 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001351 } // end of switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001353 // If typed something may trigger CursorHoldI again.
Bram Moolenaar245c4102016-04-20 17:37:41 +02001354 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001355#ifdef FEAT_COMPL_FUNC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001356 // but not in CTRL-X mode, a script can't restore the state
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001357 && ctrl_x_mode_normal()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001358#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001359 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001360 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001361
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001362 // If the cursor was moved we didn't just insert a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (arrow_used)
1364 inserted_space = FALSE;
1365
1366#ifdef FEAT_CINDENT
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001367 if (can_cindent && cindent_on() && ctrl_x_mode_normal())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
1369force_cindent:
1370 /*
1371 * Indent now if a key was typed that is in 'cinkeys'.
1372 */
1373 if (in_cinkeys(c, ' ', line_is_white))
1374 {
1375 if (stop_arrow() == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001376 // re-indent the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 do_c_expr_indent();
1378 }
1379 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001380#endif // FEAT_CINDENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001382 } // for (;;)
1383 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384}
1385
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001386 int
1387ins_need_undo_get(void)
1388{
1389 return ins_need_undo;
1390}
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392/*
1393 * Redraw for Insert mode.
1394 * This is postponed until getting the next character to make '$' in the 'cpo'
1395 * option work correctly.
1396 * Only redraw when there are no characters available. This speeds up
1397 * inserting sequences of characters (e.g., for CTRL-R).
1398 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001399 void
Bram Moolenaar3397f742019-06-02 18:40:06 +02001400ins_redraw(int ready) // not busy with something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001402#ifdef FEAT_CONCEAL
1403 linenr_T conceal_old_cursor_line = 0;
1404 linenr_T conceal_new_cursor_line = 0;
1405 int conceal_update_lines = FALSE;
1406#endif
1407
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001408 if (char_avail())
1409 return;
1410
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001411 // Trigger CursorMoved if the cursor moved. Not when the popup menu is
1412 // visible, the command might delete it.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001413 if (ready && (has_cursormovedI()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001414# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001415 || popup_visible
1416# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001417# if defined(FEAT_CONCEAL)
1418 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001419# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001420 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001421 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001422 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001423 {
1424# ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001425 // Need to update the screen first, to make sure syntax
1426 // highlighting is correct after making a change (e.g., inserting
1427 // a "(". The autocommand may also require a redraw, so it's done
1428 // again below, unfortunately.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001429 if (syntax_present(curwin) && must_redraw)
1430 update_screen(0);
1431# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001432 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001433 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001434 // Make sure curswant is correct, an autocommand may call
1435 // getcurpos().
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001436 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001437 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001438 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001439#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001440 if (popup_visible)
1441 popup_check_cursor_pos();
1442#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001443# ifdef FEAT_CONCEAL
1444 if (curwin->w_p_cole > 0)
1445 {
1446 conceal_old_cursor_line = last_cursormoved.lnum;
1447 conceal_new_cursor_line = curwin->w_cursor.lnum;
1448 conceal_update_lines = TRUE;
1449 }
1450# endif
1451 last_cursormoved = curwin->w_cursor;
1452 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001454 // Trigger TextChangedI if b_changedtick differs.
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001455 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001456 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001457 && !pum_visible())
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001458 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001459 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001460 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001461
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001462 // save and restore curwin and curbuf, in case the autocmd changes them
1463 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001464 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001465 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001466 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001467 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1468 u_save(curwin->w_cursor.lnum,
1469 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001472 // Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1473 // TextChangedI will need to trigger for backwards compatibility, thus use
1474 // different b_last_changedtick* variables.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001475 if (ready && has_textchangedP()
1476 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1477 && pum_visible())
1478 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001479 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001480 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001481
1482 // save and restore curwin and curbuf, in case the autocmd changes them
1483 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001484 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001485 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001486 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001487 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1488 u_save(curwin->w_cursor.lnum,
1489 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001490 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001491
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001492 // Trigger SafeState if nothing is pending.
1493 may_trigger_safestate(ready
1494 && !ins_compl_active()
1495 && !pum_visible());
1496
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001497#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001498 if ((conceal_update_lines
1499 && (conceal_old_cursor_line != conceal_new_cursor_line
1500 || conceal_cursor_line(curwin)))
1501 || need_cursor_line_redraw)
1502 {
1503 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001504 redrawWinline(curwin, conceal_old_cursor_line);
1505 redrawWinline(curwin, conceal_new_cursor_line == 0
1506 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001507 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001508 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001509 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001510#endif
1511 if (must_redraw)
1512 update_screen(0);
1513 else if (clear_cmdline || redraw_cmdline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001514 showmode(); // clear cmdline and show mode
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001515 showruler(FALSE);
1516 setcursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001517 emsg_on_display = FALSE; // may remove error message now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518}
1519
1520/*
1521 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001527 int did_putchar = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001528 int prev_mod_mask = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001530 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00001531 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
1533 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001536 did_putchar = TRUE;
1537 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001538 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
1540#ifdef FEAT_CMDL_INFO
1541 add_to_showcmd_c(Ctrl_V);
1542#endif
1543
1544 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001545 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001546 // when the line fits in 'columns' the '^' is at the start of the next
1547 // line and will not removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001548 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#ifdef FEAT_CMDL_INFO
1550 clear_showcmd();
1551#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001552
1553 if ((c == ESC || c == CSI) && !(prev_mod_mask & MOD_MASK_SHIFT))
1554 // Using CTRL-V: Change any modifyOtherKeys ESC sequence to a normal
1555 // key. Don't do this for CTRL-SHIFT-V.
1556 c = decodeModifyOtherKeys(c);
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 insert_special(c, FALSE, TRUE);
1559#ifdef FEAT_RIGHTLEFT
1560 revins_chars++;
1561 revins_legal++;
1562#endif
1563}
1564
1565/*
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001566 * After getting an ESC or CSI for a literal key: If the typeahead buffer
1567 * contains a modifyOtherKeys sequence then decode it and return the result.
1568 * Otherwise return "c".
1569 * Note that this doesn't wait for characters, they must be in the typeahead
1570 * buffer already.
1571 */
1572 int
1573decodeModifyOtherKeys(int c)
1574{
1575 char_u *p = typebuf.tb_buf + typebuf.tb_off;
1576 int idx;
1577 int form = 0;
1578 int argidx = 0;
1579 int arg[2] = {0, 0};
1580
1581 // Recognize:
1582 // form 0: {lead}{key};{modifier}u
1583 // form 1: {lead}27;{modifier};{key}~
1584 if ((c == CSI || (c == ESC && *p == '[')) && typebuf.tb_len >= 4)
1585 {
1586 idx = (*p == '[');
1587 if (p[idx] == '2' && p[idx + 1] == '7' && p[idx + 2] == ';')
1588 {
1589 form = 1;
1590 idx += 3;
1591 }
1592 while (idx < typebuf.tb_len && argidx < 2)
1593 {
1594 if (p[idx] == ';')
1595 ++argidx;
1596 else if (VIM_ISDIGIT(p[idx]))
1597 arg[argidx] = arg[argidx] * 10 + (p[idx] - '0');
1598 else
1599 break;
1600 ++idx;
1601 }
1602 if (idx < typebuf.tb_len
1603 && p[idx] == (form == 1 ? '~' : 'u')
1604 && argidx == 1)
1605 {
1606 // Match, consume the code.
1607 typebuf.tb_off += idx + 1;
1608 typebuf.tb_len -= idx + 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001609#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1610 if (typebuf.tb_len == 0)
1611 typebuf_was_filled = FALSE;
1612#endif
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001613
1614 mod_mask = decode_modifiers(arg[!form]);
1615 c = merge_modifyOtherKeys(arg[form]);
1616 }
1617 }
1618
1619 return c;
1620}
1621
1622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 * Put a character directly onto the screen. It's not stored in a buffer.
1624 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1625 */
1626static int pc_status;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001627#define PC_STATUS_UNSET 0 // pc_bytes was not set
1628#define PC_STATUS_RIGHT 1 // right halve of double-wide char
1629#define PC_STATUS_LEFT 2 // left halve of double-wide char
1630#define PC_STATUS_SET 3 // pc_bytes was filled
1631static char_u pc_bytes[MB_MAXBYTES + 1]; // saved bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632static int pc_attr;
1633static int pc_row;
1634static int pc_col;
1635
1636 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001637edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 int attr;
1640
1641 if (ScreenLines != NULL)
1642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001643 update_topline(); // just in case w_topline isn't valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 validate_cursor();
1645 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001646 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 else
1648 attr = 0;
1649 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001650 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652#ifdef FEAT_RIGHTLEFT
1653 if (curwin->w_p_rl)
1654 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001655 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (has_mbyte)
1657 {
1658 int fix_col = mb_fix_col(pc_col, pc_row);
1659
1660 if (fix_col != pc_col)
1661 {
1662 screen_putchar(' ', pc_row, fix_col, attr);
1663 --curwin->w_wcol;
1664 pc_status = PC_STATUS_RIGHT;
1665 }
1666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 else
1669#endif
1670 {
1671 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (mb_lefthalve(pc_row, pc_col))
1673 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 }
1675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001676 // save the character to be able to put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1680 pc_status = PC_STATUS_SET;
1681 }
1682 screen_putchar(c, pc_row, pc_col, attr);
1683 }
1684}
1685
Bram Moolenaarf2732452018-06-03 14:47:35 +02001686#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1687/*
1688 * Return the effective prompt for the current buffer.
1689 */
1690 char_u *
1691prompt_text(void)
1692{
1693 if (curbuf->b_prompt_text == NULL)
1694 return (char_u *)"% ";
1695 return curbuf->b_prompt_text;
1696}
1697
1698/*
1699 * Prepare for prompt mode: Make sure the last line has the prompt text.
1700 * Move the cursor to this line.
1701 */
1702 static void
1703init_prompt(int cmdchar_todo)
1704{
1705 char_u *prompt = prompt_text();
1706 char_u *text;
1707
1708 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1709 text = ml_get_curline();
1710 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1711 {
1712 // prompt is missing, insert it or append a line with it
1713 if (*text == NUL)
1714 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1715 else
1716 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1717 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1718 coladvance((colnr_T)MAXCOL);
1719 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1720 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001721
1722 // Insert always starts after the prompt, allow editing text after it.
1723 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1724 || Insstart_orig.col != (int)STRLEN(prompt))
1725 {
1726 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001727 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001728 Insstart_orig = Insstart;
1729 Insstart_textlen = Insstart.col;
1730 Insstart_blank_vcol = MAXCOL;
1731 arrow_used = FALSE;
1732 }
1733
Bram Moolenaarf2732452018-06-03 14:47:35 +02001734 if (cmdchar_todo == 'A')
1735 coladvance((colnr_T)MAXCOL);
1736 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001737 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001738 // Make sure the cursor is in a valid position.
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001739 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001740}
1741
1742/*
1743 * Return TRUE if the cursor is in the editable position of the prompt line.
1744 */
1745 int
1746prompt_curpos_editable()
1747{
1748 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1749 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1750}
1751#endif
1752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753/*
1754 * Undo the previous edit_putchar().
1755 */
1756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001757edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
1759 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 if (pc_status == PC_STATUS_RIGHT)
1762 ++curwin->w_wcol;
1763 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001764 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1767 }
1768}
1769
1770/*
1771 * Called when p_dollar is set: display a '$' at the end of the changed text
1772 * Only works when cursor is in the line that changes.
1773 */
1774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 colnr_T save_col;
1778
1779 if (!redrawing())
1780 return;
1781
1782 cursor_off();
1783 save_col = curwin->w_cursor.col;
1784 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (has_mbyte)
1786 {
1787 char_u *p;
1788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001789 // If on the last byte of a multi-byte move to the first byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 p = ml_get_curline();
1791 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1792 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001793 curs_columns(FALSE); // recompute w_wrow and w_wcol
Bram Moolenaar02631462017-09-22 15:20:32 +02001794 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
1796 edit_putchar('$', FALSE);
1797 dollar_vcol = curwin->w_virtcol;
1798 }
1799 curwin->w_cursor.col = save_col;
1800}
1801
1802/*
1803 * Call this function before moving the cursor from the normal insert position
1804 * in insert mode.
1805 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001806 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001807undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001809 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001811 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001812 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814}
1815
1816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 * Truncate the space at the end of a line. This is to be used only in an
1818 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1819 * modes.
1820 */
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 int i;
1825
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001826 // find start of trailing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +01001827 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001830 replace_join(0); // remove a NUL from the replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832 line[i + 1] = NUL;
1833}
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835/*
1836 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1837 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001838 * Will attempt not to go before "col" even when there is a composing
1839 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 */
1841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001842backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843{
1844 while ((int)curwin->w_cursor.col > col)
1845 {
1846 curwin->w_cursor.col--;
1847 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001848 replace_do_bs(col);
1849 else if (!del_char_after_col(col))
1850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
1852}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001854/*
1855 * Like del_char(), but make sure not to go before column "limit_col".
1856 * Only matters when there are composing characters.
1857 * Return TRUE when something was deleted.
1858 */
1859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001860del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001861{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001862 if (enc_utf8 && limit_col >= 0)
1863 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001864 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001865
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001866 // Make sure the cursor is at the start of a character, but
1867 // skip forward again when going too far back because of a
1868 // composing character.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001869 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001870 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001871 {
1872 int l = utf_ptr2len(ml_get_cursor());
1873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001874 if (l == 0) // end of line
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001875 break;
1876 curwin->w_cursor.col += l;
1877 }
1878 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1879 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001880 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001881 }
1882 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001883 (void)del_char(FALSE);
1884 return TRUE;
1885}
1886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887/*
1888 * Next character is interpreted literally.
1889 * A one, two or three digit decimal number is interpreted as its byte value.
1890 * If one or two digits are entered, the next character is given to vungetc().
1891 * For Unicode a character > 255 may be returned.
1892 */
1893 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001894get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 int cc;
1897 int nc;
1898 int i;
1899 int hex = FALSE;
1900 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
1903 if (got_int)
1904 return Ctrl_C;
1905
1906#ifdef FEAT_GUI
1907 /*
1908 * In GUI there is no point inserting the internal code for a special key.
1909 * It is more useful to insert the string "<KEY>" instead. This would
1910 * probably be useful in a text window too, but it would not be
1911 * vi-compatible (maybe there should be an option for it?) -- webb
1912 */
1913 if (gui.in_use)
1914 ++allow_keys;
1915#endif
1916#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001917 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 ++no_mapping; // don't map the next key hits
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 cc = 0;
1921 i = 0;
1922 for (;;)
1923 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001924 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01001926 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 add_to_showcmd(nc);
1928#endif
1929 if (nc == 'x' || nc == 'X')
1930 hex = TRUE;
1931 else if (nc == 'o' || nc == 'O')
1932 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 else if (nc == 'u' || nc == 'U')
1934 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 else
1936 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001937 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
1939 if (!vim_isxdigit(nc))
1940 break;
1941 cc = cc * 16 + hex2nr(nc);
1942 }
1943 else if (octal)
1944 {
1945 if (nc < '0' || nc > '7')
1946 break;
1947 cc = cc * 8 + nc - '0';
1948 }
1949 else
1950 {
1951 if (!VIM_ISDIGIT(nc))
1952 break;
1953 cc = cc * 10 + nc - '0';
1954 }
1955
1956 ++i;
1957 }
1958
Bram Moolenaar13505972019-01-24 15:04:48 +01001959 if (cc > 255 && unicode == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001960 cc = 255; // limit range to 0-255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 nc = 0;
1962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001963 if (hex) // hex: up to two chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 if (i >= 2)
1966 break;
1967 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001968 else if (unicode) // Unicode: up to four or eight chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
1970 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
1971 break;
1972 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001973 else if (i >= 3) // decimal or octal: up to three chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 break;
1975 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001976 if (i == 0) // no number entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001978 if (nc == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 cc = '\n';
1981 nc = 0;
1982 }
1983 else
1984 {
1985 cc = nc;
1986 nc = 0;
1987 }
1988 }
1989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001990 if (cc == 0) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00001992 if (enc_dbcs && (cc & 0xff) == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001993 cc = '?'; // don't accept an illegal DBCS char, the NUL in the
1994 // second byte will cause trouble!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
1996 --no_mapping;
1997#ifdef FEAT_GUI
1998 if (gui.in_use)
1999 --allow_keys;
2000#endif
2001 if (nc)
2002 vungetc(nc);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002003 got_int = FALSE; // CTRL-C typed after CTRL-V is not an interrupt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 return cc;
2005}
2006
2007/*
2008 * Insert character, taking care of special keys and mod_mask
2009 */
2010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002011insert_special(
2012 int c,
2013 int allow_modmask,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002014 int ctrlv) // c was typed after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 char_u *p;
2017 int len;
2018
2019 /*
2020 * Special function key, translate into "<Key>". Up to the last '>' is
2021 * inserted with ins_str(), so as not to replace characters in replace
2022 * mode.
2023 * Only use mod_mask for special keys, to avoid things like <S-Space>,
2024 * unless 'allow_modmask' is TRUE.
2025 */
Bram Moolenaard0573012017-10-28 21:11:06 +02002026#ifdef MACOS_X
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002027 // Command-key never produces a normal key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (mod_mask & MOD_MASK_CMD)
2029 allow_modmask = TRUE;
2030#endif
2031 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
2032 {
2033 p = get_special_key_name(c, mod_mask);
2034 len = (int)STRLEN(p);
2035 c = p[len - 1];
2036 if (len > 2)
2037 {
2038 if (stop_arrow() == FAIL)
2039 return;
2040 p[len - 1] = NUL;
2041 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002042 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 ctrlv = FALSE;
2044 }
2045 }
2046 if (stop_arrow() == OK)
2047 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
2048}
2049
2050/*
2051 * Special characters in this context are those that need processing other
2052 * than the simple insertion that can be performed here. This includes ESC
2053 * which terminates the insert, and CR/NL which need special processing to
2054 * open up a new line. This routine tries to optimize insertions performed by
2055 * the "redo", "undo" or "put" commands, so it needs to know when it should
2056 * stop and defer processing to the "normal" mechanism.
2057 * '0' and '^' are special, because they can be followed by CTRL-D.
2058 */
2059#ifdef EBCDIC
2060# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
2061#else
2062# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
2063#endif
2064
Bram Moolenaar13505972019-01-24 15:04:48 +01002065#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002067/*
2068 * "flags": INSCHAR_FORMAT - force formatting
2069 * INSCHAR_CTRLV - char typed just after CTRL-V
2070 * INSCHAR_NO_FEX - don't use 'formatexpr'
2071 *
2072 * NOTE: passes the flags value straight through to internal_format() which,
2073 * beside INSCHAR_FORMAT (above), is also looking for these:
2074 * INSCHAR_DO_COM - format comments
2075 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
2076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002078insertchar(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002079 int c, // character to insert or NUL
2080 int flags, // INSCHAR_FORMAT, etc.
2081 int second_indent) // indent for second line if >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 int textwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002086 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002088 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 /*
2092 * Try to break the line in two or more pieces when:
2093 * - Always do this if we have been called to do formatting only.
2094 * - Always do this when 'formatoptions' has the 'a' flag and the line
2095 * ends in white space.
2096 * - Otherwise:
2097 * - Don't do this if inserting a blank
2098 * - Don't do this if an existing character is being replaced, unless
2099 * we're in VREPLACE mode.
2100 * - Do this if the cursor is not on the line where insert started
2101 * or - 'formatoptions' doesn't have 'l' or the line was not too long
2102 * before the insert.
2103 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
2104 * before 'textwidth'
2105 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002106 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002107 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01002108 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 && *ml_get_cursor() != NUL)
2112 && (curwin->w_cursor.lnum != Insstart.lnum
2113 || ((!has_format_option(FO_INS_LONG)
2114 || Insstart_textlen <= (colnr_T)textwidth)
2115 && (!fo_ins_blank
2116 || Insstart_blank_vcol <= (colnr_T)textwidth
2117 ))))))
2118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002119 // Format with 'formatexpr' when it's set. Use internal formatting
2120 // when 'formatexpr' isn't set or it returns non-zero.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002121#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002122 int do_internal = TRUE;
2123 colnr_T virtcol = get_nolist_virtcol()
2124 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002125
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01002126 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
2127 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002128 {
2129 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002130 // It may be required to save for undo again, e.g. when setline()
2131 // was called.
Bram Moolenaarf3442e72006-10-10 13:49:10 +00002132 ins_need_undo = TRUE;
2133 }
2134 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00002136 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002139 if (c == NUL) // only formatting was wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 return;
2141
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002142 // Check whether this character should end a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (did_ai && (int)c == end_comment_pending)
2144 {
2145 char_u *line;
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002146 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 int middle_len, end_len;
2148 int i;
2149
2150 /*
2151 * Need to remove existing (middle) comment leader and insert end
2152 * comment leader. First, check what comment leader we can find.
2153 */
Bram Moolenaar81340392012-06-06 16:12:59 +02002154 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002155 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) // Just checking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002157 // Skip middle-comment string
2158 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 ++p;
2160 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002161 // Don't count trailing white space for middle_len
Bram Moolenaar1c465442017-03-12 20:10:05 +01002162 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 --middle_len;
2164
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002165 // Find the end-comment string
2166 while (*p && p[-1] != ':') // find end of end flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 ++p;
2168 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2169
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002170 // Skip white space before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002172 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 ;
2174 i++;
2175
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002176 // Skip to before the middle leader
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 i -= middle_len;
2178
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002179 // Check some expected things before we go on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
2181 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002182 // Backspace over all the stuff we want to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 backspace_until_column(i);
2184
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002185 // Insert the end-comment string, except for the last
2186 // character, which will get inserted as normal later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 ins_bytes_len(lead_end, end_len - 1);
2188 }
2189 }
2190 }
2191 end_comment_pending = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193 did_ai = FALSE;
2194#ifdef FEAT_SMARTINDENT
2195 did_si = FALSE;
2196 can_si = FALSE;
2197 can_si_back = FALSE;
2198#endif
2199
2200 /*
2201 * If there's any pending input, grab up to INPUT_BUFLEN at once.
2202 * This speeds up normal text input considerably.
2203 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
2204 * need to re-indent at a ':', or any other character (but not what
2205 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01002206 * Don't do this when there an InsertCharPre autocommand is defined,
2207 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02002208 * Do the check for InsertCharPre before the call to vpeekc() because the
2209 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 */
2211#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002212 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213#endif
2214
2215 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02002217 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 && vpeekc() != NUL
2219 && !(State & REPLACE_FLAG)
2220#ifdef FEAT_CINDENT
2221 && !cindent_on()
2222#endif
2223#ifdef FEAT_RIGHTLEFT
2224 && !p_ri
2225#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02002226 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228#define INPUT_BUFLEN 100
2229 char_u buf[INPUT_BUFLEN + 1];
2230 int i;
2231 colnr_T virtcol = 0;
2232
2233 buf[0] = c;
2234 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002235 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 virtcol = get_nolist_virtcol();
2237 /*
2238 * Stop the string when:
2239 * - no more chars available
2240 * - finding a special character (command key)
2241 * - buffer is full
2242 * - running into the 'textwidth' boundary
2243 * - need to check for abbreviation: A non-word char after a word-char
2244 */
2245 while ( (c = vpeekc()) != NUL
2246 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 && i < INPUT_BUFLEN
2249 && (textwidth == 0
2250 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
2251 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
2252 {
2253#ifdef FEAT_RIGHTLEFT
2254 c = vgetc();
2255 if (p_hkmap && KeyTyped)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002256 c = hkmap(c); // Hebrew mode mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 buf[i++] = c;
2258#else
2259 buf[i++] = vgetc();
2260#endif
2261 }
2262
2263#ifdef FEAT_DIGRAPHS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002264 do_digraph(-1); // clear digraphs
2265 do_digraph(buf[i-1]); // may be the start of a digraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266#endif
2267 buf[i] = NUL;
2268 ins_str(buf);
2269 if (flags & INSCHAR_CTRLV)
2270 {
2271 redo_literal(*buf);
2272 i = 1;
2273 }
2274 else
2275 i = 0;
2276 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002277 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279 else
2280 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002281 int cc;
2282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2284 {
2285 char_u buf[MB_MAXBYTES + 1];
2286
2287 (*mb_char2bytes)(c, buf);
2288 buf[cc] = NUL;
2289 ins_char_bytes(buf, cc);
2290 AppendCharToRedobuff(c);
2291 }
2292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 ins_char(c);
2295 if (flags & INSCHAR_CTRLV)
2296 redo_literal(c);
2297 else
2298 AppendCharToRedobuff(c);
2299 }
2300 }
2301}
2302
2303/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002304 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002305 *
2306 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
2307 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002308 */
2309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002310internal_format(
2311 int textwidth,
2312 int second_indent,
2313 int flags,
2314 int format_only,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002315 int c) // character to be inserted (can be NUL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002316{
2317 int cc;
2318 int save_char = NUL;
2319 int haveto_redraw = FALSE;
2320 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002321 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002322 int fo_white_par = has_format_option(FO_WHITE_PAR);
2323 int first_line = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002324 colnr_T leader_len;
2325 int no_leader = FALSE;
2326 int do_comments = (flags & INSCHAR_DO_COM);
Bram Moolenaar0026d472014-09-09 16:32:39 +02002327#ifdef FEAT_LINEBREAK
2328 int has_lbr = curwin->w_p_lbr;
2329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002330 // make sure win_lbr_chartabsize() counts correctly
Bram Moolenaar0026d472014-09-09 16:32:39 +02002331 curwin->w_p_lbr = FALSE;
2332#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002333
2334 /*
2335 * When 'ai' is off we don't want a space under the cursor to be
2336 * deleted. Replace it with an 'x' temporarily.
2337 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002338 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002339 {
2340 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01002341 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002342 {
2343 save_char = cc;
2344 pchar_cursor('x');
2345 }
2346 }
2347
2348 /*
2349 * Repeat breaking lines, until the current line is not too long.
2350 */
2351 while (!got_int)
2352 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002353 int startcol; // Cursor column at entry
2354 int wantcol; // column at textwidth border
2355 int foundcol; // column for start of spaces
2356 int end_foundcol = 0; // column for start of word
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002357 colnr_T len;
2358 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002359 int orig_col = 0;
2360 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002361 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002362 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002363 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002364
Bram Moolenaar97b98102009-11-17 16:41:01 +00002365 virtcol = get_nolist_virtcol()
2366 + char2cells(c != NUL ? c : gchar_cursor());
2367 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002368 break;
2369
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002370 if (no_leader)
2371 do_comments = FALSE;
2372 else if (!(flags & INSCHAR_FORMAT)
2373 && has_format_option(FO_WRAP_COMS))
2374 do_comments = TRUE;
2375
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002376 // Don't break until after the comment leader
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002377 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002378 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002379 else
2380 leader_len = 0;
2381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002382 // If the line doesn't start with a comment leader, then don't
2383 // start one in a following broken line. Avoids that a %word
2384 // moved to the start of the next line causes all following lines
2385 // to start with %.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002386 if (leader_len == 0)
2387 no_leader = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002388 if (!(flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002389 && leader_len == 0
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002390 && !has_format_option(FO_WRAP))
2391
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002392 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002393 if ((startcol = curwin->w_cursor.col) == 0)
2394 break;
2395
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002396 // find column of textwidth border
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002397 coladvance((colnr_T)textwidth);
2398 wantcol = curwin->w_cursor.col;
2399
Bram Moolenaar97b98102009-11-17 16:41:01 +00002400 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002401 foundcol = 0;
2402
2403 /*
2404 * Find position to break at.
2405 * Stop at first entered white when 'formatoptions' has 'v'
2406 */
2407 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02002408 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002409 || curwin->w_cursor.lnum != Insstart.lnum
2410 || curwin->w_cursor.col >= Insstart.col)
2411 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00002412 if (curwin->w_cursor.col == startcol && c != NUL)
2413 cc = c;
2414 else
2415 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002416 if (WHITECHAR(cc))
2417 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002418 // remember position of blank just before text
Bram Moolenaar97b98102009-11-17 16:41:01 +00002419 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002420
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002421 // find start of sequence of blanks
2422 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002423 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
2424 {
2425 dec_cursor();
2426 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002427
2428 // Increment count of how many whitespace chars in this
2429 // group; we only need to know if it's more than one.
2430 if (wcc < 2)
2431 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002432 }
2433 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002434 break; // only spaces in front of text
Bram Moolenaarc3c31582019-01-11 22:15:05 +01002435
2436 // Don't break after a period when 'formatoptions' has 'p' and
2437 // there are less than two spaces.
2438 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
2439 continue;
2440
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002441 // Don't break until after the comment leader
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002442 if (curwin->w_cursor.col < leader_len)
2443 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002444 if (has_format_option(FO_ONE_LETTER))
2445 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002446 // do not break after one-letter words
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002447 if (curwin->w_cursor.col == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002448 break; // one-letter word at begin
2449 // do not break "#a b" when 'tw' is 2
Bram Moolenaar97b98102009-11-17 16:41:01 +00002450 if (curwin->w_cursor.col <= leader_len)
2451 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002452 col = curwin->w_cursor.col;
2453 dec_cursor();
2454 cc = gchar_cursor();
2455
2456 if (WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002457 continue; // one-letter, continue
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002458 curwin->w_cursor.col = col;
2459 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002460
2461 inc_cursor();
2462
2463 end_foundcol = end_col + 1;
2464 foundcol = curwin->w_cursor.col;
2465 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002466 break;
2467 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00002468 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002470 // Break after or before a multi-byte character.
Bram Moolenaar97b98102009-11-17 16:41:01 +00002471 if (curwin->w_cursor.col != startcol)
2472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002473 // Don't break until after the comment leader
Bram Moolenaar97b98102009-11-17 16:41:01 +00002474 if (curwin->w_cursor.col < leader_len)
2475 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002476 col = curwin->w_cursor.col;
2477 inc_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002478 // Don't change end_foundcol if already set.
Bram Moolenaar97b98102009-11-17 16:41:01 +00002479 if (foundcol != curwin->w_cursor.col)
2480 {
2481 foundcol = curwin->w_cursor.col;
2482 end_foundcol = foundcol;
2483 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2484 break;
2485 }
2486 curwin->w_cursor.col = col;
2487 }
2488
2489 if (curwin->w_cursor.col == 0)
2490 break;
2491
2492 col = curwin->w_cursor.col;
2493
2494 dec_cursor();
2495 cc = gchar_cursor();
2496
2497 if (WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002498 continue; // break with space
2499 // Don't break until after the comment leader
Bram Moolenaar97b98102009-11-17 16:41:01 +00002500 if (curwin->w_cursor.col < leader_len)
2501 break;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002502
2503 curwin->w_cursor.col = col;
2504
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002505 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002506 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002507 if (curwin->w_cursor.col <= (colnr_T)wantcol)
2508 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002509 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002510 if (curwin->w_cursor.col == 0)
2511 break;
2512 dec_cursor();
2513 }
2514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002515 if (foundcol == 0) // no spaces, cannot break line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002516 {
2517 curwin->w_cursor.col = startcol;
2518 break;
2519 }
2520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002521 // Going to break the line, remove any "$" now.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002522 undisplay_dollar();
2523
2524 /*
2525 * Offset between cursor position and line break is used by replace
2526 * stack functions. VREPLACE does not use this, and backspaces
2527 * over the text instead.
2528 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002529 if (State & VREPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002530 orig_col = startcol; // Will start backspacing from here
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002531 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00002532 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002533
2534 /*
2535 * adjust startcol for spaces that will be deleted and
2536 * characters that will remain on top line
2537 */
2538 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002539 while ((cc = gchar_cursor(), WHITECHAR(cc))
2540 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002541 inc_cursor();
2542 startcol -= curwin->w_cursor.col;
2543 if (startcol < 0)
2544 startcol = 0;
2545
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002546 if (State & VREPLACE_FLAG)
2547 {
2548 /*
2549 * In VREPLACE mode, we will backspace over the text to be
2550 * wrapped, so save a copy now to put on the next line.
2551 */
2552 saved_text = vim_strsave(ml_get_cursor());
2553 curwin->w_cursor.col = orig_col;
2554 if (saved_text == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002555 break; // Can't do it, out of memory
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002556 saved_text[startcol] = NUL;
2557
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002558 // Backspace over characters that will move to the next line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002559 if (!fo_white_par)
2560 backspace_until_column(foundcol);
2561 }
2562 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002564 // put cursor after pos. to break line
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002565 if (!fo_white_par)
2566 curwin->w_cursor.col = foundcol;
2567 }
2568
2569 /*
2570 * Split the line just before the margin.
2571 * Only insert/delete lines, but don't really redraw the window.
2572 */
2573 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
2574 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002575 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002576 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002577 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
2578 if (!(flags & INSCHAR_COM_LIST))
2579 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002580
2581 replace_offset = 0;
2582 if (first_line)
2583 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002584 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002585 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002586 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002587 * This section is for auto-wrap of numeric lists. When not
2588 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
2589 * flag will be set and open_line() will handle it (as seen
2590 * above). The code here (and in get_number_indent()) will
2591 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002592 */
2593 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002594 second_indent =
2595 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002596 if (second_indent >= 0)
2597 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002598 if (State & VREPLACE_FLAG)
2599 change_indent(INDENT_SET, second_indent,
2600 FALSE, NUL, TRUE);
2601 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002602 if (leader_len > 0 && second_indent - leader_len > 0)
2603 {
2604 int i;
2605 int padding = second_indent - leader_len;
2606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002607 // We started at the first_line of a numbered list
2608 // that has a comment. the open_line() function has
2609 // inserted the proper comment leader and positioned
2610 // the cursor at the end of the split line. Now we
2611 // add the additional whitespace needed after the
2612 // comment leader for the numbered list.
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002613 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002614 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002615 }
2616 else
2617 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002618 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02002619 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002620 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002621 }
2622 first_line = FALSE;
2623 }
2624
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002625 if (State & VREPLACE_FLAG)
2626 {
2627 /*
2628 * In VREPLACE mode we have backspaced over the text to be
2629 * moved, now we re-insert it into the new line.
2630 */
2631 ins_bytes(saved_text);
2632 vim_free(saved_text);
2633 }
2634 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002635 {
2636 /*
2637 * Check if cursor is not past the NUL off the line, cindent
2638 * may have added or removed indent.
2639 */
2640 curwin->w_cursor.col += startcol;
2641 len = (colnr_T)STRLEN(ml_get_curline());
2642 if (curwin->w_cursor.col > len)
2643 curwin->w_cursor.col = len;
2644 }
2645
2646 haveto_redraw = TRUE;
2647#ifdef FEAT_CINDENT
2648 can_cindent = TRUE;
2649#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002650 // moved the cursor, don't autoindent or cindent now
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002651 did_ai = FALSE;
2652#ifdef FEAT_SMARTINDENT
2653 did_si = FALSE;
2654 can_si = FALSE;
2655 can_si_back = FALSE;
2656#endif
2657 line_breakcheck();
2658 }
2659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002660 if (save_char != NUL) // put back space after cursor
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002661 pchar_cursor(save_char);
2662
Bram Moolenaar0026d472014-09-09 16:32:39 +02002663#ifdef FEAT_LINEBREAK
2664 curwin->w_p_lbr = has_lbr;
2665#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002666 if (!format_only && haveto_redraw)
2667 {
2668 update_topline();
2669 redraw_curbuf_later(VALID);
2670 }
2671}
2672
2673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 * Called after inserting or deleting text: When 'formatoptions' includes the
2675 * 'a' flag format from the current line until the end of the paragraph.
2676 * Keep the cursor at the same position relative to the text.
2677 * The caller must have saved the cursor line for undo, following ones will be
2678 * saved here.
2679 */
2680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002681auto_format(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002682 int trailblank, // when TRUE also format with trailing blank
2683 int prev_line) // may start in previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
2685 pos_T pos;
2686 colnr_T len;
2687 char_u *old;
2688 char_u *new, *pnew;
2689 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002690 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 if (!has_format_option(FO_AUTO))
2693 return;
2694
2695 pos = curwin->w_cursor;
2696 old = ml_get_curline();
2697
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002698 // may remove added space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 check_auto_format(FALSE);
2700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002701 // Don't format in Insert mode when the cursor is on a trailing blank, the
2702 // user might insert normal text next. Also skip formatting when "1" is
2703 // in 'formatoptions' and there is a single character before the cursor.
2704 // Otherwise the line would be broken and when typing another non-white
2705 // next they are not joined back together.
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002706 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (*old != NUL && !trailblank && wasatend)
2708 {
2709 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002710 cc = gchar_cursor();
2711 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
2712 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002714 cc = gchar_cursor();
2715 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
2717 curwin->w_cursor = pos;
2718 return;
2719 }
2720 curwin->w_cursor = pos;
2721 }
2722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002723 // With the 'c' flag in 'formatoptions' and 't' missing: only format
2724 // comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002726 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 /*
2730 * May start formatting in a previous line, so that after "x" a word is
2731 * moved to the previous line if it fits there now. Only when this is not
2732 * the start of a paragraph.
2733 */
2734 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
2735 {
2736 --curwin->w_cursor.lnum;
2737 if (u_save_cursor() == FAIL)
2738 return;
2739 }
2740
2741 /*
2742 * Do the formatting and restore the cursor position. "saved_cursor" will
2743 * be adjusted for the text formatting.
2744 */
2745 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00002746 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 curwin->w_cursor = saved_cursor;
2748 saved_cursor.lnum = 0;
2749
2750 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002752 // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2754 coladvance((colnr_T)MAXCOL);
2755 }
2756 else
2757 check_cursor_col();
2758
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002759 // Insert mode: If the cursor is now after the end of the line while it
2760 // previously wasn't, the line was broken. Because of the rule above we
2761 // need to add a space when 'w' is in 'formatoptions' to keep a paragraph
2762 // formatted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (!wasatend && has_format_option(FO_WHITE_PAR))
2764 {
2765 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002766 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (curwin->w_cursor.col == len)
2768 {
2769 pnew = vim_strnsave(new, len + 2);
2770 pnew[len] = ' ';
2771 pnew[len + 1] = NUL;
2772 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002773 // remove the space later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 did_add_space = TRUE;
2775 }
2776 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002777 // may remove added space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 check_auto_format(FALSE);
2779 }
2780
2781 check_cursor();
2782}
2783
2784/*
2785 * When an extra space was added to continue a paragraph for auto-formatting,
2786 * delete it now. The space must be under the cursor, just after the insert
2787 * position.
2788 */
2789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002790check_auto_format(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002791 int end_insert) // TRUE when ending Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792{
2793 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002794 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
2796 if (did_add_space)
2797 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002798 cc = gchar_cursor();
2799 if (!WHITECHAR(cc))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002800 // Somehow the space was removed already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 did_add_space = FALSE;
2802 else
2803 {
2804 if (!end_insert)
2805 {
2806 inc_cursor();
2807 c = gchar_cursor();
2808 dec_cursor();
2809 }
2810 if (c != NUL)
2811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002812 // The space is no longer at the end of the line, delete it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 del_char(FALSE);
2814 did_add_space = FALSE;
2815 }
2816 }
2817 }
2818}
2819
2820/*
2821 * Find out textwidth to be used for formatting:
2822 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02002823 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 * if invalid value, use 0.
2825 * Set default to window width (maximum 79) for "gq" operator.
2826 */
2827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002828comp_textwidth(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002829 int ff) // force formatting (for "gq" command)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831 int textwidth;
2832
2833 textwidth = curbuf->b_p_tw;
2834 if (textwidth == 0 && curbuf->b_p_wm)
2835 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002836 // The width is the window width minus 'wrapmargin' minus all the
2837 // things that add to the margin.
Bram Moolenaar02631462017-09-22 15:20:32 +02002838 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839#ifdef FEAT_CMDWIN
2840 if (cmdwin_type != 0)
2841 textwidth -= 1;
2842#endif
2843#ifdef FEAT_FOLDING
2844 textwidth -= curwin->w_p_fdc;
2845#endif
2846#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002847 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 textwidth -= 1;
2849#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02002850 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 textwidth -= 8;
2852 }
2853 if (textwidth < 0)
2854 textwidth = 0;
2855 if (ff && textwidth == 0)
2856 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002857 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 if (textwidth > 79)
2859 textwidth = 79;
2860 }
2861 return textwidth;
2862}
2863
2864/*
2865 * Put a character in the redo buffer, for when just after a CTRL-V.
2866 */
2867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002868redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 char_u buf[10];
2871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002872 // Only digits need special treatment. Translate them into a string of
2873 // three digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (VIM_ISDIGIT(c))
2875 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002876 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 AppendToRedobuff(buf);
2878 }
2879 else
2880 AppendCharToRedobuff(c);
2881}
2882
2883/*
2884 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002885 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002888start_arrow(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002889 pos_T *end_insert_pos) // can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002891 start_arrow_common(end_insert_pos, TRUE);
2892}
2893
2894/*
2895 * Like start_arrow() but with end_change argument.
2896 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
2897 */
2898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002899start_arrow_with_change(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002900 pos_T *end_insert_pos, // can be NULL
2901 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002902{
2903 start_arrow_common(end_insert_pos, end_change);
2904 if (!end_change)
2905 {
2906 AppendCharToRedobuff(Ctrl_G);
2907 AppendCharToRedobuff('U');
2908 }
2909}
2910
2911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002912start_arrow_common(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002913 pos_T *end_insert_pos, // can be NULL
2914 int end_change) // end undoable change
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02002915{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002916 if (!arrow_used && end_change) // something has been inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
2918 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01002919 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002920 arrow_used = TRUE; // this means we stopped the current insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002922#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002923 check_spell_redraw();
2924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925}
2926
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002927#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002928/*
2929 * If we skipped highlighting word at cursor, do it now.
2930 * It may be skipped again, thus reset spell_redraw_lnum first.
2931 */
2932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002933check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002934{
2935 if (spell_redraw_lnum != 0)
2936 {
2937 linenr_T lnum = spell_redraw_lnum;
2938
2939 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002940 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00002941 }
2942}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002943
Bram Moolenaar217ad922005-03-20 22:37:15 +00002944#endif
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946/*
2947 * stop_arrow() is called before a change is made in insert mode.
2948 * If an arrow key has been used, start a new insertion.
2949 * Returns FAIL if undo is impossible, shouldn't insert then.
2950 */
2951 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002952stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
2954 if (arrow_used)
2955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002956 Insstart = curwin->w_cursor; // new insertion starts here
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002957 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002958 // Don't update the original insert position when moved to the
2959 // right, except when nothing was inserted yet.
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002960 update_Insstart_orig = FALSE;
2961 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
2962
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (u_save_cursor() == OK)
2964 {
2965 arrow_used = FALSE;
2966 ins_need_undo = FALSE;
2967 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02002968
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (State & VREPLACE_FLAG)
2971 {
2972 orig_line_count = curbuf->b_ml.ml_line_count;
2973 vr_lines_changed = 1;
2974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 ResetRedobuff();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002976 AppendToRedobuff((char_u *)"1i"); // pretend we start an insertion
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002977 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 }
2979 else if (ins_need_undo)
2980 {
2981 if (u_save_cursor() == OK)
2982 ins_need_undo = FALSE;
2983 }
2984
2985#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002986 // Always open fold at the cursor line when inserting something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 foldOpenCursor();
2988#endif
2989
2990 return (arrow_used || ins_need_undo ? FAIL : OK);
2991}
2992
2993/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002994 * Do a few things to stop inserting.
2995 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
2996 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 */
2998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002999stop_insert(
3000 pos_T *end_insert_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003001 int esc, // called by ins_esc()
3002 int nomove) // <c-\><c-o>, don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003004 int cc;
3005 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007 stop_redo_ins();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003008 replace_flush(); // abandon replace stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
3010 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003011 * Save the inserted text for later redo with ^@ and CTRL-A.
3012 * Don't do it when "restart_edit" was set and nothing was inserted,
3013 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003015 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00003016 if (did_restart_edit == 0 || (ptr != NULL
3017 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00003018 {
3019 vim_free(last_insert);
3020 last_insert = ptr;
3021 last_insert_skip = new_insert_skip;
3022 }
3023 else
3024 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003026 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003028 // Auto-format now. It may seem strange to do this when stopping an
3029 // insertion (or moving the cursor), but it's required when appending
3030 // a line and having it end in a space. But only do it when something
3031 // was actually inserted, otherwise undo won't work.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003032 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003034 pos_T tpos = curwin->w_cursor;
3035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003036 // When the cursor is at the end of the line after a space the
3037 // formatting will move it to the following word. Avoid that by
3038 // moving the cursor onto the space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 cc = 'x';
3040 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
3041 {
3042 dec_cursor();
3043 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003044 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003045 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047
3048 auto_format(TRUE, FALSE);
3049
Bram Moolenaar1c465442017-03-12 20:10:05 +01003050 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003051 {
3052 if (gchar_cursor() != NUL)
3053 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003054 // If the cursor is still at the same character, also keep
3055 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003056 if (gchar_cursor() == NUL
3057 && curwin->w_cursor.lnum == tpos.lnum
3058 && curwin->w_cursor.col == tpos.col)
3059 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
3062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003063 // If a space was inserted for auto-formatting, remove it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 check_auto_format(TRUE);
3065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003066 // If we just did an auto-indent, remove the white space from the end
3067 // of the line, and put the cursor back.
3068 // Do this when ESC was used or moving the cursor up/down.
3069 // Check for the old position still being valid, just in case the text
3070 // got changed unexpectedly.
Bram Moolenaarf332a652013-11-04 04:20:33 +01003071 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00003072 && curwin->w_cursor.lnum != end_insert_pos->lnum))
3073 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003075 pos_T tpos = curwin->w_cursor;
3076
3077 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003078 check_cursor_col(); // make sure it is not past the line
Bram Moolenaar39f05632006-03-19 22:15:26 +00003079 for (;;)
3080 {
3081 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
3082 --curwin->w_cursor.col;
3083 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003084 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003085 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00003086 if (del_char(TRUE) == FAIL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003087 break; // should not happen
Bram Moolenaar39f05632006-03-19 22:15:26 +00003088 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003089 if (curwin->w_cursor.lnum != tpos.lnum)
3090 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003091 else
3092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003093 // reset tpos, could have been invalidated in the loop above
Bram Moolenaaref6875b2014-11-12 18:59:25 +01003094 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003095 tpos.col++;
3096 if (cc != NUL && gchar_pos(&tpos) == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003097 ++curwin->w_cursor.col; // put cursor back on the NUL
Bram Moolenaar2f31e392014-10-31 19:20:36 +01003098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003100 // <C-S-Right> may have started Visual mode, adjust the position for
3101 // deleted characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
3103 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003104 int len = (int)STRLEN(ml_get_curline());
3105
3106 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003108 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
3111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113 }
3114 did_ai = FALSE;
3115#ifdef FEAT_SMARTINDENT
3116 did_si = FALSE;
3117 can_si = FALSE;
3118 can_si_back = FALSE;
3119#endif
3120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003121 // Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
3122 // now in a different buffer.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003123 if (end_insert_pos != NULL)
3124 {
3125 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01003126 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003127 curbuf->b_op_end = *end_insert_pos;
3128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129}
3130
3131/*
3132 * Set the last inserted text to a single character.
3133 * Used for the replace command.
3134 */
3135 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003136set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
3138 char_u *s;
3139
3140 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 if (last_insert != NULL)
3143 {
3144 s = last_insert;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003145 // Use the CTRL-V only when entering a special char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (c < ' ' || c == DEL)
3147 *s++ = Ctrl_V;
3148 s = add_char2buf(c, s);
3149 *s++ = ESC;
3150 *s++ = NUL;
3151 last_insert_skip = 0;
3152 }
3153}
3154
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003155#if defined(EXITFREE) || defined(PROTO)
3156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003157free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003158{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003159 VIM_CLEAR(last_insert);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003160}
3161#endif
3162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163/*
3164 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
3165 * and CSI. Handle multi-byte characters.
3166 * Returns a pointer to after the added bytes.
3167 */
3168 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003169add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003171 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 int i;
3173 int len;
3174
3175 len = (*mb_char2bytes)(c, temp);
3176 for (i = 0; i < len; ++i)
3177 {
3178 c = temp[i];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003179 // Need to escape K_SPECIAL and CSI like in the typeahead buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (c == K_SPECIAL)
3181 {
3182 *s++ = K_SPECIAL;
3183 *s++ = KS_SPECIAL;
3184 *s++ = KE_FILLER;
3185 }
3186#ifdef FEAT_GUI
3187 else if (c == CSI)
3188 {
3189 *s++ = CSI;
3190 *s++ = KS_EXTRA;
3191 *s++ = (int)KE_CSI;
3192 }
3193#endif
3194 else
3195 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 return s;
3198}
3199
3200/*
3201 * move cursor to start of line
3202 * if flags & BL_WHITE move to first non-white
3203 * if flags & BL_SOL move to first non-white if startofline is set,
3204 * otherwise keep "curswant" column
3205 * if flags & BL_FIX don't leave the cursor on a NUL.
3206 */
3207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003208beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 if ((flags & BL_SOL) && !p_sol)
3211 coladvance(curwin->w_curswant);
3212 else
3213 {
3214 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
3217 if (flags & (BL_WHITE | BL_SOL))
3218 {
3219 char_u *ptr;
3220
Bram Moolenaar1c465442017-03-12 20:10:05 +01003221 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
3223 ++curwin->w_cursor.col;
3224 }
3225 curwin->w_set_curswant = TRUE;
3226 }
3227}
3228
3229/*
3230 * oneright oneleft cursor_down cursor_up
3231 *
3232 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003233 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 * Return OK when successful, FAIL when we hit a line of file boundary.
3235 */
3236
3237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
3240 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (virtual_active())
3244 {
3245 pos_T prevpos = curwin->w_cursor;
3246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003247 // Adjust for multi-wide char (excluding TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003249 coladvance(getviscol() + ((*ptr != TAB
3250 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 ? ptr2cells(ptr) : 1));
3252 curwin->w_set_curswant = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003253 // Return OK if the cursor moved, FAIL otherwise (at window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 return (prevpos.col != curwin->w_cursor.col
3255 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
3256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003259 if (*ptr == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003260 return FAIL; // already at the very end
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003261
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003262 if (has_mbyte)
3263 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003265 l = 1;
3266
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003267 // move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
3268 // contains "onemore".
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003269 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003270 return FAIL;
3271 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
3273 curwin->w_set_curswant = TRUE;
3274 return OK;
3275}
3276
3277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003278oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (virtual_active())
3281 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003282#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 int v = getviscol();
3286
3287 if (v == 0)
3288 return FAIL;
3289
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003290#ifdef FEAT_LINEBREAK
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003291 // We might get stuck on 'showbreak', skip over it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 width = 1;
3293 for (;;)
3294 {
3295 coladvance(v - width);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003296 // getviscol() is slow, skip it when 'showbreak' is empty,
3297 // 'breakindent' is not set and there are no multi-byte
3298 // characters
Bram Moolenaaree857022019-11-09 23:26:40 +01003299 if ((*get_showbreak_value(curwin) == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01003300 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 break;
3302 ++width;
3303 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003304#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
3308 if (curwin->w_cursor.coladd == 1)
3309 {
3310 char_u *ptr;
3311
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003312 // Adjust for multi-wide char (not a TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01003314 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
3315 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 curwin->w_cursor.coladd = 0;
3317 }
3318
3319 curwin->w_set_curswant = TRUE;
3320 return OK;
3321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323 if (curwin->w_cursor.col == 0)
3324 return FAIL;
3325
3326 curwin->w_set_curswant = TRUE;
3327 --curwin->w_cursor.col;
3328
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003329 // if the character on the left of the current cursor is a multi-byte
3330 // character, move to its first byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (has_mbyte)
3332 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 return OK;
3334}
3335
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337cursor_up(
3338 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003339 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 linenr_T lnum;
3342
3343 if (n > 0)
3344 {
3345 lnum = curwin->w_cursor.lnum;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003346 // This fails if the cursor is already in the first line or the count
3347 // is larger than the line number and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00003348 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 return FAIL;
3350 if (n >= lnum)
3351 lnum = 1;
3352 else
3353#ifdef FEAT_FOLDING
3354 if (hasAnyFolding(curwin))
3355 {
3356 /*
3357 * Count each sequence of folded lines as one logical line.
3358 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003359 // go to the start of the current fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 (void)hasFolding(lnum, &lnum, NULL);
3361
3362 while (n--)
3363 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003364 // move up one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 --lnum;
3366 if (lnum <= 1)
3367 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003368 // If we entered a fold, move to the beginning, unless in
3369 // Insert mode or when 'foldopen' contains "all": it will open
3370 // in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
3372 (void)hasFolding(lnum, &lnum, NULL);
3373 }
3374 if (lnum < 1)
3375 lnum = 1;
3376 }
3377 else
3378#endif
3379 lnum -= n;
3380 curwin->w_cursor.lnum = lnum;
3381 }
3382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003383 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 coladvance(curwin->w_curswant);
3385
3386 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003387 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
3389 return OK;
3390}
3391
3392/*
3393 * Cursor down a number of logical lines.
3394 */
3395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003396cursor_down(
3397 long n,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003398 int upd_topline) // When TRUE: update topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399{
3400 linenr_T lnum;
3401
3402 if (n > 0)
3403 {
3404 lnum = curwin->w_cursor.lnum;
3405#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003406 // Move to last line of fold, will fail if it's the end-of-file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 (void)hasFolding(lnum, NULL, &lnum);
3408#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003409 // This fails if the cursor is already in the last line or would move
3410 // beyond the last line and '-' is in 'cpoptions'
Bram Moolenaar7c626922005-02-07 22:01:03 +00003411 if (lnum >= curbuf->b_ml.ml_line_count
3412 || (lnum + n > curbuf->b_ml.ml_line_count
3413 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 return FAIL;
3415 if (lnum + n >= curbuf->b_ml.ml_line_count)
3416 lnum = curbuf->b_ml.ml_line_count;
3417 else
3418#ifdef FEAT_FOLDING
3419 if (hasAnyFolding(curwin))
3420 {
3421 linenr_T last;
3422
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003423 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 while (n--)
3425 {
3426 if (hasFolding(lnum, NULL, &last))
3427 lnum = last + 1;
3428 else
3429 ++lnum;
3430 if (lnum >= curbuf->b_ml.ml_line_count)
3431 break;
3432 }
3433 if (lnum > curbuf->b_ml.ml_line_count)
3434 lnum = curbuf->b_ml.ml_line_count;
3435 }
3436 else
3437#endif
3438 lnum += n;
3439 curwin->w_cursor.lnum = lnum;
3440 }
3441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003442 // try to advance to the column we want to be at
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 coladvance(curwin->w_curswant);
3444
3445 if (upd_topline)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003446 update_topline(); // make sure curwin->w_topline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 return OK;
3449}
3450
3451/*
3452 * Stuff the last inserted text in the read buffer.
3453 * Last_insert actually is a copy of the redo buffer, so we
3454 * first have to remove the command.
3455 */
3456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003457stuff_inserted(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003458 int c, // Command character to be inserted
3459 long count, // Repeat this many times
3460 int no_esc) // Don't add an ESC at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 char_u *esc_ptr;
3463 char_u *ptr;
3464 char_u *last_ptr;
3465 char_u last = NUL;
3466
3467 ptr = get_last_insert();
3468 if (ptr == NULL)
3469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003470 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 return FAIL;
3472 }
3473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003474 // may want to stuff the command character, to start Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (c != NUL)
3476 stuffcharReadbuff(c);
3477 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003478 *esc_ptr = NUL; // remove the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003480 // when the last char is either "0" or "^" it will be quoted if no ESC
3481 // comes after it OR if it will inserted more than once and "ptr"
3482 // starts with ^D. -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
3484 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
3485 && (no_esc || (*ptr == Ctrl_D && count > 1)))
3486 {
3487 last = *last_ptr;
3488 *last_ptr = NUL;
3489 }
3490
3491 do
3492 {
3493 stuffReadbuff(ptr);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003494 // a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (last)
3496 stuffReadbuff((char_u *)(last == '0'
3497 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
3498 : IF_EB("\026^", CTRL_V_STR "^")));
3499 }
3500 while (--count > 0);
3501
3502 if (last)
3503 *last_ptr = last;
3504
3505 if (esc_ptr != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003506 *esc_ptr = ESC; // put the ESC back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003508 // may want to stuff a trailing ESC, to get out of Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if (!no_esc)
3510 stuffcharReadbuff(ESC);
3511
3512 return OK;
3513}
3514
3515 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003516get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
3518 if (last_insert == NULL)
3519 return NULL;
3520 return last_insert + last_insert_skip;
3521}
3522
3523/*
3524 * Get last inserted string, and remove trailing <Esc>.
3525 * Returns pointer to allocated memory (must be freed) or NULL.
3526 */
3527 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003528get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
3530 char_u *s;
3531 int len;
3532
3533 if (last_insert == NULL)
3534 return NULL;
3535 s = vim_strsave(last_insert + last_insert_skip);
3536 if (s != NULL)
3537 {
3538 len = (int)STRLEN(s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003539 if (len > 0 && s[len - 1] == ESC) // remove trailing ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 s[len - 1] = NUL;
3541 }
3542 return s;
3543}
3544
3545/*
3546 * Check the word in front of the cursor for an abbreviation.
3547 * Called when the non-id character "c" has been entered.
3548 * When an abbreviation is recognized it is removed from the text and
3549 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
3550 */
3551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003552echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003554 // Don't check for abbreviation in paste mode, when disabled and just
3555 // after moving around with cursor keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (p_paste || no_abbr || arrow_used)
3557 return FALSE;
3558
3559 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
3560 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
3561}
3562
3563/*
3564 * replace-stack functions
3565 *
3566 * When replacing characters, the replaced characters are remembered for each
3567 * new character. This is used to re-insert the old text when backspacing.
3568 *
3569 * There is a NUL headed list of characters for each character that is
3570 * currently in the file after the insertion point. When BS is used, one NUL
3571 * headed list is put back for the deleted character.
3572 *
3573 * For a newline, there are two NUL headed lists. One contains the characters
3574 * that the NL replaced. The extra one stores the characters after the cursor
3575 * that were deleted (always white space).
3576 *
3577 * Replace_offset is normally 0, in which case replace_push will add a new
3578 * character at the end of the stack. If replace_offset is not 0, that many
3579 * characters will be left on the stack above the newly inserted character.
3580 */
3581
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003582static char_u *replace_stack = NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003583static long replace_stack_nr = 0; // next entry in replace stack
3584static long replace_stack_len = 0; // max. number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587replace_push(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003588 int c) // character that is replaced (NUL is none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
3590 char_u *p;
3591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003592 if (replace_stack_nr < replace_offset) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 return;
3594 if (replace_stack_len <= replace_stack_nr)
3595 {
3596 replace_stack_len += 50;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003597 p = ALLOC_MULT(char_u, replace_stack_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003598 if (p == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 replace_stack_len -= 50;
3601 return;
3602 }
3603 if (replace_stack != NULL)
3604 {
3605 mch_memmove(p, replace_stack,
3606 (size_t)(replace_stack_nr * sizeof(char_u)));
3607 vim_free(replace_stack);
3608 }
3609 replace_stack = p;
3610 }
3611 p = replace_stack + replace_stack_nr - replace_offset;
3612 if (replace_offset)
3613 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
3614 *p = c;
3615 ++replace_stack_nr;
3616}
3617
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003618/*
3619 * Push a character onto the replace stack. Handles a multi-byte character in
3620 * reverse byte order, so that the first byte is popped off first.
3621 * Return the number of bytes done (includes composing characters).
3622 */
3623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003624replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003625{
3626 int l = (*mb_ptr2len)(p);
3627 int j;
3628
3629 for (j = l - 1; j >= 0; --j)
3630 replace_push(p[j]);
3631 return l;
3632}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00003633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634/*
3635 * Pop one item from the replace stack.
3636 * return -1 if stack empty
3637 * return replaced character or NUL otherwise
3638 */
3639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003640replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 if (replace_stack_nr == 0)
3643 return -1;
3644 return (int)replace_stack[--replace_stack_nr];
3645}
3646
3647/*
3648 * Join the top two items on the replace stack. This removes to "off"'th NUL
3649 * encountered.
3650 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003651 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003652replace_join(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003653 int off) // offset for which NUL to remove
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654{
3655 int i;
3656
3657 for (i = replace_stack_nr; --i >= 0; )
3658 if (replace_stack[i] == NUL && off-- <= 0)
3659 {
3660 --replace_stack_nr;
3661 mch_memmove(replace_stack + i, replace_stack + i + 1,
3662 (size_t)(replace_stack_nr - i));
3663 return;
3664 }
3665}
3666
3667/*
3668 * Pop bytes from the replace stack until a NUL is found, and insert them
3669 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
3670 */
3671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 int cc;
3675 int oldState = State;
3676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003677 State = NORMAL; // don't want REPLACE here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 while ((cc = replace_pop()) > 0)
3679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 dec_cursor();
3682 }
3683 State = oldState;
3684}
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686/*
3687 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
3688 * indicates a multi-byte char, pop the other bytes too.
3689 */
3690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003691mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692{
3693 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02003694 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 int i;
3696 int c;
3697
3698 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
3699 {
3700 buf[0] = cc;
3701 for (i = 1; i < n; ++i)
3702 buf[i] = replace_pop();
3703 ins_bytes_len(buf, n);
3704 }
3705 else
3706 ins_char(cc);
3707
3708 if (enc_utf8)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003709 // Handle composing chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 for (;;)
3711 {
3712 c = replace_pop();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003713 if (c == -1) // stack empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 break;
3715 if ((n = MB_BYTE2LEN(c)) == 1)
3716 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003717 // Not a multi-byte char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 replace_push(c);
3719 break;
3720 }
3721 else
3722 {
3723 buf[0] = c;
3724 for (i = 1; i < n; ++i)
3725 buf[i] = replace_pop();
3726 if (utf_iscomposing(utf_ptr2char(buf)))
3727 ins_bytes_len(buf, n);
3728 else
3729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003730 // Not a composing char, put it back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 for (i = n - 1; i >= 0; --i)
3732 replace_push(buf[i]);
3733 break;
3734 }
3735 }
3736 }
3737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
3739/*
3740 * make the replace stack empty
3741 * (called when exiting replace mode)
3742 */
3743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003744replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
Bram Moolenaard23a8232018-02-10 18:45:26 +01003746 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 replace_stack_len = 0;
3748 replace_stack_nr = 0;
3749}
3750
3751/*
3752 * Handle doing a BS for one character.
3753 * cc < 0: replace stack empty, just move cursor
3754 * cc == 0: character was inserted, delete it
3755 * cc > 0: character was replaced, put cc (first byte of original char) back
3756 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003757 * When "limit_col" is >= 0, don't delete before this column. Matters when
3758 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 */
3760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003761replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
3763 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 int orig_len = 0;
3765 int ins_len;
3766 int orig_vcols = 0;
3767 colnr_T start_vcol;
3768 char_u *p;
3769 int i;
3770 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 cc = replace_pop();
3773 if (cc > 0)
3774 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003775#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01003776 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01003777
3778 if (curbuf->b_has_textprop)
3779 {
3780 // Do not adjust text properties for individual delete and insert
3781 // operations, do it afterwards on the resulting text.
3782 len_before = STRLEN(ml_get_curline());
3783 ++text_prop_frozen;
3784 }
3785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 if (State & VREPLACE_FLAG)
3787 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003788 // Get the number of screen cells used by the character we are
3789 // going to delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
3791 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
3792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (has_mbyte)
3794 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003795 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003797 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 replace_push(cc);
3799 }
3800 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
3802 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003804 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806 replace_pop_ins();
3807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (State & VREPLACE_FLAG)
3809 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003810 // Get the number of screen cells used by the inserted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003812 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 vcol = start_vcol;
3814 for (i = 0; i < ins_len; ++i)
3815 {
3816 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003817 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 vcol -= start_vcol;
3820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003821 // Delete spaces that were inserted after the cursor to keep the
3822 // text aligned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 curwin->w_cursor.col += ins_len;
3824 while (vcol > orig_vcols && gchar_cursor() == ' ')
3825 {
3826 del_char(FALSE);
3827 ++orig_vcols;
3828 }
3829 curwin->w_cursor.col -= ins_len;
3830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
Bram Moolenaar196d1572019-01-02 23:47:18 +01003832 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003834
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003835#ifdef FEAT_PROP_POPUP
Bram Moolenaar196d1572019-01-02 23:47:18 +01003836 if (curbuf->b_has_textprop)
3837 {
3838 size_t len_now = STRLEN(ml_get_curline());
3839
3840 --text_prop_frozen;
3841 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02003842 (int)(len_now - len_before), 0);
Bram Moolenaar196d1572019-01-02 23:47:18 +01003843 }
3844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003847 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848}
3849
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3851/*
3852 * Map Hebrew keyboard when in hkmap mode.
3853 */
3854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003855hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003857 if (p_hkmapp) // phonetic mapping, by Ilya Dogolazky
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
3859 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
3860 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
3861 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
3862 static char_u map[26] =
3863 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
3864 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
3865 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
3866 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
3867 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
3868 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
3869 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
3870 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
3871 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
3872
3873 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
3874 return (int)(map[CharOrd(c)] - 1 + p_aleph);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003875 // '-1'='sofit'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 else if (c == 'x')
3877 return 'X';
3878 else if (c == 'q')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003879 return '\''; // {geresh}={'}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 else if (c == 246)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 return ' '; // \"o --> ' ' for a german keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 else if (c == 228)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003883 return ' '; // \"a --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 else if (c == 252)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003885 return ' '; // \"u --> ' ' -- / --
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886#ifdef EBCDIC
3887 else if (islower(c))
3888#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003889 // NOTE: islower() does not do the right thing for us on Linux so we
3890 // do this the same was as 5.7 and previous, so it works correctly on
3891 // all systems. Specifically, the e.g. Delete and Arrow keys are
3892 // munged and won't work if e.g. searching for Hebrew text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 else if (c >= 'a' && c <= 'z')
3894#endif
3895 return (int)(map[CharOrdLow(c)] + p_aleph);
3896 else
3897 return c;
3898 }
3899 else
3900 {
3901 switch (c)
3902 {
3903 case '`': return ';';
3904 case '/': return '.';
3905 case '\'': return ',';
3906 case 'q': return '/';
3907 case 'w': return '\'';
3908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 // Hebrew letters - set offset from 'a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 case ',': c = '{'; break;
3911 case '.': c = 'v'; break;
3912 case ';': c = 't'; break;
3913 default: {
3914 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
3915
3916#ifdef EBCDIC
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003917 // see note about islower() above
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if (!islower(c))
3919#else
3920 if (c < 'a' || c > 'z')
3921#endif
3922 return c;
3923 c = str[CharOrdLow(c)];
3924 break;
3925 }
3926 }
3927
3928 return (int)(CharOrdLow(c) + p_aleph);
3929 }
3930}
3931#endif
3932
3933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003934ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
3936 int need_redraw = FALSE;
3937 int regname;
3938 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003939 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
3941 /*
3942 * If we are going to wait for a character, show a '"'.
3943 */
3944 pc_status = PC_STATUS_UNSET;
3945 if (redrawing() && !char_avail())
3946 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003947 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00003948 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950 edit_putchar('"', TRUE);
3951#ifdef FEAT_CMDL_INFO
3952 add_to_showcmd_c(Ctrl_R);
3953#endif
3954 }
3955
3956#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003957 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#endif
3959
3960 /*
3961 * Don't map the register name. This also prevents the mode message to be
3962 * deleted when ESC is hit.
3963 */
3964 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003965 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003966 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
3969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003970 // Get a third key for literal register insertion
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 literally = regname;
3972#ifdef FEAT_CMDL_INFO
3973 add_to_showcmd_c(literally);
3974#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003975 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01003979 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981#ifdef FEAT_EVAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003982 // Don't call u_sync() while typing the expression or giving an error
3983 // message for it. Only call it explicitly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 ++no_u_sync;
3985 if (regname == '=')
3986 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003987 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003988# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00003990# endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003991 // Sync undo when evaluating the expression calls setline() or
3992 // append(), so that it can be undone separately.
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02003993 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02003994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003996
3997 // Cursor may be moved back a column.
3998 curwin->w_cursor = curpos;
3999 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004000# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004001 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (im_on)
4003 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00004006 if (regname == NUL || !valid_yank_reg(regname, FALSE))
4007 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004008 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004009 need_redraw = TRUE; // remove the '"'
Bram Moolenaar677ee682005-01-27 14:41:15 +00004010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 else
4012 {
4013#endif
4014 if (literally == Ctrl_O || literally == Ctrl_P)
4015 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004016 // Append the command to the redo buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 AppendCharToRedobuff(Ctrl_R);
4018 AppendCharToRedobuff(literally);
4019 AppendCharToRedobuff(regname);
4020
4021 do_put(regname, BACKWARD, 1L,
4022 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
4023 }
4024 else if (insert_reg(regname, literally) == FAIL)
4025 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004026 vim_beep(BO_REG);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004027 need_redraw = TRUE; // remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004029 else if (stop_insert_mode)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004030 // When the '=' register was used and a function was invoked that
4031 // did ":stopinsert" then stuff_empty() returns FALSE but we won't
4032 // insert anything, need to remove the '"'
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004033 need_redraw = TRUE;
4034
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035#ifdef FEAT_EVAL
4036 }
4037 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02004038 if (u_sync_once == 1)
4039 ins_need_undo = TRUE;
4040 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041#endif
4042#ifdef FEAT_CMDL_INFO
4043 clear_showcmd();
4044#endif
4045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004046 // If the inserted register is empty, we need to remove the '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (need_redraw || stuff_empty())
4048 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004050 // Disallow starting Visual mode here, would get a weird mode.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004051 if (!vis_active && VIsual_active)
4052 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053}
4054
4055/*
4056 * CTRL-G commands in Insert mode.
4057 */
4058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004059ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
4061 int c;
4062
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004063 // Right after CTRL-X the cursor will be after the ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 /*
4067 * Don't map the second key. This also prevents the mode message to be
4068 * deleted when ESC is hit.
4069 */
4070 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004071 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004072 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01004074 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 switch (c)
4076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004077 // CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 case K_UP:
4079 case Ctrl_K:
4080 case 'k': ins_up(TRUE);
4081 break;
4082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004083 // CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 case K_DOWN:
4085 case Ctrl_J:
4086 case 'j': ins_down(TRUE);
4087 break;
4088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004089 // CTRL-G u: start new undoable edit
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004090 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004093 // Need to reset Insstart, esp. because a BS that joins
4094 // a line to the previous one must save for undo.
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01004095 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004096 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 break;
4098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004099 // CTRL-G U: do not break undo with the next char
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004100 case 'U':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 // Allow one left/right cursor movement with the next char,
4102 // without breaking undo.
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02004103 dont_sync_undo = MAYBE;
4104 break;
4105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004106 // Unknown CTRL-G command, reserved for future expansion.
Bram Moolenaar165bc692015-07-21 17:53:25 +02004107 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109}
4110
4111/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004112 * CTRL-^ in Insert mode.
4113 */
4114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004115ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004116{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004117 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004119 // ":lmap" mappings exists, Toggle use of ":lmap" mappings.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120 if (State & LANGMAP)
4121 {
4122 curbuf->b_p_iminsert = B_IMODE_NONE;
4123 State &= ~LANGMAP;
4124 }
4125 else
4126 {
4127 curbuf->b_p_iminsert = B_IMODE_LMAP;
4128 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004129#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 im_set_active(FALSE);
4131#endif
4132 }
4133 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004134#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004135 else
4136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004137 // There are no ":lmap" mappings, toggle IM
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004138 if (im_get_status())
4139 {
4140 curbuf->b_p_iminsert = B_IMODE_NONE;
4141 im_set_active(FALSE);
4142 }
4143 else
4144 {
4145 curbuf->b_p_iminsert = B_IMODE_IM;
4146 State &= ~LANGMAP;
4147 im_set_active(TRUE);
4148 }
4149 }
4150#endif
4151 set_iminsert_global();
4152 showmode();
4153#ifdef FEAT_GUI
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004154 // may show different cursor shape or color
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004155 if (gui.in_use)
4156 gui_update_cursor(TRUE, FALSE);
4157#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004158#if defined(FEAT_KEYMAP)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004159 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004160 status_redraw_curbuf();
4161#endif
4162}
4163
4164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * Handle ESC in insert mode.
4166 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
4167 * insert.
4168 */
4169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004170ins_esc(
4171 long *count,
4172 int cmdchar,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004173 int nomove) // don't move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
4175 int temp;
4176 static int disabled_redraw = FALSE;
4177
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004178#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004179 check_spell_redraw();
4180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
4182 temp = curwin->w_cursor.col;
4183 if (disabled_redraw)
4184 {
4185 --RedrawingDisabled;
4186 disabled_redraw = FALSE;
4187 }
4188 if (!arrow_used)
4189 {
4190 /*
4191 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00004192 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
4193 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 */
4195 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00004196 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 /*
4199 * Repeating insert may take a long time. Check for
4200 * interrupt now and then.
4201 */
4202 if (*count > 0)
4203 {
4204 line_breakcheck();
4205 if (got_int)
4206 *count = 0;
4207 }
4208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004209 if (--*count > 0) // repeat what was typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004211 // Vi repeats the insert without replacing characters.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004212 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
4213 State &= ~REPLACE_FLAG;
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 (void)start_redo_ins();
4216 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004217 stuffRedoReadbuff(ESC_STR); // no ESC in redo buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 ++RedrawingDisabled;
4219 disabled_redraw = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004220 return FALSE; // repeat the insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01004222 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 undisplay_dollar();
4224 }
4225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004226 // When an autoindent was removed, curswant stays after the
4227 // indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
4229 curwin->w_set_curswant = TRUE;
4230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004231 // Remember the last Insert position in the '^ mark.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 if (!cmdmod.keepjumps)
4233 curbuf->b_last_insert = curwin->w_cursor;
4234
4235 /*
4236 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00004237 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00004239 if (!nomove
4240 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004241 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004242 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004243 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244#ifdef FEAT_RIGHTLEFT
4245 && !revins_on
4246#endif
4247 )
4248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
4250 {
4251 oneleft();
4252 if (restart_edit != NUL)
4253 ++curwin->w_cursor.coladd;
4254 }
4255 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
4257 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004258 // Correct cursor for multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (has_mbyte)
4260 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 }
4263
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004264#ifdef HAVE_INPUT_METHOD
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004265 // Disable IM to allow typing English directly for Normal mode commands.
4266 // When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
4267 // well).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 if (!(State & LANGMAP))
4269 im_save_status(&curbuf->b_p_iminsert);
4270 im_set_active(FALSE);
4271#endif
4272
4273 State = NORMAL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004274 // need to position cursor again (e.g. when on a TAB )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 changed_cline_bef_curs();
4276
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278#ifdef CURSOR_SHAPE
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004279 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004281 if (!p_ek)
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004282 {
4283 // Re-enable bracketed paste mode.
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01004284 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004286 // Re-enable modifyOtherKeys.
4287 out_str(T_CTI);
4288 }
4289
Bram Moolenaarad3ec762019-04-21 00:00:13 +02004290 // When recording or for CTRL-O, need to display the new mode.
4291 // Otherwise remove the mode message.
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004292 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 showmode();
Bram Moolenaarabc7c7f2019-04-20 15:10:13 +02004294 else if (p_smd && (got_int || !skip_showmode()))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004295 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar177c9f22019-11-06 13:59:16 +01004297 return TRUE; // exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298}
4299
4300#ifdef FEAT_RIGHTLEFT
4301/*
4302 * Toggle language: hkmap and revins_on.
4303 * Move to end of reverse inserted text.
4304 */
4305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004306ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
4308 if (revins_on && revins_chars && revins_scol >= 0)
4309 {
4310 while (gchar_cursor() != NUL && revins_chars--)
4311 ++curwin->w_cursor.col;
4312 }
4313 p_ri = !p_ri;
4314 revins_on = (State == INSERT && p_ri);
4315 if (revins_on)
4316 {
4317 revins_scol = curwin->w_cursor.col;
4318 revins_legal++;
4319 revins_chars = 0;
4320 undisplay_dollar();
4321 }
4322 else
4323 revins_scol = -1;
Bram Moolenaar14184a32019-02-16 15:10:30 +01004324 p_hkmap = curwin->w_p_rl ^ p_ri; // be consistent!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 showmode();
4326}
4327#endif
4328
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329/*
4330 * If 'keymodel' contains "startsel", may start selection.
4331 * Returns TRUE when a CTRL-O and other keys stuffed.
4332 */
4333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004334ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
4336 if (km_startsel)
4337 switch (c)
4338 {
4339 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 case K_PAGEUP:
4342 case K_KPAGEUP:
4343 case K_PAGEDOWN:
4344 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02004345# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 case K_LEFT:
4347 case K_RIGHT:
4348 case K_UP:
4349 case K_DOWN:
4350 case K_END:
4351 case K_HOME:
4352# endif
4353 if (!(mod_mask & MOD_MASK_SHIFT))
4354 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004355 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 case K_S_LEFT:
4357 case K_S_RIGHT:
4358 case K_S_UP:
4359 case K_S_DOWN:
4360 case K_S_END:
4361 case K_S_HOME:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004362 // Start selection right away, the cursor can move with
4363 // CTRL-O when beyond the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 start_selection();
4365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004366 // Execute the key in (insert) Select mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 stuffcharReadbuff(Ctrl_O);
4368 if (mod_mask)
4369 {
4370 char_u buf[4];
4371
4372 buf[0] = K_SPECIAL;
4373 buf[1] = KS_MODIFIER;
4374 buf[2] = mod_mask;
4375 buf[3] = NUL;
4376 stuffReadbuff(buf);
4377 }
4378 stuffcharReadbuff(c);
4379 return TRUE;
4380 }
4381 return FALSE;
4382}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
4384/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004385 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386 */
4387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004388ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389{
Bram Moolenaar14184a32019-02-16 15:10:30 +01004390#ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004392 (char_u *)((State & REPLACE_FLAG) ? "i"
4393 : replaceState == VREPLACE ? "v"
4394 : "r"), 1);
Bram Moolenaar14184a32019-02-16 15:10:30 +01004395#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004396 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004397 if (State & REPLACE_FLAG)
4398 State = INSERT | (State & LANGMAP);
4399 else
4400 State = replaceState | (State & LANGMAP);
4401 AppendCharToRedobuff(K_INS);
4402 showmode();
4403#ifdef CURSOR_SHAPE
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004404 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405#endif
4406}
4407
4408/*
4409 * Pressed CTRL-O in Insert mode.
4410 */
4411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004412ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004414 if (State & VREPLACE_FLAG)
4415 restart_edit = 'V';
4416 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 if (State & REPLACE_FLAG)
4418 restart_edit = 'R';
4419 else
4420 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 if (virtual_active())
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004422 ins_at_eol = FALSE; // cursor always keeps its column
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 ins_at_eol = (gchar_cursor() == NUL);
4425}
4426
4427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 * If the cursor is on an indent, ^T/^D insert/delete one
4429 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004430 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 * with vi. But vi only supports ^T and ^D after an
4432 * autoindent, we support it everywhere.
4433 */
4434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004435ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436{
4437 if (stop_arrow() == FAIL)
4438 return;
4439 AppendCharToRedobuff(c);
4440
4441 /*
4442 * 0^D and ^^D: remove all indent.
4443 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00004444 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
4445 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
4447 --curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004448 (void)del_char(FALSE); // delete the '^' or '0'
4449 // In Replace mode, restore the characters that '^' or '0' replaced.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 if (State & REPLACE_FLAG)
4451 replace_pop_ins();
4452 if (lastc == '^')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004453 old_indent = get_indent(); // remember curr. indent
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004454 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00004457 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
4459 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
4460 did_ai = FALSE;
4461#ifdef FEAT_SMARTINDENT
4462 did_si = FALSE;
4463 can_si = FALSE;
4464 can_si_back = FALSE;
4465#endif
4466#ifdef FEAT_CINDENT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004467 can_cindent = FALSE; // no cindenting after ^D or ^T
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468#endif
4469}
4470
4471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004472ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473{
4474 int temp;
4475
4476 if (stop_arrow() == FAIL)
4477 return;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 if (gchar_cursor() == NUL) // delete newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 temp = curwin->w_cursor.col;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 if (!can_bs(BS_EOL) // only if "eol" included
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004482 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004483 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 curwin->w_cursor.col = temp;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004487 // Adjust orig_line_count in case more lines have been deleted than
4488 // have been added. That makes sure, that open_line() later
4489 // can access all buffer lines correctly
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004490 if (State & VREPLACE_FLAG &&
4491 orig_line_count > curbuf->b_ml.ml_line_count)
4492 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01004493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004495 else if (del_char(FALSE) == FAIL) // delete char under cursor
Bram Moolenaar165bc692015-07-21 17:53:25 +02004496 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 did_ai = FALSE;
4498#ifdef FEAT_SMARTINDENT
4499 did_si = FALSE;
4500 can_si = FALSE;
4501 can_si_back = FALSE;
4502#endif
4503 AppendCharToRedobuff(K_DEL);
4504}
4505
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004506/*
4507 * Delete one character for ins_bs().
4508 */
4509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004510ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004511{
4512 dec_cursor();
4513 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
4514 if (State & REPLACE_FLAG)
4515 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004516 // Don't delete characters before the insert point when in
4517 // Replace mode
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004518 if (curwin->w_cursor.lnum != Insstart.lnum
4519 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004520 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004521 }
4522 else
4523 (void)del_char(FALSE);
4524}
4525
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526/*
4527 * Handle Backspace, delete-word and delete-line in Insert mode.
4528 * Return TRUE when backspace was actually used.
4529 */
4530 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004531ins_bs(
4532 int c,
4533 int mode,
4534 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535{
4536 linenr_T lnum;
4537 int cc;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004538 int temp = 0; // init for GCC
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004539 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 colnr_T mincol;
4541 int did_backspace = FALSE;
4542 int in_indent;
4543 int oldState;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004544 int cpc[MAX_MCO]; // composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546 /*
4547 * can't delete anything in an empty file
4548 * can't backup past first character in buffer
4549 * can't backup past starting point unless 'backspace' > 1
4550 * can backup to a previous line if 'backspace' == 0
4551 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004552 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 || (
4554#ifdef FEAT_RIGHTLEFT
4555 !revins_on &&
4556#endif
4557 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
4558 || (!can_bs(BS_START)
4559 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004560 || (curwin->w_cursor.lnum == Insstart_orig.lnum
4561 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
4563 && curwin->w_cursor.col <= ai_col)
4564 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
4565 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02004566 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 return FALSE;
4568 }
4569
4570 if (stop_arrow() == FAIL)
4571 return FALSE;
4572 in_indent = inindent(0);
4573#ifdef FEAT_CINDENT
4574 if (in_indent)
4575 can_cindent = FALSE;
4576#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004577 end_comment_pending = NUL; // After BS, don't auto-end comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004579 if (revins_on) // put cursor after last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 inc_cursor();
4581#endif
4582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004583 // Virtualedit:
4584 // BACKSPACE_CHAR eats a virtual space
4585 // BACKSPACE_WORD eats all coladd
4586 // BACKSPACE_LINE eats all coladd and keeps going
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 if (curwin->w_cursor.coladd > 0)
4588 {
4589 if (mode == BACKSPACE_CHAR)
4590 {
4591 --curwin->w_cursor.coladd;
4592 return TRUE;
4593 }
4594 if (mode == BACKSPACE_WORD)
4595 {
4596 curwin->w_cursor.coladd = 0;
4597 return TRUE;
4598 }
4599 curwin->w_cursor.coladd = 0;
4600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
4602 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02004603 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 */
4605 if (curwin->w_cursor.col == 0)
4606 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004607 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004608 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609#ifdef FEAT_RIGHTLEFT
4610 || revins_on
4611#endif
4612 )
4613 {
4614 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
4615 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
4616 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01004617 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02004618 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
4620 /*
4621 * In replace mode:
4622 * cc < 0: NL was inserted, delete it
4623 * cc >= 0: NL was replaced, put original characters back
4624 */
4625 cc = -1;
4626 if (State & REPLACE_FLAG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 cc = replace_pop(); // returns -1 if NL was inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 /*
4629 * In replace mode, in the line we started replacing, we only move the
4630 * cursor.
4631 */
4632 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
4633 {
4634 dec_cursor();
4635 }
4636 else
4637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (!(State & VREPLACE_FLAG)
4639 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004641 temp = gchar_cursor(); // remember current char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004644 // When "aw" is in 'formatoptions' we must delete the space at
4645 // the end of the line, otherwise the line will be broken
4646 // again when auto-formatting.
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004647 if (has_format_option(FO_AUTO)
4648 && has_format_option(FO_WHITE_PAR))
4649 {
4650 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
4651 TRUE);
4652 int len;
4653
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004654 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00004655 if (len > 0 && ptr[len - 1] == ' ')
4656 ptr[len - 1] = NUL;
4657 }
4658
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004659 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (temp == NUL && gchar_cursor() != NUL)
4661 inc_cursor();
4662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 else
4664 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
4666 /*
4667 * In REPLACE mode we have to put back the text that was replaced
4668 * by the NL. On the replace stack is first a NUL-terminated
4669 * sequence of characters that were deleted and then the
4670 * characters that NL replaced.
4671 */
4672 if (State & REPLACE_FLAG)
4673 {
4674 /*
4675 * Do the next ins_char() in NORMAL state, to
4676 * prevent ins_char() from replacing characters and
4677 * avoiding showmatch().
4678 */
4679 oldState = State;
4680 State = NORMAL;
4681 /*
4682 * restore characters (blanks) deleted after cursor
4683 */
4684 while (cc > 0)
4685 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004686 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004688 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 cc = replace_pop();
4690 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004691 // restore the characters that NL replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 replace_pop_ins();
4693 State = oldState;
4694 }
4695 }
4696 did_ai = FALSE;
4697 }
4698 else
4699 {
4700 /*
4701 * Delete character(s) before the cursor.
4702 */
4703#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004704 if (revins_on) // put cursor on last inserted char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 dec_cursor();
4706#endif
4707 mincol = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004708 // keep indent
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004709 if (mode == BACKSPACE_LINE
4710 && (curbuf->b_p_ai
4711#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00004712 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00004713#endif
4714 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715#ifdef FEAT_RIGHTLEFT
4716 && !revins_on
4717#endif
4718 )
4719 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004720 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00004722 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004724 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
4726
4727 /*
4728 * Handle deleting one 'shiftwidth' or 'softtabstop'.
4729 */
4730 if ( mode == BACKSPACE_CHAR
4731 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004732 || ((get_sts_value() != 0
4733#ifdef FEAT_VARTABS
4734 || tabstop_count(curbuf->b_p_vsts_array)
4735#endif
4736 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00004737 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 && (*(ml_get_cursor() - 1) == TAB
4739 || (*(ml_get_cursor() - 1) == ' '
4740 && (!*inserted_space_p
4741 || arrow_used))))))
4742 {
4743 int ts;
4744 colnr_T vcol;
4745 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004746 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748 *inserted_space_p = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004749 // Compute the virtual column where we want to be. Since
4750 // 'showbreak' may get in the way, need to get the last column of
4751 // the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004753 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 dec_cursor();
4755 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
4756 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004757#ifdef FEAT_VARTABS
4758 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004759 {
4760 ts = (int)get_sw_value(curbuf);
4761 want_vcol = (want_vcol / ts) * ts;
4762 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004763 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02004764 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004765 curbuf->b_p_vsts_array);
4766#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02004767 if (p_sta && in_indent)
4768 ts = (int)get_sw_value(curbuf);
4769 else
4770 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004774 // delete characters until we are at or before want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01004776 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004777 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004779 // insert extra spaces until we are at want_vcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 while (vcol < want_vcol)
4781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004782 // Remember the first char we inserted
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004783 if (curwin->w_cursor.lnum == Insstart_orig.lnum
4784 && curwin->w_cursor.col < Insstart_orig.col)
4785 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (State & VREPLACE_FLAG)
4788 ins_char(' ');
4789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
4791 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004792 if ((State & REPLACE_FLAG))
4793 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
4796 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 // If we are now back where we started delete one character. Can
4799 // happen when using 'sts' and 'linebreak'.
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00004800 if (vcol >= start_vcol)
4801 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803
4804 /*
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004805 * Delete up to starting point, start of line or previous word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004807 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004809 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004811 if (has_mbyte)
4812 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004813 do
4814 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004816 if (!revins_on) // put cursor on char to be deleted
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004819
4820 cc = gchar_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004821 // look multi-byte character class
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004822 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004824 prev_cclass = cclass;
4825 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004828 // start of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004829 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
4830 {
4831 mode = BACKSPACE_WORD_NOT_SPACE;
4832 temp = vim_iswordc(cc);
4833 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004834 // end of word?
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004835 else if (mode == BACKSPACE_WORD_NOT_SPACE
4836 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01004837 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004840 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004842 inc_cursor();
4843#ifdef FEAT_RIGHTLEFT
4844 else if (State & REPLACE_FLAG)
4845 dec_cursor();
4846#endif
4847 break;
4848 }
4849 if (State & REPLACE_FLAG)
4850 replace_do_bs(-1);
4851 else
4852 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004853 if (enc_utf8 && p_deco)
4854 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004855 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004856 /*
4857 * If there are combining characters and 'delcombine' is set
4858 * move the cursor back. Don't back up before the base
4859 * character.
4860 */
4861 if (enc_utf8 && p_deco && cpc[0] != NUL)
4862 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004863#ifdef FEAT_RIGHTLEFT
4864 if (revins_chars)
4865 {
4866 revins_chars--;
4867 revins_legal++;
4868 }
4869 if (revins_on && gchar_cursor() == NUL)
4870 break;
4871#endif
4872 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004873 // Just a single backspace?:
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004874 if (mode == BACKSPACE_CHAR)
4875 break;
4876 } while (
4877#ifdef FEAT_RIGHTLEFT
4878 revins_on ||
4879#endif
4880 (curwin->w_cursor.col > mincol
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02004881 && (can_bs(BS_NOSTOP)
4882 || (curwin->w_cursor.lnum != Insstart_orig.lnum
4883 || curwin->w_cursor.col != Insstart_orig.col)
4884 )));
Bram Moolenaar310f2d52015-03-24 17:49:51 +01004885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 did_backspace = TRUE;
4887 }
4888#ifdef FEAT_SMARTINDENT
4889 did_si = FALSE;
4890 can_si = FALSE;
4891 can_si_back = FALSE;
4892#endif
4893 if (curwin->w_cursor.col <= 1)
4894 did_ai = FALSE;
4895 /*
4896 * It's a little strange to put backspaces into the redo
4897 * buffer, but it makes auto-indent a lot easier to deal
4898 * with.
4899 */
4900 AppendCharToRedobuff(c);
4901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004902 // If deleted before the insertion point, adjust it
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004903 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02004904 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02004905 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004907 // vi behaviour: the cursor moves backward but the character that
4908 // was there remains visible
4909 // Vim behaviour: the cursor moves backward and the character that
4910 // was there is erased from the screen.
4911 // We can emulate the vi behaviour by pretending there is a dollar
4912 // displayed even when there isn't.
4913 // --pkv Sun Jan 19 01:56:40 EST 2003
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004914 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 dollar_vcol = curwin->w_virtcol;
4916
Bram Moolenaarce3be472008-01-14 19:12:28 +00004917#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 // When deleting a char the cursor line must never be in a closed fold.
4919 // E.g., when 'foldmethod' is indent and deleting the first non-white
4920 // char before a Tab.
Bram Moolenaarce3be472008-01-14 19:12:28 +00004921 if (did_backspace)
4922 foldOpenCursor();
4923#endif
4924
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return did_backspace;
4926}
4927
Bram Moolenaarec2da362017-01-21 20:04:22 +01004928/*
4929 * Handle receiving P_PS: start paste mode. Inserts the following text up to
4930 * P_PE literally.
4931 * When "drop" is TRUE then consume the text and drop it.
4932 */
4933 int
4934bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
4935{
4936 int c;
4937 char_u buf[NUMBUFLEN + MB_MAXBYTES];
4938 int idx = 0;
4939 char_u *end = find_termcode((char_u *)"PE");
4940 int ret_char = -1;
4941 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004942 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01004943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004944 // If the end code is too long we can't detect it, read everything.
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01004945 if (end != NULL && STRLEN(end) >= NUMBUFLEN)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004946 end = NULL;
4947 ++no_mapping;
4948 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004949 if (!p_paste)
4950 // Also have the side effects of setting 'paste' to make it work much
4951 // faster.
4952 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01004953
Bram Moolenaarec2da362017-01-21 20:04:22 +01004954 for (;;)
4955 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004956 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004957 if (end == NULL && vpeekc() == NUL)
4958 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004959 do
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004960 c = vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01004961 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004962 if (c == NUL || got_int || (ex_normal_busy > 0 && c == Ctrl_C))
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004963 // When CTRL-C was encountered the typeahead will be flushed and we
Bram Moolenaar98a336d2020-01-20 20:22:30 +01004964 // won't get the end sequence. Except when using ":normal".
Bram Moolenaarfdd71552018-07-28 23:12:05 +02004965 break;
4966
Bram Moolenaarec2da362017-01-21 20:04:22 +01004967 if (has_mbyte)
4968 idx += (*mb_char2bytes)(c, buf + idx);
4969 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01004970 buf[idx++] = c;
4971 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02004972 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01004973 {
4974 if (end[idx] == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004975 break; // Found the end of paste code.
Bram Moolenaarec2da362017-01-21 20:04:22 +01004976 continue;
4977 }
4978 if (!drop)
4979 {
4980 switch (mode)
4981 {
4982 case PASTE_CMDLINE:
4983 put_on_cmdline(buf, idx, TRUE);
4984 break;
4985
4986 case PASTE_EX:
4987 if (gap != NULL && ga_grow(gap, idx) == OK)
4988 {
4989 mch_memmove((char *)gap->ga_data + gap->ga_len,
4990 buf, (size_t)idx);
4991 gap->ga_len += idx;
4992 }
4993 break;
4994
4995 case PASTE_INSERT:
4996 if (stop_arrow() == OK)
4997 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01004998 c = buf[0];
4999 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
5000 ins_eol(c);
5001 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01005002 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01005003 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01005004 AppendToRedobuffLit(buf, idx);
5005 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01005006 }
5007 break;
5008
5009 case PASTE_ONE_CHAR:
5010 if (ret_char == -1)
5011 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01005012 if (has_mbyte)
5013 ret_char = (*mb_ptr2char)(buf);
5014 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01005015 ret_char = buf[0];
5016 }
5017 break;
5018 }
5019 }
5020 idx = 0;
5021 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01005022
Bram Moolenaarec2da362017-01-21 20:04:22 +01005023 --no_mapping;
5024 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02005025 if (!save_paste)
5026 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01005027
5028 return ret_char;
5029}
5030
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005031#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00005032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005033ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005034{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005035 // We will be leaving the current window, unless closing another tab.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005036 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
5037 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
5038 {
5039 undisplay_dollar();
5040 start_arrow(&curwin->w_cursor);
5041# ifdef FEAT_CINDENT
5042 can_cindent = TRUE;
5043# endif
5044 }
5045
5046 if (c == K_TABLINE)
5047 goto_tabpage(current_tab);
5048 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005049 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005050 handle_tabmenu();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005051 redraw_statuslines(); // will redraw the tabline when needed
Bram Moolenaar437df8f2006-04-27 21:47:44 +00005052 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005053}
5054#endif
5055
5056#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005058ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
5060 pos_T tpos;
5061
5062 undisplay_dollar();
5063 tpos = curwin->w_cursor;
5064 if (gui_do_scroll())
5065 {
5066 start_arrow(&tpos);
5067# ifdef FEAT_CINDENT
5068 can_cindent = TRUE;
5069# endif
5070 }
5071}
5072
5073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005074ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
5076 pos_T tpos;
5077
5078 undisplay_dollar();
5079 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005080 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 start_arrow(&tpos);
5083# ifdef FEAT_CINDENT
5084 can_cindent = TRUE;
5085# endif
5086 }
5087}
5088#endif
5089
5090 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005091ins_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
5093 pos_T tpos;
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005094 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
5096#ifdef FEAT_FOLDING
5097 if ((fdo_flags & FDO_HOR) && KeyTyped)
5098 foldOpenCursor();
5099#endif
5100 undisplay_dollar();
5101 tpos = curwin->w_cursor;
5102 if (oneleft() == OK)
5103 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005104#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005105 // Only call start_arrow() when not busy with preediting, it will
5106 // break undo. K_LEFT is inserted in im_correct_cursor().
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005107 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00005108#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005109 {
5110 start_arrow_with_change(&tpos, end_change);
5111 if (!end_change)
5112 AppendCharToRedobuff(K_LEFT);
5113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005115 // If exit reversed string, position is fixed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
5117 revins_legal++;
5118 revins_chars++;
5119#endif
5120 }
5121
5122 /*
5123 * if 'whichwrap' set for cursor in insert mode may go to
5124 * previous line
5125 */
5126 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
5127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005128 // always break undo when moving upwards/downwards, else undo may break
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 start_arrow(&tpos);
5130 --(curwin->w_cursor.lnum);
5131 coladvance((colnr_T)MAXCOL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005132 curwin->w_set_curswant = TRUE; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005135 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005136 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137}
5138
5139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005140ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 pos_T tpos;
5143
5144#ifdef FEAT_FOLDING
5145 if ((fdo_flags & FDO_HOR) && KeyTyped)
5146 foldOpenCursor();
5147#endif
5148 undisplay_dollar();
5149 tpos = curwin->w_cursor;
5150 if (c == K_C_HOME)
5151 curwin->w_cursor.lnum = 1;
5152 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 curwin->w_curswant = 0;
5155 start_arrow(&tpos);
5156}
5157
5158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005159ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160{
5161 pos_T tpos;
5162
5163#ifdef FEAT_FOLDING
5164 if ((fdo_flags & FDO_HOR) && KeyTyped)
5165 foldOpenCursor();
5166#endif
5167 undisplay_dollar();
5168 tpos = curwin->w_cursor;
5169 if (c == K_C_END)
5170 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5171 coladvance((colnr_T)MAXCOL);
5172 curwin->w_curswant = MAXCOL;
5173
5174 start_arrow(&tpos);
5175}
5176
5177 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005178ins_s_left()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005180 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#ifdef FEAT_FOLDING
5182 if ((fdo_flags & FDO_HOR) && KeyTyped)
5183 foldOpenCursor();
5184#endif
5185 undisplay_dollar();
5186 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
5187 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005188 start_arrow_with_change(&curwin->w_cursor, end_change);
5189 if (!end_change)
5190 AppendCharToRedobuff(K_S_LEFT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 (void)bck_word(1L, FALSE, FALSE);
5192 curwin->w_set_curswant = TRUE;
5193 }
5194 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005195 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005196 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197}
5198
5199 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005200ins_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005202 int end_change = dont_sync_undo == FALSE; // end undoable change
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204#ifdef FEAT_FOLDING
5205 if ((fdo_flags & FDO_HOR) && KeyTyped)
5206 foldOpenCursor();
5207#endif
5208 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005209 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005211 start_arrow_with_change(&curwin->w_cursor, end_change);
5212 if (!end_change)
5213 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (virtual_active())
5216 oneright();
5217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005220 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 ++curwin->w_cursor.col;
5223 }
5224
5225#ifdef FEAT_RIGHTLEFT
5226 revins_legal++;
5227 if (revins_chars)
5228 revins_chars--;
5229#endif
5230 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005231 // if 'whichwrap' set for cursor in insert mode, may move the
5232 // cursor to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 else if (vim_strchr(p_ww, ']') != NULL
5234 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5235 {
5236 start_arrow(&curwin->w_cursor);
5237 curwin->w_set_curswant = TRUE;
5238 ++curwin->w_cursor.lnum;
5239 curwin->w_cursor.col = 0;
5240 }
5241 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005242 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02005243 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244}
5245
5246 static void
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005247ins_s_right()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248{
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005249 int end_change = dont_sync_undo == FALSE; // end undoable change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250#ifdef FEAT_FOLDING
5251 if ((fdo_flags & FDO_HOR) && KeyTyped)
5252 foldOpenCursor();
5253#endif
5254 undisplay_dollar();
5255 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
5256 || gchar_cursor() != NUL)
5257 {
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005258 start_arrow_with_change(&curwin->w_cursor, end_change);
5259 if (!end_change)
5260 AppendCharToRedobuff(K_S_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 (void)fwd_word(1L, FALSE, 0);
5262 curwin->w_set_curswant = TRUE;
5263 }
5264 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005265 vim_beep(BO_CRSR);
Bram Moolenaar75bf3d22019-03-26 22:46:05 +01005266 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267}
5268
5269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005270ins_up(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005271 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 pos_T tpos;
5274 linenr_T old_topline = curwin->w_topline;
5275#ifdef FEAT_DIFF
5276 int old_topfill = curwin->w_topfill;
5277#endif
5278
5279 undisplay_dollar();
5280 tpos = curwin->w_cursor;
5281 if (cursor_up(1L, TRUE) == OK)
5282 {
5283 if (startcol)
5284 coladvance(getvcol_nolist(&Insstart));
5285 if (old_topline != curwin->w_topline
5286#ifdef FEAT_DIFF
5287 || old_topfill != curwin->w_topfill
5288#endif
5289 )
5290 redraw_later(VALID);
5291 start_arrow(&tpos);
5292#ifdef FEAT_CINDENT
5293 can_cindent = TRUE;
5294#endif
5295 }
5296 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005297 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298}
5299
5300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005301ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
5303 pos_T tpos;
5304
5305 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005306
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005307 if (mod_mask & MOD_MASK_CTRL)
5308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 // <C-PageUp>: tab page back
Bram Moolenaarbc444822006-10-17 11:37:50 +00005310 if (first_tabpage->tp_next != NULL)
5311 {
5312 start_arrow(&curwin->w_cursor);
5313 goto_tabpage(-1);
5314 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005315 return;
5316 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 tpos = curwin->w_cursor;
5319 if (onepage(BACKWARD, 1L) == OK)
5320 {
5321 start_arrow(&tpos);
5322#ifdef FEAT_CINDENT
5323 can_cindent = TRUE;
5324#endif
5325 }
5326 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005327 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328}
5329
5330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005331ins_down(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005332 int startcol) // when TRUE move to Insstart.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 pos_T tpos;
5335 linenr_T old_topline = curwin->w_topline;
5336#ifdef FEAT_DIFF
5337 int old_topfill = curwin->w_topfill;
5338#endif
5339
5340 undisplay_dollar();
5341 tpos = curwin->w_cursor;
5342 if (cursor_down(1L, TRUE) == OK)
5343 {
5344 if (startcol)
5345 coladvance(getvcol_nolist(&Insstart));
5346 if (old_topline != curwin->w_topline
5347#ifdef FEAT_DIFF
5348 || old_topfill != curwin->w_topfill
5349#endif
5350 )
5351 redraw_later(VALID);
5352 start_arrow(&tpos);
5353#ifdef FEAT_CINDENT
5354 can_cindent = TRUE;
5355#endif
5356 }
5357 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005358 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359}
5360
5361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005362ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363{
5364 pos_T tpos;
5365
5366 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005367
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005368 if (mod_mask & MOD_MASK_CTRL)
5369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 // <C-PageDown>: tab page forward
Bram Moolenaarbc444822006-10-17 11:37:50 +00005371 if (first_tabpage->tp_next != NULL)
5372 {
5373 start_arrow(&curwin->w_cursor);
5374 goto_tabpage(0);
5375 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005376 return;
5377 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 tpos = curwin->w_cursor;
5380 if (onepage(FORWARD, 1L) == OK)
5381 {
5382 start_arrow(&tpos);
5383#ifdef FEAT_CINDENT
5384 can_cindent = TRUE;
5385#endif
5386 }
5387 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02005388 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389}
5390
5391#ifdef FEAT_DND
5392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005393ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
5395 do_put('~', BACKWARD, 1L, PUT_CURSEND);
5396}
5397#endif
5398
5399/*
5400 * Handle TAB in Insert or Replace mode.
5401 * Return TRUE when the TAB needs to be inserted like a normal character.
5402 */
5403 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005404ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 int ind;
5407 int i;
5408 int temp;
5409
5410 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
5411 Insstart_blank_vcol = get_nolist_virtcol();
5412 if (echeck_abbr(TAB + ABBR_OFF))
5413 return FALSE;
5414
5415 ind = inindent(0);
5416#ifdef FEAT_CINDENT
5417 if (ind)
5418 can_cindent = FALSE;
5419#endif
5420
5421 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005422 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 */
5424 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005425#ifdef FEAT_VARTABS
5426 && !(p_sta && ind
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 // These five lines mean 'tabstop' != 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005428 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
5429 || (tabstop_count(curbuf->b_p_vts_array) == 1
5430 && tabstop_first(curbuf->b_p_vts_array)
5431 != get_sw_value(curbuf))
5432 || (tabstop_count(curbuf->b_p_vts_array) == 0
5433 && curbuf->b_p_ts != get_sw_value(curbuf))))
5434 && tabstop_count(curbuf->b_p_vsts_array) == 0
5435#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005436 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005437#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005438 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 return TRUE;
5440
5441 if (stop_arrow() == FAIL)
5442 return TRUE;
5443
5444 did_ai = FALSE;
5445#ifdef FEAT_SMARTINDENT
5446 did_si = FALSE;
5447 can_si = FALSE;
5448 can_si_back = FALSE;
5449#endif
5450 AppendToRedobuff((char_u *)"\t");
5451
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005452#ifdef FEAT_VARTABS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005454 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02005455 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005456 temp -= get_nolist_virtcol() % temp;
5457 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005458 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 // use 'softtabstop' when set
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02005460 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005461 curbuf->b_p_vsts_array);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005462 else // otherwise use 'tabstop'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005463 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
5464 curbuf->b_p_vts_array);
5465#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 if (p_sta && ind) // insert tab in indent, use 'shiftwidth'
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005467 temp = (int)get_sw_value(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005468 else if (curbuf->b_p_sts != 0) // use 'softtabstop' when set
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005469 temp = (int)get_sts_value();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 else // otherwise use 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 temp = (int)curbuf->b_p_ts;
5472 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 /*
5476 * Insert the first space with ins_char(). It will delete one char in
5477 * replace mode. Insert the rest with ins_str(); it will not delete any
5478 * chars. For VREPLACE mode, we use ins_char() for all characters.
5479 */
5480 ins_char(' ');
5481 while (--temp > 0)
5482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (State & VREPLACE_FLAG)
5484 ins_char(' ');
5485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
5487 ins_str((char_u *)" ");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005488 if (State & REPLACE_FLAG) // no char replaced
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 replace_push(NUL);
5490 }
5491 }
5492
5493 /*
5494 * When 'expandtab' not set: Replace spaces by TABs where possible.
5495 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005496#ifdef FEAT_VARTABS
5497 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
5498 || get_sts_value() > 0
5499 || (p_sta && ind)))
5500#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02005501 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 char_u *ptr;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 char_u *saved_line = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 pos_T fpos;
5508 pos_T *cursor;
5509 colnr_T want_vcol, vcol;
5510 int change_col = -1;
5511 int save_list = curwin->w_p_list;
5512
5513 /*
5514 * Get the current line. For VREPLACE mode, don't make real changes
5515 * yet, just work on a copy of the line.
5516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (State & VREPLACE_FLAG)
5518 {
5519 pos = curwin->w_cursor;
5520 cursor = &pos;
5521 saved_line = vim_strsave(ml_get_curline());
5522 if (saved_line == NULL)
5523 return FALSE;
5524 ptr = saved_line + pos.col;
5525 }
5526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 {
5528 ptr = ml_get_cursor();
5529 cursor = &curwin->w_cursor;
5530 }
5531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005532 // When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5534 curwin->w_p_list = FALSE;
5535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005536 // Find first white before the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005538 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 --fpos.col;
5541 --ptr;
5542 }
5543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005544 // In Replace mode, don't change characters before the insert point.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 if ((State & REPLACE_FLAG)
5546 && fpos.lnum == Insstart.lnum
5547 && fpos.col < Insstart.col)
5548 {
5549 ptr += Insstart.col - fpos.col;
5550 fpos.col = Insstart.col;
5551 }
5552
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005553 // compute virtual column numbers of first white and cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 getvcol(curwin, &fpos, &vcol, NULL, NULL);
5555 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
5556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005557 // Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
5558 // and 'linebreak' adding extra virtual columns.
Bram Moolenaar1c465442017-03-12 20:10:05 +01005559 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005561 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 if (vcol + i > want_vcol)
5563 break;
5564 if (*ptr != TAB)
5565 {
5566 *ptr = TAB;
5567 if (change_col < 0)
5568 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005569 change_col = fpos.col; // Column of first change
5570 // May have to adjust Insstart
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
5572 Insstart.col = fpos.col;
5573 }
5574 }
5575 ++fpos.col;
5576 ++ptr;
5577 vcol += i;
5578 }
5579
5580 if (change_col >= 0)
5581 {
5582 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005583 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 // Skip over the spaces we need.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 while (vcol < want_vcol && *ptr == ' ')
5587 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02005588 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 ++ptr;
5590 ++repl_off;
5591 }
5592 if (vcol > want_vcol)
5593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005594 // Must have a char with 'showbreak' just before it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 --ptr;
5596 --repl_off;
5597 }
5598 fpos.col += repl_off;
5599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005600 // Delete following spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 i = cursor->col - fpos.col;
5602 if (i > 0)
5603 {
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005604#ifdef FEAT_PROP_POPUP
5605 if (!(State & VREPLACE_FLAG))
5606 {
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005607 char_u *newp;
5608 int col;
5609
5610 newp = alloc(curbuf->b_ml.ml_line_len - i);
5611 if (newp == NULL)
5612 return FALSE;
5613
5614 col = ptr - curbuf->b_ml.ml_line_ptr;
5615 if (col > 0)
5616 mch_memmove(newp, ptr - col, col);
5617 mch_memmove(newp + col, ptr + i,
5618 curbuf->b_ml.ml_line_len - col - i);
5619
5620 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
5621 vim_free(curbuf->b_ml.ml_line_ptr);
5622 curbuf->b_ml.ml_line_ptr = newp;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005623 curbuf->b_ml.ml_line_len -= i;
Bram Moolenaarac15fd82020-01-09 21:35:48 +01005624 curbuf->b_ml.ml_flags =
5625 (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY;
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01005626 }
5627 else
5628#endif
5629 STRMOVE(ptr, ptr + i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 // correct replace stack.
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005631 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 for (temp = i; --temp >= 0; )
5633 replace_join(repl_off);
5634 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00005635#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005636 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00005637 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02005638 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00005639 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
5640 (char_u *)"\t", 1);
5641 }
5642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 cursor->col -= i;
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 /*
5646 * In VREPLACE mode, we haven't changed anything yet. Do it now by
5647 * backspacing over the changed spacing and then inserting the new
5648 * spacing.
5649 */
5650 if (State & VREPLACE_FLAG)
5651 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005652 // Backspace from real cursor to change_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 backspace_until_column(change_col);
5654
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005655 // Insert each char in saved_line from changed_col to
5656 // ptr-cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 ins_bytes_len(saved_line + change_col,
5658 cursor->col - change_col);
5659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 if (State & VREPLACE_FLAG)
5663 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 curwin->w_p_list = save_list;
5665 }
5666
5667 return FALSE;
5668}
5669
5670/*
5671 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005672 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005675ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 int i;
5678
5679 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005680 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02005682 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 undisplay_dollar();
5684
5685 /*
5686 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
5687 * character under the cursor. Only push a NUL on the replace stack,
5688 * nothing to put back when the NL is deleted.
5689 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02005690 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 replace_push(NUL);
5692
5693 /*
5694 * In VREPLACE mode, a NL replaces the rest of the line, and starts
5695 * replacing the next line, so we push all of the characters left on the
5696 * line onto the replace stack. This is not done here though, it is done
5697 * in open_line().
5698 */
5699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005700 // Put cursor on NUL if on the last char and coladd is 1 (happens after
5701 // CTRL-O).
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005702 if (virtual_active() && curwin->w_cursor.coladd > 0)
5703 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005704
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705#ifdef FEAT_RIGHTLEFT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005706 // NL in reverse insert will always start in the end of
5707 // current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (revins_on)
5709 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
5710#endif
5711
5712 AppendToRedobuff(NL_STR);
5713 i = open_line(FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02005714 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, old_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 old_indent = 0;
5716#ifdef FEAT_CINDENT
5717 can_cindent = TRUE;
5718#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005719#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005720 // When inserting a line the cursor line must never be in a closed fold.
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00005721 foldOpenCursor();
5722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaar24a2d722018-04-24 19:36:43 +02005724 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725}
5726
5727#ifdef FEAT_DIGRAPHS
5728/*
5729 * Handle digraph in insert mode.
5730 * Returns character still to be inserted, or NUL when nothing remaining to be
5731 * done.
5732 */
5733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 int c;
5737 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005738 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
5740 pc_status = PC_STATUS_UNSET;
5741 if (redrawing() && !char_avail())
5742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005743 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005744 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
5746 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005747 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748#ifdef FEAT_CMDL_INFO
5749 add_to_showcmd_c(Ctrl_K);
5750#endif
5751 }
5752
5753#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005754 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#endif
5756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005757 // don't map the digraph chars. This also prevents the
5758 // mode message to be deleted when ESC is hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 ++no_mapping;
5760 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005761 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 --no_mapping;
5763 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005764 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005765 // when the line fits in 'columns' the '?' is at the start of the next
5766 // line and will not be removed by the redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005767 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02005768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005769 if (IS_SPECIAL(c) || mod_mask) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771#ifdef FEAT_CMDL_INFO
5772 clear_showcmd();
5773#endif
5774 insert_special(c, TRUE, FALSE);
5775 return NUL;
5776 }
5777 if (c != ESC)
5778 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005779 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 if (redrawing() && !char_avail())
5781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005782 // may need to redraw when no more chars available now
Bram Moolenaar754b5602006-02-09 23:53:20 +00005783 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 if (char2cells(c) == 1)
5786 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00005787 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005789 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791#ifdef FEAT_CMDL_INFO
5792 add_to_showcmd_c(c);
5793#endif
5794 }
5795 ++no_mapping;
5796 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005797 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 --no_mapping;
5799 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005800 if (did_putchar)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005801 // when the line fits in 'columns' the '?' is at the start of the
5802 // next line and will not be removed by a redraw
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02005803 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 if (cc != ESC)
5805 {
5806 AppendToRedobuff((char_u *)CTRL_V_STR);
5807 c = getdigraph(c, cc, TRUE);
5808#ifdef FEAT_CMDL_INFO
5809 clear_showcmd();
5810#endif
5811 return c;
5812 }
5813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814#ifdef FEAT_CMDL_INFO
5815 clear_showcmd();
5816#endif
5817 return NUL;
5818}
5819#endif
5820
5821/*
5822 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
5823 * Returns the char to be inserted, or NUL if none found.
5824 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005826ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828 int c;
5829 int temp;
5830 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005831 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
5833 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5834 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02005835 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 return NUL;
5837 }
5838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005839 // try to advance to the cursor column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005841 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 prev_ptr = ptr;
5843 validate_virtcol();
5844 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
5845 {
5846 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005847 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 }
5849 if ((colnr_T)temp > curwin->w_virtcol)
5850 ptr = prev_ptr;
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005854 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 return c;
5856}
5857
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005858/*
5859 * CTRL-Y or CTRL-E typed in Insert mode.
5860 */
5861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005862ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005863{
5864 int c = tc;
5865
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005866 if (ctrl_x_mode_scroll())
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005867 {
5868 if (c == Ctrl_Y)
5869 scrolldown_clamp();
5870 else
5871 scrollup_clamp();
5872 redraw_later(VALID);
5873 }
5874 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005875 {
5876 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
5877 if (c != NUL)
5878 {
5879 long tw_save;
5880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005881 // The character must be taken literally, insert like it
5882 // was typed after a CTRL-V, and pretend 'textwidth'
5883 // wasn't set. Digits, 'o' and 'x' are special after a
5884 // CTRL-V, don't use it for these.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005885 if (c < 256 && !isalnum(c))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005886 AppendToRedobuff((char_u *)CTRL_V_STR); // CTRL-V
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005887 tw_save = curbuf->b_p_tw;
5888 curbuf->b_p_tw = -1;
5889 insert_special(c, TRUE, FALSE);
5890 curbuf->b_p_tw = tw_save;
5891#ifdef FEAT_RIGHTLEFT
5892 revins_chars++;
5893 revins_legal++;
5894#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005895 c = Ctrl_V; // pretend CTRL-V is last character
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005896 auto_format(FALSE, TRUE);
5897 }
5898 }
5899 return c;
5900}
5901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902/*
5903 * Get the value that w_virtcol would have when 'list' is off.
5904 * Unless 'cpo' contains the 'L' flag.
5905 */
Bram Moolenaarf9514162018-11-22 03:08:29 +01005906 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005907get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908{
Bram Moolenaarf9514162018-11-22 03:08:29 +01005909 // check validity of cursor in current buffer
5910 if (curwin->w_buffer == NULL
5911 || curwin->w_buffer->b_ml.ml_mfp == NULL
5912 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
5913 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
5915 return getvcol_nolist(&curwin->w_cursor);
5916 validate_virtcol();
5917 return curwin->w_virtcol;
5918}
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005919
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005920#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005921/*
5922 * Handle the InsertCharPre autocommand.
5923 * "c" is the character that was typed.
5924 * Return a pointer to allocated memory with the replacement string.
5925 * Return NULL to continue inserting "c".
5926 */
5927 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005928do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005929{
Bram Moolenaar704984a2012-06-01 14:57:51 +02005930 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +02005931 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005932 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005934 // Return quickly when there is nothing to do.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005935 if (!has_insertcharpre())
5936 return NULL;
5937
Bram Moolenaar704984a2012-06-01 14:57:51 +02005938 if (has_mbyte)
5939 buf[(*mb_char2bytes)(c, buf)] = NUL;
5940 else
Bram Moolenaar704984a2012-06-01 14:57:51 +02005941 {
5942 buf[0] = c;
5943 buf[1] = NUL;
5944 }
5945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005946 // Lock the text to avoid weird things from happening.
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005947 ++textlock;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005948 set_vim_var_string(VV_CHAR, buf, -1); // set v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005949
Bram Moolenaar704984a2012-06-01 14:57:51 +02005950 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005951 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +02005952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953 // Get the value of v:char. It may be empty or more than one
5954 // character. Only use it when changed, otherwise continue with the
5955 // original character to avoid breaking autoindent.
Bram Moolenaar704984a2012-06-01 14:57:51 +02005956 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
5957 res = vim_strsave(get_vim_var_str(VV_CHAR));
5958 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005960 set_vim_var_string(VV_CHAR, NULL, -1); // clear v:char
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005961 --textlock;
5962
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005963 // Restore the State, it may have been changed.
5964 State = save_State;
5965
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005966 return res;
5967}
5968#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005969
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005970#if defined(FEAT_CINDENT) || defined(PROTO)
5971 int
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005972get_can_cindent(void)
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005973{
5974 return can_cindent;
5975}
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02005976
5977 void
5978set_can_cindent(int val)
5979{
5980 can_cindent = val;
5981}
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005982#endif
5983
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005984/*
5985 * Trigger "event" and take care of fixing undo.
5986 */
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005987 int
Bram Moolenaar9fa95062018-08-08 22:08:32 +02005988ins_apply_autocmds(event_T event)
5989{
5990 varnumber_T tick = CHANGEDTICK(curbuf);
5991 int r;
5992
5993 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
5994
5995 // If u_savesub() was called then we are not prepared to start
5996 // a new line. Call u_save() with no contents to fix that.
5997 if (tick != CHANGEDTICK(curbuf))
5998 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
5999
6000 return r;
6001}