blob: eac480314761df8a389fb9dbff557fc20966f0d3 [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
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010020# define CTRL_X_WANT_IDENT 0x100
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010022# define CTRL_X_NORMAL 0 /* CTRL-N CTRL-P completion, default */
23# define CTRL_X_NOT_DEFINED_YET 1
24# define CTRL_X_SCROLL 2
25# define CTRL_X_WHOLE_LINE 3
26# define CTRL_X_FILES 4
27# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
28# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
30# define CTRL_X_FINISHED 8
31# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
32# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
33# define CTRL_X_CMDLINE 11
34# define CTRL_X_FUNCTION 12
35# define CTRL_X_OMNI 13
36# define CTRL_X_SPELL 14
37# define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
38# define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010040# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
41# define CTRL_X_MODE_LINE_OR_EVAL(m) ((m) == CTRL_X_WHOLE_LINE || (m) == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010043/* Message for CTRL-X mode, index is ctrl_x_mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static char *ctrl_x_msgs[] =
45{
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010046 N_(" Keyword completion (^N^P)"), /* CTRL_X_NORMAL, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000047 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010048 NULL, /* CTRL_X_SCROLL: depends on state */
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 N_(" Whole line completion (^L^N^P)"),
50 N_(" File name completion (^F^N^P)"),
51 N_(" Tag completion (^]^N^P)"),
52 N_(" Path pattern completion (^N^P)"),
53 N_(" Definition completion (^D^N^P)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010054 NULL, /* CTRL_X_FINISHED */
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 N_(" Dictionary completion (^K^N^P)"),
56 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000057 N_(" Command-line completion (^V^N^P)"),
58 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000059 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000060 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000061 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010062 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000063};
64
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000065static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010066# ifdef FEAT_COMPL_FUNC
Bram Moolenaar37dd0182010-11-10 16:54:20 +010067static char e_complwin[] = N_("E839: Completion function changed window");
68static char e_compldel[] = N_("E840: Completion function deleted text");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71/*
72 * Structure used to store one match for insert completion.
73 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000074typedef struct compl_S compl_T;
75struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 compl_T *cp_next;
78 compl_T *cp_prev;
79 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000080 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000081 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000082 char_u *cp_fname; /* file containing the match, allocated when
83 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000084 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
85 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086};
87
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010088# define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
89# define FREE_FNAME (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91/*
92 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000093 * "compl_first_match" points to the start of the list.
94 * "compl_curr_match" points to the currently selected entry.
95 * "compl_shown_match" is different from compl_curr_match during
96 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000098static compl_T *compl_first_match = NULL;
99static compl_T *compl_curr_match = NULL;
100static compl_T *compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +0200101static compl_T *compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000103/* After using a cursor key <Enter> selects a match in the popup menu,
104 * otherwise it inserts a line break. */
105static int compl_enter_selects = FALSE;
106
Bram Moolenaara6557602006-02-04 22:43:20 +0000107/* When "compl_leader" is not NULL only matches that start with this string
108 * are used. */
109static char_u *compl_leader = NULL;
110
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000111static int compl_get_longest = FALSE; /* put longest common string
112 in compl_leader */
113
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200114static int compl_no_insert = FALSE; /* FALSE: select & insert
115 TRUE: noinsert */
116static int compl_no_select = FALSE; /* FALSE: select & insert
117 TRUE: noselect */
118
Bram Moolenaara6557602006-02-04 22:43:20 +0000119static int compl_used_match; /* Selected one of the matches. When
120 FALSE the match was edited or using
121 the longest common string. */
122
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000123static int compl_was_interrupted = FALSE; /* didn't finish finding
124 completions. */
125
126static int compl_restarting = FALSE; /* don't insert match */
127
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128/* When the first completion is done "compl_started" is set. When it's
129 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000131
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100132/* Which Ctrl-X mode are we in? */
133static int ctrl_x_mode = CTRL_X_NORMAL;
134
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000135/* Set when doing something for completion that may call edit() recursively,
136 * which is not allowed. */
137static int compl_busy = FALSE;
138
Bram Moolenaar572cb562005-08-05 21:35:02 +0000139static int compl_matches = 0;
140static char_u *compl_pattern = NULL;
141static int compl_direction = FORWARD;
142static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000143static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000144static pos_T compl_startpos;
145static colnr_T compl_col = 0; /* column where the text starts
146 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000147static char_u *compl_orig_text = NULL; /* text as it was before
148 * completion started */
149static int compl_cont_mode = 0;
150static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
Bram Moolenaar82139082011-09-14 16:52:09 +0200152static int compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100153static int compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +0200154
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100155static void ins_ctrl_x(void);
156static int has_compl_option(int dict_opt);
157static int ins_compl_accept_char(int c);
158static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100159static void ins_compl_longest_match(compl_T *match);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100160static void ins_compl_del_pum(void);
161static int pum_wanted(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100162static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
163static char_u *find_line_end(char_u *ptr);
164static void ins_compl_free(void);
165static void ins_compl_clear(void);
166static int ins_compl_bs(void);
167static int ins_compl_need_restart(void);
168static void ins_compl_new_leader(void);
169static void ins_compl_addleader(int c);
170static int ins_compl_len(void);
171static void ins_compl_restart(void);
172static void ins_compl_set_original_text(char_u *str);
173static void ins_compl_addfrommatch(void);
174static int ins_compl_prep(int c);
175static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100176# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100179# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200181static void ins_compl_insert(int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100182static int ins_compl_key2dir(int c);
183static int ins_compl_pum_key(int c);
184static int ins_compl_key2count(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100185static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100186static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#endif /* FEAT_INS_EXPAND */
189
190#define BACKSPACE_CHAR 1
191#define BACKSPACE_WORD 2
192#define BACKSPACE_WORD_NOT_SPACE 3
193#define BACKSPACE_LINE 4
194
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ins_redraw(int ready);
196static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200197#ifdef FEAT_JOB_CHANNEL
198static void init_prompt(int cmdchar_todo);
199#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100206static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000207#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100208static void check_spell_redraw(void);
209static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000210static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000211#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100212static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
213static int echeck_abbr(int);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void replace_join(int off);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void mb_replace_pop_ins(int cc);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void replace_flush(void);
217static void replace_do_bs(int limit_col);
218static int del_char_after_col(int limit_col);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void ins_reg(void);
220static void ins_ctrl_g(void);
221static void ins_ctrl_hat(void);
222static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static int ins_start_select(int c);
227static void ins_insert(int replaceState);
228static void ins_ctrl_o(void);
229static void ins_shift(int c, int lastc);
230static void ins_del(void);
231static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100233static void ins_mouse(int c);
234static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000236#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000238#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ins_left(int end_change);
240static void ins_home(int c);
241static void ins_end(int c);
242static void ins_s_left(void);
243static void ins_right(int end_change);
244static void ins_s_right(void);
245static void ins_up(int startcol);
246static void ins_pageup(void);
247static void ins_down(int startcol);
248static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static int ins_tab(void);
253static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100255static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100261#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100263#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200264static int ins_apply_autocmds(event_T event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266static colnr_T Insstart_textlen; /* length of line when insert started */
267static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100268static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
270static char_u *last_insert = NULL; /* the text of the previous insert,
271 K_SPECIAL and CSI are escaped */
272static int last_insert_skip; /* nr of chars in front of previous insert */
273static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000274static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275
276#ifdef FEAT_CINDENT
277static int can_cindent; /* may do cindenting on this line */
278#endif
279
280static int old_indent = 0; /* for ^^D command in insert mode */
281
282#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000283static int revins_on; /* reverse insert mode on */
284static int revins_chars; /* how much to skip after edit */
285static int revins_legal; /* was the last char 'legal'? */
286static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287#endif
288
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289static int ins_need_undo; /* call u_save() before inserting a
290 char. Set when edit() is called.
291 after that arrow_used is used. */
292
293static int did_add_space = FALSE; /* auto_format() added an extra space
294 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200295static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
296 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297
298/*
299 * edit(): Start inserting text.
300 *
301 * "cmdchar" can be:
302 * 'i' normal insert command
303 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100304 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 * 'R' replace command
306 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
307 * but still only one <CR> is inserted. The <Esc> is not used for redo.
308 * 'g' "gI" command.
309 * 'V' "gR" command for Virtual Replace mode.
310 * 'v' "gr" command for single character Virtual Replace mode.
311 *
312 * This function is not called recursively. For CTRL-O commands, it returns
313 * and lets the caller handle the Normal-mode command.
314 *
315 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
316 */
317 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100318edit(
319 int cmdchar,
320 int startln, /* if set, insert at start of line */
321 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322{
323 int c = 0;
324 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100325 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000326 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 int i;
329 int did_backspace = TRUE; /* previous char was backspace */
330#ifdef FEAT_CINDENT
331 int line_is_white = FALSE; /* line is empty before insert */
332#endif
333 linenr_T old_topline = 0; /* topline before insertion */
334#ifdef FEAT_DIFF
335 int old_topfill = -1;
336#endif
337 int inserted_space = FALSE; /* just inserted a space */
338 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000339 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200340#ifdef FEAT_JOB_CHANNEL
341 int cmdchar_todo = cmdchar;
342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000344 /* Remember whether editing was restarted after CTRL-O. */
345 did_restart_edit = restart_edit;
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 /* sleep before redrawing, needed for "CTRL-O :" that results in an
348 * error message */
349 check_for_delay(TRUE);
350
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100351 /* set Insstart_orig to Insstart */
352 update_Insstart_orig = TRUE;
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#ifdef HAVE_SANDBOX
355 /* Don't allow inserting in the sandbox. */
356 if (sandbox != 0)
357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100358 emsg(_(e_sandbox));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 return FALSE;
360 }
361#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000362 /* Don't allow changes in the buffer while editing the cmdline. The
363 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000364 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100366 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367 return FALSE;
368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
370#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000371 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000372 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100374 emsg(_(e_secure));
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000375 return FALSE;
376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 ins_compl_clear(); /* clear stuff for CTRL-X mode */
378#endif
379
Bram Moolenaar843ee412004-06-30 16:16:41 +0000380 /*
381 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
382 */
383 if (cmdchar != 'r' && cmdchar != 'v')
384 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100385 pos_T save_cursor = curwin->w_cursor;
386
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100387#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000388 if (cmdchar == 'R')
389 ptr = (char_u *)"r";
390 else if (cmdchar == 'V')
391 ptr = (char_u *)"v";
392 else
393 ptr = (char_u *)"i";
394 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200395 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100396#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200397 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100398
Bram Moolenaar097c9922013-05-19 21:15:15 +0200399 /* Make sure the cursor didn't move. Do call check_cursor_col() in
400 * case the text was modified. Since Insert mode was not started yet
401 * a call to check_cursor_col() may move the cursor, especially with
402 * the "A" command, thus set State to avoid that. Also check that the
403 * line number is still valid (lines may have been deleted).
404 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100405 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100406#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200407 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100408#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200409 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100410 {
411 int save_state = State;
412
413 curwin->w_cursor = save_cursor;
414 State = INSERT;
415 check_cursor_col();
416 State = save_state;
417 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000418 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000419
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200420#ifdef FEAT_CONCEAL
421 /* Check if the cursor line needs redrawing before changing State. If
422 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200423 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200424#endif
425
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#ifdef FEAT_MOUSE
427 /*
428 * When doing a paste with the middle mouse button, Insstart is set to
429 * where the paste started.
430 */
431 if (where_paste_started.lnum != 0)
432 Insstart = where_paste_started;
433 else
434#endif
435 {
436 Insstart = curwin->w_cursor;
437 if (startln)
438 Insstart.col = 0;
439 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000440 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 Insstart_blank_vcol = MAXCOL;
442 if (!did_ai)
443 ai_col = 0;
444
445 if (cmdchar != NUL && restart_edit == 0)
446 {
447 ResetRedobuff();
448 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (cmdchar == 'V' || cmdchar == 'v')
450 {
451 /* "gR" or "gr" command */
452 AppendCharToRedobuff('g');
453 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
454 }
455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100457 if (cmdchar == K_PS)
458 AppendCharToRedobuff('a');
459 else
460 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 if (cmdchar == 'g') /* "gI" command */
462 AppendCharToRedobuff('I');
463 else if (cmdchar == 'r') /* "r<CR>" command */
464 count = 1; /* insert only one <CR> */
465 }
466 }
467
468 if (cmdchar == 'R')
469 {
470#ifdef FEAT_FKMAP
471 if (p_fkmap && p_ri)
472 {
473 beep_flush();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100474 emsg(farsi_text_3); /* encoded in Farsi */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 State = INSERT;
476 }
477 else
478#endif
479 State = REPLACE;
480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 else if (cmdchar == 'V' || cmdchar == 'v')
482 {
483 State = VREPLACE;
484 replaceState = VREPLACE;
485 orig_line_count = curbuf->b_ml.ml_line_count;
486 vr_lines_changed = 1;
487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 else
489 State = INSERT;
490
491 stop_insert_mode = FALSE;
492
493 /*
494 * Need to recompute the cursor position, it might move when the cursor is
495 * on a TAB or special character.
496 */
497 curs_columns(TRUE);
498
499 /*
500 * Enable langmap or IME, indicated by 'iminsert'.
501 * Note that IME may enabled/disabled without us noticing here, thus the
502 * 'iminsert' value may not reflect what is actually used. It is updated
503 * when hitting <Esc>.
504 */
505 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
506 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100507#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
509#endif
510
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511#ifdef FEAT_MOUSE
512 setmouse();
513#endif
514#ifdef FEAT_CMDL_INFO
515 clear_showcmd();
516#endif
517#ifdef FEAT_RIGHTLEFT
518 /* there is no reverse replace mode */
519 revins_on = (State == INSERT && p_ri);
520 if (revins_on)
521 undisplay_dollar();
522 revins_chars = 0;
523 revins_legal = 0;
524 revins_scol = -1;
525#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100526 if (!p_ek)
527 /* Disable bracketed paste mode, we won't recognize the escape
528 * sequences. */
529 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
531 /*
532 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100533 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
534 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 */
536 if (restart_edit != 0 && stuff_empty())
537 {
538#ifdef FEAT_MOUSE
539 /*
540 * After a paste we consider text typed to be part of the insert for
541 * the pasted text. You can backspace over the pasted text too.
542 */
543 if (where_paste_started.lnum)
544 arrow_used = FALSE;
545 else
546#endif
547 arrow_used = TRUE;
548 restart_edit = 0;
549
550 /*
551 * If the cursor was after the end-of-line before the CTRL-O and it is
552 * now at the end-of-line, put it after the end-of-line (this is not
553 * correct in very rare cases).
554 * Also do this if curswant is greater than the current virtual
555 * column. Eg after "^O$" or "^O80|".
556 */
557 validate_virtcol();
558 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000559 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 || curwin->w_curswant > curwin->w_virtcol)
561 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
562 {
563 if (ptr[1] == NUL)
564 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 else if (has_mbyte)
566 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000567 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (ptr[i] == NUL)
569 curwin->w_cursor.col += i;
570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000572 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
574 else
575 arrow_used = FALSE;
576
577 /* we are in insert mode now, don't need to start it anymore */
578 need_start_insertmode = FALSE;
579
580 /* Need to save the line for undo before inserting the first char. */
581 ins_need_undo = TRUE;
582
583#ifdef FEAT_MOUSE
584 where_paste_started.lnum = 0;
585#endif
586#ifdef FEAT_CINDENT
587 can_cindent = TRUE;
588#endif
589#ifdef FEAT_FOLDING
590 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
591 * restarting. */
592 if (!p_im && did_restart_edit == 0)
593 foldOpenCursor();
594#endif
595
596 /*
597 * If 'showmode' is set, show the current (insert/replace/..) mode.
598 * A warning message for changing a readonly file is given here, before
599 * actually changing anything. It's put after the mode, if any.
600 */
601 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000602 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 i = showmode();
604
605 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000606 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608#ifdef CURSOR_SHAPE
609 ui_cursor_shape(); /* may show different cursor shape */
610#endif
611#ifdef FEAT_DIGRAPHS
612 do_digraph(-1); /* clear digraphs */
613#endif
614
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000615 /*
616 * Get the current length of the redo buffer, those characters have to be
617 * skipped if we want to get to the inserted characters.
618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 ptr = get_inserted();
620 if (ptr == NULL)
621 new_insert_skip = 0;
622 else
623 {
624 new_insert_skip = (int)STRLEN(ptr);
625 vim_free(ptr);
626 }
627
628 old_indent = 0;
629
630 /*
631 * Main loop in Insert mode: repeat until Insert mode is left.
632 */
633 for (;;)
634 {
635#ifdef FEAT_RIGHTLEFT
636 if (!revins_legal)
637 revins_scol = -1; /* reset on illegal motions */
638 else
639 revins_legal = 0;
640#endif
641 if (arrow_used) /* don't repeat insert when arrow key used */
642 count = 0;
643
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100644 if (update_Insstart_orig)
645 Insstart_orig = Insstart;
646
Bram Moolenaar00672e12016-06-26 18:38:13 +0200647 if (stop_insert_mode
648#ifdef FEAT_INS_EXPAND
649 && !pum_visible()
650#endif
651 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {
653 /* ":stopinsert" used or 'insertmode' reset */
654 count = 0;
655 goto doESCkey;
656 }
657
658 /* set curwin->w_curswant for next K_DOWN or K_UP */
659 if (!arrow_used)
660 curwin->w_set_curswant = TRUE;
661
662 /* If there is no typeahead may check for timestamps (e.g., for when a
663 * menu invoked a shell command). */
664 if (stuff_empty())
665 {
666 did_check_timestamps = FALSE;
667 if (need_check_timestamps)
668 check_timestamps(FALSE);
669 }
670
671 /*
672 * When emsg() was called msg_scroll will have been set.
673 */
674 msg_scroll = FALSE;
675
676#ifdef FEAT_GUI
677 /* When 'mousefocus' is set a mouse movement may have taken us to
678 * another window. "need_mouse_correct" may then be set because of an
679 * autocommand. */
680 if (need_mouse_correct)
681 gui_mouse_correct();
682#endif
683
684#ifdef FEAT_FOLDING
685 /* Open fold at the cursor line, according to 'foldopen'. */
686 if (fdo_flags & FDO_INSERT)
687 foldOpenCursor();
688 /* Close folds where the cursor isn't, according to 'foldclose' */
689 if (!char_avail())
690 foldCheckClose();
691#endif
692
Bram Moolenaarf2732452018-06-03 14:47:35 +0200693#ifdef FEAT_JOB_CHANNEL
694 if (bt_prompt(curbuf))
695 {
696 init_prompt(cmdchar_todo);
697 cmdchar_todo = NUL;
698 }
699#endif
700
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 /*
702 * If we inserted a character at the last position of the last line in
703 * the window, scroll the window one line up. This avoids an extra
704 * redraw.
705 * This is detected when the cursor column is smaller after inserting
706 * something.
707 * Don't do this when the topline changed already, it has
708 * already been adjusted (by insertchar() calling open_line())).
709 */
710 if (curbuf->b_mod_set
711 && curwin->w_p_wrap
712 && !did_backspace
713 && curwin->w_topline == old_topline
714#ifdef FEAT_DIFF
715 && curwin->w_topfill == old_topfill
716#endif
717 )
718 {
719 mincol = curwin->w_wcol;
720 validate_cursor_col();
721
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200722 if (
723#ifdef FEAT_VARTABS
724 (int)curwin->w_wcol < mincol - tabstop_at(
725 get_nolist_virtcol(), curbuf->b_p_ts,
726 curbuf->b_p_vts_array)
727#else
728 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 && curwin->w_wrow == W_WINROW(curwin)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100731 + curwin->w_height - 1 - get_scrolloff_value()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 && (curwin->w_cursor.lnum != curwin->w_topline
733#ifdef FEAT_DIFF
734 || curwin->w_topfill > 0
735#endif
736 ))
737 {
738#ifdef FEAT_DIFF
739 if (curwin->w_topfill > 0)
740 --curwin->w_topfill;
741 else
742#endif
743#ifdef FEAT_FOLDING
744 if (hasFolding(curwin->w_topline, NULL, &old_topline))
745 set_topline(curwin, old_topline + 1);
746 else
747#endif
748 set_topline(curwin, curwin->w_topline + 1);
749 }
750 }
751
752 /* May need to adjust w_topline to show the cursor. */
753 update_topline();
754
755 did_backspace = FALSE;
756
757 validate_cursor(); /* may set must_redraw */
758
759 /*
760 * Redraw the display when no characters are waiting.
761 * Also shows mode, ruler and positions cursor.
762 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000763 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (curwin->w_p_scb)
766 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
Bram Moolenaar860cae12010-06-05 23:22:07 +0200768 if (curwin->w_p_crb)
769 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 update_curswant();
771 old_topline = curwin->w_topline;
772#ifdef FEAT_DIFF
773 old_topfill = curwin->w_topfill;
774#endif
775
776#ifdef USE_ON_FLY_SCROLL
777 dont_scroll = FALSE; /* allow scrolling here */
778#endif
779
780 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100781 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100783 if (c != K_CURSORHOLD)
784 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200785
786 /* After using CTRL-G U the next cursor key will not break undo. */
787 if (dont_sync_undo == MAYBE)
788 dont_sync_undo = TRUE;
789 else
790 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100791 if (cmdchar == K_PS)
792 /* Got here from normal mode when bracketed paste started. */
793 c = K_PS;
794 else
795 do
796 {
797 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200798
799 if (stop_insert_mode)
800 {
801 // Insert mode ended, possibly from a callback.
802 count = 0;
803 nomove = TRUE;
804 goto doESCkey;
805 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100806 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000808 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
809 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811#ifdef FEAT_RIGHTLEFT
812 if (p_hkmap && KeyTyped)
813 c = hkmap(c); /* Hebrew mode mapping */
814#endif
815#ifdef FEAT_FKMAP
816 if (p_fkmap && KeyTyped)
817 c = fkmap(c); /* Farsi mode mapping */
818#endif
819
820#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 /*
822 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000823 * and the cursor is still in the completed word. Only when there is
824 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000825 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000826 if (compl_started
827 && pum_wanted()
828 && curwin->w_cursor.col >= compl_col
829 && (compl_shown_match == NULL
830 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000831 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000832 /* BS: Delete one character from "compl_leader". */
833 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000834 && curwin->w_cursor.col > compl_col
835 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000836 continue;
837
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000838 /* When no match was selected or it was edited. */
839 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000840 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000841 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000842 * "compl_leader". Except when at the original match and
843 * there is nothing to add, CTRL-L works like CTRL-P then. */
844 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100845 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000846 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000847 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000848 {
849 ins_compl_addfrommatch();
850 continue;
851 }
852
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000853 /* A non-white character that fits in with the current
854 * completion: Add to "compl_leader". */
855 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000856 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100857#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100858 /* Trigger InsertCharPre. */
859 char_u *str = do_insert_char_pre(c);
860 char_u *p;
861
862 if (str != NULL)
863 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100864 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100865 ins_compl_addleader(PTR2CHAR(p));
866 vim_free(str);
867 }
868 else
869#endif
870 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000871 continue;
872 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000873
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000874 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000875 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200876 if ((c == Ctrl_Y || (compl_enter_selects
877 && (c == CAR || c == K_KENTER || c == NL)))
878 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000879 {
880 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200881 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000882 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000883 }
884 }
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
887 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000888 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000889 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000890 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#endif
892
Bram Moolenaar488c6512005-08-11 20:09:58 +0000893 /* CTRL-\ CTRL-N goes to Normal mode,
894 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
895 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (c == Ctrl_BSL)
897 {
898 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000899 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 ++no_mapping;
901 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000902 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 --no_mapping;
904 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000905 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000907 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 vungetc(c);
909 c = Ctrl_BSL;
910 }
911 else if (c == Ctrl_G && p_im)
912 continue;
913 else
914 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000915 if (c == Ctrl_O)
916 {
917 ins_ctrl_o();
918 ins_at_eol = FALSE; /* cursor keeps its column */
919 nomove = TRUE;
920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 count = 0;
922 goto doESCkey;
923 }
924 }
925
926#ifdef FEAT_DIGRAPHS
927 c = do_digraph(c);
928#endif
929
930#ifdef FEAT_INS_EXPAND
931 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
932 goto docomplete;
933#endif
934 if (c == Ctrl_V || c == Ctrl_Q)
935 {
936 ins_ctrl_v();
937 c = Ctrl_V; /* pretend CTRL-V is last typed character */
938 continue;
939 }
940
941#ifdef FEAT_CINDENT
942 if (cindent_on()
943# ifdef FEAT_INS_EXPAND
944 && ctrl_x_mode == 0
945# endif
946 )
947 {
948 /* A key name preceded by a bang means this key is not to be
949 * inserted. Skip ahead to the re-indenting below.
950 * A key name preceded by a star means that indenting has to be
951 * done before inserting the key. */
952 line_is_white = inindent(0);
953 if (in_cinkeys(c, '!', line_is_white))
954 goto force_cindent;
955 if (can_cindent && in_cinkeys(c, '*', line_is_white)
956 && stop_arrow() == OK)
957 do_c_expr_indent();
958 }
959#endif
960
961#ifdef FEAT_RIGHTLEFT
962 if (curwin->w_p_rl)
963 switch (c)
964 {
965 case K_LEFT: c = K_RIGHT; break;
966 case K_S_LEFT: c = K_S_RIGHT; break;
967 case K_C_LEFT: c = K_C_RIGHT; break;
968 case K_RIGHT: c = K_LEFT; break;
969 case K_S_RIGHT: c = K_S_LEFT; break;
970 case K_C_RIGHT: c = K_C_LEFT; break;
971 }
972#endif
973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 /*
975 * If 'keymodel' contains "startsel", may start selection. If it
976 * does, a CTRL-O and c will be stuffed, we need to get these
977 * characters.
978 */
979 if (ins_start_select(c))
980 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982 /*
983 * The big switch to handle a character in insert mode.
984 */
985 switch (c)
986 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000987 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (echeck_abbr(ESC + ABBR_OFF))
989 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200990 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000992 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_CMDWIN
994 if (c == Ctrl_C && cmdwin_type != 0)
995 {
996 /* Close the cmdline window. */
997 cmdwin_result = K_IGNORE;
998 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000999 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 goto doESCkey;
1001 }
1002#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001003#ifdef FEAT_JOB_CHANNEL
1004 if (c == Ctrl_C && bt_prompt(curbuf))
1005 {
1006 if (invoke_prompt_interrupt())
1007 {
1008 if (!bt_prompt(curbuf))
1009 // buffer changed to a non-prompt buffer, get out of
1010 // Insert mode
1011 goto doESCkey;
1012 break;
1013 }
1014 }
1015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017#ifdef UNIX
1018do_intr:
1019#endif
1020 /* when 'insertmode' set, and not halfway a mapping, don't leave
1021 * Insert mode */
1022 if (goto_im())
1023 {
1024 if (got_int)
1025 {
1026 (void)vgetc(); /* flush all buffers */
1027 got_int = FALSE;
1028 }
1029 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001030 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 break;
1032 }
1033doESCkey:
1034 /*
1035 * This is the ONLY return from edit()!
1036 */
1037 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1038 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001039 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 o_lnum = curwin->w_cursor.lnum;
1041
Bram Moolenaar488c6512005-08-11 20:09:58 +00001042 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001043 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001044 // When CTRL-C was typed got_int will be set, with the result
1045 // that the autocommands won't be executed. When mapped got_int
1046 // is not set, but let's keep the behavior the same.
1047 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001048 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001049 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 continue;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case Ctrl_Z: /* suspend when 'insertmode' set */
1055 if (!p_im)
1056 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001057 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001058#ifdef CURSOR_SHAPE
1059 ui_cursor_shape(); /* may need to update cursor shape */
1060#endif
1061 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001062
1063 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001064#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001065 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 goto docomplete;
1067#endif
1068 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1069 break;
1070 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001071
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001072 /* don't move the cursor left when 'virtualedit' has "onemore". */
1073 if (ve_flags & VE_ONEMORE)
1074 {
1075 ins_at_eol = FALSE;
1076 nomove = TRUE;
1077 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 count = 0;
1079 goto doESCkey;
1080
Bram Moolenaar572cb562005-08-05 21:35:02 +00001081 case K_INS: /* toggle insert/replace mode */
1082 case K_KINS:
1083 ins_insert(replaceState);
1084 break;
1085
1086 case K_SELECT: /* end of Select mode mapping - ignore */
1087 break;
1088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 case K_HELP: /* Help key works like <ESC> <Help> */
1090 case K_F1:
1091 case K_XF1:
1092 stuffcharReadbuff(K_HELP);
1093 if (p_im)
1094 need_start_insertmode = TRUE;
1095 goto doESCkey;
1096
1097#ifdef FEAT_NETBEANS_INTG
1098 case K_F21: /* NetBeans command */
1099 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001100 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001101 --no_mapping;
1102 netbeans_keycommand(i);
1103 break;
1104#endif
1105
1106 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 case NUL:
1108 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 /* For ^@ the trailing ESC will end the insert, unless there is an
1110 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1112 && c != Ctrl_A && !p_im)
1113 goto doESCkey; /* quit insert mode */
1114 inserted_space = FALSE;
1115 break;
1116
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 ins_reg();
1119 auto_format(FALSE, TRUE);
1120 inserted_space = FALSE;
1121 break;
1122
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001123 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 ins_ctrl_g();
1125 break;
1126
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001127 case Ctrl_HAT: /* switch input mode and/or langmap */
1128 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 break;
1130
1131#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (!p_ari)
1134 goto normalchar;
1135 ins_ctrl_();
1136 break;
1137#endif
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1141 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1142 goto docomplete;
1143#endif
1144 /* FALLTHROUGH */
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147# ifdef FEAT_INS_EXPAND
1148 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1149 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001150 if (has_compl_option(FALSE))
1151 goto docomplete;
1152 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
1154# endif
1155 ins_shift(c, lastc);
1156 auto_format(FALSE, TRUE);
1157 inserted_space = FALSE;
1158 break;
1159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 case K_KDEL:
1162 ins_del();
1163 auto_format(FALSE, TRUE);
1164 break;
1165
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001166 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 case Ctrl_H:
1168 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1169 auto_format(FALSE, TRUE);
1170 break;
1171
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001173#ifdef FEAT_JOB_CHANNEL
1174 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1175 {
1176 // In a prompt window CTRL-W is used for window commands.
1177 // Use Shift-CTRL-W to delete a word.
1178 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001179 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001180 nomove = TRUE;
1181 count = 0;
1182 goto doESCkey;
1183 }
1184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1186 auto_format(FALSE, TRUE);
1187 break;
1188
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001189 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001190# ifdef FEAT_COMPL_FUNC
1191 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001193 goto docomplete;
1194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1196 auto_format(FALSE, TRUE);
1197 inserted_space = FALSE;
1198 break;
1199
1200#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 case K_LEFTMOUSE_NM:
1203 case K_LEFTDRAG:
1204 case K_LEFTRELEASE:
1205 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001206 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 case K_MIDDLEMOUSE:
1208 case K_MIDDLEDRAG:
1209 case K_MIDDLERELEASE:
1210 case K_RIGHTMOUSE:
1211 case K_RIGHTDRAG:
1212 case K_RIGHTRELEASE:
1213 case K_X1MOUSE:
1214 case K_X1DRAG:
1215 case K_X1RELEASE:
1216 case K_X2MOUSE:
1217 case K_X2DRAG:
1218 case K_X2RELEASE:
1219 ins_mouse(c);
1220 break;
1221
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001222 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001223 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 break;
1225
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001226 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001227 ins_mousescroll(MSCR_UP);
1228 break;
1229
1230 case K_MOUSELEFT: /* Scroll wheel left */
1231 ins_mousescroll(MSCR_LEFT);
1232 break;
1233
1234 case K_MOUSERIGHT: /* Scroll wheel right */
1235 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 break;
1237#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001238 case K_PS:
1239 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1240 if (cmdchar == K_PS)
1241 /* invoked from normal mode, bail out */
1242 goto doESCkey;
1243 break;
1244 case K_PE:
1245 /* Got K_PE without K_PS, ignore. */
1246 break;
1247
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001248#ifdef FEAT_GUI_TABLINE
1249 case K_TABLINE:
1250 case K_TABMENU:
1251 ins_tabline(c);
1252 break;
1253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001255 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 break;
1257
Bram Moolenaar754b5602006-02-09 23:53:20 +00001258 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001259 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001260 did_cursorhold = TRUE;
1261 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001262
Bram Moolenaar4770d092006-01-12 23:22:24 +00001263#ifdef FEAT_GUI_W32
1264 /* On Win32 ignore <M-F4>, we get it when closing the window was
1265 * cancelled. */
1266 case K_F4:
1267 if (mod_mask != MOD_MASK_ALT)
1268 goto normalchar;
1269 break;
1270#endif
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272#ifdef FEAT_GUI
1273 case K_VER_SCROLLBAR:
1274 ins_scroll();
1275 break;
1276
1277 case K_HOR_SCROLLBAR:
1278 ins_horscroll();
1279 break;
1280#endif
1281
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001282 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 case K_S_HOME:
1285 case K_C_HOME:
1286 ins_home(c);
1287 break;
1288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 case K_S_END:
1292 case K_C_END:
1293 ins_end(c);
1294 break;
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001297 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1298 ins_s_left();
1299 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001300 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 break;
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 case K_C_LEFT:
1305 ins_s_left();
1306 break;
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001309 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1310 ins_s_right();
1311 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001312 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 break;
1314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001315 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 case K_C_RIGHT:
1317 ins_s_right();
1318 break;
1319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001321#ifdef FEAT_INS_EXPAND
1322 if (pum_visible())
1323 goto docomplete;
1324#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001325 if (mod_mask & MOD_MASK_SHIFT)
1326 ins_pageup();
1327 else
1328 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 break;
1330
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001331 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 case K_PAGEUP:
1333 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001334#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001335 if (pum_visible())
1336 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 ins_pageup();
1339 break;
1340
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001341 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001342#ifdef FEAT_INS_EXPAND
1343 if (pum_visible())
1344 goto docomplete;
1345#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001346 if (mod_mask & MOD_MASK_SHIFT)
1347 ins_pagedown();
1348 else
1349 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 break;
1351
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001352 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 case K_PAGEDOWN:
1354 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001355#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001356 if (pum_visible())
1357 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 ins_pagedown();
1360 break;
1361
1362#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001363 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 ins_drop();
1365 break;
1366#endif
1367
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001368 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 c = TAB;
1370 /* FALLTHROUGH */
1371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001372 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1374 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1375 goto docomplete;
1376#endif
1377 inserted_space = FALSE;
1378 if (ins_tab())
1379 goto normalchar; /* insert TAB as a normal char */
1380 auto_format(FALSE, TRUE);
1381 break;
1382
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001383 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 c = CAR;
1385 /* FALLTHROUGH */
1386 case CAR:
1387 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001388#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 /* In a quickfix window a <CR> jumps to the error under the
1390 * cursor. */
1391 if (bt_quickfix(curbuf) && c == CAR)
1392 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393 if (curwin->w_llist_ref == NULL) /* quickfix window */
1394 do_cmdline_cmd((char_u *)".cc");
1395 else /* location list window */
1396 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 break;
1398 }
1399#endif
1400#ifdef FEAT_CMDWIN
1401 if (cmdwin_type != 0)
1402 {
1403 /* Execute the command in the cmdline window. */
1404 cmdwin_result = CAR;
1405 goto doESCkey;
1406 }
1407#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001408#ifdef FEAT_JOB_CHANNEL
1409 if (bt_prompt(curbuf))
1410 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001411 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001412 if (!bt_prompt(curbuf))
1413 // buffer changed to a non-prompt buffer, get out of
1414 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001415 goto doESCkey;
1416 break;
1417 }
1418#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001419 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 goto doESCkey; /* out of memory */
1421 auto_format(FALSE, FALSE);
1422 inserted_space = FALSE;
1423 break;
1424
Bram Moolenaar860cae12010-06-05 23:22:07 +02001425#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001426 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427# ifdef FEAT_INS_EXPAND
1428 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1429 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001430 if (has_compl_option(TRUE))
1431 goto docomplete;
1432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
1434# endif
1435# ifdef FEAT_DIGRAPHS
1436 c = ins_digraph();
1437 if (c == NUL)
1438 break;
1439# endif
1440 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001444 case Ctrl_X: /* Enter CTRL-X mode */
1445 ins_ctrl_x();
1446 break;
1447
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001448 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (ctrl_x_mode != CTRL_X_TAGS)
1450 goto normalchar;
1451 goto docomplete;
1452
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001453 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if (ctrl_x_mode != CTRL_X_FILES)
1455 goto normalchar;
1456 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001457
1458 case 's': /* Spelling completion after ^X */
1459 case Ctrl_S:
1460 if (ctrl_x_mode != CTRL_X_SPELL)
1461 goto normalchar;
1462 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463#endif
1464
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001465 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#ifdef FEAT_INS_EXPAND
1467 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1468#endif
1469 {
1470 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1471 if (p_im)
1472 {
1473 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1474 break;
1475 goto doESCkey;
1476 }
1477 goto normalchar;
1478 }
1479#ifdef FEAT_INS_EXPAND
1480 /* FALLTHROUGH */
1481
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001482 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 case Ctrl_N:
1484 /* if 'complete' is empty then plain ^P is no longer special,
1485 * but it is under other ^X modes */
1486 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001487 && (ctrl_x_mode == CTRL_X_NORMAL
1488 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001489 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 goto normalchar;
1491
1492docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001493 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001494#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001495 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001496#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001497 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001498 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001499#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001500 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001501#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001502 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 break;
1504#endif /* FEAT_INS_EXPAND */
1505
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001506 case Ctrl_Y: /* copy from previous line or scroll down */
1507 case Ctrl_E: /* copy from next line or scroll up */
1508 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 break;
1510
1511 default:
1512#ifdef UNIX
1513 if (c == intr_char) /* special interrupt char */
1514 goto do_intr;
1515#endif
1516
Bram Moolenaare659c952011-05-19 17:25:41 +02001517normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001519 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001521#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001522 if (!p_paste)
1523 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001524 /* Trigger InsertCharPre. */
1525 char_u *str = do_insert_char_pre(c);
1526 char_u *p;
1527
1528 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001529 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001530 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001531 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001532 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001533 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001534 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001535 c = PTR2CHAR(p);
1536 if (c == CAR || c == K_KENTER || c == NL)
1537 ins_eol(c);
1538 else
1539 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001540 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001541 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001542 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001543 vim_free(str);
1544 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001545 }
1546
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001547 /* If the new value is already inserted or an empty string
1548 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001549 if (c == NUL)
1550 break;
1551 }
1552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#ifdef FEAT_SMARTINDENT
1554 /* Try to perform smart-indenting. */
1555 ins_try_si(c);
1556#endif
1557
1558 if (c == ' ')
1559 {
1560 inserted_space = TRUE;
1561#ifdef FEAT_CINDENT
1562 if (inindent(0))
1563 can_cindent = FALSE;
1564#endif
1565 if (Insstart_blank_vcol == MAXCOL
1566 && curwin->w_cursor.lnum == Insstart.lnum)
1567 Insstart_blank_vcol = get_nolist_virtcol();
1568 }
1569
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001570 /* Insert a normal character and check for abbreviations on a
1571 * special character. Let CTRL-] expand abbreviations without
1572 * inserting it. */
1573 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar13505972019-01-24 15:04:48 +01001574 // Add ABBR_OFF for characters above 0x100, this is
1575 // what check_abbr() expects.
1576 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
1577 && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {
1579 insert_special(c, FALSE, FALSE);
1580#ifdef FEAT_RIGHTLEFT
1581 revins_legal++;
1582 revins_chars++;
1583#endif
1584 }
1585
1586 auto_format(FALSE, TRUE);
1587
1588#ifdef FEAT_FOLDING
1589 /* When inserting a character the cursor line must never be in a
1590 * closed fold. */
1591 foldOpenCursor();
1592#endif
1593 break;
1594 } /* end of switch (c) */
1595
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001596 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001597 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001598#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001599 /* but not in CTRL-X mode, a script can't restore the state */
1600 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001601#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001602 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001603 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001604
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 /* If the cursor was moved we didn't just insert a space */
1606 if (arrow_used)
1607 inserted_space = FALSE;
1608
1609#ifdef FEAT_CINDENT
1610 if (can_cindent && cindent_on()
1611# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001612 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613# endif
1614 )
1615 {
1616force_cindent:
1617 /*
1618 * Indent now if a key was typed that is in 'cinkeys'.
1619 */
1620 if (in_cinkeys(c, ' ', line_is_white))
1621 {
1622 if (stop_arrow() == OK)
1623 /* re-indent the current line */
1624 do_c_expr_indent();
1625 }
1626 }
1627#endif /* FEAT_CINDENT */
1628
1629 } /* for (;;) */
1630 /* NOTREACHED */
1631}
1632
1633/*
1634 * Redraw for Insert mode.
1635 * This is postponed until getting the next character to make '$' in the 'cpo'
1636 * option work correctly.
1637 * Only redraw when there are no characters available. This speeds up
1638 * inserting sequences of characters (e.g., for CTRL-R).
1639 */
1640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001641ins_redraw(
1642 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001644#ifdef FEAT_CONCEAL
1645 linenr_T conceal_old_cursor_line = 0;
1646 linenr_T conceal_new_cursor_line = 0;
1647 int conceal_update_lines = FALSE;
1648#endif
1649
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001650 if (char_avail())
1651 return;
1652
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001653#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001654 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1655 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001656 if (ready && (has_cursormovedI()
1657# if defined(FEAT_CONCEAL)
1658 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001659# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001660 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001661 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001662# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001663 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001664# endif
1665 )
1666 {
1667# ifdef FEAT_SYN_HL
1668 /* Need to update the screen first, to make sure syntax
1669 * highlighting is correct after making a change (e.g., inserting
1670 * a "(". The autocommand may also require a redraw, so it's done
1671 * again below, unfortunately. */
1672 if (syntax_present(curwin) && must_redraw)
1673 update_screen(0);
1674# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001676 {
1677 /* Make sure curswant is correct, an autocommand may call
1678 * getcurpos(). */
1679 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001680 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001681 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001682# ifdef FEAT_CONCEAL
1683 if (curwin->w_p_cole > 0)
1684 {
1685 conceal_old_cursor_line = last_cursormoved.lnum;
1686 conceal_new_cursor_line = curwin->w_cursor.lnum;
1687 conceal_update_lines = TRUE;
1688 }
1689# endif
1690 last_cursormoved = curwin->w_cursor;
1691 }
1692#endif
1693
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001694 /* Trigger TextChangedI if b_changedtick differs. */
1695 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001696 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001697#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001698 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001699#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001700 )
1701 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001702 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001703 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001704
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001705 // save and restore curwin and curbuf, in case the autocmd changes them
1706 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001707 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001708 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001709 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001710 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1711 u_save(curwin->w_cursor.lnum,
1712 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001714
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001715#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001716 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1717 * TextChangedI will need to trigger for backwards compatibility, thus use
1718 * different b_last_changedtick* variables. */
1719 if (ready && has_textchangedP()
1720 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1721 && pum_visible())
1722 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001723 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001724 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001725
1726 // save and restore curwin and curbuf, in case the autocmd changes them
1727 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001728 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001729 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001730 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001731 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1732 u_save(curwin->w_cursor.lnum,
1733 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001734 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001735#endif
1736
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001737#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001738 if ((conceal_update_lines
1739 && (conceal_old_cursor_line != conceal_new_cursor_line
1740 || conceal_cursor_line(curwin)))
1741 || need_cursor_line_redraw)
1742 {
1743 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001744 redrawWinline(curwin, conceal_old_cursor_line);
1745 redrawWinline(curwin, conceal_new_cursor_line == 0
1746 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001747 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001748 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001749 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001750#endif
1751 if (must_redraw)
1752 update_screen(0);
1753 else if (clear_cmdline || redraw_cmdline)
1754 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001755 showruler(FALSE);
1756 setcursor();
1757 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758}
1759
1760/*
1761 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1762 */
1763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001764ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
1766 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001767 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001770 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
1772 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001775 did_putchar = TRUE;
1776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1778
1779#ifdef FEAT_CMDL_INFO
1780 add_to_showcmd_c(Ctrl_V);
1781#endif
1782
1783 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001784 if (did_putchar)
1785 /* when the line fits in 'columns' the '^' is at the start of the next
1786 * line and will not removed by the redraw */
1787 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#ifdef FEAT_CMDL_INFO
1789 clear_showcmd();
1790#endif
1791 insert_special(c, FALSE, TRUE);
1792#ifdef FEAT_RIGHTLEFT
1793 revins_chars++;
1794 revins_legal++;
1795#endif
1796}
1797
1798/*
1799 * Put a character directly onto the screen. It's not stored in a buffer.
1800 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1801 */
1802static int pc_status;
1803#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1804#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1805#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1806#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808static int pc_attr;
1809static int pc_row;
1810static int pc_col;
1811
1812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 int attr;
1816
1817 if (ScreenLines != NULL)
1818 {
1819 update_topline(); /* just in case w_topline isn't valid */
1820 validate_cursor();
1821 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001822 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 else
1824 attr = 0;
1825 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001826 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 pc_status = PC_STATUS_UNSET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828#ifdef FEAT_RIGHTLEFT
1829 if (curwin->w_p_rl)
1830 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001831 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (has_mbyte)
1833 {
1834 int fix_col = mb_fix_col(pc_col, pc_row);
1835
1836 if (fix_col != pc_col)
1837 {
1838 screen_putchar(' ', pc_row, fix_col, attr);
1839 --curwin->w_wcol;
1840 pc_status = PC_STATUS_RIGHT;
1841 }
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 else
1845#endif
1846 {
1847 pc_col += curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (mb_lefthalve(pc_row, pc_col))
1849 pc_status = PC_STATUS_LEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851
1852 /* save the character to be able to put it back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (pc_status == PC_STATUS_UNSET)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
1855 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1856 pc_status = PC_STATUS_SET;
1857 }
1858 screen_putchar(c, pc_row, pc_col, attr);
1859 }
1860}
1861
Bram Moolenaarf2732452018-06-03 14:47:35 +02001862#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1863/*
1864 * Return the effective prompt for the current buffer.
1865 */
1866 char_u *
1867prompt_text(void)
1868{
1869 if (curbuf->b_prompt_text == NULL)
1870 return (char_u *)"% ";
1871 return curbuf->b_prompt_text;
1872}
1873
1874/*
1875 * Prepare for prompt mode: Make sure the last line has the prompt text.
1876 * Move the cursor to this line.
1877 */
1878 static void
1879init_prompt(int cmdchar_todo)
1880{
1881 char_u *prompt = prompt_text();
1882 char_u *text;
1883
1884 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1885 text = ml_get_curline();
1886 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1887 {
1888 // prompt is missing, insert it or append a line with it
1889 if (*text == NUL)
1890 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1891 else
1892 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1893 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1894 coladvance((colnr_T)MAXCOL);
1895 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1896 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001897
1898 // Insert always starts after the prompt, allow editing text after it.
1899 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1900 || Insstart_orig.col != (int)STRLEN(prompt))
1901 {
1902 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001903 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001904 Insstart_orig = Insstart;
1905 Insstart_textlen = Insstart.col;
1906 Insstart_blank_vcol = MAXCOL;
1907 arrow_used = FALSE;
1908 }
1909
Bram Moolenaarf2732452018-06-03 14:47:35 +02001910 if (cmdchar_todo == 'A')
1911 coladvance((colnr_T)MAXCOL);
1912 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001913 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001914 /* Make sure the cursor is in a valid position. */
1915 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001916}
1917
1918/*
1919 * Return TRUE if the cursor is in the editable position of the prompt line.
1920 */
1921 int
1922prompt_curpos_editable()
1923{
1924 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1925 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1926}
1927#endif
1928
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929/*
1930 * Undo the previous edit_putchar().
1931 */
1932 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001933edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
1935 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (pc_status == PC_STATUS_RIGHT)
1938 ++curwin->w_wcol;
1939 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001940 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1943 }
1944}
1945
1946/*
1947 * Called when p_dollar is set: display a '$' at the end of the changed text
1948 * Only works when cursor is in the line that changes.
1949 */
1950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001951display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
1953 colnr_T save_col;
1954
1955 if (!redrawing())
1956 return;
1957
1958 cursor_off();
1959 save_col = curwin->w_cursor.col;
1960 curwin->w_cursor.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 if (has_mbyte)
1962 {
1963 char_u *p;
1964
1965 /* If on the last byte of a multi-byte move to the first byte. */
1966 p = ml_get_curline();
1967 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001970 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 edit_putchar('$', FALSE);
1973 dollar_vcol = curwin->w_virtcol;
1974 }
1975 curwin->w_cursor.col = save_col;
1976}
1977
1978/*
1979 * Call this function before moving the cursor from the normal insert position
1980 * in insert mode.
1981 */
1982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001983undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001985 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001987 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001988 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990}
1991
1992/*
1993 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1994 * Keep the cursor on the same character.
1995 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1996 * type == INDENT_DEC decrease indent (for CTRL-D)
1997 * type == INDENT_SET set indent to "amount"
1998 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1999 */
2000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002001change_indent(
2002 int type,
2003 int amount,
2004 int round,
2005 int replaced, /* replaced character, put on replace stack */
2006 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
2008 int vcol;
2009 int last_vcol;
2010 int insstart_less; /* reduction for Insstart.col */
2011 int new_cursor_col;
2012 int i;
2013 char_u *ptr;
2014 int save_p_list;
2015 int start_col;
2016 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 colnr_T orig_col = 0; /* init for GCC */
2018 char_u *new_line, *orig_line = NULL; /* init for GCC */
2019
2020 /* VREPLACE mode needs to know what the line was like before changing */
2021 if (State & VREPLACE_FLAG)
2022 {
2023 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2024 orig_col = curwin->w_cursor.col;
2025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 /* for the following tricks we don't want list mode */
2028 save_p_list = curwin->w_p_list;
2029 curwin->w_p_list = FALSE;
2030 vc = getvcol_nolist(&curwin->w_cursor);
2031 vcol = vc;
2032
2033 /*
2034 * For Replace mode we need to fix the replace stack later, which is only
2035 * possible when the cursor is in the indent. Remember the number of
2036 * characters before the cursor if it's possible.
2037 */
2038 start_col = curwin->w_cursor.col;
2039
2040 /* determine offset from first non-blank */
2041 new_cursor_col = curwin->w_cursor.col;
2042 beginline(BL_WHITE);
2043 new_cursor_col -= curwin->w_cursor.col;
2044
2045 insstart_less = curwin->w_cursor.col;
2046
2047 /*
2048 * If the cursor is in the indent, compute how many screen columns the
2049 * cursor is to the left of the first non-blank.
2050 */
2051 if (new_cursor_col < 0)
2052 vcol = get_indent() - vcol;
2053
2054 if (new_cursor_col > 0) /* can't fix replace stack */
2055 start_col = -1;
2056
2057 /*
2058 * Set the new indent. The cursor will be put on the first non-blank.
2059 */
2060 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002061 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 else
2063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 int save_State = State;
2065
2066 /* Avoid being called recursively. */
2067 if (State & VREPLACE_FLAG)
2068 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002069 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072 insstart_less -= curwin->w_cursor.col;
2073
2074 /*
2075 * Try to put cursor on same character.
2076 * If the cursor is at or after the first non-blank in the line,
2077 * compute the cursor column relative to the column of the first
2078 * non-blank character.
2079 * If we are not in insert mode, leave the cursor on the first non-blank.
2080 * If the cursor is before the first non-blank, position it relative
2081 * to the first non-blank, counted in screen columns.
2082 */
2083 if (new_cursor_col >= 0)
2084 {
2085 /*
2086 * When changing the indent while the cursor is touching it, reset
2087 * Insstart_col to 0.
2088 */
2089 if (new_cursor_col == 0)
2090 insstart_less = MAXCOL;
2091 new_cursor_col += curwin->w_cursor.col;
2092 }
2093 else if (!(State & INSERT))
2094 new_cursor_col = curwin->w_cursor.col;
2095 else
2096 {
2097 /*
2098 * Compute the screen column where the cursor should be.
2099 */
2100 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002101 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
2103 /*
2104 * Advance the cursor until we reach the right screen column.
2105 */
2106 vcol = last_vcol = 0;
2107 new_cursor_col = -1;
2108 ptr = ml_get_curline();
2109 while (vcol <= (int)curwin->w_virtcol)
2110 {
2111 last_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002113 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002116 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118 vcol = last_vcol;
2119
2120 /*
2121 * May need to insert spaces to be able to position the cursor on
2122 * the right screen column.
2123 */
2124 if (vcol != (int)curwin->w_virtcol)
2125 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002126 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002128 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (ptr != NULL)
2130 {
2131 new_cursor_col += i;
2132 ptr[i] = NUL;
2133 while (--i >= 0)
2134 ptr[i] = ' ';
2135 ins_str(ptr);
2136 vim_free(ptr);
2137 }
2138 }
2139
2140 /*
2141 * When changing the indent while the cursor is in it, reset
2142 * Insstart_col to 0.
2143 */
2144 insstart_less = MAXCOL;
2145 }
2146
2147 curwin->w_p_list = save_p_list;
2148
2149 if (new_cursor_col <= 0)
2150 curwin->w_cursor.col = 0;
2151 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002152 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 curwin->w_set_curswant = TRUE;
2154 changed_cline_bef_curs();
2155
2156 /*
2157 * May have to adjust the start of the insert.
2158 */
2159 if (State & INSERT)
2160 {
2161 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2162 {
2163 if ((int)Insstart.col <= insstart_less)
2164 Insstart.col = 0;
2165 else
2166 Insstart.col -= insstart_less;
2167 }
2168 if ((int)ai_col <= insstart_less)
2169 ai_col = 0;
2170 else
2171 ai_col -= insstart_less;
2172 }
2173
2174 /*
2175 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2176 * If the number of characters before the cursor decreased, need to pop a
2177 * few characters from the replace stack.
2178 * If the number of characters before the cursor increased, need to push a
2179 * few NULs onto the replace stack.
2180 */
2181 if (REPLACE_NORMAL(State) && start_col >= 0)
2182 {
2183 while (start_col > (int)curwin->w_cursor.col)
2184 {
2185 replace_join(0); /* remove a NUL from the replace stack */
2186 --start_col;
2187 }
2188 while (start_col < (int)curwin->w_cursor.col || replaced)
2189 {
2190 replace_push(NUL);
2191 if (replaced)
2192 {
2193 replace_push(replaced);
2194 replaced = NUL;
2195 }
2196 ++start_col;
2197 }
2198 }
2199
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 /*
2201 * For VREPLACE mode, we also have to fix the replace stack. In this case
2202 * it is always possible because we backspace over the whole line and then
2203 * put it back again the way we wanted it.
2204 */
2205 if (State & VREPLACE_FLAG)
2206 {
2207 /* If orig_line didn't allocate, just return. At least we did the job,
2208 * even if you can't backspace. */
2209 if (orig_line == NULL)
2210 return;
2211
2212 /* Save new line */
2213 new_line = vim_strsave(ml_get_curline());
2214 if (new_line == NULL)
2215 return;
2216
2217 /* We only put back the new line up to the cursor */
2218 new_line[curwin->w_cursor.col] = NUL;
2219
2220 /* Put back original line */
2221 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2222 curwin->w_cursor.col = orig_col;
2223
2224 /* Backspace from cursor to start of line */
2225 backspace_until_column(0);
2226
2227 /* Insert new stuff into line again */
2228 ins_bytes(new_line);
2229
2230 vim_free(new_line);
2231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232}
2233
2234/*
2235 * Truncate the space at the end of a line. This is to be used only in an
2236 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2237 * modes.
2238 */
2239 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 int i;
2243
2244 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002245 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
2247 if (State & REPLACE_FLAG)
2248 replace_join(0); /* remove a NUL from the replace stack */
2249 }
2250 line[i + 1] = NUL;
2251}
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253/*
2254 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2255 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002256 * Will attempt not to go before "col" even when there is a composing
2257 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 */
2259 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002260backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261{
2262 while ((int)curwin->w_cursor.col > col)
2263 {
2264 curwin->w_cursor.col--;
2265 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002266 replace_do_bs(col);
2267 else if (!del_char_after_col(col))
2268 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
2270}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002272/*
2273 * Like del_char(), but make sure not to go before column "limit_col".
2274 * Only matters when there are composing characters.
2275 * Return TRUE when something was deleted.
2276 */
2277 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002278del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002279{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002280 if (enc_utf8 && limit_col >= 0)
2281 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002282 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002283
2284 /* Make sure the cursor is at the start of a character, but
2285 * skip forward again when going too far back because of a
2286 * composing character. */
2287 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002288 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002289 {
2290 int l = utf_ptr2len(ml_get_cursor());
2291
2292 if (l == 0) /* end of line */
2293 break;
2294 curwin->w_cursor.col += l;
2295 }
2296 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2297 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002298 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002299 }
2300 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002301 (void)del_char(FALSE);
2302 return TRUE;
2303}
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2306/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002307 * CTRL-X pressed in Insert mode.
2308 */
2309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002310ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002311{
2312 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2313 * CTRL-V works like CTRL-N */
2314 if (ctrl_x_mode != CTRL_X_CMDLINE)
2315 {
2316 /* if the next ^X<> won't ADD nothing, then reset
2317 * compl_cont_status */
2318 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002319 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002320 else
2321 compl_cont_status = 0;
2322 /* We're not sure which CTRL-X mode it will be yet */
2323 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2324 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2325 edit_submode_pre = NULL;
2326 showmode();
2327 }
2328}
2329
2330/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002331 * Whether other than default completion has been selected.
2332 */
2333 int
2334ctrl_x_mode_not_default(void)
2335{
2336 return ctrl_x_mode != CTRL_X_NORMAL;
2337}
2338
2339/*
2340 * Whether CTRL-X was typed without a following character.
2341 */
2342 int
2343ctrl_x_mode_not_defined_yet(void)
2344{
2345 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2346}
2347
2348/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002349 * Return TRUE if the 'dict' or 'tsr' option can be used.
2350 */
2351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002352has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002353{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002354 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002355# ifdef FEAT_SPELL
2356 && !curwin->w_p_spell
2357# endif
2358 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002359 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2360 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002361 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002362 edit_submode = NULL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002363 msg_attr(dict_opt ? _("'dictionary' option is empty")
2364 : _("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002365 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002366 if (emsg_silent == 0)
2367 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002368 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002369 setcursor();
2370 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002371#ifdef FEAT_EVAL
2372 if (!get_vim_var_nr(VV_TESTING))
2373#endif
2374 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002375 }
2376 return FALSE;
2377 }
2378 return TRUE;
2379}
2380
2381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2383 * This depends on the current mode.
2384 */
2385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002386vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01002388 // Always allow ^R - let its results then be checked
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (c == Ctrl_R)
2390 return TRUE;
2391
Bram Moolenaare3226be2005-12-18 22:10:00 +00002392 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002393 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002394 return TRUE;
2395
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 switch (ctrl_x_mode)
2397 {
2398 case 0: /* Not in any CTRL-X mode */
2399 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2400 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002401 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2403 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2404 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002405 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002406 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 case CTRL_X_SCROLL:
2408 return (c == Ctrl_Y || c == Ctrl_E);
2409 case CTRL_X_WHOLE_LINE:
2410 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2411 case CTRL_X_FILES:
2412 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2413 case CTRL_X_DICTIONARY:
2414 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2415 case CTRL_X_THESAURUS:
2416 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2417 case CTRL_X_TAGS:
2418 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2419#ifdef FEAT_FIND_ID
2420 case CTRL_X_PATH_PATTERNS:
2421 return (c == Ctrl_P || c == Ctrl_N);
2422 case CTRL_X_PATH_DEFINES:
2423 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2424#endif
2425 case CTRL_X_CMDLINE:
2426 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2427 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002428#ifdef FEAT_COMPL_FUNC
2429 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002430 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002431 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002432 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002433#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002434 case CTRL_X_SPELL:
2435 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002436 case CTRL_X_EVAL:
2437 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002439 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 return FALSE;
2441}
2442
2443/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002444 * Return TRUE when character "c" is part of the item currently being
2445 * completed. Used to decide whether to abandon complete mode when the menu
2446 * is visible.
2447 */
2448 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002449ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002450{
2451 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2452 /* When expanding an identifier only accept identifier chars. */
2453 return vim_isIDc(c);
2454
2455 switch (ctrl_x_mode)
2456 {
2457 case CTRL_X_FILES:
2458 /* When expanding file name only accept file name chars. But not
2459 * path separators, so that "proto/<Tab>" expands files in
2460 * "proto", not "proto/" as a whole */
2461 return vim_isfilec(c) && !vim_ispathsep(c);
2462
2463 case CTRL_X_CMDLINE:
2464 case CTRL_X_OMNI:
2465 /* Command line and Omni completion can work with just about any
2466 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002467 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002468
2469 case CTRL_X_WHOLE_LINE:
2470 /* For while line completion a space can be part of the line. */
2471 return vim_isprintc(c);
2472 }
2473 return vim_iswordc(c);
2474}
2475
2476/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002477 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002479 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 * the rest of the word to be in -- webb
2481 */
2482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002483ins_compl_add_infercase(
2484 char_u *str,
2485 int len,
2486 int icase,
2487 char_u *fname,
2488 int dir,
2489 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002491 char_u *p;
2492 int i, c;
2493 int actual_len; /* Take multi-byte characters */
2494 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002495 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002496 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 int has_lower = FALSE;
2498 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002500 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002502 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
Bram Moolenaara2993e12007-08-12 14:38:46 +00002504 /* Find actual length of completion. */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002505 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002507 p = str;
2508 actual_len = 0;
2509 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002511 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002512 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002515 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaara2993e12007-08-12 14:38:46 +00002518 /* Find actual length of original text. */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002519 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002521 p = compl_orig_text;
2522 actual_compl_length = 0;
2523 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002525 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002526 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002529 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002530 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002532 /* "actual_len" may be smaller than "actual_compl_length" when using
2533 * thesaurus, only use the minimum when comparing. */
2534 min_len = actual_len < actual_compl_length
2535 ? actual_len : actual_compl_length;
2536
Bram Moolenaara2993e12007-08-12 14:38:46 +00002537 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002538 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002539 if (wca != NULL)
2540 {
2541 p = str;
2542 for (i = 0; i < actual_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002543 if (has_mbyte)
2544 wca[i] = mb_ptr2char_adv(&p);
2545 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002546 wca[i] = *(p++);
2547
2548 /* Rule 1: Were any chars converted to lower? */
2549 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002550 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002551 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002552 if (has_mbyte)
2553 c = mb_ptr2char_adv(&p);
2554 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002555 c = *(p++);
2556 if (MB_ISLOWER(c))
2557 {
2558 has_lower = TRUE;
2559 if (MB_ISUPPER(wca[i]))
2560 {
2561 /* Rule 1 is satisfied. */
2562 for (i = actual_compl_length; i < actual_len; ++i)
2563 wca[i] = MB_TOLOWER(wca[i]);
2564 break;
2565 }
2566 }
2567 }
2568
2569 /*
2570 * Rule 2: No lower case, 2nd consecutive letter converted to
2571 * upper case.
2572 */
2573 if (!has_lower)
2574 {
2575 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002576 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002577 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002578 if (has_mbyte)
2579 c = mb_ptr2char_adv(&p);
2580 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002581 c = *(p++);
2582 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2583 {
2584 /* Rule 2 is satisfied. */
2585 for (i = actual_compl_length; i < actual_len; ++i)
2586 wca[i] = MB_TOUPPER(wca[i]);
2587 break;
2588 }
2589 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2590 }
2591 }
2592
2593 /* Copy the original case of the part we typed. */
2594 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002595 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002596 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002597 if (has_mbyte)
2598 c = mb_ptr2char_adv(&p);
2599 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002600 c = *(p++);
2601 if (MB_ISLOWER(c))
2602 wca[i] = MB_TOLOWER(wca[i]);
2603 else if (MB_ISUPPER(c))
2604 wca[i] = MB_TOUPPER(wca[i]);
2605 }
2606
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002607 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002608 * Generate encoding specific output from wide character array.
2609 * Multi-byte characters can occupy up to five bytes more than
2610 * ASCII characters, and we also need one byte for NUL, so stay
2611 * six bytes away from the edge of IObuff.
2612 */
2613 p = IObuff;
2614 i = 0;
2615 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002616 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002617 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002618 else
Bram Moolenaara2993e12007-08-12 14:38:46 +00002619 *(p++) = wca[i++];
2620 *p = NUL;
2621
2622 vim_free(wca);
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002625 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2626 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002628 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629}
2630
2631/*
2632 * Add a match to the list of matches.
2633 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002634 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002635 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002638ins_compl_add(
2639 char_u *str,
2640 int len,
2641 int icase,
2642 char_u *fname,
2643 char_u **cptext, /* extra text for popup menu or NULL */
2644 int cdir,
2645 int flags,
2646 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002648 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002649 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 ui_breakcheck();
2652 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002653 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (len < 0)
2655 len = (int)STRLEN(str);
2656
2657 /*
2658 * If the same match is already present, don't add it.
2659 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002660 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002662 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 do
2664 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002665 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002666 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002667 && match->cp_str[len] == NUL)
2668 return NOTDONE;
2669 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002670 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
2672
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002673 /* Remove any popup menu before changing the list of matches. */
2674 ins_compl_del_pum();
2675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 /*
2677 * Allocate a new match structure.
2678 * Copy the values to the new match structure.
2679 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002680 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002682 return FAIL;
2683 match->cp_number = -1;
2684 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002685 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002686 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
2688 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002689 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002691 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002694 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2696 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002697 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002698 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002699 && compl_curr_match->cp_fname != NULL
2700 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002701 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002702 else if (fname != NULL)
2703 {
2704 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002705 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002708 match->cp_fname = NULL;
2709 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002710
2711 if (cptext != NULL)
2712 {
2713 int i;
2714
2715 for (i = 0; i < CPT_COUNT; ++i)
2716 if (cptext[i] != NULL && *cptext[i] != NUL)
2717 match->cp_text[i] = vim_strsave(cptext[i]);
2718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 /*
2721 * Link the new match structure in the list of matches.
2722 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002723 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002724 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 else if (dir == FORWARD)
2726 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002727 match->cp_next = compl_curr_match->cp_next;
2728 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 else /* BACKWARD */
2731 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002732 match->cp_next = compl_curr_match;
2733 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002735 if (match->cp_next)
2736 match->cp_next->cp_prev = match;
2737 if (match->cp_prev)
2738 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002740 compl_first_match = match;
2741 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002743 /*
2744 * Find the longest common string if still doing that.
2745 */
2746 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2747 ins_compl_longest_match(match);
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 return OK;
2750}
2751
2752/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002753 * Return TRUE if "str[len]" matches with match->cp_str, considering
2754 * match->cp_icase.
2755 */
2756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002757ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002758{
2759 if (match->cp_icase)
2760 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2761 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2762}
2763
2764/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002765 * Reduce the longest common string for match "match".
2766 */
2767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002768ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002769{
2770 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002771 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002772 int had_match;
2773
2774 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002775 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002776 /* First match, use it as a whole. */
2777 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002778 if (compl_leader != NULL)
2779 {
2780 had_match = (curwin->w_cursor.col > compl_col);
2781 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002782 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002783 ins_redraw(FALSE);
2784
2785 /* When the match isn't there (to avoid matching itself) remove it
2786 * again after redrawing. */
2787 if (!had_match)
2788 ins_compl_delete();
2789 compl_used_match = FALSE;
2790 }
2791 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002792 else
2793 {
2794 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002795 p = compl_leader;
2796 s = match->cp_str;
2797 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002798 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002799 if (has_mbyte)
2800 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002801 c1 = mb_ptr2char(p);
2802 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002803 }
2804 else
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002805 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002806 c1 = *p;
2807 c2 = *s;
2808 }
2809 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2810 : (c1 != c2))
2811 break;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002812 if (has_mbyte)
2813 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002814 MB_PTR_ADV(p);
2815 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002816 }
2817 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002818 {
2819 ++p;
2820 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002821 }
2822 }
2823
2824 if (*p != NUL)
2825 {
2826 /* Leader was shortened, need to change the inserted text. */
2827 *p = NUL;
2828 had_match = (curwin->w_cursor.col > compl_col);
2829 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002830 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002831 ins_redraw(FALSE);
2832
2833 /* When the match isn't there (to avoid matching itself) remove it
2834 * again after redrawing. */
2835 if (!had_match)
2836 ins_compl_delete();
2837 }
2838
2839 compl_used_match = FALSE;
2840 }
2841}
2842
2843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 * Add an array of matches to the list of matches.
2845 * Frees matches[].
2846 */
2847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002848ins_compl_add_matches(
2849 int num_matches,
2850 char_u **matches,
2851 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 int i;
2854 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002855 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaar572cb562005-08-05 21:35:02 +00002857 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002858 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002859 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002861 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 FreeWild(num_matches, matches);
2863}
2864
2865/* Make the completion list cyclic.
2866 * Return the number of matches (excluding the original).
2867 */
2868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002869ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002871 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 int count = 0;
2873
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002874 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
2876 /*
2877 * Find the end of the list.
2878 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002879 match = compl_first_match;
2880 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002881 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002883 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 ++count;
2885 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002886 match->cp_next = compl_first_match;
2887 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
2889 return count;
2890}
2891
Bram Moolenaara94bc432006-03-10 21:42:59 +00002892/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002893 * Set variables that store noselect and noinsert behavior from the
2894 * 'completeopt' value.
2895 */
2896 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002897completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002898{
2899 compl_no_insert = FALSE;
2900 compl_no_select = FALSE;
2901 if (strstr((char *)p_cot, "noselect") != NULL)
2902 compl_no_select = TRUE;
2903 if (strstr((char *)p_cot, "noinsert") != NULL)
2904 compl_no_insert = TRUE;
2905}
2906
2907/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002908 * Start completion for the complete() function.
2909 * "startcol" is where the matched text starts (1 is first column).
2910 * "list" is the list of matches.
2911 */
2912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002913set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002914{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002915 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002916 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002917
Bram Moolenaara94bc432006-03-10 21:42:59 +00002918 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002919 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002920 ins_compl_prep(' ');
2921 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002922 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002923
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002924 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002925 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002926 startcol = curwin->w_cursor.col;
2927 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002928 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002929 /* compl_pattern doesn't need to be set */
2930 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2931 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002932 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002933 return;
2934
Bram Moolenaare4214502015-03-05 18:08:43 +01002935 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002936
2937 ins_compl_add_list(list);
2938 compl_matches = ins_compl_make_cyclic();
2939 compl_started = TRUE;
2940 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002941 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002942
2943 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002944 if (compl_no_insert || compl_no_select)
2945 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002946 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002947 if (compl_no_select)
2948 /* Down/Up has no real effect. */
2949 ins_complete(K_UP, FALSE);
2950 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002951 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002952 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002953 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002954
2955 /* Lazily show the popup menu, unless we got interrupted. */
2956 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002957 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002958 out_flush();
2959}
2960
2961
Bram Moolenaar9372a112005-12-06 19:59:18 +00002962/* "compl_match_array" points the currently displayed list of entries in the
2963 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002964static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002965static int compl_match_arraysize;
2966
2967/*
2968 * Update the screen and when there is any scrolling remove the popup menu.
2969 */
2970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002971ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002972{
2973 int h;
2974
2975 if (compl_match_array != NULL)
2976 {
2977 h = curwin->w_cline_height;
Bram Moolenaarae654382019-01-17 21:09:05 +01002978 // Update the screen later, before drawing the popup menu over it.
2979 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002980 if (h != curwin->w_cline_height)
2981 ins_compl_del_pum();
2982 }
2983}
2984
2985/*
2986 * Remove any popup menu.
2987 */
2988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002989ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002990{
2991 if (compl_match_array != NULL)
2992 {
2993 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01002994 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002995 }
2996}
2997
2998/*
2999 * Return TRUE if the popup menu should be displayed.
3000 */
3001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003002pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003003{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003004 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003005 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003006 return FALSE;
3007
3008 /* The display looks bad on a B&W display. */
3009 if (t_colors < 8
3010#ifdef FEAT_GUI
3011 && !gui.in_use
3012#endif
3013 )
3014 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003015 return TRUE;
3016}
3017
3018/*
3019 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003020 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003021 */
3022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003023pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003024{
3025 compl_T *compl;
3026 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003027
3028 /* Don't display the popup menu if there are no matches or there is only
3029 * one (ignoring the original text). */
3030 compl = compl_first_match;
3031 i = 0;
3032 do
3033 {
3034 if (compl == NULL
3035 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3036 break;
3037 compl = compl->cp_next;
3038 } while (compl != compl_first_match);
3039
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003040 if (strstr((char *)p_cot, "menuone") != NULL)
3041 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003042 return (i >= 2);
3043}
3044
3045/*
3046 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003047 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003048 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003050ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003051{
3052 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003053 compl_T *shown_compl = NULL;
3054 int did_find_shown_match = FALSE;
3055 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056 int i;
3057 int cur = -1;
3058 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003059 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003060
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003061 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003062 return;
3063
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003064#if defined(FEAT_EVAL)
3065 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3066 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3067#endif
3068
Bram Moolenaarae654382019-01-17 21:09:05 +01003069 // Update the screen later, before drawing the popup menu over it.
3070 pum_call_update_screen();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003071
3072 if (compl_match_array == NULL)
3073 {
3074 /* Need to build the popup menu list. */
3075 compl_match_arraysize = 0;
3076 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003077 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003078 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003079 do
3080 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003081 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3082 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003083 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003084 ++compl_match_arraysize;
3085 compl = compl->cp_next;
3086 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003087 if (compl_match_arraysize == 0)
3088 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003089 compl_match_array = (pumitem_T *)alloc_clear(
3090 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003091 * compl_match_arraysize));
3092 if (compl_match_array != NULL)
3093 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003094 /* If the current match is the original text don't find the first
3095 * match after it, don't highlight anything. */
3096 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3097 shown_match_ok = TRUE;
3098
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003099 i = 0;
3100 compl = compl_first_match;
3101 do
3102 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003103 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3104 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003105 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003106 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003107 if (!shown_match_ok)
3108 {
3109 if (compl == compl_shown_match || did_find_shown_match)
3110 {
3111 /* This item is the shown match or this is the
3112 * first displayed item after the shown match. */
3113 compl_shown_match = compl;
3114 did_find_shown_match = TRUE;
3115 shown_match_ok = TRUE;
3116 }
3117 else
3118 /* Remember this displayed match for when the
3119 * shown match is just below it. */
3120 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003121 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003122 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003123
3124 if (compl->cp_text[CPT_ABBR] != NULL)
3125 compl_match_array[i].pum_text =
3126 compl->cp_text[CPT_ABBR];
3127 else
3128 compl_match_array[i].pum_text = compl->cp_str;
3129 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3130 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3131 if (compl->cp_text[CPT_MENU] != NULL)
3132 compl_match_array[i++].pum_extra =
3133 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003134 else
3135 compl_match_array[i++].pum_extra = compl->cp_fname;
3136 }
3137
3138 if (compl == compl_shown_match)
3139 {
3140 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003141
3142 /* When the original text is the shown match don't set
3143 * compl_shown_match. */
3144 if (compl->cp_flags & ORIGINAL_TEXT)
3145 shown_match_ok = TRUE;
3146
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003147 if (!shown_match_ok && shown_compl != NULL)
3148 {
3149 /* The shown match isn't displayed, set it to the
3150 * previously displayed match. */
3151 compl_shown_match = shown_compl;
3152 shown_match_ok = TRUE;
3153 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003154 }
3155 compl = compl->cp_next;
3156 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003157
3158 if (!shown_match_ok) /* no displayed match at all */
3159 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003160 }
3161 }
3162 else
3163 {
3164 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003165 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003166 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3167 || compl_match_array[i].pum_text
3168 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003169 {
3170 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003171 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003172 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003173 }
3174
3175 if (compl_match_array != NULL)
3176 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003177 /* In Replace mode when a $ is displayed at the end of the line only
3178 * part of the screen would be updated. We do need to redraw here. */
3179 dollar_vcol = -1;
3180
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003181 /* Compute the screen column of the start of the completed text.
3182 * Use the cursor to get all wrapping and other settings right. */
3183 col = curwin->w_cursor.col;
3184 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003185 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003186 curwin->w_cursor.col = col;
3187 }
3188}
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190#define DICT_FIRST (1) /* use just first element in "dict" */
3191#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003192
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003194 * Add any identifiers that match the given pattern in the list of dictionary
3195 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 */
3197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198ins_compl_dictionaries(
3199 char_u *dict_start,
3200 char_u *pat,
3201 int flags, /* DICT_FIRST and/or DICT_EXACT */
3202 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003204 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 char_u *ptr;
3206 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 char_u **files;
3209 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003211 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Bram Moolenaar0b238792006-03-02 22:49:12 +00003213 if (*dict == NUL)
3214 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003215#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003216 /* When 'dictionary' is empty and spell checking is enabled use
3217 * "spell". */
3218 if (!thesaurus && curwin->w_p_spell)
3219 dict = (char_u *)"spell";
3220 else
3221#endif
3222 return;
3223 }
3224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003226 if (buf == NULL)
3227 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003228 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 /* If 'infercase' is set, don't use 'smartcase' here */
3231 save_p_scs = p_scs;
3232 if (curbuf->b_p_inf)
3233 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003234
3235 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3236 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003237 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003238 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003239 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003240 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003241 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003242
3243 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003244 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003245 len = STRLEN(pat_esc) + 10;
3246 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003247 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003248 {
3249 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003250 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003251 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003252 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003253 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3254 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003255 vim_free(ptr);
3256 }
3257 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003259 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003260 if (regmatch.regprog == NULL)
3261 goto theend;
3262 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3265 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003266 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
3268 /* copy one dictionary file name into buf */
3269 if (flags == DICT_EXACT)
3270 {
3271 count = 1;
3272 files = &dict;
3273 }
3274 else
3275 {
3276 /* Expand wildcards in the dictionary name, but do not allow
3277 * backticks (for security, the 'dict' option may have been set in
3278 * a modeline). */
3279 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003280# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003281 if (!thesaurus && STRCMP(buf, "spell") == 0)
3282 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003283 else
3284# endif
3285 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 || expand_wildcards(1, &buf, &count, &files,
3287 EW_FILE|EW_SILENT) != OK)
3288 count = 0;
3289 }
3290
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003291# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003292 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003294 /* Complete from active spelling. Skip "\<" in the pattern, we
3295 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003296 if (pat[0] == '\\' && pat[1] == '<')
3297 ptr = pat + 2;
3298 else
3299 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003300 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003302 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003303# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003304 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003305 {
3306 ins_compl_files(count, files, thesaurus, flags,
3307 &regmatch, buf, &dir);
3308 if (flags != DICT_EXACT)
3309 FreeWild(count, files);
3310 }
3311 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 break;
3313 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003314
3315theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003317 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 vim_free(buf);
3319}
3320
Bram Moolenaar0b238792006-03-02 22:49:12 +00003321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003322ins_compl_files(
3323 int count,
3324 char_u **files,
3325 int thesaurus,
3326 int flags,
3327 regmatch_T *regmatch,
3328 char_u *buf,
3329 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003330{
3331 char_u *ptr;
3332 int i;
3333 FILE *fp;
3334 int add_r;
3335
3336 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3337 {
3338 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3339 if (flags != DICT_EXACT)
3340 {
3341 vim_snprintf((char *)IObuff, IOSIZE,
3342 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003343 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003344 }
3345
3346 if (fp != NULL)
3347 {
3348 /*
3349 * Read dictionary file line by line.
3350 * Check each line for a match.
3351 */
3352 while (!got_int && !compl_interrupted
3353 && !vim_fgets(buf, LSIZE, fp))
3354 {
3355 ptr = buf;
3356 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3357 {
3358 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003359 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003360 ptr = find_line_end(ptr);
3361 else
3362 ptr = find_word_end(ptr);
3363 add_r = ins_compl_add_infercase(regmatch->startp[0],
3364 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003365 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003366 if (thesaurus)
3367 {
3368 char_u *wstart;
3369
3370 /*
3371 * Add the other matches on the line
3372 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003373 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003374 while (!got_int)
3375 {
3376 /* Find start of the next word. Skip white
3377 * space and punctuation. */
3378 ptr = find_word_start(ptr);
3379 if (*ptr == NUL || *ptr == NL)
3380 break;
3381 wstart = ptr;
3382
Bram Moolenaara2993e12007-08-12 14:38:46 +00003383 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003384 if (has_mbyte)
3385 /* Japanese words may have characters in
3386 * different classes, only separate words
3387 * with single-byte non-word characters. */
3388 while (*ptr != NUL)
3389 {
3390 int l = (*mb_ptr2len)(ptr);
3391
3392 if (l < 2 && !vim_iswordc(*ptr))
3393 break;
3394 ptr += l;
3395 }
3396 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003397 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003398
3399 /* Add the word. Skip the regexp match. */
3400 if (wstart != regmatch->startp[0])
3401 add_r = ins_compl_add_infercase(wstart,
3402 (int)(ptr - wstart),
3403 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003404 }
3405 }
3406 if (add_r == OK)
3407 /* if dir was BACKWARD then honor it just once */
3408 *dir = FORWARD;
3409 else if (add_r == FAIL)
3410 break;
3411 /* avoid expensive call to vim_regexec() when at end
3412 * of line */
3413 if (*ptr == '\n' || got_int)
3414 break;
3415 }
3416 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003417 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003418 }
3419 fclose(fp);
3420 }
3421 }
3422}
3423
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424/*
3425 * Find the start of the next word.
3426 * Returns a pointer to the first char of the word. Also stops at a NUL.
3427 */
3428 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003429find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 if (has_mbyte)
3432 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003433 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3436 ++ptr;
3437 return ptr;
3438}
3439
3440/*
3441 * Find the end of the word. Assumes it starts inside a word.
3442 * Returns a pointer to just after the word.
3443 */
3444 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003445find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 int start_class;
3448
3449 if (has_mbyte)
3450 {
3451 start_class = mb_get_class(ptr);
3452 if (start_class > 1)
3453 while (*ptr != NUL)
3454 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003455 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if (mb_get_class(ptr) != start_class)
3457 break;
3458 }
3459 }
3460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 while (vim_iswordc(*ptr))
3462 ++ptr;
3463 return ptr;
3464}
3465
3466/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003467 * Find the end of the line, omitting CR and NL at the end.
3468 * Returns a pointer to just after the line.
3469 */
3470 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003471find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003472{
3473 char_u *s;
3474
3475 s = ptr + STRLEN(ptr);
3476 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3477 --s;
3478 return s;
3479}
3480
3481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 * Free the list of completions
3483 */
3484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003485ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003487 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003488 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaard23a8232018-02-10 18:45:26 +01003490 VIM_CLEAR(compl_pattern);
3491 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003493 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003495
3496 ins_compl_del_pum();
3497 pum_clear();
3498
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003499 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 do
3501 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003502 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003503 compl_curr_match = compl_curr_match->cp_next;
3504 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003506 if (match->cp_flags & FREE_FNAME)
3507 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003508 for (i = 0; i < CPT_COUNT; ++i)
3509 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003511 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3512 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003513 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003514 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515}
3516
3517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003518ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003520 compl_cont_status = 0;
3521 compl_started = FALSE;
3522 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003523 VIM_CLEAR(compl_pattern);
3524 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003526 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003527 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003528 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003529 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530}
3531
3532/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003533 * Return TRUE when Insert completion is active.
3534 */
3535 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003536ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003537{
3538 return compl_started;
3539}
3540
3541/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003542 * Delete one character before the cursor and show the subset of the matches
3543 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003544 * Returns the character to be used, NUL if the work is done and another char
3545 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003546 */
3547 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003549{
3550 char_u *line;
3551 char_u *p;
3552
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003553 line = ml_get_curline();
3554 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003555 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003556
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003557 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003558 * allow the word to be deleted, we won't match everything.
3559 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003560 if ((int)(p - line) - (int)compl_col < 0
3561 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003562 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3563 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3564 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003565 return K_BS;
3566
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003567 /* Deleted more than what was used to find matches or didn't finish
3568 * finding all matches: need to look for matches all over again. */
3569 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003570 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003571 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003572
Bram Moolenaara6557602006-02-04 22:43:20 +00003573 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003574 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003575 if (compl_leader != NULL)
3576 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003577 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003578 if (compl_shown_match != NULL)
3579 /* Make sure current match is not a hidden item. */
3580 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003581 return NUL;
3582 }
3583 return K_BS;
3584}
Bram Moolenaara6557602006-02-04 22:43:20 +00003585
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003586/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003587 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3588 * be called.
3589 */
3590 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003591ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003592{
3593 /* Return TRUE if we didn't complete finding matches or when the
3594 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3595 return compl_was_interrupted
3596 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3597 && compl_opt_refresh_always);
3598}
3599
3600/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003601 * Called after changing "compl_leader".
3602 * Show the popup menu with a different set of matches.
3603 * May also search for matches again if the previous search was interrupted.
3604 */
3605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003606ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003607{
3608 ins_compl_del_pum();
3609 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003610 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003611 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003612
3613 if (compl_started)
3614 ins_compl_set_original_text(compl_leader);
3615 else
3616 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003617#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003618 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003619#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003620 /*
Bram Moolenaarae654382019-01-17 21:09:05 +01003621 * Matches were cleared, need to search for them now. Befor drawing
3622 * the popup menu display the changed text before the cursor. Set
3623 * "compl_restarting" to avoid that the first match is inserted.
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003624 */
Bram Moolenaarae654382019-01-17 21:09:05 +01003625 pum_call_update_screen();
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003626#ifdef FEAT_GUI
3627 if (gui.in_use)
3628 {
3629 /* Show the cursor after the match, not after the redrawn text. */
3630 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003631 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003632 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003633#endif
3634 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003635 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003636 compl_cont_status = 0;
3637 compl_restarting = FALSE;
3638 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003639
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003640 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003641
3642 /* Show the popup menu with a different set of matches. */
3643 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003644
3645 /* Don't let Enter select the original text when there is no popup menu. */
3646 if (compl_match_array == NULL)
3647 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003648}
3649
3650/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003651 * Return the length of the completion, from the completion start column to
3652 * the cursor column. Making sure it never goes below zero.
3653 */
3654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003656{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003657 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003658
3659 if (off < 0)
3660 return 0;
3661 return off;
3662}
3663
3664/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003665 * Append one character to the match leader. May reduce the number of
3666 * matches.
3667 */
3668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003669ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003670{
Bram Moolenaara6557602006-02-04 22:43:20 +00003671 int cc;
3672
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003673 if (stop_arrow() == FAIL)
3674 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003675 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3676 {
3677 char_u buf[MB_MAXBYTES + 1];
3678
3679 (*mb_char2bytes)(c, buf);
3680 buf[cc] = NUL;
3681 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003682 if (compl_opt_refresh_always)
3683 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003684 }
3685 else
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003686 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003687 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003688 if (compl_opt_refresh_always)
3689 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003690 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003691
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003692 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003693 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003694 ins_compl_restart();
3695
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003696 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003697 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003698 * break redo. */
3699 if (!compl_opt_refresh_always)
3700 {
3701 vim_free(compl_leader);
3702 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003703 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003704 if (compl_leader != NULL)
3705 ins_compl_new_leader();
3706 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003707}
3708
3709/*
3710 * Setup for finding completions again without leaving CTRL-X mode. Used when
3711 * BS or a key was typed while still searching for matches.
3712 */
3713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003714ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003715{
3716 ins_compl_free();
3717 compl_started = FALSE;
3718 compl_matches = 0;
3719 compl_cont_status = 0;
3720 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003721}
3722
3723/*
3724 * Set the first match, the original text.
3725 */
3726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003727ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003728{
3729 char_u *p;
3730
Bram Moolenaare87edf32018-04-17 22:14:32 +02003731 /* Replace the original text entry.
3732 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3733 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003734 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3735 {
3736 p = vim_strsave(str);
3737 if (p != NULL)
3738 {
3739 vim_free(compl_first_match->cp_str);
3740 compl_first_match->cp_str = p;
3741 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003742 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003743 else if (compl_first_match->cp_prev != NULL
3744 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3745 {
3746 p = vim_strsave(str);
3747 if (p != NULL)
3748 {
3749 vim_free(compl_first_match->cp_prev->cp_str);
3750 compl_first_match->cp_prev->cp_str = p;
3751 }
3752 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003753}
3754
3755/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003756 * Append one character to the match leader. May reduce the number of
3757 * matches.
3758 */
3759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003760ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003761{
3762 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003763 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003764 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003765 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003766
3767 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003768 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003769 {
3770 /* When still at the original match use the first entry that matches
3771 * the leader. */
3772 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3773 {
3774 p = NULL;
3775 for (cp = compl_shown_match->cp_next; cp != NULL
3776 && cp != compl_first_match; cp = cp->cp_next)
3777 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003778 if (compl_leader == NULL
3779 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003780 (int)STRLEN(compl_leader)))
3781 {
3782 p = cp->cp_str;
3783 break;
3784 }
3785 }
3786 if (p == NULL || (int)STRLEN(p) <= len)
3787 return;
3788 }
3789 else
3790 return;
3791 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003792 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003793 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003794 ins_compl_addleader(c);
3795}
3796
3797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003799 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003800 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003803ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
3805 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003807 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
3809 /* Forget any previous 'special' messages if this is actually
3810 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3811 */
3812 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3813 edit_submode_extra = NULL;
3814
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003815 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003816 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3817 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003818 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003820 /* Set "compl_get_longest" when finding the first matches. */
3821 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003822 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003823 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003824 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003825 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003826
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003827 }
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3830 {
3831 /*
3832 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3833 * it will be yet. Now we decide.
3834 */
3835 switch (c)
3836 {
3837 case Ctrl_E:
3838 case Ctrl_Y:
3839 ctrl_x_mode = CTRL_X_SCROLL;
3840 if (!(State & REPLACE_FLAG))
3841 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3842 else
3843 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3844 edit_submode_pre = NULL;
3845 showmode();
3846 break;
3847 case Ctrl_L:
3848 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3849 break;
3850 case Ctrl_F:
3851 ctrl_x_mode = CTRL_X_FILES;
3852 break;
3853 case Ctrl_K:
3854 ctrl_x_mode = CTRL_X_DICTIONARY;
3855 break;
3856 case Ctrl_R:
3857 /* Simply allow ^R to happen without affecting ^X mode */
3858 break;
3859 case Ctrl_T:
3860 ctrl_x_mode = CTRL_X_THESAURUS;
3861 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003862#ifdef FEAT_COMPL_FUNC
3863 case Ctrl_U:
3864 ctrl_x_mode = CTRL_X_FUNCTION;
3865 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003866 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003867 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003868 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003869#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003870 case 's':
3871 case Ctrl_S:
3872 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003873#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003874 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003875 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003876 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003877#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 case Ctrl_RSB:
3880 ctrl_x_mode = CTRL_X_TAGS;
3881 break;
3882#ifdef FEAT_FIND_ID
3883 case Ctrl_I:
3884 case K_S_TAB:
3885 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3886 break;
3887 case Ctrl_D:
3888 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3889 break;
3890#endif
3891 case Ctrl_V:
3892 case Ctrl_Q:
3893 ctrl_x_mode = CTRL_X_CMDLINE;
3894 break;
3895 case Ctrl_P:
3896 case Ctrl_N:
3897 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3898 * just started ^X mode, or there were enough ^X's to cancel
3899 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3900 * do normal expansion when interrupting a different mode (say
3901 * ^X^F^X^P or ^P^X^X^P, see below)
3902 * nothing changes if interrupting mode 0, (eg, the flag
3903 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 if (!(compl_cont_status & CONT_INTRPT))
3905 compl_cont_status |= CONT_LOCAL;
3906 else if (compl_cont_mode != 0)
3907 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 /* FALLTHROUGH */
3909 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003910 /* If we have typed at least 2 ^X's... for modes != 0, we set
3911 * compl_cont_status = 0 (eg, as if we had just started ^X
3912 * mode).
3913 * For mode 0, we set "compl_cont_mode" to an impossible
3914 * value, in both cases ^X^X can be used to restart the same
3915 * mode (avoiding ADDING mode).
3916 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3917 * 'complete' and local ^P expansions respectively.
3918 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3919 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (c == Ctrl_X)
3921 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003922 if (compl_cont_mode != 0)
3923 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003925 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003927 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 edit_submode = NULL;
3929 showmode();
3930 break;
3931 }
3932 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003933 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
3935 /* We're already in CTRL-X mode, do we stay in it? */
3936 if (!vim_is_ctrl_x_key(c))
3937 {
3938 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003939 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 else
3941 ctrl_x_mode = CTRL_X_FINISHED;
3942 edit_submode = NULL;
3943 }
3944 showmode();
3945 }
3946
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003947 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
3949 /* Show error message from attempted keyword completion (probably
3950 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003953 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3954 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 || ctrl_x_mode == CTRL_X_FINISHED)
3956 {
3957 /* Get here when we have finished typing a sequence of ^N and
3958 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003959 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003960 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
3962 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003963 * If any of the original typed text has been changed, eg when
3964 * ignorecase is set, we must add back-spaces to the redo
3965 * buffer. We add as few as necessary to delete just the part
3966 * of the original text that has changed.
3967 * When using the longest match, edited the match or used
3968 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003970 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3971 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003972 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003973 ptr = NULL;
3974 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976
3977#ifdef FEAT_CINDENT
3978 want_cindent = (can_cindent && cindent_on());
3979#endif
3980 /*
3981 * When completing whole lines: fix indent for 'cindent'.
3982 * Otherwise, break line if it's too long.
3983 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003984 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
3986#ifdef FEAT_CINDENT
3987 /* re-indent the current line */
3988 if (want_cindent)
3989 {
3990 do_c_expr_indent();
3991 want_cindent = FALSE; /* don't do it again */
3992 }
3993#endif
3994 }
3995 else
3996 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003997 int prev_col = curwin->w_cursor.col;
3998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004000 if (prev_col > 0)
4001 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004002 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004003 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004005 if (prev_col > 0
4006 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4007 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004010 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004011 * the selection without inserting anything. When
4012 * compl_enter_selects is set the Enter key does the same. */
4013 if ((c == Ctrl_Y || (compl_enter_selects
4014 && (c == CAR || c == K_KENTER || c == NL)))
4015 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004016 retval = TRUE;
4017
Bram Moolenaar47247282016-08-02 22:36:02 +02004018 /* CTRL-E means completion is Ended, go back to the typed text.
4019 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004020 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004021 {
4022 ins_compl_delete();
4023 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004024 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004025 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004026 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004027 retval = TRUE;
4028 }
4029
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004030 auto_format(FALSE, TRUE);
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004033 compl_started = FALSE;
4034 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004035 if (!shortmess(SHM_COMPLETIONMENU))
4036 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004037 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004038 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (edit_submode != NULL)
4040 {
4041 edit_submode = NULL;
4042 showmode();
4043 }
4044
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004045#ifdef FEAT_CMDWIN
4046 if (c == Ctrl_C && cmdwin_type != 0)
4047 /* Avoid the popup menu remains displayed when leaving the
4048 * command line window. */
4049 update_screen(0);
4050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051#ifdef FEAT_CINDENT
4052 /*
4053 * Indent now if a key was typed that is in 'cinkeys'.
4054 */
4055 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4056 do_c_expr_indent();
4057#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004058 /* Trigger the CompleteDone event to give scripts a chance to act
4059 * upon the completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004060 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004063 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4064 /* Trigger the CompleteDone event to give scripts a chance to act
4065 * upon the (possibly failed) completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004066 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 /* reset continue_* if we left expansion-mode, if we stay they'll be
4069 * (re)set properly in ins_complete() */
4070 if (!vim_is_ctrl_x_key(c))
4071 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072 compl_cont_status = 0;
4073 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004075
4076 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077}
4078
4079/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004080 * Fix the redo buffer for the completion leader replacing some of the typed
4081 * text. This inserts backspaces and appends the changed text.
4082 * "ptr" is the known leader text or NUL.
4083 */
4084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004085ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004086{
4087 int len;
4088 char_u *p;
4089 char_u *ptr = ptr_arg;
4090
4091 if (ptr == NULL)
4092 {
4093 if (compl_leader != NULL)
4094 ptr = compl_leader;
4095 else
4096 return; /* nothing to do */
4097 }
4098 if (compl_orig_text != NULL)
4099 {
4100 p = compl_orig_text;
4101 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4102 ;
Bram Moolenaar62951b12011-09-21 18:23:05 +02004103 if (len > 0)
4104 len -= (*mb_head_off)(p, p + len);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004105 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004106 AppendCharToRedobuff(K_BS);
4107 }
4108 else
4109 len = 0;
4110 if (ptr != NULL)
4111 AppendToRedobuffLit(ptr + len, -1);
4112}
4113
4114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4116 * (depending on flag) starting from buf and looking for a non-scanned
4117 * buffer (other than curbuf). curbuf is special, if it is called with
4118 * buf=curbuf then it has to be the first call for a given flag/expansion.
4119 *
4120 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4121 */
4122 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004123ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127 if (flag == 'w') /* just windows */
4128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 if (buf == curbuf) /* first call for this flag/expansion */
4130 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004131 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 && wp->w_buffer->b_scanned)
4133 ;
4134 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 else
4137 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4138 * (unlisted buffers)
4139 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004140 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 && ((flag == 'U'
4142 ? buf->b_p_bl
4143 : (!buf->b_p_bl
4144 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004145 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 ;
4147 return buf;
4148}
4149
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004150#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004151/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004152 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004153 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004154 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004156expand_by_function(
4157 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4158 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004159{
Bram Moolenaar82139082011-09-14 16:52:09 +02004160 list_T *matchlist = NULL;
4161 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004162 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004163 char_u *funcname;
4164 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004165 win_T *curwin_save;
4166 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004167 typval_T rettv;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004168 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004169
Bram Moolenaare344bea2005-09-01 20:46:49 +00004170 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4171 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004172 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004173
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004174 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004175 args[0].v_type = VAR_NUMBER;
4176 args[0].vval.v_number = 0;
4177 args[1].v_type = VAR_STRING;
4178 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4179 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004180
Bram Moolenaare344bea2005-09-01 20:46:49 +00004181 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004182 curwin_save = curwin;
4183 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004184
4185 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004186 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004187 {
4188 switch (rettv.v_type)
4189 {
4190 case VAR_LIST:
4191 matchlist = rettv.vval.v_list;
4192 break;
4193 case VAR_DICT:
4194 matchdict = rettv.vval.v_dict;
4195 break;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004196 case VAR_SPECIAL:
4197 if (rettv.vval.v_number == VVAL_NONE)
4198 compl_opt_suppress_empty = TRUE;
4199 // FALLTHROUGH
Bram Moolenaar82139082011-09-14 16:52:09 +02004200 default:
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004201 // TODO: Give error message?
Bram Moolenaar82139082011-09-14 16:52:09 +02004202 clear_tv(&rettv);
4203 break;
4204 }
4205 }
4206
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004207 if (curwin_save != curwin || curbuf_save != curbuf)
4208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004209 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004210 goto theend;
4211 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004212 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004213 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004214 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004216 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004217 goto theend;
4218 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004219
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004220 if (matchlist != NULL)
4221 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004222 else if (matchdict != NULL)
4223 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004224
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004225theend:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004226 // Restore State, it might have been changed.
4227 State = save_State;
4228
Bram Moolenaar82139082011-09-14 16:52:09 +02004229 if (matchdict != NULL)
4230 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004231 if (matchlist != NULL)
4232 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004233}
4234#endif /* FEAT_COMPL_FUNC */
4235
Bram Moolenaar39f05632006-03-19 22:15:26 +00004236#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004237/*
4238 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004239 */
4240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004241ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004242{
4243 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004244 int dir = compl_direction;
4245
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004246 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004247 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004248 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004249 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4250 /* if dir was BACKWARD then honor it just once */
4251 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004252 else if (did_emsg)
4253 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004254 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004255}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004256
4257/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004258 * Add completions from a dict.
4259 */
4260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004261ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004262{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004263 dictitem_T *di_refresh;
4264 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004265
4266 /* Check for optional "refresh" item. */
4267 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004268 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4269 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004270 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004271 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004272
4273 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4274 compl_opt_refresh_always = TRUE;
4275 }
4276
4277 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004278 di_words = dict_find(dict, (char_u *)"words", 5);
4279 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4280 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004281}
4282
4283/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004284 * Add a match to the list of matches from a typeval_T.
4285 * If the given string is already in the list of completions, then return
4286 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4287 * maybe because alloc() returns NULL, then FAIL is returned.
4288 */
4289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004290ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004291{
4292 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004293 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004294 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004295 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004296 char_u *(cptext[CPT_COUNT]);
4297
4298 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4299 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004300 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4301 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004302 (char_u *)"abbr", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004303 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004304 (char_u *)"menu", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004305 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004306 (char_u *)"kind", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004307 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004308 (char_u *)"info", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004309 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004310 (char_u *)"user_data", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004311 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4312 icase = dict_get_number(tv->vval.v_dict, (char_u *)"icase");
4313 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
4314 adup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
4315 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4316 aempty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004317 }
4318 else
4319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004320 word = tv_get_string_chk(tv);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004321 vim_memset(cptext, 0, sizeof(cptext));
4322 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004323 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004324 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004325 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004326}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004327#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329/*
4330 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004331 * The search starts at position "ini" in curbuf and in the direction
4332 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004333 * When "compl_started" is FALSE start at that position, otherwise continue
4334 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004335 * This may return before finding all the matches.
4336 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 */
4338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004339ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 static pos_T first_match_pos;
4342 static pos_T last_match_pos;
4343 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 static int found_all = FALSE; /* Found all matches of a
4345 certain type. */
4346 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
Bram Moolenaar572cb562005-08-05 21:35:02 +00004348 pos_T *pos;
4349 char_u **matches;
4350 int save_p_scs;
4351 int save_p_ws;
4352 int save_p_ic;
4353 int i;
4354 int num_matches;
4355 int len;
4356 int found_new_match;
4357 int type = ctrl_x_mode;
4358 char_u *ptr;
4359 char_u *dict = NULL;
4360 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004361 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004365 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 ins_buf->b_scanned = 0;
4367 found_all = FALSE;
4368 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004370 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 last_match_pos = first_match_pos = *ini;
4372 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004373 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4374 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
Bram Moolenaar4475b622017-05-01 20:46:52 +02004376 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004377 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004378
4379 /*
4380 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 for (;;)
4383 {
4384 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004385 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 * or if found_all says this entry is done. For ^X^L only use the
4389 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004390 if ((ctrl_x_mode == CTRL_X_NORMAL
4391 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
4394 found_all = FALSE;
4395 while (*e_cpt == ',' || *e_cpt == ' ')
4396 e_cpt++;
4397 if (*e_cpt == '.' && !curbuf->b_scanned)
4398 {
4399 ins_buf = curbuf;
4400 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004401 /* Move the cursor back one character so that ^N can match the
4402 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004403 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004404 {
4405 /* Move the cursor to after the last character in the
4406 * buffer, so that word at start of buffer is found
4407 * correctly. */
4408 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4409 first_match_pos.col =
4410 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 last_match_pos = first_match_pos;
4413 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004414
4415 /* Remember the first match so that the loop stops when we
4416 * wrap and come back there a second time. */
4417 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4420 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4421 {
4422 /* Scan a buffer, but not the current one. */
4423 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4424 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 first_match_pos.col = last_match_pos.col = 0;
4427 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4428 last_match_pos.lnum = 0;
4429 type = 0;
4430 }
4431 else /* unloaded buffer, scan like dictionary */
4432 {
4433 found_all = TRUE;
4434 if (ins_buf->b_fname == NULL)
4435 continue;
4436 type = CTRL_X_DICTIONARY;
4437 dict = ins_buf->b_fname;
4438 dict_f = DICT_EXACT;
4439 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004440 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 ins_buf->b_fname == NULL
4442 ? buf_spname(ins_buf)
4443 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004444 ? ins_buf->b_fname
4445 : ins_buf->b_sfname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004446 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 else if (*e_cpt == NUL)
4449 break;
4450 else
4451 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004452 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 type = -1;
4454 else if (*e_cpt == 'k' || *e_cpt == 's')
4455 {
4456 if (*e_cpt == 'k')
4457 type = CTRL_X_DICTIONARY;
4458 else
4459 type = CTRL_X_THESAURUS;
4460 if (*++e_cpt != ',' && *e_cpt != NUL)
4461 {
4462 dict = e_cpt;
4463 dict_f = DICT_FIRST;
4464 }
4465 }
4466#ifdef FEAT_FIND_ID
4467 else if (*e_cpt == 'i')
4468 type = CTRL_X_PATH_PATTERNS;
4469 else if (*e_cpt == 'd')
4470 type = CTRL_X_PATH_DEFINES;
4471#endif
4472 else if (*e_cpt == ']' || *e_cpt == 't')
4473 {
4474 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004475 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar32526b32019-01-19 17:43:09 +01004476 (void)msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 else
4479 type = -1;
4480
4481 /* in any case e_cpt is advanced to the next entry */
4482 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4483
4484 found_all = TRUE;
4485 if (type == -1)
4486 continue;
4487 }
4488 }
4489
Bram Moolenaar4475b622017-05-01 20:46:52 +02004490 /* If complete() was called then compl_pattern has been reset. The
4491 * following won't work then, bail out. */
4492 if (compl_pattern == NULL)
4493 break;
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 switch (type)
4496 {
4497 case -1:
4498 break;
4499#ifdef FEAT_FIND_ID
4500 case CTRL_X_PATH_PATTERNS:
4501 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004502 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4507 (linenr_T)1, (linenr_T)MAXLNUM);
4508 break;
4509#endif
4510
4511 case CTRL_X_DICTIONARY:
4512 case CTRL_X_THESAURUS:
4513 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004514 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 : (type == CTRL_X_THESAURUS
4516 ? (*curbuf->b_p_tsr == NUL
4517 ? p_tsr
4518 : curbuf->b_p_tsr)
4519 : (*curbuf->b_p_dict == NUL
4520 ? p_dict
4521 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004522 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004523 dict != NULL ? dict_f
4524 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 dict = NULL;
4526 break;
4527
4528 case CTRL_X_TAGS:
4529 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4530 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004531 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004533 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004534 * of matches is found when compl_pattern is empty */
4535 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004536 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4537 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4539 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004540 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 }
4542 p_ic = save_p_ic;
4543 break;
4544
4545 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4548 {
4549
4550 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004552 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554 break;
4555
4556 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 if (expand_cmdline(&compl_xp, compl_pattern,
4558 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004560 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 break;
4562
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004563#ifdef FEAT_COMPL_FUNC
4564 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004565 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004566 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004567 break;
4568#endif
4569
Bram Moolenaar488c6512005-08-11 20:09:58 +00004570 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004571#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004572 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004573 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004574 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004575 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004576#endif
4577 break;
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 default: /* normal ^P/^N and ^X^L */
4580 /*
4581 * If 'infercase' is set, don't use 'smartcase' here
4582 */
4583 save_p_scs = p_scs;
4584 if (ins_buf->b_p_inf)
4585 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004586
Bram Moolenaar361aa502014-01-23 22:45:58 +01004587 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 * end but never from the middle, thus setting nowrapscan in this
4589 * buffers is a good idea, on the other hand, we always set
4590 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4591 save_p_ws = p_ws;
4592 if (ins_buf != curbuf)
4593 p_ws = FALSE;
4594 else if (*e_cpt == '.')
4595 p_ws = TRUE;
4596 for (;;)
4597 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004598 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004600 ++msg_silent; /* Don't want messages for wrapscan. */
4601
Bram Moolenaare4214502015-03-05 18:08:43 +01004602 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4603 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004604 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004605 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004608 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 else
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004610 found_new_match = searchit(NULL, ins_buf, pos, NULL,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004611 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004613 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004614 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004615 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004617 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 first_match_pos = *pos;
4620 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004621 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
4623 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004624 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 found_new_match = FAIL;
4626 if (found_new_match == FAIL)
4627 {
4628 if (ins_buf == curbuf)
4629 found_all = TRUE;
4630 break;
4631 }
4632
4633 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004634 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 && ini->lnum == pos->lnum
4636 && ini->col == pos->col)
4637 continue;
4638 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004639 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004641 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
4643 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4644 continue;
4645 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4646 if (!p_paste)
4647 ptr = skipwhite(ptr);
4648 }
4649 len = (int)STRLEN(ptr);
4650 }
4651 else
4652 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004653 char_u *tmp_ptr = ptr;
4654
4655 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004657 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 /* Skip if already inside a word. */
4659 if (vim_iswordp(tmp_ptr))
4660 continue;
4661 /* Find start of next word. */
4662 tmp_ptr = find_word_start(tmp_ptr);
4663 }
4664 /* Find end of this word. */
4665 tmp_ptr = find_word_end(tmp_ptr);
4666 len = (int)(tmp_ptr - ptr);
4667
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004668 if ((compl_cont_status & CONT_ADDING)
4669 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
4671 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4672 {
4673 /* Try next line, if any. the new word will be
4674 * "join" as if the normal command "J" was used.
4675 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004676 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 * works -- Acevedo */
4678 STRNCPY(IObuff, ptr, len);
4679 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4680 tmp_ptr = ptr = skipwhite(ptr);
4681 /* Find start of next word. */
4682 tmp_ptr = find_word_start(tmp_ptr);
4683 /* Find end of next word. */
4684 tmp_ptr = find_word_end(tmp_ptr);
4685 if (tmp_ptr > ptr)
4686 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004687 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004689 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 IObuff[len++] = ' ';
4691 /* IObuf =~ "\k.* ", thus len >= 2 */
4692 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004693 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 || (vim_strchr(p_cpo, CPO_JOINSP)
4695 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004696 && (IObuff[len - 2] == '?'
4697 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 IObuff[len++] = ' ';
4699 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004700 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 if (tmp_ptr - ptr >= IOSIZE - len)
4702 tmp_ptr = ptr + IOSIZE - len - 1;
4703 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4704 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004705 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707 IObuff[len] = NUL;
4708 ptr = IObuff;
4709 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004710 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 continue;
4712 }
4713 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004714 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004715 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004716 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
4718 found_new_match = OK;
4719 break;
4720 }
4721 }
4722 p_scs = save_p_scs;
4723 p_ws = save_p_ws;
4724 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004725
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004727 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004728 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 found_new_match = OK;
4730
4731 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004732 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4733 * match */
4734 if ((ctrl_x_mode != CTRL_X_NORMAL
4735 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004737 {
4738 if (got_int)
4739 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004740 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004741 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004742 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004743
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004744 if ((ctrl_x_mode != CTRL_X_NORMAL
4745 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004746 || compl_interrupted)
4747 break;
4748 compl_started = TRUE;
4749 }
4750 else
4751 {
4752 /* Mark a buffer scanned when it has been scanned completely */
4753 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4754 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004756 compl_started = FALSE;
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004759 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004761 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 && *e_cpt == NUL) /* Got to end of 'complete' */
4763 found_new_match = FAIL;
4764
4765 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004766 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4767 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 i = ins_compl_make_cyclic();
4769
Bram Moolenaar4475b622017-05-01 20:46:52 +02004770 if (compl_old_match != NULL)
4771 {
4772 /* If several matches were added (FORWARD) or the search failed and has
4773 * just been made cyclic then we have to move compl_curr_match to the
4774 * next or previous entry (if any) -- Acevedo */
4775 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4776 : compl_old_match->cp_prev;
4777 if (compl_curr_match == NULL)
4778 compl_curr_match = compl_old_match;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 return i;
4781}
4782
4783/* Delete the old text being completed. */
4784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004785ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004787 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
4789 /*
4790 * In insert mode: Delete the typed part.
4791 * In replace mode: Put the old characters back, if any.
4792 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004793 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4794 if ((int)curwin->w_cursor.col > col)
4795 {
4796 if (stop_arrow() == FAIL)
4797 return;
4798 backspace_until_column(col);
4799 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004800
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004801 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4802 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004804 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004805 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806}
4807
Bram Moolenaar472e8592016-10-15 17:06:47 +02004808/*
4809 * Insert the new text being completed.
4810 * "in_compl_func" is TRUE when called from complete_check().
4811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004813ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004815 dict_T *dict;
4816
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004817 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004818 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4819 compl_used_match = FALSE;
4820 else
4821 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004822
4823 /* Set completed item. */
4824 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004825 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004826 if (dict != NULL)
4827 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004828 dict_add_string(dict, "word", compl_shown_match->cp_str);
4829 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4830 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4831 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4832 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4833 dict_add_string(dict, "user_data",
4834 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004835 }
4836 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004837 if (!in_compl_func)
4838 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839}
4840
4841/*
4842 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004843 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4844 * get more completions. If it is FALSE, then we just do nothing when there
4845 * are no more completions in a given direction. The latter case is used when
4846 * we are still in the middle of finding completions, to allow browsing
4847 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 * Return the total number of matches, or -1 if still unknown -- webb.
4849 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004850 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4851 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 *
4853 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004854 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4855 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 */
4857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004858ins_compl_next(
4859 int allow_get_expansion,
4860 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004861 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004862 int insert_match, /* Insert the newly selected match */
4863 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004866 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004867 compl_T *found_compl = NULL;
4868 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004869 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004870 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004872 /* When user complete function return -1 for findstart which is next
4873 * time of 'always', compl_shown_match become NULL. */
4874 if (compl_shown_match == NULL)
4875 return -1;
4876
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004877 if (compl_leader != NULL
4878 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004880 /* Set "compl_shown_match" to the actually shown match, it may differ
4881 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004882 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004883 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004884 && compl_shown_match->cp_next != NULL
4885 && compl_shown_match->cp_next != compl_first_match)
4886 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004887
4888 /* If we didn't find it searching forward, and compl_shows_dir is
4889 * backward, find the last match. */
4890 if (compl_shows_dir == BACKWARD
4891 && !ins_compl_equal(compl_shown_match,
4892 compl_leader, (int)STRLEN(compl_leader))
4893 && (compl_shown_match->cp_next == NULL
4894 || compl_shown_match->cp_next == compl_first_match))
4895 {
4896 while (!ins_compl_equal(compl_shown_match,
4897 compl_leader, (int)STRLEN(compl_leader))
4898 && compl_shown_match->cp_prev != NULL
4899 && compl_shown_match->cp_prev != compl_first_match)
4900 compl_shown_match = compl_shown_match->cp_prev;
4901 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004902 }
4903
4904 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004905 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 /* Delete old text to be replaced */
4907 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004908
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004909 /* When finding the longest common text we stick at the original text,
4910 * don't let CTRL-N or CTRL-P move to the first match. */
4911 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4912
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004913 /* When restarting the search don't insert the first match either. */
4914 if (compl_restarting)
4915 {
4916 advance = FALSE;
4917 compl_restarting = FALSE;
4918 }
4919
Bram Moolenaare3226be2005-12-18 22:10:00 +00004920 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4921 * around. */
4922 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004924 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004926 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004927 found_end = (compl_first_match != NULL
4928 && (compl_shown_match->cp_next == compl_first_match
4929 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004930 }
4931 else if (compl_shows_dir == BACKWARD
4932 && compl_shown_match->cp_prev != NULL)
4933 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004934 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004935 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004936 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004939 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004940 if (!allow_get_expansion)
4941 {
4942 if (advance)
4943 {
4944 if (compl_shows_dir == BACKWARD)
4945 compl_pending -= todo + 1;
4946 else
4947 compl_pending += todo + 1;
4948 }
4949 return -1;
4950 }
4951
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004952 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004953 {
4954 if (compl_shows_dir == BACKWARD)
4955 --compl_pending;
4956 else
4957 ++compl_pending;
4958 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004959
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004960 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004961 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004962
4963 /* handle any pending completions */
4964 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004965 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004966 {
4967 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4968 {
4969 compl_shown_match = compl_shown_match->cp_next;
4970 --compl_pending;
4971 }
4972 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4973 {
4974 compl_shown_match = compl_shown_match->cp_prev;
4975 ++compl_pending;
4976 }
4977 else
4978 break;
4979 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004980 found_end = FALSE;
4981 }
4982 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4983 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004984 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004985 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004986 ++todo;
4987 else
4988 /* Remember a matching item. */
4989 found_compl = compl_shown_match;
4990
4991 /* Stop at the end of the list when we found a usable match. */
4992 if (found_end)
4993 {
4994 if (found_compl != NULL)
4995 {
4996 compl_shown_match = found_compl;
4997 break;
4998 }
4999 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005003 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005004 if (compl_no_insert && !started)
5005 {
5006 ins_bytes(compl_orig_text + ins_compl_len());
5007 compl_used_match = FALSE;
5008 }
5009 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005010 {
5011 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005012 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005013 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005014 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005015 }
5016 else
5017 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019 if (!allow_get_expansion)
5020 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005021 /* may undisplay the popup menu first */
5022 ins_compl_upd_pum();
5023
Bram Moolenaarae654382019-01-17 21:09:05 +01005024 // Redraw before showing the popup menu to show the user what was
5025 // inserted.
5026 pum_call_update_screen();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005027
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005028 /* display the updated popup menu */
5029 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005030#ifdef FEAT_GUI
5031 if (gui.in_use)
5032 {
5033 /* Show the cursor after the match, not after the redrawn text. */
5034 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005035 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005036 }
5037#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 /* Delete old text to be replaced, since we're still searching and
5040 * don't want to match ourselves! */
5041 ins_compl_delete();
5042 }
5043
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005044 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005045 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005046 if (compl_no_insert && !started)
5047 compl_enter_selects = TRUE;
5048 else
5049 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005050
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 /*
5052 * Show the file name for the match (if any)
5053 * Truncate the file name to avoid a wait for return.
5054 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005055 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005057 char *lead = _("match in file");
5058 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5059 char_u *s;
5060 char_u *e;
5061
5062 if (space > 0)
5063 {
5064 /* We need the tail that fits. With double-byte encoding going
5065 * back from the end is very slow, thus go from the start and keep
5066 * the text that fits in "space" between "s" and "e". */
5067 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5068 {
5069 space -= ptr2cells(e);
5070 while (space < 0)
5071 {
5072 space += ptr2cells(s);
5073 MB_PTR_ADV(s);
5074 }
5075 }
5076 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5077 s > compl_shown_match->cp_fname ? "<" : "", s);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005078 msg((char *)IObuff);
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005079 redraw_cmdline = FALSE; /* don't overwrite! */
5080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082
5083 return num_matches;
5084}
5085
5086/*
5087 * Call this while finding completions, to check whether the user has hit a key
5088 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005089 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005091 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005092 * "in_compl_func" is TRUE when called from complete_check(), don't set
5093 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 */
5095 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005096ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097{
5098 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005099 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005101 /* Don't check when reading keys from a script, :normal or feedkeys().
5102 * That would break the test scripts. But do check for keys when called
5103 * from complete_check(). */
5104 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 return;
5106
5107 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005108 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 return;
5110 count = 0;
5111
Bram Moolenaara260a972006-06-23 19:36:29 +00005112 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5113 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (c != NUL)
5116 {
5117 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5118 {
5119 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005120 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005121 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005122 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005124 else
5125 {
5126 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005127 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005128 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005129 if (c != K_IGNORE)
5130 {
5131 /* Don't interrupt completion when the character wasn't typed,
5132 * e.g., when doing @q to replay keys. */
5133 if (c != Ctrl_R && KeyTyped)
5134 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005135
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005136 vungetc(c);
5137 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005140 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005141 {
5142 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5143
5144 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005145 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005146 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005147}
5148
5149/*
5150 * Decide the direction of Insert mode complete from the key typed.
5151 * Returns BACKWARD or FORWARD.
5152 */
5153 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005154ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005155{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005156 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005157 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005158 return BACKWARD;
5159 return FORWARD;
5160}
5161
5162/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005163 * Return TRUE for keys that are used for completion only when the popup menu
5164 * is visible.
5165 */
5166 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005167ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005168{
5169 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005170 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5171 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005172}
5173
5174/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005175 * Decide the number of completions to move forward.
5176 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5177 */
5178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005179ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005180{
5181 int h;
5182
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005183 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005184 {
5185 h = pum_get_height();
5186 if (h > 3)
5187 h -= 2; /* keep some context */
5188 return h;
5189 }
5190 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191}
5192
5193/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005194 * Return TRUE if completion with "c" should insert the match, FALSE if only
5195 * to change the currently selected completion.
5196 */
5197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005198ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005199{
5200 switch (c)
5201 {
5202 case K_UP:
5203 case K_DOWN:
5204 case K_PAGEDOWN:
5205 case K_KPAGEDOWN:
5206 case K_S_DOWN:
5207 case K_PAGEUP:
5208 case K_KPAGEUP:
5209 case K_S_UP:
5210 return FALSE;
5211 }
5212 return TRUE;
5213}
5214
5215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 * Do Insert mode completion.
5217 * Called when character "c" was typed, which has a meaning for completion.
5218 * Returns OK if completion was done, FAIL if something failed (out of mem).
5219 */
5220 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005221ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 char_u *line;
5224 int startcol = 0; /* column where searched text starts */
5225 colnr_T curs_col; /* cursor column */
5226 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005227 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005228 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005229 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005230 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
Bram Moolenaare3226be2005-12-18 22:10:00 +00005232 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005233 insert_match = ins_compl_use_match(c);
5234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
5237 /* First time we hit ^N or ^P (in a row, I mean) */
5238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 did_ai = FALSE;
5240#ifdef FEAT_SMARTINDENT
5241 did_si = FALSE;
5242 can_si = FALSE;
5243 can_si_back = FALSE;
5244#endif
5245 if (stop_arrow() == FAIL)
5246 return FAIL;
5247
5248 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005250 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005252 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005253 * "compl_startpos" to the cursor as a pattern to add a new word
5254 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005255 * "compl_startpos" is not in the same line as the cursor then fix it
5256 * (the line has been split because it was longer than 'tw'). if SOL
5257 * is set then skip the previous pattern, a word at the beginning of
5258 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005259 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5260 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
5262 /*
5263 * it is a continued search
5264 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005266 if (ctrl_x_mode == CTRL_X_NORMAL
5267 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5268 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005270 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 /* line (probably) wrapped, set compl_startpos to the
5273 * first non_blank in the line, if it is not a wordchar
5274 * include it to get a better pattern, but then we don't
5275 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005276 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005277 compl_startpos.col = compl_col;
5278 compl_startpos.lnum = curwin->w_cursor.lnum;
5279 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
5281 else
5282 {
5283 /* S_IPOS was set when we inserted a word that was at the
5284 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005285 * mode but first we need to redefine compl_startpos */
5286 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 compl_cont_status |= CONT_SOL;
5289 compl_startpos.col = (colnr_T)(skipwhite(
5290 line + compl_length
5291 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005295 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005296 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005297 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 compl_cont_status &= ~CONT_SOL;
5302 compl_length = (IOSIZE - MIN_SPACE);
5303 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5306 if (compl_length < 1)
5307 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005309 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005312 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
5314 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005317 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005319 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005320 if (ctrl_x_mode != CTRL_X_NORMAL)
5321 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 compl_cont_status = 0;
5323 compl_cont_status |= CONT_N_ADDS;
5324 compl_startpos = curwin->w_cursor;
5325 startcol = (int)curs_col;
5326 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 }
5328
5329 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005330 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005332 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5334 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 compl_col += ++startcol;
5340 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 compl_pattern = str_foldcase(line + compl_col,
5344 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 compl_pattern = vim_strnsave(line + compl_col,
5347 compl_length);
5348 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 return FAIL;
5350 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
5353 char_u *prefix = (char_u *)"\\<";
5354
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005355 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005356 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005357 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 if (!vim_iswordp(line + compl_col)
5361 || (compl_col > 0
Bram Moolenaar13505972019-01-24 15:04:48 +01005362 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 STRCPY((char *)compl_pattern, prefix);
5365 (void)quote_meta(compl_pattern + STRLEN(prefix),
5366 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
Bram Moolenaar13505972019-01-24 15:04:48 +01005368 else if (--startcol < 0
5369 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
5371 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5373 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 compl_col += curs_col;
5376 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378 else
5379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 /* Search the point of change class of multibyte character
5381 * or not a word single byte character backward. */
5382 if (has_mbyte)
5383 {
5384 int base_class;
5385 int head_off;
5386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 startcol -= (*mb_head_off)(line, line + startcol);
5388 base_class = mb_get_class(line + startcol);
5389 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005391 head_off = (*mb_head_off)(line, line + startcol);
5392 if (base_class != mb_get_class(line + startcol
5393 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397 }
5398 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005401 compl_col += ++startcol;
5402 compl_length = (int)curs_col - startcol;
5403 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
5405 /* Only match word with at least two chars -- webb
5406 * there's no need to call quote_meta,
5407 * alloc(7) is enough -- Acevedo
5408 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 compl_pattern = alloc(7);
5410 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 STRCPY((char *)compl_pattern, "\\<");
5413 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5414 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 }
5416 else
5417 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005419 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005420 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005422 STRCPY((char *)compl_pattern, "\\<");
5423 (void)quote_meta(compl_pattern + 2, line + compl_col,
5424 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 }
5426 }
5427 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005428 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005430 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005431 compl_length = (int)curs_col - (int)compl_col;
5432 if (compl_length < 0) /* cursor in indent: empty pattern */
5433 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005435 compl_pattern = str_foldcase(line + compl_col, compl_length,
5436 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005438 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5439 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 return FAIL;
5441 }
5442 else if (ctrl_x_mode == CTRL_X_FILES)
5443 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005444 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005445 if (startcol > 0)
5446 {
5447 char_u *p = line + startcol;
5448
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005449 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005450 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005451 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005452 if (p == line && vim_isfilec(PTR2CHAR(p)))
5453 startcol = 0;
5454 else
5455 startcol = (int)(p - line) + 1;
5456 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005457
Bram Moolenaar0300e462013-09-08 16:03:45 +02005458 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005459 compl_length = (int)curs_col - startcol;
5460 compl_pattern = addstar(line + compl_col, compl_length,
5461 EXPAND_FILES);
5462 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 return FAIL;
5464 }
5465 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5466 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005467 compl_pattern = vim_strnsave(line, curs_col);
5468 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005471 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005472 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5473 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005474 /* No completion possible, use an empty pattern to get a
5475 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005476 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005477 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005478 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5479 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005481 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005482 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005483#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005484 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005485 * Call user defined function 'completefunc' with "a:findstart"
5486 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005487 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005488 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005489 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005490 char_u *funcname;
5491 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005492 win_T *curwin_save;
5493 buf_T *curbuf_save;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005494 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005495
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005496 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005497 * string */
5498 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5499 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5500 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005502 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005503 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005504 /* restore did_ai, so that adding comment leader works */
5505 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005506 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005507 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005508
Bram Moolenaarffa96842018-06-12 22:05:14 +02005509 args[0].v_type = VAR_NUMBER;
5510 args[0].vval.v_number = 1;
5511 args[1].v_type = VAR_STRING;
5512 args[1].vval.v_string = (char_u *)"";
5513 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005514 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005515 curwin_save = curwin;
5516 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005517 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005518
5519 State = save_State;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005520 if (curwin_save != curwin || curbuf_save != curbuf)
5521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005522 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005523 return FAIL;
5524 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005525 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005526 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005527 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005529 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005530 return FAIL;
5531 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005532
Bram Moolenaar6110a002012-01-26 18:58:38 +01005533 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005534 * cancel the complete without an error.
5535 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005536 if (col == -2)
5537 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005538 if (col == -3)
5539 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005540 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005541 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005542 if (!shortmess(SHM_COMPLETIONMENU))
5543 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005544 return FAIL;
5545 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005546
Bram Moolenaar82139082011-09-14 16:52:09 +02005547 /*
5548 * Reset extended parameters of completion, when start new
5549 * completion.
5550 */
5551 compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005552 compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +02005553
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005554 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005555 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005556 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005557 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005558 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005559
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005560 /* Setup variables for completion. Need to obtain "line" again,
5561 * it may have become invalid. */
5562 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005563 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005564 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5565 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005566#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005567 return FAIL;
5568 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005569 else if (ctrl_x_mode == CTRL_X_SPELL)
5570 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005571#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005572 if (spell_bad_len > 0)
5573 compl_col = curs_col - spell_bad_len;
5574 else
5575 compl_col = spell_word_start(startcol);
5576 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005577 {
5578 compl_length = 0;
5579 compl_col = curs_col;
5580 }
5581 else
5582 {
5583 spell_expand_check_cap(compl_col);
5584 compl_length = (int)curs_col - compl_col;
5585 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005586 /* Need to obtain "line" again, it may have become invalid. */
5587 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005588 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5589 if (compl_pattern == NULL)
5590#endif
5591 return FAIL;
5592 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005593 else
5594 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005595 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005596 return FAIL;
5597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005599 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
5601 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005602 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
5604 /* Insert a new line, keep indentation but ignore 'comments' */
5605#ifdef FEAT_COMMENTS
5606 char_u *old = curbuf->b_p_com;
5607
5608 curbuf->b_p_com = (char_u *)"";
5609#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005610 compl_startpos.lnum = curwin->w_cursor.lnum;
5611 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 ins_eol('\r');
5613#ifdef FEAT_COMMENTS
5614 curbuf->b_p_com = old;
5615#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005616 compl_length = 0;
5617 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619 }
5620 else
5621 {
5622 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005623 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
5625
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005626 if (compl_cont_status & CONT_LOCAL)
5627 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 else
5629 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5630
Bram Moolenaar62951b12011-09-21 18:23:05 +02005631 /* If any of the original typed text has been changed we need to fix
5632 * the redo buffer. */
5633 ins_compl_fixRedoBufForLeader(NULL);
5634
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005635 /* Always add completion for the original text. */
5636 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005637 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5638 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005639 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005641 VIM_CLEAR(compl_pattern);
5642 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 return FAIL;
5644 }
5645
5646 /* showmode might reset the internal line pointers, so it must
5647 * be called before line = ml_get(), or when this address is no
5648 * longer needed. -- Acevedo.
5649 */
5650 edit_submode_extra = (char_u *)_("-- Searching...");
5651 edit_submode_highl = HLF_COUNT;
5652 showmode();
5653 edit_submode_extra = NULL;
5654 out_flush();
5655 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005656 else if (insert_match && stop_arrow() == FAIL)
5657 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005659 compl_shown_match = compl_curr_match;
5660 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005663 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005665 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005666 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005667 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005669 /* may undisplay the popup menu */
5670 ins_compl_upd_pum();
5671
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005672 if (n > 1) /* all matches have been found */
5673 compl_matches = n;
5674 compl_curr_match = compl_shown_match;
5675 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
Bram Moolenaard68071d2006-05-02 22:08:30 +00005677 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5678 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (got_int && !global_busy)
5680 {
5681 (void)vgetc();
5682 got_int = FALSE;
5683 }
5684
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005685 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005686 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005688 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5689 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5691 edit_submode_highl = HLF_E;
5692 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5693 * because we couldn't expand anything at first place, but if we used
5694 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5695 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005696 if ( compl_length > 1
5697 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005698 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5700 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005701 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
5703
Bram Moolenaar572cb562005-08-05 21:35:02 +00005704 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005705 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005707 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708
5709 if (edit_submode_extra == NULL)
5710 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005711 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
5713 edit_submode_extra = (char_u *)_("Back at original");
5714 edit_submode_highl = HLF_W;
5715 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005716 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
5718 edit_submode_extra = (char_u *)_("Word from other line");
5719 edit_submode_highl = HLF_COUNT;
5720 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005721 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 edit_submode_extra = (char_u *)_("The only match");
5724 edit_submode_highl = HLF_COUNT;
5725 }
5726 else
5727 {
5728 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005729 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005731 int number = 0;
5732 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005734 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
5736 /* search backwards for the first valid (!= -1) number.
5737 * This should normally succeed already at the first loop
5738 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005739 for (match = compl_curr_match->cp_prev; match != NULL
5740 && match != compl_first_match;
5741 match = match->cp_prev)
5742 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005744 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 break;
5746 }
5747 if (match != NULL)
5748 /* go up and assign all numbers which are not assigned
5749 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005750 for (match = match->cp_next;
5751 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005752 match = match->cp_next)
5753 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755 else /* BACKWARD */
5756 {
5757 /* search forwards (upwards) for the first valid (!= -1)
5758 * number. This should normally succeed already at the
5759 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005760 for (match = compl_curr_match->cp_next; match != NULL
5761 && match != compl_first_match;
5762 match = match->cp_next)
5763 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005765 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 break;
5767 }
5768 if (match != NULL)
5769 /* go down and assign all numbers which are not
5770 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005771 for (match = match->cp_prev; match
5772 && match->cp_number == -1;
5773 match = match->cp_prev)
5774 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776 }
5777
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005778 /* The match should always have a sequence number now, this is
5779 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005780 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005782 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5783 * Translations may need more than twice that. */
5784 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005786 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005787 vim_snprintf((char *)match_ref, sizeof(match_ref),
5788 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005789 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005791 vim_snprintf((char *)match_ref, sizeof(match_ref),
5792 _("match %d"),
5793 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 edit_submode_extra = match_ref;
5795 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005796 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 curs_columns(FALSE);
5798 }
5799 }
5800 }
5801
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005802 // Show a message about what (completion) mode we're in.
5803 if (!compl_opt_suppress_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005805 showmode();
5806 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaarea389e92014-05-28 21:40:52 +02005807 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005808 if (edit_submode_extra != NULL)
5809 {
5810 if (!p_smd)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005811 msg_attr((char *)edit_submode_extra,
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005812 edit_submode_highl < HLF_COUNT
5813 ? HL_ATTR(edit_submode_highl) : 0);
5814 }
5815 else
5816 msg_clr_cmdline(); // necessary for "noshowmode"
Bram Moolenaarea389e92014-05-28 21:40:52 +02005817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
Bram Moolenaard68071d2006-05-02 22:08:30 +00005820 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005821 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005822 show_pum(save_w_wrow, save_w_leftcol);
5823
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005824 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005825 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 return OK;
5828}
5829
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005830 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005831show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005832{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005833 /* RedrawingDisabled may be set when invoked through complete(). */
5834 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005835
Bram Moolenaarcb036422017-03-01 12:29:10 +01005836 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005837
Bram Moolenaarcb036422017-03-01 12:29:10 +01005838 /* If the cursor moved or the display scrolled we need to remove the pum
5839 * first. */
5840 setcursor();
5841 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5842 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005843
Bram Moolenaarcb036422017-03-01 12:29:10 +01005844 ins_compl_show_pum();
5845 setcursor();
5846 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005847}
5848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849/*
5850 * Looks in the first "len" chars. of "src" for search-metachars.
5851 * If dest is not NULL the chars. are copied there quoting (with
5852 * a backslash) the metachars, and dest would be NUL terminated.
5853 * Returns the length (needed) of dest
5854 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005855 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005856quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005858 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005860 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {
5862 switch (*src)
5863 {
5864 case '.':
5865 case '*':
5866 case '[':
5867 if (ctrl_x_mode == CTRL_X_DICTIONARY
5868 || ctrl_x_mode == CTRL_X_THESAURUS)
5869 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005870 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 case '~':
5872 if (!p_magic) /* quote these only if magic is set */
5873 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005874 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 case '\\':
5876 if (ctrl_x_mode == CTRL_X_DICTIONARY
5877 || ctrl_x_mode == CTRL_X_THESAURUS)
5878 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005879 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 case '^': /* currently it's not needed. */
5881 case '$':
5882 m++;
5883 if (dest != NULL)
5884 *dest++ = '\\';
5885 break;
5886 }
5887 if (dest != NULL)
5888 *dest++ = *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 /* Copy remaining bytes of a multibyte character. */
5890 if (has_mbyte)
5891 {
5892 int i, mb_len;
5893
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005894 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (mb_len > 0 && len >= mb_len)
5896 for (i = 0; i < mb_len; ++i)
5897 {
5898 --len;
5899 ++src;
5900 if (dest != NULL)
5901 *dest++ = *src;
5902 }
5903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
5905 if (dest != NULL)
5906 *dest = NUL;
5907
5908 return m;
5909}
5910#endif /* FEAT_INS_EXPAND */
5911
5912/*
5913 * Next character is interpreted literally.
5914 * A one, two or three digit decimal number is interpreted as its byte value.
5915 * If one or two digits are entered, the next character is given to vungetc().
5916 * For Unicode a character > 255 may be returned.
5917 */
5918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005919get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 int cc;
5922 int nc;
5923 int i;
5924 int hex = FALSE;
5925 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
5928 if (got_int)
5929 return Ctrl_C;
5930
5931#ifdef FEAT_GUI
5932 /*
5933 * In GUI there is no point inserting the internal code for a special key.
5934 * It is more useful to insert the string "<KEY>" instead. This would
5935 * probably be useful in a text window too, but it would not be
5936 * vi-compatible (maybe there should be an option for it?) -- webb
5937 */
5938 if (gui.in_use)
5939 ++allow_keys;
5940#endif
5941#ifdef USE_ON_FLY_SCROLL
5942 dont_scroll = TRUE; /* disallow scrolling here */
5943#endif
5944 ++no_mapping; /* don't map the next key hits */
5945 cc = 0;
5946 i = 0;
5947 for (;;)
5948 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005949 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01005951 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 add_to_showcmd(nc);
5953#endif
5954 if (nc == 'x' || nc == 'X')
5955 hex = TRUE;
5956 else if (nc == 'o' || nc == 'O')
5957 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 else if (nc == 'u' || nc == 'U')
5959 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 else
5961 {
Bram Moolenaar13505972019-01-24 15:04:48 +01005962 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 {
5964 if (!vim_isxdigit(nc))
5965 break;
5966 cc = cc * 16 + hex2nr(nc);
5967 }
5968 else if (octal)
5969 {
5970 if (nc < '0' || nc > '7')
5971 break;
5972 cc = cc * 8 + nc - '0';
5973 }
5974 else
5975 {
5976 if (!VIM_ISDIGIT(nc))
5977 break;
5978 cc = cc * 10 + nc - '0';
5979 }
5980
5981 ++i;
5982 }
5983
Bram Moolenaar13505972019-01-24 15:04:48 +01005984 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 cc = 255; /* limit range to 0-255 */
5986 nc = 0;
5987
5988 if (hex) /* hex: up to two chars */
5989 {
5990 if (i >= 2)
5991 break;
5992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 else if (unicode) /* Unicode: up to four or eight chars */
5994 {
5995 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5996 break;
5997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 else if (i >= 3) /* decimal or octal: up to three chars */
5999 break;
6000 }
6001 if (i == 0) /* no number entered */
6002 {
6003 if (nc == K_ZERO) /* NUL is stored as NL */
6004 {
6005 cc = '\n';
6006 nc = 0;
6007 }
6008 else
6009 {
6010 cc = nc;
6011 nc = 0;
6012 }
6013 }
6014
6015 if (cc == 0) /* NUL is stored as NL */
6016 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006017 if (enc_dbcs && (cc & 0xff) == 0)
6018 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6019 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
6021 --no_mapping;
6022#ifdef FEAT_GUI
6023 if (gui.in_use)
6024 --allow_keys;
6025#endif
6026 if (nc)
6027 vungetc(nc);
6028 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6029 return cc;
6030}
6031
6032/*
6033 * Insert character, taking care of special keys and mod_mask
6034 */
6035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006036insert_special(
6037 int c,
6038 int allow_modmask,
6039 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
6041 char_u *p;
6042 int len;
6043
6044 /*
6045 * Special function key, translate into "<Key>". Up to the last '>' is
6046 * inserted with ins_str(), so as not to replace characters in replace
6047 * mode.
6048 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6049 * unless 'allow_modmask' is TRUE.
6050 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006051#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 /* Command-key never produces a normal key */
6053 if (mod_mask & MOD_MASK_CMD)
6054 allow_modmask = TRUE;
6055#endif
6056 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6057 {
6058 p = get_special_key_name(c, mod_mask);
6059 len = (int)STRLEN(p);
6060 c = p[len - 1];
6061 if (len > 2)
6062 {
6063 if (stop_arrow() == FAIL)
6064 return;
6065 p[len - 1] = NUL;
6066 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006067 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 ctrlv = FALSE;
6069 }
6070 }
6071 if (stop_arrow() == OK)
6072 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6073}
6074
6075/*
6076 * Special characters in this context are those that need processing other
6077 * than the simple insertion that can be performed here. This includes ESC
6078 * which terminates the insert, and CR/NL which need special processing to
6079 * open up a new line. This routine tries to optimize insertions performed by
6080 * the "redo", "undo" or "put" commands, so it needs to know when it should
6081 * stop and defer processing to the "normal" mechanism.
6082 * '0' and '^' are special, because they can be followed by CTRL-D.
6083 */
6084#ifdef EBCDIC
6085# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6086#else
6087# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6088#endif
6089
Bram Moolenaar13505972019-01-24 15:04:48 +01006090#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006092/*
6093 * "flags": INSCHAR_FORMAT - force formatting
6094 * INSCHAR_CTRLV - char typed just after CTRL-V
6095 * INSCHAR_NO_FEX - don't use 'formatexpr'
6096 *
6097 * NOTE: passes the flags value straight through to internal_format() which,
6098 * beside INSCHAR_FORMAT (above), is also looking for these:
6099 * INSCHAR_DO_COM - format comments
6100 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006103insertchar(
6104 int c, /* character to insert or NUL */
6105 int flags, /* INSCHAR_FORMAT, etc. */
6106 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 int textwidth;
6109#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006113 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006115 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
6118 /*
6119 * Try to break the line in two or more pieces when:
6120 * - Always do this if we have been called to do formatting only.
6121 * - Always do this when 'formatoptions' has the 'a' flag and the line
6122 * ends in white space.
6123 * - Otherwise:
6124 * - Don't do this if inserting a blank
6125 * - Don't do this if an existing character is being replaced, unless
6126 * we're in VREPLACE mode.
6127 * - Do this if the cursor is not on the line where insert started
6128 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6129 * before the insert.
6130 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6131 * before 'textwidth'
6132 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006133 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006134 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006135 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 && *ml_get_cursor() != NUL)
6139 && (curwin->w_cursor.lnum != Insstart.lnum
6140 || ((!has_format_option(FO_INS_LONG)
6141 || Insstart_textlen <= (colnr_T)textwidth)
6142 && (!fo_ins_blank
6143 || Insstart_blank_vcol <= (colnr_T)textwidth
6144 ))))))
6145 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006146 /* Format with 'formatexpr' when it's set. Use internal formatting
6147 * when 'formatexpr' isn't set or it returns non-zero. */
6148#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006149 int do_internal = TRUE;
6150 colnr_T virtcol = get_nolist_virtcol()
6151 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006152
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006153 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6154 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006155 {
6156 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6157 /* It may be required to save for undo again, e.g. when setline()
6158 * was called. */
6159 ins_need_undo = TRUE;
6160 }
6161 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006163 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (c == NUL) /* only formatting was wanted */
6167 return;
6168
6169#ifdef FEAT_COMMENTS
6170 /* Check whether this character should end a comment. */
6171 if (did_ai && (int)c == end_comment_pending)
6172 {
6173 char_u *line;
6174 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6175 int middle_len, end_len;
6176 int i;
6177
6178 /*
6179 * Need to remove existing (middle) comment leader and insert end
6180 * comment leader. First, check what comment leader we can find.
6181 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006182 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6184 {
6185 /* Skip middle-comment string */
6186 while (*p && p[-1] != ':') /* find end of middle flags */
6187 ++p;
6188 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6189 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006190 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 --middle_len;
6192
6193 /* Find the end-comment string */
6194 while (*p && p[-1] != ':') /* find end of end flags */
6195 ++p;
6196 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6197
6198 /* Skip white space before the cursor */
6199 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006200 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 ;
6202 i++;
6203
6204 /* Skip to before the middle leader */
6205 i -= middle_len;
6206
6207 /* Check some expected things before we go on */
6208 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6209 {
6210 /* Backspace over all the stuff we want to replace */
6211 backspace_until_column(i);
6212
6213 /*
6214 * Insert the end-comment string, except for the last
6215 * character, which will get inserted as normal later.
6216 */
6217 ins_bytes_len(lead_end, end_len - 1);
6218 }
6219 }
6220 }
6221 end_comment_pending = NUL;
6222#endif
6223
6224 did_ai = FALSE;
6225#ifdef FEAT_SMARTINDENT
6226 did_si = FALSE;
6227 can_si = FALSE;
6228 can_si_back = FALSE;
6229#endif
6230
6231 /*
6232 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6233 * This speeds up normal text input considerably.
6234 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6235 * need to re-indent at a ':', or any other character (but not what
6236 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006237 * Don't do this when there an InsertCharPre autocommand is defined,
6238 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006239 * Do the check for InsertCharPre before the call to vpeekc() because the
6240 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 */
6242#ifdef USE_ON_FLY_SCROLL
6243 dont_scroll = FALSE; /* allow scrolling here */
6244#endif
6245
6246 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02006248 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 && vpeekc() != NUL
6250 && !(State & REPLACE_FLAG)
6251#ifdef FEAT_CINDENT
6252 && !cindent_on()
6253#endif
6254#ifdef FEAT_RIGHTLEFT
6255 && !p_ri
6256#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006257 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {
6259#define INPUT_BUFLEN 100
6260 char_u buf[INPUT_BUFLEN + 1];
6261 int i;
6262 colnr_T virtcol = 0;
6263
6264 buf[0] = c;
6265 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 virtcol = get_nolist_virtcol();
6268 /*
6269 * Stop the string when:
6270 * - no more chars available
6271 * - finding a special character (command key)
6272 * - buffer is full
6273 * - running into the 'textwidth' boundary
6274 * - need to check for abbreviation: A non-word char after a word-char
6275 */
6276 while ( (c = vpeekc()) != NUL
6277 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006280# ifdef FEAT_FKMAP
6281 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 && (textwidth == 0
6284 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6285 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6286 {
6287#ifdef FEAT_RIGHTLEFT
6288 c = vgetc();
6289 if (p_hkmap && KeyTyped)
6290 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 buf[i++] = c;
6292#else
6293 buf[i++] = vgetc();
6294#endif
6295 }
6296
6297#ifdef FEAT_DIGRAPHS
6298 do_digraph(-1); /* clear digraphs */
6299 do_digraph(buf[i-1]); /* may be the start of a digraph */
6300#endif
6301 buf[i] = NUL;
6302 ins_str(buf);
6303 if (flags & INSCHAR_CTRLV)
6304 {
6305 redo_literal(*buf);
6306 i = 1;
6307 }
6308 else
6309 i = 0;
6310 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006311 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 }
6313 else
6314 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006315 int cc;
6316
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6318 {
6319 char_u buf[MB_MAXBYTES + 1];
6320
6321 (*mb_char2bytes)(c, buf);
6322 buf[cc] = NUL;
6323 ins_char_bytes(buf, cc);
6324 AppendCharToRedobuff(c);
6325 }
6326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 {
6328 ins_char(c);
6329 if (flags & INSCHAR_CTRLV)
6330 redo_literal(c);
6331 else
6332 AppendCharToRedobuff(c);
6333 }
6334 }
6335}
6336
6337/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006338 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006339 *
6340 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6341 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006342 */
6343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006344internal_format(
6345 int textwidth,
6346 int second_indent,
6347 int flags,
6348 int format_only,
6349 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006350{
6351 int cc;
6352 int save_char = NUL;
6353 int haveto_redraw = FALSE;
6354 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006355 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006356 int fo_white_par = has_format_option(FO_WHITE_PAR);
6357 int first_line = TRUE;
6358#ifdef FEAT_COMMENTS
6359 colnr_T leader_len;
6360 int no_leader = FALSE;
6361 int do_comments = (flags & INSCHAR_DO_COM);
6362#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006363#ifdef FEAT_LINEBREAK
6364 int has_lbr = curwin->w_p_lbr;
6365
6366 /* make sure win_lbr_chartabsize() counts correctly */
6367 curwin->w_p_lbr = FALSE;
6368#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006369
6370 /*
6371 * When 'ai' is off we don't want a space under the cursor to be
6372 * deleted. Replace it with an 'x' temporarily.
6373 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006374 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006375 {
6376 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006377 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006378 {
6379 save_char = cc;
6380 pchar_cursor('x');
6381 }
6382 }
6383
6384 /*
6385 * Repeat breaking lines, until the current line is not too long.
6386 */
6387 while (!got_int)
6388 {
6389 int startcol; /* Cursor column at entry */
6390 int wantcol; /* column at textwidth border */
6391 int foundcol; /* column for start of spaces */
6392 int end_foundcol = 0; /* column for start of word */
6393 colnr_T len;
6394 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006395 int orig_col = 0;
6396 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006397 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006398 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006399 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006400
Bram Moolenaar97b98102009-11-17 16:41:01 +00006401 virtcol = get_nolist_virtcol()
6402 + char2cells(c != NUL ? c : gchar_cursor());
6403 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006404 break;
6405
6406#ifdef FEAT_COMMENTS
6407 if (no_leader)
6408 do_comments = FALSE;
6409 else if (!(flags & INSCHAR_FORMAT)
6410 && has_format_option(FO_WRAP_COMS))
6411 do_comments = TRUE;
6412
6413 /* Don't break until after the comment leader */
6414 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006415 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006416 else
6417 leader_len = 0;
6418
6419 /* If the line doesn't start with a comment leader, then don't
6420 * start one in a following broken line. Avoids that a %word
6421 * moved to the start of the next line causes all following lines
6422 * to start with %. */
6423 if (leader_len == 0)
6424 no_leader = TRUE;
6425#endif
6426 if (!(flags & INSCHAR_FORMAT)
6427#ifdef FEAT_COMMENTS
6428 && leader_len == 0
6429#endif
6430 && !has_format_option(FO_WRAP))
6431
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006432 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433 if ((startcol = curwin->w_cursor.col) == 0)
6434 break;
6435
6436 /* find column of textwidth border */
6437 coladvance((colnr_T)textwidth);
6438 wantcol = curwin->w_cursor.col;
6439
Bram Moolenaar97b98102009-11-17 16:41:01 +00006440 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006441 foundcol = 0;
6442
6443 /*
6444 * Find position to break at.
6445 * Stop at first entered white when 'formatoptions' has 'v'
6446 */
6447 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006448 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449 || curwin->w_cursor.lnum != Insstart.lnum
6450 || curwin->w_cursor.col >= Insstart.col)
6451 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006452 if (curwin->w_cursor.col == startcol && c != NUL)
6453 cc = c;
6454 else
6455 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006456 if (WHITECHAR(cc))
6457 {
6458 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006459 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006461 // find start of sequence of blanks
6462 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006463 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6464 {
6465 dec_cursor();
6466 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006467
6468 // Increment count of how many whitespace chars in this
6469 // group; we only need to know if it's more than one.
6470 if (wcc < 2)
6471 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006472 }
6473 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6474 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006475
6476 // Don't break after a period when 'formatoptions' has 'p' and
6477 // there are less than two spaces.
6478 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
6479 continue;
6480
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006481#ifdef FEAT_COMMENTS
6482 /* Don't break until after the comment leader */
6483 if (curwin->w_cursor.col < leader_len)
6484 break;
6485#endif
6486 if (has_format_option(FO_ONE_LETTER))
6487 {
6488 /* do not break after one-letter words */
6489 if (curwin->w_cursor.col == 0)
6490 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006491#ifdef FEAT_COMMENTS
6492 /* do not break "#a b" when 'tw' is 2 */
6493 if (curwin->w_cursor.col <= leader_len)
6494 break;
6495#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006496 col = curwin->w_cursor.col;
6497 dec_cursor();
6498 cc = gchar_cursor();
6499
6500 if (WHITECHAR(cc))
6501 continue; /* one-letter, continue */
6502 curwin->w_cursor.col = col;
6503 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006504
6505 inc_cursor();
6506
6507 end_foundcol = end_col + 1;
6508 foundcol = curwin->w_cursor.col;
6509 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006510 break;
6511 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006512 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006513 {
6514 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006515 if (curwin->w_cursor.col != startcol)
6516 {
6517#ifdef FEAT_COMMENTS
6518 /* Don't break until after the comment leader */
6519 if (curwin->w_cursor.col < leader_len)
6520 break;
6521#endif
6522 col = curwin->w_cursor.col;
6523 inc_cursor();
6524 /* Don't change end_foundcol if already set. */
6525 if (foundcol != curwin->w_cursor.col)
6526 {
6527 foundcol = curwin->w_cursor.col;
6528 end_foundcol = foundcol;
6529 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6530 break;
6531 }
6532 curwin->w_cursor.col = col;
6533 }
6534
6535 if (curwin->w_cursor.col == 0)
6536 break;
6537
6538 col = curwin->w_cursor.col;
6539
6540 dec_cursor();
6541 cc = gchar_cursor();
6542
6543 if (WHITECHAR(cc))
6544 continue; /* break with space */
6545#ifdef FEAT_COMMENTS
6546 /* Don't break until after the comment leader */
6547 if (curwin->w_cursor.col < leader_len)
6548 break;
6549#endif
6550
6551 curwin->w_cursor.col = col;
6552
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006553 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006554 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006555 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6556 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006557 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006558 if (curwin->w_cursor.col == 0)
6559 break;
6560 dec_cursor();
6561 }
6562
6563 if (foundcol == 0) /* no spaces, cannot break line */
6564 {
6565 curwin->w_cursor.col = startcol;
6566 break;
6567 }
6568
6569 /* Going to break the line, remove any "$" now. */
6570 undisplay_dollar();
6571
6572 /*
6573 * Offset between cursor position and line break is used by replace
6574 * stack functions. VREPLACE does not use this, and backspaces
6575 * over the text instead.
6576 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006577 if (State & VREPLACE_FLAG)
6578 orig_col = startcol; /* Will start backspacing from here */
6579 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006580 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006581
6582 /*
6583 * adjust startcol for spaces that will be deleted and
6584 * characters that will remain on top line
6585 */
6586 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006587 while ((cc = gchar_cursor(), WHITECHAR(cc))
6588 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006589 inc_cursor();
6590 startcol -= curwin->w_cursor.col;
6591 if (startcol < 0)
6592 startcol = 0;
6593
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006594 if (State & VREPLACE_FLAG)
6595 {
6596 /*
6597 * In VREPLACE mode, we will backspace over the text to be
6598 * wrapped, so save a copy now to put on the next line.
6599 */
6600 saved_text = vim_strsave(ml_get_cursor());
6601 curwin->w_cursor.col = orig_col;
6602 if (saved_text == NULL)
6603 break; /* Can't do it, out of memory */
6604 saved_text[startcol] = NUL;
6605
6606 /* Backspace over characters that will move to the next line */
6607 if (!fo_white_par)
6608 backspace_until_column(foundcol);
6609 }
6610 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006611 {
6612 /* put cursor after pos. to break line */
6613 if (!fo_white_par)
6614 curwin->w_cursor.col = foundcol;
6615 }
6616
6617 /*
6618 * Split the line just before the margin.
6619 * Only insert/delete lines, but don't really redraw the window.
6620 */
6621 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6622 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6623#ifdef FEAT_COMMENTS
6624 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006625 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006626#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006627 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6628 if (!(flags & INSCHAR_COM_LIST))
6629 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006630
6631 replace_offset = 0;
6632 if (first_line)
6633 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006634 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006635 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006636 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006637 * This section is for auto-wrap of numeric lists. When not
6638 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6639 * flag will be set and open_line() will handle it (as seen
6640 * above). The code here (and in get_number_indent()) will
6641 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006642 */
6643 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006644 second_indent =
6645 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006646 if (second_indent >= 0)
6647 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006648 if (State & VREPLACE_FLAG)
6649 change_indent(INDENT_SET, second_indent,
6650 FALSE, NUL, TRUE);
6651 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006652#ifdef FEAT_COMMENTS
6653 if (leader_len > 0 && second_indent - leader_len > 0)
6654 {
6655 int i;
6656 int padding = second_indent - leader_len;
6657
6658 /* We started at the first_line of a numbered list
6659 * that has a comment. the open_line() function has
6660 * inserted the proper comment leader and positioned
6661 * the cursor at the end of the split line. Now we
6662 * add the additional whitespace needed after the
6663 * comment leader for the numbered list. */
6664 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006665 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006666 }
6667 else
6668 {
6669#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006670 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006671#ifdef FEAT_COMMENTS
6672 }
6673#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006674 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006675 }
6676 first_line = FALSE;
6677 }
6678
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006679 if (State & VREPLACE_FLAG)
6680 {
6681 /*
6682 * In VREPLACE mode we have backspaced over the text to be
6683 * moved, now we re-insert it into the new line.
6684 */
6685 ins_bytes(saved_text);
6686 vim_free(saved_text);
6687 }
6688 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006689 {
6690 /*
6691 * Check if cursor is not past the NUL off the line, cindent
6692 * may have added or removed indent.
6693 */
6694 curwin->w_cursor.col += startcol;
6695 len = (colnr_T)STRLEN(ml_get_curline());
6696 if (curwin->w_cursor.col > len)
6697 curwin->w_cursor.col = len;
6698 }
6699
6700 haveto_redraw = TRUE;
6701#ifdef FEAT_CINDENT
6702 can_cindent = TRUE;
6703#endif
6704 /* moved the cursor, don't autoindent or cindent now */
6705 did_ai = FALSE;
6706#ifdef FEAT_SMARTINDENT
6707 did_si = FALSE;
6708 can_si = FALSE;
6709 can_si_back = FALSE;
6710#endif
6711 line_breakcheck();
6712 }
6713
6714 if (save_char != NUL) /* put back space after cursor */
6715 pchar_cursor(save_char);
6716
Bram Moolenaar0026d472014-09-09 16:32:39 +02006717#ifdef FEAT_LINEBREAK
6718 curwin->w_p_lbr = has_lbr;
6719#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006720 if (!format_only && haveto_redraw)
6721 {
6722 update_topline();
6723 redraw_curbuf_later(VALID);
6724 }
6725}
6726
6727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 * Called after inserting or deleting text: When 'formatoptions' includes the
6729 * 'a' flag format from the current line until the end of the paragraph.
6730 * Keep the cursor at the same position relative to the text.
6731 * The caller must have saved the cursor line for undo, following ones will be
6732 * saved here.
6733 */
6734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735auto_format(
6736 int trailblank, /* when TRUE also format with trailing blank */
6737 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 pos_T pos;
6740 colnr_T len;
6741 char_u *old;
6742 char_u *new, *pnew;
6743 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006744 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
6746 if (!has_format_option(FO_AUTO))
6747 return;
6748
6749 pos = curwin->w_cursor;
6750 old = ml_get_curline();
6751
6752 /* may remove added space */
6753 check_auto_format(FALSE);
6754
6755 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6756 * user might insert normal text next. Also skip formatting when "1" is
6757 * in 'formatoptions' and there is a single character before the cursor.
6758 * Otherwise the line would be broken and when typing another non-white
6759 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006760 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 if (*old != NUL && !trailblank && wasatend)
6762 {
6763 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006764 cc = gchar_cursor();
6765 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6766 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006768 cc = gchar_cursor();
6769 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {
6771 curwin->w_cursor = pos;
6772 return;
6773 }
6774 curwin->w_cursor = pos;
6775 }
6776
6777#ifdef FEAT_COMMENTS
6778 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6779 * comments. */
6780 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006781 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 return;
6783#endif
6784
6785 /*
6786 * May start formatting in a previous line, so that after "x" a word is
6787 * moved to the previous line if it fits there now. Only when this is not
6788 * the start of a paragraph.
6789 */
6790 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6791 {
6792 --curwin->w_cursor.lnum;
6793 if (u_save_cursor() == FAIL)
6794 return;
6795 }
6796
6797 /*
6798 * Do the formatting and restore the cursor position. "saved_cursor" will
6799 * be adjusted for the text formatting.
6800 */
6801 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006802 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 curwin->w_cursor = saved_cursor;
6804 saved_cursor.lnum = 0;
6805
6806 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6807 {
6808 /* "cannot happen" */
6809 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6810 coladvance((colnr_T)MAXCOL);
6811 }
6812 else
6813 check_cursor_col();
6814
6815 /* Insert mode: If the cursor is now after the end of the line while it
6816 * previously wasn't, the line was broken. Because of the rule above we
6817 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6818 * formatted. */
6819 if (!wasatend && has_format_option(FO_WHITE_PAR))
6820 {
6821 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006822 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 if (curwin->w_cursor.col == len)
6824 {
6825 pnew = vim_strnsave(new, len + 2);
6826 pnew[len] = ' ';
6827 pnew[len + 1] = NUL;
6828 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6829 /* remove the space later */
6830 did_add_space = TRUE;
6831 }
6832 else
6833 /* may remove added space */
6834 check_auto_format(FALSE);
6835 }
6836
6837 check_cursor();
6838}
6839
6840/*
6841 * When an extra space was added to continue a paragraph for auto-formatting,
6842 * delete it now. The space must be under the cursor, just after the insert
6843 * position.
6844 */
6845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006846check_auto_format(
6847 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006850 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
6852 if (did_add_space)
6853 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006854 cc = gchar_cursor();
6855 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 /* Somehow the space was removed already. */
6857 did_add_space = FALSE;
6858 else
6859 {
6860 if (!end_insert)
6861 {
6862 inc_cursor();
6863 c = gchar_cursor();
6864 dec_cursor();
6865 }
6866 if (c != NUL)
6867 {
6868 /* The space is no longer at the end of the line, delete it. */
6869 del_char(FALSE);
6870 did_add_space = FALSE;
6871 }
6872 }
6873 }
6874}
6875
6876/*
6877 * Find out textwidth to be used for formatting:
6878 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006879 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 * if invalid value, use 0.
6881 * Set default to window width (maximum 79) for "gq" operator.
6882 */
6883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006884comp_textwidth(
6885 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 int textwidth;
6888
6889 textwidth = curbuf->b_p_tw;
6890 if (textwidth == 0 && curbuf->b_p_wm)
6891 {
6892 /* The width is the window width minus 'wrapmargin' minus all the
6893 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006894 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895#ifdef FEAT_CMDWIN
6896 if (cmdwin_type != 0)
6897 textwidth -= 1;
6898#endif
6899#ifdef FEAT_FOLDING
6900 textwidth -= curwin->w_p_fdc;
6901#endif
6902#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006903 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 textwidth -= 1;
6905#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006906 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 textwidth -= 8;
6908 }
6909 if (textwidth < 0)
6910 textwidth = 0;
6911 if (ff && textwidth == 0)
6912 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006913 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (textwidth > 79)
6915 textwidth = 79;
6916 }
6917 return textwidth;
6918}
6919
6920/*
6921 * Put a character in the redo buffer, for when just after a CTRL-V.
6922 */
6923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006924redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 char_u buf[10];
6927
6928 /* Only digits need special treatment. Translate them into a string of
6929 * three digits. */
6930 if (VIM_ISDIGIT(c))
6931 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006932 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 AppendToRedobuff(buf);
6934 }
6935 else
6936 AppendCharToRedobuff(c);
6937}
6938
6939/*
6940 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006941 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 */
6943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006944start_arrow(
6945 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006947 start_arrow_common(end_insert_pos, TRUE);
6948}
6949
6950/*
6951 * Like start_arrow() but with end_change argument.
6952 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6953 */
6954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006955start_arrow_with_change(
6956 pos_T *end_insert_pos, /* can be NULL */
6957 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006958{
6959 start_arrow_common(end_insert_pos, end_change);
6960 if (!end_change)
6961 {
6962 AppendCharToRedobuff(Ctrl_G);
6963 AppendCharToRedobuff('U');
6964 }
6965}
6966
6967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006968start_arrow_common(
6969 pos_T *end_insert_pos, /* can be NULL */
6970 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006971{
6972 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
6974 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006975 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 arrow_used = TRUE; /* this means we stopped the current insert */
6977 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006978#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006979 check_spell_redraw();
6980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981}
6982
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006983#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006984/*
6985 * If we skipped highlighting word at cursor, do it now.
6986 * It may be skipped again, thus reset spell_redraw_lnum first.
6987 */
6988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006989check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006990{
6991 if (spell_redraw_lnum != 0)
6992 {
6993 linenr_T lnum = spell_redraw_lnum;
6994
6995 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01006996 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00006997 }
6998}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006999
7000/*
7001 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7002 * spelled word, if there is one.
7003 */
7004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007006{
7007 pos_T tpos = curwin->w_cursor;
7008
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007009 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007010 if (curwin->w_cursor.col != tpos.col)
7011 start_arrow(&tpos);
7012}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007013#endif
7014
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015/*
7016 * stop_arrow() is called before a change is made in insert mode.
7017 * If an arrow key has been used, start a new insertion.
7018 * Returns FAIL if undo is impossible, shouldn't insert then.
7019 */
7020 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007021stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 if (arrow_used)
7024 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007025 Insstart = curwin->w_cursor; /* new insertion starts here */
7026 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7027 /* Don't update the original insert position when moved to the
7028 * right, except when nothing was inserted yet. */
7029 update_Insstart_orig = FALSE;
7030 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7031
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 if (u_save_cursor() == OK)
7033 {
7034 arrow_used = FALSE;
7035 ins_need_undo = FALSE;
7036 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 if (State & VREPLACE_FLAG)
7040 {
7041 orig_line_count = curbuf->b_ml.ml_line_count;
7042 vr_lines_changed = 1;
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 ResetRedobuff();
7045 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007046 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 }
7048 else if (ins_need_undo)
7049 {
7050 if (u_save_cursor() == OK)
7051 ins_need_undo = FALSE;
7052 }
7053
7054#ifdef FEAT_FOLDING
7055 /* Always open fold at the cursor line when inserting something. */
7056 foldOpenCursor();
7057#endif
7058
7059 return (arrow_used || ins_need_undo ? FAIL : OK);
7060}
7061
7062/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007063 * Do a few things to stop inserting.
7064 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7065 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 */
7067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007068stop_insert(
7069 pos_T *end_insert_pos,
7070 int esc, /* called by ins_esc() */
7071 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007073 int cc;
7074 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
7076 stop_redo_ins();
7077 replace_flush(); /* abandon replace stack */
7078
7079 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007080 * Save the inserted text for later redo with ^@ and CTRL-A.
7081 * Don't do it when "restart_edit" was set and nothing was inserted,
7082 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007084 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007085 if (did_restart_edit == 0 || (ptr != NULL
7086 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007087 {
7088 vim_free(last_insert);
7089 last_insert = ptr;
7090 last_insert_skip = new_insert_skip;
7091 }
7092 else
7093 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007095 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {
7097 /* Auto-format now. It may seem strange to do this when stopping an
7098 * insertion (or moving the cursor), but it's required when appending
7099 * a line and having it end in a space. But only do it when something
7100 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007101 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007103 pos_T tpos = curwin->w_cursor;
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 /* When the cursor is at the end of the line after a space the
7106 * formatting will move it to the following word. Avoid that by
7107 * moving the cursor onto the space. */
7108 cc = 'x';
7109 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7110 {
7111 dec_cursor();
7112 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007113 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007114 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
7116
7117 auto_format(TRUE, FALSE);
7118
Bram Moolenaar1c465442017-03-12 20:10:05 +01007119 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007120 {
7121 if (gchar_cursor() != NUL)
7122 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007123 // If the cursor is still at the same character, also keep
7124 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007125 if (gchar_cursor() == NUL
7126 && curwin->w_cursor.lnum == tpos.lnum
7127 && curwin->w_cursor.col == tpos.col)
7128 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
7131
7132 /* If a space was inserted for auto-formatting, remove it now. */
7133 check_auto_format(TRUE);
7134
7135 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007136 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007137 * Do this when ESC was used or moving the cursor up/down.
7138 * Check for the old position still being valid, just in case the text
7139 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007140 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007141 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7142 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007144 pos_T tpos = curwin->w_cursor;
7145
7146 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007147 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007148 for (;;)
7149 {
7150 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7151 --curwin->w_cursor.col;
7152 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007153 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007154 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007155 if (del_char(TRUE) == FAIL)
7156 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007157 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007158 if (curwin->w_cursor.lnum != tpos.lnum)
7159 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007160 else
7161 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007162 /* reset tpos, could have been invalidated in the loop above */
7163 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007164 tpos.col++;
7165 if (cc != NUL && gchar_pos(&tpos) == NUL)
7166 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 /* <C-S-Right> may have started Visual mode, adjust the position for
7170 * deleted characters. */
7171 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7172 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007173 int len = (int)STRLEN(ml_get_curline());
7174
7175 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007177 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 }
7180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 }
7182 }
7183 did_ai = FALSE;
7184#ifdef FEAT_SMARTINDENT
7185 did_si = FALSE;
7186 can_si = FALSE;
7187 can_si_back = FALSE;
7188#endif
7189
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007190 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7191 * now in a different buffer. */
7192 if (end_insert_pos != NULL)
7193 {
7194 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007195 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007196 curbuf->b_op_end = *end_insert_pos;
7197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198}
7199
7200/*
7201 * Set the last inserted text to a single character.
7202 * Used for the replace command.
7203 */
7204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007205set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
7207 char_u *s;
7208
7209 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 if (last_insert != NULL)
7212 {
7213 s = last_insert;
7214 /* Use the CTRL-V only when entering a special char */
7215 if (c < ' ' || c == DEL)
7216 *s++ = Ctrl_V;
7217 s = add_char2buf(c, s);
7218 *s++ = ESC;
7219 *s++ = NUL;
7220 last_insert_skip = 0;
7221 }
7222}
7223
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007224#if defined(EXITFREE) || defined(PROTO)
7225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007226free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007227{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007228 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007229# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007230 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007231# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007232}
7233#endif
7234
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235/*
7236 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7237 * and CSI. Handle multi-byte characters.
7238 * Returns a pointer to after the added bytes.
7239 */
7240 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007241add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007243 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 int i;
7245 int len;
7246
7247 len = (*mb_char2bytes)(c, temp);
7248 for (i = 0; i < len; ++i)
7249 {
7250 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7252 if (c == K_SPECIAL)
7253 {
7254 *s++ = K_SPECIAL;
7255 *s++ = KS_SPECIAL;
7256 *s++ = KE_FILLER;
7257 }
7258#ifdef FEAT_GUI
7259 else if (c == CSI)
7260 {
7261 *s++ = CSI;
7262 *s++ = KS_EXTRA;
7263 *s++ = (int)KE_CSI;
7264 }
7265#endif
7266 else
7267 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 return s;
7270}
7271
7272/*
7273 * move cursor to start of line
7274 * if flags & BL_WHITE move to first non-white
7275 * if flags & BL_SOL move to first non-white if startofline is set,
7276 * otherwise keep "curswant" column
7277 * if flags & BL_FIX don't leave the cursor on a NUL.
7278 */
7279 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007280beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281{
7282 if ((flags & BL_SOL) && !p_sol)
7283 coladvance(curwin->w_curswant);
7284 else
7285 {
7286 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288
7289 if (flags & (BL_WHITE | BL_SOL))
7290 {
7291 char_u *ptr;
7292
Bram Moolenaar1c465442017-03-12 20:10:05 +01007293 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7295 ++curwin->w_cursor.col;
7296 }
7297 curwin->w_set_curswant = TRUE;
7298 }
7299}
7300
7301/*
7302 * oneright oneleft cursor_down cursor_up
7303 *
7304 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007305 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 * Return OK when successful, FAIL when we hit a line of file boundary.
7307 */
7308
7309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007310oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
7312 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 if (virtual_active())
7316 {
7317 pos_T prevpos = curwin->w_cursor;
7318
7319 /* Adjust for multi-wide char (excluding TAB) */
7320 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007321 coladvance(getviscol() + ((*ptr != TAB
7322 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 ? ptr2cells(ptr) : 1));
7324 curwin->w_set_curswant = TRUE;
7325 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7326 return (prevpos.col != curwin->w_cursor.col
7327 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
7330 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007331 if (*ptr == NUL)
7332 return FAIL; /* already at the very end */
7333
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007334 if (has_mbyte)
7335 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007337 l = 1;
7338
7339 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7340 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007341 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007342 return FAIL;
7343 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
7345 curwin->w_set_curswant = TRUE;
7346 return OK;
7347}
7348
7349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007350oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 if (virtual_active())
7353 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007354#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 int v = getviscol();
7358
7359 if (v == 0)
7360 return FAIL;
7361
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007362#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 /* We might get stuck on 'showbreak', skip over it. */
7364 width = 1;
7365 for (;;)
7366 {
7367 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007368 /* getviscol() is slow, skip it when 'showbreak' is empty,
7369 * 'breakindent' is not set and there are no multi-byte
7370 * characters */
7371 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01007372 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 break;
7374 ++width;
7375 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007376#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
7380 if (curwin->w_cursor.coladd == 1)
7381 {
7382 char_u *ptr;
7383
7384 /* Adjust for multi-wide char (not a TAB) */
7385 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007386 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
7387 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 curwin->w_cursor.coladd = 0;
7389 }
7390
7391 curwin->w_set_curswant = TRUE;
7392 return OK;
7393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394
7395 if (curwin->w_cursor.col == 0)
7396 return FAIL;
7397
7398 curwin->w_set_curswant = TRUE;
7399 --curwin->w_cursor.col;
7400
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 /* if the character on the left of the current cursor is a multi-byte
7402 * character, move to its first byte */
7403 if (has_mbyte)
7404 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 return OK;
7406}
7407
7408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409cursor_up(
7410 long n,
7411 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413 linenr_T lnum;
7414
7415 if (n > 0)
7416 {
7417 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007418 /* This fails if the cursor is already in the first line or the count
7419 * is larger than the line number and '-' is in 'cpoptions' */
7420 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 return FAIL;
7422 if (n >= lnum)
7423 lnum = 1;
7424 else
7425#ifdef FEAT_FOLDING
7426 if (hasAnyFolding(curwin))
7427 {
7428 /*
7429 * Count each sequence of folded lines as one logical line.
7430 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007431 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 (void)hasFolding(lnum, &lnum, NULL);
7433
7434 while (n--)
7435 {
7436 /* move up one line */
7437 --lnum;
7438 if (lnum <= 1)
7439 break;
7440 /* If we entered a fold, move to the beginning, unless in
7441 * Insert mode or when 'foldopen' contains "all": it will open
7442 * in a moment. */
7443 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7444 (void)hasFolding(lnum, &lnum, NULL);
7445 }
7446 if (lnum < 1)
7447 lnum = 1;
7448 }
7449 else
7450#endif
7451 lnum -= n;
7452 curwin->w_cursor.lnum = lnum;
7453 }
7454
7455 /* try to advance to the column we want to be at */
7456 coladvance(curwin->w_curswant);
7457
7458 if (upd_topline)
7459 update_topline(); /* make sure curwin->w_topline is valid */
7460
7461 return OK;
7462}
7463
7464/*
7465 * Cursor down a number of logical lines.
7466 */
7467 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007468cursor_down(
7469 long n,
7470 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471{
7472 linenr_T lnum;
7473
7474 if (n > 0)
7475 {
7476 lnum = curwin->w_cursor.lnum;
7477#ifdef FEAT_FOLDING
7478 /* Move to last line of fold, will fail if it's the end-of-file. */
7479 (void)hasFolding(lnum, NULL, &lnum);
7480#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007481 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007482 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007483 if (lnum >= curbuf->b_ml.ml_line_count
7484 || (lnum + n > curbuf->b_ml.ml_line_count
7485 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 return FAIL;
7487 if (lnum + n >= curbuf->b_ml.ml_line_count)
7488 lnum = curbuf->b_ml.ml_line_count;
7489 else
7490#ifdef FEAT_FOLDING
7491 if (hasAnyFolding(curwin))
7492 {
7493 linenr_T last;
7494
7495 /* count each sequence of folded lines as one logical line */
7496 while (n--)
7497 {
7498 if (hasFolding(lnum, NULL, &last))
7499 lnum = last + 1;
7500 else
7501 ++lnum;
7502 if (lnum >= curbuf->b_ml.ml_line_count)
7503 break;
7504 }
7505 if (lnum > curbuf->b_ml.ml_line_count)
7506 lnum = curbuf->b_ml.ml_line_count;
7507 }
7508 else
7509#endif
7510 lnum += n;
7511 curwin->w_cursor.lnum = lnum;
7512 }
7513
7514 /* try to advance to the column we want to be at */
7515 coladvance(curwin->w_curswant);
7516
7517 if (upd_topline)
7518 update_topline(); /* make sure curwin->w_topline is valid */
7519
7520 return OK;
7521}
7522
7523/*
7524 * Stuff the last inserted text in the read buffer.
7525 * Last_insert actually is a copy of the redo buffer, so we
7526 * first have to remove the command.
7527 */
7528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007529stuff_inserted(
7530 int c, /* Command character to be inserted */
7531 long count, /* Repeat this many times */
7532 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533{
7534 char_u *esc_ptr;
7535 char_u *ptr;
7536 char_u *last_ptr;
7537 char_u last = NUL;
7538
7539 ptr = get_last_insert();
7540 if (ptr == NULL)
7541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007542 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 return FAIL;
7544 }
7545
7546 /* may want to stuff the command character, to start Insert mode */
7547 if (c != NUL)
7548 stuffcharReadbuff(c);
7549 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7550 *esc_ptr = NUL; /* remove the ESC */
7551
7552 /* when the last char is either "0" or "^" it will be quoted if no ESC
7553 * comes after it OR if it will inserted more than once and "ptr"
7554 * starts with ^D. -- Acevedo
7555 */
7556 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7557 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7558 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7559 {
7560 last = *last_ptr;
7561 *last_ptr = NUL;
7562 }
7563
7564 do
7565 {
7566 stuffReadbuff(ptr);
7567 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7568 if (last)
7569 stuffReadbuff((char_u *)(last == '0'
7570 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7571 : IF_EB("\026^", CTRL_V_STR "^")));
7572 }
7573 while (--count > 0);
7574
7575 if (last)
7576 *last_ptr = last;
7577
7578 if (esc_ptr != NULL)
7579 *esc_ptr = ESC; /* put the ESC back */
7580
7581 /* may want to stuff a trailing ESC, to get out of Insert mode */
7582 if (!no_esc)
7583 stuffcharReadbuff(ESC);
7584
7585 return OK;
7586}
7587
7588 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007589get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
7591 if (last_insert == NULL)
7592 return NULL;
7593 return last_insert + last_insert_skip;
7594}
7595
7596/*
7597 * Get last inserted string, and remove trailing <Esc>.
7598 * Returns pointer to allocated memory (must be freed) or NULL.
7599 */
7600 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007601get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
7603 char_u *s;
7604 int len;
7605
7606 if (last_insert == NULL)
7607 return NULL;
7608 s = vim_strsave(last_insert + last_insert_skip);
7609 if (s != NULL)
7610 {
7611 len = (int)STRLEN(s);
7612 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7613 s[len - 1] = NUL;
7614 }
7615 return s;
7616}
7617
7618/*
7619 * Check the word in front of the cursor for an abbreviation.
7620 * Called when the non-id character "c" has been entered.
7621 * When an abbreviation is recognized it is removed from the text and
7622 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7623 */
7624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007625echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626{
7627 /* Don't check for abbreviation in paste mode, when disabled and just
7628 * after moving around with cursor keys. */
7629 if (p_paste || no_abbr || arrow_used)
7630 return FALSE;
7631
7632 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7633 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7634}
7635
7636/*
7637 * replace-stack functions
7638 *
7639 * When replacing characters, the replaced characters are remembered for each
7640 * new character. This is used to re-insert the old text when backspacing.
7641 *
7642 * There is a NUL headed list of characters for each character that is
7643 * currently in the file after the insertion point. When BS is used, one NUL
7644 * headed list is put back for the deleted character.
7645 *
7646 * For a newline, there are two NUL headed lists. One contains the characters
7647 * that the NL replaced. The extra one stores the characters after the cursor
7648 * that were deleted (always white space).
7649 *
7650 * Replace_offset is normally 0, in which case replace_push will add a new
7651 * character at the end of the stack. If replace_offset is not 0, that many
7652 * characters will be left on the stack above the newly inserted character.
7653 */
7654
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007655static char_u *replace_stack = NULL;
7656static long replace_stack_nr = 0; /* next entry in replace stack */
7657static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
7659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007660replace_push(
7661 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663 char_u *p;
7664
7665 if (replace_stack_nr < replace_offset) /* nothing to do */
7666 return;
7667 if (replace_stack_len <= replace_stack_nr)
7668 {
7669 replace_stack_len += 50;
7670 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7671 if (p == NULL) /* out of memory */
7672 {
7673 replace_stack_len -= 50;
7674 return;
7675 }
7676 if (replace_stack != NULL)
7677 {
7678 mch_memmove(p, replace_stack,
7679 (size_t)(replace_stack_nr * sizeof(char_u)));
7680 vim_free(replace_stack);
7681 }
7682 replace_stack = p;
7683 }
7684 p = replace_stack + replace_stack_nr - replace_offset;
7685 if (replace_offset)
7686 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7687 *p = c;
7688 ++replace_stack_nr;
7689}
7690
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007691/*
7692 * Push a character onto the replace stack. Handles a multi-byte character in
7693 * reverse byte order, so that the first byte is popped off first.
7694 * Return the number of bytes done (includes composing characters).
7695 */
7696 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007697replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007698{
7699 int l = (*mb_ptr2len)(p);
7700 int j;
7701
7702 for (j = l - 1; j >= 0; --j)
7703 replace_push(p[j]);
7704 return l;
7705}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007706
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707/*
7708 * Pop one item from the replace stack.
7709 * return -1 if stack empty
7710 * return replaced character or NUL otherwise
7711 */
7712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007713replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714{
7715 if (replace_stack_nr == 0)
7716 return -1;
7717 return (int)replace_stack[--replace_stack_nr];
7718}
7719
7720/*
7721 * Join the top two items on the replace stack. This removes to "off"'th NUL
7722 * encountered.
7723 */
7724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007725replace_join(
7726 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 int i;
7729
7730 for (i = replace_stack_nr; --i >= 0; )
7731 if (replace_stack[i] == NUL && off-- <= 0)
7732 {
7733 --replace_stack_nr;
7734 mch_memmove(replace_stack + i, replace_stack + i + 1,
7735 (size_t)(replace_stack_nr - i));
7736 return;
7737 }
7738}
7739
7740/*
7741 * Pop bytes from the replace stack until a NUL is found, and insert them
7742 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7743 */
7744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007745replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747 int cc;
7748 int oldState = State;
7749
7750 State = NORMAL; /* don't want REPLACE here */
7751 while ((cc = replace_pop()) > 0)
7752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 dec_cursor();
7755 }
7756 State = oldState;
7757}
7758
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759/*
7760 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7761 * indicates a multi-byte char, pop the other bytes too.
7762 */
7763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007764mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
7766 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007767 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 int i;
7769 int c;
7770
7771 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7772 {
7773 buf[0] = cc;
7774 for (i = 1; i < n; ++i)
7775 buf[i] = replace_pop();
7776 ins_bytes_len(buf, n);
7777 }
7778 else
7779 ins_char(cc);
7780
7781 if (enc_utf8)
7782 /* Handle composing chars. */
7783 for (;;)
7784 {
7785 c = replace_pop();
7786 if (c == -1) /* stack empty */
7787 break;
7788 if ((n = MB_BYTE2LEN(c)) == 1)
7789 {
7790 /* Not a multi-byte char, put it back. */
7791 replace_push(c);
7792 break;
7793 }
7794 else
7795 {
7796 buf[0] = c;
7797 for (i = 1; i < n; ++i)
7798 buf[i] = replace_pop();
7799 if (utf_iscomposing(utf_ptr2char(buf)))
7800 ins_bytes_len(buf, n);
7801 else
7802 {
7803 /* Not a composing char, put it back. */
7804 for (i = n - 1; i >= 0; --i)
7805 replace_push(buf[i]);
7806 break;
7807 }
7808 }
7809 }
7810}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811
7812/*
7813 * make the replace stack empty
7814 * (called when exiting replace mode)
7815 */
7816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007817replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007819 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 replace_stack_len = 0;
7821 replace_stack_nr = 0;
7822}
7823
7824/*
7825 * Handle doing a BS for one character.
7826 * cc < 0: replace stack empty, just move cursor
7827 * cc == 0: character was inserted, delete it
7828 * cc > 0: character was replaced, put cc (first byte of original char) back
7829 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007830 * When "limit_col" is >= 0, don't delete before this column. Matters when
7831 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 */
7833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007834replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 int orig_len = 0;
7838 int ins_len;
7839 int orig_vcols = 0;
7840 colnr_T start_vcol;
7841 char_u *p;
7842 int i;
7843 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
7845 cc = replace_pop();
7846 if (cc > 0)
7847 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007848#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007849 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007850
7851 if (curbuf->b_has_textprop)
7852 {
7853 // Do not adjust text properties for individual delete and insert
7854 // operations, do it afterwards on the resulting text.
7855 len_before = STRLEN(ml_get_curline());
7856 ++text_prop_frozen;
7857 }
7858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 if (State & VREPLACE_FLAG)
7860 {
7861 /* Get the number of screen cells used by the character we are
7862 * going to delete. */
7863 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7864 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 if (has_mbyte)
7867 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007868 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007870 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 replace_push(cc);
7872 }
7873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {
7875 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007877 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 }
7879 replace_pop_ins();
7880
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 if (State & VREPLACE_FLAG)
7882 {
7883 /* Get the number of screen cells used by the inserted characters */
7884 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007885 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 vcol = start_vcol;
7887 for (i = 0; i < ins_len; ++i)
7888 {
7889 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007890 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892 vcol -= start_vcol;
7893
7894 /* Delete spaces that were inserted after the cursor to keep the
7895 * text aligned. */
7896 curwin->w_cursor.col += ins_len;
7897 while (vcol > orig_vcols && gchar_cursor() == ' ')
7898 {
7899 del_char(FALSE);
7900 ++orig_vcols;
7901 }
7902 curwin->w_cursor.col -= ins_len;
7903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
Bram Moolenaar196d1572019-01-02 23:47:18 +01007905 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01007907
7908#ifdef FEAT_TEXT_PROP
7909 if (curbuf->b_has_textprop)
7910 {
7911 size_t len_now = STRLEN(ml_get_curline());
7912
7913 --text_prop_frozen;
7914 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
7915 (int)(len_now - len_before));
7916 }
7917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 }
7919 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007920 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921}
7922
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7924/*
7925 * Map Hebrew keyboard when in hkmap mode.
7926 */
7927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007928hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7931 {
7932 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7933 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7934 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7935 static char_u map[26] =
7936 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7937 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7938 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7939 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7940 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7941 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7942 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7943 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7944 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7945
7946 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7947 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7948 /* '-1'='sofit' */
7949 else if (c == 'x')
7950 return 'X';
7951 else if (c == 'q')
7952 return '\''; /* {geresh}={'} */
7953 else if (c == 246)
7954 return ' '; /* \"o --> ' ' for a german keyboard */
7955 else if (c == 228)
7956 return ' '; /* \"a --> ' ' -- / -- */
7957 else if (c == 252)
7958 return ' '; /* \"u --> ' ' -- / -- */
7959#ifdef EBCDIC
7960 else if (islower(c))
7961#else
7962 /* NOTE: islower() does not do the right thing for us on Linux so we
7963 * do this the same was as 5.7 and previous, so it works correctly on
7964 * all systems. Specifically, the e.g. Delete and Arrow keys are
7965 * munged and won't work if e.g. searching for Hebrew text.
7966 */
7967 else if (c >= 'a' && c <= 'z')
7968#endif
7969 return (int)(map[CharOrdLow(c)] + p_aleph);
7970 else
7971 return c;
7972 }
7973 else
7974 {
7975 switch (c)
7976 {
7977 case '`': return ';';
7978 case '/': return '.';
7979 case '\'': return ',';
7980 case 'q': return '/';
7981 case 'w': return '\'';
7982
7983 /* Hebrew letters - set offset from 'a' */
7984 case ',': c = '{'; break;
7985 case '.': c = 'v'; break;
7986 case ';': c = 't'; break;
7987 default: {
7988 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7989
7990#ifdef EBCDIC
7991 /* see note about islower() above */
7992 if (!islower(c))
7993#else
7994 if (c < 'a' || c > 'z')
7995#endif
7996 return c;
7997 c = str[CharOrdLow(c)];
7998 break;
7999 }
8000 }
8001
8002 return (int)(CharOrdLow(c) + p_aleph);
8003 }
8004}
8005#endif
8006
8007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008008ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 int need_redraw = FALSE;
8011 int regname;
8012 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008013 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014
8015 /*
8016 * If we are going to wait for a character, show a '"'.
8017 */
8018 pc_status = PC_STATUS_UNSET;
8019 if (redrawing() && !char_avail())
8020 {
8021 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008022 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024 edit_putchar('"', TRUE);
8025#ifdef FEAT_CMDL_INFO
8026 add_to_showcmd_c(Ctrl_R);
8027#endif
8028 }
8029
8030#ifdef USE_ON_FLY_SCROLL
8031 dont_scroll = TRUE; /* disallow scrolling here */
8032#endif
8033
8034 /*
8035 * Don't map the register name. This also prevents the mode message to be
8036 * deleted when ESC is hit.
8037 */
8038 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008039 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8042 {
8043 /* Get a third key for literal register insertion */
8044 literally = regname;
8045#ifdef FEAT_CMDL_INFO
8046 add_to_showcmd_c(literally);
8047#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008048 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 }
8051 --no_mapping;
8052
8053#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008054 /* Don't call u_sync() while typing the expression or giving an error
8055 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 ++no_u_sync;
8057 if (regname == '=')
8058 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008059 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008060# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008062# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008063 /* Sync undo when evaluating the expression calls setline() or
8064 * append(), so that it can be undone separately. */
8065 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008068
8069 // Cursor may be moved back a column.
8070 curwin->w_cursor = curpos;
8071 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008072# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008073 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 if (im_on)
8075 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008078 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8079 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008080 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 else
8084 {
8085#endif
8086 if (literally == Ctrl_O || literally == Ctrl_P)
8087 {
8088 /* Append the command to the redo buffer. */
8089 AppendCharToRedobuff(Ctrl_R);
8090 AppendCharToRedobuff(literally);
8091 AppendCharToRedobuff(regname);
8092
8093 do_put(regname, BACKWARD, 1L,
8094 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8095 }
8096 else if (insert_reg(regname, literally) == FAIL)
8097 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008098 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 need_redraw = TRUE; /* remove the '"' */
8100 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008101 else if (stop_insert_mode)
8102 /* When the '=' register was used and a function was invoked that
8103 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8104 * insert anything, need to remove the '"' */
8105 need_redraw = TRUE;
8106
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107#ifdef FEAT_EVAL
8108 }
8109 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008110 if (u_sync_once == 1)
8111 ins_need_undo = TRUE;
8112 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113#endif
8114#ifdef FEAT_CMDL_INFO
8115 clear_showcmd();
8116#endif
8117
8118 /* If the inserted register is empty, we need to remove the '"' */
8119 if (need_redraw || stuff_empty())
8120 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008121
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008122 /* Disallow starting Visual mode here, would get a weird mode. */
8123 if (!vis_active && VIsual_active)
8124 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125}
8126
8127/*
8128 * CTRL-G commands in Insert mode.
8129 */
8130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008131ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
8133 int c;
8134
8135#ifdef FEAT_INS_EXPAND
8136 /* Right after CTRL-X the cursor will be after the ruler. */
8137 setcursor();
8138#endif
8139
8140 /*
8141 * Don't map the second key. This also prevents the mode message to be
8142 * deleted when ESC is hit.
8143 */
8144 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008145 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 --no_mapping;
8147 switch (c)
8148 {
8149 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8150 case K_UP:
8151 case Ctrl_K:
8152 case 'k': ins_up(TRUE);
8153 break;
8154
8155 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8156 case K_DOWN:
8157 case Ctrl_J:
8158 case 'j': ins_down(TRUE);
8159 break;
8160
8161 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008162 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008164
8165 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008166 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008167 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008168 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 break;
8170
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008171 /* CTRL-G U: do not break undo with the next char */
8172 case 'U':
8173 /* Allow one left/right cursor movement with the next char,
8174 * without breaking undo. */
8175 dont_sync_undo = MAYBE;
8176 break;
8177
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008179 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 }
8181}
8182
8183/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008184 * CTRL-^ in Insert mode.
8185 */
8186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008187ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008188{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008189 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008190 {
8191 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8192 if (State & LANGMAP)
8193 {
8194 curbuf->b_p_iminsert = B_IMODE_NONE;
8195 State &= ~LANGMAP;
8196 }
8197 else
8198 {
8199 curbuf->b_p_iminsert = B_IMODE_LMAP;
8200 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008201#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008202 im_set_active(FALSE);
8203#endif
8204 }
8205 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008206#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008207 else
8208 {
8209 /* There are no ":lmap" mappings, toggle IM */
8210 if (im_get_status())
8211 {
8212 curbuf->b_p_iminsert = B_IMODE_NONE;
8213 im_set_active(FALSE);
8214 }
8215 else
8216 {
8217 curbuf->b_p_iminsert = B_IMODE_IM;
8218 State &= ~LANGMAP;
8219 im_set_active(TRUE);
8220 }
8221 }
8222#endif
8223 set_iminsert_global();
8224 showmode();
8225#ifdef FEAT_GUI
8226 /* may show different cursor shape or color */
8227 if (gui.in_use)
8228 gui_update_cursor(TRUE, FALSE);
8229#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008230#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008231 /* Show/unshow value of 'keymap' in status lines. */
8232 status_redraw_curbuf();
8233#endif
8234}
8235
8236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 * Handle ESC in insert mode.
8238 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8239 * insert.
8240 */
8241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008242ins_esc(
8243 long *count,
8244 int cmdchar,
8245 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246{
8247 int temp;
8248 static int disabled_redraw = FALSE;
8249
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008250#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008251 check_spell_redraw();
8252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253#if defined(FEAT_HANGULIN)
8254# if defined(ESC_CHG_TO_ENG_MODE)
8255 hangul_input_state_set(0);
8256# endif
8257 if (composing_hangul)
8258 {
8259 push_raw_key(composing_hangul_buffer, 2);
8260 composing_hangul = 0;
8261 }
8262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263
8264 temp = curwin->w_cursor.col;
8265 if (disabled_redraw)
8266 {
8267 --RedrawingDisabled;
8268 disabled_redraw = FALSE;
8269 }
8270 if (!arrow_used)
8271 {
8272 /*
8273 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008274 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8275 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 */
8277 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008278 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279
8280 /*
8281 * Repeating insert may take a long time. Check for
8282 * interrupt now and then.
8283 */
8284 if (*count > 0)
8285 {
8286 line_breakcheck();
8287 if (got_int)
8288 *count = 0;
8289 }
8290
8291 if (--*count > 0) /* repeat what was typed */
8292 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008293 /* Vi repeats the insert without replacing characters. */
8294 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8295 State &= ~REPLACE_FLAG;
8296
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 (void)start_redo_ins();
8298 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008299 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 ++RedrawingDisabled;
8301 disabled_redraw = TRUE;
8302 return FALSE; /* repeat the insert */
8303 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008304 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 undisplay_dollar();
8306 }
8307
8308 /* When an autoindent was removed, curswant stays after the
8309 * indent */
8310 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8311 curwin->w_set_curswant = TRUE;
8312
8313 /* Remember the last Insert position in the '^ mark. */
8314 if (!cmdmod.keepjumps)
8315 curbuf->b_last_insert = curwin->w_cursor;
8316
8317 /*
8318 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008319 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008321 if (!nomove
8322 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008323 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00008324 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008325 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326#ifdef FEAT_RIGHTLEFT
8327 && !revins_on
8328#endif
8329 )
8330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8332 {
8333 oneleft();
8334 if (restart_edit != NUL)
8335 ++curwin->w_cursor.coladd;
8336 }
8337 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {
8339 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 /* Correct cursor for multi-byte character. */
8341 if (has_mbyte)
8342 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 }
8344 }
8345
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008346#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 /* Disable IM to allow typing English directly for Normal mode commands.
8348 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8349 * well). */
8350 if (!(State & LANGMAP))
8351 im_save_status(&curbuf->b_p_iminsert);
8352 im_set_active(FALSE);
8353#endif
8354
8355 State = NORMAL;
8356 /* need to position cursor again (e.g. when on a TAB ) */
8357 changed_cline_bef_curs();
8358
8359#ifdef FEAT_MOUSE
8360 setmouse();
8361#endif
8362#ifdef CURSOR_SHAPE
8363 ui_cursor_shape(); /* may show different cursor shape */
8364#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008365 if (!p_ek)
8366 /* Re-enable bracketed paste mode. */
8367 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
8369 /*
8370 * When recording or for CTRL-O, need to display the new mode.
8371 * Otherwise remove the mode message.
8372 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008373 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 showmode();
Bram Moolenaarcb574f42019-01-25 22:29:57 +01008375 else if (p_smd && !skip_showmode())
Bram Moolenaar32526b32019-01-19 17:43:09 +01008376 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377
8378 return TRUE; /* exit Insert mode */
8379}
8380
8381#ifdef FEAT_RIGHTLEFT
8382/*
8383 * Toggle language: hkmap and revins_on.
8384 * Move to end of reverse inserted text.
8385 */
8386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008387ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388{
8389 if (revins_on && revins_chars && revins_scol >= 0)
8390 {
8391 while (gchar_cursor() != NUL && revins_chars--)
8392 ++curwin->w_cursor.col;
8393 }
8394 p_ri = !p_ri;
8395 revins_on = (State == INSERT && p_ri);
8396 if (revins_on)
8397 {
8398 revins_scol = curwin->w_cursor.col;
8399 revins_legal++;
8400 revins_chars = 0;
8401 undisplay_dollar();
8402 }
8403 else
8404 revins_scol = -1;
8405#ifdef FEAT_FKMAP
8406 if (p_altkeymap)
8407 {
8408 /*
8409 * to be consistent also for redo command, using '.'
8410 * set arrow_used to true and stop it - causing to redo
8411 * characters entered in one mode (normal/reverse insert).
8412 */
8413 arrow_used = TRUE;
8414 (void)stop_arrow();
8415 p_fkmap = curwin->w_p_rl ^ p_ri;
8416 if (p_fkmap && p_ri)
8417 State = INSERT;
8418 }
8419 else
8420#endif
8421 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8422 showmode();
8423}
8424#endif
8425
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426/*
8427 * If 'keymodel' contains "startsel", may start selection.
8428 * Returns TRUE when a CTRL-O and other keys stuffed.
8429 */
8430 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008431ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432{
8433 if (km_startsel)
8434 switch (c)
8435 {
8436 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 case K_PAGEUP:
8439 case K_KPAGEUP:
8440 case K_PAGEDOWN:
8441 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008442# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 case K_LEFT:
8444 case K_RIGHT:
8445 case K_UP:
8446 case K_DOWN:
8447 case K_END:
8448 case K_HOME:
8449# endif
8450 if (!(mod_mask & MOD_MASK_SHIFT))
8451 break;
8452 /* FALLTHROUGH */
8453 case K_S_LEFT:
8454 case K_S_RIGHT:
8455 case K_S_UP:
8456 case K_S_DOWN:
8457 case K_S_END:
8458 case K_S_HOME:
8459 /* Start selection right away, the cursor can move with
8460 * CTRL-O when beyond the end of the line. */
8461 start_selection();
8462
8463 /* Execute the key in (insert) Select mode. */
8464 stuffcharReadbuff(Ctrl_O);
8465 if (mod_mask)
8466 {
8467 char_u buf[4];
8468
8469 buf[0] = K_SPECIAL;
8470 buf[1] = KS_MODIFIER;
8471 buf[2] = mod_mask;
8472 buf[3] = NUL;
8473 stuffReadbuff(buf);
8474 }
8475 stuffcharReadbuff(c);
8476 return TRUE;
8477 }
8478 return FALSE;
8479}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480
8481/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008482 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008483 */
8484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008485ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008486{
8487#ifdef FEAT_FKMAP
8488 if (p_fkmap && p_ri)
8489 {
8490 beep_flush();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008491 emsg(farsi_text_3); /* encoded in Farsi */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008492 return;
8493 }
8494#endif
8495
Bram Moolenaar1e015462005-09-25 22:16:38 +00008496# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008497 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008498 (char_u *)((State & REPLACE_FLAG) ? "i"
8499 : replaceState == VREPLACE ? "v"
8500 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008501# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008502 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008503 if (State & REPLACE_FLAG)
8504 State = INSERT | (State & LANGMAP);
8505 else
8506 State = replaceState | (State & LANGMAP);
8507 AppendCharToRedobuff(K_INS);
8508 showmode();
8509#ifdef CURSOR_SHAPE
8510 ui_cursor_shape(); /* may show different cursor shape */
8511#endif
8512}
8513
8514/*
8515 * Pressed CTRL-O in Insert mode.
8516 */
8517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008518ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008519{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008520 if (State & VREPLACE_FLAG)
8521 restart_edit = 'V';
8522 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008523 if (State & REPLACE_FLAG)
8524 restart_edit = 'R';
8525 else
8526 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008527 if (virtual_active())
8528 ins_at_eol = FALSE; /* cursor always keeps its column */
8529 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008530 ins_at_eol = (gchar_cursor() == NUL);
8531}
8532
8533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 * If the cursor is on an indent, ^T/^D insert/delete one
8535 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008536 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 * with vi. But vi only supports ^T and ^D after an
8538 * autoindent, we support it everywhere.
8539 */
8540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008541ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
8543 if (stop_arrow() == FAIL)
8544 return;
8545 AppendCharToRedobuff(c);
8546
8547 /*
8548 * 0^D and ^^D: remove all indent.
8549 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008550 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8551 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
8553 --curwin->w_cursor.col;
8554 (void)del_char(FALSE); /* delete the '^' or '0' */
8555 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8556 if (State & REPLACE_FLAG)
8557 replace_pop_ins();
8558 if (lastc == '^')
8559 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008560 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 }
8562 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008563 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564
8565 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8566 did_ai = FALSE;
8567#ifdef FEAT_SMARTINDENT
8568 did_si = FALSE;
8569 can_si = FALSE;
8570 can_si_back = FALSE;
8571#endif
8572#ifdef FEAT_CINDENT
8573 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8574#endif
8575}
8576
8577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008578ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
8580 int temp;
8581
8582 if (stop_arrow() == FAIL)
8583 return;
8584 if (gchar_cursor() == NUL) /* delete newline */
8585 {
8586 temp = curwin->w_cursor.col;
8587 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008588 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008589 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008593 /* Adjust orig_line_count in case more lines have been deleted than
8594 * have been added. That makes sure, that open_line() later
8595 * can access all buffer lines correctly */
8596 if (State & VREPLACE_FLAG &&
8597 orig_line_count > curbuf->b_ml.ml_line_count)
8598 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008601 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8602 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 did_ai = FALSE;
8604#ifdef FEAT_SMARTINDENT
8605 did_si = FALSE;
8606 can_si = FALSE;
8607 can_si_back = FALSE;
8608#endif
8609 AppendCharToRedobuff(K_DEL);
8610}
8611
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008612/*
8613 * Delete one character for ins_bs().
8614 */
8615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008616ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008617{
8618 dec_cursor();
8619 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8620 if (State & REPLACE_FLAG)
8621 {
8622 /* Don't delete characters before the insert point when in
8623 * Replace mode */
8624 if (curwin->w_cursor.lnum != Insstart.lnum
8625 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008626 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008627 }
8628 else
8629 (void)del_char(FALSE);
8630}
8631
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632/*
8633 * Handle Backspace, delete-word and delete-line in Insert mode.
8634 * Return TRUE when backspace was actually used.
8635 */
8636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008637ins_bs(
8638 int c,
8639 int mode,
8640 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 linenr_T lnum;
8643 int cc;
8644 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008645 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 colnr_T mincol;
8647 int did_backspace = FALSE;
8648 int in_indent;
8649 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008650 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651
8652 /*
8653 * can't delete anything in an empty file
8654 * can't backup past first character in buffer
8655 * can't backup past starting point unless 'backspace' > 1
8656 * can backup to a previous line if 'backspace' == 0
8657 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008658 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 || (
8660#ifdef FEAT_RIGHTLEFT
8661 !revins_on &&
8662#endif
8663 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8664 || (!can_bs(BS_START)
8665 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008666 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8667 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8669 && curwin->w_cursor.col <= ai_col)
8670 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8671 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008672 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 return FALSE;
8674 }
8675
8676 if (stop_arrow() == FAIL)
8677 return FALSE;
8678 in_indent = inindent(0);
8679#ifdef FEAT_CINDENT
8680 if (in_indent)
8681 can_cindent = FALSE;
8682#endif
8683#ifdef FEAT_COMMENTS
8684 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8685#endif
8686#ifdef FEAT_RIGHTLEFT
8687 if (revins_on) /* put cursor after last inserted char */
8688 inc_cursor();
8689#endif
8690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 /* Virtualedit:
8692 * BACKSPACE_CHAR eats a virtual space
8693 * BACKSPACE_WORD eats all coladd
8694 * BACKSPACE_LINE eats all coladd and keeps going
8695 */
8696 if (curwin->w_cursor.coladd > 0)
8697 {
8698 if (mode == BACKSPACE_CHAR)
8699 {
8700 --curwin->w_cursor.coladd;
8701 return TRUE;
8702 }
8703 if (mode == BACKSPACE_WORD)
8704 {
8705 curwin->w_cursor.coladd = 0;
8706 return TRUE;
8707 }
8708 curwin->w_cursor.coladd = 0;
8709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710
8711 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02008712 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 */
8714 if (curwin->w_cursor.col == 0)
8715 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008716 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008717 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718#ifdef FEAT_RIGHTLEFT
8719 || revins_on
8720#endif
8721 )
8722 {
8723 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8724 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8725 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008726 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02008727 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 }
8729 /*
8730 * In replace mode:
8731 * cc < 0: NL was inserted, delete it
8732 * cc >= 0: NL was replaced, put original characters back
8733 */
8734 cc = -1;
8735 if (State & REPLACE_FLAG)
8736 cc = replace_pop(); /* returns -1 if NL was inserted */
8737 /*
8738 * In replace mode, in the line we started replacing, we only move the
8739 * cursor.
8740 */
8741 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8742 {
8743 dec_cursor();
8744 }
8745 else
8746 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 if (!(State & VREPLACE_FLAG)
8748 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {
8750 temp = gchar_cursor(); /* remember current char */
8751 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008752
8753 /* When "aw" is in 'formatoptions' we must delete the space at
8754 * the end of the line, otherwise the line will be broken
8755 * again when auto-formatting. */
8756 if (has_format_option(FO_AUTO)
8757 && has_format_option(FO_WHITE_PAR))
8758 {
8759 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8760 TRUE);
8761 int len;
8762
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008763 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008764 if (len > 0 && ptr[len - 1] == ' ')
8765 ptr[len - 1] = NUL;
8766 }
8767
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008768 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 if (temp == NUL && gchar_cursor() != NUL)
8770 inc_cursor();
8771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 else
8773 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774
8775 /*
8776 * In REPLACE mode we have to put back the text that was replaced
8777 * by the NL. On the replace stack is first a NUL-terminated
8778 * sequence of characters that were deleted and then the
8779 * characters that NL replaced.
8780 */
8781 if (State & REPLACE_FLAG)
8782 {
8783 /*
8784 * Do the next ins_char() in NORMAL state, to
8785 * prevent ins_char() from replacing characters and
8786 * avoiding showmatch().
8787 */
8788 oldState = State;
8789 State = NORMAL;
8790 /*
8791 * restore characters (blanks) deleted after cursor
8792 */
8793 while (cc > 0)
8794 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008795 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008797 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 cc = replace_pop();
8799 }
8800 /* restore the characters that NL replaced */
8801 replace_pop_ins();
8802 State = oldState;
8803 }
8804 }
8805 did_ai = FALSE;
8806 }
8807 else
8808 {
8809 /*
8810 * Delete character(s) before the cursor.
8811 */
8812#ifdef FEAT_RIGHTLEFT
8813 if (revins_on) /* put cursor on last inserted char */
8814 dec_cursor();
8815#endif
8816 mincol = 0;
8817 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008818 if (mode == BACKSPACE_LINE
8819 && (curbuf->b_p_ai
8820#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008821 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008822#endif
8823 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824#ifdef FEAT_RIGHTLEFT
8825 && !revins_on
8826#endif
8827 )
8828 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008829 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008831 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008833 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
8835
8836 /*
8837 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8838 */
8839 if ( mode == BACKSPACE_CHAR
8840 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008841 || ((get_sts_value() != 0
8842#ifdef FEAT_VARTABS
8843 || tabstop_count(curbuf->b_p_vsts_array)
8844#endif
8845 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008846 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 && (*(ml_get_cursor() - 1) == TAB
8848 || (*(ml_get_cursor() - 1) == ' '
8849 && (!*inserted_space_p
8850 || arrow_used))))))
8851 {
8852 int ts;
8853 colnr_T vcol;
8854 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008855 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
8857 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 /* Compute the virtual column where we want to be. Since
8859 * 'showbreak' may get in the way, need to get the last column of
8860 * the previous character. */
8861 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008862 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 dec_cursor();
8864 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8865 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008866#ifdef FEAT_VARTABS
8867 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02008868 {
8869 ts = (int)get_sw_value(curbuf);
8870 want_vcol = (want_vcol / ts) * ts;
8871 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008872 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02008873 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008874 curbuf->b_p_vsts_array);
8875#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02008876 if (p_sta && in_indent)
8877 ts = (int)get_sw_value(curbuf);
8878 else
8879 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882
8883 /* delete characters until we are at or before want_vcol */
8884 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01008885 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008886 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887
8888 /* insert extra spaces until we are at want_vcol */
8889 while (vcol < want_vcol)
8890 {
8891 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008892 if (curwin->w_cursor.lnum == Insstart_orig.lnum
8893 && curwin->w_cursor.col < Insstart_orig.col)
8894 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 if (State & VREPLACE_FLAG)
8897 ins_char(' ');
8898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
8900 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008901 if ((State & REPLACE_FLAG))
8902 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 }
8904 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8905 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008906
8907 /* If we are now back where we started delete one character. Can
8908 * happen when using 'sts' and 'linebreak'. */
8909 if (vcol >= start_vcol)
8910 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 }
8912
8913 /*
8914 * Delete upto starting point, start of line or previous word.
8915 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008918 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008920 if (has_mbyte)
8921 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008922 do
8923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008925 if (!revins_on) /* put cursor on char to be deleted */
8926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008928
8929 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008930 /* look multi-byte character class */
8931 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008933 prev_cclass = cclass;
8934 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008936
8937 /* start of word? */
8938 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
8939 {
8940 mode = BACKSPACE_WORD_NOT_SPACE;
8941 temp = vim_iswordc(cc);
8942 }
8943 /* end of word? */
8944 else if (mode == BACKSPACE_WORD_NOT_SPACE
8945 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01008946 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008949 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008951 inc_cursor();
8952#ifdef FEAT_RIGHTLEFT
8953 else if (State & REPLACE_FLAG)
8954 dec_cursor();
8955#endif
8956 break;
8957 }
8958 if (State & REPLACE_FLAG)
8959 replace_do_bs(-1);
8960 else
8961 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008962 if (enc_utf8 && p_deco)
8963 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008964 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008965 /*
8966 * If there are combining characters and 'delcombine' is set
8967 * move the cursor back. Don't back up before the base
8968 * character.
8969 */
8970 if (enc_utf8 && p_deco && cpc[0] != NUL)
8971 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008972#ifdef FEAT_RIGHTLEFT
8973 if (revins_chars)
8974 {
8975 revins_chars--;
8976 revins_legal++;
8977 }
8978 if (revins_on && gchar_cursor() == NUL)
8979 break;
8980#endif
8981 }
8982 /* Just a single backspace?: */
8983 if (mode == BACKSPACE_CHAR)
8984 break;
8985 } while (
8986#ifdef FEAT_RIGHTLEFT
8987 revins_on ||
8988#endif
8989 (curwin->w_cursor.col > mincol
8990 && (curwin->w_cursor.lnum != Insstart_orig.lnum
8991 || curwin->w_cursor.col != Insstart_orig.col)));
8992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 did_backspace = TRUE;
8994 }
8995#ifdef FEAT_SMARTINDENT
8996 did_si = FALSE;
8997 can_si = FALSE;
8998 can_si_back = FALSE;
8999#endif
9000 if (curwin->w_cursor.col <= 1)
9001 did_ai = FALSE;
9002 /*
9003 * It's a little strange to put backspaces into the redo
9004 * buffer, but it makes auto-indent a lot easier to deal
9005 * with.
9006 */
9007 AppendCharToRedobuff(c);
9008
9009 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009010 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009011 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009012 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013
9014 /* vi behaviour: the cursor moves backward but the character that
9015 * was there remains visible
9016 * Vim behaviour: the cursor moves backward and the character that
9017 * was there is erased from the screen.
9018 * We can emulate the vi behaviour by pretending there is a dollar
9019 * displayed even when there isn't.
9020 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009021 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 dollar_vcol = curwin->w_virtcol;
9023
Bram Moolenaarce3be472008-01-14 19:12:28 +00009024#ifdef FEAT_FOLDING
9025 /* When deleting a char the cursor line must never be in a closed fold.
9026 * E.g., when 'foldmethod' is indent and deleting the first non-white
9027 * char before a Tab. */
9028 if (did_backspace)
9029 foldOpenCursor();
9030#endif
9031
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 return did_backspace;
9033}
9034
9035#ifdef FEAT_MOUSE
9036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009037ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009040 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041
9042# ifdef FEAT_GUI
9043 /* When GUI is active, also move/paste when 'mouse' is empty */
9044 if (!gui.in_use)
9045# endif
9046 if (!mouse_has(MOUSE_INSERT))
9047 return;
9048
9049 undisplay_dollar();
9050 tpos = curwin->w_cursor;
9051 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9052 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009053 win_T *new_curwin = curwin;
9054
9055 if (curwin != old_curwin && win_valid(old_curwin))
9056 {
9057 /* Mouse took us to another window. We need to go back to the
9058 * previous one to stop insert there properly. */
9059 curwin = old_curwin;
9060 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009061#ifdef FEAT_JOB_CHANNEL
9062 if (bt_prompt(curbuf))
9063 // Restart Insert mode when re-entering the prompt buffer.
9064 curbuf->b_prompt_insert = 'A';
9065#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009066 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009067 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009068 if (curwin != new_curwin && win_valid(new_curwin))
9069 {
9070 curwin = new_curwin;
9071 curbuf = curwin->w_buffer;
9072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073# ifdef FEAT_CINDENT
9074 can_cindent = TRUE;
9075# endif
9076 }
9077
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 /* redraw status lines (in case another window became active) */
9079 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080}
9081
9082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009083ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084{
9085 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009086 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009087# ifdef FEAT_INS_EXPAND
9088 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089# endif
9090
9091 tpos = curwin->w_cursor;
9092
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009093 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 {
9095 int row, col;
9096
9097 row = mouse_row;
9098 col = mouse_col;
9099
9100 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009101 wp = mouse_find_win(&row, &col);
9102 if (wp == NULL)
9103 return;
9104 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 curbuf = curwin->w_buffer;
9106 }
9107 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 undisplay_dollar();
9109
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009110# ifdef FEAT_INS_EXPAND
9111 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009112 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009113# endif
9114 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009115 if (dir == MSCR_DOWN || dir == MSCR_UP)
9116 {
9117 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9118 scroll_redraw(dir,
9119 (long)(curwin->w_botline - curwin->w_topline));
9120 else
9121 scroll_redraw(dir, 3L);
9122 }
9123#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009124 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009125 {
9126 int val, step = 6;
9127
9128 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009129 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009130 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9131 if (val < 0)
9132 val = 0;
9133 gui_do_horiz_scroll(val, TRUE);
9134 }
9135#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009136# ifdef FEAT_INS_EXPAND
9137 did_scroll = TRUE;
9138# endif
9139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 curwin->w_redr_status = TRUE;
9142
9143 curwin = old_curwin;
9144 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009146# ifdef FEAT_INS_EXPAND
9147 /* The popup menu may overlay the window, need to redraw it.
9148 * TODO: Would be more efficient to only redraw the windows that are
9149 * overlapped by the popup menu. */
9150 if (pum_visible() && did_scroll)
9151 {
9152 redraw_all_later(NOT_VALID);
9153 ins_compl_show_pum();
9154 }
9155# endif
9156
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009157 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 {
9159 start_arrow(&tpos);
9160# ifdef FEAT_CINDENT
9161 can_cindent = TRUE;
9162# endif
9163 }
9164}
9165#endif
9166
Bram Moolenaarec2da362017-01-21 20:04:22 +01009167/*
9168 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9169 * P_PE literally.
9170 * When "drop" is TRUE then consume the text and drop it.
9171 */
9172 int
9173bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9174{
9175 int c;
9176 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9177 int idx = 0;
9178 char_u *end = find_termcode((char_u *)"PE");
9179 int ret_char = -1;
9180 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009181 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009182
9183 /* If the end code is too long we can't detect it, read everything. */
9184 if (STRLEN(end) >= NUMBUFLEN)
9185 end = NULL;
9186 ++no_mapping;
9187 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009188 if (!p_paste)
9189 // Also have the side effects of setting 'paste' to make it work much
9190 // faster.
9191 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009192
Bram Moolenaarec2da362017-01-21 20:04:22 +01009193 for (;;)
9194 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009195 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009196 if (end == NULL && vpeekc() == NUL)
9197 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009198 do
9199 {
9200 c = vgetc();
9201 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9202 if (c == NUL || got_int)
9203 // When CTRL-C was encountered the typeahead will be flushed and we
9204 // won't get the end sequence.
9205 break;
9206
Bram Moolenaarec2da362017-01-21 20:04:22 +01009207 if (has_mbyte)
9208 idx += (*mb_char2bytes)(c, buf + idx);
9209 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009210 buf[idx++] = c;
9211 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009212 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009213 {
9214 if (end[idx] == NUL)
9215 break; /* Found the end of paste code. */
9216 continue;
9217 }
9218 if (!drop)
9219 {
9220 switch (mode)
9221 {
9222 case PASTE_CMDLINE:
9223 put_on_cmdline(buf, idx, TRUE);
9224 break;
9225
9226 case PASTE_EX:
9227 if (gap != NULL && ga_grow(gap, idx) == OK)
9228 {
9229 mch_memmove((char *)gap->ga_data + gap->ga_len,
9230 buf, (size_t)idx);
9231 gap->ga_len += idx;
9232 }
9233 break;
9234
9235 case PASTE_INSERT:
9236 if (stop_arrow() == OK)
9237 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009238 c = buf[0];
9239 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9240 ins_eol(c);
9241 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009242 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009243 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009244 AppendToRedobuffLit(buf, idx);
9245 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009246 }
9247 break;
9248
9249 case PASTE_ONE_CHAR:
9250 if (ret_char == -1)
9251 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01009252 if (has_mbyte)
9253 ret_char = (*mb_ptr2char)(buf);
9254 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009255 ret_char = buf[0];
9256 }
9257 break;
9258 }
9259 }
9260 idx = 0;
9261 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009262
Bram Moolenaarec2da362017-01-21 20:04:22 +01009263 --no_mapping;
9264 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009265 if (!save_paste)
9266 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009267
9268 return ret_char;
9269}
9270
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009271#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009273ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009274{
9275 /* We will be leaving the current window, unless closing another tab. */
9276 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9277 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9278 {
9279 undisplay_dollar();
9280 start_arrow(&curwin->w_cursor);
9281# ifdef FEAT_CINDENT
9282 can_cindent = TRUE;
9283# endif
9284 }
9285
9286 if (c == K_TABLINE)
9287 goto_tabpage(current_tab);
9288 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009289 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009290 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009291 redraw_statuslines(); /* will redraw the tabline when needed */
9292 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009293}
9294#endif
9295
9296#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009298ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 pos_T tpos;
9301
9302 undisplay_dollar();
9303 tpos = curwin->w_cursor;
9304 if (gui_do_scroll())
9305 {
9306 start_arrow(&tpos);
9307# ifdef FEAT_CINDENT
9308 can_cindent = TRUE;
9309# endif
9310 }
9311}
9312
9313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009314ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316 pos_T tpos;
9317
9318 undisplay_dollar();
9319 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009320 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 {
9322 start_arrow(&tpos);
9323# ifdef FEAT_CINDENT
9324 can_cindent = TRUE;
9325# endif
9326 }
9327}
9328#endif
9329
9330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009331ins_left(
9332 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 pos_T tpos;
9335
9336#ifdef FEAT_FOLDING
9337 if ((fdo_flags & FDO_HOR) && KeyTyped)
9338 foldOpenCursor();
9339#endif
9340 undisplay_dollar();
9341 tpos = curwin->w_cursor;
9342 if (oneleft() == OK)
9343 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009344#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9345 /* Only call start_arrow() when not busy with preediting, it will
9346 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009347 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009348#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009349 {
9350 start_arrow_with_change(&tpos, end_change);
9351 if (!end_change)
9352 AppendCharToRedobuff(K_LEFT);
9353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354#ifdef FEAT_RIGHTLEFT
9355 /* If exit reversed string, position is fixed */
9356 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9357 revins_legal++;
9358 revins_chars++;
9359#endif
9360 }
9361
9362 /*
9363 * if 'whichwrap' set for cursor in insert mode may go to
9364 * previous line
9365 */
9366 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9367 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009368 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 start_arrow(&tpos);
9370 --(curwin->w_cursor.lnum);
9371 coladvance((colnr_T)MAXCOL);
9372 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9373 }
9374 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009375 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009376 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377}
9378
9379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009380ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 pos_T tpos;
9383
9384#ifdef FEAT_FOLDING
9385 if ((fdo_flags & FDO_HOR) && KeyTyped)
9386 foldOpenCursor();
9387#endif
9388 undisplay_dollar();
9389 tpos = curwin->w_cursor;
9390 if (c == K_C_HOME)
9391 curwin->w_cursor.lnum = 1;
9392 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 curwin->w_curswant = 0;
9395 start_arrow(&tpos);
9396}
9397
9398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009399ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
9401 pos_T tpos;
9402
9403#ifdef FEAT_FOLDING
9404 if ((fdo_flags & FDO_HOR) && KeyTyped)
9405 foldOpenCursor();
9406#endif
9407 undisplay_dollar();
9408 tpos = curwin->w_cursor;
9409 if (c == K_C_END)
9410 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9411 coladvance((colnr_T)MAXCOL);
9412 curwin->w_curswant = MAXCOL;
9413
9414 start_arrow(&tpos);
9415}
9416
9417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009418ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420#ifdef FEAT_FOLDING
9421 if ((fdo_flags & FDO_HOR) && KeyTyped)
9422 foldOpenCursor();
9423#endif
9424 undisplay_dollar();
9425 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9426 {
9427 start_arrow(&curwin->w_cursor);
9428 (void)bck_word(1L, FALSE, FALSE);
9429 curwin->w_set_curswant = TRUE;
9430 }
9431 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009432 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433}
9434
9435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009436ins_right(
9437 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439#ifdef FEAT_FOLDING
9440 if ((fdo_flags & FDO_HOR) && KeyTyped)
9441 foldOpenCursor();
9442#endif
9443 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009444 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009446 start_arrow_with_change(&curwin->w_cursor, end_change);
9447 if (!end_change)
9448 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 if (virtual_active())
9451 oneright();
9452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009455 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 ++curwin->w_cursor.col;
9458 }
9459
9460#ifdef FEAT_RIGHTLEFT
9461 revins_legal++;
9462 if (revins_chars)
9463 revins_chars--;
9464#endif
9465 }
9466 /* if 'whichwrap' set for cursor in insert mode, may move the
9467 * cursor to the next line */
9468 else if (vim_strchr(p_ww, ']') != NULL
9469 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9470 {
9471 start_arrow(&curwin->w_cursor);
9472 curwin->w_set_curswant = TRUE;
9473 ++curwin->w_cursor.lnum;
9474 curwin->w_cursor.col = 0;
9475 }
9476 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009477 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009478 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479}
9480
9481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484#ifdef FEAT_FOLDING
9485 if ((fdo_flags & FDO_HOR) && KeyTyped)
9486 foldOpenCursor();
9487#endif
9488 undisplay_dollar();
9489 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9490 || gchar_cursor() != NUL)
9491 {
9492 start_arrow(&curwin->w_cursor);
9493 (void)fwd_word(1L, FALSE, 0);
9494 curwin->w_set_curswant = TRUE;
9495 }
9496 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009497 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498}
9499
9500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501ins_up(
9502 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503{
9504 pos_T tpos;
9505 linenr_T old_topline = curwin->w_topline;
9506#ifdef FEAT_DIFF
9507 int old_topfill = curwin->w_topfill;
9508#endif
9509
9510 undisplay_dollar();
9511 tpos = curwin->w_cursor;
9512 if (cursor_up(1L, TRUE) == OK)
9513 {
9514 if (startcol)
9515 coladvance(getvcol_nolist(&Insstart));
9516 if (old_topline != curwin->w_topline
9517#ifdef FEAT_DIFF
9518 || old_topfill != curwin->w_topfill
9519#endif
9520 )
9521 redraw_later(VALID);
9522 start_arrow(&tpos);
9523#ifdef FEAT_CINDENT
9524 can_cindent = TRUE;
9525#endif
9526 }
9527 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009528 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529}
9530
9531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009532ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533{
9534 pos_T tpos;
9535
9536 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009537
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009538 if (mod_mask & MOD_MASK_CTRL)
9539 {
9540 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009541 if (first_tabpage->tp_next != NULL)
9542 {
9543 start_arrow(&curwin->w_cursor);
9544 goto_tabpage(-1);
9545 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009546 return;
9547 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009548
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 tpos = curwin->w_cursor;
9550 if (onepage(BACKWARD, 1L) == OK)
9551 {
9552 start_arrow(&tpos);
9553#ifdef FEAT_CINDENT
9554 can_cindent = TRUE;
9555#endif
9556 }
9557 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009558 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559}
9560
9561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009562ins_down(
9563 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 pos_T tpos;
9566 linenr_T old_topline = curwin->w_topline;
9567#ifdef FEAT_DIFF
9568 int old_topfill = curwin->w_topfill;
9569#endif
9570
9571 undisplay_dollar();
9572 tpos = curwin->w_cursor;
9573 if (cursor_down(1L, TRUE) == OK)
9574 {
9575 if (startcol)
9576 coladvance(getvcol_nolist(&Insstart));
9577 if (old_topline != curwin->w_topline
9578#ifdef FEAT_DIFF
9579 || old_topfill != curwin->w_topfill
9580#endif
9581 )
9582 redraw_later(VALID);
9583 start_arrow(&tpos);
9584#ifdef FEAT_CINDENT
9585 can_cindent = TRUE;
9586#endif
9587 }
9588 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009589 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590}
9591
9592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009593ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 pos_T tpos;
9596
9597 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009598
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009599 if (mod_mask & MOD_MASK_CTRL)
9600 {
9601 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009602 if (first_tabpage->tp_next != NULL)
9603 {
9604 start_arrow(&curwin->w_cursor);
9605 goto_tabpage(0);
9606 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009607 return;
9608 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009609
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 tpos = curwin->w_cursor;
9611 if (onepage(FORWARD, 1L) == OK)
9612 {
9613 start_arrow(&tpos);
9614#ifdef FEAT_CINDENT
9615 can_cindent = TRUE;
9616#endif
9617 }
9618 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009619 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620}
9621
9622#ifdef FEAT_DND
9623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009624ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
9626 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9627}
9628#endif
9629
9630/*
9631 * Handle TAB in Insert or Replace mode.
9632 * Return TRUE when the TAB needs to be inserted like a normal character.
9633 */
9634 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009635ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637 int ind;
9638 int i;
9639 int temp;
9640
9641 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9642 Insstart_blank_vcol = get_nolist_virtcol();
9643 if (echeck_abbr(TAB + ABBR_OFF))
9644 return FALSE;
9645
9646 ind = inindent(0);
9647#ifdef FEAT_CINDENT
9648 if (ind)
9649 can_cindent = FALSE;
9650#endif
9651
9652 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009653 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 */
9655 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009656#ifdef FEAT_VARTABS
9657 && !(p_sta && ind
9658 /* These five lines mean 'tabstop' != 'shiftwidth' */
9659 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
9660 || (tabstop_count(curbuf->b_p_vts_array) == 1
9661 && tabstop_first(curbuf->b_p_vts_array)
9662 != get_sw_value(curbuf))
9663 || (tabstop_count(curbuf->b_p_vts_array) == 0
9664 && curbuf->b_p_ts != get_sw_value(curbuf))))
9665 && tabstop_count(curbuf->b_p_vsts_array) == 0
9666#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009667 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009668#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009669 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 return TRUE;
9671
9672 if (stop_arrow() == FAIL)
9673 return TRUE;
9674
9675 did_ai = FALSE;
9676#ifdef FEAT_SMARTINDENT
9677 did_si = FALSE;
9678 can_si = FALSE;
9679 can_si_back = FALSE;
9680#endif
9681 AppendToRedobuff((char_u *)"\t");
9682
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009683#ifdef FEAT_VARTABS
9684 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9685 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009686 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009687 temp -= get_nolist_virtcol() % temp;
9688 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009689 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009690 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009691 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009692 curbuf->b_p_vsts_array);
9693 else /* otherwise use 'tabstop' */
9694 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
9695 curbuf->b_p_vts_array);
9696#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009698 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009699 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9700 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 else /* otherwise use 'tabstop' */
9702 temp = (int)curbuf->b_p_ts;
9703 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705
9706 /*
9707 * Insert the first space with ins_char(). It will delete one char in
9708 * replace mode. Insert the rest with ins_str(); it will not delete any
9709 * chars. For VREPLACE mode, we use ins_char() for all characters.
9710 */
9711 ins_char(' ');
9712 while (--temp > 0)
9713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 if (State & VREPLACE_FLAG)
9715 ins_char(' ');
9716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 {
9718 ins_str((char_u *)" ");
9719 if (State & REPLACE_FLAG) /* no char replaced */
9720 replace_push(NUL);
9721 }
9722 }
9723
9724 /*
9725 * When 'expandtab' not set: Replace spaces by TABs where possible.
9726 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009727#ifdef FEAT_VARTABS
9728 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
9729 || get_sts_value() > 0
9730 || (p_sta && ind)))
9731#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009732 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 {
9735 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 char_u *saved_line = NULL; /* init for GCC */
9737 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 pos_T fpos;
9739 pos_T *cursor;
9740 colnr_T want_vcol, vcol;
9741 int change_col = -1;
9742 int save_list = curwin->w_p_list;
9743
9744 /*
9745 * Get the current line. For VREPLACE mode, don't make real changes
9746 * yet, just work on a copy of the line.
9747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 if (State & VREPLACE_FLAG)
9749 {
9750 pos = curwin->w_cursor;
9751 cursor = &pos;
9752 saved_line = vim_strsave(ml_get_curline());
9753 if (saved_line == NULL)
9754 return FALSE;
9755 ptr = saved_line + pos.col;
9756 }
9757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 {
9759 ptr = ml_get_cursor();
9760 cursor = &curwin->w_cursor;
9761 }
9762
9763 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9764 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9765 curwin->w_p_list = FALSE;
9766
9767 /* Find first white before the cursor */
9768 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01009769 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 {
9771 --fpos.col;
9772 --ptr;
9773 }
9774
9775 /* In Replace mode, don't change characters before the insert point. */
9776 if ((State & REPLACE_FLAG)
9777 && fpos.lnum == Insstart.lnum
9778 && fpos.col < Insstart.col)
9779 {
9780 ptr += Insstart.col - fpos.col;
9781 fpos.col = Insstart.col;
9782 }
9783
9784 /* compute virtual column numbers of first white and cursor */
9785 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9786 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9787
Bram Moolenaar597a4222014-06-25 14:39:50 +02009788 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9789 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01009790 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009792 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 if (vcol + i > want_vcol)
9794 break;
9795 if (*ptr != TAB)
9796 {
9797 *ptr = TAB;
9798 if (change_col < 0)
9799 {
9800 change_col = fpos.col; /* Column of first change */
9801 /* May have to adjust Insstart */
9802 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9803 Insstart.col = fpos.col;
9804 }
9805 }
9806 ++fpos.col;
9807 ++ptr;
9808 vcol += i;
9809 }
9810
9811 if (change_col >= 0)
9812 {
9813 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009814 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
9816 /* Skip over the spaces we need. */
9817 while (vcol < want_vcol && *ptr == ' ')
9818 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009819 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 ++ptr;
9821 ++repl_off;
9822 }
9823 if (vcol > want_vcol)
9824 {
9825 /* Must have a char with 'showbreak' just before it. */
9826 --ptr;
9827 --repl_off;
9828 }
9829 fpos.col += repl_off;
9830
9831 /* Delete following spaces. */
9832 i = cursor->col - fpos.col;
9833 if (i > 0)
9834 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009835 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02009837 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 for (temp = i; --temp >= 0; )
9839 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01009840#ifdef FEAT_TEXT_PROP
9841 curbuf->b_ml.ml_line_len -= i;
9842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009844#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009845 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009846 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009847 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009848 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9849 (char_u *)"\t", 1);
9850 }
9851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 cursor->col -= i;
9853
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 /*
9855 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9856 * backspacing over the changed spacing and then inserting the new
9857 * spacing.
9858 */
9859 if (State & VREPLACE_FLAG)
9860 {
9861 /* Backspace from real cursor to change_col */
9862 backspace_until_column(change_col);
9863
9864 /* Insert each char in saved_line from changed_col to
9865 * ptr-cursor */
9866 ins_bytes_len(saved_line + change_col,
9867 cursor->col - change_col);
9868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 }
9870
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 if (State & VREPLACE_FLAG)
9872 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 curwin->w_p_list = save_list;
9874 }
9875
9876 return FALSE;
9877}
9878
9879/*
9880 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02009881 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 */
9883 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009884ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
9886 int i;
9887
9888 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02009889 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02009891 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 undisplay_dollar();
9893
9894 /*
9895 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9896 * character under the cursor. Only push a NUL on the replace stack,
9897 * nothing to put back when the NL is deleted.
9898 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02009899 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 replace_push(NUL);
9901
9902 /*
9903 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9904 * replacing the next line, so we push all of the characters left on the
9905 * line onto the replace stack. This is not done here though, it is done
9906 * in open_line().
9907 */
9908
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009909 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9910 * CTRL-O). */
9911 if (virtual_active() && curwin->w_cursor.coladd > 0)
9912 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009913
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914#ifdef FEAT_RIGHTLEFT
9915# ifdef FEAT_FKMAP
9916 if (p_altkeymap && p_fkmap)
9917 fkmap(NL);
9918# endif
9919 /* NL in reverse insert will always start in the end of
9920 * current line. */
9921 if (revins_on)
9922 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9923#endif
9924
9925 AppendToRedobuff(NL_STR);
9926 i = open_line(FORWARD,
9927#ifdef FEAT_COMMENTS
9928 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9929#endif
9930 0, old_indent);
9931 old_indent = 0;
9932#ifdef FEAT_CINDENT
9933 can_cindent = TRUE;
9934#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009935#ifdef FEAT_FOLDING
9936 /* When inserting a line the cursor line must never be in a closed fold. */
9937 foldOpenCursor();
9938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
Bram Moolenaar24a2d722018-04-24 19:36:43 +02009940 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941}
9942
9943#ifdef FEAT_DIGRAPHS
9944/*
9945 * Handle digraph in insert mode.
9946 * Returns character still to be inserted, or NUL when nothing remaining to be
9947 * done.
9948 */
9949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009950ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
9952 int c;
9953 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009954 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
9956 pc_status = PC_STATUS_UNSET;
9957 if (redrawing() && !char_avail())
9958 {
9959 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009960 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961
9962 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009963 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964#ifdef FEAT_CMDL_INFO
9965 add_to_showcmd_c(Ctrl_K);
9966#endif
9967 }
9968
9969#ifdef USE_ON_FLY_SCROLL
9970 dont_scroll = TRUE; /* disallow scrolling here */
9971#endif
9972
9973 /* don't map the digraph chars. This also prevents the
9974 * mode message to be deleted when ESC is hit */
9975 ++no_mapping;
9976 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009977 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 --no_mapping;
9979 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009980 if (did_putchar)
9981 /* when the line fits in 'columns' the '?' is at the start of the next
9982 * line and will not be removed by the redraw */
9983 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009984
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 if (IS_SPECIAL(c) || mod_mask) /* special key */
9986 {
9987#ifdef FEAT_CMDL_INFO
9988 clear_showcmd();
9989#endif
9990 insert_special(c, TRUE, FALSE);
9991 return NUL;
9992 }
9993 if (c != ESC)
9994 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009995 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 if (redrawing() && !char_avail())
9997 {
9998 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009999 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000
10001 if (char2cells(c) == 1)
10002 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010003 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010005 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 }
10007#ifdef FEAT_CMDL_INFO
10008 add_to_showcmd_c(c);
10009#endif
10010 }
10011 ++no_mapping;
10012 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010013 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 --no_mapping;
10015 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010016 if (did_putchar)
10017 /* when the line fits in 'columns' the '?' is at the start of the
10018 * next line and will not be removed by a redraw */
10019 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 if (cc != ESC)
10021 {
10022 AppendToRedobuff((char_u *)CTRL_V_STR);
10023 c = getdigraph(c, cc, TRUE);
10024#ifdef FEAT_CMDL_INFO
10025 clear_showcmd();
10026#endif
10027 return c;
10028 }
10029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030#ifdef FEAT_CMDL_INFO
10031 clear_showcmd();
10032#endif
10033 return NUL;
10034}
10035#endif
10036
10037/*
10038 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10039 * Returns the char to be inserted, or NUL if none found.
10040 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010042ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043{
10044 int c;
10045 int temp;
10046 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010047 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048
10049 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10050 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010051 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 return NUL;
10053 }
10054
10055 /* try to advance to the cursor column */
10056 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010057 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 prev_ptr = ptr;
10059 validate_virtcol();
10060 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10061 {
10062 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010063 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 }
10065 if ((colnr_T)temp > curwin->w_virtcol)
10066 ptr = prev_ptr;
10067
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010070 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 return c;
10072}
10073
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010074/*
10075 * CTRL-Y or CTRL-E typed in Insert mode.
10076 */
10077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010078ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010079{
10080 int c = tc;
10081
10082#ifdef FEAT_INS_EXPAND
10083 if (ctrl_x_mode == CTRL_X_SCROLL)
10084 {
10085 if (c == Ctrl_Y)
10086 scrolldown_clamp();
10087 else
10088 scrollup_clamp();
10089 redraw_later(VALID);
10090 }
10091 else
10092#endif
10093 {
10094 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10095 if (c != NUL)
10096 {
10097 long tw_save;
10098
10099 /* The character must be taken literally, insert like it
10100 * was typed after a CTRL-V, and pretend 'textwidth'
10101 * wasn't set. Digits, 'o' and 'x' are special after a
10102 * CTRL-V, don't use it for these. */
10103 if (c < 256 && !isalnum(c))
10104 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10105 tw_save = curbuf->b_p_tw;
10106 curbuf->b_p_tw = -1;
10107 insert_special(c, TRUE, FALSE);
10108 curbuf->b_p_tw = tw_save;
10109#ifdef FEAT_RIGHTLEFT
10110 revins_chars++;
10111 revins_legal++;
10112#endif
10113 c = Ctrl_V; /* pretend CTRL-V is last character */
10114 auto_format(FALSE, TRUE);
10115 }
10116 }
10117 return c;
10118}
10119
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#ifdef FEAT_SMARTINDENT
10121/*
10122 * Try to do some very smart auto-indenting.
10123 * Used when inserting a "normal" character.
10124 */
10125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010126ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128 pos_T *pos, old_pos;
10129 char_u *ptr;
10130 int i;
10131 int temp;
10132
10133 /*
10134 * do some very smart indenting when entering '{' or '}'
10135 */
10136 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10137 {
10138 /*
10139 * for '}' set indent equal to indent of line containing matching '{'
10140 */
10141 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10142 {
10143 old_pos = curwin->w_cursor;
10144 /*
10145 * If the matching '{' has a ')' immediately before it (ignoring
10146 * white-space), then line up with the start of the line
10147 * containing the matching '(' if there is one. This handles the
10148 * case where an "if (..\n..) {" statement continues over multiple
10149 * lines -- webb
10150 */
10151 ptr = ml_get(pos->lnum);
10152 i = pos->col;
10153 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010154 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 ;
10156 curwin->w_cursor.lnum = pos->lnum;
10157 curwin->w_cursor.col = i;
10158 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10159 curwin->w_cursor = *pos;
10160 i = get_indent();
10161 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010163 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 (void)set_indent(i, SIN_CHANGED);
10166 }
10167 else if (curwin->w_cursor.col > 0)
10168 {
10169 /*
10170 * when inserting '{' after "O" reduce indent, but not
10171 * more than indent of previous line
10172 */
10173 temp = TRUE;
10174 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10175 {
10176 old_pos = curwin->w_cursor;
10177 i = get_indent();
10178 while (curwin->w_cursor.lnum > 1)
10179 {
10180 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10181
10182 /* ignore empty lines and lines starting with '#'. */
10183 if (*ptr != '#' && *ptr != NUL)
10184 break;
10185 }
10186 if (get_indent() >= i)
10187 temp = FALSE;
10188 curwin->w_cursor = old_pos;
10189 }
10190 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010191 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 }
10193 }
10194
10195 /*
10196 * set indent of '#' always to 0
10197 */
10198 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10199 {
10200 /* remember current indent for next line */
10201 old_indent = get_indent();
10202 (void)set_indent(0, SIN_CHANGED);
10203 }
10204
10205 /* Adjust ai_col, the char at this position can be deleted. */
10206 if (ai_col > curwin->w_cursor.col)
10207 ai_col = curwin->w_cursor.col;
10208}
10209#endif
10210
10211/*
10212 * Get the value that w_virtcol would have when 'list' is off.
10213 * Unless 'cpo' contains the 'L' flag.
10214 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010215 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010216get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010218 // check validity of cursor in current buffer
10219 if (curwin->w_buffer == NULL
10220 || curwin->w_buffer->b_ml.ml_mfp == NULL
10221 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10222 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10224 return getvcol_nolist(&curwin->w_cursor);
10225 validate_virtcol();
10226 return curwin->w_virtcol;
10227}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010228
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010229#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010230/*
10231 * Handle the InsertCharPre autocommand.
10232 * "c" is the character that was typed.
10233 * Return a pointer to allocated memory with the replacement string.
10234 * Return NULL to continue inserting "c".
10235 */
10236 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010237do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010238{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010239 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010240 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010241 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010242
10243 /* Return quickly when there is nothing to do. */
10244 if (!has_insertcharpre())
10245 return NULL;
10246
Bram Moolenaar704984a2012-06-01 14:57:51 +020010247 if (has_mbyte)
10248 buf[(*mb_char2bytes)(c, buf)] = NUL;
10249 else
Bram Moolenaar704984a2012-06-01 14:57:51 +020010250 {
10251 buf[0] = c;
10252 buf[1] = NUL;
10253 }
10254
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010255 /* Lock the text to avoid weird things from happening. */
10256 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010257 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010258
Bram Moolenaar704984a2012-06-01 14:57:51 +020010259 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010260 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010261 {
10262 /* Get the value of v:char. It may be empty or more than one
10263 * character. Only use it when changed, otherwise continue with the
10264 * original character to avoid breaking autoindent. */
10265 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10266 res = vim_strsave(get_vim_var_str(VV_CHAR));
10267 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010268
10269 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10270 --textlock;
10271
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010272 // Restore the State, it may have been changed.
10273 State = save_State;
10274
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010275 return res;
10276}
10277#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010278
10279/*
10280 * Trigger "event" and take care of fixing undo.
10281 */
10282 static int
10283ins_apply_autocmds(event_T event)
10284{
10285 varnumber_T tick = CHANGEDTICK(curbuf);
10286 int r;
10287
10288 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10289
10290 // If u_savesub() was called then we are not prepared to start
10291 // a new line. Call u_save() with no contents to fix that.
10292 if (tick != CHANGEDTICK(curbuf))
10293 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10294
10295 return r;
10296}