blob: 042826a872e4c770619ae9b94d0cb048fac3cd07 [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 Moolenaar2a78b7c2019-02-05 20:12:06 +01005024 if (pum_enough_matches())
5025 // Will display the popup menu, don't redraw yet to avoid flicker.
5026 pum_call_update_screen();
5027 else
5028 // Not showing the popup menu yet, redraw to show the user what was
5029 // inserted.
5030 update_screen(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005031
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005032 /* display the updated popup menu */
5033 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005034#ifdef FEAT_GUI
5035 if (gui.in_use)
5036 {
5037 /* Show the cursor after the match, not after the redrawn text. */
5038 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005039 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005040 }
5041#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 /* Delete old text to be replaced, since we're still searching and
5044 * don't want to match ourselves! */
5045 ins_compl_delete();
5046 }
5047
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005048 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005049 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005050 if (compl_no_insert && !started)
5051 compl_enter_selects = TRUE;
5052 else
5053 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 /*
5056 * Show the file name for the match (if any)
5057 * Truncate the file name to avoid a wait for return.
5058 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005059 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005061 char *lead = _("match in file");
5062 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5063 char_u *s;
5064 char_u *e;
5065
5066 if (space > 0)
5067 {
5068 /* We need the tail that fits. With double-byte encoding going
5069 * back from the end is very slow, thus go from the start and keep
5070 * the text that fits in "space" between "s" and "e". */
5071 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5072 {
5073 space -= ptr2cells(e);
5074 while (space < 0)
5075 {
5076 space += ptr2cells(s);
5077 MB_PTR_ADV(s);
5078 }
5079 }
5080 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5081 s > compl_shown_match->cp_fname ? "<" : "", s);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005082 msg((char *)IObuff);
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005083 redraw_cmdline = FALSE; /* don't overwrite! */
5084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086
5087 return num_matches;
5088}
5089
5090/*
5091 * Call this while finding completions, to check whether the user has hit a key
5092 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005093 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005095 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005096 * "in_compl_func" is TRUE when called from complete_check(), don't set
5097 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 */
5099 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005100ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101{
5102 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005103 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005105 /* Don't check when reading keys from a script, :normal or feedkeys().
5106 * That would break the test scripts. But do check for keys when called
5107 * from complete_check(). */
5108 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 return;
5110
5111 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005112 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 return;
5114 count = 0;
5115
Bram Moolenaara260a972006-06-23 19:36:29 +00005116 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5117 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 if (c != NUL)
5120 {
5121 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5122 {
5123 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005124 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005125 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005126 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005128 else
5129 {
5130 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005131 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005132 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005133 if (c != K_IGNORE)
5134 {
5135 /* Don't interrupt completion when the character wasn't typed,
5136 * e.g., when doing @q to replay keys. */
5137 if (c != Ctrl_R && KeyTyped)
5138 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005139
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005140 vungetc(c);
5141 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005144 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005145 {
5146 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5147
5148 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005149 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005150 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005151}
5152
5153/*
5154 * Decide the direction of Insert mode complete from the key typed.
5155 * Returns BACKWARD or FORWARD.
5156 */
5157 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005158ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005159{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005160 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005161 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005162 return BACKWARD;
5163 return FORWARD;
5164}
5165
5166/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005167 * Return TRUE for keys that are used for completion only when the popup menu
5168 * is visible.
5169 */
5170 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005171ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005172{
5173 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005174 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5175 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005176}
5177
5178/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005179 * Decide the number of completions to move forward.
5180 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5181 */
5182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005183ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005184{
5185 int h;
5186
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005187 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005188 {
5189 h = pum_get_height();
5190 if (h > 3)
5191 h -= 2; /* keep some context */
5192 return h;
5193 }
5194 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195}
5196
5197/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005198 * Return TRUE if completion with "c" should insert the match, FALSE if only
5199 * to change the currently selected completion.
5200 */
5201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005202ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005203{
5204 switch (c)
5205 {
5206 case K_UP:
5207 case K_DOWN:
5208 case K_PAGEDOWN:
5209 case K_KPAGEDOWN:
5210 case K_S_DOWN:
5211 case K_PAGEUP:
5212 case K_KPAGEUP:
5213 case K_S_UP:
5214 return FALSE;
5215 }
5216 return TRUE;
5217}
5218
5219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 * Do Insert mode completion.
5221 * Called when character "c" was typed, which has a meaning for completion.
5222 * Returns OK if completion was done, FAIL if something failed (out of mem).
5223 */
5224 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005225ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005227 char_u *line;
5228 int startcol = 0; /* column where searched text starts */
5229 colnr_T curs_col; /* cursor column */
5230 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005231 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005232 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005233 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005234 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
Bram Moolenaare3226be2005-12-18 22:10:00 +00005236 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005237 insert_match = ins_compl_use_match(c);
5238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 /* First time we hit ^N or ^P (in a row, I mean) */
5242
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 did_ai = FALSE;
5244#ifdef FEAT_SMARTINDENT
5245 did_si = FALSE;
5246 can_si = FALSE;
5247 can_si_back = FALSE;
5248#endif
5249 if (stop_arrow() == FAIL)
5250 return FAIL;
5251
5252 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005253 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005254 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005256 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 * "compl_startpos" to the cursor as a pattern to add a new word
5258 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005259 * "compl_startpos" is not in the same line as the cursor then fix it
5260 * (the line has been split because it was longer than 'tw'). if SOL
5261 * is set then skip the previous pattern, a word at the beginning of
5262 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005263 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5264 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
5266 /*
5267 * it is a continued search
5268 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005269 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005270 if (ctrl_x_mode == CTRL_X_NORMAL
5271 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5272 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005274 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005276 /* line (probably) wrapped, set compl_startpos to the
5277 * first non_blank in the line, if it is not a wordchar
5278 * include it to get a better pattern, but then we don't
5279 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005280 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 compl_startpos.col = compl_col;
5282 compl_startpos.lnum = curwin->w_cursor.lnum;
5283 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
5285 else
5286 {
5287 /* S_IPOS was set when we inserted a word that was at the
5288 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 * mode but first we need to redefine compl_startpos */
5290 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005292 compl_cont_status |= CONT_SOL;
5293 compl_startpos.col = (colnr_T)(skipwhite(
5294 line + compl_length
5295 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005297 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005300 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005301 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005303 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 compl_cont_status &= ~CONT_SOL;
5306 compl_length = (IOSIZE - MIN_SPACE);
5307 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5310 if (compl_length < 1)
5311 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005313 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005314 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005316 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
5318 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005319 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005321 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005323 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005324 if (ctrl_x_mode != CTRL_X_NORMAL)
5325 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 compl_cont_status = 0;
5327 compl_cont_status |= CONT_N_ADDS;
5328 compl_startpos = curwin->w_cursor;
5329 startcol = (int)curs_col;
5330 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
5332
5333 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005334 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5338 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 compl_col += ++startcol;
5344 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 compl_pattern = str_foldcase(line + compl_col,
5348 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 compl_pattern = vim_strnsave(line + compl_col,
5351 compl_length);
5352 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 return FAIL;
5354 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
5357 char_u *prefix = (char_u *)"\\<";
5358
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005359 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005361 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 if (!vim_iswordp(line + compl_col)
5365 || (compl_col > 0
Bram Moolenaar13505972019-01-24 15:04:48 +01005366 && (vim_iswordp(mb_prevptr(line, line + compl_col)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 STRCPY((char *)compl_pattern, prefix);
5369 (void)quote_meta(compl_pattern + STRLEN(prefix),
5370 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
Bram Moolenaar13505972019-01-24 15:04:48 +01005372 else if (--startcol < 0
5373 || !vim_iswordp(mb_prevptr(line, line + startcol + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
5375 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005376 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5377 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005379 compl_col += curs_col;
5380 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382 else
5383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 /* Search the point of change class of multibyte character
5385 * or not a word single byte character backward. */
5386 if (has_mbyte)
5387 {
5388 int base_class;
5389 int head_off;
5390
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005391 startcol -= (*mb_head_off)(line, line + startcol);
5392 base_class = mb_get_class(line + startcol);
5393 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 head_off = (*mb_head_off)(line, line + startcol);
5396 if (base_class != mb_get_class(line + startcol
5397 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
5401 }
5402 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 compl_col += ++startcol;
5406 compl_length = (int)curs_col - startcol;
5407 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 /* Only match word with at least two chars -- webb
5410 * there's no need to call quote_meta,
5411 * alloc(7) is enough -- Acevedo
5412 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 compl_pattern = alloc(7);
5414 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005416 STRCPY((char *)compl_pattern, "\\<");
5417 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5418 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
5420 else
5421 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005422 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005423 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005424 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 STRCPY((char *)compl_pattern, "\\<");
5427 (void)quote_meta(compl_pattern + 2, line + compl_col,
5428 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 }
5430 }
5431 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005432 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005434 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005435 compl_length = (int)curs_col - (int)compl_col;
5436 if (compl_length < 0) /* cursor in indent: empty pattern */
5437 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005439 compl_pattern = str_foldcase(line + compl_col, compl_length,
5440 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005442 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5443 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 return FAIL;
5445 }
5446 else if (ctrl_x_mode == CTRL_X_FILES)
5447 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005448 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005449 if (startcol > 0)
5450 {
5451 char_u *p = line + startcol;
5452
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005453 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005454 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005455 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005456 if (p == line && vim_isfilec(PTR2CHAR(p)))
5457 startcol = 0;
5458 else
5459 startcol = (int)(p - line) + 1;
5460 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005461
Bram Moolenaar0300e462013-09-08 16:03:45 +02005462 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005463 compl_length = (int)curs_col - startcol;
5464 compl_pattern = addstar(line + compl_col, compl_length,
5465 EXPAND_FILES);
5466 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 return FAIL;
5468 }
5469 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5470 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005471 compl_pattern = vim_strnsave(line, curs_col);
5472 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005475 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5477 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005478 /* No completion possible, use an empty pattern to get a
5479 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005480 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005481 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005482 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5483 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005485 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005486 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005487#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005488 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005489 * Call user defined function 'completefunc' with "a:findstart"
5490 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005491 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005492 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005493 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005494 char_u *funcname;
5495 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005496 win_T *curwin_save;
5497 buf_T *curbuf_save;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005498 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005499
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005500 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005501 * string */
5502 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5503 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5504 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005506 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005507 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005508 /* restore did_ai, so that adding comment leader works */
5509 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005510 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005511 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005512
Bram Moolenaarffa96842018-06-12 22:05:14 +02005513 args[0].v_type = VAR_NUMBER;
5514 args[0].vval.v_number = 1;
5515 args[1].v_type = VAR_STRING;
5516 args[1].vval.v_string = (char_u *)"";
5517 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005518 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005519 curwin_save = curwin;
5520 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005521 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005522
5523 State = save_State;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005524 if (curwin_save != curwin || curbuf_save != curbuf)
5525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005526 emsg(_(e_complwin));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005527 return FAIL;
5528 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005529 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005530 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005531 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005532 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005533 emsg(_(e_compldel));
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005534 return FAIL;
5535 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005536
Bram Moolenaar6110a002012-01-26 18:58:38 +01005537 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005538 * cancel the complete without an error.
5539 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005540 if (col == -2)
5541 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005542 if (col == -3)
5543 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005544 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005545 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005546 if (!shortmess(SHM_COMPLETIONMENU))
5547 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005548 return FAIL;
5549 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005550
Bram Moolenaar82139082011-09-14 16:52:09 +02005551 /*
5552 * Reset extended parameters of completion, when start new
5553 * completion.
5554 */
5555 compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005556 compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +02005557
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005558 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005559 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005560 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005561 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005562 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005563
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005564 /* Setup variables for completion. Need to obtain "line" again,
5565 * it may have become invalid. */
5566 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005567 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005568 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5569 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005570#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005571 return FAIL;
5572 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005573 else if (ctrl_x_mode == CTRL_X_SPELL)
5574 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005575#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005576 if (spell_bad_len > 0)
5577 compl_col = curs_col - spell_bad_len;
5578 else
5579 compl_col = spell_word_start(startcol);
5580 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005581 {
5582 compl_length = 0;
5583 compl_col = curs_col;
5584 }
5585 else
5586 {
5587 spell_expand_check_cap(compl_col);
5588 compl_length = (int)curs_col - compl_col;
5589 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005590 /* Need to obtain "line" again, it may have become invalid. */
5591 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005592 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5593 if (compl_pattern == NULL)
5594#endif
5595 return FAIL;
5596 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005597 else
5598 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005599 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005600 return FAIL;
5601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005603 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
5605 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005606 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
5608 /* Insert a new line, keep indentation but ignore 'comments' */
5609#ifdef FEAT_COMMENTS
5610 char_u *old = curbuf->b_p_com;
5611
5612 curbuf->b_p_com = (char_u *)"";
5613#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005614 compl_startpos.lnum = curwin->w_cursor.lnum;
5615 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 ins_eol('\r');
5617#ifdef FEAT_COMMENTS
5618 curbuf->b_p_com = old;
5619#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005620 compl_length = 0;
5621 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
5623 }
5624 else
5625 {
5626 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005627 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
5629
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005630 if (compl_cont_status & CONT_LOCAL)
5631 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 else
5633 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5634
Bram Moolenaar62951b12011-09-21 18:23:05 +02005635 /* If any of the original typed text has been changed we need to fix
5636 * the redo buffer. */
5637 ins_compl_fixRedoBufForLeader(NULL);
5638
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005639 /* Always add completion for the original text. */
5640 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005641 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5642 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005643 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005645 VIM_CLEAR(compl_pattern);
5646 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 return FAIL;
5648 }
5649
5650 /* showmode might reset the internal line pointers, so it must
5651 * be called before line = ml_get(), or when this address is no
5652 * longer needed. -- Acevedo.
5653 */
5654 edit_submode_extra = (char_u *)_("-- Searching...");
5655 edit_submode_highl = HLF_COUNT;
5656 showmode();
5657 edit_submode_extra = NULL;
5658 out_flush();
5659 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005660 else if (insert_match && stop_arrow() == FAIL)
5661 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005663 compl_shown_match = compl_curr_match;
5664 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005667 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005669 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005670 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005671 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005673 /* may undisplay the popup menu */
5674 ins_compl_upd_pum();
5675
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005676 if (n > 1) /* all matches have been found */
5677 compl_matches = n;
5678 compl_curr_match = compl_shown_match;
5679 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
Bram Moolenaard68071d2006-05-02 22:08:30 +00005681 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5682 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if (got_int && !global_busy)
5684 {
5685 (void)vgetc();
5686 got_int = FALSE;
5687 }
5688
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005689 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005690 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005692 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5693 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5695 edit_submode_highl = HLF_E;
5696 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5697 * because we couldn't expand anything at first place, but if we used
5698 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5699 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005700 if ( compl_length > 1
5701 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005702 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5704 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005705 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707
Bram Moolenaar572cb562005-08-05 21:35:02 +00005708 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005709 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005711 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
5713 if (edit_submode_extra == NULL)
5714 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005715 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717 edit_submode_extra = (char_u *)_("Back at original");
5718 edit_submode_highl = HLF_W;
5719 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005720 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 edit_submode_extra = (char_u *)_("Word from other line");
5723 edit_submode_highl = HLF_COUNT;
5724 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005725 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 {
5727 edit_submode_extra = (char_u *)_("The only match");
5728 edit_submode_highl = HLF_COUNT;
5729 }
5730 else
5731 {
5732 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005733 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005735 int number = 0;
5736 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005738 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
5740 /* search backwards for the first valid (!= -1) number.
5741 * This should normally succeed already at the first loop
5742 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005743 for (match = compl_curr_match->cp_prev; match != NULL
5744 && match != compl_first_match;
5745 match = match->cp_prev)
5746 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005748 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 break;
5750 }
5751 if (match != NULL)
5752 /* go up and assign all numbers which are not assigned
5753 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005754 for (match = match->cp_next;
5755 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005756 match = match->cp_next)
5757 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759 else /* BACKWARD */
5760 {
5761 /* search forwards (upwards) for the first valid (!= -1)
5762 * number. This should normally succeed already at the
5763 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005764 for (match = compl_curr_match->cp_next; match != NULL
5765 && match != compl_first_match;
5766 match = match->cp_next)
5767 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005769 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 break;
5771 }
5772 if (match != NULL)
5773 /* go down and assign all numbers which are not
5774 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005775 for (match = match->cp_prev; match
5776 && match->cp_number == -1;
5777 match = match->cp_prev)
5778 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
5780 }
5781
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005782 /* The match should always have a sequence number now, this is
5783 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005784 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005786 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5787 * Translations may need more than twice that. */
5788 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005790 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005791 vim_snprintf((char *)match_ref, sizeof(match_ref),
5792 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005793 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005795 vim_snprintf((char *)match_ref, sizeof(match_ref),
5796 _("match %d"),
5797 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 edit_submode_extra = match_ref;
5799 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005800 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 curs_columns(FALSE);
5802 }
5803 }
5804 }
5805
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005806 // Show a message about what (completion) mode we're in.
5807 if (!compl_opt_suppress_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005809 showmode();
5810 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaarea389e92014-05-28 21:40:52 +02005811 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005812 if (edit_submode_extra != NULL)
5813 {
5814 if (!p_smd)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005815 msg_attr((char *)edit_submode_extra,
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005816 edit_submode_highl < HLF_COUNT
5817 ? HL_ATTR(edit_submode_highl) : 0);
5818 }
5819 else
5820 msg_clr_cmdline(); // necessary for "noshowmode"
Bram Moolenaarea389e92014-05-28 21:40:52 +02005821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
Bram Moolenaard68071d2006-05-02 22:08:30 +00005824 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005825 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005826 show_pum(save_w_wrow, save_w_leftcol);
5827
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005828 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005829 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 return OK;
5832}
5833
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005834 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005835show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005836{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005837 /* RedrawingDisabled may be set when invoked through complete(). */
5838 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005839
Bram Moolenaarcb036422017-03-01 12:29:10 +01005840 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005841
Bram Moolenaarcb036422017-03-01 12:29:10 +01005842 /* If the cursor moved or the display scrolled we need to remove the pum
5843 * first. */
5844 setcursor();
5845 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5846 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005847
Bram Moolenaarcb036422017-03-01 12:29:10 +01005848 ins_compl_show_pum();
5849 setcursor();
5850 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005851}
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853/*
5854 * Looks in the first "len" chars. of "src" for search-metachars.
5855 * If dest is not NULL the chars. are copied there quoting (with
5856 * a backslash) the metachars, and dest would be NUL terminated.
5857 * Returns the length (needed) of dest
5858 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005859 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005860quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005862 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005864 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {
5866 switch (*src)
5867 {
5868 case '.':
5869 case '*':
5870 case '[':
5871 if (ctrl_x_mode == CTRL_X_DICTIONARY
5872 || ctrl_x_mode == CTRL_X_THESAURUS)
5873 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005874 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 case '~':
5876 if (!p_magic) /* quote these only if magic is set */
5877 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005878 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 case '\\':
5880 if (ctrl_x_mode == CTRL_X_DICTIONARY
5881 || ctrl_x_mode == CTRL_X_THESAURUS)
5882 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005883 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 case '^': /* currently it's not needed. */
5885 case '$':
5886 m++;
5887 if (dest != NULL)
5888 *dest++ = '\\';
5889 break;
5890 }
5891 if (dest != NULL)
5892 *dest++ = *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 /* Copy remaining bytes of a multibyte character. */
5894 if (has_mbyte)
5895 {
5896 int i, mb_len;
5897
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005898 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 if (mb_len > 0 && len >= mb_len)
5900 for (i = 0; i < mb_len; ++i)
5901 {
5902 --len;
5903 ++src;
5904 if (dest != NULL)
5905 *dest++ = *src;
5906 }
5907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 }
5909 if (dest != NULL)
5910 *dest = NUL;
5911
5912 return m;
5913}
5914#endif /* FEAT_INS_EXPAND */
5915
5916/*
5917 * Next character is interpreted literally.
5918 * A one, two or three digit decimal number is interpreted as its byte value.
5919 * If one or two digits are entered, the next character is given to vungetc().
5920 * For Unicode a character > 255 may be returned.
5921 */
5922 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005923get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 int cc;
5926 int nc;
5927 int i;
5928 int hex = FALSE;
5929 int octal = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 int unicode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
5932 if (got_int)
5933 return Ctrl_C;
5934
5935#ifdef FEAT_GUI
5936 /*
5937 * In GUI there is no point inserting the internal code for a special key.
5938 * It is more useful to insert the string "<KEY>" instead. This would
5939 * probably be useful in a text window too, but it would not be
5940 * vi-compatible (maybe there should be an option for it?) -- webb
5941 */
5942 if (gui.in_use)
5943 ++allow_keys;
5944#endif
5945#ifdef USE_ON_FLY_SCROLL
5946 dont_scroll = TRUE; /* disallow scrolling here */
5947#endif
5948 ++no_mapping; /* don't map the next key hits */
5949 cc = 0;
5950 i = 0;
5951 for (;;)
5952 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005953 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954#ifdef FEAT_CMDL_INFO
Bram Moolenaar13505972019-01-24 15:04:48 +01005955 if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 add_to_showcmd(nc);
5957#endif
5958 if (nc == 'x' || nc == 'X')
5959 hex = TRUE;
5960 else if (nc == 'o' || nc == 'O')
5961 octal = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 else if (nc == 'u' || nc == 'U')
5963 unicode = nc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 else
5965 {
Bram Moolenaar13505972019-01-24 15:04:48 +01005966 if (hex || unicode != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {
5968 if (!vim_isxdigit(nc))
5969 break;
5970 cc = cc * 16 + hex2nr(nc);
5971 }
5972 else if (octal)
5973 {
5974 if (nc < '0' || nc > '7')
5975 break;
5976 cc = cc * 8 + nc - '0';
5977 }
5978 else
5979 {
5980 if (!VIM_ISDIGIT(nc))
5981 break;
5982 cc = cc * 10 + nc - '0';
5983 }
5984
5985 ++i;
5986 }
5987
Bram Moolenaar13505972019-01-24 15:04:48 +01005988 if (cc > 255 && unicode == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 cc = 255; /* limit range to 0-255 */
5990 nc = 0;
5991
5992 if (hex) /* hex: up to two chars */
5993 {
5994 if (i >= 2)
5995 break;
5996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 else if (unicode) /* Unicode: up to four or eight chars */
5998 {
5999 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6000 break;
6001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 else if (i >= 3) /* decimal or octal: up to three chars */
6003 break;
6004 }
6005 if (i == 0) /* no number entered */
6006 {
6007 if (nc == K_ZERO) /* NUL is stored as NL */
6008 {
6009 cc = '\n';
6010 nc = 0;
6011 }
6012 else
6013 {
6014 cc = nc;
6015 nc = 0;
6016 }
6017 }
6018
6019 if (cc == 0) /* NUL is stored as NL */
6020 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006021 if (enc_dbcs && (cc & 0xff) == 0)
6022 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6023 second byte will cause trouble! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024
6025 --no_mapping;
6026#ifdef FEAT_GUI
6027 if (gui.in_use)
6028 --allow_keys;
6029#endif
6030 if (nc)
6031 vungetc(nc);
6032 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6033 return cc;
6034}
6035
6036/*
6037 * Insert character, taking care of special keys and mod_mask
6038 */
6039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006040insert_special(
6041 int c,
6042 int allow_modmask,
6043 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 char_u *p;
6046 int len;
6047
6048 /*
6049 * Special function key, translate into "<Key>". Up to the last '>' is
6050 * inserted with ins_str(), so as not to replace characters in replace
6051 * mode.
6052 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6053 * unless 'allow_modmask' is TRUE.
6054 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006055#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 /* Command-key never produces a normal key */
6057 if (mod_mask & MOD_MASK_CMD)
6058 allow_modmask = TRUE;
6059#endif
6060 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6061 {
6062 p = get_special_key_name(c, mod_mask);
6063 len = (int)STRLEN(p);
6064 c = p[len - 1];
6065 if (len > 2)
6066 {
6067 if (stop_arrow() == FAIL)
6068 return;
6069 p[len - 1] = NUL;
6070 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006071 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 ctrlv = FALSE;
6073 }
6074 }
6075 if (stop_arrow() == OK)
6076 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6077}
6078
6079/*
6080 * Special characters in this context are those that need processing other
6081 * than the simple insertion that can be performed here. This includes ESC
6082 * which terminates the insert, and CR/NL which need special processing to
6083 * open up a new line. This routine tries to optimize insertions performed by
6084 * the "redo", "undo" or "put" commands, so it needs to know when it should
6085 * stop and defer processing to the "normal" mechanism.
6086 * '0' and '^' are special, because they can be followed by CTRL-D.
6087 */
6088#ifdef EBCDIC
6089# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6090#else
6091# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6092#endif
6093
Bram Moolenaar13505972019-01-24 15:04:48 +01006094#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006096/*
6097 * "flags": INSCHAR_FORMAT - force formatting
6098 * INSCHAR_CTRLV - char typed just after CTRL-V
6099 * INSCHAR_NO_FEX - don't use 'formatexpr'
6100 *
6101 * NOTE: passes the flags value straight through to internal_format() which,
6102 * beside INSCHAR_FORMAT (above), is also looking for these:
6103 * INSCHAR_DO_COM - format comments
6104 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107insertchar(
6108 int c, /* character to insert or NUL */
6109 int flags, /* INSCHAR_FORMAT, etc. */
6110 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 int textwidth;
6113#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006117 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006119 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 /*
6123 * Try to break the line in two or more pieces when:
6124 * - Always do this if we have been called to do formatting only.
6125 * - Always do this when 'formatoptions' has the 'a' flag and the line
6126 * ends in white space.
6127 * - Otherwise:
6128 * - Don't do this if inserting a blank
6129 * - Don't do this if an existing character is being replaced, unless
6130 * we're in VREPLACE mode.
6131 * - Do this if the cursor is not on the line where insert started
6132 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6133 * before the insert.
6134 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6135 * before 'textwidth'
6136 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006137 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006138 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006139 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 && *ml_get_cursor() != NUL)
6143 && (curwin->w_cursor.lnum != Insstart.lnum
6144 || ((!has_format_option(FO_INS_LONG)
6145 || Insstart_textlen <= (colnr_T)textwidth)
6146 && (!fo_ins_blank
6147 || Insstart_blank_vcol <= (colnr_T)textwidth
6148 ))))))
6149 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006150 /* Format with 'formatexpr' when it's set. Use internal formatting
6151 * when 'formatexpr' isn't set or it returns non-zero. */
6152#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006153 int do_internal = TRUE;
6154 colnr_T virtcol = get_nolist_virtcol()
6155 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006156
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006157 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6158 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006159 {
6160 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6161 /* It may be required to save for undo again, e.g. when setline()
6162 * was called. */
6163 ins_need_undo = TRUE;
6164 }
6165 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006167 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 if (c == NUL) /* only formatting was wanted */
6171 return;
6172
6173#ifdef FEAT_COMMENTS
6174 /* Check whether this character should end a comment. */
6175 if (did_ai && (int)c == end_comment_pending)
6176 {
6177 char_u *line;
6178 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6179 int middle_len, end_len;
6180 int i;
6181
6182 /*
6183 * Need to remove existing (middle) comment leader and insert end
6184 * comment leader. First, check what comment leader we can find.
6185 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006186 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6188 {
6189 /* Skip middle-comment string */
6190 while (*p && p[-1] != ':') /* find end of middle flags */
6191 ++p;
6192 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6193 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006194 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 --middle_len;
6196
6197 /* Find the end-comment string */
6198 while (*p && p[-1] != ':') /* find end of end flags */
6199 ++p;
6200 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6201
6202 /* Skip white space before the cursor */
6203 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006204 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 ;
6206 i++;
6207
6208 /* Skip to before the middle leader */
6209 i -= middle_len;
6210
6211 /* Check some expected things before we go on */
6212 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6213 {
6214 /* Backspace over all the stuff we want to replace */
6215 backspace_until_column(i);
6216
6217 /*
6218 * Insert the end-comment string, except for the last
6219 * character, which will get inserted as normal later.
6220 */
6221 ins_bytes_len(lead_end, end_len - 1);
6222 }
6223 }
6224 }
6225 end_comment_pending = NUL;
6226#endif
6227
6228 did_ai = FALSE;
6229#ifdef FEAT_SMARTINDENT
6230 did_si = FALSE;
6231 can_si = FALSE;
6232 can_si_back = FALSE;
6233#endif
6234
6235 /*
6236 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6237 * This speeds up normal text input considerably.
6238 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6239 * need to re-indent at a ':', or any other character (but not what
6240 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006241 * Don't do this when there an InsertCharPre autocommand is defined,
6242 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006243 * Do the check for InsertCharPre before the call to vpeekc() because the
6244 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 */
6246#ifdef USE_ON_FLY_SCROLL
6247 dont_scroll = FALSE; /* allow scrolling here */
6248#endif
6249
6250 if ( !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 && (!has_mbyte || (*mb_char2len)(c) == 1)
Bram Moolenaar39de9522018-05-08 22:48:00 +02006252 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 && vpeekc() != NUL
6254 && !(State & REPLACE_FLAG)
6255#ifdef FEAT_CINDENT
6256 && !cindent_on()
6257#endif
6258#ifdef FEAT_RIGHTLEFT
6259 && !p_ri
6260#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006261 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263#define INPUT_BUFLEN 100
6264 char_u buf[INPUT_BUFLEN + 1];
6265 int i;
6266 colnr_T virtcol = 0;
6267
6268 buf[0] = c;
6269 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006270 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 virtcol = get_nolist_virtcol();
6272 /*
6273 * Stop the string when:
6274 * - no more chars available
6275 * - finding a special character (command key)
6276 * - buffer is full
6277 * - running into the 'textwidth' boundary
6278 * - need to check for abbreviation: A non-word char after a word-char
6279 */
6280 while ( (c = vpeekc()) != NUL
6281 && !ISSPECIAL(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006284# ifdef FEAT_FKMAP
6285 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 && (textwidth == 0
6288 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6289 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6290 {
6291#ifdef FEAT_RIGHTLEFT
6292 c = vgetc();
6293 if (p_hkmap && KeyTyped)
6294 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 buf[i++] = c;
6296#else
6297 buf[i++] = vgetc();
6298#endif
6299 }
6300
6301#ifdef FEAT_DIGRAPHS
6302 do_digraph(-1); /* clear digraphs */
6303 do_digraph(buf[i-1]); /* may be the start of a digraph */
6304#endif
6305 buf[i] = NUL;
6306 ins_str(buf);
6307 if (flags & INSCHAR_CTRLV)
6308 {
6309 redo_literal(*buf);
6310 i = 1;
6311 }
6312 else
6313 i = 0;
6314 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006315 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 }
6317 else
6318 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006319 int cc;
6320
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6322 {
6323 char_u buf[MB_MAXBYTES + 1];
6324
6325 (*mb_char2bytes)(c, buf);
6326 buf[cc] = NUL;
6327 ins_char_bytes(buf, cc);
6328 AppendCharToRedobuff(c);
6329 }
6330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {
6332 ins_char(c);
6333 if (flags & INSCHAR_CTRLV)
6334 redo_literal(c);
6335 else
6336 AppendCharToRedobuff(c);
6337 }
6338 }
6339}
6340
6341/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006342 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006343 *
6344 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6345 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006346 */
6347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006348internal_format(
6349 int textwidth,
6350 int second_indent,
6351 int flags,
6352 int format_only,
6353 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006354{
6355 int cc;
6356 int save_char = NUL;
6357 int haveto_redraw = FALSE;
6358 int fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006359 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006360 int fo_white_par = has_format_option(FO_WHITE_PAR);
6361 int first_line = TRUE;
6362#ifdef FEAT_COMMENTS
6363 colnr_T leader_len;
6364 int no_leader = FALSE;
6365 int do_comments = (flags & INSCHAR_DO_COM);
6366#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006367#ifdef FEAT_LINEBREAK
6368 int has_lbr = curwin->w_p_lbr;
6369
6370 /* make sure win_lbr_chartabsize() counts correctly */
6371 curwin->w_p_lbr = FALSE;
6372#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006373
6374 /*
6375 * When 'ai' is off we don't want a space under the cursor to be
6376 * deleted. Replace it with an 'x' temporarily.
6377 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006378 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006379 {
6380 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006381 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006382 {
6383 save_char = cc;
6384 pchar_cursor('x');
6385 }
6386 }
6387
6388 /*
6389 * Repeat breaking lines, until the current line is not too long.
6390 */
6391 while (!got_int)
6392 {
6393 int startcol; /* Cursor column at entry */
6394 int wantcol; /* column at textwidth border */
6395 int foundcol; /* column for start of spaces */
6396 int end_foundcol = 0; /* column for start of word */
6397 colnr_T len;
6398 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006399 int orig_col = 0;
6400 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006402 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006403 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006404
Bram Moolenaar97b98102009-11-17 16:41:01 +00006405 virtcol = get_nolist_virtcol()
6406 + char2cells(c != NUL ? c : gchar_cursor());
6407 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006408 break;
6409
6410#ifdef FEAT_COMMENTS
6411 if (no_leader)
6412 do_comments = FALSE;
6413 else if (!(flags & INSCHAR_FORMAT)
6414 && has_format_option(FO_WRAP_COMS))
6415 do_comments = TRUE;
6416
6417 /* Don't break until after the comment leader */
6418 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006419 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006420 else
6421 leader_len = 0;
6422
6423 /* If the line doesn't start with a comment leader, then don't
6424 * start one in a following broken line. Avoids that a %word
6425 * moved to the start of the next line causes all following lines
6426 * to start with %. */
6427 if (leader_len == 0)
6428 no_leader = TRUE;
6429#endif
6430 if (!(flags & INSCHAR_FORMAT)
6431#ifdef FEAT_COMMENTS
6432 && leader_len == 0
6433#endif
6434 && !has_format_option(FO_WRAP))
6435
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006436 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437 if ((startcol = curwin->w_cursor.col) == 0)
6438 break;
6439
6440 /* find column of textwidth border */
6441 coladvance((colnr_T)textwidth);
6442 wantcol = curwin->w_cursor.col;
6443
Bram Moolenaar97b98102009-11-17 16:41:01 +00006444 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006445 foundcol = 0;
6446
6447 /*
6448 * Find position to break at.
6449 * Stop at first entered white when 'formatoptions' has 'v'
6450 */
6451 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006452 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453 || curwin->w_cursor.lnum != Insstart.lnum
6454 || curwin->w_cursor.col >= Insstart.col)
6455 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006456 if (curwin->w_cursor.col == startcol && c != NUL)
6457 cc = c;
6458 else
6459 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460 if (WHITECHAR(cc))
6461 {
6462 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006463 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006464
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006465 // find start of sequence of blanks
6466 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006467 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6468 {
6469 dec_cursor();
6470 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006471
6472 // Increment count of how many whitespace chars in this
6473 // group; we only need to know if it's more than one.
6474 if (wcc < 2)
6475 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006476 }
6477 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6478 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006479
6480 // Don't break after a period when 'formatoptions' has 'p' and
6481 // there are less than two spaces.
6482 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
6483 continue;
6484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006485#ifdef FEAT_COMMENTS
6486 /* Don't break until after the comment leader */
6487 if (curwin->w_cursor.col < leader_len)
6488 break;
6489#endif
6490 if (has_format_option(FO_ONE_LETTER))
6491 {
6492 /* do not break after one-letter words */
6493 if (curwin->w_cursor.col == 0)
6494 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006495#ifdef FEAT_COMMENTS
6496 /* do not break "#a b" when 'tw' is 2 */
6497 if (curwin->w_cursor.col <= leader_len)
6498 break;
6499#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006500 col = curwin->w_cursor.col;
6501 dec_cursor();
6502 cc = gchar_cursor();
6503
6504 if (WHITECHAR(cc))
6505 continue; /* one-letter, continue */
6506 curwin->w_cursor.col = col;
6507 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006508
6509 inc_cursor();
6510
6511 end_foundcol = end_col + 1;
6512 foundcol = curwin->w_cursor.col;
6513 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514 break;
6515 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006516 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006517 {
6518 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006519 if (curwin->w_cursor.col != startcol)
6520 {
6521#ifdef FEAT_COMMENTS
6522 /* Don't break until after the comment leader */
6523 if (curwin->w_cursor.col < leader_len)
6524 break;
6525#endif
6526 col = curwin->w_cursor.col;
6527 inc_cursor();
6528 /* Don't change end_foundcol if already set. */
6529 if (foundcol != curwin->w_cursor.col)
6530 {
6531 foundcol = curwin->w_cursor.col;
6532 end_foundcol = foundcol;
6533 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6534 break;
6535 }
6536 curwin->w_cursor.col = col;
6537 }
6538
6539 if (curwin->w_cursor.col == 0)
6540 break;
6541
6542 col = curwin->w_cursor.col;
6543
6544 dec_cursor();
6545 cc = gchar_cursor();
6546
6547 if (WHITECHAR(cc))
6548 continue; /* break with space */
6549#ifdef FEAT_COMMENTS
6550 /* Don't break until after the comment leader */
6551 if (curwin->w_cursor.col < leader_len)
6552 break;
6553#endif
6554
6555 curwin->w_cursor.col = col;
6556
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006557 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006558 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006559 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6560 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006561 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006562 if (curwin->w_cursor.col == 0)
6563 break;
6564 dec_cursor();
6565 }
6566
6567 if (foundcol == 0) /* no spaces, cannot break line */
6568 {
6569 curwin->w_cursor.col = startcol;
6570 break;
6571 }
6572
6573 /* Going to break the line, remove any "$" now. */
6574 undisplay_dollar();
6575
6576 /*
6577 * Offset between cursor position and line break is used by replace
6578 * stack functions. VREPLACE does not use this, and backspaces
6579 * over the text instead.
6580 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006581 if (State & VREPLACE_FLAG)
6582 orig_col = startcol; /* Will start backspacing from here */
6583 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006584 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006585
6586 /*
6587 * adjust startcol for spaces that will be deleted and
6588 * characters that will remain on top line
6589 */
6590 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006591 while ((cc = gchar_cursor(), WHITECHAR(cc))
6592 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006593 inc_cursor();
6594 startcol -= curwin->w_cursor.col;
6595 if (startcol < 0)
6596 startcol = 0;
6597
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006598 if (State & VREPLACE_FLAG)
6599 {
6600 /*
6601 * In VREPLACE mode, we will backspace over the text to be
6602 * wrapped, so save a copy now to put on the next line.
6603 */
6604 saved_text = vim_strsave(ml_get_cursor());
6605 curwin->w_cursor.col = orig_col;
6606 if (saved_text == NULL)
6607 break; /* Can't do it, out of memory */
6608 saved_text[startcol] = NUL;
6609
6610 /* Backspace over characters that will move to the next line */
6611 if (!fo_white_par)
6612 backspace_until_column(foundcol);
6613 }
6614 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006615 {
6616 /* put cursor after pos. to break line */
6617 if (!fo_white_par)
6618 curwin->w_cursor.col = foundcol;
6619 }
6620
6621 /*
6622 * Split the line just before the margin.
6623 * Only insert/delete lines, but don't really redraw the window.
6624 */
6625 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6626 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6627#ifdef FEAT_COMMENTS
6628 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006629 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006630#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006631 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6632 if (!(flags & INSCHAR_COM_LIST))
6633 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006634
6635 replace_offset = 0;
6636 if (first_line)
6637 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006638 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006639 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006640 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006641 * This section is for auto-wrap of numeric lists. When not
6642 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6643 * flag will be set and open_line() will handle it (as seen
6644 * above). The code here (and in get_number_indent()) will
6645 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006646 */
6647 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006648 second_indent =
6649 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006650 if (second_indent >= 0)
6651 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006652 if (State & VREPLACE_FLAG)
6653 change_indent(INDENT_SET, second_indent,
6654 FALSE, NUL, TRUE);
6655 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006656#ifdef FEAT_COMMENTS
6657 if (leader_len > 0 && second_indent - leader_len > 0)
6658 {
6659 int i;
6660 int padding = second_indent - leader_len;
6661
6662 /* We started at the first_line of a numbered list
6663 * that has a comment. the open_line() function has
6664 * inserted the proper comment leader and positioned
6665 * the cursor at the end of the split line. Now we
6666 * add the additional whitespace needed after the
6667 * comment leader for the numbered list. */
6668 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006669 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006670 }
6671 else
6672 {
6673#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006674 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006675#ifdef FEAT_COMMENTS
6676 }
6677#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006678 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006679 }
6680 first_line = FALSE;
6681 }
6682
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006683 if (State & VREPLACE_FLAG)
6684 {
6685 /*
6686 * In VREPLACE mode we have backspaced over the text to be
6687 * moved, now we re-insert it into the new line.
6688 */
6689 ins_bytes(saved_text);
6690 vim_free(saved_text);
6691 }
6692 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006693 {
6694 /*
6695 * Check if cursor is not past the NUL off the line, cindent
6696 * may have added or removed indent.
6697 */
6698 curwin->w_cursor.col += startcol;
6699 len = (colnr_T)STRLEN(ml_get_curline());
6700 if (curwin->w_cursor.col > len)
6701 curwin->w_cursor.col = len;
6702 }
6703
6704 haveto_redraw = TRUE;
6705#ifdef FEAT_CINDENT
6706 can_cindent = TRUE;
6707#endif
6708 /* moved the cursor, don't autoindent or cindent now */
6709 did_ai = FALSE;
6710#ifdef FEAT_SMARTINDENT
6711 did_si = FALSE;
6712 can_si = FALSE;
6713 can_si_back = FALSE;
6714#endif
6715 line_breakcheck();
6716 }
6717
6718 if (save_char != NUL) /* put back space after cursor */
6719 pchar_cursor(save_char);
6720
Bram Moolenaar0026d472014-09-09 16:32:39 +02006721#ifdef FEAT_LINEBREAK
6722 curwin->w_p_lbr = has_lbr;
6723#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006724 if (!format_only && haveto_redraw)
6725 {
6726 update_topline();
6727 redraw_curbuf_later(VALID);
6728 }
6729}
6730
6731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 * Called after inserting or deleting text: When 'formatoptions' includes the
6733 * 'a' flag format from the current line until the end of the paragraph.
6734 * Keep the cursor at the same position relative to the text.
6735 * The caller must have saved the cursor line for undo, following ones will be
6736 * saved here.
6737 */
6738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006739auto_format(
6740 int trailblank, /* when TRUE also format with trailing blank */
6741 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
6743 pos_T pos;
6744 colnr_T len;
6745 char_u *old;
6746 char_u *new, *pnew;
6747 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006748 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
6750 if (!has_format_option(FO_AUTO))
6751 return;
6752
6753 pos = curwin->w_cursor;
6754 old = ml_get_curline();
6755
6756 /* may remove added space */
6757 check_auto_format(FALSE);
6758
6759 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6760 * user might insert normal text next. Also skip formatting when "1" is
6761 * in 'formatoptions' and there is a single character before the cursor.
6762 * Otherwise the line would be broken and when typing another non-white
6763 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006764 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (*old != NUL && !trailblank && wasatend)
6766 {
6767 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006768 cc = gchar_cursor();
6769 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6770 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006772 cc = gchar_cursor();
6773 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
6775 curwin->w_cursor = pos;
6776 return;
6777 }
6778 curwin->w_cursor = pos;
6779 }
6780
6781#ifdef FEAT_COMMENTS
6782 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6783 * comments. */
6784 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006785 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 return;
6787#endif
6788
6789 /*
6790 * May start formatting in a previous line, so that after "x" a word is
6791 * moved to the previous line if it fits there now. Only when this is not
6792 * the start of a paragraph.
6793 */
6794 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6795 {
6796 --curwin->w_cursor.lnum;
6797 if (u_save_cursor() == FAIL)
6798 return;
6799 }
6800
6801 /*
6802 * Do the formatting and restore the cursor position. "saved_cursor" will
6803 * be adjusted for the text formatting.
6804 */
6805 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006806 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 curwin->w_cursor = saved_cursor;
6808 saved_cursor.lnum = 0;
6809
6810 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6811 {
6812 /* "cannot happen" */
6813 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6814 coladvance((colnr_T)MAXCOL);
6815 }
6816 else
6817 check_cursor_col();
6818
6819 /* Insert mode: If the cursor is now after the end of the line while it
6820 * previously wasn't, the line was broken. Because of the rule above we
6821 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6822 * formatted. */
6823 if (!wasatend && has_format_option(FO_WHITE_PAR))
6824 {
6825 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006826 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (curwin->w_cursor.col == len)
6828 {
6829 pnew = vim_strnsave(new, len + 2);
6830 pnew[len] = ' ';
6831 pnew[len + 1] = NUL;
6832 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6833 /* remove the space later */
6834 did_add_space = TRUE;
6835 }
6836 else
6837 /* may remove added space */
6838 check_auto_format(FALSE);
6839 }
6840
6841 check_cursor();
6842}
6843
6844/*
6845 * When an extra space was added to continue a paragraph for auto-formatting,
6846 * delete it now. The space must be under the cursor, just after the insert
6847 * position.
6848 */
6849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006850check_auto_format(
6851 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006854 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 if (did_add_space)
6857 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006858 cc = gchar_cursor();
6859 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 /* Somehow the space was removed already. */
6861 did_add_space = FALSE;
6862 else
6863 {
6864 if (!end_insert)
6865 {
6866 inc_cursor();
6867 c = gchar_cursor();
6868 dec_cursor();
6869 }
6870 if (c != NUL)
6871 {
6872 /* The space is no longer at the end of the line, delete it. */
6873 del_char(FALSE);
6874 did_add_space = FALSE;
6875 }
6876 }
6877 }
6878}
6879
6880/*
6881 * Find out textwidth to be used for formatting:
6882 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006883 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 * if invalid value, use 0.
6885 * Set default to window width (maximum 79) for "gq" operator.
6886 */
6887 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006888comp_textwidth(
6889 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
6891 int textwidth;
6892
6893 textwidth = curbuf->b_p_tw;
6894 if (textwidth == 0 && curbuf->b_p_wm)
6895 {
6896 /* The width is the window width minus 'wrapmargin' minus all the
6897 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006898 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899#ifdef FEAT_CMDWIN
6900 if (cmdwin_type != 0)
6901 textwidth -= 1;
6902#endif
6903#ifdef FEAT_FOLDING
6904 textwidth -= curwin->w_p_fdc;
6905#endif
6906#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006907 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 textwidth -= 1;
6909#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006910 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 textwidth -= 8;
6912 }
6913 if (textwidth < 0)
6914 textwidth = 0;
6915 if (ff && textwidth == 0)
6916 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006917 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (textwidth > 79)
6919 textwidth = 79;
6920 }
6921 return textwidth;
6922}
6923
6924/*
6925 * Put a character in the redo buffer, for when just after a CTRL-V.
6926 */
6927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006928redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 char_u buf[10];
6931
6932 /* Only digits need special treatment. Translate them into a string of
6933 * three digits. */
6934 if (VIM_ISDIGIT(c))
6935 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006936 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 AppendToRedobuff(buf);
6938 }
6939 else
6940 AppendCharToRedobuff(c);
6941}
6942
6943/*
6944 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006945 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 */
6947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006948start_arrow(
6949 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006951 start_arrow_common(end_insert_pos, TRUE);
6952}
6953
6954/*
6955 * Like start_arrow() but with end_change argument.
6956 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6957 */
6958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006959start_arrow_with_change(
6960 pos_T *end_insert_pos, /* can be NULL */
6961 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006962{
6963 start_arrow_common(end_insert_pos, end_change);
6964 if (!end_change)
6965 {
6966 AppendCharToRedobuff(Ctrl_G);
6967 AppendCharToRedobuff('U');
6968 }
6969}
6970
6971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972start_arrow_common(
6973 pos_T *end_insert_pos, /* can be NULL */
6974 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006975{
6976 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {
6978 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006979 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 arrow_used = TRUE; /* this means we stopped the current insert */
6981 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006982#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006983 check_spell_redraw();
6984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985}
6986
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006987#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006988/*
6989 * If we skipped highlighting word at cursor, do it now.
6990 * It may be skipped again, thus reset spell_redraw_lnum first.
6991 */
6992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006993check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006994{
6995 if (spell_redraw_lnum != 0)
6996 {
6997 linenr_T lnum = spell_redraw_lnum;
6998
6999 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01007000 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007001 }
7002}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007003
7004/*
7005 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7006 * spelled word, if there is one.
7007 */
7008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007009spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007010{
7011 pos_T tpos = curwin->w_cursor;
7012
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007013 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007014 if (curwin->w_cursor.col != tpos.col)
7015 start_arrow(&tpos);
7016}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007017#endif
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019/*
7020 * stop_arrow() is called before a change is made in insert mode.
7021 * If an arrow key has been used, start a new insertion.
7022 * Returns FAIL if undo is impossible, shouldn't insert then.
7023 */
7024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007025stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
7027 if (arrow_used)
7028 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007029 Insstart = curwin->w_cursor; /* new insertion starts here */
7030 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7031 /* Don't update the original insert position when moved to the
7032 * right, except when nothing was inserted yet. */
7033 update_Insstart_orig = FALSE;
7034 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7035
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 if (u_save_cursor() == OK)
7037 {
7038 arrow_used = FALSE;
7039 ins_need_undo = FALSE;
7040 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 if (State & VREPLACE_FLAG)
7044 {
7045 orig_line_count = curbuf->b_ml.ml_line_count;
7046 vr_lines_changed = 1;
7047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 ResetRedobuff();
7049 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007050 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 }
7052 else if (ins_need_undo)
7053 {
7054 if (u_save_cursor() == OK)
7055 ins_need_undo = FALSE;
7056 }
7057
7058#ifdef FEAT_FOLDING
7059 /* Always open fold at the cursor line when inserting something. */
7060 foldOpenCursor();
7061#endif
7062
7063 return (arrow_used || ins_need_undo ? FAIL : OK);
7064}
7065
7066/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007067 * Do a few things to stop inserting.
7068 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7069 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 */
7071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072stop_insert(
7073 pos_T *end_insert_pos,
7074 int esc, /* called by ins_esc() */
7075 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007077 int cc;
7078 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
7080 stop_redo_ins();
7081 replace_flush(); /* abandon replace stack */
7082
7083 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007084 * Save the inserted text for later redo with ^@ and CTRL-A.
7085 * Don't do it when "restart_edit" was set and nothing was inserted,
7086 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007088 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007089 if (did_restart_edit == 0 || (ptr != NULL
7090 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007091 {
7092 vim_free(last_insert);
7093 last_insert = ptr;
7094 last_insert_skip = new_insert_skip;
7095 }
7096 else
7097 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007099 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
7101 /* Auto-format now. It may seem strange to do this when stopping an
7102 * insertion (or moving the cursor), but it's required when appending
7103 * a line and having it end in a space. But only do it when something
7104 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007105 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007107 pos_T tpos = curwin->w_cursor;
7108
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 /* When the cursor is at the end of the line after a space the
7110 * formatting will move it to the following word. Avoid that by
7111 * moving the cursor onto the space. */
7112 cc = 'x';
7113 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7114 {
7115 dec_cursor();
7116 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007117 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007118 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
7120
7121 auto_format(TRUE, FALSE);
7122
Bram Moolenaar1c465442017-03-12 20:10:05 +01007123 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007124 {
7125 if (gchar_cursor() != NUL)
7126 inc_cursor();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007127 // If the cursor is still at the same character, also keep
7128 // the "coladd".
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007129 if (gchar_cursor() == NUL
7130 && curwin->w_cursor.lnum == tpos.lnum
7131 && curwin->w_cursor.col == tpos.col)
7132 curwin->w_cursor.coladd = tpos.coladd;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 }
7135
7136 /* If a space was inserted for auto-formatting, remove it now. */
7137 check_auto_format(TRUE);
7138
7139 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007140 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007141 * Do this when ESC was used or moving the cursor up/down.
7142 * Check for the old position still being valid, just in case the text
7143 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007144 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007145 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7146 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007148 pos_T tpos = curwin->w_cursor;
7149
7150 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007151 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007152 for (;;)
7153 {
7154 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7155 --curwin->w_cursor.col;
7156 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007157 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007158 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007159 if (del_char(TRUE) == FAIL)
7160 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007161 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007162 if (curwin->w_cursor.lnum != tpos.lnum)
7163 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007164 else
7165 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007166 /* reset tpos, could have been invalidated in the loop above */
7167 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007168 tpos.col++;
7169 if (cc != NUL && gchar_pos(&tpos) == NUL)
7170 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 /* <C-S-Right> may have started Visual mode, adjust the position for
7174 * deleted characters. */
7175 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7176 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007177 int len = (int)STRLEN(ml_get_curline());
7178
7179 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007181 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 VIsual.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
7184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 }
7186 }
7187 did_ai = FALSE;
7188#ifdef FEAT_SMARTINDENT
7189 did_si = FALSE;
7190 can_si = FALSE;
7191 can_si_back = FALSE;
7192#endif
7193
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007194 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7195 * now in a different buffer. */
7196 if (end_insert_pos != NULL)
7197 {
7198 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007199 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007200 curbuf->b_op_end = *end_insert_pos;
7201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202}
7203
7204/*
7205 * Set the last inserted text to a single character.
7206 * Used for the replace command.
7207 */
7208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007209set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
7211 char_u *s;
7212
7213 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 if (last_insert != NULL)
7216 {
7217 s = last_insert;
7218 /* Use the CTRL-V only when entering a special char */
7219 if (c < ' ' || c == DEL)
7220 *s++ = Ctrl_V;
7221 s = add_char2buf(c, s);
7222 *s++ = ESC;
7223 *s++ = NUL;
7224 last_insert_skip = 0;
7225 }
7226}
7227
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007228#if defined(EXITFREE) || defined(PROTO)
7229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007230free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007231{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007232 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007233# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007234 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007235# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007236}
7237#endif
7238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239/*
7240 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7241 * and CSI. Handle multi-byte characters.
7242 * Returns a pointer to after the added bytes.
7243 */
7244 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007245add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007247 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 int i;
7249 int len;
7250
7251 len = (*mb_char2bytes)(c, temp);
7252 for (i = 0; i < len; ++i)
7253 {
7254 c = temp[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7256 if (c == K_SPECIAL)
7257 {
7258 *s++ = K_SPECIAL;
7259 *s++ = KS_SPECIAL;
7260 *s++ = KE_FILLER;
7261 }
7262#ifdef FEAT_GUI
7263 else if (c == CSI)
7264 {
7265 *s++ = CSI;
7266 *s++ = KS_EXTRA;
7267 *s++ = (int)KE_CSI;
7268 }
7269#endif
7270 else
7271 *s++ = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 return s;
7274}
7275
7276/*
7277 * move cursor to start of line
7278 * if flags & BL_WHITE move to first non-white
7279 * if flags & BL_SOL move to first non-white if startofline is set,
7280 * otherwise keep "curswant" column
7281 * if flags & BL_FIX don't leave the cursor on a NUL.
7282 */
7283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007284beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
7286 if ((flags & BL_SOL) && !p_sol)
7287 coladvance(curwin->w_curswant);
7288 else
7289 {
7290 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
7293 if (flags & (BL_WHITE | BL_SOL))
7294 {
7295 char_u *ptr;
7296
Bram Moolenaar1c465442017-03-12 20:10:05 +01007297 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7299 ++curwin->w_cursor.col;
7300 }
7301 curwin->w_set_curswant = TRUE;
7302 }
7303}
7304
7305/*
7306 * oneright oneleft cursor_down cursor_up
7307 *
7308 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007309 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 * Return OK when successful, FAIL when we hit a line of file boundary.
7311 */
7312
7313 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007314oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 if (virtual_active())
7320 {
7321 pos_T prevpos = curwin->w_cursor;
7322
7323 /* Adjust for multi-wide char (excluding TAB) */
7324 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007325 coladvance(getviscol() + ((*ptr != TAB
7326 && vim_isprintc((*mb_ptr2char)(ptr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 ? ptr2cells(ptr) : 1));
7328 curwin->w_set_curswant = TRUE;
7329 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7330 return (prevpos.col != curwin->w_cursor.col
7331 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
7334 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007335 if (*ptr == NUL)
7336 return FAIL; /* already at the very end */
7337
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007338 if (has_mbyte)
7339 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 else
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007341 l = 1;
7342
7343 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7344 * contains "onemore". */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007345 if (ptr[l] == NUL && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007346 return FAIL;
7347 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
7349 curwin->w_set_curswant = TRUE;
7350 return OK;
7351}
7352
7353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007354oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 if (virtual_active())
7357 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007358#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 int width;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 int v = getviscol();
7362
7363 if (v == 0)
7364 return FAIL;
7365
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007366#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 /* We might get stuck on 'showbreak', skip over it. */
7368 width = 1;
7369 for (;;)
7370 {
7371 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007372 /* getviscol() is slow, skip it when 'showbreak' is empty,
7373 * 'breakindent' is not set and there are no multi-byte
7374 * characters */
7375 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar13505972019-01-24 15:04:48 +01007376 && !has_mbyte) || getviscol() < v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 break;
7378 ++width;
7379 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007380#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 coladvance(v - 1);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383
7384 if (curwin->w_cursor.coladd == 1)
7385 {
7386 char_u *ptr;
7387
7388 /* Adjust for multi-wide char (not a TAB) */
7389 ptr = ml_get_cursor();
Bram Moolenaar13505972019-01-24 15:04:48 +01007390 if (*ptr != TAB && vim_isprintc((*mb_ptr2char)(ptr))
7391 && ptr2cells(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 curwin->w_cursor.coladd = 0;
7393 }
7394
7395 curwin->w_set_curswant = TRUE;
7396 return OK;
7397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
7399 if (curwin->w_cursor.col == 0)
7400 return FAIL;
7401
7402 curwin->w_set_curswant = TRUE;
7403 --curwin->w_cursor.col;
7404
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 /* if the character on the left of the current cursor is a multi-byte
7406 * character, move to its first byte */
7407 if (has_mbyte)
7408 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 return OK;
7410}
7411
7412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007413cursor_up(
7414 long n,
7415 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
7417 linenr_T lnum;
7418
7419 if (n > 0)
7420 {
7421 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007422 /* This fails if the cursor is already in the first line or the count
7423 * is larger than the line number and '-' is in 'cpoptions' */
7424 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 return FAIL;
7426 if (n >= lnum)
7427 lnum = 1;
7428 else
7429#ifdef FEAT_FOLDING
7430 if (hasAnyFolding(curwin))
7431 {
7432 /*
7433 * Count each sequence of folded lines as one logical line.
7434 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007435 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 (void)hasFolding(lnum, &lnum, NULL);
7437
7438 while (n--)
7439 {
7440 /* move up one line */
7441 --lnum;
7442 if (lnum <= 1)
7443 break;
7444 /* If we entered a fold, move to the beginning, unless in
7445 * Insert mode or when 'foldopen' contains "all": it will open
7446 * in a moment. */
7447 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7448 (void)hasFolding(lnum, &lnum, NULL);
7449 }
7450 if (lnum < 1)
7451 lnum = 1;
7452 }
7453 else
7454#endif
7455 lnum -= n;
7456 curwin->w_cursor.lnum = lnum;
7457 }
7458
7459 /* try to advance to the column we want to be at */
7460 coladvance(curwin->w_curswant);
7461
7462 if (upd_topline)
7463 update_topline(); /* make sure curwin->w_topline is valid */
7464
7465 return OK;
7466}
7467
7468/*
7469 * Cursor down a number of logical lines.
7470 */
7471 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007472cursor_down(
7473 long n,
7474 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475{
7476 linenr_T lnum;
7477
7478 if (n > 0)
7479 {
7480 lnum = curwin->w_cursor.lnum;
7481#ifdef FEAT_FOLDING
7482 /* Move to last line of fold, will fail if it's the end-of-file. */
7483 (void)hasFolding(lnum, NULL, &lnum);
7484#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007485 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007486 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007487 if (lnum >= curbuf->b_ml.ml_line_count
7488 || (lnum + n > curbuf->b_ml.ml_line_count
7489 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 return FAIL;
7491 if (lnum + n >= curbuf->b_ml.ml_line_count)
7492 lnum = curbuf->b_ml.ml_line_count;
7493 else
7494#ifdef FEAT_FOLDING
7495 if (hasAnyFolding(curwin))
7496 {
7497 linenr_T last;
7498
7499 /* count each sequence of folded lines as one logical line */
7500 while (n--)
7501 {
7502 if (hasFolding(lnum, NULL, &last))
7503 lnum = last + 1;
7504 else
7505 ++lnum;
7506 if (lnum >= curbuf->b_ml.ml_line_count)
7507 break;
7508 }
7509 if (lnum > curbuf->b_ml.ml_line_count)
7510 lnum = curbuf->b_ml.ml_line_count;
7511 }
7512 else
7513#endif
7514 lnum += n;
7515 curwin->w_cursor.lnum = lnum;
7516 }
7517
7518 /* try to advance to the column we want to be at */
7519 coladvance(curwin->w_curswant);
7520
7521 if (upd_topline)
7522 update_topline(); /* make sure curwin->w_topline is valid */
7523
7524 return OK;
7525}
7526
7527/*
7528 * Stuff the last inserted text in the read buffer.
7529 * Last_insert actually is a copy of the redo buffer, so we
7530 * first have to remove the command.
7531 */
7532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007533stuff_inserted(
7534 int c, /* Command character to be inserted */
7535 long count, /* Repeat this many times */
7536 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 char_u *esc_ptr;
7539 char_u *ptr;
7540 char_u *last_ptr;
7541 char_u last = NUL;
7542
7543 ptr = get_last_insert();
7544 if (ptr == NULL)
7545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007546 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 return FAIL;
7548 }
7549
7550 /* may want to stuff the command character, to start Insert mode */
7551 if (c != NUL)
7552 stuffcharReadbuff(c);
7553 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7554 *esc_ptr = NUL; /* remove the ESC */
7555
7556 /* when the last char is either "0" or "^" it will be quoted if no ESC
7557 * comes after it OR if it will inserted more than once and "ptr"
7558 * starts with ^D. -- Acevedo
7559 */
7560 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7561 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7562 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7563 {
7564 last = *last_ptr;
7565 *last_ptr = NUL;
7566 }
7567
7568 do
7569 {
7570 stuffReadbuff(ptr);
7571 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7572 if (last)
7573 stuffReadbuff((char_u *)(last == '0'
7574 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7575 : IF_EB("\026^", CTRL_V_STR "^")));
7576 }
7577 while (--count > 0);
7578
7579 if (last)
7580 *last_ptr = last;
7581
7582 if (esc_ptr != NULL)
7583 *esc_ptr = ESC; /* put the ESC back */
7584
7585 /* may want to stuff a trailing ESC, to get out of Insert mode */
7586 if (!no_esc)
7587 stuffcharReadbuff(ESC);
7588
7589 return OK;
7590}
7591
7592 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007593get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594{
7595 if (last_insert == NULL)
7596 return NULL;
7597 return last_insert + last_insert_skip;
7598}
7599
7600/*
7601 * Get last inserted string, and remove trailing <Esc>.
7602 * Returns pointer to allocated memory (must be freed) or NULL.
7603 */
7604 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007605get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 char_u *s;
7608 int len;
7609
7610 if (last_insert == NULL)
7611 return NULL;
7612 s = vim_strsave(last_insert + last_insert_skip);
7613 if (s != NULL)
7614 {
7615 len = (int)STRLEN(s);
7616 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7617 s[len - 1] = NUL;
7618 }
7619 return s;
7620}
7621
7622/*
7623 * Check the word in front of the cursor for an abbreviation.
7624 * Called when the non-id character "c" has been entered.
7625 * When an abbreviation is recognized it is removed from the text and
7626 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7627 */
7628 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007629echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 /* Don't check for abbreviation in paste mode, when disabled and just
7632 * after moving around with cursor keys. */
7633 if (p_paste || no_abbr || arrow_used)
7634 return FALSE;
7635
7636 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7637 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7638}
7639
7640/*
7641 * replace-stack functions
7642 *
7643 * When replacing characters, the replaced characters are remembered for each
7644 * new character. This is used to re-insert the old text when backspacing.
7645 *
7646 * There is a NUL headed list of characters for each character that is
7647 * currently in the file after the insertion point. When BS is used, one NUL
7648 * headed list is put back for the deleted character.
7649 *
7650 * For a newline, there are two NUL headed lists. One contains the characters
7651 * that the NL replaced. The extra one stores the characters after the cursor
7652 * that were deleted (always white space).
7653 *
7654 * Replace_offset is normally 0, in which case replace_push will add a new
7655 * character at the end of the stack. If replace_offset is not 0, that many
7656 * characters will be left on the stack above the newly inserted character.
7657 */
7658
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007659static char_u *replace_stack = NULL;
7660static long replace_stack_nr = 0; /* next entry in replace stack */
7661static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662
7663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007664replace_push(
7665 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
7667 char_u *p;
7668
7669 if (replace_stack_nr < replace_offset) /* nothing to do */
7670 return;
7671 if (replace_stack_len <= replace_stack_nr)
7672 {
7673 replace_stack_len += 50;
7674 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7675 if (p == NULL) /* out of memory */
7676 {
7677 replace_stack_len -= 50;
7678 return;
7679 }
7680 if (replace_stack != NULL)
7681 {
7682 mch_memmove(p, replace_stack,
7683 (size_t)(replace_stack_nr * sizeof(char_u)));
7684 vim_free(replace_stack);
7685 }
7686 replace_stack = p;
7687 }
7688 p = replace_stack + replace_stack_nr - replace_offset;
7689 if (replace_offset)
7690 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7691 *p = c;
7692 ++replace_stack_nr;
7693}
7694
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007695/*
7696 * Push a character onto the replace stack. Handles a multi-byte character in
7697 * reverse byte order, so that the first byte is popped off first.
7698 * Return the number of bytes done (includes composing characters).
7699 */
7700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007701replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007702{
7703 int l = (*mb_ptr2len)(p);
7704 int j;
7705
7706 for (j = l - 1; j >= 0; --j)
7707 replace_push(p[j]);
7708 return l;
7709}
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007710
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711/*
7712 * Pop one item from the replace stack.
7713 * return -1 if stack empty
7714 * return replaced character or NUL otherwise
7715 */
7716 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007717replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
7719 if (replace_stack_nr == 0)
7720 return -1;
7721 return (int)replace_stack[--replace_stack_nr];
7722}
7723
7724/*
7725 * Join the top two items on the replace stack. This removes to "off"'th NUL
7726 * encountered.
7727 */
7728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007729replace_join(
7730 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
7732 int i;
7733
7734 for (i = replace_stack_nr; --i >= 0; )
7735 if (replace_stack[i] == NUL && off-- <= 0)
7736 {
7737 --replace_stack_nr;
7738 mch_memmove(replace_stack + i, replace_stack + i + 1,
7739 (size_t)(replace_stack_nr - i));
7740 return;
7741 }
7742}
7743
7744/*
7745 * Pop bytes from the replace stack until a NUL is found, and insert them
7746 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7747 */
7748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007749replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750{
7751 int cc;
7752 int oldState = State;
7753
7754 State = NORMAL; /* don't want REPLACE here */
7755 while ((cc = replace_pop()) > 0)
7756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 mb_replace_pop_ins(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 dec_cursor();
7759 }
7760 State = oldState;
7761}
7762
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763/*
7764 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7765 * indicates a multi-byte char, pop the other bytes too.
7766 */
7767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007768mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769{
7770 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007771 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 int i;
7773 int c;
7774
7775 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7776 {
7777 buf[0] = cc;
7778 for (i = 1; i < n; ++i)
7779 buf[i] = replace_pop();
7780 ins_bytes_len(buf, n);
7781 }
7782 else
7783 ins_char(cc);
7784
7785 if (enc_utf8)
7786 /* Handle composing chars. */
7787 for (;;)
7788 {
7789 c = replace_pop();
7790 if (c == -1) /* stack empty */
7791 break;
7792 if ((n = MB_BYTE2LEN(c)) == 1)
7793 {
7794 /* Not a multi-byte char, put it back. */
7795 replace_push(c);
7796 break;
7797 }
7798 else
7799 {
7800 buf[0] = c;
7801 for (i = 1; i < n; ++i)
7802 buf[i] = replace_pop();
7803 if (utf_iscomposing(utf_ptr2char(buf)))
7804 ins_bytes_len(buf, n);
7805 else
7806 {
7807 /* Not a composing char, put it back. */
7808 for (i = n - 1; i >= 0; --i)
7809 replace_push(buf[i]);
7810 break;
7811 }
7812 }
7813 }
7814}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815
7816/*
7817 * make the replace stack empty
7818 * (called when exiting replace mode)
7819 */
7820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007821replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007823 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 replace_stack_len = 0;
7825 replace_stack_nr = 0;
7826}
7827
7828/*
7829 * Handle doing a BS for one character.
7830 * cc < 0: replace stack empty, just move cursor
7831 * cc == 0: character was inserted, delete it
7832 * cc > 0: character was replaced, put cc (first byte of original char) back
7833 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007834 * When "limit_col" is >= 0, don't delete before this column. Matters when
7835 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 */
7837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007838replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 int orig_len = 0;
7842 int ins_len;
7843 int orig_vcols = 0;
7844 colnr_T start_vcol;
7845 char_u *p;
7846 int i;
7847 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
7849 cc = replace_pop();
7850 if (cc > 0)
7851 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007852#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007853 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007854
7855 if (curbuf->b_has_textprop)
7856 {
7857 // Do not adjust text properties for individual delete and insert
7858 // operations, do it afterwards on the resulting text.
7859 len_before = STRLEN(ml_get_curline());
7860 ++text_prop_frozen;
7861 }
7862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 if (State & VREPLACE_FLAG)
7864 {
7865 /* Get the number of screen cells used by the character we are
7866 * going to delete. */
7867 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7868 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 if (has_mbyte)
7871 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007872 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007874 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 replace_push(cc);
7876 }
7877 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {
7879 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007881 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883 replace_pop_ins();
7884
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (State & VREPLACE_FLAG)
7886 {
7887 /* Get the number of screen cells used by the inserted characters */
7888 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007889 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 vcol = start_vcol;
7891 for (i = 0; i < ins_len; ++i)
7892 {
7893 vcol += chartabsize(p + i, vcol);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007894 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896 vcol -= start_vcol;
7897
7898 /* Delete spaces that were inserted after the cursor to keep the
7899 * text aligned. */
7900 curwin->w_cursor.col += ins_len;
7901 while (vcol > orig_vcols && gchar_cursor() == ' ')
7902 {
7903 del_char(FALSE);
7904 ++orig_vcols;
7905 }
7906 curwin->w_cursor.col -= ins_len;
7907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908
Bram Moolenaar196d1572019-01-02 23:47:18 +01007909 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01007911
7912#ifdef FEAT_TEXT_PROP
7913 if (curbuf->b_has_textprop)
7914 {
7915 size_t len_now = STRLEN(ml_get_curline());
7916
7917 --text_prop_frozen;
7918 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
7919 (int)(len_now - len_before));
7920 }
7921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 }
7923 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007924 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925}
7926
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7928/*
7929 * Map Hebrew keyboard when in hkmap mode.
7930 */
7931 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007932hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7935 {
7936 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7937 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7938 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7939 static char_u map[26] =
7940 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7941 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7942 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7943 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7944 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7945 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7946 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7947 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7948 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7949
7950 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7951 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7952 /* '-1'='sofit' */
7953 else if (c == 'x')
7954 return 'X';
7955 else if (c == 'q')
7956 return '\''; /* {geresh}={'} */
7957 else if (c == 246)
7958 return ' '; /* \"o --> ' ' for a german keyboard */
7959 else if (c == 228)
7960 return ' '; /* \"a --> ' ' -- / -- */
7961 else if (c == 252)
7962 return ' '; /* \"u --> ' ' -- / -- */
7963#ifdef EBCDIC
7964 else if (islower(c))
7965#else
7966 /* NOTE: islower() does not do the right thing for us on Linux so we
7967 * do this the same was as 5.7 and previous, so it works correctly on
7968 * all systems. Specifically, the e.g. Delete and Arrow keys are
7969 * munged and won't work if e.g. searching for Hebrew text.
7970 */
7971 else if (c >= 'a' && c <= 'z')
7972#endif
7973 return (int)(map[CharOrdLow(c)] + p_aleph);
7974 else
7975 return c;
7976 }
7977 else
7978 {
7979 switch (c)
7980 {
7981 case '`': return ';';
7982 case '/': return '.';
7983 case '\'': return ',';
7984 case 'q': return '/';
7985 case 'w': return '\'';
7986
7987 /* Hebrew letters - set offset from 'a' */
7988 case ',': c = '{'; break;
7989 case '.': c = 'v'; break;
7990 case ';': c = 't'; break;
7991 default: {
7992 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7993
7994#ifdef EBCDIC
7995 /* see note about islower() above */
7996 if (!islower(c))
7997#else
7998 if (c < 'a' || c > 'z')
7999#endif
8000 return c;
8001 c = str[CharOrdLow(c)];
8002 break;
8003 }
8004 }
8005
8006 return (int)(CharOrdLow(c) + p_aleph);
8007 }
8008}
8009#endif
8010
8011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008012ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013{
8014 int need_redraw = FALSE;
8015 int regname;
8016 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008017 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
8019 /*
8020 * If we are going to wait for a character, show a '"'.
8021 */
8022 pc_status = PC_STATUS_UNSET;
8023 if (redrawing() && !char_avail())
8024 {
8025 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008026 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027
8028 edit_putchar('"', TRUE);
8029#ifdef FEAT_CMDL_INFO
8030 add_to_showcmd_c(Ctrl_R);
8031#endif
8032 }
8033
8034#ifdef USE_ON_FLY_SCROLL
8035 dont_scroll = TRUE; /* disallow scrolling here */
8036#endif
8037
8038 /*
8039 * Don't map the register name. This also prevents the mode message to be
8040 * deleted when ESC is hit.
8041 */
8042 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008043 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8046 {
8047 /* Get a third key for literal register insertion */
8048 literally = regname;
8049#ifdef FEAT_CMDL_INFO
8050 add_to_showcmd_c(literally);
8051#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008052 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 }
8055 --no_mapping;
8056
8057#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008058 /* Don't call u_sync() while typing the expression or giving an error
8059 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 ++no_u_sync;
8061 if (regname == '=')
8062 {
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008063 pos_T curpos = curwin->w_cursor;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008064# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008066# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008067 /* Sync undo when evaluating the expression calls setline() or
8068 * append(), so that it can be undone separately. */
8069 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008070
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 regname = get_expr_register();
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008072
8073 // Cursor may be moved back a column.
8074 curwin->w_cursor = curpos;
8075 check_cursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008076# ifdef HAVE_INPUT_METHOD
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01008077 // Restore the Input Method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 if (im_on)
8079 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008082 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8083 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008084 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 else
8088 {
8089#endif
8090 if (literally == Ctrl_O || literally == Ctrl_P)
8091 {
8092 /* Append the command to the redo buffer. */
8093 AppendCharToRedobuff(Ctrl_R);
8094 AppendCharToRedobuff(literally);
8095 AppendCharToRedobuff(regname);
8096
8097 do_put(regname, BACKWARD, 1L,
8098 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8099 }
8100 else if (insert_reg(regname, literally) == FAIL)
8101 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008102 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 need_redraw = TRUE; /* remove the '"' */
8104 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008105 else if (stop_insert_mode)
8106 /* When the '=' register was used and a function was invoked that
8107 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8108 * insert anything, need to remove the '"' */
8109 need_redraw = TRUE;
8110
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111#ifdef FEAT_EVAL
8112 }
8113 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008114 if (u_sync_once == 1)
8115 ins_need_undo = TRUE;
8116 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117#endif
8118#ifdef FEAT_CMDL_INFO
8119 clear_showcmd();
8120#endif
8121
8122 /* If the inserted register is empty, we need to remove the '"' */
8123 if (need_redraw || stuff_empty())
8124 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008125
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008126 /* Disallow starting Visual mode here, would get a weird mode. */
8127 if (!vis_active && VIsual_active)
8128 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129}
8130
8131/*
8132 * CTRL-G commands in Insert mode.
8133 */
8134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008135ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 int c;
8138
8139#ifdef FEAT_INS_EXPAND
8140 /* Right after CTRL-X the cursor will be after the ruler. */
8141 setcursor();
8142#endif
8143
8144 /*
8145 * Don't map the second key. This also prevents the mode message to be
8146 * deleted when ESC is hit.
8147 */
8148 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008149 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 --no_mapping;
8151 switch (c)
8152 {
8153 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8154 case K_UP:
8155 case Ctrl_K:
8156 case 'k': ins_up(TRUE);
8157 break;
8158
8159 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8160 case K_DOWN:
8161 case Ctrl_J:
8162 case 'j': ins_down(TRUE);
8163 break;
8164
8165 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008166 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008168
8169 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008170 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008171 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008172 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 break;
8174
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008175 /* CTRL-G U: do not break undo with the next char */
8176 case 'U':
8177 /* Allow one left/right cursor movement with the next char,
8178 * without breaking undo. */
8179 dont_sync_undo = MAYBE;
8180 break;
8181
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008183 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185}
8186
8187/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008188 * CTRL-^ in Insert mode.
8189 */
8190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008191ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008192{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008193 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008194 {
8195 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8196 if (State & LANGMAP)
8197 {
8198 curbuf->b_p_iminsert = B_IMODE_NONE;
8199 State &= ~LANGMAP;
8200 }
8201 else
8202 {
8203 curbuf->b_p_iminsert = B_IMODE_LMAP;
8204 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008205#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008206 im_set_active(FALSE);
8207#endif
8208 }
8209 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008210#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008211 else
8212 {
8213 /* There are no ":lmap" mappings, toggle IM */
8214 if (im_get_status())
8215 {
8216 curbuf->b_p_iminsert = B_IMODE_NONE;
8217 im_set_active(FALSE);
8218 }
8219 else
8220 {
8221 curbuf->b_p_iminsert = B_IMODE_IM;
8222 State &= ~LANGMAP;
8223 im_set_active(TRUE);
8224 }
8225 }
8226#endif
8227 set_iminsert_global();
8228 showmode();
8229#ifdef FEAT_GUI
8230 /* may show different cursor shape or color */
8231 if (gui.in_use)
8232 gui_update_cursor(TRUE, FALSE);
8233#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008234#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008235 /* Show/unshow value of 'keymap' in status lines. */
8236 status_redraw_curbuf();
8237#endif
8238}
8239
8240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 * Handle ESC in insert mode.
8242 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8243 * insert.
8244 */
8245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008246ins_esc(
8247 long *count,
8248 int cmdchar,
8249 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
8251 int temp;
8252 static int disabled_redraw = FALSE;
8253
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008254#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008255 check_spell_redraw();
8256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257#if defined(FEAT_HANGULIN)
8258# if defined(ESC_CHG_TO_ENG_MODE)
8259 hangul_input_state_set(0);
8260# endif
8261 if (composing_hangul)
8262 {
8263 push_raw_key(composing_hangul_buffer, 2);
8264 composing_hangul = 0;
8265 }
8266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267
8268 temp = curwin->w_cursor.col;
8269 if (disabled_redraw)
8270 {
8271 --RedrawingDisabled;
8272 disabled_redraw = FALSE;
8273 }
8274 if (!arrow_used)
8275 {
8276 /*
8277 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008278 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8279 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 */
8281 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008282 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283
8284 /*
8285 * Repeating insert may take a long time. Check for
8286 * interrupt now and then.
8287 */
8288 if (*count > 0)
8289 {
8290 line_breakcheck();
8291 if (got_int)
8292 *count = 0;
8293 }
8294
8295 if (--*count > 0) /* repeat what was typed */
8296 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008297 /* Vi repeats the insert without replacing characters. */
8298 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8299 State &= ~REPLACE_FLAG;
8300
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 (void)start_redo_ins();
8302 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008303 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 ++RedrawingDisabled;
8305 disabled_redraw = TRUE;
8306 return FALSE; /* repeat the insert */
8307 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008308 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 undisplay_dollar();
8310 }
8311
8312 /* When an autoindent was removed, curswant stays after the
8313 * indent */
8314 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8315 curwin->w_set_curswant = TRUE;
8316
8317 /* Remember the last Insert position in the '^ mark. */
8318 if (!cmdmod.keepjumps)
8319 curbuf->b_last_insert = curwin->w_cursor;
8320
8321 /*
8322 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008323 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008325 if (!nomove
8326 && (curwin->w_cursor.col != 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008327 || curwin->w_cursor.coladd > 0)
Bram Moolenaar488c6512005-08-11 20:09:58 +00008328 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008329 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330#ifdef FEAT_RIGHTLEFT
8331 && !revins_on
8332#endif
8333 )
8334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8336 {
8337 oneleft();
8338 if (restart_edit != NUL)
8339 ++curwin->w_cursor.coladd;
8340 }
8341 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {
8343 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 /* Correct cursor for multi-byte character. */
8345 if (has_mbyte)
8346 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 }
8348 }
8349
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008350#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 /* Disable IM to allow typing English directly for Normal mode commands.
8352 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8353 * well). */
8354 if (!(State & LANGMAP))
8355 im_save_status(&curbuf->b_p_iminsert);
8356 im_set_active(FALSE);
8357#endif
8358
8359 State = NORMAL;
8360 /* need to position cursor again (e.g. when on a TAB ) */
8361 changed_cline_bef_curs();
8362
8363#ifdef FEAT_MOUSE
8364 setmouse();
8365#endif
8366#ifdef CURSOR_SHAPE
8367 ui_cursor_shape(); /* may show different cursor shape */
8368#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008369 if (!p_ek)
8370 /* Re-enable bracketed paste mode. */
8371 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372
8373 /*
8374 * When recording or for CTRL-O, need to display the new mode.
8375 * Otherwise remove the mode message.
8376 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008377 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 showmode();
Bram Moolenaarcb574f42019-01-25 22:29:57 +01008379 else if (p_smd && !skip_showmode())
Bram Moolenaar32526b32019-01-19 17:43:09 +01008380 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381
8382 return TRUE; /* exit Insert mode */
8383}
8384
8385#ifdef FEAT_RIGHTLEFT
8386/*
8387 * Toggle language: hkmap and revins_on.
8388 * Move to end of reverse inserted text.
8389 */
8390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008391ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392{
8393 if (revins_on && revins_chars && revins_scol >= 0)
8394 {
8395 while (gchar_cursor() != NUL && revins_chars--)
8396 ++curwin->w_cursor.col;
8397 }
8398 p_ri = !p_ri;
8399 revins_on = (State == INSERT && p_ri);
8400 if (revins_on)
8401 {
8402 revins_scol = curwin->w_cursor.col;
8403 revins_legal++;
8404 revins_chars = 0;
8405 undisplay_dollar();
8406 }
8407 else
8408 revins_scol = -1;
8409#ifdef FEAT_FKMAP
8410 if (p_altkeymap)
8411 {
8412 /*
8413 * to be consistent also for redo command, using '.'
8414 * set arrow_used to true and stop it - causing to redo
8415 * characters entered in one mode (normal/reverse insert).
8416 */
8417 arrow_used = TRUE;
8418 (void)stop_arrow();
8419 p_fkmap = curwin->w_p_rl ^ p_ri;
8420 if (p_fkmap && p_ri)
8421 State = INSERT;
8422 }
8423 else
8424#endif
8425 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8426 showmode();
8427}
8428#endif
8429
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430/*
8431 * If 'keymodel' contains "startsel", may start selection.
8432 * Returns TRUE when a CTRL-O and other keys stuffed.
8433 */
8434 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008435ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
8437 if (km_startsel)
8438 switch (c)
8439 {
8440 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 case K_PAGEUP:
8443 case K_KPAGEUP:
8444 case K_PAGEDOWN:
8445 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008446# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 case K_LEFT:
8448 case K_RIGHT:
8449 case K_UP:
8450 case K_DOWN:
8451 case K_END:
8452 case K_HOME:
8453# endif
8454 if (!(mod_mask & MOD_MASK_SHIFT))
8455 break;
8456 /* FALLTHROUGH */
8457 case K_S_LEFT:
8458 case K_S_RIGHT:
8459 case K_S_UP:
8460 case K_S_DOWN:
8461 case K_S_END:
8462 case K_S_HOME:
8463 /* Start selection right away, the cursor can move with
8464 * CTRL-O when beyond the end of the line. */
8465 start_selection();
8466
8467 /* Execute the key in (insert) Select mode. */
8468 stuffcharReadbuff(Ctrl_O);
8469 if (mod_mask)
8470 {
8471 char_u buf[4];
8472
8473 buf[0] = K_SPECIAL;
8474 buf[1] = KS_MODIFIER;
8475 buf[2] = mod_mask;
8476 buf[3] = NUL;
8477 stuffReadbuff(buf);
8478 }
8479 stuffcharReadbuff(c);
8480 return TRUE;
8481 }
8482 return FALSE;
8483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484
8485/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008486 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008487 */
8488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008489ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008490{
8491#ifdef FEAT_FKMAP
8492 if (p_fkmap && p_ri)
8493 {
8494 beep_flush();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008495 emsg(farsi_text_3); /* encoded in Farsi */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008496 return;
8497 }
8498#endif
8499
Bram Moolenaar1e015462005-09-25 22:16:38 +00008500# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008501 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008502 (char_u *)((State & REPLACE_FLAG) ? "i"
8503 : replaceState == VREPLACE ? "v"
8504 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008505# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008506 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008507 if (State & REPLACE_FLAG)
8508 State = INSERT | (State & LANGMAP);
8509 else
8510 State = replaceState | (State & LANGMAP);
8511 AppendCharToRedobuff(K_INS);
8512 showmode();
8513#ifdef CURSOR_SHAPE
8514 ui_cursor_shape(); /* may show different cursor shape */
8515#endif
8516}
8517
8518/*
8519 * Pressed CTRL-O in Insert mode.
8520 */
8521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008522ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008523{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008524 if (State & VREPLACE_FLAG)
8525 restart_edit = 'V';
8526 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008527 if (State & REPLACE_FLAG)
8528 restart_edit = 'R';
8529 else
8530 restart_edit = 'I';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008531 if (virtual_active())
8532 ins_at_eol = FALSE; /* cursor always keeps its column */
8533 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008534 ins_at_eol = (gchar_cursor() == NUL);
8535}
8536
8537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 * If the cursor is on an indent, ^T/^D insert/delete one
8539 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008540 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 * with vi. But vi only supports ^T and ^D after an
8542 * autoindent, we support it everywhere.
8543 */
8544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008545ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 if (stop_arrow() == FAIL)
8548 return;
8549 AppendCharToRedobuff(c);
8550
8551 /*
8552 * 0^D and ^^D: remove all indent.
8553 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008554 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8555 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 {
8557 --curwin->w_cursor.col;
8558 (void)del_char(FALSE); /* delete the '^' or '0' */
8559 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8560 if (State & REPLACE_FLAG)
8561 replace_pop_ins();
8562 if (lastc == '^')
8563 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008564 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 }
8566 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008567 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568
8569 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8570 did_ai = FALSE;
8571#ifdef FEAT_SMARTINDENT
8572 did_si = FALSE;
8573 can_si = FALSE;
8574 can_si_back = FALSE;
8575#endif
8576#ifdef FEAT_CINDENT
8577 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8578#endif
8579}
8580
8581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008582ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
8584 int temp;
8585
8586 if (stop_arrow() == FAIL)
8587 return;
8588 if (gchar_cursor() == NUL) /* delete newline */
8589 {
8590 temp = curwin->w_cursor.col;
8591 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008592 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008593 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008597 /* Adjust orig_line_count in case more lines have been deleted than
8598 * have been added. That makes sure, that open_line() later
8599 * can access all buffer lines correctly */
8600 if (State & VREPLACE_FLAG &&
8601 orig_line_count > curbuf->b_ml.ml_line_count)
8602 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01008603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008605 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8606 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 did_ai = FALSE;
8608#ifdef FEAT_SMARTINDENT
8609 did_si = FALSE;
8610 can_si = FALSE;
8611 can_si_back = FALSE;
8612#endif
8613 AppendCharToRedobuff(K_DEL);
8614}
8615
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008616/*
8617 * Delete one character for ins_bs().
8618 */
8619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008620ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008621{
8622 dec_cursor();
8623 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8624 if (State & REPLACE_FLAG)
8625 {
8626 /* Don't delete characters before the insert point when in
8627 * Replace mode */
8628 if (curwin->w_cursor.lnum != Insstart.lnum
8629 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008630 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008631 }
8632 else
8633 (void)del_char(FALSE);
8634}
8635
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636/*
8637 * Handle Backspace, delete-word and delete-line in Insert mode.
8638 * Return TRUE when backspace was actually used.
8639 */
8640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008641ins_bs(
8642 int c,
8643 int mode,
8644 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 linenr_T lnum;
8647 int cc;
8648 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008649 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 colnr_T mincol;
8651 int did_backspace = FALSE;
8652 int in_indent;
8653 int oldState;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008654 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655
8656 /*
8657 * can't delete anything in an empty file
8658 * can't backup past first character in buffer
8659 * can't backup past starting point unless 'backspace' > 1
8660 * can backup to a previous line if 'backspace' == 0
8661 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008662 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 || (
8664#ifdef FEAT_RIGHTLEFT
8665 !revins_on &&
8666#endif
8667 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8668 || (!can_bs(BS_START)
8669 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008670 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8671 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8673 && curwin->w_cursor.col <= ai_col)
8674 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8675 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008676 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 return FALSE;
8678 }
8679
8680 if (stop_arrow() == FAIL)
8681 return FALSE;
8682 in_indent = inindent(0);
8683#ifdef FEAT_CINDENT
8684 if (in_indent)
8685 can_cindent = FALSE;
8686#endif
8687#ifdef FEAT_COMMENTS
8688 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8689#endif
8690#ifdef FEAT_RIGHTLEFT
8691 if (revins_on) /* put cursor after last inserted char */
8692 inc_cursor();
8693#endif
8694
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 /* Virtualedit:
8696 * BACKSPACE_CHAR eats a virtual space
8697 * BACKSPACE_WORD eats all coladd
8698 * BACKSPACE_LINE eats all coladd and keeps going
8699 */
8700 if (curwin->w_cursor.coladd > 0)
8701 {
8702 if (mode == BACKSPACE_CHAR)
8703 {
8704 --curwin->w_cursor.coladd;
8705 return TRUE;
8706 }
8707 if (mode == BACKSPACE_WORD)
8708 {
8709 curwin->w_cursor.coladd = 0;
8710 return TRUE;
8711 }
8712 curwin->w_cursor.coladd = 0;
8713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
8715 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02008716 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 */
8718 if (curwin->w_cursor.col == 0)
8719 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008720 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008721 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722#ifdef FEAT_RIGHTLEFT
8723 || revins_on
8724#endif
8725 )
8726 {
8727 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8728 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8729 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008730 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02008731 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 }
8733 /*
8734 * In replace mode:
8735 * cc < 0: NL was inserted, delete it
8736 * cc >= 0: NL was replaced, put original characters back
8737 */
8738 cc = -1;
8739 if (State & REPLACE_FLAG)
8740 cc = replace_pop(); /* returns -1 if NL was inserted */
8741 /*
8742 * In replace mode, in the line we started replacing, we only move the
8743 * cursor.
8744 */
8745 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8746 {
8747 dec_cursor();
8748 }
8749 else
8750 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 if (!(State & VREPLACE_FLAG)
8752 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {
8754 temp = gchar_cursor(); /* remember current char */
8755 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008756
8757 /* When "aw" is in 'formatoptions' we must delete the space at
8758 * the end of the line, otherwise the line will be broken
8759 * again when auto-formatting. */
8760 if (has_format_option(FO_AUTO)
8761 && has_format_option(FO_WHITE_PAR))
8762 {
8763 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8764 TRUE);
8765 int len;
8766
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008767 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008768 if (len > 0 && ptr[len - 1] == ' ')
8769 ptr[len - 1] = NUL;
8770 }
8771
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008772 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 if (temp == NUL && gchar_cursor() != NUL)
8774 inc_cursor();
8775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 else
8777 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778
8779 /*
8780 * In REPLACE mode we have to put back the text that was replaced
8781 * by the NL. On the replace stack is first a NUL-terminated
8782 * sequence of characters that were deleted and then the
8783 * characters that NL replaced.
8784 */
8785 if (State & REPLACE_FLAG)
8786 {
8787 /*
8788 * Do the next ins_char() in NORMAL state, to
8789 * prevent ins_char() from replacing characters and
8790 * avoiding showmatch().
8791 */
8792 oldState = State;
8793 State = NORMAL;
8794 /*
8795 * restore characters (blanks) deleted after cursor
8796 */
8797 while (cc > 0)
8798 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008799 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 mb_replace_pop_ins(cc);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008801 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 cc = replace_pop();
8803 }
8804 /* restore the characters that NL replaced */
8805 replace_pop_ins();
8806 State = oldState;
8807 }
8808 }
8809 did_ai = FALSE;
8810 }
8811 else
8812 {
8813 /*
8814 * Delete character(s) before the cursor.
8815 */
8816#ifdef FEAT_RIGHTLEFT
8817 if (revins_on) /* put cursor on last inserted char */
8818 dec_cursor();
8819#endif
8820 mincol = 0;
8821 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008822 if (mode == BACKSPACE_LINE
8823 && (curbuf->b_p_ai
8824#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008825 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008826#endif
8827 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828#ifdef FEAT_RIGHTLEFT
8829 && !revins_on
8830#endif
8831 )
8832 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008833 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008835 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008837 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 }
8839
8840 /*
8841 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8842 */
8843 if ( mode == BACKSPACE_CHAR
8844 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008845 || ((get_sts_value() != 0
8846#ifdef FEAT_VARTABS
8847 || tabstop_count(curbuf->b_p_vsts_array)
8848#endif
8849 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008850 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 && (*(ml_get_cursor() - 1) == TAB
8852 || (*(ml_get_cursor() - 1) == ' '
8853 && (!*inserted_space_p
8854 || arrow_used))))))
8855 {
8856 int ts;
8857 colnr_T vcol;
8858 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008859 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860
8861 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 /* Compute the virtual column where we want to be. Since
8863 * 'showbreak' may get in the way, need to get the last column of
8864 * the previous character. */
8865 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008866 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 dec_cursor();
8868 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8869 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008870#ifdef FEAT_VARTABS
8871 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02008872 {
8873 ts = (int)get_sw_value(curbuf);
8874 want_vcol = (want_vcol / ts) * ts;
8875 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008876 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02008877 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008878 curbuf->b_p_vsts_array);
8879#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02008880 if (p_sta && in_indent)
8881 ts = (int)get_sw_value(curbuf);
8882 else
8883 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
8887 /* delete characters until we are at or before want_vcol */
8888 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01008889 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008890 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
8892 /* insert extra spaces until we are at want_vcol */
8893 while (vcol < want_vcol)
8894 {
8895 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008896 if (curwin->w_cursor.lnum == Insstart_orig.lnum
8897 && curwin->w_cursor.col < Insstart_orig.col)
8898 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 if (State & VREPLACE_FLAG)
8901 ins_char(' ');
8902 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 {
8904 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008905 if ((State & REPLACE_FLAG))
8906 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 }
8908 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8909 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008910
8911 /* If we are now back where we started delete one character. Can
8912 * happen when using 'sts' and 'linebreak'. */
8913 if (vcol >= start_vcol)
8914 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 }
8916
8917 /*
8918 * Delete upto starting point, start of line or previous word.
8919 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008922 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008924 if (has_mbyte)
8925 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008926 do
8927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008929 if (!revins_on) /* put cursor on char to be deleted */
8930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008932
8933 cc = gchar_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008934 /* look multi-byte character class */
8935 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008937 prev_cclass = cclass;
8938 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 }
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008940
8941 /* start of word? */
8942 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
8943 {
8944 mode = BACKSPACE_WORD_NOT_SPACE;
8945 temp = vim_iswordc(cc);
8946 }
8947 /* end of word? */
8948 else if (mode == BACKSPACE_WORD_NOT_SPACE
8949 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
Bram Moolenaar13505972019-01-24 15:04:48 +01008950 || prev_cclass != cclass))
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008953 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008955 inc_cursor();
8956#ifdef FEAT_RIGHTLEFT
8957 else if (State & REPLACE_FLAG)
8958 dec_cursor();
8959#endif
8960 break;
8961 }
8962 if (State & REPLACE_FLAG)
8963 replace_do_bs(-1);
8964 else
8965 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008966 if (enc_utf8 && p_deco)
8967 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008968 (void)del_char(FALSE);
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008969 /*
8970 * If there are combining characters and 'delcombine' is set
8971 * move the cursor back. Don't back up before the base
8972 * character.
8973 */
8974 if (enc_utf8 && p_deco && cpc[0] != NUL)
8975 inc_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01008976#ifdef FEAT_RIGHTLEFT
8977 if (revins_chars)
8978 {
8979 revins_chars--;
8980 revins_legal++;
8981 }
8982 if (revins_on && gchar_cursor() == NUL)
8983 break;
8984#endif
8985 }
8986 /* Just a single backspace?: */
8987 if (mode == BACKSPACE_CHAR)
8988 break;
8989 } while (
8990#ifdef FEAT_RIGHTLEFT
8991 revins_on ||
8992#endif
8993 (curwin->w_cursor.col > mincol
8994 && (curwin->w_cursor.lnum != Insstart_orig.lnum
8995 || curwin->w_cursor.col != Insstart_orig.col)));
8996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 did_backspace = TRUE;
8998 }
8999#ifdef FEAT_SMARTINDENT
9000 did_si = FALSE;
9001 can_si = FALSE;
9002 can_si_back = FALSE;
9003#endif
9004 if (curwin->w_cursor.col <= 1)
9005 did_ai = FALSE;
9006 /*
9007 * It's a little strange to put backspaces into the redo
9008 * buffer, but it makes auto-indent a lot easier to deal
9009 * with.
9010 */
9011 AppendCharToRedobuff(c);
9012
9013 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009014 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009015 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009016 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017
9018 /* vi behaviour: the cursor moves backward but the character that
9019 * was there remains visible
9020 * Vim behaviour: the cursor moves backward and the character that
9021 * was there is erased from the screen.
9022 * We can emulate the vi behaviour by pretending there is a dollar
9023 * displayed even when there isn't.
9024 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009025 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 dollar_vcol = curwin->w_virtcol;
9027
Bram Moolenaarce3be472008-01-14 19:12:28 +00009028#ifdef FEAT_FOLDING
9029 /* When deleting a char the cursor line must never be in a closed fold.
9030 * E.g., when 'foldmethod' is indent and deleting the first non-white
9031 * char before a Tab. */
9032 if (did_backspace)
9033 foldOpenCursor();
9034#endif
9035
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 return did_backspace;
9037}
9038
9039#ifdef FEAT_MOUSE
9040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009041ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
9043 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009044 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045
9046# ifdef FEAT_GUI
9047 /* When GUI is active, also move/paste when 'mouse' is empty */
9048 if (!gui.in_use)
9049# endif
9050 if (!mouse_has(MOUSE_INSERT))
9051 return;
9052
9053 undisplay_dollar();
9054 tpos = curwin->w_cursor;
9055 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9056 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009057 win_T *new_curwin = curwin;
9058
9059 if (curwin != old_curwin && win_valid(old_curwin))
9060 {
9061 /* Mouse took us to another window. We need to go back to the
9062 * previous one to stop insert there properly. */
9063 curwin = old_curwin;
9064 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009065#ifdef FEAT_JOB_CHANNEL
9066 if (bt_prompt(curbuf))
9067 // Restart Insert mode when re-entering the prompt buffer.
9068 curbuf->b_prompt_insert = 'A';
9069#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009070 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009071 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009072 if (curwin != new_curwin && win_valid(new_curwin))
9073 {
9074 curwin = new_curwin;
9075 curbuf = curwin->w_buffer;
9076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077# ifdef FEAT_CINDENT
9078 can_cindent = TRUE;
9079# endif
9080 }
9081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 /* redraw status lines (in case another window became active) */
9083 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084}
9085
9086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009087ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
9089 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009090 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009091# ifdef FEAT_INS_EXPAND
9092 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093# endif
9094
9095 tpos = curwin->w_cursor;
9096
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009097 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 {
9099 int row, col;
9100
9101 row = mouse_row;
9102 col = mouse_col;
9103
9104 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009105 wp = mouse_find_win(&row, &col);
9106 if (wp == NULL)
9107 return;
9108 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 curbuf = curwin->w_buffer;
9110 }
9111 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 undisplay_dollar();
9113
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009114# ifdef FEAT_INS_EXPAND
9115 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009116 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009117# endif
9118 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009119 if (dir == MSCR_DOWN || dir == MSCR_UP)
9120 {
9121 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9122 scroll_redraw(dir,
9123 (long)(curwin->w_botline - curwin->w_topline));
9124 else
9125 scroll_redraw(dir, 3L);
9126 }
9127#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009128 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009129 {
9130 int val, step = 6;
9131
9132 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009133 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009134 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9135 if (val < 0)
9136 val = 0;
9137 gui_do_horiz_scroll(val, TRUE);
9138 }
9139#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009140# ifdef FEAT_INS_EXPAND
9141 did_scroll = TRUE;
9142# endif
9143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 curwin->w_redr_status = TRUE;
9146
9147 curwin = old_curwin;
9148 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009150# ifdef FEAT_INS_EXPAND
9151 /* The popup menu may overlay the window, need to redraw it.
9152 * TODO: Would be more efficient to only redraw the windows that are
9153 * overlapped by the popup menu. */
9154 if (pum_visible() && did_scroll)
9155 {
9156 redraw_all_later(NOT_VALID);
9157 ins_compl_show_pum();
9158 }
9159# endif
9160
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009161 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 {
9163 start_arrow(&tpos);
9164# ifdef FEAT_CINDENT
9165 can_cindent = TRUE;
9166# endif
9167 }
9168}
9169#endif
9170
Bram Moolenaarec2da362017-01-21 20:04:22 +01009171/*
9172 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9173 * P_PE literally.
9174 * When "drop" is TRUE then consume the text and drop it.
9175 */
9176 int
9177bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9178{
9179 int c;
9180 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9181 int idx = 0;
9182 char_u *end = find_termcode((char_u *)"PE");
9183 int ret_char = -1;
9184 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009185 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009186
9187 /* If the end code is too long we can't detect it, read everything. */
9188 if (STRLEN(end) >= NUMBUFLEN)
9189 end = NULL;
9190 ++no_mapping;
9191 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009192 if (!p_paste)
9193 // Also have the side effects of setting 'paste' to make it work much
9194 // faster.
9195 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009196
Bram Moolenaarec2da362017-01-21 20:04:22 +01009197 for (;;)
9198 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009199 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009200 if (end == NULL && vpeekc() == NUL)
9201 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009202 do
9203 {
9204 c = vgetc();
9205 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9206 if (c == NUL || got_int)
9207 // When CTRL-C was encountered the typeahead will be flushed and we
9208 // won't get the end sequence.
9209 break;
9210
Bram Moolenaarec2da362017-01-21 20:04:22 +01009211 if (has_mbyte)
9212 idx += (*mb_char2bytes)(c, buf + idx);
9213 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009214 buf[idx++] = c;
9215 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009216 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009217 {
9218 if (end[idx] == NUL)
9219 break; /* Found the end of paste code. */
9220 continue;
9221 }
9222 if (!drop)
9223 {
9224 switch (mode)
9225 {
9226 case PASTE_CMDLINE:
9227 put_on_cmdline(buf, idx, TRUE);
9228 break;
9229
9230 case PASTE_EX:
9231 if (gap != NULL && ga_grow(gap, idx) == OK)
9232 {
9233 mch_memmove((char *)gap->ga_data + gap->ga_len,
9234 buf, (size_t)idx);
9235 gap->ga_len += idx;
9236 }
9237 break;
9238
9239 case PASTE_INSERT:
9240 if (stop_arrow() == OK)
9241 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009242 c = buf[0];
9243 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9244 ins_eol(c);
9245 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009246 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009247 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009248 AppendToRedobuffLit(buf, idx);
9249 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009250 }
9251 break;
9252
9253 case PASTE_ONE_CHAR:
9254 if (ret_char == -1)
9255 {
Bram Moolenaarec2da362017-01-21 20:04:22 +01009256 if (has_mbyte)
9257 ret_char = (*mb_ptr2char)(buf);
9258 else
Bram Moolenaarec2da362017-01-21 20:04:22 +01009259 ret_char = buf[0];
9260 }
9261 break;
9262 }
9263 }
9264 idx = 0;
9265 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009266
Bram Moolenaarec2da362017-01-21 20:04:22 +01009267 --no_mapping;
9268 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009269 if (!save_paste)
9270 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009271
9272 return ret_char;
9273}
9274
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009275#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009277ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009278{
9279 /* We will be leaving the current window, unless closing another tab. */
9280 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9281 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9282 {
9283 undisplay_dollar();
9284 start_arrow(&curwin->w_cursor);
9285# ifdef FEAT_CINDENT
9286 can_cindent = TRUE;
9287# endif
9288 }
9289
9290 if (c == K_TABLINE)
9291 goto_tabpage(current_tab);
9292 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009293 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009294 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009295 redraw_statuslines(); /* will redraw the tabline when needed */
9296 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009297}
9298#endif
9299
9300#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009302ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304 pos_T tpos;
9305
9306 undisplay_dollar();
9307 tpos = curwin->w_cursor;
9308 if (gui_do_scroll())
9309 {
9310 start_arrow(&tpos);
9311# ifdef FEAT_CINDENT
9312 can_cindent = TRUE;
9313# endif
9314 }
9315}
9316
9317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009318ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
9320 pos_T tpos;
9321
9322 undisplay_dollar();
9323 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009324 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 {
9326 start_arrow(&tpos);
9327# ifdef FEAT_CINDENT
9328 can_cindent = TRUE;
9329# endif
9330 }
9331}
9332#endif
9333
9334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009335ins_left(
9336 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338 pos_T tpos;
9339
9340#ifdef FEAT_FOLDING
9341 if ((fdo_flags & FDO_HOR) && KeyTyped)
9342 foldOpenCursor();
9343#endif
9344 undisplay_dollar();
9345 tpos = curwin->w_cursor;
9346 if (oneleft() == OK)
9347 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009348#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9349 /* Only call start_arrow() when not busy with preediting, it will
9350 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009351 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009352#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009353 {
9354 start_arrow_with_change(&tpos, end_change);
9355 if (!end_change)
9356 AppendCharToRedobuff(K_LEFT);
9357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358#ifdef FEAT_RIGHTLEFT
9359 /* If exit reversed string, position is fixed */
9360 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9361 revins_legal++;
9362 revins_chars++;
9363#endif
9364 }
9365
9366 /*
9367 * if 'whichwrap' set for cursor in insert mode may go to
9368 * previous line
9369 */
9370 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9371 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009372 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 start_arrow(&tpos);
9374 --(curwin->w_cursor.lnum);
9375 coladvance((colnr_T)MAXCOL);
9376 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9377 }
9378 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009379 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009380 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381}
9382
9383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009384ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 pos_T tpos;
9387
9388#ifdef FEAT_FOLDING
9389 if ((fdo_flags & FDO_HOR) && KeyTyped)
9390 foldOpenCursor();
9391#endif
9392 undisplay_dollar();
9393 tpos = curwin->w_cursor;
9394 if (c == K_C_HOME)
9395 curwin->w_cursor.lnum = 1;
9396 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 curwin->w_curswant = 0;
9399 start_arrow(&tpos);
9400}
9401
9402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009403ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404{
9405 pos_T tpos;
9406
9407#ifdef FEAT_FOLDING
9408 if ((fdo_flags & FDO_HOR) && KeyTyped)
9409 foldOpenCursor();
9410#endif
9411 undisplay_dollar();
9412 tpos = curwin->w_cursor;
9413 if (c == K_C_END)
9414 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9415 coladvance((colnr_T)MAXCOL);
9416 curwin->w_curswant = MAXCOL;
9417
9418 start_arrow(&tpos);
9419}
9420
9421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009422ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424#ifdef FEAT_FOLDING
9425 if ((fdo_flags & FDO_HOR) && KeyTyped)
9426 foldOpenCursor();
9427#endif
9428 undisplay_dollar();
9429 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9430 {
9431 start_arrow(&curwin->w_cursor);
9432 (void)bck_word(1L, FALSE, FALSE);
9433 curwin->w_set_curswant = TRUE;
9434 }
9435 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009436 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437}
9438
9439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009440ins_right(
9441 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
9443#ifdef FEAT_FOLDING
9444 if ((fdo_flags & FDO_HOR) && KeyTyped)
9445 foldOpenCursor();
9446#endif
9447 undisplay_dollar();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009448 if (gchar_cursor() != NUL || virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009450 start_arrow_with_change(&curwin->w_cursor, end_change);
9451 if (!end_change)
9452 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 if (virtual_active())
9455 oneright();
9456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009459 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 ++curwin->w_cursor.col;
9462 }
9463
9464#ifdef FEAT_RIGHTLEFT
9465 revins_legal++;
9466 if (revins_chars)
9467 revins_chars--;
9468#endif
9469 }
9470 /* if 'whichwrap' set for cursor in insert mode, may move the
9471 * cursor to the next line */
9472 else if (vim_strchr(p_ww, ']') != NULL
9473 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9474 {
9475 start_arrow(&curwin->w_cursor);
9476 curwin->w_set_curswant = TRUE;
9477 ++curwin->w_cursor.lnum;
9478 curwin->w_cursor.col = 0;
9479 }
9480 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009481 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009482 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484
9485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009486ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487{
9488#ifdef FEAT_FOLDING
9489 if ((fdo_flags & FDO_HOR) && KeyTyped)
9490 foldOpenCursor();
9491#endif
9492 undisplay_dollar();
9493 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9494 || gchar_cursor() != NUL)
9495 {
9496 start_arrow(&curwin->w_cursor);
9497 (void)fwd_word(1L, FALSE, 0);
9498 curwin->w_set_curswant = TRUE;
9499 }
9500 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009501 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502}
9503
9504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009505ins_up(
9506 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507{
9508 pos_T tpos;
9509 linenr_T old_topline = curwin->w_topline;
9510#ifdef FEAT_DIFF
9511 int old_topfill = curwin->w_topfill;
9512#endif
9513
9514 undisplay_dollar();
9515 tpos = curwin->w_cursor;
9516 if (cursor_up(1L, TRUE) == OK)
9517 {
9518 if (startcol)
9519 coladvance(getvcol_nolist(&Insstart));
9520 if (old_topline != curwin->w_topline
9521#ifdef FEAT_DIFF
9522 || old_topfill != curwin->w_topfill
9523#endif
9524 )
9525 redraw_later(VALID);
9526 start_arrow(&tpos);
9527#ifdef FEAT_CINDENT
9528 can_cindent = TRUE;
9529#endif
9530 }
9531 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009532 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533}
9534
9535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538 pos_T tpos;
9539
9540 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009541
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009542 if (mod_mask & MOD_MASK_CTRL)
9543 {
9544 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009545 if (first_tabpage->tp_next != NULL)
9546 {
9547 start_arrow(&curwin->w_cursor);
9548 goto_tabpage(-1);
9549 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009550 return;
9551 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009552
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 tpos = curwin->w_cursor;
9554 if (onepage(BACKWARD, 1L) == OK)
9555 {
9556 start_arrow(&tpos);
9557#ifdef FEAT_CINDENT
9558 can_cindent = TRUE;
9559#endif
9560 }
9561 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009562 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563}
9564
9565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009566ins_down(
9567 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 pos_T tpos;
9570 linenr_T old_topline = curwin->w_topline;
9571#ifdef FEAT_DIFF
9572 int old_topfill = curwin->w_topfill;
9573#endif
9574
9575 undisplay_dollar();
9576 tpos = curwin->w_cursor;
9577 if (cursor_down(1L, TRUE) == OK)
9578 {
9579 if (startcol)
9580 coladvance(getvcol_nolist(&Insstart));
9581 if (old_topline != curwin->w_topline
9582#ifdef FEAT_DIFF
9583 || old_topfill != curwin->w_topfill
9584#endif
9585 )
9586 redraw_later(VALID);
9587 start_arrow(&tpos);
9588#ifdef FEAT_CINDENT
9589 can_cindent = TRUE;
9590#endif
9591 }
9592 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009593 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594}
9595
9596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009597ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
9599 pos_T tpos;
9600
9601 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009602
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009603 if (mod_mask & MOD_MASK_CTRL)
9604 {
9605 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009606 if (first_tabpage->tp_next != NULL)
9607 {
9608 start_arrow(&curwin->w_cursor);
9609 goto_tabpage(0);
9610 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009611 return;
9612 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009613
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 tpos = curwin->w_cursor;
9615 if (onepage(FORWARD, 1L) == OK)
9616 {
9617 start_arrow(&tpos);
9618#ifdef FEAT_CINDENT
9619 can_cindent = TRUE;
9620#endif
9621 }
9622 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009623 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624}
9625
9626#ifdef FEAT_DND
9627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009628ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629{
9630 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9631}
9632#endif
9633
9634/*
9635 * Handle TAB in Insert or Replace mode.
9636 * Return TRUE when the TAB needs to be inserted like a normal character.
9637 */
9638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009639ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640{
9641 int ind;
9642 int i;
9643 int temp;
9644
9645 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9646 Insstart_blank_vcol = get_nolist_virtcol();
9647 if (echeck_abbr(TAB + ABBR_OFF))
9648 return FALSE;
9649
9650 ind = inindent(0);
9651#ifdef FEAT_CINDENT
9652 if (ind)
9653 can_cindent = FALSE;
9654#endif
9655
9656 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009657 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 */
9659 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009660#ifdef FEAT_VARTABS
9661 && !(p_sta && ind
9662 /* These five lines mean 'tabstop' != 'shiftwidth' */
9663 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
9664 || (tabstop_count(curbuf->b_p_vts_array) == 1
9665 && tabstop_first(curbuf->b_p_vts_array)
9666 != get_sw_value(curbuf))
9667 || (tabstop_count(curbuf->b_p_vts_array) == 0
9668 && curbuf->b_p_ts != get_sw_value(curbuf))))
9669 && tabstop_count(curbuf->b_p_vsts_array) == 0
9670#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009671 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009672#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009673 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 return TRUE;
9675
9676 if (stop_arrow() == FAIL)
9677 return TRUE;
9678
9679 did_ai = FALSE;
9680#ifdef FEAT_SMARTINDENT
9681 did_si = FALSE;
9682 can_si = FALSE;
9683 can_si_back = FALSE;
9684#endif
9685 AppendToRedobuff((char_u *)"\t");
9686
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009687#ifdef FEAT_VARTABS
9688 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9689 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009690 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009691 temp -= get_nolist_virtcol() % temp;
9692 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009693 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009694 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009695 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009696 curbuf->b_p_vsts_array);
9697 else /* otherwise use 'tabstop' */
9698 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
9699 curbuf->b_p_vts_array);
9700#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009702 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009703 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9704 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 else /* otherwise use 'tabstop' */
9706 temp = (int)curbuf->b_p_ts;
9707 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709
9710 /*
9711 * Insert the first space with ins_char(). It will delete one char in
9712 * replace mode. Insert the rest with ins_str(); it will not delete any
9713 * chars. For VREPLACE mode, we use ins_char() for all characters.
9714 */
9715 ins_char(' ');
9716 while (--temp > 0)
9717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 if (State & VREPLACE_FLAG)
9719 ins_char(' ');
9720 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 {
9722 ins_str((char_u *)" ");
9723 if (State & REPLACE_FLAG) /* no char replaced */
9724 replace_push(NUL);
9725 }
9726 }
9727
9728 /*
9729 * When 'expandtab' not set: Replace spaces by TABs where possible.
9730 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009731#ifdef FEAT_VARTABS
9732 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
9733 || get_sts_value() > 0
9734 || (p_sta && ind)))
9735#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009736 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 {
9739 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 char_u *saved_line = NULL; /* init for GCC */
9741 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 pos_T fpos;
9743 pos_T *cursor;
9744 colnr_T want_vcol, vcol;
9745 int change_col = -1;
9746 int save_list = curwin->w_p_list;
9747
9748 /*
9749 * Get the current line. For VREPLACE mode, don't make real changes
9750 * yet, just work on a copy of the line.
9751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (State & VREPLACE_FLAG)
9753 {
9754 pos = curwin->w_cursor;
9755 cursor = &pos;
9756 saved_line = vim_strsave(ml_get_curline());
9757 if (saved_line == NULL)
9758 return FALSE;
9759 ptr = saved_line + pos.col;
9760 }
9761 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 {
9763 ptr = ml_get_cursor();
9764 cursor = &curwin->w_cursor;
9765 }
9766
9767 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9768 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9769 curwin->w_p_list = FALSE;
9770
9771 /* Find first white before the cursor */
9772 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01009773 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 {
9775 --fpos.col;
9776 --ptr;
9777 }
9778
9779 /* In Replace mode, don't change characters before the insert point. */
9780 if ((State & REPLACE_FLAG)
9781 && fpos.lnum == Insstart.lnum
9782 && fpos.col < Insstart.col)
9783 {
9784 ptr += Insstart.col - fpos.col;
9785 fpos.col = Insstart.col;
9786 }
9787
9788 /* compute virtual column numbers of first white and cursor */
9789 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9790 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9791
Bram Moolenaar597a4222014-06-25 14:39:50 +02009792 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9793 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01009794 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009796 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 if (vcol + i > want_vcol)
9798 break;
9799 if (*ptr != TAB)
9800 {
9801 *ptr = TAB;
9802 if (change_col < 0)
9803 {
9804 change_col = fpos.col; /* Column of first change */
9805 /* May have to adjust Insstart */
9806 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9807 Insstart.col = fpos.col;
9808 }
9809 }
9810 ++fpos.col;
9811 ++ptr;
9812 vcol += i;
9813 }
9814
9815 if (change_col >= 0)
9816 {
9817 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009818 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819
9820 /* Skip over the spaces we need. */
9821 while (vcol < want_vcol && *ptr == ' ')
9822 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009823 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 ++ptr;
9825 ++repl_off;
9826 }
9827 if (vcol > want_vcol)
9828 {
9829 /* Must have a char with 'showbreak' just before it. */
9830 --ptr;
9831 --repl_off;
9832 }
9833 fpos.col += repl_off;
9834
9835 /* Delete following spaces. */
9836 i = cursor->col - fpos.col;
9837 if (i > 0)
9838 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009839 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02009841 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 for (temp = i; --temp >= 0; )
9843 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01009844#ifdef FEAT_TEXT_PROP
9845 curbuf->b_ml.ml_line_len -= i;
9846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009848#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009849 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009850 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009851 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009852 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9853 (char_u *)"\t", 1);
9854 }
9855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 cursor->col -= i;
9857
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 /*
9859 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9860 * backspacing over the changed spacing and then inserting the new
9861 * spacing.
9862 */
9863 if (State & VREPLACE_FLAG)
9864 {
9865 /* Backspace from real cursor to change_col */
9866 backspace_until_column(change_col);
9867
9868 /* Insert each char in saved_line from changed_col to
9869 * ptr-cursor */
9870 ins_bytes_len(saved_line + change_col,
9871 cursor->col - change_col);
9872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 }
9874
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 if (State & VREPLACE_FLAG)
9876 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 curwin->w_p_list = save_list;
9878 }
9879
9880 return FALSE;
9881}
9882
9883/*
9884 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +02009885 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 */
9887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009888ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
9890 int i;
9891
9892 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02009893 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +02009895 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 undisplay_dollar();
9897
9898 /*
9899 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9900 * character under the cursor. Only push a NUL on the replace stack,
9901 * nothing to put back when the NL is deleted.
9902 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02009903 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 replace_push(NUL);
9905
9906 /*
9907 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9908 * replacing the next line, so we push all of the characters left on the
9909 * line onto the replace stack. This is not done here though, it is done
9910 * in open_line().
9911 */
9912
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009913 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9914 * CTRL-O). */
9915 if (virtual_active() && curwin->w_cursor.coladd > 0)
9916 coladvance(getviscol());
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009917
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918#ifdef FEAT_RIGHTLEFT
9919# ifdef FEAT_FKMAP
9920 if (p_altkeymap && p_fkmap)
9921 fkmap(NL);
9922# endif
9923 /* NL in reverse insert will always start in the end of
9924 * current line. */
9925 if (revins_on)
9926 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9927#endif
9928
9929 AppendToRedobuff(NL_STR);
9930 i = open_line(FORWARD,
9931#ifdef FEAT_COMMENTS
9932 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9933#endif
9934 0, old_indent);
9935 old_indent = 0;
9936#ifdef FEAT_CINDENT
9937 can_cindent = TRUE;
9938#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009939#ifdef FEAT_FOLDING
9940 /* When inserting a line the cursor line must never be in a closed fold. */
9941 foldOpenCursor();
9942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
Bram Moolenaar24a2d722018-04-24 19:36:43 +02009944 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945}
9946
9947#ifdef FEAT_DIGRAPHS
9948/*
9949 * Handle digraph in insert mode.
9950 * Returns character still to be inserted, or NUL when nothing remaining to be
9951 * done.
9952 */
9953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009954ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955{
9956 int c;
9957 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009958 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959
9960 pc_status = PC_STATUS_UNSET;
9961 if (redrawing() && !char_avail())
9962 {
9963 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009964 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
9966 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009967 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968#ifdef FEAT_CMDL_INFO
9969 add_to_showcmd_c(Ctrl_K);
9970#endif
9971 }
9972
9973#ifdef USE_ON_FLY_SCROLL
9974 dont_scroll = TRUE; /* disallow scrolling here */
9975#endif
9976
9977 /* don't map the digraph chars. This also prevents the
9978 * mode message to be deleted when ESC is hit */
9979 ++no_mapping;
9980 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009981 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 --no_mapping;
9983 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009984 if (did_putchar)
9985 /* when the line fits in 'columns' the '?' is at the start of the next
9986 * line and will not be removed by the redraw */
9987 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009988
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 if (IS_SPECIAL(c) || mod_mask) /* special key */
9990 {
9991#ifdef FEAT_CMDL_INFO
9992 clear_showcmd();
9993#endif
9994 insert_special(c, TRUE, FALSE);
9995 return NUL;
9996 }
9997 if (c != ESC)
9998 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009999 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 if (redrawing() && !char_avail())
10001 {
10002 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010003 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004
10005 if (char2cells(c) == 1)
10006 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010007 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010009 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 }
10011#ifdef FEAT_CMDL_INFO
10012 add_to_showcmd_c(c);
10013#endif
10014 }
10015 ++no_mapping;
10016 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010017 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 --no_mapping;
10019 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010020 if (did_putchar)
10021 /* when the line fits in 'columns' the '?' is at the start of the
10022 * next line and will not be removed by a redraw */
10023 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 if (cc != ESC)
10025 {
10026 AppendToRedobuff((char_u *)CTRL_V_STR);
10027 c = getdigraph(c, cc, TRUE);
10028#ifdef FEAT_CMDL_INFO
10029 clear_showcmd();
10030#endif
10031 return c;
10032 }
10033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034#ifdef FEAT_CMDL_INFO
10035 clear_showcmd();
10036#endif
10037 return NUL;
10038}
10039#endif
10040
10041/*
10042 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10043 * Returns the char to be inserted, or NUL if none found.
10044 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010045 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010046ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047{
10048 int c;
10049 int temp;
10050 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010051 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052
10053 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10054 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010055 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 return NUL;
10057 }
10058
10059 /* try to advance to the cursor column */
10060 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010061 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 prev_ptr = ptr;
10063 validate_virtcol();
10064 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10065 {
10066 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010067 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 }
10069 if ((colnr_T)temp > curwin->w_virtcol)
10070 ptr = prev_ptr;
10071
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 c = (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010074 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 return c;
10076}
10077
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010078/*
10079 * CTRL-Y or CTRL-E typed in Insert mode.
10080 */
10081 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010082ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010083{
10084 int c = tc;
10085
10086#ifdef FEAT_INS_EXPAND
10087 if (ctrl_x_mode == CTRL_X_SCROLL)
10088 {
10089 if (c == Ctrl_Y)
10090 scrolldown_clamp();
10091 else
10092 scrollup_clamp();
10093 redraw_later(VALID);
10094 }
10095 else
10096#endif
10097 {
10098 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10099 if (c != NUL)
10100 {
10101 long tw_save;
10102
10103 /* The character must be taken literally, insert like it
10104 * was typed after a CTRL-V, and pretend 'textwidth'
10105 * wasn't set. Digits, 'o' and 'x' are special after a
10106 * CTRL-V, don't use it for these. */
10107 if (c < 256 && !isalnum(c))
10108 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10109 tw_save = curbuf->b_p_tw;
10110 curbuf->b_p_tw = -1;
10111 insert_special(c, TRUE, FALSE);
10112 curbuf->b_p_tw = tw_save;
10113#ifdef FEAT_RIGHTLEFT
10114 revins_chars++;
10115 revins_legal++;
10116#endif
10117 c = Ctrl_V; /* pretend CTRL-V is last character */
10118 auto_format(FALSE, TRUE);
10119 }
10120 }
10121 return c;
10122}
10123
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124#ifdef FEAT_SMARTINDENT
10125/*
10126 * Try to do some very smart auto-indenting.
10127 * Used when inserting a "normal" character.
10128 */
10129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010130ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131{
10132 pos_T *pos, old_pos;
10133 char_u *ptr;
10134 int i;
10135 int temp;
10136
10137 /*
10138 * do some very smart indenting when entering '{' or '}'
10139 */
10140 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10141 {
10142 /*
10143 * for '}' set indent equal to indent of line containing matching '{'
10144 */
10145 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10146 {
10147 old_pos = curwin->w_cursor;
10148 /*
10149 * If the matching '{' has a ')' immediately before it (ignoring
10150 * white-space), then line up with the start of the line
10151 * containing the matching '(' if there is one. This handles the
10152 * case where an "if (..\n..) {" statement continues over multiple
10153 * lines -- webb
10154 */
10155 ptr = ml_get(pos->lnum);
10156 i = pos->col;
10157 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010158 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 ;
10160 curwin->w_cursor.lnum = pos->lnum;
10161 curwin->w_cursor.col = i;
10162 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10163 curwin->w_cursor = *pos;
10164 i = get_indent();
10165 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010167 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 (void)set_indent(i, SIN_CHANGED);
10170 }
10171 else if (curwin->w_cursor.col > 0)
10172 {
10173 /*
10174 * when inserting '{' after "O" reduce indent, but not
10175 * more than indent of previous line
10176 */
10177 temp = TRUE;
10178 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10179 {
10180 old_pos = curwin->w_cursor;
10181 i = get_indent();
10182 while (curwin->w_cursor.lnum > 1)
10183 {
10184 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10185
10186 /* ignore empty lines and lines starting with '#'. */
10187 if (*ptr != '#' && *ptr != NUL)
10188 break;
10189 }
10190 if (get_indent() >= i)
10191 temp = FALSE;
10192 curwin->w_cursor = old_pos;
10193 }
10194 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010195 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 }
10197 }
10198
10199 /*
10200 * set indent of '#' always to 0
10201 */
10202 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10203 {
10204 /* remember current indent for next line */
10205 old_indent = get_indent();
10206 (void)set_indent(0, SIN_CHANGED);
10207 }
10208
10209 /* Adjust ai_col, the char at this position can be deleted. */
10210 if (ai_col > curwin->w_cursor.col)
10211 ai_col = curwin->w_cursor.col;
10212}
10213#endif
10214
10215/*
10216 * Get the value that w_virtcol would have when 'list' is off.
10217 * Unless 'cpo' contains the 'L' flag.
10218 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010219 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010220get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010222 // check validity of cursor in current buffer
10223 if (curwin->w_buffer == NULL
10224 || curwin->w_buffer->b_ml.ml_mfp == NULL
10225 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10226 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10228 return getvcol_nolist(&curwin->w_cursor);
10229 validate_virtcol();
10230 return curwin->w_virtcol;
10231}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010232
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010233#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010234/*
10235 * Handle the InsertCharPre autocommand.
10236 * "c" is the character that was typed.
10237 * Return a pointer to allocated memory with the replacement string.
10238 * Return NULL to continue inserting "c".
10239 */
10240 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010241do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010242{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010243 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010244 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010245 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010246
10247 /* Return quickly when there is nothing to do. */
10248 if (!has_insertcharpre())
10249 return NULL;
10250
Bram Moolenaar704984a2012-06-01 14:57:51 +020010251 if (has_mbyte)
10252 buf[(*mb_char2bytes)(c, buf)] = NUL;
10253 else
Bram Moolenaar704984a2012-06-01 14:57:51 +020010254 {
10255 buf[0] = c;
10256 buf[1] = NUL;
10257 }
10258
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010259 /* Lock the text to avoid weird things from happening. */
10260 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010261 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010262
Bram Moolenaar704984a2012-06-01 14:57:51 +020010263 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010264 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010265 {
10266 /* Get the value of v:char. It may be empty or more than one
10267 * character. Only use it when changed, otherwise continue with the
10268 * original character to avoid breaking autoindent. */
10269 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10270 res = vim_strsave(get_vim_var_str(VV_CHAR));
10271 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010272
10273 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10274 --textlock;
10275
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010276 // Restore the State, it may have been changed.
10277 State = save_State;
10278
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010279 return res;
10280}
10281#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010282
10283/*
10284 * Trigger "event" and take care of fixing undo.
10285 */
10286 static int
10287ins_apply_autocmds(event_T event)
10288{
10289 varnumber_T tick = CHANGEDTICK(curbuf);
10290 int r;
10291
10292 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10293
10294 // If u_savesub() was called then we are not prepared to start
10295 // a new line. Call u_save() with no contents to fix that.
10296 if (tick != CHANGEDTICK(curbuf))
10297 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10298
10299 return r;
10300}