blob: 9099c0c7079db8b1da0df56e1ed805aaa723b70a [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 Moolenaar071d4272004-06-13 20:20:40 +0000215#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100218static void replace_flush(void);
219static void replace_do_bs(int limit_col);
220static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ins_reg(void);
225static void ins_ctrl_g(void);
226static void ins_ctrl_hat(void);
227static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static int ins_start_select(int c);
232static void ins_insert(int replaceState);
233static void ins_ctrl_o(void);
234static void ins_shift(int c, int lastc);
235static void ins_del(void);
236static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ins_mouse(int c);
239static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000241#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000243#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ins_left(int end_change);
245static void ins_home(int c);
246static void ins_end(int c);
247static void ins_s_left(void);
248static void ins_right(int end_change);
249static void ins_s_right(void);
250static void ins_up(int startcol);
251static void ins_pageup(void);
252static void ins_down(int startcol);
253static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100255static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static int ins_tab(void);
258static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100266#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100268#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200269static int ins_apply_autocmds(event_T event);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271static colnr_T Insstart_textlen; /* length of line when insert started */
272static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100273static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275static char_u *last_insert = NULL; /* the text of the previous insert,
276 K_SPECIAL and CSI are escaped */
277static int last_insert_skip; /* nr of chars in front of previous insert */
278static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000279static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281#ifdef FEAT_CINDENT
282static int can_cindent; /* may do cindenting on this line */
283#endif
284
285static int old_indent = 0; /* for ^^D command in insert mode */
286
287#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000288static int revins_on; /* reverse insert mode on */
289static int revins_chars; /* how much to skip after edit */
290static int revins_legal; /* was the last char 'legal'? */
291static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294static int ins_need_undo; /* call u_save() before inserting a
295 char. Set when edit() is called.
296 after that arrow_used is used. */
297
298static int did_add_space = FALSE; /* auto_format() added an extra space
299 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200300static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
301 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302
303/*
304 * edit(): Start inserting text.
305 *
306 * "cmdchar" can be:
307 * 'i' normal insert command
308 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100309 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 * 'R' replace command
311 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
312 * but still only one <CR> is inserted. The <Esc> is not used for redo.
313 * 'g' "gI" command.
314 * 'V' "gR" command for Virtual Replace mode.
315 * 'v' "gr" command for single character Virtual Replace mode.
316 *
317 * This function is not called recursively. For CTRL-O commands, it returns
318 * and lets the caller handle the Normal-mode command.
319 *
320 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
321 */
322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100323edit(
324 int cmdchar,
325 int startln, /* if set, insert at start of line */
326 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327{
328 int c = 0;
329 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100330 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000331 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 int i;
334 int did_backspace = TRUE; /* previous char was backspace */
335#ifdef FEAT_CINDENT
336 int line_is_white = FALSE; /* line is empty before insert */
337#endif
338 linenr_T old_topline = 0; /* topline before insertion */
339#ifdef FEAT_DIFF
340 int old_topfill = -1;
341#endif
342 int inserted_space = FALSE; /* just inserted a space */
343 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000344 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200345#ifdef FEAT_JOB_CHANNEL
346 int cmdchar_todo = cmdchar;
347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000349 /* Remember whether editing was restarted after CTRL-O. */
350 did_restart_edit = restart_edit;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /* sleep before redrawing, needed for "CTRL-O :" that results in an
353 * error message */
354 check_for_delay(TRUE);
355
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100356 /* set Insstart_orig to Insstart */
357 update_Insstart_orig = TRUE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef HAVE_SANDBOX
360 /* Don't allow inserting in the sandbox. */
361 if (sandbox != 0)
362 {
363 EMSG(_(e_sandbox));
364 return FALSE;
365 }
366#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367 /* Don't allow changes in the buffer while editing the cmdline. The
368 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000369 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 {
371 EMSG(_(e_secure));
372 return FALSE;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000377 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 {
379 EMSG(_(e_secure));
380 return FALSE;
381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 ins_compl_clear(); /* clear stuff for CTRL-X mode */
383#endif
384
Bram Moolenaar843ee412004-06-30 16:16:41 +0000385 /*
386 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
387 */
388 if (cmdchar != 'r' && cmdchar != 'v')
389 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100390 pos_T save_cursor = curwin->w_cursor;
391
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100392#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000393 if (cmdchar == 'R')
394 ptr = (char_u *)"r";
395 else if (cmdchar == 'V')
396 ptr = (char_u *)"v";
397 else
398 ptr = (char_u *)"i";
399 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200400 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100401#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +0200402 ins_apply_autocmds(EVENT_INSERTENTER);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100403
Bram Moolenaar097c9922013-05-19 21:15:15 +0200404 /* Make sure the cursor didn't move. Do call check_cursor_col() in
405 * case the text was modified. Since Insert mode was not started yet
406 * a call to check_cursor_col() may move the cursor, especially with
407 * the "A" command, thus set State to avoid that. Also check that the
408 * line number is still valid (lines may have been deleted).
409 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100410 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100411#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200412 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100413#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200414 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100415 {
416 int save_state = State;
417
418 curwin->w_cursor = save_cursor;
419 State = INSERT;
420 check_cursor_col();
421 State = save_state;
422 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000423 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000424
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200425#ifdef FEAT_CONCEAL
426 /* Check if the cursor line needs redrawing before changing State. If
427 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200428 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200429#endif
430
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#ifdef FEAT_MOUSE
432 /*
433 * When doing a paste with the middle mouse button, Insstart is set to
434 * where the paste started.
435 */
436 if (where_paste_started.lnum != 0)
437 Insstart = where_paste_started;
438 else
439#endif
440 {
441 Insstart = curwin->w_cursor;
442 if (startln)
443 Insstart.col = 0;
444 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000445 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 Insstart_blank_vcol = MAXCOL;
447 if (!did_ai)
448 ai_col = 0;
449
450 if (cmdchar != NUL && restart_edit == 0)
451 {
452 ResetRedobuff();
453 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (cmdchar == 'V' || cmdchar == 'v')
455 {
456 /* "gR" or "gr" command */
457 AppendCharToRedobuff('g');
458 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
459 }
460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100462 if (cmdchar == K_PS)
463 AppendCharToRedobuff('a');
464 else
465 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 if (cmdchar == 'g') /* "gI" command */
467 AppendCharToRedobuff('I');
468 else if (cmdchar == 'r') /* "r<CR>" command */
469 count = 1; /* insert only one <CR> */
470 }
471 }
472
473 if (cmdchar == 'R')
474 {
475#ifdef FEAT_FKMAP
476 if (p_fkmap && p_ri)
477 {
478 beep_flush();
479 EMSG(farsi_text_3); /* encoded in Farsi */
480 State = INSERT;
481 }
482 else
483#endif
484 State = REPLACE;
485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 else if (cmdchar == 'V' || cmdchar == 'v')
487 {
488 State = VREPLACE;
489 replaceState = VREPLACE;
490 orig_line_count = curbuf->b_ml.ml_line_count;
491 vr_lines_changed = 1;
492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 else
494 State = INSERT;
495
496 stop_insert_mode = FALSE;
497
498 /*
499 * Need to recompute the cursor position, it might move when the cursor is
500 * on a TAB or special character.
501 */
502 curs_columns(TRUE);
503
504 /*
505 * Enable langmap or IME, indicated by 'iminsert'.
506 * Note that IME may enabled/disabled without us noticing here, thus the
507 * 'iminsert' value may not reflect what is actually used. It is updated
508 * when hitting <Esc>.
509 */
510 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
511 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100512#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
514#endif
515
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516#ifdef FEAT_MOUSE
517 setmouse();
518#endif
519#ifdef FEAT_CMDL_INFO
520 clear_showcmd();
521#endif
522#ifdef FEAT_RIGHTLEFT
523 /* there is no reverse replace mode */
524 revins_on = (State == INSERT && p_ri);
525 if (revins_on)
526 undisplay_dollar();
527 revins_chars = 0;
528 revins_legal = 0;
529 revins_scol = -1;
530#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100531 if (!p_ek)
532 /* Disable bracketed paste mode, we won't recognize the escape
533 * sequences. */
534 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
536 /*
537 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100538 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
539 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 */
541 if (restart_edit != 0 && stuff_empty())
542 {
543#ifdef FEAT_MOUSE
544 /*
545 * After a paste we consider text typed to be part of the insert for
546 * the pasted text. You can backspace over the pasted text too.
547 */
548 if (where_paste_started.lnum)
549 arrow_used = FALSE;
550 else
551#endif
552 arrow_used = TRUE;
553 restart_edit = 0;
554
555 /*
556 * If the cursor was after the end-of-line before the CTRL-O and it is
557 * now at the end-of-line, put it after the end-of-line (this is not
558 * correct in very rare cases).
559 * Also do this if curswant is greater than the current virtual
560 * column. Eg after "^O$" or "^O80|".
561 */
562 validate_virtcol();
563 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000564 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 || curwin->w_curswant > curwin->w_virtcol)
566 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
567 {
568 if (ptr[1] == NUL)
569 ++curwin->w_cursor.col;
570#ifdef FEAT_MBYTE
571 else if (has_mbyte)
572 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000573 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 if (ptr[i] == NUL)
575 curwin->w_cursor.col += i;
576 }
577#endif
578 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000579 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
581 else
582 arrow_used = FALSE;
583
584 /* we are in insert mode now, don't need to start it anymore */
585 need_start_insertmode = FALSE;
586
587 /* Need to save the line for undo before inserting the first char. */
588 ins_need_undo = TRUE;
589
590#ifdef FEAT_MOUSE
591 where_paste_started.lnum = 0;
592#endif
593#ifdef FEAT_CINDENT
594 can_cindent = TRUE;
595#endif
596#ifdef FEAT_FOLDING
597 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
598 * restarting. */
599 if (!p_im && did_restart_edit == 0)
600 foldOpenCursor();
601#endif
602
603 /*
604 * If 'showmode' is set, show the current (insert/replace/..) mode.
605 * A warning message for changing a readonly file is given here, before
606 * actually changing anything. It's put after the mode, if any.
607 */
608 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000609 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 i = showmode();
611
612 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000613 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
615#ifdef CURSOR_SHAPE
616 ui_cursor_shape(); /* may show different cursor shape */
617#endif
618#ifdef FEAT_DIGRAPHS
619 do_digraph(-1); /* clear digraphs */
620#endif
621
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000622 /*
623 * Get the current length of the redo buffer, those characters have to be
624 * skipped if we want to get to the inserted characters.
625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 ptr = get_inserted();
627 if (ptr == NULL)
628 new_insert_skip = 0;
629 else
630 {
631 new_insert_skip = (int)STRLEN(ptr);
632 vim_free(ptr);
633 }
634
635 old_indent = 0;
636
637 /*
638 * Main loop in Insert mode: repeat until Insert mode is left.
639 */
640 for (;;)
641 {
642#ifdef FEAT_RIGHTLEFT
643 if (!revins_legal)
644 revins_scol = -1; /* reset on illegal motions */
645 else
646 revins_legal = 0;
647#endif
648 if (arrow_used) /* don't repeat insert when arrow key used */
649 count = 0;
650
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100651 if (update_Insstart_orig)
652 Insstart_orig = Insstart;
653
Bram Moolenaar00672e12016-06-26 18:38:13 +0200654 if (stop_insert_mode
655#ifdef FEAT_INS_EXPAND
656 && !pum_visible()
657#endif
658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
660 /* ":stopinsert" used or 'insertmode' reset */
661 count = 0;
662 goto doESCkey;
663 }
664
665 /* set curwin->w_curswant for next K_DOWN or K_UP */
666 if (!arrow_used)
667 curwin->w_set_curswant = TRUE;
668
669 /* If there is no typeahead may check for timestamps (e.g., for when a
670 * menu invoked a shell command). */
671 if (stuff_empty())
672 {
673 did_check_timestamps = FALSE;
674 if (need_check_timestamps)
675 check_timestamps(FALSE);
676 }
677
678 /*
679 * When emsg() was called msg_scroll will have been set.
680 */
681 msg_scroll = FALSE;
682
683#ifdef FEAT_GUI
684 /* When 'mousefocus' is set a mouse movement may have taken us to
685 * another window. "need_mouse_correct" may then be set because of an
686 * autocommand. */
687 if (need_mouse_correct)
688 gui_mouse_correct();
689#endif
690
691#ifdef FEAT_FOLDING
692 /* Open fold at the cursor line, according to 'foldopen'. */
693 if (fdo_flags & FDO_INSERT)
694 foldOpenCursor();
695 /* Close folds where the cursor isn't, according to 'foldclose' */
696 if (!char_avail())
697 foldCheckClose();
698#endif
699
Bram Moolenaarf2732452018-06-03 14:47:35 +0200700#ifdef FEAT_JOB_CHANNEL
701 if (bt_prompt(curbuf))
702 {
703 init_prompt(cmdchar_todo);
704 cmdchar_todo = NUL;
705 }
706#endif
707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 /*
709 * If we inserted a character at the last position of the last line in
710 * the window, scroll the window one line up. This avoids an extra
711 * redraw.
712 * This is detected when the cursor column is smaller after inserting
713 * something.
714 * Don't do this when the topline changed already, it has
715 * already been adjusted (by insertchar() calling open_line())).
716 */
717 if (curbuf->b_mod_set
718 && curwin->w_p_wrap
719 && !did_backspace
720 && curwin->w_topline == old_topline
721#ifdef FEAT_DIFF
722 && curwin->w_topfill == old_topfill
723#endif
724 )
725 {
726 mincol = curwin->w_wcol;
727 validate_cursor_col();
728
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200729 if (
730#ifdef FEAT_VARTABS
731 (int)curwin->w_wcol < mincol - tabstop_at(
732 get_nolist_virtcol(), curbuf->b_p_ts,
733 curbuf->b_p_vts_array)
734#else
735 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 && curwin->w_wrow == W_WINROW(curwin)
738 + curwin->w_height - 1 - p_so
739 && (curwin->w_cursor.lnum != curwin->w_topline
740#ifdef FEAT_DIFF
741 || curwin->w_topfill > 0
742#endif
743 ))
744 {
745#ifdef FEAT_DIFF
746 if (curwin->w_topfill > 0)
747 --curwin->w_topfill;
748 else
749#endif
750#ifdef FEAT_FOLDING
751 if (hasFolding(curwin->w_topline, NULL, &old_topline))
752 set_topline(curwin, old_topline + 1);
753 else
754#endif
755 set_topline(curwin, curwin->w_topline + 1);
756 }
757 }
758
759 /* May need to adjust w_topline to show the cursor. */
760 update_topline();
761
762 did_backspace = FALSE;
763
764 validate_cursor(); /* may set must_redraw */
765
766 /*
767 * Redraw the display when no characters are waiting.
768 * Also shows mode, ruler and positions cursor.
769 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000770 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (curwin->w_p_scb)
773 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar860cae12010-06-05 23:22:07 +0200775 if (curwin->w_p_crb)
776 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 update_curswant();
778 old_topline = curwin->w_topline;
779#ifdef FEAT_DIFF
780 old_topfill = curwin->w_topfill;
781#endif
782
783#ifdef USE_ON_FLY_SCROLL
784 dont_scroll = FALSE; /* allow scrolling here */
785#endif
786
787 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100788 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100790 if (c != K_CURSORHOLD)
791 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200792
793 /* After using CTRL-G U the next cursor key will not break undo. */
794 if (dont_sync_undo == MAYBE)
795 dont_sync_undo = TRUE;
796 else
797 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100798 if (cmdchar == K_PS)
799 /* Got here from normal mode when bracketed paste started. */
800 c = K_PS;
801 else
802 do
803 {
804 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200805
806 if (stop_insert_mode)
807 {
808 // Insert mode ended, possibly from a callback.
809 count = 0;
810 nomove = TRUE;
811 goto doESCkey;
812 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100813 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000815 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
816 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#ifdef FEAT_RIGHTLEFT
819 if (p_hkmap && KeyTyped)
820 c = hkmap(c); /* Hebrew mode mapping */
821#endif
822#ifdef FEAT_FKMAP
823 if (p_fkmap && KeyTyped)
824 c = fkmap(c); /* Farsi mode mapping */
825#endif
826
827#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 /*
829 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000830 * and the cursor is still in the completed word. Only when there is
831 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000832 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000833 if (compl_started
834 && pum_wanted()
835 && curwin->w_cursor.col >= compl_col
836 && (compl_shown_match == NULL
837 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000838 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000839 /* BS: Delete one character from "compl_leader". */
840 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000841 && curwin->w_cursor.col > compl_col
842 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000843 continue;
844
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000845 /* When no match was selected or it was edited. */
846 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000847 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000848 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000849 * "compl_leader". Except when at the original match and
850 * there is nothing to add, CTRL-L works like CTRL-P then. */
851 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100852 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000853 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000854 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000855 {
856 ins_compl_addfrommatch();
857 continue;
858 }
859
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000860 /* A non-white character that fits in with the current
861 * completion: Add to "compl_leader". */
862 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000863 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100864#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100865 /* Trigger InsertCharPre. */
866 char_u *str = do_insert_char_pre(c);
867 char_u *p;
868
869 if (str != NULL)
870 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100871 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100872 ins_compl_addleader(PTR2CHAR(p));
873 vim_free(str);
874 }
875 else
876#endif
877 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000878 continue;
879 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000880
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000881 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000882 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200883 if ((c == Ctrl_Y || (compl_enter_selects
884 && (c == CAR || c == K_KENTER || c == NL)))
885 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000886 {
887 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200888 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000889 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000890 }
891 }
892
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
894 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000895 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000896 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000897 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#endif
899
Bram Moolenaar488c6512005-08-11 20:09:58 +0000900 /* CTRL-\ CTRL-N goes to Normal mode,
901 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
902 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (c == Ctrl_BSL)
904 {
905 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000906 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 ++no_mapping;
908 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000909 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 --no_mapping;
911 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000912 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000914 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 vungetc(c);
916 c = Ctrl_BSL;
917 }
918 else if (c == Ctrl_G && p_im)
919 continue;
920 else
921 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000922 if (c == Ctrl_O)
923 {
924 ins_ctrl_o();
925 ins_at_eol = FALSE; /* cursor keeps its column */
926 nomove = TRUE;
927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 count = 0;
929 goto doESCkey;
930 }
931 }
932
933#ifdef FEAT_DIGRAPHS
934 c = do_digraph(c);
935#endif
936
937#ifdef FEAT_INS_EXPAND
938 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
939 goto docomplete;
940#endif
941 if (c == Ctrl_V || c == Ctrl_Q)
942 {
943 ins_ctrl_v();
944 c = Ctrl_V; /* pretend CTRL-V is last typed character */
945 continue;
946 }
947
948#ifdef FEAT_CINDENT
949 if (cindent_on()
950# ifdef FEAT_INS_EXPAND
951 && ctrl_x_mode == 0
952# endif
953 )
954 {
955 /* A key name preceded by a bang means this key is not to be
956 * inserted. Skip ahead to the re-indenting below.
957 * A key name preceded by a star means that indenting has to be
958 * done before inserting the key. */
959 line_is_white = inindent(0);
960 if (in_cinkeys(c, '!', line_is_white))
961 goto force_cindent;
962 if (can_cindent && in_cinkeys(c, '*', line_is_white)
963 && stop_arrow() == OK)
964 do_c_expr_indent();
965 }
966#endif
967
968#ifdef FEAT_RIGHTLEFT
969 if (curwin->w_p_rl)
970 switch (c)
971 {
972 case K_LEFT: c = K_RIGHT; break;
973 case K_S_LEFT: c = K_S_RIGHT; break;
974 case K_C_LEFT: c = K_C_RIGHT; break;
975 case K_RIGHT: c = K_LEFT; break;
976 case K_S_RIGHT: c = K_S_LEFT; break;
977 case K_C_RIGHT: c = K_C_LEFT; break;
978 }
979#endif
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 /*
982 * If 'keymodel' contains "startsel", may start selection. If it
983 * does, a CTRL-O and c will be stuffed, we need to get these
984 * characters.
985 */
986 if (ins_start_select(c))
987 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
989 /*
990 * The big switch to handle a character in insert mode.
991 */
992 switch (c)
993 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if (echeck_abbr(ESC + ABBR_OFF))
996 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200997 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000999 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#ifdef FEAT_CMDWIN
1001 if (c == Ctrl_C && cmdwin_type != 0)
1002 {
1003 /* Close the cmdline window. */
1004 cmdwin_result = K_IGNORE;
1005 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001006 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 goto doESCkey;
1008 }
1009#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001010#ifdef FEAT_JOB_CHANNEL
1011 if (c == Ctrl_C && bt_prompt(curbuf))
1012 {
1013 if (invoke_prompt_interrupt())
1014 {
1015 if (!bt_prompt(curbuf))
1016 // buffer changed to a non-prompt buffer, get out of
1017 // Insert mode
1018 goto doESCkey;
1019 break;
1020 }
1021 }
1022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024#ifdef UNIX
1025do_intr:
1026#endif
1027 /* when 'insertmode' set, and not halfway a mapping, don't leave
1028 * Insert mode */
1029 if (goto_im())
1030 {
1031 if (got_int)
1032 {
1033 (void)vgetc(); /* flush all buffers */
1034 got_int = FALSE;
1035 }
1036 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001037 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 break;
1039 }
1040doESCkey:
1041 /*
1042 * This is the ONLY return from edit()!
1043 */
1044 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1045 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001046 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 o_lnum = curwin->w_cursor.lnum;
1048
Bram Moolenaar488c6512005-08-11 20:09:58 +00001049 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001050 {
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01001051 // When CTRL-C was typed got_int will be set, with the result
1052 // that the autocommands won't be executed. When mapped got_int
1053 // is not set, but let's keep the behavior the same.
1054 if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001055 ins_apply_autocmds(EVENT_INSERTLEAVE);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001056 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 continue;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case Ctrl_Z: /* suspend when 'insertmode' set */
1062 if (!p_im)
1063 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001064 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001065#ifdef CURSOR_SHAPE
1066 ui_cursor_shape(); /* may need to update cursor shape */
1067#endif
1068 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069
1070 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001071#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001072 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 goto docomplete;
1074#endif
1075 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1076 break;
1077 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001078
1079#ifdef FEAT_VIRTUALEDIT
1080 /* don't move the cursor left when 'virtualedit' has "onemore". */
1081 if (ve_flags & VE_ONEMORE)
1082 {
1083 ins_at_eol = FALSE;
1084 nomove = TRUE;
1085 }
1086#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 count = 0;
1088 goto doESCkey;
1089
Bram Moolenaar572cb562005-08-05 21:35:02 +00001090 case K_INS: /* toggle insert/replace mode */
1091 case K_KINS:
1092 ins_insert(replaceState);
1093 break;
1094
1095 case K_SELECT: /* end of Select mode mapping - ignore */
1096 break;
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case K_HELP: /* Help key works like <ESC> <Help> */
1099 case K_F1:
1100 case K_XF1:
1101 stuffcharReadbuff(K_HELP);
1102 if (p_im)
1103 need_start_insertmode = TRUE;
1104 goto doESCkey;
1105
1106#ifdef FEAT_NETBEANS_INTG
1107 case K_F21: /* NetBeans command */
1108 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001109 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 --no_mapping;
1111 netbeans_keycommand(i);
1112 break;
1113#endif
1114
1115 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 case NUL:
1117 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 /* For ^@ the trailing ESC will end the insert, unless there is an
1119 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1121 && c != Ctrl_A && !p_im)
1122 goto doESCkey; /* quit insert mode */
1123 inserted_space = FALSE;
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ins_reg();
1128 auto_format(FALSE, TRUE);
1129 inserted_space = FALSE;
1130 break;
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 ins_ctrl_g();
1134 break;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case Ctrl_HAT: /* switch input mode and/or langmap */
1137 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 break;
1139
1140#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (!p_ari)
1143 goto normalchar;
1144 ins_ctrl_();
1145 break;
1146#endif
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1150 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1151 goto docomplete;
1152#endif
1153 /* FALLTHROUGH */
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156# ifdef FEAT_INS_EXPAND
1157 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1158 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001159 if (has_compl_option(FALSE))
1160 goto docomplete;
1161 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163# endif
1164 ins_shift(c, lastc);
1165 auto_format(FALSE, TRUE);
1166 inserted_space = FALSE;
1167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 case K_KDEL:
1171 ins_del();
1172 auto_format(FALSE, TRUE);
1173 break;
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 case Ctrl_H:
1177 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1178 auto_format(FALSE, TRUE);
1179 break;
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001182#ifdef FEAT_JOB_CHANNEL
1183 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1184 {
1185 // In a prompt window CTRL-W is used for window commands.
1186 // Use Shift-CTRL-W to delete a word.
1187 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001188 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001189 nomove = TRUE;
1190 count = 0;
1191 goto doESCkey;
1192 }
1193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1195 auto_format(FALSE, TRUE);
1196 break;
1197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001199# ifdef FEAT_COMPL_FUNC
1200 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001202 goto docomplete;
1203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1205 auto_format(FALSE, TRUE);
1206 inserted_space = FALSE;
1207 break;
1208
1209#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001210 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 case K_LEFTMOUSE_NM:
1212 case K_LEFTDRAG:
1213 case K_LEFTRELEASE:
1214 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001215 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 case K_MIDDLEMOUSE:
1217 case K_MIDDLEDRAG:
1218 case K_MIDDLERELEASE:
1219 case K_RIGHTMOUSE:
1220 case K_RIGHTDRAG:
1221 case K_RIGHTRELEASE:
1222 case K_X1MOUSE:
1223 case K_X1DRAG:
1224 case K_X1RELEASE:
1225 case K_X2MOUSE:
1226 case K_X2DRAG:
1227 case K_X2RELEASE:
1228 ins_mouse(c);
1229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001232 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 break;
1234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001235 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001236 ins_mousescroll(MSCR_UP);
1237 break;
1238
1239 case K_MOUSELEFT: /* Scroll wheel left */
1240 ins_mousescroll(MSCR_LEFT);
1241 break;
1242
1243 case K_MOUSERIGHT: /* Scroll wheel right */
1244 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 break;
1246#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001247 case K_PS:
1248 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1249 if (cmdchar == K_PS)
1250 /* invoked from normal mode, bail out */
1251 goto doESCkey;
1252 break;
1253 case K_PE:
1254 /* Got K_PE without K_PS, ignore. */
1255 break;
1256
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001257#ifdef FEAT_GUI_TABLINE
1258 case K_TABLINE:
1259 case K_TABMENU:
1260 ins_tabline(c);
1261 break;
1262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001264 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 break;
1266
Bram Moolenaar754b5602006-02-09 23:53:20 +00001267 case K_CURSORHOLD: /* Didn't type something for a while. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001268 ins_apply_autocmds(EVENT_CURSORHOLDI);
Bram Moolenaar754b5602006-02-09 23:53:20 +00001269 did_cursorhold = TRUE;
1270 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001271
Bram Moolenaar4770d092006-01-12 23:22:24 +00001272#ifdef FEAT_GUI_W32
1273 /* On Win32 ignore <M-F4>, we get it when closing the window was
1274 * cancelled. */
1275 case K_F4:
1276 if (mod_mask != MOD_MASK_ALT)
1277 goto normalchar;
1278 break;
1279#endif
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281#ifdef FEAT_GUI
1282 case K_VER_SCROLLBAR:
1283 ins_scroll();
1284 break;
1285
1286 case K_HOR_SCROLLBAR:
1287 ins_horscroll();
1288 break;
1289#endif
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 case K_S_HOME:
1294 case K_C_HOME:
1295 ins_home(c);
1296 break;
1297
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001298 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 case K_S_END:
1301 case K_C_END:
1302 ins_end(c);
1303 break;
1304
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001305 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001306 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1307 ins_s_left();
1308 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001309 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 break;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 case K_C_LEFT:
1314 ins_s_left();
1315 break;
1316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001317 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001318 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1319 ins_s_right();
1320 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001321 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 break;
1323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 case K_C_RIGHT:
1326 ins_s_right();
1327 break;
1328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001329 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001330#ifdef FEAT_INS_EXPAND
1331 if (pum_visible())
1332 goto docomplete;
1333#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001334 if (mod_mask & MOD_MASK_SHIFT)
1335 ins_pageup();
1336 else
1337 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 break;
1339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001340 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 case K_PAGEUP:
1342 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001343#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001344 if (pum_visible())
1345 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 ins_pageup();
1348 break;
1349
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001350 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001351#ifdef FEAT_INS_EXPAND
1352 if (pum_visible())
1353 goto docomplete;
1354#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001355 if (mod_mask & MOD_MASK_SHIFT)
1356 ins_pagedown();
1357 else
1358 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 break;
1360
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001361 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 case K_PAGEDOWN:
1363 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001364#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001365 if (pum_visible())
1366 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 ins_pagedown();
1369 break;
1370
1371#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001372 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 ins_drop();
1374 break;
1375#endif
1376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001377 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 c = TAB;
1379 /* FALLTHROUGH */
1380
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001381 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1383 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1384 goto docomplete;
1385#endif
1386 inserted_space = FALSE;
1387 if (ins_tab())
1388 goto normalchar; /* insert TAB as a normal char */
1389 auto_format(FALSE, TRUE);
1390 break;
1391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001392 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 c = CAR;
1394 /* FALLTHROUGH */
1395 case CAR:
1396 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001397#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 /* In a quickfix window a <CR> jumps to the error under the
1399 * cursor. */
1400 if (bt_quickfix(curbuf) && c == CAR)
1401 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001402 if (curwin->w_llist_ref == NULL) /* quickfix window */
1403 do_cmdline_cmd((char_u *)".cc");
1404 else /* location list window */
1405 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 break;
1407 }
1408#endif
1409#ifdef FEAT_CMDWIN
1410 if (cmdwin_type != 0)
1411 {
1412 /* Execute the command in the cmdline window. */
1413 cmdwin_result = CAR;
1414 goto doESCkey;
1415 }
1416#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001417#ifdef FEAT_JOB_CHANNEL
1418 if (bt_prompt(curbuf))
1419 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001420 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001421 if (!bt_prompt(curbuf))
1422 // buffer changed to a non-prompt buffer, get out of
1423 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001424 goto doESCkey;
1425 break;
1426 }
1427#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001428 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 goto doESCkey; /* out of memory */
1430 auto_format(FALSE, FALSE);
1431 inserted_space = FALSE;
1432 break;
1433
Bram Moolenaar860cae12010-06-05 23:22:07 +02001434#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001435 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436# ifdef FEAT_INS_EXPAND
1437 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1438 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001439 if (has_compl_option(TRUE))
1440 goto docomplete;
1441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443# endif
1444# ifdef FEAT_DIGRAPHS
1445 c = ins_digraph();
1446 if (c == NUL)
1447 break;
1448# endif
1449 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
1452#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001453 case Ctrl_X: /* Enter CTRL-X mode */
1454 ins_ctrl_x();
1455 break;
1456
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001457 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (ctrl_x_mode != CTRL_X_TAGS)
1459 goto normalchar;
1460 goto docomplete;
1461
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001462 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (ctrl_x_mode != CTRL_X_FILES)
1464 goto normalchar;
1465 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001466
1467 case 's': /* Spelling completion after ^X */
1468 case Ctrl_S:
1469 if (ctrl_x_mode != CTRL_X_SPELL)
1470 goto normalchar;
1471 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472#endif
1473
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001474 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475#ifdef FEAT_INS_EXPAND
1476 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1477#endif
1478 {
1479 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1480 if (p_im)
1481 {
1482 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1483 break;
1484 goto doESCkey;
1485 }
1486 goto normalchar;
1487 }
1488#ifdef FEAT_INS_EXPAND
1489 /* FALLTHROUGH */
1490
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001491 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 case Ctrl_N:
1493 /* if 'complete' is empty then plain ^P is no longer special,
1494 * but it is under other ^X modes */
1495 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001496 && (ctrl_x_mode == CTRL_X_NORMAL
1497 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001498 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 goto normalchar;
1500
1501docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001502 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001503#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001504 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001505#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001506 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001507 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001508#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001509 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001510#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001511 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 break;
1513#endif /* FEAT_INS_EXPAND */
1514
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001515 case Ctrl_Y: /* copy from previous line or scroll down */
1516 case Ctrl_E: /* copy from next line or scroll up */
1517 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 break;
1519
1520 default:
1521#ifdef UNIX
1522 if (c == intr_char) /* special interrupt char */
1523 goto do_intr;
1524#endif
1525
Bram Moolenaare659c952011-05-19 17:25:41 +02001526normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001528 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001530#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001531 if (!p_paste)
1532 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001533 /* Trigger InsertCharPre. */
1534 char_u *str = do_insert_char_pre(c);
1535 char_u *p;
1536
1537 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001538 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001539 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001540 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001541 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001542 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001543 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001544 c = PTR2CHAR(p);
1545 if (c == CAR || c == K_KENTER || c == NL)
1546 ins_eol(c);
1547 else
1548 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001549 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001550 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001551 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001552 vim_free(str);
1553 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001554 }
1555
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001556 /* If the new value is already inserted or an empty string
1557 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001558 if (c == NUL)
1559 break;
1560 }
1561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562#ifdef FEAT_SMARTINDENT
1563 /* Try to perform smart-indenting. */
1564 ins_try_si(c);
1565#endif
1566
1567 if (c == ' ')
1568 {
1569 inserted_space = TRUE;
1570#ifdef FEAT_CINDENT
1571 if (inindent(0))
1572 can_cindent = FALSE;
1573#endif
1574 if (Insstart_blank_vcol == MAXCOL
1575 && curwin->w_cursor.lnum == Insstart.lnum)
1576 Insstart_blank_vcol = get_nolist_virtcol();
1577 }
1578
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001579 /* Insert a normal character and check for abbreviations on a
1580 * special character. Let CTRL-] expand abbreviations without
1581 * inserting it. */
1582 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583#ifdef FEAT_MBYTE
1584 /* Add ABBR_OFF for characters above 0x100, this is
1585 * what check_abbr() expects. */
1586 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1587#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001588 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 insert_special(c, FALSE, FALSE);
1591#ifdef FEAT_RIGHTLEFT
1592 revins_legal++;
1593 revins_chars++;
1594#endif
1595 }
1596
1597 auto_format(FALSE, TRUE);
1598
1599#ifdef FEAT_FOLDING
1600 /* When inserting a character the cursor line must never be in a
1601 * closed fold. */
1602 foldOpenCursor();
1603#endif
1604 break;
1605 } /* end of switch (c) */
1606
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001607 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001608 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001609#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001610 /* but not in CTRL-X mode, a script can't restore the state */
1611 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001612#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001613 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001614 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 /* If the cursor was moved we didn't just insert a space */
1617 if (arrow_used)
1618 inserted_space = FALSE;
1619
1620#ifdef FEAT_CINDENT
1621 if (can_cindent && cindent_on()
1622# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001623 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624# endif
1625 )
1626 {
1627force_cindent:
1628 /*
1629 * Indent now if a key was typed that is in 'cinkeys'.
1630 */
1631 if (in_cinkeys(c, ' ', line_is_white))
1632 {
1633 if (stop_arrow() == OK)
1634 /* re-indent the current line */
1635 do_c_expr_indent();
1636 }
1637 }
1638#endif /* FEAT_CINDENT */
1639
1640 } /* for (;;) */
1641 /* NOTREACHED */
1642}
1643
1644/*
1645 * Redraw for Insert mode.
1646 * This is postponed until getting the next character to make '$' in the 'cpo'
1647 * option work correctly.
1648 * Only redraw when there are no characters available. This speeds up
1649 * inserting sequences of characters (e.g., for CTRL-R).
1650 */
1651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001652ins_redraw(
1653 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001655#ifdef FEAT_CONCEAL
1656 linenr_T conceal_old_cursor_line = 0;
1657 linenr_T conceal_new_cursor_line = 0;
1658 int conceal_update_lines = FALSE;
1659#endif
1660
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001661 if (char_avail())
1662 return;
1663
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001664#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1666 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001667 if (ready && (has_cursormovedI()
1668# if defined(FEAT_CONCEAL)
1669 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001670# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001672 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001673# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675# endif
1676 )
1677 {
1678# ifdef FEAT_SYN_HL
1679 /* Need to update the screen first, to make sure syntax
1680 * highlighting is correct after making a change (e.g., inserting
1681 * a "(". The autocommand may also require a redraw, so it's done
1682 * again below, unfortunately. */
1683 if (syntax_present(curwin) && must_redraw)
1684 update_screen(0);
1685# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001686 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001687 {
1688 /* Make sure curswant is correct, an autocommand may call
1689 * getcurpos(). */
1690 update_curswant();
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001691 ins_apply_autocmds(EVENT_CURSORMOVEDI);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001692 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001693# ifdef FEAT_CONCEAL
1694 if (curwin->w_p_cole > 0)
1695 {
1696 conceal_old_cursor_line = last_cursormoved.lnum;
1697 conceal_new_cursor_line = curwin->w_cursor.lnum;
1698 conceal_update_lines = TRUE;
1699 }
1700# endif
1701 last_cursormoved = curwin->w_cursor;
1702 }
1703#endif
1704
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001705 /* Trigger TextChangedI if b_changedtick differs. */
1706 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001707 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001708#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001709 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001710#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001711 )
1712 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001713 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001714 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001715
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001716 // save and restore curwin and curbuf, in case the autocmd changes them
1717 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001718 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001719 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001720 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001721 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1722 u_save(curwin->w_cursor.lnum,
1723 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001725
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001726#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001727 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1728 * TextChangedI will need to trigger for backwards compatibility, thus use
1729 * different b_last_changedtick* variables. */
1730 if (ready && has_textchangedP()
1731 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1732 && pum_visible())
1733 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001734 aco_save_T aco;
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001735 varnumber_T tick = CHANGEDTICK(curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001736
1737 // save and restore curwin and curbuf, in case the autocmd changes them
1738 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001739 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001740 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001741 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001742 if (tick != CHANGEDTICK(curbuf)) // see ins_apply_autocmds()
1743 u_save(curwin->w_cursor.lnum,
1744 (linenr_T)(curwin->w_cursor.lnum + 1));
Bram Moolenaar5a093432018-02-10 18:15:19 +01001745 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001746#endif
1747
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001748#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001749 if ((conceal_update_lines
1750 && (conceal_old_cursor_line != conceal_new_cursor_line
1751 || conceal_cursor_line(curwin)))
1752 || need_cursor_line_redraw)
1753 {
1754 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001755 redrawWinline(curwin, conceal_old_cursor_line);
1756 redrawWinline(curwin, conceal_new_cursor_line == 0
1757 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001758 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001759 need_cursor_line_redraw = FALSE;
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001760 }
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001761#endif
1762 if (must_redraw)
1763 update_screen(0);
1764 else if (clear_cmdline || redraw_cmdline)
1765 showmode(); /* clear cmdline and show mode */
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001766 showruler(FALSE);
1767 setcursor();
1768 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
1771/*
1772 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1773 */
1774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001778 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001781 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001786 did_putchar = TRUE;
1787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1789
1790#ifdef FEAT_CMDL_INFO
1791 add_to_showcmd_c(Ctrl_V);
1792#endif
1793
1794 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001795 if (did_putchar)
1796 /* when the line fits in 'columns' the '^' is at the start of the next
1797 * line and will not removed by the redraw */
1798 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#ifdef FEAT_CMDL_INFO
1800 clear_showcmd();
1801#endif
1802 insert_special(c, FALSE, TRUE);
1803#ifdef FEAT_RIGHTLEFT
1804 revins_chars++;
1805 revins_legal++;
1806#endif
1807}
1808
1809/*
1810 * Put a character directly onto the screen. It's not stored in a buffer.
1811 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1812 */
1813static int pc_status;
1814#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1815#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1816#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1817#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819static int pc_attr;
1820static int pc_row;
1821static int pc_col;
1822
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 int attr;
1827
1828 if (ScreenLines != NULL)
1829 {
1830 update_topline(); /* just in case w_topline isn't valid */
1831 validate_cursor();
1832 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001833 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 else
1835 attr = 0;
1836 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001837 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1839 pc_status = PC_STATUS_UNSET;
1840#endif
1841#ifdef FEAT_RIGHTLEFT
1842 if (curwin->w_p_rl)
1843 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001844 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845# ifdef FEAT_MBYTE
1846 if (has_mbyte)
1847 {
1848 int fix_col = mb_fix_col(pc_col, pc_row);
1849
1850 if (fix_col != pc_col)
1851 {
1852 screen_putchar(' ', pc_row, fix_col, attr);
1853 --curwin->w_wcol;
1854 pc_status = PC_STATUS_RIGHT;
1855 }
1856 }
1857# endif
1858 }
1859 else
1860#endif
1861 {
1862 pc_col += curwin->w_wcol;
1863#ifdef FEAT_MBYTE
1864 if (mb_lefthalve(pc_row, pc_col))
1865 pc_status = PC_STATUS_LEFT;
1866#endif
1867 }
1868
1869 /* save the character to be able to put it back */
1870#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1871 if (pc_status == PC_STATUS_UNSET)
1872#endif
1873 {
1874 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1875 pc_status = PC_STATUS_SET;
1876 }
1877 screen_putchar(c, pc_row, pc_col, attr);
1878 }
1879}
1880
Bram Moolenaarf2732452018-06-03 14:47:35 +02001881#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1882/*
1883 * Return the effective prompt for the current buffer.
1884 */
1885 char_u *
1886prompt_text(void)
1887{
1888 if (curbuf->b_prompt_text == NULL)
1889 return (char_u *)"% ";
1890 return curbuf->b_prompt_text;
1891}
1892
1893/*
1894 * Prepare for prompt mode: Make sure the last line has the prompt text.
1895 * Move the cursor to this line.
1896 */
1897 static void
1898init_prompt(int cmdchar_todo)
1899{
1900 char_u *prompt = prompt_text();
1901 char_u *text;
1902
1903 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1904 text = ml_get_curline();
1905 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1906 {
1907 // prompt is missing, insert it or append a line with it
1908 if (*text == NUL)
1909 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1910 else
1911 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1912 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1913 coladvance((colnr_T)MAXCOL);
1914 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1915 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001916
1917 // Insert always starts after the prompt, allow editing text after it.
1918 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1919 || Insstart_orig.col != (int)STRLEN(prompt))
1920 {
1921 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001922 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001923 Insstart_orig = Insstart;
1924 Insstart_textlen = Insstart.col;
1925 Insstart_blank_vcol = MAXCOL;
1926 arrow_used = FALSE;
1927 }
1928
Bram Moolenaarf2732452018-06-03 14:47:35 +02001929 if (cmdchar_todo == 'A')
1930 coladvance((colnr_T)MAXCOL);
1931 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001932 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001933 /* Make sure the cursor is in a valid position. */
1934 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001935}
1936
1937/*
1938 * Return TRUE if the cursor is in the editable position of the prompt line.
1939 */
1940 int
1941prompt_curpos_editable()
1942{
1943 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1944 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1945}
1946#endif
1947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948/*
1949 * Undo the previous edit_putchar().
1950 */
1951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001952edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953{
1954 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1955 {
1956#if defined(FEAT_MBYTE)
1957 if (pc_status == PC_STATUS_RIGHT)
1958 ++curwin->w_wcol;
1959 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01001960 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 else
1962#endif
1963 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1964 }
1965}
1966
1967/*
1968 * Called when p_dollar is set: display a '$' at the end of the changed text
1969 * Only works when cursor is in the line that changes.
1970 */
1971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001972display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
1974 colnr_T save_col;
1975
1976 if (!redrawing())
1977 return;
1978
1979 cursor_off();
1980 save_col = curwin->w_cursor.col;
1981 curwin->w_cursor.col = col;
1982#ifdef FEAT_MBYTE
1983 if (has_mbyte)
1984 {
1985 char_u *p;
1986
1987 /* If on the last byte of a multi-byte move to the first byte. */
1988 p = ml_get_curline();
1989 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1990 }
1991#endif
1992 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001993 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
1995 edit_putchar('$', FALSE);
1996 dollar_vcol = curwin->w_virtcol;
1997 }
1998 curwin->w_cursor.col = save_col;
1999}
2000
2001/*
2002 * Call this function before moving the cursor from the normal insert position
2003 * in insert mode.
2004 */
2005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002006undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002008 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002010 dollar_vcol = -1;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01002011 redrawWinline(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013}
2014
2015/*
2016 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2017 * Keep the cursor on the same character.
2018 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2019 * type == INDENT_DEC decrease indent (for CTRL-D)
2020 * type == INDENT_SET set indent to "amount"
2021 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2022 */
2023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002024change_indent(
2025 int type,
2026 int amount,
2027 int round,
2028 int replaced, /* replaced character, put on replace stack */
2029 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030{
2031 int vcol;
2032 int last_vcol;
2033 int insstart_less; /* reduction for Insstart.col */
2034 int new_cursor_col;
2035 int i;
2036 char_u *ptr;
2037 int save_p_list;
2038 int start_col;
2039 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 colnr_T orig_col = 0; /* init for GCC */
2041 char_u *new_line, *orig_line = NULL; /* init for GCC */
2042
2043 /* VREPLACE mode needs to know what the line was like before changing */
2044 if (State & VREPLACE_FLAG)
2045 {
2046 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2047 orig_col = curwin->w_cursor.col;
2048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 /* for the following tricks we don't want list mode */
2051 save_p_list = curwin->w_p_list;
2052 curwin->w_p_list = FALSE;
2053 vc = getvcol_nolist(&curwin->w_cursor);
2054 vcol = vc;
2055
2056 /*
2057 * For Replace mode we need to fix the replace stack later, which is only
2058 * possible when the cursor is in the indent. Remember the number of
2059 * characters before the cursor if it's possible.
2060 */
2061 start_col = curwin->w_cursor.col;
2062
2063 /* determine offset from first non-blank */
2064 new_cursor_col = curwin->w_cursor.col;
2065 beginline(BL_WHITE);
2066 new_cursor_col -= curwin->w_cursor.col;
2067
2068 insstart_less = curwin->w_cursor.col;
2069
2070 /*
2071 * If the cursor is in the indent, compute how many screen columns the
2072 * cursor is to the left of the first non-blank.
2073 */
2074 if (new_cursor_col < 0)
2075 vcol = get_indent() - vcol;
2076
2077 if (new_cursor_col > 0) /* can't fix replace stack */
2078 start_col = -1;
2079
2080 /*
2081 * Set the new indent. The cursor will be put on the first non-blank.
2082 */
2083 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002084 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 else
2086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 int save_State = State;
2088
2089 /* Avoid being called recursively. */
2090 if (State & VREPLACE_FLAG)
2091 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002092 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095 insstart_less -= curwin->w_cursor.col;
2096
2097 /*
2098 * Try to put cursor on same character.
2099 * If the cursor is at or after the first non-blank in the line,
2100 * compute the cursor column relative to the column of the first
2101 * non-blank character.
2102 * If we are not in insert mode, leave the cursor on the first non-blank.
2103 * If the cursor is before the first non-blank, position it relative
2104 * to the first non-blank, counted in screen columns.
2105 */
2106 if (new_cursor_col >= 0)
2107 {
2108 /*
2109 * When changing the indent while the cursor is touching it, reset
2110 * Insstart_col to 0.
2111 */
2112 if (new_cursor_col == 0)
2113 insstart_less = MAXCOL;
2114 new_cursor_col += curwin->w_cursor.col;
2115 }
2116 else if (!(State & INSERT))
2117 new_cursor_col = curwin->w_cursor.col;
2118 else
2119 {
2120 /*
2121 * Compute the screen column where the cursor should be.
2122 */
2123 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002124 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
2126 /*
2127 * Advance the cursor until we reach the right screen column.
2128 */
2129 vcol = last_vcol = 0;
2130 new_cursor_col = -1;
2131 ptr = ml_get_curline();
2132 while (vcol <= (int)curwin->w_virtcol)
2133 {
2134 last_vcol = vcol;
2135#ifdef FEAT_MBYTE
2136 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002137 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 else
2139#endif
2140 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002141 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 vcol = last_vcol;
2144
2145 /*
2146 * May need to insert spaces to be able to position the cursor on
2147 * the right screen column.
2148 */
2149 if (vcol != (int)curwin->w_virtcol)
2150 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002151 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002153 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (ptr != NULL)
2155 {
2156 new_cursor_col += i;
2157 ptr[i] = NUL;
2158 while (--i >= 0)
2159 ptr[i] = ' ';
2160 ins_str(ptr);
2161 vim_free(ptr);
2162 }
2163 }
2164
2165 /*
2166 * When changing the indent while the cursor is in it, reset
2167 * Insstart_col to 0.
2168 */
2169 insstart_less = MAXCOL;
2170 }
2171
2172 curwin->w_p_list = save_p_list;
2173
2174 if (new_cursor_col <= 0)
2175 curwin->w_cursor.col = 0;
2176 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002177 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 curwin->w_set_curswant = TRUE;
2179 changed_cline_bef_curs();
2180
2181 /*
2182 * May have to adjust the start of the insert.
2183 */
2184 if (State & INSERT)
2185 {
2186 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2187 {
2188 if ((int)Insstart.col <= insstart_less)
2189 Insstart.col = 0;
2190 else
2191 Insstart.col -= insstart_less;
2192 }
2193 if ((int)ai_col <= insstart_less)
2194 ai_col = 0;
2195 else
2196 ai_col -= insstart_less;
2197 }
2198
2199 /*
2200 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2201 * If the number of characters before the cursor decreased, need to pop a
2202 * few characters from the replace stack.
2203 * If the number of characters before the cursor increased, need to push a
2204 * few NULs onto the replace stack.
2205 */
2206 if (REPLACE_NORMAL(State) && start_col >= 0)
2207 {
2208 while (start_col > (int)curwin->w_cursor.col)
2209 {
2210 replace_join(0); /* remove a NUL from the replace stack */
2211 --start_col;
2212 }
2213 while (start_col < (int)curwin->w_cursor.col || replaced)
2214 {
2215 replace_push(NUL);
2216 if (replaced)
2217 {
2218 replace_push(replaced);
2219 replaced = NUL;
2220 }
2221 ++start_col;
2222 }
2223 }
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 /*
2226 * For VREPLACE mode, we also have to fix the replace stack. In this case
2227 * it is always possible because we backspace over the whole line and then
2228 * put it back again the way we wanted it.
2229 */
2230 if (State & VREPLACE_FLAG)
2231 {
2232 /* If orig_line didn't allocate, just return. At least we did the job,
2233 * even if you can't backspace. */
2234 if (orig_line == NULL)
2235 return;
2236
2237 /* Save new line */
2238 new_line = vim_strsave(ml_get_curline());
2239 if (new_line == NULL)
2240 return;
2241
2242 /* We only put back the new line up to the cursor */
2243 new_line[curwin->w_cursor.col] = NUL;
2244
2245 /* Put back original line */
2246 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2247 curwin->w_cursor.col = orig_col;
2248
2249 /* Backspace from cursor to start of line */
2250 backspace_until_column(0);
2251
2252 /* Insert new stuff into line again */
2253 ins_bytes(new_line);
2254
2255 vim_free(new_line);
2256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257}
2258
2259/*
2260 * Truncate the space at the end of a line. This is to be used only in an
2261 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2262 * modes.
2263 */
2264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002265truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 int i;
2268
2269 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002270 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 if (State & REPLACE_FLAG)
2273 replace_join(0); /* remove a NUL from the replace stack */
2274 }
2275 line[i + 1] = NUL;
2276}
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278/*
2279 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2280 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002281 * Will attempt not to go before "col" even when there is a composing
2282 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
2284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002285backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
2287 while ((int)curwin->w_cursor.col > col)
2288 {
2289 curwin->w_cursor.col--;
2290 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002291 replace_do_bs(col);
2292 else if (!del_char_after_col(col))
2293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002297/*
2298 * Like del_char(), but make sure not to go before column "limit_col".
2299 * Only matters when there are composing characters.
2300 * Return TRUE when something was deleted.
2301 */
2302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002303del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002304{
2305#ifdef FEAT_MBYTE
2306 if (enc_utf8 && limit_col >= 0)
2307 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002308 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002309
2310 /* Make sure the cursor is at the start of a character, but
2311 * skip forward again when going too far back because of a
2312 * composing character. */
2313 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002314 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002315 {
2316 int l = utf_ptr2len(ml_get_cursor());
2317
2318 if (l == 0) /* end of line */
2319 break;
2320 curwin->w_cursor.col += l;
2321 }
2322 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2323 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002324 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002325 }
2326 else
2327#endif
2328 (void)del_char(FALSE);
2329 return TRUE;
2330}
2331
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2333/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002334 * CTRL-X pressed in Insert mode.
2335 */
2336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002337ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002338{
2339 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2340 * CTRL-V works like CTRL-N */
2341 if (ctrl_x_mode != CTRL_X_CMDLINE)
2342 {
2343 /* if the next ^X<> won't ADD nothing, then reset
2344 * compl_cont_status */
2345 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002346 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002347 else
2348 compl_cont_status = 0;
2349 /* We're not sure which CTRL-X mode it will be yet */
2350 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2351 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2352 edit_submode_pre = NULL;
2353 showmode();
2354 }
2355}
2356
2357/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002358 * Whether other than default completion has been selected.
2359 */
2360 int
2361ctrl_x_mode_not_default(void)
2362{
2363 return ctrl_x_mode != CTRL_X_NORMAL;
2364}
2365
2366/*
2367 * Whether CTRL-X was typed without a following character.
2368 */
2369 int
2370ctrl_x_mode_not_defined_yet(void)
2371{
2372 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2373}
2374
2375/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002376 * Return TRUE if the 'dict' or 'tsr' option can be used.
2377 */
2378 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002379has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002380{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002381 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002382# ifdef FEAT_SPELL
2383 && !curwin->w_p_spell
2384# endif
2385 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002386 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2387 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002388 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002389 edit_submode = NULL;
2390 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2391 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002392 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002393 if (emsg_silent == 0)
2394 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002395 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002396 setcursor();
2397 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002398#ifdef FEAT_EVAL
2399 if (!get_vim_var_nr(VV_TESTING))
2400#endif
2401 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002402 }
2403 return FALSE;
2404 }
2405 return TRUE;
2406}
2407
2408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2410 * This depends on the current mode.
2411 */
2412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002413vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
Bram Moolenaar4dbc2622018-11-02 11:59:15 +01002415 // Always allow ^R - let its results then be checked
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (c == Ctrl_R)
2417 return TRUE;
2418
Bram Moolenaare3226be2005-12-18 22:10:00 +00002419 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002420 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002421 return TRUE;
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 switch (ctrl_x_mode)
2424 {
2425 case 0: /* Not in any CTRL-X mode */
2426 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2427 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002428 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2430 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2431 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002432 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002433 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CTRL_X_SCROLL:
2435 return (c == Ctrl_Y || c == Ctrl_E);
2436 case CTRL_X_WHOLE_LINE:
2437 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2438 case CTRL_X_FILES:
2439 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2440 case CTRL_X_DICTIONARY:
2441 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2442 case CTRL_X_THESAURUS:
2443 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2444 case CTRL_X_TAGS:
2445 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2446#ifdef FEAT_FIND_ID
2447 case CTRL_X_PATH_PATTERNS:
2448 return (c == Ctrl_P || c == Ctrl_N);
2449 case CTRL_X_PATH_DEFINES:
2450 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2451#endif
2452 case CTRL_X_CMDLINE:
2453 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2454 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002455#ifdef FEAT_COMPL_FUNC
2456 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002457 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002458 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002459 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002460#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002461 case CTRL_X_SPELL:
2462 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002463 case CTRL_X_EVAL:
2464 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002466 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 return FALSE;
2468}
2469
2470/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002471 * Return TRUE when character "c" is part of the item currently being
2472 * completed. Used to decide whether to abandon complete mode when the menu
2473 * is visible.
2474 */
2475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002476ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002477{
2478 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2479 /* When expanding an identifier only accept identifier chars. */
2480 return vim_isIDc(c);
2481
2482 switch (ctrl_x_mode)
2483 {
2484 case CTRL_X_FILES:
2485 /* When expanding file name only accept file name chars. But not
2486 * path separators, so that "proto/<Tab>" expands files in
2487 * "proto", not "proto/" as a whole */
2488 return vim_isfilec(c) && !vim_ispathsep(c);
2489
2490 case CTRL_X_CMDLINE:
2491 case CTRL_X_OMNI:
2492 /* Command line and Omni completion can work with just about any
2493 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002494 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002495
2496 case CTRL_X_WHOLE_LINE:
2497 /* For while line completion a space can be part of the line. */
2498 return vim_isprintc(c);
2499 }
2500 return vim_iswordc(c);
2501}
2502
2503/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002504 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002506 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 * the rest of the word to be in -- webb
2508 */
2509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002510ins_compl_add_infercase(
2511 char_u *str,
2512 int len,
2513 int icase,
2514 char_u *fname,
2515 int dir,
2516 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002518 char_u *p;
2519 int i, c;
2520 int actual_len; /* Take multi-byte characters */
2521 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002522 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002523 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 int has_lower = FALSE;
2525 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002527 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002529 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaara2993e12007-08-12 14:38:46 +00002531 /* Find actual length of completion. */
2532#ifdef FEAT_MBYTE
2533 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002535 p = str;
2536 actual_len = 0;
2537 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002539 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002540 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
2542 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002543 else
2544#endif
2545 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaara2993e12007-08-12 14:38:46 +00002547 /* Find actual length of original text. */
2548#ifdef FEAT_MBYTE
2549 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002551 p = compl_orig_text;
2552 actual_compl_length = 0;
2553 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002555 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002556 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
2558 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002559 else
2560#endif
2561 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002563 /* "actual_len" may be smaller than "actual_compl_length" when using
2564 * thesaurus, only use the minimum when comparing. */
2565 min_len = actual_len < actual_compl_length
2566 ? actual_len : actual_compl_length;
2567
Bram Moolenaara2993e12007-08-12 14:38:46 +00002568 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002569 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002570 if (wca != NULL)
2571 {
2572 p = str;
2573 for (i = 0; i < actual_len; ++i)
2574#ifdef FEAT_MBYTE
2575 if (has_mbyte)
2576 wca[i] = mb_ptr2char_adv(&p);
2577 else
2578#endif
2579 wca[i] = *(p++);
2580
2581 /* Rule 1: Were any chars converted to lower? */
2582 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002583 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002584 {
2585#ifdef FEAT_MBYTE
2586 if (has_mbyte)
2587 c = mb_ptr2char_adv(&p);
2588 else
2589#endif
2590 c = *(p++);
2591 if (MB_ISLOWER(c))
2592 {
2593 has_lower = TRUE;
2594 if (MB_ISUPPER(wca[i]))
2595 {
2596 /* Rule 1 is satisfied. */
2597 for (i = actual_compl_length; i < actual_len; ++i)
2598 wca[i] = MB_TOLOWER(wca[i]);
2599 break;
2600 }
2601 }
2602 }
2603
2604 /*
2605 * Rule 2: No lower case, 2nd consecutive letter converted to
2606 * upper case.
2607 */
2608 if (!has_lower)
2609 {
2610 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002611 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002612 {
2613#ifdef FEAT_MBYTE
2614 if (has_mbyte)
2615 c = mb_ptr2char_adv(&p);
2616 else
2617#endif
2618 c = *(p++);
2619 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2620 {
2621 /* Rule 2 is satisfied. */
2622 for (i = actual_compl_length; i < actual_len; ++i)
2623 wca[i] = MB_TOUPPER(wca[i]);
2624 break;
2625 }
2626 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2627 }
2628 }
2629
2630 /* Copy the original case of the part we typed. */
2631 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002632 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002633 {
2634#ifdef FEAT_MBYTE
2635 if (has_mbyte)
2636 c = mb_ptr2char_adv(&p);
2637 else
2638#endif
2639 c = *(p++);
2640 if (MB_ISLOWER(c))
2641 wca[i] = MB_TOLOWER(wca[i]);
2642 else if (MB_ISUPPER(c))
2643 wca[i] = MB_TOUPPER(wca[i]);
2644 }
2645
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002646 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002647 * Generate encoding specific output from wide character array.
2648 * Multi-byte characters can occupy up to five bytes more than
2649 * ASCII characters, and we also need one byte for NUL, so stay
2650 * six bytes away from the edge of IObuff.
2651 */
2652 p = IObuff;
2653 i = 0;
2654 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2655#ifdef FEAT_MBYTE
2656 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002657 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002658 else
2659#endif
2660 *(p++) = wca[i++];
2661 *p = NUL;
2662
2663 vim_free(wca);
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002666 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2667 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002669 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670}
2671
2672/*
2673 * Add a match to the list of matches.
2674 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002675 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002676 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002679ins_compl_add(
2680 char_u *str,
2681 int len,
2682 int icase,
2683 char_u *fname,
2684 char_u **cptext, /* extra text for popup menu or NULL */
2685 int cdir,
2686 int flags,
2687 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002689 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002690 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 ui_breakcheck();
2693 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002694 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (len < 0)
2696 len = (int)STRLEN(str);
2697
2698 /*
2699 * If the same match is already present, don't add it.
2700 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002701 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002703 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 do
2705 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002706 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002707 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002708 && match->cp_str[len] == NUL)
2709 return NOTDONE;
2710 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002711 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002714 /* Remove any popup menu before changing the list of matches. */
2715 ins_compl_del_pum();
2716
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 /*
2718 * Allocate a new match structure.
2719 * Copy the values to the new match structure.
2720 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002721 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002723 return FAIL;
2724 match->cp_number = -1;
2725 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002726 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002727 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002730 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002732 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002735 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2737 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002738 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002739 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002740 && compl_curr_match->cp_fname != NULL
2741 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002742 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002743 else if (fname != NULL)
2744 {
2745 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002746 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002749 match->cp_fname = NULL;
2750 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002751
2752 if (cptext != NULL)
2753 {
2754 int i;
2755
2756 for (i = 0; i < CPT_COUNT; ++i)
2757 if (cptext[i] != NULL && *cptext[i] != NUL)
2758 match->cp_text[i] = vim_strsave(cptext[i]);
2759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 /*
2762 * Link the new match structure in the list of matches.
2763 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002764 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002765 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else if (dir == FORWARD)
2767 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002768 match->cp_next = compl_curr_match->cp_next;
2769 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 else /* BACKWARD */
2772 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002773 match->cp_next = compl_curr_match;
2774 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002776 if (match->cp_next)
2777 match->cp_next->cp_prev = match;
2778 if (match->cp_prev)
2779 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002781 compl_first_match = match;
2782 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002784 /*
2785 * Find the longest common string if still doing that.
2786 */
2787 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2788 ins_compl_longest_match(match);
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 return OK;
2791}
2792
2793/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002794 * Return TRUE if "str[len]" matches with match->cp_str, considering
2795 * match->cp_icase.
2796 */
2797 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002798ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002799{
2800 if (match->cp_icase)
2801 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2802 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2803}
2804
2805/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002806 * Reduce the longest common string for match "match".
2807 */
2808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002809ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002810{
2811 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002812 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002813 int had_match;
2814
2815 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002816 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002817 /* First match, use it as a whole. */
2818 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002819 if (compl_leader != NULL)
2820 {
2821 had_match = (curwin->w_cursor.col > compl_col);
2822 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002823 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002824 ins_redraw(FALSE);
2825
2826 /* When the match isn't there (to avoid matching itself) remove it
2827 * again after redrawing. */
2828 if (!had_match)
2829 ins_compl_delete();
2830 compl_used_match = FALSE;
2831 }
2832 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002833 else
2834 {
2835 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002836 p = compl_leader;
2837 s = match->cp_str;
2838 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002839 {
2840#ifdef FEAT_MBYTE
2841 if (has_mbyte)
2842 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002843 c1 = mb_ptr2char(p);
2844 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002845 }
2846 else
2847#endif
2848 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002849 c1 = *p;
2850 c2 = *s;
2851 }
2852 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2853 : (c1 != c2))
2854 break;
2855#ifdef FEAT_MBYTE
2856 if (has_mbyte)
2857 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002858 MB_PTR_ADV(p);
2859 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002860 }
2861 else
2862#endif
2863 {
2864 ++p;
2865 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002866 }
2867 }
2868
2869 if (*p != NUL)
2870 {
2871 /* Leader was shortened, need to change the inserted text. */
2872 *p = NUL;
2873 had_match = (curwin->w_cursor.col > compl_col);
2874 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002875 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002876 ins_redraw(FALSE);
2877
2878 /* When the match isn't there (to avoid matching itself) remove it
2879 * again after redrawing. */
2880 if (!had_match)
2881 ins_compl_delete();
2882 }
2883
2884 compl_used_match = FALSE;
2885 }
2886}
2887
2888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 * Add an array of matches to the list of matches.
2890 * Frees matches[].
2891 */
2892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893ins_compl_add_matches(
2894 int num_matches,
2895 char_u **matches,
2896 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
2898 int i;
2899 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002900 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
Bram Moolenaar572cb562005-08-05 21:35:02 +00002902 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002903 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002904 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002906 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 FreeWild(num_matches, matches);
2908}
2909
2910/* Make the completion list cyclic.
2911 * Return the number of matches (excluding the original).
2912 */
2913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002916 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 int count = 0;
2918
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002919 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
2921 /*
2922 * Find the end of the list.
2923 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002924 match = compl_first_match;
2925 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002926 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002928 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 ++count;
2930 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002931 match->cp_next = compl_first_match;
2932 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934 return count;
2935}
2936
Bram Moolenaara94bc432006-03-10 21:42:59 +00002937/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002938 * Set variables that store noselect and noinsert behavior from the
2939 * 'completeopt' value.
2940 */
2941 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002942completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002943{
2944 compl_no_insert = FALSE;
2945 compl_no_select = FALSE;
2946 if (strstr((char *)p_cot, "noselect") != NULL)
2947 compl_no_select = TRUE;
2948 if (strstr((char *)p_cot, "noinsert") != NULL)
2949 compl_no_insert = TRUE;
2950}
2951
2952/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002953 * Start completion for the complete() function.
2954 * "startcol" is where the matched text starts (1 is first column).
2955 * "list" is the list of matches.
2956 */
2957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002958set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002959{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002960 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002961 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002962
Bram Moolenaara94bc432006-03-10 21:42:59 +00002963 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002964 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002965 ins_compl_prep(' ');
2966 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002967 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002968
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002969 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002970 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002971 startcol = curwin->w_cursor.col;
2972 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002973 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002974 /* compl_pattern doesn't need to be set */
2975 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2976 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002977 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002978 return;
2979
Bram Moolenaare4214502015-03-05 18:08:43 +01002980 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002981
2982 ins_compl_add_list(list);
2983 compl_matches = ins_compl_make_cyclic();
2984 compl_started = TRUE;
2985 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002986 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002987
2988 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002989 if (compl_no_insert || compl_no_select)
2990 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002991 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002992 if (compl_no_select)
2993 /* Down/Up has no real effect. */
2994 ins_complete(K_UP, FALSE);
2995 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002996 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002997 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002998 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002999
3000 /* Lazily show the popup menu, unless we got interrupted. */
3001 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003002 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003003 out_flush();
3004}
3005
3006
Bram Moolenaar9372a112005-12-06 19:59:18 +00003007/* "compl_match_array" points the currently displayed list of entries in the
3008 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003009static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003010static int compl_match_arraysize;
3011
3012/*
3013 * Update the screen and when there is any scrolling remove the popup menu.
3014 */
3015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003016ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003017{
3018 int h;
3019
3020 if (compl_match_array != NULL)
3021 {
3022 h = curwin->w_cline_height;
3023 update_screen(0);
3024 if (h != curwin->w_cline_height)
3025 ins_compl_del_pum();
3026 }
3027}
3028
3029/*
3030 * Remove any popup menu.
3031 */
3032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003033ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003034{
3035 if (compl_match_array != NULL)
3036 {
3037 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003038 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003039 }
3040}
3041
3042/*
3043 * Return TRUE if the popup menu should be displayed.
3044 */
3045 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003046pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003047{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003048 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003049 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003050 return FALSE;
3051
3052 /* The display looks bad on a B&W display. */
3053 if (t_colors < 8
3054#ifdef FEAT_GUI
3055 && !gui.in_use
3056#endif
3057 )
3058 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003059 return TRUE;
3060}
3061
3062/*
3063 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003064 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003065 */
3066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003067pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003068{
3069 compl_T *compl;
3070 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003071
3072 /* Don't display the popup menu if there are no matches or there is only
3073 * one (ignoring the original text). */
3074 compl = compl_first_match;
3075 i = 0;
3076 do
3077 {
3078 if (compl == NULL
3079 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3080 break;
3081 compl = compl->cp_next;
3082 } while (compl != compl_first_match);
3083
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003084 if (strstr((char *)p_cot, "menuone") != NULL)
3085 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003086 return (i >= 2);
3087}
3088
3089/*
3090 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003091 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003092 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003094ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003095{
3096 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003097 compl_T *shown_compl = NULL;
3098 int did_find_shown_match = FALSE;
3099 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003100 int i;
3101 int cur = -1;
3102 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003103 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003104
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003105 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003106 return;
3107
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003108#if defined(FEAT_EVAL)
3109 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3110 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3111#endif
3112
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003113 /* Update the screen before drawing the popup menu over it. */
3114 update_screen(0);
3115
3116 if (compl_match_array == NULL)
3117 {
3118 /* Need to build the popup menu list. */
3119 compl_match_arraysize = 0;
3120 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003121 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003122 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003123 do
3124 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003125 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3126 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003127 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003128 ++compl_match_arraysize;
3129 compl = compl->cp_next;
3130 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003131 if (compl_match_arraysize == 0)
3132 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003133 compl_match_array = (pumitem_T *)alloc_clear(
3134 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003135 * compl_match_arraysize));
3136 if (compl_match_array != NULL)
3137 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003138 /* If the current match is the original text don't find the first
3139 * match after it, don't highlight anything. */
3140 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3141 shown_match_ok = TRUE;
3142
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003143 i = 0;
3144 compl = compl_first_match;
3145 do
3146 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003147 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3148 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003149 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003150 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003151 if (!shown_match_ok)
3152 {
3153 if (compl == compl_shown_match || did_find_shown_match)
3154 {
3155 /* This item is the shown match or this is the
3156 * first displayed item after the shown match. */
3157 compl_shown_match = compl;
3158 did_find_shown_match = TRUE;
3159 shown_match_ok = TRUE;
3160 }
3161 else
3162 /* Remember this displayed match for when the
3163 * shown match is just below it. */
3164 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003165 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003166 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003167
3168 if (compl->cp_text[CPT_ABBR] != NULL)
3169 compl_match_array[i].pum_text =
3170 compl->cp_text[CPT_ABBR];
3171 else
3172 compl_match_array[i].pum_text = compl->cp_str;
3173 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3174 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3175 if (compl->cp_text[CPT_MENU] != NULL)
3176 compl_match_array[i++].pum_extra =
3177 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003178 else
3179 compl_match_array[i++].pum_extra = compl->cp_fname;
3180 }
3181
3182 if (compl == compl_shown_match)
3183 {
3184 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003185
3186 /* When the original text is the shown match don't set
3187 * compl_shown_match. */
3188 if (compl->cp_flags & ORIGINAL_TEXT)
3189 shown_match_ok = TRUE;
3190
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003191 if (!shown_match_ok && shown_compl != NULL)
3192 {
3193 /* The shown match isn't displayed, set it to the
3194 * previously displayed match. */
3195 compl_shown_match = shown_compl;
3196 shown_match_ok = TRUE;
3197 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003198 }
3199 compl = compl->cp_next;
3200 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003201
3202 if (!shown_match_ok) /* no displayed match at all */
3203 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003204 }
3205 }
3206 else
3207 {
3208 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003209 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003210 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3211 || compl_match_array[i].pum_text
3212 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003213 {
3214 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003215 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003216 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003217 }
3218
3219 if (compl_match_array != NULL)
3220 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003221 /* In Replace mode when a $ is displayed at the end of the line only
3222 * part of the screen would be updated. We do need to redraw here. */
3223 dollar_vcol = -1;
3224
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003225 /* Compute the screen column of the start of the completed text.
3226 * Use the cursor to get all wrapping and other settings right. */
3227 col = curwin->w_cursor.col;
3228 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003229 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003230 curwin->w_cursor.col = col;
3231 }
3232}
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#define DICT_FIRST (1) /* use just first element in "dict" */
3235#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003236
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003238 * Add any identifiers that match the given pattern in the list of dictionary
3239 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 */
3241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003242ins_compl_dictionaries(
3243 char_u *dict_start,
3244 char_u *pat,
3245 int flags, /* DICT_FIRST and/or DICT_EXACT */
3246 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003248 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 char_u *ptr;
3250 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 char_u **files;
3253 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003255 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaar0b238792006-03-02 22:49:12 +00003257 if (*dict == NUL)
3258 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003259#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003260 /* When 'dictionary' is empty and spell checking is enabled use
3261 * "spell". */
3262 if (!thesaurus && curwin->w_p_spell)
3263 dict = (char_u *)"spell";
3264 else
3265#endif
3266 return;
3267 }
3268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003270 if (buf == NULL)
3271 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003272 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 /* If 'infercase' is set, don't use 'smartcase' here */
3275 save_p_scs = p_scs;
3276 if (curbuf->b_p_inf)
3277 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003278
3279 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3280 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003281 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003282 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003283 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003284 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003285 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003286
3287 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003288 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003289 len = STRLEN(pat_esc) + 10;
3290 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003291 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003292 {
3293 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003294 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003295 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003296 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003297 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3298 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003299 vim_free(ptr);
3300 }
3301 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003302 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003303 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003304 if (regmatch.regprog == NULL)
3305 goto theend;
3306 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003307
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3309 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003310 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 /* copy one dictionary file name into buf */
3313 if (flags == DICT_EXACT)
3314 {
3315 count = 1;
3316 files = &dict;
3317 }
3318 else
3319 {
3320 /* Expand wildcards in the dictionary name, but do not allow
3321 * backticks (for security, the 'dict' option may have been set in
3322 * a modeline). */
3323 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003324# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003325 if (!thesaurus && STRCMP(buf, "spell") == 0)
3326 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003327 else
3328# endif
3329 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 || expand_wildcards(1, &buf, &count, &files,
3331 EW_FILE|EW_SILENT) != OK)
3332 count = 0;
3333 }
3334
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003335# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003336 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003338 /* Complete from active spelling. Skip "\<" in the pattern, we
3339 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003340 if (pat[0] == '\\' && pat[1] == '<')
3341 ptr = pat + 2;
3342 else
3343 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003344 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003346 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003347# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003348 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003349 {
3350 ins_compl_files(count, files, thesaurus, flags,
3351 &regmatch, buf, &dir);
3352 if (flags != DICT_EXACT)
3353 FreeWild(count, files);
3354 }
3355 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 break;
3357 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003358
3359theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003361 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 vim_free(buf);
3363}
3364
Bram Moolenaar0b238792006-03-02 22:49:12 +00003365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003366ins_compl_files(
3367 int count,
3368 char_u **files,
3369 int thesaurus,
3370 int flags,
3371 regmatch_T *regmatch,
3372 char_u *buf,
3373 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003374{
3375 char_u *ptr;
3376 int i;
3377 FILE *fp;
3378 int add_r;
3379
3380 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3381 {
3382 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3383 if (flags != DICT_EXACT)
3384 {
3385 vim_snprintf((char *)IObuff, IOSIZE,
3386 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003387 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003388 }
3389
3390 if (fp != NULL)
3391 {
3392 /*
3393 * Read dictionary file line by line.
3394 * Check each line for a match.
3395 */
3396 while (!got_int && !compl_interrupted
3397 && !vim_fgets(buf, LSIZE, fp))
3398 {
3399 ptr = buf;
3400 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3401 {
3402 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003403 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003404 ptr = find_line_end(ptr);
3405 else
3406 ptr = find_word_end(ptr);
3407 add_r = ins_compl_add_infercase(regmatch->startp[0],
3408 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003409 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003410 if (thesaurus)
3411 {
3412 char_u *wstart;
3413
3414 /*
3415 * Add the other matches on the line
3416 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003417 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003418 while (!got_int)
3419 {
3420 /* Find start of the next word. Skip white
3421 * space and punctuation. */
3422 ptr = find_word_start(ptr);
3423 if (*ptr == NUL || *ptr == NL)
3424 break;
3425 wstart = ptr;
3426
Bram Moolenaara2993e12007-08-12 14:38:46 +00003427 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003428#ifdef FEAT_MBYTE
3429 if (has_mbyte)
3430 /* Japanese words may have characters in
3431 * different classes, only separate words
3432 * with single-byte non-word characters. */
3433 while (*ptr != NUL)
3434 {
3435 int l = (*mb_ptr2len)(ptr);
3436
3437 if (l < 2 && !vim_iswordc(*ptr))
3438 break;
3439 ptr += l;
3440 }
3441 else
3442#endif
3443 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003444
3445 /* Add the word. Skip the regexp match. */
3446 if (wstart != regmatch->startp[0])
3447 add_r = ins_compl_add_infercase(wstart,
3448 (int)(ptr - wstart),
3449 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003450 }
3451 }
3452 if (add_r == OK)
3453 /* if dir was BACKWARD then honor it just once */
3454 *dir = FORWARD;
3455 else if (add_r == FAIL)
3456 break;
3457 /* avoid expensive call to vim_regexec() when at end
3458 * of line */
3459 if (*ptr == '\n' || got_int)
3460 break;
3461 }
3462 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003463 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003464 }
3465 fclose(fp);
3466 }
3467 }
3468}
3469
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470/*
3471 * Find the start of the next word.
3472 * Returns a pointer to the first char of the word. Also stops at a NUL.
3473 */
3474 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003475find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477#ifdef FEAT_MBYTE
3478 if (has_mbyte)
3479 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003480 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 else
3482#endif
3483 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3484 ++ptr;
3485 return ptr;
3486}
3487
3488/*
3489 * Find the end of the word. Assumes it starts inside a word.
3490 * Returns a pointer to just after the word.
3491 */
3492 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495#ifdef FEAT_MBYTE
3496 int start_class;
3497
3498 if (has_mbyte)
3499 {
3500 start_class = mb_get_class(ptr);
3501 if (start_class > 1)
3502 while (*ptr != NUL)
3503 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003504 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 if (mb_get_class(ptr) != start_class)
3506 break;
3507 }
3508 }
3509 else
3510#endif
3511 while (vim_iswordc(*ptr))
3512 ++ptr;
3513 return ptr;
3514}
3515
3516/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 * Find the end of the line, omitting CR and NL at the end.
3518 * Returns a pointer to just after the line.
3519 */
3520 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003521find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003522{
3523 char_u *s;
3524
3525 s = ptr + STRLEN(ptr);
3526 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3527 --s;
3528 return s;
3529}
3530
3531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 * Free the list of completions
3533 */
3534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003535ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003537 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003538 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
Bram Moolenaard23a8232018-02-10 18:45:26 +01003540 VIM_CLEAR(compl_pattern);
3541 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003545
3546 ins_compl_del_pum();
3547 pum_clear();
3548
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003549 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 do
3551 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003552 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003553 compl_curr_match = compl_curr_match->cp_next;
3554 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003556 if (match->cp_flags & FREE_FNAME)
3557 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003558 for (i = 0; i < CPT_COUNT; ++i)
3559 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003561 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3562 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003563 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003564 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565}
3566
3567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003570 compl_cont_status = 0;
3571 compl_started = FALSE;
3572 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003573 VIM_CLEAR(compl_pattern);
3574 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003576 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003577 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003578 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003579 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580}
3581
3582/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003583 * Return TRUE when Insert completion is active.
3584 */
3585 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003586ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003587{
3588 return compl_started;
3589}
3590
3591/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003592 * Delete one character before the cursor and show the subset of the matches
3593 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003594 * Returns the character to be used, NUL if the work is done and another char
3595 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003596 */
3597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003598ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003599{
3600 char_u *line;
3601 char_u *p;
3602
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003603 line = ml_get_curline();
3604 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003605 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003606
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003607 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003608 * allow the word to be deleted, we won't match everything.
3609 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003610 if ((int)(p - line) - (int)compl_col < 0
3611 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003612 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3613 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3614 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003615 return K_BS;
3616
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003617 /* Deleted more than what was used to find matches or didn't finish
3618 * finding all matches: need to look for matches all over again. */
3619 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003620 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003621 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003622
Bram Moolenaara6557602006-02-04 22:43:20 +00003623 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003624 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003625 if (compl_leader != NULL)
3626 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003627 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003628 if (compl_shown_match != NULL)
3629 /* Make sure current match is not a hidden item. */
3630 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003631 return NUL;
3632 }
3633 return K_BS;
3634}
Bram Moolenaara6557602006-02-04 22:43:20 +00003635
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003636/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003637 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3638 * be called.
3639 */
3640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003642{
3643 /* Return TRUE if we didn't complete finding matches or when the
3644 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3645 return compl_was_interrupted
3646 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3647 && compl_opt_refresh_always);
3648}
3649
3650/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003651 * Called after changing "compl_leader".
3652 * Show the popup menu with a different set of matches.
3653 * May also search for matches again if the previous search was interrupted.
3654 */
3655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003657{
3658 ins_compl_del_pum();
3659 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003660 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003661 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003662
3663 if (compl_started)
3664 ins_compl_set_original_text(compl_leader);
3665 else
3666 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003667#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003668 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003669#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003670 /*
3671 * Matches were cleared, need to search for them now. First display
3672 * the changed text before the cursor. Set "compl_restarting" to
3673 * avoid that the first match is inserted.
3674 */
3675 update_screen(0);
3676#ifdef FEAT_GUI
3677 if (gui.in_use)
3678 {
3679 /* Show the cursor after the match, not after the redrawn text. */
3680 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003681 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003682 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003683#endif
3684 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003685 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003686 compl_cont_status = 0;
3687 compl_restarting = FALSE;
3688 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003689
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003690 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003691
3692 /* Show the popup menu with a different set of matches. */
3693 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003694
3695 /* Don't let Enter select the original text when there is no popup menu. */
3696 if (compl_match_array == NULL)
3697 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003698}
3699
3700/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003701 * Return the length of the completion, from the completion start column to
3702 * the cursor column. Making sure it never goes below zero.
3703 */
3704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003706{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003707 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003708
3709 if (off < 0)
3710 return 0;
3711 return off;
3712}
3713
3714/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003715 * Append one character to the match leader. May reduce the number of
3716 * matches.
3717 */
3718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003719ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003720{
3721#ifdef FEAT_MBYTE
3722 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003723#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003724
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003725 if (stop_arrow() == FAIL)
3726 return;
3727#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003728 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3729 {
3730 char_u buf[MB_MAXBYTES + 1];
3731
3732 (*mb_char2bytes)(c, buf);
3733 buf[cc] = NUL;
3734 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003735 if (compl_opt_refresh_always)
3736 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003737 }
3738 else
3739#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003740 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003741 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003742 if (compl_opt_refresh_always)
3743 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003744 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003745
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003746 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003747 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003748 ins_compl_restart();
3749
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003750 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003751 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003752 * break redo. */
3753 if (!compl_opt_refresh_always)
3754 {
3755 vim_free(compl_leader);
3756 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003757 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003758 if (compl_leader != NULL)
3759 ins_compl_new_leader();
3760 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003761}
3762
3763/*
3764 * Setup for finding completions again without leaving CTRL-X mode. Used when
3765 * BS or a key was typed while still searching for matches.
3766 */
3767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003768ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003769{
3770 ins_compl_free();
3771 compl_started = FALSE;
3772 compl_matches = 0;
3773 compl_cont_status = 0;
3774 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003775}
3776
3777/*
3778 * Set the first match, the original text.
3779 */
3780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003782{
3783 char_u *p;
3784
Bram Moolenaare87edf32018-04-17 22:14:32 +02003785 /* Replace the original text entry.
3786 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3787 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003788 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3789 {
3790 p = vim_strsave(str);
3791 if (p != NULL)
3792 {
3793 vim_free(compl_first_match->cp_str);
3794 compl_first_match->cp_str = p;
3795 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003796 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003797 else if (compl_first_match->cp_prev != NULL
3798 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3799 {
3800 p = vim_strsave(str);
3801 if (p != NULL)
3802 {
3803 vim_free(compl_first_match->cp_prev->cp_str);
3804 compl_first_match->cp_prev->cp_str = p;
3805 }
3806 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003807}
3808
3809/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003810 * Append one character to the match leader. May reduce the number of
3811 * matches.
3812 */
3813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003814ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003815{
3816 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003817 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003818 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003819 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003820
3821 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003822 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003823 {
3824 /* When still at the original match use the first entry that matches
3825 * the leader. */
3826 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3827 {
3828 p = NULL;
3829 for (cp = compl_shown_match->cp_next; cp != NULL
3830 && cp != compl_first_match; cp = cp->cp_next)
3831 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003832 if (compl_leader == NULL
3833 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003834 (int)STRLEN(compl_leader)))
3835 {
3836 p = cp->cp_str;
3837 break;
3838 }
3839 }
3840 if (p == NULL || (int)STRLEN(p) <= len)
3841 return;
3842 }
3843 else
3844 return;
3845 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003846 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003847 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003848 ins_compl_addleader(c);
3849}
3850
3851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003853 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003854 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003857ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858{
3859 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003861 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 /* Forget any previous 'special' messages if this is actually
3864 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3865 */
3866 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3867 edit_submode_extra = NULL;
3868
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003869 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003870 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3871 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003872 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003874 /* Set "compl_get_longest" when finding the first matches. */
3875 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003876 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003877 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003878 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003879 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003880
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003881 }
3882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3884 {
3885 /*
3886 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3887 * it will be yet. Now we decide.
3888 */
3889 switch (c)
3890 {
3891 case Ctrl_E:
3892 case Ctrl_Y:
3893 ctrl_x_mode = CTRL_X_SCROLL;
3894 if (!(State & REPLACE_FLAG))
3895 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3896 else
3897 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3898 edit_submode_pre = NULL;
3899 showmode();
3900 break;
3901 case Ctrl_L:
3902 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3903 break;
3904 case Ctrl_F:
3905 ctrl_x_mode = CTRL_X_FILES;
3906 break;
3907 case Ctrl_K:
3908 ctrl_x_mode = CTRL_X_DICTIONARY;
3909 break;
3910 case Ctrl_R:
3911 /* Simply allow ^R to happen without affecting ^X mode */
3912 break;
3913 case Ctrl_T:
3914 ctrl_x_mode = CTRL_X_THESAURUS;
3915 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003916#ifdef FEAT_COMPL_FUNC
3917 case Ctrl_U:
3918 ctrl_x_mode = CTRL_X_FUNCTION;
3919 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003920 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003921 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003922 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003923#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003924 case 's':
3925 case Ctrl_S:
3926 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003927#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003928 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003929 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003930 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003931#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 case Ctrl_RSB:
3934 ctrl_x_mode = CTRL_X_TAGS;
3935 break;
3936#ifdef FEAT_FIND_ID
3937 case Ctrl_I:
3938 case K_S_TAB:
3939 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3940 break;
3941 case Ctrl_D:
3942 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3943 break;
3944#endif
3945 case Ctrl_V:
3946 case Ctrl_Q:
3947 ctrl_x_mode = CTRL_X_CMDLINE;
3948 break;
3949 case Ctrl_P:
3950 case Ctrl_N:
3951 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3952 * just started ^X mode, or there were enough ^X's to cancel
3953 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3954 * do normal expansion when interrupting a different mode (say
3955 * ^X^F^X^P or ^P^X^X^P, see below)
3956 * nothing changes if interrupting mode 0, (eg, the flag
3957 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003958 if (!(compl_cont_status & CONT_INTRPT))
3959 compl_cont_status |= CONT_LOCAL;
3960 else if (compl_cont_mode != 0)
3961 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 /* FALLTHROUGH */
3963 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003964 /* If we have typed at least 2 ^X's... for modes != 0, we set
3965 * compl_cont_status = 0 (eg, as if we had just started ^X
3966 * mode).
3967 * For mode 0, we set "compl_cont_mode" to an impossible
3968 * value, in both cases ^X^X can be used to restart the same
3969 * mode (avoiding ADDING mode).
3970 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3971 * 'complete' and local ^P expansions respectively.
3972 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3973 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (c == Ctrl_X)
3975 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003976 if (compl_cont_mode != 0)
3977 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003979 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003981 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 edit_submode = NULL;
3983 showmode();
3984 break;
3985 }
3986 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003987 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
3989 /* We're already in CTRL-X mode, do we stay in it? */
3990 if (!vim_is_ctrl_x_key(c))
3991 {
3992 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003993 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 else
3995 ctrl_x_mode = CTRL_X_FINISHED;
3996 edit_submode = NULL;
3997 }
3998 showmode();
3999 }
4000
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004001 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {
4003 /* Show error message from attempted keyword completion (probably
4004 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004005 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004007 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4008 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 || ctrl_x_mode == CTRL_X_FINISHED)
4010 {
4011 /* Get here when we have finished typing a sequence of ^N and
4012 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004013 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004014 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004017 * If any of the original typed text has been changed, eg when
4018 * ignorecase is set, we must add back-spaces to the redo
4019 * buffer. We add as few as necessary to delete just the part
4020 * of the original text that has changed.
4021 * When using the longest match, edited the match or used
4022 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004024 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4025 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004026 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004027 ptr = NULL;
4028 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030
4031#ifdef FEAT_CINDENT
4032 want_cindent = (can_cindent && cindent_on());
4033#endif
4034 /*
4035 * When completing whole lines: fix indent for 'cindent'.
4036 * Otherwise, break line if it's too long.
4037 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004038 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
4040#ifdef FEAT_CINDENT
4041 /* re-indent the current line */
4042 if (want_cindent)
4043 {
4044 do_c_expr_indent();
4045 want_cindent = FALSE; /* don't do it again */
4046 }
4047#endif
4048 }
4049 else
4050 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004051 int prev_col = curwin->w_cursor.col;
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004054 if (prev_col > 0)
4055 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004056 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004057 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004059 if (prev_col > 0
4060 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4061 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004064 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004065 * the selection without inserting anything. When
4066 * compl_enter_selects is set the Enter key does the same. */
4067 if ((c == Ctrl_Y || (compl_enter_selects
4068 && (c == CAR || c == K_KENTER || c == NL)))
4069 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004070 retval = TRUE;
4071
Bram Moolenaar47247282016-08-02 22:36:02 +02004072 /* CTRL-E means completion is Ended, go back to the typed text.
4073 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004074 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004075 {
4076 ins_compl_delete();
4077 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004078 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004079 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004080 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004081 retval = TRUE;
4082 }
4083
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004084 auto_format(FALSE, TRUE);
4085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004087 compl_started = FALSE;
4088 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004089 if (!shortmess(SHM_COMPLETIONMENU))
4090 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004091 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004092 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (edit_submode != NULL)
4094 {
4095 edit_submode = NULL;
4096 showmode();
4097 }
4098
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004099#ifdef FEAT_CMDWIN
4100 if (c == Ctrl_C && cmdwin_type != 0)
4101 /* Avoid the popup menu remains displayed when leaving the
4102 * command line window. */
4103 update_screen(0);
4104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#ifdef FEAT_CINDENT
4106 /*
4107 * Indent now if a key was typed that is in 'cinkeys'.
4108 */
4109 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4110 do_c_expr_indent();
4111#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004112 /* Trigger the CompleteDone event to give scripts a chance to act
4113 * upon the completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004114 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004117 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4118 /* Trigger the CompleteDone event to give scripts a chance to act
4119 * upon the (possibly failed) completion. */
Bram Moolenaar9fa95062018-08-08 22:08:32 +02004120 ins_apply_autocmds(EVENT_COMPLETEDONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
4122 /* reset continue_* if we left expansion-mode, if we stay they'll be
4123 * (re)set properly in ins_complete() */
4124 if (!vim_is_ctrl_x_key(c))
4125 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004126 compl_cont_status = 0;
4127 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004129
4130 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131}
4132
4133/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004134 * Fix the redo buffer for the completion leader replacing some of the typed
4135 * text. This inserts backspaces and appends the changed text.
4136 * "ptr" is the known leader text or NUL.
4137 */
4138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004139ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004140{
4141 int len;
4142 char_u *p;
4143 char_u *ptr = ptr_arg;
4144
4145 if (ptr == NULL)
4146 {
4147 if (compl_leader != NULL)
4148 ptr = compl_leader;
4149 else
4150 return; /* nothing to do */
4151 }
4152 if (compl_orig_text != NULL)
4153 {
4154 p = compl_orig_text;
4155 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4156 ;
4157#ifdef FEAT_MBYTE
4158 if (len > 0)
4159 len -= (*mb_head_off)(p, p + len);
4160#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004161 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004162 AppendCharToRedobuff(K_BS);
4163 }
4164 else
4165 len = 0;
4166 if (ptr != NULL)
4167 AppendToRedobuffLit(ptr + len, -1);
4168}
4169
4170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4172 * (depending on flag) starting from buf and looking for a non-scanned
4173 * buffer (other than curbuf). curbuf is special, if it is called with
4174 * buf=curbuf then it has to be the first call for a given flag/expansion.
4175 *
4176 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4177 */
4178 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004179ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 if (flag == 'w') /* just windows */
4184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 if (buf == curbuf) /* first call for this flag/expansion */
4186 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004187 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 && wp->w_buffer->b_scanned)
4189 ;
4190 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192 else
4193 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4194 * (unlisted buffers)
4195 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004196 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 && ((flag == 'U'
4198 ? buf->b_p_bl
4199 : (!buf->b_p_bl
4200 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004201 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 ;
4203 return buf;
4204}
4205
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004206#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004207/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004208 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004209 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004210 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004212expand_by_function(
4213 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4214 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004215{
Bram Moolenaar82139082011-09-14 16:52:09 +02004216 list_T *matchlist = NULL;
4217 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004218 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004219 char_u *funcname;
4220 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004221 win_T *curwin_save;
4222 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004223 typval_T rettv;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004224 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004225
Bram Moolenaare344bea2005-09-01 20:46:49 +00004226 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4227 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004228 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004229
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004230 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004231 args[0].v_type = VAR_NUMBER;
4232 args[0].vval.v_number = 0;
4233 args[1].v_type = VAR_STRING;
4234 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4235 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004236
Bram Moolenaare344bea2005-09-01 20:46:49 +00004237 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004238 curwin_save = curwin;
4239 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004240
4241 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004242 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004243 {
4244 switch (rettv.v_type)
4245 {
4246 case VAR_LIST:
4247 matchlist = rettv.vval.v_list;
4248 break;
4249 case VAR_DICT:
4250 matchdict = rettv.vval.v_dict;
4251 break;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004252 case VAR_SPECIAL:
4253 if (rettv.vval.v_number == VVAL_NONE)
4254 compl_opt_suppress_empty = TRUE;
4255 // FALLTHROUGH
Bram Moolenaar82139082011-09-14 16:52:09 +02004256 default:
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01004257 // TODO: Give error message?
Bram Moolenaar82139082011-09-14 16:52:09 +02004258 clear_tv(&rettv);
4259 break;
4260 }
4261 }
4262
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004263 if (curwin_save != curwin || curbuf_save != curbuf)
4264 {
4265 EMSG(_(e_complwin));
4266 goto theend;
4267 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004268 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004269 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004270 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004271 {
4272 EMSG(_(e_compldel));
4273 goto theend;
4274 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004275
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004276 if (matchlist != NULL)
4277 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004278 else if (matchdict != NULL)
4279 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004280
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004281theend:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01004282 // Restore State, it might have been changed.
4283 State = save_State;
4284
Bram Moolenaar82139082011-09-14 16:52:09 +02004285 if (matchdict != NULL)
4286 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004287 if (matchlist != NULL)
4288 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004289}
4290#endif /* FEAT_COMPL_FUNC */
4291
Bram Moolenaar39f05632006-03-19 22:15:26 +00004292#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004293/*
4294 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004295 */
4296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004297ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004298{
4299 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004300 int dir = compl_direction;
4301
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004302 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004303 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004304 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004305 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4306 /* if dir was BACKWARD then honor it just once */
4307 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004308 else if (did_emsg)
4309 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004310 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004311}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004312
4313/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004314 * Add completions from a dict.
4315 */
4316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004317ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004318{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004319 dictitem_T *di_refresh;
4320 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004321
4322 /* Check for optional "refresh" item. */
4323 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004324 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4325 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004326 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004327 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004328
4329 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4330 compl_opt_refresh_always = TRUE;
4331 }
4332
4333 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004334 di_words = dict_find(dict, (char_u *)"words", 5);
4335 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4336 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004337}
4338
4339/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004340 * Add a match to the list of matches from a typeval_T.
4341 * If the given string is already in the list of completions, then return
4342 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4343 * maybe because alloc() returns NULL, then FAIL is returned.
4344 */
4345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004346ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004347{
4348 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004349 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004350 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004351 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004352 char_u *(cptext[CPT_COUNT]);
4353
4354 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4355 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004356 word = dict_get_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4357 cptext[CPT_ABBR] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004358 (char_u *)"abbr", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004359 cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004360 (char_u *)"menu", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004361 cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004362 (char_u *)"kind", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004363 cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004364 (char_u *)"info", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004365 cptext[CPT_USER_DATA] = dict_get_string(tv->vval.v_dict,
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004366 (char_u *)"user_data", FALSE);
Bram Moolenaar8f667172018-12-14 15:38:31 +01004367 if (dict_get_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4368 icase = dict_get_number(tv->vval.v_dict, (char_u *)"icase");
4369 if (dict_get_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
4370 adup = dict_get_number(tv->vval.v_dict, (char_u *)"dup");
4371 if (dict_get_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4372 aempty = dict_get_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004373 }
4374 else
4375 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004376 word = tv_get_string_chk(tv);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004377 vim_memset(cptext, 0, sizeof(cptext));
4378 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004379 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004380 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004381 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004382}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004383#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004384
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004385/*
4386 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004387 * The search starts at position "ini" in curbuf and in the direction
4388 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004389 * When "compl_started" is FALSE start at that position, otherwise continue
4390 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 * This may return before finding all the matches.
4392 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 */
4394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004395ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
4397 static pos_T first_match_pos;
4398 static pos_T last_match_pos;
4399 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004400 static int found_all = FALSE; /* Found all matches of a
4401 certain type. */
4402 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
Bram Moolenaar572cb562005-08-05 21:35:02 +00004404 pos_T *pos;
4405 char_u **matches;
4406 int save_p_scs;
4407 int save_p_ws;
4408 int save_p_ic;
4409 int i;
4410 int num_matches;
4411 int len;
4412 int found_new_match;
4413 int type = ctrl_x_mode;
4414 char_u *ptr;
4415 char_u *dict = NULL;
4416 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004417 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004419 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004421 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 ins_buf->b_scanned = 0;
4423 found_all = FALSE;
4424 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004426 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 last_match_pos = first_match_pos = *ini;
4428 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004429 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4430 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
Bram Moolenaar4475b622017-05-01 20:46:52 +02004432 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004433 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004434
4435 /*
4436 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 for (;;)
4439 {
4440 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004441 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 * or if found_all says this entry is done. For ^X^L only use the
4445 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004446 if ((ctrl_x_mode == CTRL_X_NORMAL
4447 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
4450 found_all = FALSE;
4451 while (*e_cpt == ',' || *e_cpt == ' ')
4452 e_cpt++;
4453 if (*e_cpt == '.' && !curbuf->b_scanned)
4454 {
4455 ins_buf = curbuf;
4456 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004457 /* Move the cursor back one character so that ^N can match the
4458 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004459 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004460 {
4461 /* Move the cursor to after the last character in the
4462 * buffer, so that word at start of buffer is found
4463 * correctly. */
4464 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4465 first_match_pos.col =
4466 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 last_match_pos = first_match_pos;
4469 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004470
4471 /* Remember the first match so that the loop stops when we
4472 * wrap and come back there a second time. */
4473 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4476 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4477 {
4478 /* Scan a buffer, but not the current one. */
4479 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4480 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004481 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 first_match_pos.col = last_match_pos.col = 0;
4483 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4484 last_match_pos.lnum = 0;
4485 type = 0;
4486 }
4487 else /* unloaded buffer, scan like dictionary */
4488 {
4489 found_all = TRUE;
4490 if (ins_buf->b_fname == NULL)
4491 continue;
4492 type = CTRL_X_DICTIONARY;
4493 dict = ins_buf->b_fname;
4494 dict_f = DICT_EXACT;
4495 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004496 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 ins_buf->b_fname == NULL
4498 ? buf_spname(ins_buf)
4499 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004500 ? ins_buf->b_fname
4501 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004502 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504 else if (*e_cpt == NUL)
4505 break;
4506 else
4507 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004508 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 type = -1;
4510 else if (*e_cpt == 'k' || *e_cpt == 's')
4511 {
4512 if (*e_cpt == 'k')
4513 type = CTRL_X_DICTIONARY;
4514 else
4515 type = CTRL_X_THESAURUS;
4516 if (*++e_cpt != ',' && *e_cpt != NUL)
4517 {
4518 dict = e_cpt;
4519 dict_f = DICT_FIRST;
4520 }
4521 }
4522#ifdef FEAT_FIND_ID
4523 else if (*e_cpt == 'i')
4524 type = CTRL_X_PATH_PATTERNS;
4525 else if (*e_cpt == 'd')
4526 type = CTRL_X_PATH_DEFINES;
4527#endif
4528 else if (*e_cpt == ']' || *e_cpt == 't')
4529 {
4530 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004531 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004532 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 else
4535 type = -1;
4536
4537 /* in any case e_cpt is advanced to the next entry */
4538 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4539
4540 found_all = TRUE;
4541 if (type == -1)
4542 continue;
4543 }
4544 }
4545
Bram Moolenaar4475b622017-05-01 20:46:52 +02004546 /* If complete() was called then compl_pattern has been reset. The
4547 * following won't work then, bail out. */
4548 if (compl_pattern == NULL)
4549 break;
4550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 switch (type)
4552 {
4553 case -1:
4554 break;
4555#ifdef FEAT_FIND_ID
4556 case CTRL_X_PATH_PATTERNS:
4557 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004558 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004561 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4563 (linenr_T)1, (linenr_T)MAXLNUM);
4564 break;
4565#endif
4566
4567 case CTRL_X_DICTIONARY:
4568 case CTRL_X_THESAURUS:
4569 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004570 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 : (type == CTRL_X_THESAURUS
4572 ? (*curbuf->b_p_tsr == NUL
4573 ? p_tsr
4574 : curbuf->b_p_tsr)
4575 : (*curbuf->b_p_dict == NUL
4576 ? p_dict
4577 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004578 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004579 dict != NULL ? dict_f
4580 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 dict = NULL;
4582 break;
4583
4584 case CTRL_X_TAGS:
4585 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4586 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004587 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004589 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 * of matches is found when compl_pattern is empty */
4591 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004592 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4593 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4595 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004596 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 p_ic = save_p_ic;
4599 break;
4600
4601 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004602 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4604 {
4605
4606 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004608 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610 break;
4611
4612 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613 if (expand_cmdline(&compl_xp, compl_pattern,
4614 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004616 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 break;
4618
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004619#ifdef FEAT_COMPL_FUNC
4620 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004621 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004622 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004623 break;
4624#endif
4625
Bram Moolenaar488c6512005-08-11 20:09:58 +00004626 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004627#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004628 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004629 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004630 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004631 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004632#endif
4633 break;
4634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 default: /* normal ^P/^N and ^X^L */
4636 /*
4637 * If 'infercase' is set, don't use 'smartcase' here
4638 */
4639 save_p_scs = p_scs;
4640 if (ins_buf->b_p_inf)
4641 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642
Bram Moolenaar361aa502014-01-23 22:45:58 +01004643 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 * end but never from the middle, thus setting nowrapscan in this
4645 * buffers is a good idea, on the other hand, we always set
4646 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4647 save_p_ws = p_ws;
4648 if (ins_buf != curbuf)
4649 p_ws = FALSE;
4650 else if (*e_cpt == '.')
4651 p_ws = TRUE;
4652 for (;;)
4653 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004654 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004656 ++msg_silent; /* Don't want messages for wrapscan. */
4657
Bram Moolenaare4214502015-03-05 18:08:43 +01004658 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4659 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004660 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004661 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004662 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004664 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 else
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004666 found_new_match = searchit(NULL, ins_buf, pos, NULL,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004667 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004668 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004669 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004670 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004671 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004673 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004674 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 first_match_pos = *pos;
4676 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004677 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004680 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 found_new_match = FAIL;
4682 if (found_new_match == FAIL)
4683 {
4684 if (ins_buf == curbuf)
4685 found_all = TRUE;
4686 break;
4687 }
4688
4689 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004690 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 && ini->lnum == pos->lnum
4692 && ini->col == pos->col)
4693 continue;
4694 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004695 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004697 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
4699 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4700 continue;
4701 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4702 if (!p_paste)
4703 ptr = skipwhite(ptr);
4704 }
4705 len = (int)STRLEN(ptr);
4706 }
4707 else
4708 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004709 char_u *tmp_ptr = ptr;
4710
4711 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004713 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 /* Skip if already inside a word. */
4715 if (vim_iswordp(tmp_ptr))
4716 continue;
4717 /* Find start of next word. */
4718 tmp_ptr = find_word_start(tmp_ptr);
4719 }
4720 /* Find end of this word. */
4721 tmp_ptr = find_word_end(tmp_ptr);
4722 len = (int)(tmp_ptr - ptr);
4723
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004724 if ((compl_cont_status & CONT_ADDING)
4725 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 {
4727 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4728 {
4729 /* Try next line, if any. the new word will be
4730 * "join" as if the normal command "J" was used.
4731 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004732 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 * works -- Acevedo */
4734 STRNCPY(IObuff, ptr, len);
4735 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4736 tmp_ptr = ptr = skipwhite(ptr);
4737 /* Find start of next word. */
4738 tmp_ptr = find_word_start(tmp_ptr);
4739 /* Find end of next word. */
4740 tmp_ptr = find_word_end(tmp_ptr);
4741 if (tmp_ptr > ptr)
4742 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004743 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004745 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 IObuff[len++] = ' ';
4747 /* IObuf =~ "\k.* ", thus len >= 2 */
4748 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004749 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 || (vim_strchr(p_cpo, CPO_JOINSP)
4751 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004752 && (IObuff[len - 2] == '?'
4753 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 IObuff[len++] = ' ';
4755 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004756 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (tmp_ptr - ptr >= IOSIZE - len)
4758 tmp_ptr = ptr + IOSIZE - len - 1;
4759 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4760 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004761 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763 IObuff[len] = NUL;
4764 ptr = IObuff;
4765 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004766 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 continue;
4768 }
4769 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004770 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004771 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004772 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
4774 found_new_match = OK;
4775 break;
4776 }
4777 }
4778 p_scs = save_p_scs;
4779 p_ws = save_p_ws;
4780 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004781
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004782 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004783 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004784 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 found_new_match = OK;
4786
4787 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004788 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4789 * match */
4790 if ((ctrl_x_mode != CTRL_X_NORMAL
4791 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004792 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004793 {
4794 if (got_int)
4795 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004796 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004797 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004798 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004799
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004800 if ((ctrl_x_mode != CTRL_X_NORMAL
4801 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004802 || compl_interrupted)
4803 break;
4804 compl_started = TRUE;
4805 }
4806 else
4807 {
4808 /* Mark a buffer scanned when it has been scanned completely */
4809 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4810 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004812 compl_started = FALSE;
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004815 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004817 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 && *e_cpt == NUL) /* Got to end of 'complete' */
4819 found_new_match = FAIL;
4820
4821 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004822 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4823 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 i = ins_compl_make_cyclic();
4825
Bram Moolenaar4475b622017-05-01 20:46:52 +02004826 if (compl_old_match != NULL)
4827 {
4828 /* If several matches were added (FORWARD) or the search failed and has
4829 * just been made cyclic then we have to move compl_curr_match to the
4830 * next or previous entry (if any) -- Acevedo */
4831 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4832 : compl_old_match->cp_prev;
4833 if (compl_curr_match == NULL)
4834 compl_curr_match = compl_old_match;
4835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 return i;
4837}
4838
4839/* Delete the old text being completed. */
4840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004841ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004843 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
4845 /*
4846 * In insert mode: Delete the typed part.
4847 * In replace mode: Put the old characters back, if any.
4848 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004849 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4850 if ((int)curwin->w_cursor.col > col)
4851 {
4852 if (stop_arrow() == FAIL)
4853 return;
4854 backspace_until_column(col);
4855 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004856
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004857 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4858 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004860 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004861 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862}
4863
Bram Moolenaar472e8592016-10-15 17:06:47 +02004864/*
4865 * Insert the new text being completed.
4866 * "in_compl_func" is TRUE when called from complete_check().
4867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004869ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004871 dict_T *dict;
4872
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004873 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004874 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4875 compl_used_match = FALSE;
4876 else
4877 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004878
4879 /* Set completed item. */
4880 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004881 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004882 if (dict != NULL)
4883 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004884 dict_add_string(dict, "word", compl_shown_match->cp_str);
4885 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4886 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4887 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4888 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4889 dict_add_string(dict, "user_data",
4890 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004891 }
4892 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004893 if (!in_compl_func)
4894 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895}
4896
4897/*
4898 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004899 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4900 * get more completions. If it is FALSE, then we just do nothing when there
4901 * are no more completions in a given direction. The latter case is used when
4902 * we are still in the middle of finding completions, to allow browsing
4903 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 * Return the total number of matches, or -1 if still unknown -- webb.
4905 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004906 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4907 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 *
4909 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004910 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4911 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 */
4913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004914ins_compl_next(
4915 int allow_get_expansion,
4916 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004917 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004918 int insert_match, /* Insert the newly selected match */
4919 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920{
4921 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004922 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004923 compl_T *found_compl = NULL;
4924 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004925 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004926 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004928 /* When user complete function return -1 for findstart which is next
4929 * time of 'always', compl_shown_match become NULL. */
4930 if (compl_shown_match == NULL)
4931 return -1;
4932
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004933 if (compl_leader != NULL
4934 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004936 /* Set "compl_shown_match" to the actually shown match, it may differ
4937 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004938 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004939 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004940 && compl_shown_match->cp_next != NULL
4941 && compl_shown_match->cp_next != compl_first_match)
4942 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004943
4944 /* If we didn't find it searching forward, and compl_shows_dir is
4945 * backward, find the last match. */
4946 if (compl_shows_dir == BACKWARD
4947 && !ins_compl_equal(compl_shown_match,
4948 compl_leader, (int)STRLEN(compl_leader))
4949 && (compl_shown_match->cp_next == NULL
4950 || compl_shown_match->cp_next == compl_first_match))
4951 {
4952 while (!ins_compl_equal(compl_shown_match,
4953 compl_leader, (int)STRLEN(compl_leader))
4954 && compl_shown_match->cp_prev != NULL
4955 && compl_shown_match->cp_prev != compl_first_match)
4956 compl_shown_match = compl_shown_match->cp_prev;
4957 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004958 }
4959
4960 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004961 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 /* Delete old text to be replaced */
4963 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004964
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004965 /* When finding the longest common text we stick at the original text,
4966 * don't let CTRL-N or CTRL-P move to the first match. */
4967 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4968
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004969 /* When restarting the search don't insert the first match either. */
4970 if (compl_restarting)
4971 {
4972 advance = FALSE;
4973 compl_restarting = FALSE;
4974 }
4975
Bram Moolenaare3226be2005-12-18 22:10:00 +00004976 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4977 * around. */
4978 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004980 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004982 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004983 found_end = (compl_first_match != NULL
4984 && (compl_shown_match->cp_next == compl_first_match
4985 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004986 }
4987 else if (compl_shows_dir == BACKWARD
4988 && compl_shown_match->cp_prev != NULL)
4989 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004990 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004991 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004992 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004995 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004996 if (!allow_get_expansion)
4997 {
4998 if (advance)
4999 {
5000 if (compl_shows_dir == BACKWARD)
5001 compl_pending -= todo + 1;
5002 else
5003 compl_pending += todo + 1;
5004 }
5005 return -1;
5006 }
5007
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005008 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005009 {
5010 if (compl_shows_dir == BACKWARD)
5011 --compl_pending;
5012 else
5013 ++compl_pending;
5014 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005015
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005016 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005017 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005018
5019 /* handle any pending completions */
5020 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005021 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005022 {
5023 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5024 {
5025 compl_shown_match = compl_shown_match->cp_next;
5026 --compl_pending;
5027 }
5028 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5029 {
5030 compl_shown_match = compl_shown_match->cp_prev;
5031 ++compl_pending;
5032 }
5033 else
5034 break;
5035 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005036 found_end = FALSE;
5037 }
5038 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5039 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005040 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005041 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005042 ++todo;
5043 else
5044 /* Remember a matching item. */
5045 found_compl = compl_shown_match;
5046
5047 /* Stop at the end of the list when we found a usable match. */
5048 if (found_end)
5049 {
5050 if (found_compl != NULL)
5051 {
5052 compl_shown_match = found_compl;
5053 break;
5054 }
5055 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005059 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005060 if (compl_no_insert && !started)
5061 {
5062 ins_bytes(compl_orig_text + ins_compl_len());
5063 compl_used_match = FALSE;
5064 }
5065 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005066 {
5067 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005068 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005069 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005070 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005071 }
5072 else
5073 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 if (!allow_get_expansion)
5076 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005077 /* may undisplay the popup menu first */
5078 ins_compl_upd_pum();
5079
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005080 /* redraw to show the user what was inserted */
5081 update_screen(0);
5082
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005083 /* display the updated popup menu */
5084 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005085#ifdef FEAT_GUI
5086 if (gui.in_use)
5087 {
5088 /* Show the cursor after the match, not after the redrawn text. */
5089 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005090 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005091 }
5092#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 /* Delete old text to be replaced, since we're still searching and
5095 * don't want to match ourselves! */
5096 ins_compl_delete();
5097 }
5098
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005099 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005100 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005101 if (compl_no_insert && !started)
5102 compl_enter_selects = TRUE;
5103 else
5104 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 /*
5107 * Show the file name for the match (if any)
5108 * Truncate the file name to avoid a wait for return.
5109 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005110 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005112 char *lead = _("match in file");
5113 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5114 char_u *s;
5115 char_u *e;
5116
5117 if (space > 0)
5118 {
5119 /* We need the tail that fits. With double-byte encoding going
5120 * back from the end is very slow, thus go from the start and keep
5121 * the text that fits in "space" between "s" and "e". */
5122 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5123 {
5124 space -= ptr2cells(e);
5125 while (space < 0)
5126 {
5127 space += ptr2cells(s);
5128 MB_PTR_ADV(s);
5129 }
5130 }
5131 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5132 s > compl_shown_match->cp_fname ? "<" : "", s);
5133 msg(IObuff);
5134 redraw_cmdline = FALSE; /* don't overwrite! */
5135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
5137
5138 return num_matches;
5139}
5140
5141/*
5142 * Call this while finding completions, to check whether the user has hit a key
5143 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005144 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005146 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005147 * "in_compl_func" is TRUE when called from complete_check(), don't set
5148 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 */
5150 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005151ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005154 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005156 /* Don't check when reading keys from a script, :normal or feedkeys().
5157 * That would break the test scripts. But do check for keys when called
5158 * from complete_check(). */
5159 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 return;
5161
5162 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005163 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 return;
5165 count = 0;
5166
Bram Moolenaara260a972006-06-23 19:36:29 +00005167 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5168 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (c != NUL)
5171 {
5172 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5173 {
5174 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005175 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005176 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005177 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005179 else
5180 {
5181 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005182 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005183 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005184 if (c != K_IGNORE)
5185 {
5186 /* Don't interrupt completion when the character wasn't typed,
5187 * e.g., when doing @q to replay keys. */
5188 if (c != Ctrl_R && KeyTyped)
5189 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005190
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005191 vungetc(c);
5192 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005195 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005196 {
5197 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5198
5199 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005200 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005201 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005202}
5203
5204/*
5205 * Decide the direction of Insert mode complete from the key typed.
5206 * Returns BACKWARD or FORWARD.
5207 */
5208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005209ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005210{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005211 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005212 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005213 return BACKWARD;
5214 return FORWARD;
5215}
5216
5217/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005218 * Return TRUE for keys that are used for completion only when the popup menu
5219 * is visible.
5220 */
5221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005222ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005223{
5224 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005225 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5226 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005227}
5228
5229/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005230 * Decide the number of completions to move forward.
5231 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5232 */
5233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005234ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005235{
5236 int h;
5237
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005238 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005239 {
5240 h = pum_get_height();
5241 if (h > 3)
5242 h -= 2; /* keep some context */
5243 return h;
5244 }
5245 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246}
5247
5248/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005249 * Return TRUE if completion with "c" should insert the match, FALSE if only
5250 * to change the currently selected completion.
5251 */
5252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005253ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005254{
5255 switch (c)
5256 {
5257 case K_UP:
5258 case K_DOWN:
5259 case K_PAGEDOWN:
5260 case K_KPAGEDOWN:
5261 case K_S_DOWN:
5262 case K_PAGEUP:
5263 case K_KPAGEUP:
5264 case K_S_UP:
5265 return FALSE;
5266 }
5267 return TRUE;
5268}
5269
5270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 * Do Insert mode completion.
5272 * Called when character "c" was typed, which has a meaning for completion.
5273 * Returns OK if completion was done, FAIL if something failed (out of mem).
5274 */
5275 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005276ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 char_u *line;
5279 int startcol = 0; /* column where searched text starts */
5280 colnr_T curs_col; /* cursor column */
5281 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005282 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005283 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005284 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005285 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
Bram Moolenaare3226be2005-12-18 22:10:00 +00005287 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005288 insert_match = ins_compl_use_match(c);
5289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005290 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 /* First time we hit ^N or ^P (in a row, I mean) */
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 did_ai = FALSE;
5295#ifdef FEAT_SMARTINDENT
5296 did_si = FALSE;
5297 can_si = FALSE;
5298 can_si_back = FALSE;
5299#endif
5300 if (stop_arrow() == FAIL)
5301 return FAIL;
5302
5303 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005305 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005307 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 * "compl_startpos" to the cursor as a pattern to add a new word
5309 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005310 * "compl_startpos" is not in the same line as the cursor then fix it
5311 * (the line has been split because it was longer than 'tw'). if SOL
5312 * is set then skip the previous pattern, a word at the beginning of
5313 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005314 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5315 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
5317 /*
5318 * it is a continued search
5319 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005320 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005321 if (ctrl_x_mode == CTRL_X_NORMAL
5322 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5323 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005327 /* line (probably) wrapped, set compl_startpos to the
5328 * first non_blank in the line, if it is not a wordchar
5329 * include it to get a better pattern, but then we don't
5330 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005331 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005332 compl_startpos.col = compl_col;
5333 compl_startpos.lnum = curwin->w_cursor.lnum;
5334 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336 else
5337 {
5338 /* S_IPOS was set when we inserted a word that was at the
5339 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 * mode but first we need to redefine compl_startpos */
5341 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 compl_cont_status |= CONT_SOL;
5344 compl_startpos.col = (colnr_T)(skipwhite(
5345 line + compl_length
5346 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005351 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005352 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005354 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005356 compl_cont_status &= ~CONT_SOL;
5357 compl_length = (IOSIZE - MIN_SPACE);
5358 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5361 if (compl_length < 1)
5362 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005364 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005367 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005374 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005375 if (ctrl_x_mode != CTRL_X_NORMAL)
5376 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005377 compl_cont_status = 0;
5378 compl_cont_status |= CONT_N_ADDS;
5379 compl_startpos = curwin->w_cursor;
5380 startcol = (int)curs_col;
5381 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383
5384 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005385 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5389 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005390 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005392 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 compl_col += ++startcol;
5395 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005398 compl_pattern = str_foldcase(line + compl_col,
5399 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005401 compl_pattern = vim_strnsave(line + compl_col,
5402 compl_length);
5403 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 return FAIL;
5405 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005406 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
5408 char_u *prefix = (char_u *)"\\<";
5409
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005410 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005411 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005412 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005415 if (!vim_iswordp(line + compl_col)
5416 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 && (
5418#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#endif
5423 )))
5424 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 STRCPY((char *)compl_pattern, prefix);
5426 (void)quote_meta(compl_pattern + STRLEN(prefix),
5427 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005431 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005433 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434#endif
5435 )
5436 {
5437 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005438 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5439 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 compl_col += curs_col;
5442 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444 else
5445 {
5446#ifdef FEAT_MBYTE
5447 /* Search the point of change class of multibyte character
5448 * or not a word single byte character backward. */
5449 if (has_mbyte)
5450 {
5451 int base_class;
5452 int head_off;
5453
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005454 startcol -= (*mb_head_off)(line, line + startcol);
5455 base_class = mb_get_class(line + startcol);
5456 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005458 head_off = (*mb_head_off)(line, line + startcol);
5459 if (base_class != mb_get_class(line + startcol
5460 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005462 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464 }
5465 else
5466#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005467 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005469 compl_col += ++startcol;
5470 compl_length = (int)curs_col - startcol;
5471 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 {
5473 /* Only match word with at least two chars -- webb
5474 * there's no need to call quote_meta,
5475 * alloc(7) is enough -- Acevedo
5476 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005477 compl_pattern = alloc(7);
5478 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005480 STRCPY((char *)compl_pattern, "\\<");
5481 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5482 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 else
5485 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005486 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005487 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005488 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005490 STRCPY((char *)compl_pattern, "\\<");
5491 (void)quote_meta(compl_pattern + 2, line + compl_col,
5492 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
5494 }
5495 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005496 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005498 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005499 compl_length = (int)curs_col - (int)compl_col;
5500 if (compl_length < 0) /* cursor in indent: empty pattern */
5501 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005503 compl_pattern = str_foldcase(line + compl_col, compl_length,
5504 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005506 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5507 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 return FAIL;
5509 }
5510 else if (ctrl_x_mode == CTRL_X_FILES)
5511 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005512 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005513 if (startcol > 0)
5514 {
5515 char_u *p = line + startcol;
5516
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005517 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005518 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005519 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005520 if (p == line && vim_isfilec(PTR2CHAR(p)))
5521 startcol = 0;
5522 else
5523 startcol = (int)(p - line) + 1;
5524 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005525
Bram Moolenaar0300e462013-09-08 16:03:45 +02005526 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005527 compl_length = (int)curs_col - startcol;
5528 compl_pattern = addstar(line + compl_col, compl_length,
5529 EXPAND_FILES);
5530 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 return FAIL;
5532 }
5533 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5534 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 compl_pattern = vim_strnsave(line, curs_col);
5536 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005538 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005539 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005540 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5541 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005542 /* No completion possible, use an empty pattern to get a
5543 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005544 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005545 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005546 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5547 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005549 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005550 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005551#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005552 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005553 * Call user defined function 'completefunc' with "a:findstart"
5554 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005555 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005556 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005557 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005558 char_u *funcname;
5559 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005560 win_T *curwin_save;
5561 buf_T *curbuf_save;
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005562 int save_State = State;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005563
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005564 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005565 * string */
5566 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5567 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5568 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005569 {
5570 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5571 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005572 /* restore did_ai, so that adding comment leader works */
5573 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005574 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005575 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005576
Bram Moolenaarffa96842018-06-12 22:05:14 +02005577 args[0].v_type = VAR_NUMBER;
5578 args[0].vval.v_number = 1;
5579 args[1].v_type = VAR_STRING;
5580 args[1].vval.v_string = (char_u *)"";
5581 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005582 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005583 curwin_save = curwin;
5584 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005585 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar8ad16da2019-01-06 15:29:57 +01005586
5587 State = save_State;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005588 if (curwin_save != curwin || curbuf_save != curbuf)
5589 {
5590 EMSG(_(e_complwin));
5591 return FAIL;
5592 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005593 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005594 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005595 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005596 {
5597 EMSG(_(e_compldel));
5598 return FAIL;
5599 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005600
Bram Moolenaar6110a002012-01-26 18:58:38 +01005601 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005602 * cancel the complete without an error.
5603 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005604 if (col == -2)
5605 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005606 if (col == -3)
5607 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005608 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005609 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005610 if (!shortmess(SHM_COMPLETIONMENU))
5611 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005612 return FAIL;
5613 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005614
Bram Moolenaar82139082011-09-14 16:52:09 +02005615 /*
5616 * Reset extended parameters of completion, when start new
5617 * completion.
5618 */
5619 compl_opt_refresh_always = FALSE;
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005620 compl_opt_suppress_empty = FALSE;
Bram Moolenaar82139082011-09-14 16:52:09 +02005621
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005622 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005623 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005624 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005625 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005626 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005627
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005628 /* Setup variables for completion. Need to obtain "line" again,
5629 * it may have become invalid. */
5630 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005631 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005632 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5633 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005634#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005635 return FAIL;
5636 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005637 else if (ctrl_x_mode == CTRL_X_SPELL)
5638 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005639#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005640 if (spell_bad_len > 0)
5641 compl_col = curs_col - spell_bad_len;
5642 else
5643 compl_col = spell_word_start(startcol);
5644 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005645 {
5646 compl_length = 0;
5647 compl_col = curs_col;
5648 }
5649 else
5650 {
5651 spell_expand_check_cap(compl_col);
5652 compl_length = (int)curs_col - compl_col;
5653 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005654 /* Need to obtain "line" again, it may have become invalid. */
5655 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005656 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5657 if (compl_pattern == NULL)
5658#endif
5659 return FAIL;
5660 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005661 else
5662 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005663 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005664 return FAIL;
5665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005667 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005670 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
5672 /* Insert a new line, keep indentation but ignore 'comments' */
5673#ifdef FEAT_COMMENTS
5674 char_u *old = curbuf->b_p_com;
5675
5676 curbuf->b_p_com = (char_u *)"";
5677#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005678 compl_startpos.lnum = curwin->w_cursor.lnum;
5679 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 ins_eol('\r');
5681#ifdef FEAT_COMMENTS
5682 curbuf->b_p_com = old;
5683#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005684 compl_length = 0;
5685 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 }
5688 else
5689 {
5690 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005691 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005694 if (compl_cont_status & CONT_LOCAL)
5695 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 else
5697 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5698
Bram Moolenaar62951b12011-09-21 18:23:05 +02005699 /* If any of the original typed text has been changed we need to fix
5700 * the redo buffer. */
5701 ins_compl_fixRedoBufForLeader(NULL);
5702
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005703 /* Always add completion for the original text. */
5704 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005705 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5706 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005707 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005709 VIM_CLEAR(compl_pattern);
5710 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 return FAIL;
5712 }
5713
5714 /* showmode might reset the internal line pointers, so it must
5715 * be called before line = ml_get(), or when this address is no
5716 * longer needed. -- Acevedo.
5717 */
5718 edit_submode_extra = (char_u *)_("-- Searching...");
5719 edit_submode_highl = HLF_COUNT;
5720 showmode();
5721 edit_submode_extra = NULL;
5722 out_flush();
5723 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005724 else if (insert_match && stop_arrow() == FAIL)
5725 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005727 compl_shown_match = compl_curr_match;
5728 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
5730 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005731 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005733 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005734 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005735 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005737 /* may undisplay the popup menu */
5738 ins_compl_upd_pum();
5739
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005740 if (n > 1) /* all matches have been found */
5741 compl_matches = n;
5742 compl_curr_match = compl_shown_match;
5743 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
Bram Moolenaard68071d2006-05-02 22:08:30 +00005745 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5746 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 if (got_int && !global_busy)
5748 {
5749 (void)vgetc();
5750 got_int = FALSE;
5751 }
5752
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005753 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005754 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005756 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5757 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5759 edit_submode_highl = HLF_E;
5760 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5761 * because we couldn't expand anything at first place, but if we used
5762 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5763 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005764 if ( compl_length > 1
5765 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005766 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5768 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005769 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771
Bram Moolenaar572cb562005-08-05 21:35:02 +00005772 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005773 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005775 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
5777 if (edit_submode_extra == NULL)
5778 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005779 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 {
5781 edit_submode_extra = (char_u *)_("Back at original");
5782 edit_submode_highl = HLF_W;
5783 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005784 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 {
5786 edit_submode_extra = (char_u *)_("Word from other line");
5787 edit_submode_highl = HLF_COUNT;
5788 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005789 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 {
5791 edit_submode_extra = (char_u *)_("The only match");
5792 edit_submode_highl = HLF_COUNT;
5793 }
5794 else
5795 {
5796 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005797 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005799 int number = 0;
5800 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005802 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 {
5804 /* search backwards for the first valid (!= -1) number.
5805 * This should normally succeed already at the first loop
5806 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005807 for (match = compl_curr_match->cp_prev; match != NULL
5808 && match != compl_first_match;
5809 match = match->cp_prev)
5810 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005812 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814 }
5815 if (match != NULL)
5816 /* go up and assign all numbers which are not assigned
5817 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005818 for (match = match->cp_next;
5819 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005820 match = match->cp_next)
5821 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 }
5823 else /* BACKWARD */
5824 {
5825 /* search forwards (upwards) for the first valid (!= -1)
5826 * number. This should normally succeed already at the
5827 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005828 for (match = compl_curr_match->cp_next; match != NULL
5829 && match != compl_first_match;
5830 match = match->cp_next)
5831 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005833 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 break;
5835 }
5836 if (match != NULL)
5837 /* go down and assign all numbers which are not
5838 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005839 for (match = match->cp_prev; match
5840 && match->cp_number == -1;
5841 match = match->cp_prev)
5842 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
5844 }
5845
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005846 /* The match should always have a sequence number now, this is
5847 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005848 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005850 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5851 * Translations may need more than twice that. */
5852 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005854 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005855 vim_snprintf((char *)match_ref, sizeof(match_ref),
5856 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005857 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005859 vim_snprintf((char *)match_ref, sizeof(match_ref),
5860 _("match %d"),
5861 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 edit_submode_extra = match_ref;
5863 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005864 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 curs_columns(FALSE);
5866 }
5867 }
5868 }
5869
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005870 // Show a message about what (completion) mode we're in.
5871 if (!compl_opt_suppress_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005873 showmode();
5874 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaarea389e92014-05-28 21:40:52 +02005875 {
Bram Moolenaarcee9bc22019-01-11 13:02:23 +01005876 if (edit_submode_extra != NULL)
5877 {
5878 if (!p_smd)
5879 msg_attr(edit_submode_extra,
5880 edit_submode_highl < HLF_COUNT
5881 ? HL_ATTR(edit_submode_highl) : 0);
5882 }
5883 else
5884 msg_clr_cmdline(); // necessary for "noshowmode"
Bram Moolenaarea389e92014-05-28 21:40:52 +02005885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
Bram Moolenaard68071d2006-05-02 22:08:30 +00005888 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005889 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005890 show_pum(save_w_wrow, save_w_leftcol);
5891
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005892 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005893 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 return OK;
5896}
5897
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005898 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005899show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005900{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005901 /* RedrawingDisabled may be set when invoked through complete(). */
5902 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005903
Bram Moolenaarcb036422017-03-01 12:29:10 +01005904 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005905
Bram Moolenaarcb036422017-03-01 12:29:10 +01005906 /* If the cursor moved or the display scrolled we need to remove the pum
5907 * first. */
5908 setcursor();
5909 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5910 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005911
Bram Moolenaarcb036422017-03-01 12:29:10 +01005912 ins_compl_show_pum();
5913 setcursor();
5914 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005915}
5916
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917/*
5918 * Looks in the first "len" chars. of "src" for search-metachars.
5919 * If dest is not NULL the chars. are copied there quoting (with
5920 * a backslash) the metachars, and dest would be NUL terminated.
5921 * Returns the length (needed) of dest
5922 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005923 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005924quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005926 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005928 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 {
5930 switch (*src)
5931 {
5932 case '.':
5933 case '*':
5934 case '[':
5935 if (ctrl_x_mode == CTRL_X_DICTIONARY
5936 || ctrl_x_mode == CTRL_X_THESAURUS)
5937 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005938 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 case '~':
5940 if (!p_magic) /* quote these only if magic is set */
5941 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005942 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 case '\\':
5944 if (ctrl_x_mode == CTRL_X_DICTIONARY
5945 || ctrl_x_mode == CTRL_X_THESAURUS)
5946 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005947 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 case '^': /* currently it's not needed. */
5949 case '$':
5950 m++;
5951 if (dest != NULL)
5952 *dest++ = '\\';
5953 break;
5954 }
5955 if (dest != NULL)
5956 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005957# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 /* Copy remaining bytes of a multibyte character. */
5959 if (has_mbyte)
5960 {
5961 int i, mb_len;
5962
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005963 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (mb_len > 0 && len >= mb_len)
5965 for (i = 0; i < mb_len; ++i)
5966 {
5967 --len;
5968 ++src;
5969 if (dest != NULL)
5970 *dest++ = *src;
5971 }
5972 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 }
5975 if (dest != NULL)
5976 *dest = NUL;
5977
5978 return m;
5979}
5980#endif /* FEAT_INS_EXPAND */
5981
5982/*
5983 * Next character is interpreted literally.
5984 * A one, two or three digit decimal number is interpreted as its byte value.
5985 * If one or two digits are entered, the next character is given to vungetc().
5986 * For Unicode a character > 255 may be returned.
5987 */
5988 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005989get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 int cc;
5992 int nc;
5993 int i;
5994 int hex = FALSE;
5995 int octal = FALSE;
5996#ifdef FEAT_MBYTE
5997 int unicode = 0;
5998#endif
5999
6000 if (got_int)
6001 return Ctrl_C;
6002
6003#ifdef FEAT_GUI
6004 /*
6005 * In GUI there is no point inserting the internal code for a special key.
6006 * It is more useful to insert the string "<KEY>" instead. This would
6007 * probably be useful in a text window too, but it would not be
6008 * vi-compatible (maybe there should be an option for it?) -- webb
6009 */
6010 if (gui.in_use)
6011 ++allow_keys;
6012#endif
6013#ifdef USE_ON_FLY_SCROLL
6014 dont_scroll = TRUE; /* disallow scrolling here */
6015#endif
6016 ++no_mapping; /* don't map the next key hits */
6017 cc = 0;
6018 i = 0;
6019 for (;;)
6020 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006021 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022#ifdef FEAT_CMDL_INFO
6023 if (!(State & CMDLINE)
6024# ifdef FEAT_MBYTE
6025 && MB_BYTE2LEN_CHECK(nc) == 1
6026# endif
6027 )
6028 add_to_showcmd(nc);
6029#endif
6030 if (nc == 'x' || nc == 'X')
6031 hex = TRUE;
6032 else if (nc == 'o' || nc == 'O')
6033 octal = TRUE;
6034#ifdef FEAT_MBYTE
6035 else if (nc == 'u' || nc == 'U')
6036 unicode = nc;
6037#endif
6038 else
6039 {
6040 if (hex
6041#ifdef FEAT_MBYTE
6042 || unicode != 0
6043#endif
6044 )
6045 {
6046 if (!vim_isxdigit(nc))
6047 break;
6048 cc = cc * 16 + hex2nr(nc);
6049 }
6050 else if (octal)
6051 {
6052 if (nc < '0' || nc > '7')
6053 break;
6054 cc = cc * 8 + nc - '0';
6055 }
6056 else
6057 {
6058 if (!VIM_ISDIGIT(nc))
6059 break;
6060 cc = cc * 10 + nc - '0';
6061 }
6062
6063 ++i;
6064 }
6065
6066 if (cc > 255
6067#ifdef FEAT_MBYTE
6068 && unicode == 0
6069#endif
6070 )
6071 cc = 255; /* limit range to 0-255 */
6072 nc = 0;
6073
6074 if (hex) /* hex: up to two chars */
6075 {
6076 if (i >= 2)
6077 break;
6078 }
6079#ifdef FEAT_MBYTE
6080 else if (unicode) /* Unicode: up to four or eight chars */
6081 {
6082 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6083 break;
6084 }
6085#endif
6086 else if (i >= 3) /* decimal or octal: up to three chars */
6087 break;
6088 }
6089 if (i == 0) /* no number entered */
6090 {
6091 if (nc == K_ZERO) /* NUL is stored as NL */
6092 {
6093 cc = '\n';
6094 nc = 0;
6095 }
6096 else
6097 {
6098 cc = nc;
6099 nc = 0;
6100 }
6101 }
6102
6103 if (cc == 0) /* NUL is stored as NL */
6104 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006105#ifdef FEAT_MBYTE
6106 if (enc_dbcs && (cc & 0xff) == 0)
6107 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6108 second byte will cause trouble! */
6109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
6111 --no_mapping;
6112#ifdef FEAT_GUI
6113 if (gui.in_use)
6114 --allow_keys;
6115#endif
6116 if (nc)
6117 vungetc(nc);
6118 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6119 return cc;
6120}
6121
6122/*
6123 * Insert character, taking care of special keys and mod_mask
6124 */
6125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006126insert_special(
6127 int c,
6128 int allow_modmask,
6129 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
6131 char_u *p;
6132 int len;
6133
6134 /*
6135 * Special function key, translate into "<Key>". Up to the last '>' is
6136 * inserted with ins_str(), so as not to replace characters in replace
6137 * mode.
6138 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6139 * unless 'allow_modmask' is TRUE.
6140 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006141#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 /* Command-key never produces a normal key */
6143 if (mod_mask & MOD_MASK_CMD)
6144 allow_modmask = TRUE;
6145#endif
6146 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6147 {
6148 p = get_special_key_name(c, mod_mask);
6149 len = (int)STRLEN(p);
6150 c = p[len - 1];
6151 if (len > 2)
6152 {
6153 if (stop_arrow() == FAIL)
6154 return;
6155 p[len - 1] = NUL;
6156 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006157 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 ctrlv = FALSE;
6159 }
6160 }
6161 if (stop_arrow() == OK)
6162 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6163}
6164
6165/*
6166 * Special characters in this context are those that need processing other
6167 * than the simple insertion that can be performed here. This includes ESC
6168 * which terminates the insert, and CR/NL which need special processing to
6169 * open up a new line. This routine tries to optimize insertions performed by
6170 * the "redo", "undo" or "put" commands, so it needs to know when it should
6171 * stop and defer processing to the "normal" mechanism.
6172 * '0' and '^' are special, because they can be followed by CTRL-D.
6173 */
6174#ifdef EBCDIC
6175# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6176#else
6177# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6178#endif
6179
6180#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006181# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006183# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#endif
6185
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006186/*
6187 * "flags": INSCHAR_FORMAT - force formatting
6188 * INSCHAR_CTRLV - char typed just after CTRL-V
6189 * INSCHAR_NO_FEX - don't use 'formatexpr'
6190 *
6191 * NOTE: passes the flags value straight through to internal_format() which,
6192 * beside INSCHAR_FORMAT (above), is also looking for these:
6193 * INSCHAR_DO_COM - format comments
6194 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006197insertchar(
6198 int c, /* character to insert or NUL */
6199 int flags, /* INSCHAR_FORMAT, etc. */
6200 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 int textwidth;
6203#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006207 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006209 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
6212 /*
6213 * Try to break the line in two or more pieces when:
6214 * - Always do this if we have been called to do formatting only.
6215 * - Always do this when 'formatoptions' has the 'a' flag and the line
6216 * ends in white space.
6217 * - Otherwise:
6218 * - Don't do this if inserting a blank
6219 * - Don't do this if an existing character is being replaced, unless
6220 * we're in VREPLACE mode.
6221 * - Do this if the cursor is not on the line where insert started
6222 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6223 * before the insert.
6224 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6225 * before 'textwidth'
6226 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006228 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006229 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 && *ml_get_cursor() != NUL)
6233 && (curwin->w_cursor.lnum != Insstart.lnum
6234 || ((!has_format_option(FO_INS_LONG)
6235 || Insstart_textlen <= (colnr_T)textwidth)
6236 && (!fo_ins_blank
6237 || Insstart_blank_vcol <= (colnr_T)textwidth
6238 ))))))
6239 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240 /* Format with 'formatexpr' when it's set. Use internal formatting
6241 * when 'formatexpr' isn't set or it returns non-zero. */
6242#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006243 int do_internal = TRUE;
6244 colnr_T virtcol = get_nolist_virtcol()
6245 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006246
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006247 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6248 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006249 {
6250 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6251 /* It may be required to save for undo again, e.g. when setline()
6252 * was called. */
6253 ins_need_undo = TRUE;
6254 }
6255 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006257 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (c == NUL) /* only formatting was wanted */
6261 return;
6262
6263#ifdef FEAT_COMMENTS
6264 /* Check whether this character should end a comment. */
6265 if (did_ai && (int)c == end_comment_pending)
6266 {
6267 char_u *line;
6268 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6269 int middle_len, end_len;
6270 int i;
6271
6272 /*
6273 * Need to remove existing (middle) comment leader and insert end
6274 * comment leader. First, check what comment leader we can find.
6275 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006276 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6278 {
6279 /* Skip middle-comment string */
6280 while (*p && p[-1] != ':') /* find end of middle flags */
6281 ++p;
6282 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6283 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006284 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 --middle_len;
6286
6287 /* Find the end-comment string */
6288 while (*p && p[-1] != ':') /* find end of end flags */
6289 ++p;
6290 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6291
6292 /* Skip white space before the cursor */
6293 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006294 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 ;
6296 i++;
6297
6298 /* Skip to before the middle leader */
6299 i -= middle_len;
6300
6301 /* Check some expected things before we go on */
6302 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6303 {
6304 /* Backspace over all the stuff we want to replace */
6305 backspace_until_column(i);
6306
6307 /*
6308 * Insert the end-comment string, except for the last
6309 * character, which will get inserted as normal later.
6310 */
6311 ins_bytes_len(lead_end, end_len - 1);
6312 }
6313 }
6314 }
6315 end_comment_pending = NUL;
6316#endif
6317
6318 did_ai = FALSE;
6319#ifdef FEAT_SMARTINDENT
6320 did_si = FALSE;
6321 can_si = FALSE;
6322 can_si_back = FALSE;
6323#endif
6324
6325 /*
6326 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6327 * This speeds up normal text input considerably.
6328 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6329 * need to re-indent at a ':', or any other character (but not what
6330 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006331 * Don't do this when there an InsertCharPre autocommand is defined,
6332 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006333 * Do the check for InsertCharPre before the call to vpeekc() because the
6334 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 */
6336#ifdef USE_ON_FLY_SCROLL
6337 dont_scroll = FALSE; /* allow scrolling here */
6338#endif
6339
6340 if ( !ISSPECIAL(c)
6341#ifdef FEAT_MBYTE
6342 && (!has_mbyte || (*mb_char2len)(c) == 1)
6343#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006344 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 && vpeekc() != NUL
6346 && !(State & REPLACE_FLAG)
6347#ifdef FEAT_CINDENT
6348 && !cindent_on()
6349#endif
6350#ifdef FEAT_RIGHTLEFT
6351 && !p_ri
6352#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006353 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 {
6355#define INPUT_BUFLEN 100
6356 char_u buf[INPUT_BUFLEN + 1];
6357 int i;
6358 colnr_T virtcol = 0;
6359
6360 buf[0] = c;
6361 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006362 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 virtcol = get_nolist_virtcol();
6364 /*
6365 * Stop the string when:
6366 * - no more chars available
6367 * - finding a special character (command key)
6368 * - buffer is full
6369 * - running into the 'textwidth' boundary
6370 * - need to check for abbreviation: A non-word char after a word-char
6371 */
6372 while ( (c = vpeekc()) != NUL
6373 && !ISSPECIAL(c)
6374#ifdef FEAT_MBYTE
6375 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6376#endif
6377 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006378# ifdef FEAT_FKMAP
6379 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 && (textwidth == 0
6382 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6383 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6384 {
6385#ifdef FEAT_RIGHTLEFT
6386 c = vgetc();
6387 if (p_hkmap && KeyTyped)
6388 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 buf[i++] = c;
6390#else
6391 buf[i++] = vgetc();
6392#endif
6393 }
6394
6395#ifdef FEAT_DIGRAPHS
6396 do_digraph(-1); /* clear digraphs */
6397 do_digraph(buf[i-1]); /* may be the start of a digraph */
6398#endif
6399 buf[i] = NUL;
6400 ins_str(buf);
6401 if (flags & INSCHAR_CTRLV)
6402 {
6403 redo_literal(*buf);
6404 i = 1;
6405 }
6406 else
6407 i = 0;
6408 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006409 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
6411 else
6412 {
6413#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006414 int cc;
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6417 {
6418 char_u buf[MB_MAXBYTES + 1];
6419
6420 (*mb_char2bytes)(c, buf);
6421 buf[cc] = NUL;
6422 ins_char_bytes(buf, cc);
6423 AppendCharToRedobuff(c);
6424 }
6425 else
6426#endif
6427 {
6428 ins_char(c);
6429 if (flags & INSCHAR_CTRLV)
6430 redo_literal(c);
6431 else
6432 AppendCharToRedobuff(c);
6433 }
6434 }
6435}
6436
6437/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006438 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006439 *
6440 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6441 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006442 */
6443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006444internal_format(
6445 int textwidth,
6446 int second_indent,
6447 int flags,
6448 int format_only,
6449 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006450{
6451 int cc;
6452 int save_char = NUL;
6453 int haveto_redraw = FALSE;
6454 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6455#ifdef FEAT_MBYTE
6456 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6457#endif
6458 int fo_white_par = has_format_option(FO_WHITE_PAR);
6459 int first_line = TRUE;
6460#ifdef FEAT_COMMENTS
6461 colnr_T leader_len;
6462 int no_leader = FALSE;
6463 int do_comments = (flags & INSCHAR_DO_COM);
6464#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006465#ifdef FEAT_LINEBREAK
6466 int has_lbr = curwin->w_p_lbr;
6467
6468 /* make sure win_lbr_chartabsize() counts correctly */
6469 curwin->w_p_lbr = FALSE;
6470#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006471
6472 /*
6473 * When 'ai' is off we don't want a space under the cursor to be
6474 * deleted. Replace it with an 'x' temporarily.
6475 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006476 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477 {
6478 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006479 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006480 {
6481 save_char = cc;
6482 pchar_cursor('x');
6483 }
6484 }
6485
6486 /*
6487 * Repeat breaking lines, until the current line is not too long.
6488 */
6489 while (!got_int)
6490 {
6491 int startcol; /* Cursor column at entry */
6492 int wantcol; /* column at textwidth border */
6493 int foundcol; /* column for start of spaces */
6494 int end_foundcol = 0; /* column for start of word */
6495 colnr_T len;
6496 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006497 int orig_col = 0;
6498 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006499 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006500 colnr_T end_col;
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006501 int wcc; // counter for whitespace chars
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006502
Bram Moolenaar97b98102009-11-17 16:41:01 +00006503 virtcol = get_nolist_virtcol()
6504 + char2cells(c != NUL ? c : gchar_cursor());
6505 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006506 break;
6507
6508#ifdef FEAT_COMMENTS
6509 if (no_leader)
6510 do_comments = FALSE;
6511 else if (!(flags & INSCHAR_FORMAT)
6512 && has_format_option(FO_WRAP_COMS))
6513 do_comments = TRUE;
6514
6515 /* Don't break until after the comment leader */
6516 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006517 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518 else
6519 leader_len = 0;
6520
6521 /* If the line doesn't start with a comment leader, then don't
6522 * start one in a following broken line. Avoids that a %word
6523 * moved to the start of the next line causes all following lines
6524 * to start with %. */
6525 if (leader_len == 0)
6526 no_leader = TRUE;
6527#endif
6528 if (!(flags & INSCHAR_FORMAT)
6529#ifdef FEAT_COMMENTS
6530 && leader_len == 0
6531#endif
6532 && !has_format_option(FO_WRAP))
6533
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006534 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006535 if ((startcol = curwin->w_cursor.col) == 0)
6536 break;
6537
6538 /* find column of textwidth border */
6539 coladvance((colnr_T)textwidth);
6540 wantcol = curwin->w_cursor.col;
6541
Bram Moolenaar97b98102009-11-17 16:41:01 +00006542 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006543 foundcol = 0;
6544
6545 /*
6546 * Find position to break at.
6547 * Stop at first entered white when 'formatoptions' has 'v'
6548 */
6549 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006550 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006551 || curwin->w_cursor.lnum != Insstart.lnum
6552 || curwin->w_cursor.col >= Insstart.col)
6553 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006554 if (curwin->w_cursor.col == startcol && c != NUL)
6555 cc = c;
6556 else
6557 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006558 if (WHITECHAR(cc))
6559 {
6560 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006561 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006562
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006563 // find start of sequence of blanks
6564 wcc = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006565 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6566 {
6567 dec_cursor();
6568 cc = gchar_cursor();
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006569
6570 // Increment count of how many whitespace chars in this
6571 // group; we only need to know if it's more than one.
6572 if (wcc < 2)
6573 wcc++;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006574 }
6575 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6576 break; /* only spaces in front of text */
Bram Moolenaarc3c31582019-01-11 22:15:05 +01006577
6578 // Don't break after a period when 'formatoptions' has 'p' and
6579 // there are less than two spaces.
6580 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
6581 continue;
6582
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006583#ifdef FEAT_COMMENTS
6584 /* Don't break until after the comment leader */
6585 if (curwin->w_cursor.col < leader_len)
6586 break;
6587#endif
6588 if (has_format_option(FO_ONE_LETTER))
6589 {
6590 /* do not break after one-letter words */
6591 if (curwin->w_cursor.col == 0)
6592 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006593#ifdef FEAT_COMMENTS
6594 /* do not break "#a b" when 'tw' is 2 */
6595 if (curwin->w_cursor.col <= leader_len)
6596 break;
6597#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006598 col = curwin->w_cursor.col;
6599 dec_cursor();
6600 cc = gchar_cursor();
6601
6602 if (WHITECHAR(cc))
6603 continue; /* one-letter, continue */
6604 curwin->w_cursor.col = col;
6605 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006606
6607 inc_cursor();
6608
6609 end_foundcol = end_col + 1;
6610 foundcol = curwin->w_cursor.col;
6611 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006612 break;
6613 }
6614#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006615 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006616 {
6617 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006618 if (curwin->w_cursor.col != startcol)
6619 {
6620#ifdef FEAT_COMMENTS
6621 /* Don't break until after the comment leader */
6622 if (curwin->w_cursor.col < leader_len)
6623 break;
6624#endif
6625 col = curwin->w_cursor.col;
6626 inc_cursor();
6627 /* Don't change end_foundcol if already set. */
6628 if (foundcol != curwin->w_cursor.col)
6629 {
6630 foundcol = curwin->w_cursor.col;
6631 end_foundcol = foundcol;
6632 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6633 break;
6634 }
6635 curwin->w_cursor.col = col;
6636 }
6637
6638 if (curwin->w_cursor.col == 0)
6639 break;
6640
6641 col = curwin->w_cursor.col;
6642
6643 dec_cursor();
6644 cc = gchar_cursor();
6645
6646 if (WHITECHAR(cc))
6647 continue; /* break with space */
6648#ifdef FEAT_COMMENTS
6649 /* Don't break until after the comment leader */
6650 if (curwin->w_cursor.col < leader_len)
6651 break;
6652#endif
6653
6654 curwin->w_cursor.col = col;
6655
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006656 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006657 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006658 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6659 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006660 }
6661#endif
6662 if (curwin->w_cursor.col == 0)
6663 break;
6664 dec_cursor();
6665 }
6666
6667 if (foundcol == 0) /* no spaces, cannot break line */
6668 {
6669 curwin->w_cursor.col = startcol;
6670 break;
6671 }
6672
6673 /* Going to break the line, remove any "$" now. */
6674 undisplay_dollar();
6675
6676 /*
6677 * Offset between cursor position and line break is used by replace
6678 * stack functions. VREPLACE does not use this, and backspaces
6679 * over the text instead.
6680 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006681 if (State & VREPLACE_FLAG)
6682 orig_col = startcol; /* Will start backspacing from here */
6683 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006684 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006685
6686 /*
6687 * adjust startcol for spaces that will be deleted and
6688 * characters that will remain on top line
6689 */
6690 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006691 while ((cc = gchar_cursor(), WHITECHAR(cc))
6692 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006693 inc_cursor();
6694 startcol -= curwin->w_cursor.col;
6695 if (startcol < 0)
6696 startcol = 0;
6697
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698 if (State & VREPLACE_FLAG)
6699 {
6700 /*
6701 * In VREPLACE mode, we will backspace over the text to be
6702 * wrapped, so save a copy now to put on the next line.
6703 */
6704 saved_text = vim_strsave(ml_get_cursor());
6705 curwin->w_cursor.col = orig_col;
6706 if (saved_text == NULL)
6707 break; /* Can't do it, out of memory */
6708 saved_text[startcol] = NUL;
6709
6710 /* Backspace over characters that will move to the next line */
6711 if (!fo_white_par)
6712 backspace_until_column(foundcol);
6713 }
6714 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006715 {
6716 /* put cursor after pos. to break line */
6717 if (!fo_white_par)
6718 curwin->w_cursor.col = foundcol;
6719 }
6720
6721 /*
6722 * Split the line just before the margin.
6723 * Only insert/delete lines, but don't really redraw the window.
6724 */
6725 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6726 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6727#ifdef FEAT_COMMENTS
6728 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006729 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006730#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006731 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6732 if (!(flags & INSCHAR_COM_LIST))
6733 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006734
6735 replace_offset = 0;
6736 if (first_line)
6737 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006738 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006739 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006740 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006741 * This section is for auto-wrap of numeric lists. When not
6742 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6743 * flag will be set and open_line() will handle it (as seen
6744 * above). The code here (and in get_number_indent()) will
6745 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006746 */
6747 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006748 second_indent =
6749 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006750 if (second_indent >= 0)
6751 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006752 if (State & VREPLACE_FLAG)
6753 change_indent(INDENT_SET, second_indent,
6754 FALSE, NUL, TRUE);
6755 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006756#ifdef FEAT_COMMENTS
6757 if (leader_len > 0 && second_indent - leader_len > 0)
6758 {
6759 int i;
6760 int padding = second_indent - leader_len;
6761
6762 /* We started at the first_line of a numbered list
6763 * that has a comment. the open_line() function has
6764 * inserted the proper comment leader and positioned
6765 * the cursor at the end of the split line. Now we
6766 * add the additional whitespace needed after the
6767 * comment leader for the numbered list. */
6768 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006769 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006770 }
6771 else
6772 {
6773#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006774 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006775#ifdef FEAT_COMMENTS
6776 }
6777#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006778 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006779 }
6780 first_line = FALSE;
6781 }
6782
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006783 if (State & VREPLACE_FLAG)
6784 {
6785 /*
6786 * In VREPLACE mode we have backspaced over the text to be
6787 * moved, now we re-insert it into the new line.
6788 */
6789 ins_bytes(saved_text);
6790 vim_free(saved_text);
6791 }
6792 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006793 {
6794 /*
6795 * Check if cursor is not past the NUL off the line, cindent
6796 * may have added or removed indent.
6797 */
6798 curwin->w_cursor.col += startcol;
6799 len = (colnr_T)STRLEN(ml_get_curline());
6800 if (curwin->w_cursor.col > len)
6801 curwin->w_cursor.col = len;
6802 }
6803
6804 haveto_redraw = TRUE;
6805#ifdef FEAT_CINDENT
6806 can_cindent = TRUE;
6807#endif
6808 /* moved the cursor, don't autoindent or cindent now */
6809 did_ai = FALSE;
6810#ifdef FEAT_SMARTINDENT
6811 did_si = FALSE;
6812 can_si = FALSE;
6813 can_si_back = FALSE;
6814#endif
6815 line_breakcheck();
6816 }
6817
6818 if (save_char != NUL) /* put back space after cursor */
6819 pchar_cursor(save_char);
6820
Bram Moolenaar0026d472014-09-09 16:32:39 +02006821#ifdef FEAT_LINEBREAK
6822 curwin->w_p_lbr = has_lbr;
6823#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006824 if (!format_only && haveto_redraw)
6825 {
6826 update_topline();
6827 redraw_curbuf_later(VALID);
6828 }
6829}
6830
6831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 * Called after inserting or deleting text: When 'formatoptions' includes the
6833 * 'a' flag format from the current line until the end of the paragraph.
6834 * Keep the cursor at the same position relative to the text.
6835 * The caller must have saved the cursor line for undo, following ones will be
6836 * saved here.
6837 */
6838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839auto_format(
6840 int trailblank, /* when TRUE also format with trailing blank */
6841 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 pos_T pos;
6844 colnr_T len;
6845 char_u *old;
6846 char_u *new, *pnew;
6847 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006848 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
6850 if (!has_format_option(FO_AUTO))
6851 return;
6852
6853 pos = curwin->w_cursor;
6854 old = ml_get_curline();
6855
6856 /* may remove added space */
6857 check_auto_format(FALSE);
6858
6859 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6860 * user might insert normal text next. Also skip formatting when "1" is
6861 * in 'formatoptions' and there is a single character before the cursor.
6862 * Otherwise the line would be broken and when typing another non-white
6863 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006864 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (*old != NUL && !trailblank && wasatend)
6866 {
6867 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006868 cc = gchar_cursor();
6869 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6870 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006872 cc = gchar_cursor();
6873 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {
6875 curwin->w_cursor = pos;
6876 return;
6877 }
6878 curwin->w_cursor = pos;
6879 }
6880
6881#ifdef FEAT_COMMENTS
6882 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6883 * comments. */
6884 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006885 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 return;
6887#endif
6888
6889 /*
6890 * May start formatting in a previous line, so that after "x" a word is
6891 * moved to the previous line if it fits there now. Only when this is not
6892 * the start of a paragraph.
6893 */
6894 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6895 {
6896 --curwin->w_cursor.lnum;
6897 if (u_save_cursor() == FAIL)
6898 return;
6899 }
6900
6901 /*
6902 * Do the formatting and restore the cursor position. "saved_cursor" will
6903 * be adjusted for the text formatting.
6904 */
6905 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006906 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 curwin->w_cursor = saved_cursor;
6908 saved_cursor.lnum = 0;
6909
6910 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6911 {
6912 /* "cannot happen" */
6913 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6914 coladvance((colnr_T)MAXCOL);
6915 }
6916 else
6917 check_cursor_col();
6918
6919 /* Insert mode: If the cursor is now after the end of the line while it
6920 * previously wasn't, the line was broken. Because of the rule above we
6921 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6922 * formatted. */
6923 if (!wasatend && has_format_option(FO_WHITE_PAR))
6924 {
6925 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006926 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 if (curwin->w_cursor.col == len)
6928 {
6929 pnew = vim_strnsave(new, len + 2);
6930 pnew[len] = ' ';
6931 pnew[len + 1] = NUL;
6932 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6933 /* remove the space later */
6934 did_add_space = TRUE;
6935 }
6936 else
6937 /* may remove added space */
6938 check_auto_format(FALSE);
6939 }
6940
6941 check_cursor();
6942}
6943
6944/*
6945 * When an extra space was added to continue a paragraph for auto-formatting,
6946 * delete it now. The space must be under the cursor, just after the insert
6947 * position.
6948 */
6949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006950check_auto_format(
6951 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
6953 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006954 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 if (did_add_space)
6957 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006958 cc = gchar_cursor();
6959 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 /* Somehow the space was removed already. */
6961 did_add_space = FALSE;
6962 else
6963 {
6964 if (!end_insert)
6965 {
6966 inc_cursor();
6967 c = gchar_cursor();
6968 dec_cursor();
6969 }
6970 if (c != NUL)
6971 {
6972 /* The space is no longer at the end of the line, delete it. */
6973 del_char(FALSE);
6974 did_add_space = FALSE;
6975 }
6976 }
6977 }
6978}
6979
6980/*
6981 * Find out textwidth to be used for formatting:
6982 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006983 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 * if invalid value, use 0.
6985 * Set default to window width (maximum 79) for "gq" operator.
6986 */
6987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006988comp_textwidth(
6989 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
6991 int textwidth;
6992
6993 textwidth = curbuf->b_p_tw;
6994 if (textwidth == 0 && curbuf->b_p_wm)
6995 {
6996 /* The width is the window width minus 'wrapmargin' minus all the
6997 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006998 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999#ifdef FEAT_CMDWIN
7000 if (cmdwin_type != 0)
7001 textwidth -= 1;
7002#endif
7003#ifdef FEAT_FOLDING
7004 textwidth -= curwin->w_p_fdc;
7005#endif
7006#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007007 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 textwidth -= 1;
7009#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02007010 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 textwidth -= 8;
7012 }
7013 if (textwidth < 0)
7014 textwidth = 0;
7015 if (ff && textwidth == 0)
7016 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007017 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 if (textwidth > 79)
7019 textwidth = 79;
7020 }
7021 return textwidth;
7022}
7023
7024/*
7025 * Put a character in the redo buffer, for when just after a CTRL-V.
7026 */
7027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007028redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 char_u buf[10];
7031
7032 /* Only digits need special treatment. Translate them into a string of
7033 * three digits. */
7034 if (VIM_ISDIGIT(c))
7035 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007036 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 AppendToRedobuff(buf);
7038 }
7039 else
7040 AppendCharToRedobuff(c);
7041}
7042
7043/*
7044 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007045 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 */
7047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007048start_arrow(
7049 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007051 start_arrow_common(end_insert_pos, TRUE);
7052}
7053
7054/*
7055 * Like start_arrow() but with end_change argument.
7056 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7057 */
7058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007059start_arrow_with_change(
7060 pos_T *end_insert_pos, /* can be NULL */
7061 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007062{
7063 start_arrow_common(end_insert_pos, end_change);
7064 if (!end_change)
7065 {
7066 AppendCharToRedobuff(Ctrl_G);
7067 AppendCharToRedobuff('U');
7068 }
7069}
7070
7071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072start_arrow_common(
7073 pos_T *end_insert_pos, /* can be NULL */
7074 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007075{
7076 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {
7078 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007079 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 arrow_used = TRUE; /* this means we stopped the current insert */
7081 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007082#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007083 check_spell_redraw();
7084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085}
7086
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007087#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007088/*
7089 * If we skipped highlighting word at cursor, do it now.
7090 * It may be skipped again, thus reset spell_redraw_lnum first.
7091 */
7092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007093check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007094{
7095 if (spell_redraw_lnum != 0)
7096 {
7097 linenr_T lnum = spell_redraw_lnum;
7098
7099 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01007100 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007101 }
7102}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007103
7104/*
7105 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7106 * spelled word, if there is one.
7107 */
7108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007110{
7111 pos_T tpos = curwin->w_cursor;
7112
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007113 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007114 if (curwin->w_cursor.col != tpos.col)
7115 start_arrow(&tpos);
7116}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007117#endif
7118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119/*
7120 * stop_arrow() is called before a change is made in insert mode.
7121 * If an arrow key has been used, start a new insertion.
7122 * Returns FAIL if undo is impossible, shouldn't insert then.
7123 */
7124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007125stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
7127 if (arrow_used)
7128 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007129 Insstart = curwin->w_cursor; /* new insertion starts here */
7130 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7131 /* Don't update the original insert position when moved to the
7132 * right, except when nothing was inserted yet. */
7133 update_Insstart_orig = FALSE;
7134 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7135
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (u_save_cursor() == OK)
7137 {
7138 arrow_used = FALSE;
7139 ins_need_undo = FALSE;
7140 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007141
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 if (State & VREPLACE_FLAG)
7144 {
7145 orig_line_count = curbuf->b_ml.ml_line_count;
7146 vr_lines_changed = 1;
7147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 ResetRedobuff();
7149 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007150 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
7152 else if (ins_need_undo)
7153 {
7154 if (u_save_cursor() == OK)
7155 ins_need_undo = FALSE;
7156 }
7157
7158#ifdef FEAT_FOLDING
7159 /* Always open fold at the cursor line when inserting something. */
7160 foldOpenCursor();
7161#endif
7162
7163 return (arrow_used || ins_need_undo ? FAIL : OK);
7164}
7165
7166/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007167 * Do a few things to stop inserting.
7168 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7169 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 */
7171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007172stop_insert(
7173 pos_T *end_insert_pos,
7174 int esc, /* called by ins_esc() */
7175 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007177 int cc;
7178 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
7180 stop_redo_ins();
7181 replace_flush(); /* abandon replace stack */
7182
7183 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007184 * Save the inserted text for later redo with ^@ and CTRL-A.
7185 * Don't do it when "restart_edit" was set and nothing was inserted,
7186 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007188 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007189 if (did_restart_edit == 0 || (ptr != NULL
7190 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007191 {
7192 vim_free(last_insert);
7193 last_insert = ptr;
7194 last_insert_skip = new_insert_skip;
7195 }
7196 else
7197 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007199 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
7201 /* Auto-format now. It may seem strange to do this when stopping an
7202 * insertion (or moving the cursor), but it's required when appending
7203 * a line and having it end in a space. But only do it when something
7204 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007205 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007207 pos_T tpos = curwin->w_cursor;
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 /* When the cursor is at the end of the line after a space the
7210 * formatting will move it to the following word. Avoid that by
7211 * moving the cursor onto the space. */
7212 cc = 'x';
7213 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7214 {
7215 dec_cursor();
7216 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007217 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007218 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
7220
7221 auto_format(TRUE, FALSE);
7222
Bram Moolenaar1c465442017-03-12 20:10:05 +01007223 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007224 {
7225 if (gchar_cursor() != NUL)
7226 inc_cursor();
7227#ifdef FEAT_VIRTUALEDIT
7228 /* If the cursor is still at the same character, also keep
7229 * the "coladd". */
7230 if (gchar_cursor() == NUL
7231 && curwin->w_cursor.lnum == tpos.lnum
7232 && curwin->w_cursor.col == tpos.col)
7233 curwin->w_cursor.coladd = tpos.coladd;
7234#endif
7235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 }
7237
7238 /* If a space was inserted for auto-formatting, remove it now. */
7239 check_auto_format(TRUE);
7240
7241 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007242 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007243 * Do this when ESC was used or moving the cursor up/down.
7244 * Check for the old position still being valid, just in case the text
7245 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007246 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007247 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7248 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007250 pos_T tpos = curwin->w_cursor;
7251
7252 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007253 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007254 for (;;)
7255 {
7256 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7257 --curwin->w_cursor.col;
7258 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007259 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007260 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007261 if (del_char(TRUE) == FAIL)
7262 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007263 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007264 if (curwin->w_cursor.lnum != tpos.lnum)
7265 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007266 else
7267 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007268 /* reset tpos, could have been invalidated in the loop above */
7269 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007270 tpos.col++;
7271 if (cc != NUL && gchar_pos(&tpos) == NUL)
7272 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 /* <C-S-Right> may have started Visual mode, adjust the position for
7276 * deleted characters. */
7277 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7278 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007279 int len = (int)STRLEN(ml_get_curline());
7280
7281 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007283 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007284#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 }
7288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290 }
7291 did_ai = FALSE;
7292#ifdef FEAT_SMARTINDENT
7293 did_si = FALSE;
7294 can_si = FALSE;
7295 can_si_back = FALSE;
7296#endif
7297
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007298 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7299 * now in a different buffer. */
7300 if (end_insert_pos != NULL)
7301 {
7302 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007303 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007304 curbuf->b_op_end = *end_insert_pos;
7305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306}
7307
7308/*
7309 * Set the last inserted text to a single character.
7310 * Used for the replace command.
7311 */
7312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
7315 char_u *s;
7316
7317 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 if (last_insert != NULL)
7320 {
7321 s = last_insert;
7322 /* Use the CTRL-V only when entering a special char */
7323 if (c < ' ' || c == DEL)
7324 *s++ = Ctrl_V;
7325 s = add_char2buf(c, s);
7326 *s++ = ESC;
7327 *s++ = NUL;
7328 last_insert_skip = 0;
7329 }
7330}
7331
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007332#if defined(EXITFREE) || defined(PROTO)
7333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007334free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007335{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007336 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007337# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007338 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007339# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007340}
7341#endif
7342
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343/*
7344 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7345 * and CSI. Handle multi-byte characters.
7346 * Returns a pointer to after the added bytes.
7347 */
7348 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007349add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
7351#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007352 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 int i;
7354 int len;
7355
7356 len = (*mb_char2bytes)(c, temp);
7357 for (i = 0; i < len; ++i)
7358 {
7359 c = temp[i];
7360#endif
7361 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7362 if (c == K_SPECIAL)
7363 {
7364 *s++ = K_SPECIAL;
7365 *s++ = KS_SPECIAL;
7366 *s++ = KE_FILLER;
7367 }
7368#ifdef FEAT_GUI
7369 else if (c == CSI)
7370 {
7371 *s++ = CSI;
7372 *s++ = KS_EXTRA;
7373 *s++ = (int)KE_CSI;
7374 }
7375#endif
7376 else
7377 *s++ = c;
7378#ifdef FEAT_MBYTE
7379 }
7380#endif
7381 return s;
7382}
7383
7384/*
7385 * move cursor to start of line
7386 * if flags & BL_WHITE move to first non-white
7387 * if flags & BL_SOL move to first non-white if startofline is set,
7388 * otherwise keep "curswant" column
7389 * if flags & BL_FIX don't leave the cursor on a NUL.
7390 */
7391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007392beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 if ((flags & BL_SOL) && !p_sol)
7395 coladvance(curwin->w_curswant);
7396 else
7397 {
7398 curwin->w_cursor.col = 0;
7399#ifdef FEAT_VIRTUALEDIT
7400 curwin->w_cursor.coladd = 0;
7401#endif
7402
7403 if (flags & (BL_WHITE | BL_SOL))
7404 {
7405 char_u *ptr;
7406
Bram Moolenaar1c465442017-03-12 20:10:05 +01007407 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7409 ++curwin->w_cursor.col;
7410 }
7411 curwin->w_set_curswant = TRUE;
7412 }
7413}
7414
7415/*
7416 * oneright oneleft cursor_down cursor_up
7417 *
7418 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007419 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 * Return OK when successful, FAIL when we hit a line of file boundary.
7421 */
7422
7423 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
7429#ifdef FEAT_VIRTUALEDIT
7430 if (virtual_active())
7431 {
7432 pos_T prevpos = curwin->w_cursor;
7433
7434 /* Adjust for multi-wide char (excluding TAB) */
7435 ptr = ml_get_cursor();
7436 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007437# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007439# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 ))
7443 ? ptr2cells(ptr) : 1));
7444 curwin->w_set_curswant = TRUE;
7445 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7446 return (prevpos.col != curwin->w_cursor.col
7447 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7448 }
7449#endif
7450
7451 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007452 if (*ptr == NUL)
7453 return FAIL; /* already at the very end */
7454
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007456 if (has_mbyte)
7457 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 else
7459#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007460 l = 1;
7461
7462 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7463 * contains "onemore". */
7464 if (ptr[l] == NUL
7465#ifdef FEAT_VIRTUALEDIT
7466 && (ve_flags & VE_ONEMORE) == 0
7467#endif
7468 )
7469 return FAIL;
7470 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
7472 curwin->w_set_curswant = TRUE;
7473 return OK;
7474}
7475
7476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007477oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478{
7479#ifdef FEAT_VIRTUALEDIT
7480 if (virtual_active())
7481 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007482# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 int v = getviscol();
7486
7487 if (v == 0)
7488 return FAIL;
7489
7490# ifdef FEAT_LINEBREAK
7491 /* We might get stuck on 'showbreak', skip over it. */
7492 width = 1;
7493 for (;;)
7494 {
7495 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007496 /* getviscol() is slow, skip it when 'showbreak' is empty,
7497 * 'breakindent' is not set and there are no multi-byte
7498 * characters */
7499 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500# ifdef FEAT_MBYTE
7501 && !has_mbyte
7502# endif
7503 ) || getviscol() < v)
7504 break;
7505 ++width;
7506 }
7507# else
7508 coladvance(v - 1);
7509# endif
7510
7511 if (curwin->w_cursor.coladd == 1)
7512 {
7513 char_u *ptr;
7514
7515 /* Adjust for multi-wide char (not a TAB) */
7516 ptr = ml_get_cursor();
7517 if (*ptr != TAB && vim_isprintc(
7518# ifdef FEAT_MBYTE
7519 (*mb_ptr2char)(ptr)
7520# else
7521 *ptr
7522# endif
7523 ) && ptr2cells(ptr) > 1)
7524 curwin->w_cursor.coladd = 0;
7525 }
7526
7527 curwin->w_set_curswant = TRUE;
7528 return OK;
7529 }
7530#endif
7531
7532 if (curwin->w_cursor.col == 0)
7533 return FAIL;
7534
7535 curwin->w_set_curswant = TRUE;
7536 --curwin->w_cursor.col;
7537
7538#ifdef FEAT_MBYTE
7539 /* if the character on the left of the current cursor is a multi-byte
7540 * character, move to its first byte */
7541 if (has_mbyte)
7542 mb_adjust_cursor();
7543#endif
7544 return OK;
7545}
7546
7547 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548cursor_up(
7549 long n,
7550 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551{
7552 linenr_T lnum;
7553
7554 if (n > 0)
7555 {
7556 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007557 /* This fails if the cursor is already in the first line or the count
7558 * is larger than the line number and '-' is in 'cpoptions' */
7559 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 return FAIL;
7561 if (n >= lnum)
7562 lnum = 1;
7563 else
7564#ifdef FEAT_FOLDING
7565 if (hasAnyFolding(curwin))
7566 {
7567 /*
7568 * Count each sequence of folded lines as one logical line.
7569 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007570 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 (void)hasFolding(lnum, &lnum, NULL);
7572
7573 while (n--)
7574 {
7575 /* move up one line */
7576 --lnum;
7577 if (lnum <= 1)
7578 break;
7579 /* If we entered a fold, move to the beginning, unless in
7580 * Insert mode or when 'foldopen' contains "all": it will open
7581 * in a moment. */
7582 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7583 (void)hasFolding(lnum, &lnum, NULL);
7584 }
7585 if (lnum < 1)
7586 lnum = 1;
7587 }
7588 else
7589#endif
7590 lnum -= n;
7591 curwin->w_cursor.lnum = lnum;
7592 }
7593
7594 /* try to advance to the column we want to be at */
7595 coladvance(curwin->w_curswant);
7596
7597 if (upd_topline)
7598 update_topline(); /* make sure curwin->w_topline is valid */
7599
7600 return OK;
7601}
7602
7603/*
7604 * Cursor down a number of logical lines.
7605 */
7606 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007607cursor_down(
7608 long n,
7609 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610{
7611 linenr_T lnum;
7612
7613 if (n > 0)
7614 {
7615 lnum = curwin->w_cursor.lnum;
7616#ifdef FEAT_FOLDING
7617 /* Move to last line of fold, will fail if it's the end-of-file. */
7618 (void)hasFolding(lnum, NULL, &lnum);
7619#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007620 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007621 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007622 if (lnum >= curbuf->b_ml.ml_line_count
7623 || (lnum + n > curbuf->b_ml.ml_line_count
7624 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 return FAIL;
7626 if (lnum + n >= curbuf->b_ml.ml_line_count)
7627 lnum = curbuf->b_ml.ml_line_count;
7628 else
7629#ifdef FEAT_FOLDING
7630 if (hasAnyFolding(curwin))
7631 {
7632 linenr_T last;
7633
7634 /* count each sequence of folded lines as one logical line */
7635 while (n--)
7636 {
7637 if (hasFolding(lnum, NULL, &last))
7638 lnum = last + 1;
7639 else
7640 ++lnum;
7641 if (lnum >= curbuf->b_ml.ml_line_count)
7642 break;
7643 }
7644 if (lnum > curbuf->b_ml.ml_line_count)
7645 lnum = curbuf->b_ml.ml_line_count;
7646 }
7647 else
7648#endif
7649 lnum += n;
7650 curwin->w_cursor.lnum = lnum;
7651 }
7652
7653 /* try to advance to the column we want to be at */
7654 coladvance(curwin->w_curswant);
7655
7656 if (upd_topline)
7657 update_topline(); /* make sure curwin->w_topline is valid */
7658
7659 return OK;
7660}
7661
7662/*
7663 * Stuff the last inserted text in the read buffer.
7664 * Last_insert actually is a copy of the redo buffer, so we
7665 * first have to remove the command.
7666 */
7667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007668stuff_inserted(
7669 int c, /* Command character to be inserted */
7670 long count, /* Repeat this many times */
7671 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
7673 char_u *esc_ptr;
7674 char_u *ptr;
7675 char_u *last_ptr;
7676 char_u last = NUL;
7677
7678 ptr = get_last_insert();
7679 if (ptr == NULL)
7680 {
7681 EMSG(_(e_noinstext));
7682 return FAIL;
7683 }
7684
7685 /* may want to stuff the command character, to start Insert mode */
7686 if (c != NUL)
7687 stuffcharReadbuff(c);
7688 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7689 *esc_ptr = NUL; /* remove the ESC */
7690
7691 /* when the last char is either "0" or "^" it will be quoted if no ESC
7692 * comes after it OR if it will inserted more than once and "ptr"
7693 * starts with ^D. -- Acevedo
7694 */
7695 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7696 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7697 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7698 {
7699 last = *last_ptr;
7700 *last_ptr = NUL;
7701 }
7702
7703 do
7704 {
7705 stuffReadbuff(ptr);
7706 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7707 if (last)
7708 stuffReadbuff((char_u *)(last == '0'
7709 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7710 : IF_EB("\026^", CTRL_V_STR "^")));
7711 }
7712 while (--count > 0);
7713
7714 if (last)
7715 *last_ptr = last;
7716
7717 if (esc_ptr != NULL)
7718 *esc_ptr = ESC; /* put the ESC back */
7719
7720 /* may want to stuff a trailing ESC, to get out of Insert mode */
7721 if (!no_esc)
7722 stuffcharReadbuff(ESC);
7723
7724 return OK;
7725}
7726
7727 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007728get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 if (last_insert == NULL)
7731 return NULL;
7732 return last_insert + last_insert_skip;
7733}
7734
7735/*
7736 * Get last inserted string, and remove trailing <Esc>.
7737 * Returns pointer to allocated memory (must be freed) or NULL.
7738 */
7739 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007740get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
7742 char_u *s;
7743 int len;
7744
7745 if (last_insert == NULL)
7746 return NULL;
7747 s = vim_strsave(last_insert + last_insert_skip);
7748 if (s != NULL)
7749 {
7750 len = (int)STRLEN(s);
7751 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7752 s[len - 1] = NUL;
7753 }
7754 return s;
7755}
7756
7757/*
7758 * Check the word in front of the cursor for an abbreviation.
7759 * Called when the non-id character "c" has been entered.
7760 * When an abbreviation is recognized it is removed from the text and
7761 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7762 */
7763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007764echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
7766 /* Don't check for abbreviation in paste mode, when disabled and just
7767 * after moving around with cursor keys. */
7768 if (p_paste || no_abbr || arrow_used)
7769 return FALSE;
7770
7771 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7772 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7773}
7774
7775/*
7776 * replace-stack functions
7777 *
7778 * When replacing characters, the replaced characters are remembered for each
7779 * new character. This is used to re-insert the old text when backspacing.
7780 *
7781 * There is a NUL headed list of characters for each character that is
7782 * currently in the file after the insertion point. When BS is used, one NUL
7783 * headed list is put back for the deleted character.
7784 *
7785 * For a newline, there are two NUL headed lists. One contains the characters
7786 * that the NL replaced. The extra one stores the characters after the cursor
7787 * that were deleted (always white space).
7788 *
7789 * Replace_offset is normally 0, in which case replace_push will add a new
7790 * character at the end of the stack. If replace_offset is not 0, that many
7791 * characters will be left on the stack above the newly inserted character.
7792 */
7793
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007794static char_u *replace_stack = NULL;
7795static long replace_stack_nr = 0; /* next entry in replace stack */
7796static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
7798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007799replace_push(
7800 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802 char_u *p;
7803
7804 if (replace_stack_nr < replace_offset) /* nothing to do */
7805 return;
7806 if (replace_stack_len <= replace_stack_nr)
7807 {
7808 replace_stack_len += 50;
7809 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7810 if (p == NULL) /* out of memory */
7811 {
7812 replace_stack_len -= 50;
7813 return;
7814 }
7815 if (replace_stack != NULL)
7816 {
7817 mch_memmove(p, replace_stack,
7818 (size_t)(replace_stack_nr * sizeof(char_u)));
7819 vim_free(replace_stack);
7820 }
7821 replace_stack = p;
7822 }
7823 p = replace_stack + replace_stack_nr - replace_offset;
7824 if (replace_offset)
7825 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7826 *p = c;
7827 ++replace_stack_nr;
7828}
7829
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007830#if defined(FEAT_MBYTE) || defined(PROTO)
7831/*
7832 * Push a character onto the replace stack. Handles a multi-byte character in
7833 * reverse byte order, so that the first byte is popped off first.
7834 * Return the number of bytes done (includes composing characters).
7835 */
7836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007837replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007838{
7839 int l = (*mb_ptr2len)(p);
7840 int j;
7841
7842 for (j = l - 1; j >= 0; --j)
7843 replace_push(p[j]);
7844 return l;
7845}
7846#endif
7847
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848/*
7849 * Pop one item from the replace stack.
7850 * return -1 if stack empty
7851 * return replaced character or NUL otherwise
7852 */
7853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007854replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 if (replace_stack_nr == 0)
7857 return -1;
7858 return (int)replace_stack[--replace_stack_nr];
7859}
7860
7861/*
7862 * Join the top two items on the replace stack. This removes to "off"'th NUL
7863 * encountered.
7864 */
7865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007866replace_join(
7867 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 int i;
7870
7871 for (i = replace_stack_nr; --i >= 0; )
7872 if (replace_stack[i] == NUL && off-- <= 0)
7873 {
7874 --replace_stack_nr;
7875 mch_memmove(replace_stack + i, replace_stack + i + 1,
7876 (size_t)(replace_stack_nr - i));
7877 return;
7878 }
7879}
7880
7881/*
7882 * Pop bytes from the replace stack until a NUL is found, and insert them
7883 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7884 */
7885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007886replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 int cc;
7889 int oldState = State;
7890
7891 State = NORMAL; /* don't want REPLACE here */
7892 while ((cc = replace_pop()) > 0)
7893 {
7894#ifdef FEAT_MBYTE
7895 mb_replace_pop_ins(cc);
7896#else
7897 ins_char(cc);
7898#endif
7899 dec_cursor();
7900 }
7901 State = oldState;
7902}
7903
7904#ifdef FEAT_MBYTE
7905/*
7906 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7907 * indicates a multi-byte char, pop the other bytes too.
7908 */
7909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007910mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007913 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 int i;
7915 int c;
7916
7917 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7918 {
7919 buf[0] = cc;
7920 for (i = 1; i < n; ++i)
7921 buf[i] = replace_pop();
7922 ins_bytes_len(buf, n);
7923 }
7924 else
7925 ins_char(cc);
7926
7927 if (enc_utf8)
7928 /* Handle composing chars. */
7929 for (;;)
7930 {
7931 c = replace_pop();
7932 if (c == -1) /* stack empty */
7933 break;
7934 if ((n = MB_BYTE2LEN(c)) == 1)
7935 {
7936 /* Not a multi-byte char, put it back. */
7937 replace_push(c);
7938 break;
7939 }
7940 else
7941 {
7942 buf[0] = c;
7943 for (i = 1; i < n; ++i)
7944 buf[i] = replace_pop();
7945 if (utf_iscomposing(utf_ptr2char(buf)))
7946 ins_bytes_len(buf, n);
7947 else
7948 {
7949 /* Not a composing char, put it back. */
7950 for (i = n - 1; i >= 0; --i)
7951 replace_push(buf[i]);
7952 break;
7953 }
7954 }
7955 }
7956}
7957#endif
7958
7959/*
7960 * make the replace stack empty
7961 * (called when exiting replace mode)
7962 */
7963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007964replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007966 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 replace_stack_len = 0;
7968 replace_stack_nr = 0;
7969}
7970
7971/*
7972 * Handle doing a BS for one character.
7973 * cc < 0: replace stack empty, just move cursor
7974 * cc == 0: character was inserted, delete it
7975 * cc > 0: character was replaced, put cc (first byte of original char) back
7976 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007977 * When "limit_col" is >= 0, don't delete before this column. Matters when
7978 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 */
7980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007981replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 int orig_len = 0;
7985 int ins_len;
7986 int orig_vcols = 0;
7987 colnr_T start_vcol;
7988 char_u *p;
7989 int i;
7990 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991
7992 cc = replace_pop();
7993 if (cc > 0)
7994 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007995#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007996 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007997
7998 if (curbuf->b_has_textprop)
7999 {
8000 // Do not adjust text properties for individual delete and insert
8001 // operations, do it afterwards on the resulting text.
8002 len_before = STRLEN(ml_get_curline());
8003 ++text_prop_frozen;
8004 }
8005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 if (State & VREPLACE_FLAG)
8007 {
8008 /* Get the number of screen cells used by the character we are
8009 * going to delete. */
8010 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
8011 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
8012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013#ifdef FEAT_MBYTE
8014 if (has_mbyte)
8015 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008016 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008018 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 replace_push(cc);
8020 }
8021 else
8022#endif
8023 {
8024 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008026 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 }
8028 replace_pop_ins();
8029
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 if (State & VREPLACE_FLAG)
8031 {
8032 /* Get the number of screen cells used by the inserted characters */
8033 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008034 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 vcol = start_vcol;
8036 for (i = 0; i < ins_len; ++i)
8037 {
8038 vcol += chartabsize(p + i, vcol);
8039#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008040 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041#endif
8042 }
8043 vcol -= start_vcol;
8044
8045 /* Delete spaces that were inserted after the cursor to keep the
8046 * text aligned. */
8047 curwin->w_cursor.col += ins_len;
8048 while (vcol > orig_vcols && gchar_cursor() == ' ')
8049 {
8050 del_char(FALSE);
8051 ++orig_vcols;
8052 }
8053 curwin->w_cursor.col -= ins_len;
8054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055
Bram Moolenaar196d1572019-01-02 23:47:18 +01008056 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01008058
8059#ifdef FEAT_TEXT_PROP
8060 if (curbuf->b_has_textprop)
8061 {
8062 size_t len_now = STRLEN(ml_get_curline());
8063
8064 --text_prop_frozen;
8065 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
8066 (int)(len_now - len_before));
8067 }
8068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 }
8070 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008071 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072}
8073
8074#ifdef FEAT_CINDENT
8075/*
8076 * Return TRUE if C-indenting is on.
8077 */
8078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008079cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
8081 return (!p_paste && (curbuf->b_p_cin
8082# ifdef FEAT_EVAL
8083 || *curbuf->b_p_inde != NUL
8084# endif
8085 ));
8086}
8087#endif
8088
8089#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8090/*
8091 * Re-indent the current line, based on the current contents of it and the
8092 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8093 * confused what all the part that handles Control-T is doing that I'm not.
8094 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8095 */
8096
8097 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008098fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008100 int amount = get_the_indent();
8101
8102 if (amount >= 0)
8103 {
8104 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8105 if (linewhite(curwin->w_cursor.lnum))
8106 did_ai = TRUE; /* delete the indent if the line stays empty */
8107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108}
8109
8110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008111fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
8113 if (p_paste)
8114 return;
8115# ifdef FEAT_LISP
8116 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8117 fixthisline(get_lisp_indent);
8118# endif
8119# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8120 else
8121# endif
8122# ifdef FEAT_CINDENT
8123 if (cindent_on())
8124 do_c_expr_indent();
8125# endif
8126}
8127
8128#endif
8129
8130#ifdef FEAT_CINDENT
8131/*
8132 * return TRUE if 'cinkeys' contains the key "keytyped",
8133 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008134 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8136 *
8137 * "keytyped" can have a few special values:
8138 * KEY_OPEN_FORW
8139 * KEY_OPEN_BACK
8140 * KEY_COMPLETE just finished completion.
8141 *
8142 * If line_is_empty is TRUE accept keys with '0' before them.
8143 */
8144 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008145in_cinkeys(
8146 int keytyped,
8147 int when,
8148 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
8150 char_u *look;
8151 int try_match;
8152 int try_match_word;
8153 char_u *p;
8154 char_u *line;
8155 int icase;
8156 int i;
8157
Bram Moolenaar30848942009-12-24 14:46:12 +00008158 if (keytyped == NUL)
8159 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8160 return FALSE;
8161
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162#ifdef FEAT_EVAL
8163 if (*curbuf->b_p_inde != NUL)
8164 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8165 else
8166#endif
8167 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8168 while (*look)
8169 {
8170 /*
8171 * Find out if we want to try a match with this key, depending on
8172 * 'when' and a '*' or '!' before the key.
8173 */
8174 switch (when)
8175 {
8176 case '*': try_match = (*look == '*'); break;
8177 case '!': try_match = (*look == '!'); break;
8178 default: try_match = (*look != '*'); break;
8179 }
8180 if (*look == '*' || *look == '!')
8181 ++look;
8182
8183 /*
8184 * If there is a '0', only accept a match if the line is empty.
8185 * But may still match when typing last char of a word.
8186 */
8187 if (*look == '0')
8188 {
8189 try_match_word = try_match;
8190 if (!line_is_empty)
8191 try_match = FALSE;
8192 ++look;
8193 }
8194 else
8195 try_match_word = FALSE;
8196
8197 /*
8198 * does it look like a control character?
8199 */
8200 if (*look == '^'
8201#ifdef EBCDIC
8202 && (Ctrl_chr(look[1]) != 0)
8203#else
8204 && look[1] >= '?' && look[1] <= '_'
8205#endif
8206 )
8207 {
8208 if (try_match && keytyped == Ctrl_chr(look[1]))
8209 return TRUE;
8210 look += 2;
8211 }
8212 /*
8213 * 'o' means "o" command, open forward.
8214 * 'O' means "O" command, open backward.
8215 */
8216 else if (*look == 'o')
8217 {
8218 if (try_match && keytyped == KEY_OPEN_FORW)
8219 return TRUE;
8220 ++look;
8221 }
8222 else if (*look == 'O')
8223 {
8224 if (try_match && keytyped == KEY_OPEN_BACK)
8225 return TRUE;
8226 ++look;
8227 }
8228
8229 /*
8230 * 'e' means to check for "else" at start of line and just before the
8231 * cursor.
8232 */
8233 else if (*look == 'e')
8234 {
8235 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8236 {
8237 p = ml_get_curline();
8238 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8239 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8240 return TRUE;
8241 }
8242 ++look;
8243 }
8244
8245 /*
8246 * ':' only causes an indent if it is at the end of a label or case
8247 * statement, or when it was before typing the ':' (to fix
8248 * class::method for C++).
8249 */
8250 else if (*look == ':')
8251 {
8252 if (try_match && keytyped == ':')
8253 {
8254 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008255 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008257 /* Need to get the line again after cin_islabel(). */
8258 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 if (curwin->w_cursor.col > 2
8260 && p[curwin->w_cursor.col - 1] == ':'
8261 && p[curwin->w_cursor.col - 2] == ':')
8262 {
8263 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008264 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008265 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 p = ml_get_curline();
8267 p[curwin->w_cursor.col - 1] = ':';
8268 if (i)
8269 return TRUE;
8270 }
8271 }
8272 ++look;
8273 }
8274
8275
8276 /*
8277 * Is it a key in <>, maybe?
8278 */
8279 else if (*look == '<')
8280 {
8281 if (try_match)
8282 {
8283 /*
8284 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8285 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8286 * >, *, : and ! keys if they really really want to.
8287 */
8288 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8289 && keytyped == look[1])
8290 return TRUE;
8291
8292 if (keytyped == get_special_key_code(look + 1))
8293 return TRUE;
8294 }
8295 while (*look && *look != '>')
8296 look++;
8297 while (*look == '>')
8298 look++;
8299 }
8300
8301 /*
8302 * Is it a word: "=word"?
8303 */
8304 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8305 {
8306 ++look;
8307 if (*look == '~')
8308 {
8309 icase = TRUE;
8310 ++look;
8311 }
8312 else
8313 icase = FALSE;
8314 p = vim_strchr(look, ',');
8315 if (p == NULL)
8316 p = look + STRLEN(look);
8317 if ((try_match || try_match_word)
8318 && curwin->w_cursor.col >= (colnr_T)(p - look))
8319 {
8320 int match = FALSE;
8321
8322#ifdef FEAT_INS_EXPAND
8323 if (keytyped == KEY_COMPLETE)
8324 {
8325 char_u *s;
8326
8327 /* Just completed a word, check if it starts with "look".
8328 * search back for the start of a word. */
8329 line = ml_get_curline();
8330# ifdef FEAT_MBYTE
8331 if (has_mbyte)
8332 {
8333 char_u *n;
8334
8335 for (s = line + curwin->w_cursor.col; s > line; s = n)
8336 {
8337 n = mb_prevptr(line, s);
8338 if (!vim_iswordp(n))
8339 break;
8340 }
8341 }
8342 else
8343# endif
8344 for (s = line + curwin->w_cursor.col; s > line; --s)
8345 if (!vim_iswordc(s[-1]))
8346 break;
8347 if (s + (p - look) <= line + curwin->w_cursor.col
8348 && (icase
8349 ? MB_STRNICMP(s, look, p - look)
8350 : STRNCMP(s, look, p - look)) == 0)
8351 match = TRUE;
8352 }
8353 else
8354#endif
8355 /* TODO: multi-byte */
8356 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8357 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8358 {
8359 line = ml_get_cursor();
8360 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8361 || !vim_iswordc(line[-(p - look) - 1]))
8362 && (icase
8363 ? MB_STRNICMP(line - (p - look), look, p - look)
8364 : STRNCMP(line - (p - look), look, p - look))
8365 == 0)
8366 match = TRUE;
8367 }
8368 if (match && try_match_word && !try_match)
8369 {
8370 /* "0=word": Check if there are only blanks before the
8371 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008372 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 (int)(curwin->w_cursor.col - (p - look)))
8374 match = FALSE;
8375 }
8376 if (match)
8377 return TRUE;
8378 }
8379 look = p;
8380 }
8381
8382 /*
8383 * ok, it's a boring generic character.
8384 */
8385 else
8386 {
8387 if (try_match && *look == keytyped)
8388 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008389 if (*look != NUL)
8390 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 }
8392
8393 /*
8394 * Skip over ", ".
8395 */
8396 look = skip_to_option_part(look);
8397 }
8398 return FALSE;
8399}
8400#endif /* FEAT_CINDENT */
8401
8402#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8403/*
8404 * Map Hebrew keyboard when in hkmap mode.
8405 */
8406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008407hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408{
8409 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8410 {
8411 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8412 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8413 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8414 static char_u map[26] =
8415 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8416 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8417 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8418 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8419 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8420 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8421 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8422 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8423 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8424
8425 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8426 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8427 /* '-1'='sofit' */
8428 else if (c == 'x')
8429 return 'X';
8430 else if (c == 'q')
8431 return '\''; /* {geresh}={'} */
8432 else if (c == 246)
8433 return ' '; /* \"o --> ' ' for a german keyboard */
8434 else if (c == 228)
8435 return ' '; /* \"a --> ' ' -- / -- */
8436 else if (c == 252)
8437 return ' '; /* \"u --> ' ' -- / -- */
8438#ifdef EBCDIC
8439 else if (islower(c))
8440#else
8441 /* NOTE: islower() does not do the right thing for us on Linux so we
8442 * do this the same was as 5.7 and previous, so it works correctly on
8443 * all systems. Specifically, the e.g. Delete and Arrow keys are
8444 * munged and won't work if e.g. searching for Hebrew text.
8445 */
8446 else if (c >= 'a' && c <= 'z')
8447#endif
8448 return (int)(map[CharOrdLow(c)] + p_aleph);
8449 else
8450 return c;
8451 }
8452 else
8453 {
8454 switch (c)
8455 {
8456 case '`': return ';';
8457 case '/': return '.';
8458 case '\'': return ',';
8459 case 'q': return '/';
8460 case 'w': return '\'';
8461
8462 /* Hebrew letters - set offset from 'a' */
8463 case ',': c = '{'; break;
8464 case '.': c = 'v'; break;
8465 case ';': c = 't'; break;
8466 default: {
8467 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8468
8469#ifdef EBCDIC
8470 /* see note about islower() above */
8471 if (!islower(c))
8472#else
8473 if (c < 'a' || c > 'z')
8474#endif
8475 return c;
8476 c = str[CharOrdLow(c)];
8477 break;
8478 }
8479 }
8480
8481 return (int)(CharOrdLow(c) + p_aleph);
8482 }
8483}
8484#endif
8485
8486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008487ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
8489 int need_redraw = FALSE;
8490 int regname;
8491 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008492 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
8494 /*
8495 * If we are going to wait for a character, show a '"'.
8496 */
8497 pc_status = PC_STATUS_UNSET;
8498 if (redrawing() && !char_avail())
8499 {
8500 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008501 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502
8503 edit_putchar('"', TRUE);
8504#ifdef FEAT_CMDL_INFO
8505 add_to_showcmd_c(Ctrl_R);
8506#endif
8507 }
8508
8509#ifdef USE_ON_FLY_SCROLL
8510 dont_scroll = TRUE; /* disallow scrolling here */
8511#endif
8512
8513 /*
8514 * Don't map the register name. This also prevents the mode message to be
8515 * deleted when ESC is hit.
8516 */
8517 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008518 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8521 {
8522 /* Get a third key for literal register insertion */
8523 literally = regname;
8524#ifdef FEAT_CMDL_INFO
8525 add_to_showcmd_c(literally);
8526#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008527 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 }
8530 --no_mapping;
8531
8532#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008533 /* Don't call u_sync() while typing the expression or giving an error
8534 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 ++no_u_sync;
8536 if (regname == '=')
8537 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008538# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008540# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008541 /* Sync undo when evaluating the expression calls setline() or
8542 * append(), so that it can be undone separately. */
8543 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008544
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008546# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 /* Restore the Input Method. */
8548 if (im_on)
8549 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008552 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8553 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008554 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 else
8558 {
8559#endif
8560 if (literally == Ctrl_O || literally == Ctrl_P)
8561 {
8562 /* Append the command to the redo buffer. */
8563 AppendCharToRedobuff(Ctrl_R);
8564 AppendCharToRedobuff(literally);
8565 AppendCharToRedobuff(regname);
8566
8567 do_put(regname, BACKWARD, 1L,
8568 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8569 }
8570 else if (insert_reg(regname, literally) == FAIL)
8571 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008572 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 need_redraw = TRUE; /* remove the '"' */
8574 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008575 else if (stop_insert_mode)
8576 /* When the '=' register was used and a function was invoked that
8577 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8578 * insert anything, need to remove the '"' */
8579 need_redraw = TRUE;
8580
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#ifdef FEAT_EVAL
8582 }
8583 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008584 if (u_sync_once == 1)
8585 ins_need_undo = TRUE;
8586 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587#endif
8588#ifdef FEAT_CMDL_INFO
8589 clear_showcmd();
8590#endif
8591
8592 /* If the inserted register is empty, we need to remove the '"' */
8593 if (need_redraw || stuff_empty())
8594 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008595
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008596 /* Disallow starting Visual mode here, would get a weird mode. */
8597 if (!vis_active && VIsual_active)
8598 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599}
8600
8601/*
8602 * CTRL-G commands in Insert mode.
8603 */
8604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008605ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607 int c;
8608
8609#ifdef FEAT_INS_EXPAND
8610 /* Right after CTRL-X the cursor will be after the ruler. */
8611 setcursor();
8612#endif
8613
8614 /*
8615 * Don't map the second key. This also prevents the mode message to be
8616 * deleted when ESC is hit.
8617 */
8618 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008619 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 --no_mapping;
8621 switch (c)
8622 {
8623 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8624 case K_UP:
8625 case Ctrl_K:
8626 case 'k': ins_up(TRUE);
8627 break;
8628
8629 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8630 case K_DOWN:
8631 case Ctrl_J:
8632 case 'j': ins_down(TRUE);
8633 break;
8634
8635 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008636 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008638
8639 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008640 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008641 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008642 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 break;
8644
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008645 /* CTRL-G U: do not break undo with the next char */
8646 case 'U':
8647 /* Allow one left/right cursor movement with the next char,
8648 * without breaking undo. */
8649 dont_sync_undo = MAYBE;
8650 break;
8651
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008653 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 }
8655}
8656
8657/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008658 * CTRL-^ in Insert mode.
8659 */
8660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008661ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008662{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008663 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008664 {
8665 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8666 if (State & LANGMAP)
8667 {
8668 curbuf->b_p_iminsert = B_IMODE_NONE;
8669 State &= ~LANGMAP;
8670 }
8671 else
8672 {
8673 curbuf->b_p_iminsert = B_IMODE_LMAP;
8674 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008675#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008676 im_set_active(FALSE);
8677#endif
8678 }
8679 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008680#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008681 else
8682 {
8683 /* There are no ":lmap" mappings, toggle IM */
8684 if (im_get_status())
8685 {
8686 curbuf->b_p_iminsert = B_IMODE_NONE;
8687 im_set_active(FALSE);
8688 }
8689 else
8690 {
8691 curbuf->b_p_iminsert = B_IMODE_IM;
8692 State &= ~LANGMAP;
8693 im_set_active(TRUE);
8694 }
8695 }
8696#endif
8697 set_iminsert_global();
8698 showmode();
8699#ifdef FEAT_GUI
8700 /* may show different cursor shape or color */
8701 if (gui.in_use)
8702 gui_update_cursor(TRUE, FALSE);
8703#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008704#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008705 /* Show/unshow value of 'keymap' in status lines. */
8706 status_redraw_curbuf();
8707#endif
8708}
8709
8710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 * Handle ESC in insert mode.
8712 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8713 * insert.
8714 */
8715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008716ins_esc(
8717 long *count,
8718 int cmdchar,
8719 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
8721 int temp;
8722 static int disabled_redraw = FALSE;
8723
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008724#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008725 check_spell_redraw();
8726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727#if defined(FEAT_HANGULIN)
8728# if defined(ESC_CHG_TO_ENG_MODE)
8729 hangul_input_state_set(0);
8730# endif
8731 if (composing_hangul)
8732 {
8733 push_raw_key(composing_hangul_buffer, 2);
8734 composing_hangul = 0;
8735 }
8736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737
8738 temp = curwin->w_cursor.col;
8739 if (disabled_redraw)
8740 {
8741 --RedrawingDisabled;
8742 disabled_redraw = FALSE;
8743 }
8744 if (!arrow_used)
8745 {
8746 /*
8747 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008748 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8749 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 */
8751 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008752 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753
8754 /*
8755 * Repeating insert may take a long time. Check for
8756 * interrupt now and then.
8757 */
8758 if (*count > 0)
8759 {
8760 line_breakcheck();
8761 if (got_int)
8762 *count = 0;
8763 }
8764
8765 if (--*count > 0) /* repeat what was typed */
8766 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008767 /* Vi repeats the insert without replacing characters. */
8768 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8769 State &= ~REPLACE_FLAG;
8770
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 (void)start_redo_ins();
8772 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008773 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 ++RedrawingDisabled;
8775 disabled_redraw = TRUE;
8776 return FALSE; /* repeat the insert */
8777 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008778 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 undisplay_dollar();
8780 }
8781
8782 /* When an autoindent was removed, curswant stays after the
8783 * indent */
8784 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8785 curwin->w_set_curswant = TRUE;
8786
8787 /* Remember the last Insert position in the '^ mark. */
8788 if (!cmdmod.keepjumps)
8789 curbuf->b_last_insert = curwin->w_cursor;
8790
8791 /*
8792 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008793 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008795 if (!nomove
8796 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797#ifdef FEAT_VIRTUALEDIT
8798 || curwin->w_cursor.coladd > 0
8799#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008800 )
8801 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008802 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803#ifdef FEAT_RIGHTLEFT
8804 && !revins_on
8805#endif
8806 )
8807 {
8808#ifdef FEAT_VIRTUALEDIT
8809 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8810 {
8811 oneleft();
8812 if (restart_edit != NUL)
8813 ++curwin->w_cursor.coladd;
8814 }
8815 else
8816#endif
8817 {
8818 --curwin->w_cursor.col;
8819#ifdef FEAT_MBYTE
8820 /* Correct cursor for multi-byte character. */
8821 if (has_mbyte)
8822 mb_adjust_cursor();
8823#endif
8824 }
8825 }
8826
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008827#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 /* Disable IM to allow typing English directly for Normal mode commands.
8829 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8830 * well). */
8831 if (!(State & LANGMAP))
8832 im_save_status(&curbuf->b_p_iminsert);
8833 im_set_active(FALSE);
8834#endif
8835
8836 State = NORMAL;
8837 /* need to position cursor again (e.g. when on a TAB ) */
8838 changed_cline_bef_curs();
8839
8840#ifdef FEAT_MOUSE
8841 setmouse();
8842#endif
8843#ifdef CURSOR_SHAPE
8844 ui_cursor_shape(); /* may show different cursor shape */
8845#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008846 if (!p_ek)
8847 /* Re-enable bracketed paste mode. */
8848 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849
8850 /*
8851 * When recording or for CTRL-O, need to display the new mode.
8852 * Otherwise remove the mode message.
8853 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008854 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 showmode();
8856 else if (p_smd)
8857 MSG("");
8858
8859 return TRUE; /* exit Insert mode */
8860}
8861
8862#ifdef FEAT_RIGHTLEFT
8863/*
8864 * Toggle language: hkmap and revins_on.
8865 * Move to end of reverse inserted text.
8866 */
8867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008868ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
8870 if (revins_on && revins_chars && revins_scol >= 0)
8871 {
8872 while (gchar_cursor() != NUL && revins_chars--)
8873 ++curwin->w_cursor.col;
8874 }
8875 p_ri = !p_ri;
8876 revins_on = (State == INSERT && p_ri);
8877 if (revins_on)
8878 {
8879 revins_scol = curwin->w_cursor.col;
8880 revins_legal++;
8881 revins_chars = 0;
8882 undisplay_dollar();
8883 }
8884 else
8885 revins_scol = -1;
8886#ifdef FEAT_FKMAP
8887 if (p_altkeymap)
8888 {
8889 /*
8890 * to be consistent also for redo command, using '.'
8891 * set arrow_used to true and stop it - causing to redo
8892 * characters entered in one mode (normal/reverse insert).
8893 */
8894 arrow_used = TRUE;
8895 (void)stop_arrow();
8896 p_fkmap = curwin->w_p_rl ^ p_ri;
8897 if (p_fkmap && p_ri)
8898 State = INSERT;
8899 }
8900 else
8901#endif
8902 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8903 showmode();
8904}
8905#endif
8906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907/*
8908 * If 'keymodel' contains "startsel", may start selection.
8909 * Returns TRUE when a CTRL-O and other keys stuffed.
8910 */
8911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008912ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913{
8914 if (km_startsel)
8915 switch (c)
8916 {
8917 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 case K_PAGEUP:
8920 case K_KPAGEUP:
8921 case K_PAGEDOWN:
8922 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008923# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 case K_LEFT:
8925 case K_RIGHT:
8926 case K_UP:
8927 case K_DOWN:
8928 case K_END:
8929 case K_HOME:
8930# endif
8931 if (!(mod_mask & MOD_MASK_SHIFT))
8932 break;
8933 /* FALLTHROUGH */
8934 case K_S_LEFT:
8935 case K_S_RIGHT:
8936 case K_S_UP:
8937 case K_S_DOWN:
8938 case K_S_END:
8939 case K_S_HOME:
8940 /* Start selection right away, the cursor can move with
8941 * CTRL-O when beyond the end of the line. */
8942 start_selection();
8943
8944 /* Execute the key in (insert) Select mode. */
8945 stuffcharReadbuff(Ctrl_O);
8946 if (mod_mask)
8947 {
8948 char_u buf[4];
8949
8950 buf[0] = K_SPECIAL;
8951 buf[1] = KS_MODIFIER;
8952 buf[2] = mod_mask;
8953 buf[3] = NUL;
8954 stuffReadbuff(buf);
8955 }
8956 stuffcharReadbuff(c);
8957 return TRUE;
8958 }
8959 return FALSE;
8960}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961
8962/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008963 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008964 */
8965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008966ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008967{
8968#ifdef FEAT_FKMAP
8969 if (p_fkmap && p_ri)
8970 {
8971 beep_flush();
8972 EMSG(farsi_text_3); /* encoded in Farsi */
8973 return;
8974 }
8975#endif
8976
Bram Moolenaar1e015462005-09-25 22:16:38 +00008977# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008978 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008979 (char_u *)((State & REPLACE_FLAG) ? "i"
8980 : replaceState == VREPLACE ? "v"
8981 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008982# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008983 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008984 if (State & REPLACE_FLAG)
8985 State = INSERT | (State & LANGMAP);
8986 else
8987 State = replaceState | (State & LANGMAP);
8988 AppendCharToRedobuff(K_INS);
8989 showmode();
8990#ifdef CURSOR_SHAPE
8991 ui_cursor_shape(); /* may show different cursor shape */
8992#endif
8993}
8994
8995/*
8996 * Pressed CTRL-O in Insert mode.
8997 */
8998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008999ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009000{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009001 if (State & VREPLACE_FLAG)
9002 restart_edit = 'V';
9003 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009004 if (State & REPLACE_FLAG)
9005 restart_edit = 'R';
9006 else
9007 restart_edit = 'I';
9008#ifdef FEAT_VIRTUALEDIT
9009 if (virtual_active())
9010 ins_at_eol = FALSE; /* cursor always keeps its column */
9011 else
9012#endif
9013 ins_at_eol = (gchar_cursor() == NUL);
9014}
9015
9016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 * If the cursor is on an indent, ^T/^D insert/delete one
9018 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00009019 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 * with vi. But vi only supports ^T and ^D after an
9021 * autoindent, we support it everywhere.
9022 */
9023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009024ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
9026 if (stop_arrow() == FAIL)
9027 return;
9028 AppendCharToRedobuff(c);
9029
9030 /*
9031 * 0^D and ^^D: remove all indent.
9032 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009033 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9034 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 {
9036 --curwin->w_cursor.col;
9037 (void)del_char(FALSE); /* delete the '^' or '0' */
9038 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9039 if (State & REPLACE_FLAG)
9040 replace_pop_ins();
9041 if (lastc == '^')
9042 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009043 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 }
9045 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009046 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047
9048 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9049 did_ai = FALSE;
9050#ifdef FEAT_SMARTINDENT
9051 did_si = FALSE;
9052 can_si = FALSE;
9053 can_si_back = FALSE;
9054#endif
9055#ifdef FEAT_CINDENT
9056 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9057#endif
9058}
9059
9060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009061ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063 int temp;
9064
9065 if (stop_arrow() == FAIL)
9066 return;
9067 if (gchar_cursor() == NUL) /* delete newline */
9068 {
9069 temp = curwin->w_cursor.col;
9070 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009071 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009072 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009076 /* Adjust orig_line_count in case more lines have been deleted than
9077 * have been added. That makes sure, that open_line() later
9078 * can access all buffer lines correctly */
9079 if (State & VREPLACE_FLAG &&
9080 orig_line_count > curbuf->b_ml.ml_line_count)
9081 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009084 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9085 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 did_ai = FALSE;
9087#ifdef FEAT_SMARTINDENT
9088 did_si = FALSE;
9089 can_si = FALSE;
9090 can_si_back = FALSE;
9091#endif
9092 AppendCharToRedobuff(K_DEL);
9093}
9094
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009095/*
9096 * Delete one character for ins_bs().
9097 */
9098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009099ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009100{
9101 dec_cursor();
9102 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9103 if (State & REPLACE_FLAG)
9104 {
9105 /* Don't delete characters before the insert point when in
9106 * Replace mode */
9107 if (curwin->w_cursor.lnum != Insstart.lnum
9108 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009109 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009110 }
9111 else
9112 (void)del_char(FALSE);
9113}
9114
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115/*
9116 * Handle Backspace, delete-word and delete-line in Insert mode.
9117 * Return TRUE when backspace was actually used.
9118 */
9119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009120ins_bs(
9121 int c,
9122 int mode,
9123 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 linenr_T lnum;
9126 int cc;
9127 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009128 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 colnr_T mincol;
9130 int did_backspace = FALSE;
9131 int in_indent;
9132 int oldState;
9133#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009134 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135#endif
9136
9137 /*
9138 * can't delete anything in an empty file
9139 * can't backup past first character in buffer
9140 * can't backup past starting point unless 'backspace' > 1
9141 * can backup to a previous line if 'backspace' == 0
9142 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009143 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 || (
9145#ifdef FEAT_RIGHTLEFT
9146 !revins_on &&
9147#endif
9148 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9149 || (!can_bs(BS_START)
9150 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009151 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9152 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9154 && curwin->w_cursor.col <= ai_col)
9155 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9156 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009157 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 return FALSE;
9159 }
9160
9161 if (stop_arrow() == FAIL)
9162 return FALSE;
9163 in_indent = inindent(0);
9164#ifdef FEAT_CINDENT
9165 if (in_indent)
9166 can_cindent = FALSE;
9167#endif
9168#ifdef FEAT_COMMENTS
9169 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9170#endif
9171#ifdef FEAT_RIGHTLEFT
9172 if (revins_on) /* put cursor after last inserted char */
9173 inc_cursor();
9174#endif
9175
9176#ifdef FEAT_VIRTUALEDIT
9177 /* Virtualedit:
9178 * BACKSPACE_CHAR eats a virtual space
9179 * BACKSPACE_WORD eats all coladd
9180 * BACKSPACE_LINE eats all coladd and keeps going
9181 */
9182 if (curwin->w_cursor.coladd > 0)
9183 {
9184 if (mode == BACKSPACE_CHAR)
9185 {
9186 --curwin->w_cursor.coladd;
9187 return TRUE;
9188 }
9189 if (mode == BACKSPACE_WORD)
9190 {
9191 curwin->w_cursor.coladd = 0;
9192 return TRUE;
9193 }
9194 curwin->w_cursor.coladd = 0;
9195 }
9196#endif
9197
9198 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009199 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 */
9201 if (curwin->w_cursor.col == 0)
9202 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009203 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009204 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205#ifdef FEAT_RIGHTLEFT
9206 || revins_on
9207#endif
9208 )
9209 {
9210 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9211 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9212 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009213 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009214 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 }
9216 /*
9217 * In replace mode:
9218 * cc < 0: NL was inserted, delete it
9219 * cc >= 0: NL was replaced, put original characters back
9220 */
9221 cc = -1;
9222 if (State & REPLACE_FLAG)
9223 cc = replace_pop(); /* returns -1 if NL was inserted */
9224 /*
9225 * In replace mode, in the line we started replacing, we only move the
9226 * cursor.
9227 */
9228 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9229 {
9230 dec_cursor();
9231 }
9232 else
9233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 if (!(State & VREPLACE_FLAG)
9235 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 {
9237 temp = gchar_cursor(); /* remember current char */
9238 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009239
9240 /* When "aw" is in 'formatoptions' we must delete the space at
9241 * the end of the line, otherwise the line will be broken
9242 * again when auto-formatting. */
9243 if (has_format_option(FO_AUTO)
9244 && has_format_option(FO_WHITE_PAR))
9245 {
9246 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9247 TRUE);
9248 int len;
9249
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009250 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009251 if (len > 0 && ptr[len - 1] == ' ')
9252 ptr[len - 1] = NUL;
9253 }
9254
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009255 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 if (temp == NUL && gchar_cursor() != NUL)
9257 inc_cursor();
9258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 else
9260 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261
9262 /*
9263 * In REPLACE mode we have to put back the text that was replaced
9264 * by the NL. On the replace stack is first a NUL-terminated
9265 * sequence of characters that were deleted and then the
9266 * characters that NL replaced.
9267 */
9268 if (State & REPLACE_FLAG)
9269 {
9270 /*
9271 * Do the next ins_char() in NORMAL state, to
9272 * prevent ins_char() from replacing characters and
9273 * avoiding showmatch().
9274 */
9275 oldState = State;
9276 State = NORMAL;
9277 /*
9278 * restore characters (blanks) deleted after cursor
9279 */
9280 while (cc > 0)
9281 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009282 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283#ifdef FEAT_MBYTE
9284 mb_replace_pop_ins(cc);
9285#else
9286 ins_char(cc);
9287#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009288 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 cc = replace_pop();
9290 }
9291 /* restore the characters that NL replaced */
9292 replace_pop_ins();
9293 State = oldState;
9294 }
9295 }
9296 did_ai = FALSE;
9297 }
9298 else
9299 {
9300 /*
9301 * Delete character(s) before the cursor.
9302 */
9303#ifdef FEAT_RIGHTLEFT
9304 if (revins_on) /* put cursor on last inserted char */
9305 dec_cursor();
9306#endif
9307 mincol = 0;
9308 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009309 if (mode == BACKSPACE_LINE
9310 && (curbuf->b_p_ai
9311#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009312 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009313#endif
9314 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315#ifdef FEAT_RIGHTLEFT
9316 && !revins_on
9317#endif
9318 )
9319 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009320 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009322 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009324 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 }
9326
9327 /*
9328 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9329 */
9330 if ( mode == BACKSPACE_CHAR
9331 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009332 || ((get_sts_value() != 0
9333#ifdef FEAT_VARTABS
9334 || tabstop_count(curbuf->b_p_vsts_array)
9335#endif
9336 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009337 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 && (*(ml_get_cursor() - 1) == TAB
9339 || (*(ml_get_cursor() - 1) == ' '
9340 && (!*inserted_space_p
9341 || arrow_used))))))
9342 {
9343 int ts;
9344 colnr_T vcol;
9345 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009346 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 /* Compute the virtual column where we want to be. Since
9350 * 'showbreak' may get in the way, need to get the last column of
9351 * the previous character. */
9352 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009353 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 dec_cursor();
9355 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9356 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009357#ifdef FEAT_VARTABS
9358 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009359 {
9360 ts = (int)get_sw_value(curbuf);
9361 want_vcol = (want_vcol / ts) * ts;
9362 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009363 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009364 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009365 curbuf->b_p_vsts_array);
9366#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009367 if (p_sta && in_indent)
9368 ts = (int)get_sw_value(curbuf);
9369 else
9370 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373
9374 /* delete characters until we are at or before want_vcol */
9375 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009376 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009377 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378
9379 /* insert extra spaces until we are at want_vcol */
9380 while (vcol < want_vcol)
9381 {
9382 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009383 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9384 && curwin->w_cursor.col < Insstart_orig.col)
9385 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 if (State & VREPLACE_FLAG)
9388 ins_char(' ');
9389 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 {
9391 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009392 if ((State & REPLACE_FLAG))
9393 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 }
9395 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9396 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009397
9398 /* If we are now back where we started delete one character. Can
9399 * happen when using 'sts' and 'linebreak'. */
9400 if (vcol >= start_vcol)
9401 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 }
9403
9404 /*
9405 * Delete upto starting point, start of line or previous word.
9406 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009407 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009409#ifdef FEAT_MBYTE
9410 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009412 if (has_mbyte)
9413 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009415 do
9416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009418 if (!revins_on) /* put cursor on char to be deleted */
9419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009421
9422 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009424 /* look multi-byte character class */
9425 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009427 prev_cclass = cclass;
9428 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009431
9432 /* start of word? */
9433 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9434 {
9435 mode = BACKSPACE_WORD_NOT_SPACE;
9436 temp = vim_iswordc(cc);
9437 }
9438 /* end of word? */
9439 else if (mode == BACKSPACE_WORD_NOT_SPACE
9440 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9441#ifdef FEAT_MBYTE
9442 || prev_cclass != cclass
9443#endif
9444 ))
9445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009447 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009449 inc_cursor();
9450#ifdef FEAT_RIGHTLEFT
9451 else if (State & REPLACE_FLAG)
9452 dec_cursor();
9453#endif
9454 break;
9455 }
9456 if (State & REPLACE_FLAG)
9457 replace_do_bs(-1);
9458 else
9459 {
9460#ifdef FEAT_MBYTE
9461 if (enc_utf8 && p_deco)
9462 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9463#endif
9464 (void)del_char(FALSE);
9465#ifdef FEAT_MBYTE
9466 /*
9467 * If there are combining characters and 'delcombine' is set
9468 * move the cursor back. Don't back up before the base
9469 * character.
9470 */
9471 if (enc_utf8 && p_deco && cpc[0] != NUL)
9472 inc_cursor();
9473#endif
9474#ifdef FEAT_RIGHTLEFT
9475 if (revins_chars)
9476 {
9477 revins_chars--;
9478 revins_legal++;
9479 }
9480 if (revins_on && gchar_cursor() == NUL)
9481 break;
9482#endif
9483 }
9484 /* Just a single backspace?: */
9485 if (mode == BACKSPACE_CHAR)
9486 break;
9487 } while (
9488#ifdef FEAT_RIGHTLEFT
9489 revins_on ||
9490#endif
9491 (curwin->w_cursor.col > mincol
9492 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9493 || curwin->w_cursor.col != Insstart_orig.col)));
9494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 did_backspace = TRUE;
9496 }
9497#ifdef FEAT_SMARTINDENT
9498 did_si = FALSE;
9499 can_si = FALSE;
9500 can_si_back = FALSE;
9501#endif
9502 if (curwin->w_cursor.col <= 1)
9503 did_ai = FALSE;
9504 /*
9505 * It's a little strange to put backspaces into the redo
9506 * buffer, but it makes auto-indent a lot easier to deal
9507 * with.
9508 */
9509 AppendCharToRedobuff(c);
9510
9511 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009512 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009513 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009514 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515
9516 /* vi behaviour: the cursor moves backward but the character that
9517 * was there remains visible
9518 * Vim behaviour: the cursor moves backward and the character that
9519 * was there is erased from the screen.
9520 * We can emulate the vi behaviour by pretending there is a dollar
9521 * displayed even when there isn't.
9522 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009523 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 dollar_vcol = curwin->w_virtcol;
9525
Bram Moolenaarce3be472008-01-14 19:12:28 +00009526#ifdef FEAT_FOLDING
9527 /* When deleting a char the cursor line must never be in a closed fold.
9528 * E.g., when 'foldmethod' is indent and deleting the first non-white
9529 * char before a Tab. */
9530 if (did_backspace)
9531 foldOpenCursor();
9532#endif
9533
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 return did_backspace;
9535}
9536
9537#ifdef FEAT_MOUSE
9538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009539ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540{
9541 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009542 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543
9544# ifdef FEAT_GUI
9545 /* When GUI is active, also move/paste when 'mouse' is empty */
9546 if (!gui.in_use)
9547# endif
9548 if (!mouse_has(MOUSE_INSERT))
9549 return;
9550
9551 undisplay_dollar();
9552 tpos = curwin->w_cursor;
9553 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9554 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009555 win_T *new_curwin = curwin;
9556
9557 if (curwin != old_curwin && win_valid(old_curwin))
9558 {
9559 /* Mouse took us to another window. We need to go back to the
9560 * previous one to stop insert there properly. */
9561 curwin = old_curwin;
9562 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009563#ifdef FEAT_JOB_CHANNEL
9564 if (bt_prompt(curbuf))
9565 // Restart Insert mode when re-entering the prompt buffer.
9566 curbuf->b_prompt_insert = 'A';
9567#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009568 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009569 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009570 if (curwin != new_curwin && win_valid(new_curwin))
9571 {
9572 curwin = new_curwin;
9573 curbuf = curwin->w_buffer;
9574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575# ifdef FEAT_CINDENT
9576 can_cindent = TRUE;
9577# endif
9578 }
9579
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 /* redraw status lines (in case another window became active) */
9581 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582}
9583
9584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009585ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586{
9587 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009588 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009589# ifdef FEAT_INS_EXPAND
9590 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591# endif
9592
9593 tpos = curwin->w_cursor;
9594
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009595 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 {
9597 int row, col;
9598
9599 row = mouse_row;
9600 col = mouse_col;
9601
9602 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009603 wp = mouse_find_win(&row, &col);
9604 if (wp == NULL)
9605 return;
9606 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 curbuf = curwin->w_buffer;
9608 }
9609 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 undisplay_dollar();
9611
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009612# ifdef FEAT_INS_EXPAND
9613 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009614 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009615# endif
9616 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009617 if (dir == MSCR_DOWN || dir == MSCR_UP)
9618 {
9619 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9620 scroll_redraw(dir,
9621 (long)(curwin->w_botline - curwin->w_topline));
9622 else
9623 scroll_redraw(dir, 3L);
9624 }
9625#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009626 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009627 {
9628 int val, step = 6;
9629
9630 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009631 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009632 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9633 if (val < 0)
9634 val = 0;
9635 gui_do_horiz_scroll(val, TRUE);
9636 }
9637#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009638# ifdef FEAT_INS_EXPAND
9639 did_scroll = TRUE;
9640# endif
9641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 curwin->w_redr_status = TRUE;
9644
9645 curwin = old_curwin;
9646 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009648# ifdef FEAT_INS_EXPAND
9649 /* The popup menu may overlay the window, need to redraw it.
9650 * TODO: Would be more efficient to only redraw the windows that are
9651 * overlapped by the popup menu. */
9652 if (pum_visible() && did_scroll)
9653 {
9654 redraw_all_later(NOT_VALID);
9655 ins_compl_show_pum();
9656 }
9657# endif
9658
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009659 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 {
9661 start_arrow(&tpos);
9662# ifdef FEAT_CINDENT
9663 can_cindent = TRUE;
9664# endif
9665 }
9666}
9667#endif
9668
Bram Moolenaarec2da362017-01-21 20:04:22 +01009669/*
9670 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9671 * P_PE literally.
9672 * When "drop" is TRUE then consume the text and drop it.
9673 */
9674 int
9675bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9676{
9677 int c;
9678 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9679 int idx = 0;
9680 char_u *end = find_termcode((char_u *)"PE");
9681 int ret_char = -1;
9682 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009683 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009684
9685 /* If the end code is too long we can't detect it, read everything. */
9686 if (STRLEN(end) >= NUMBUFLEN)
9687 end = NULL;
9688 ++no_mapping;
9689 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009690 if (!p_paste)
9691 // Also have the side effects of setting 'paste' to make it work much
9692 // faster.
9693 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009694
Bram Moolenaarec2da362017-01-21 20:04:22 +01009695 for (;;)
9696 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009697 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009698 if (end == NULL && vpeekc() == NUL)
9699 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009700 do
9701 {
9702 c = vgetc();
9703 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9704 if (c == NUL || got_int)
9705 // When CTRL-C was encountered the typeahead will be flushed and we
9706 // won't get the end sequence.
9707 break;
9708
Bram Moolenaarec2da362017-01-21 20:04:22 +01009709#ifdef FEAT_MBYTE
9710 if (has_mbyte)
9711 idx += (*mb_char2bytes)(c, buf + idx);
9712 else
9713#endif
9714 buf[idx++] = c;
9715 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009716 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009717 {
9718 if (end[idx] == NUL)
9719 break; /* Found the end of paste code. */
9720 continue;
9721 }
9722 if (!drop)
9723 {
9724 switch (mode)
9725 {
9726 case PASTE_CMDLINE:
9727 put_on_cmdline(buf, idx, TRUE);
9728 break;
9729
9730 case PASTE_EX:
9731 if (gap != NULL && ga_grow(gap, idx) == OK)
9732 {
9733 mch_memmove((char *)gap->ga_data + gap->ga_len,
9734 buf, (size_t)idx);
9735 gap->ga_len += idx;
9736 }
9737 break;
9738
9739 case PASTE_INSERT:
9740 if (stop_arrow() == OK)
9741 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009742 c = buf[0];
9743 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9744 ins_eol(c);
9745 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009746 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009747 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009748 AppendToRedobuffLit(buf, idx);
9749 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009750 }
9751 break;
9752
9753 case PASTE_ONE_CHAR:
9754 if (ret_char == -1)
9755 {
9756#ifdef FEAT_MBYTE
9757 if (has_mbyte)
9758 ret_char = (*mb_ptr2char)(buf);
9759 else
9760#endif
9761 ret_char = buf[0];
9762 }
9763 break;
9764 }
9765 }
9766 idx = 0;
9767 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009768
Bram Moolenaarec2da362017-01-21 20:04:22 +01009769 --no_mapping;
9770 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009771 if (!save_paste)
9772 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009773
9774 return ret_char;
9775}
9776
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009777#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009779ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009780{
9781 /* We will be leaving the current window, unless closing another tab. */
9782 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9783 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9784 {
9785 undisplay_dollar();
9786 start_arrow(&curwin->w_cursor);
9787# ifdef FEAT_CINDENT
9788 can_cindent = TRUE;
9789# endif
9790 }
9791
9792 if (c == K_TABLINE)
9793 goto_tabpage(current_tab);
9794 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009795 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009796 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009797 redraw_statuslines(); /* will redraw the tabline when needed */
9798 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009799}
9800#endif
9801
9802#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009804ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806 pos_T tpos;
9807
9808 undisplay_dollar();
9809 tpos = curwin->w_cursor;
9810 if (gui_do_scroll())
9811 {
9812 start_arrow(&tpos);
9813# ifdef FEAT_CINDENT
9814 can_cindent = TRUE;
9815# endif
9816 }
9817}
9818
9819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009820ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822 pos_T tpos;
9823
9824 undisplay_dollar();
9825 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009826 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 {
9828 start_arrow(&tpos);
9829# ifdef FEAT_CINDENT
9830 can_cindent = TRUE;
9831# endif
9832 }
9833}
9834#endif
9835
9836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837ins_left(
9838 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
9840 pos_T tpos;
9841
9842#ifdef FEAT_FOLDING
9843 if ((fdo_flags & FDO_HOR) && KeyTyped)
9844 foldOpenCursor();
9845#endif
9846 undisplay_dollar();
9847 tpos = curwin->w_cursor;
9848 if (oneleft() == OK)
9849 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009850#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9851 /* Only call start_arrow() when not busy with preediting, it will
9852 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009853 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009854#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009855 {
9856 start_arrow_with_change(&tpos, end_change);
9857 if (!end_change)
9858 AppendCharToRedobuff(K_LEFT);
9859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860#ifdef FEAT_RIGHTLEFT
9861 /* If exit reversed string, position is fixed */
9862 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9863 revins_legal++;
9864 revins_chars++;
9865#endif
9866 }
9867
9868 /*
9869 * if 'whichwrap' set for cursor in insert mode may go to
9870 * previous line
9871 */
9872 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9873 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009874 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 start_arrow(&tpos);
9876 --(curwin->w_cursor.lnum);
9877 coladvance((colnr_T)MAXCOL);
9878 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9879 }
9880 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009881 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009882 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883}
9884
9885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009886ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887{
9888 pos_T tpos;
9889
9890#ifdef FEAT_FOLDING
9891 if ((fdo_flags & FDO_HOR) && KeyTyped)
9892 foldOpenCursor();
9893#endif
9894 undisplay_dollar();
9895 tpos = curwin->w_cursor;
9896 if (c == K_C_HOME)
9897 curwin->w_cursor.lnum = 1;
9898 curwin->w_cursor.col = 0;
9899#ifdef FEAT_VIRTUALEDIT
9900 curwin->w_cursor.coladd = 0;
9901#endif
9902 curwin->w_curswant = 0;
9903 start_arrow(&tpos);
9904}
9905
9906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009907ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908{
9909 pos_T tpos;
9910
9911#ifdef FEAT_FOLDING
9912 if ((fdo_flags & FDO_HOR) && KeyTyped)
9913 foldOpenCursor();
9914#endif
9915 undisplay_dollar();
9916 tpos = curwin->w_cursor;
9917 if (c == K_C_END)
9918 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9919 coladvance((colnr_T)MAXCOL);
9920 curwin->w_curswant = MAXCOL;
9921
9922 start_arrow(&tpos);
9923}
9924
9925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009926ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927{
9928#ifdef FEAT_FOLDING
9929 if ((fdo_flags & FDO_HOR) && KeyTyped)
9930 foldOpenCursor();
9931#endif
9932 undisplay_dollar();
9933 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9934 {
9935 start_arrow(&curwin->w_cursor);
9936 (void)bck_word(1L, FALSE, FALSE);
9937 curwin->w_set_curswant = TRUE;
9938 }
9939 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009940 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941}
9942
9943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009944ins_right(
9945 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
9947#ifdef FEAT_FOLDING
9948 if ((fdo_flags & FDO_HOR) && KeyTyped)
9949 foldOpenCursor();
9950#endif
9951 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009952 if (gchar_cursor() != NUL
9953#ifdef FEAT_VIRTUALEDIT
9954 || virtual_active()
9955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 )
9957 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009958 start_arrow_with_change(&curwin->w_cursor, end_change);
9959 if (!end_change)
9960 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 curwin->w_set_curswant = TRUE;
9962#ifdef FEAT_VIRTUALEDIT
9963 if (virtual_active())
9964 oneright();
9965 else
9966#endif
9967 {
9968#ifdef FEAT_MBYTE
9969 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009970 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 else
9972#endif
9973 ++curwin->w_cursor.col;
9974 }
9975
9976#ifdef FEAT_RIGHTLEFT
9977 revins_legal++;
9978 if (revins_chars)
9979 revins_chars--;
9980#endif
9981 }
9982 /* if 'whichwrap' set for cursor in insert mode, may move the
9983 * cursor to the next line */
9984 else if (vim_strchr(p_ww, ']') != NULL
9985 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9986 {
9987 start_arrow(&curwin->w_cursor);
9988 curwin->w_set_curswant = TRUE;
9989 ++curwin->w_cursor.lnum;
9990 curwin->w_cursor.col = 0;
9991 }
9992 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009993 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009994 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
9996
9997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009998ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000#ifdef FEAT_FOLDING
10001 if ((fdo_flags & FDO_HOR) && KeyTyped)
10002 foldOpenCursor();
10003#endif
10004 undisplay_dollar();
10005 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
10006 || gchar_cursor() != NUL)
10007 {
10008 start_arrow(&curwin->w_cursor);
10009 (void)fwd_word(1L, FALSE, 0);
10010 curwin->w_set_curswant = TRUE;
10011 }
10012 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010013 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014}
10015
10016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010017ins_up(
10018 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
10020 pos_T tpos;
10021 linenr_T old_topline = curwin->w_topline;
10022#ifdef FEAT_DIFF
10023 int old_topfill = curwin->w_topfill;
10024#endif
10025
10026 undisplay_dollar();
10027 tpos = curwin->w_cursor;
10028 if (cursor_up(1L, TRUE) == OK)
10029 {
10030 if (startcol)
10031 coladvance(getvcol_nolist(&Insstart));
10032 if (old_topline != curwin->w_topline
10033#ifdef FEAT_DIFF
10034 || old_topfill != curwin->w_topfill
10035#endif
10036 )
10037 redraw_later(VALID);
10038 start_arrow(&tpos);
10039#ifdef FEAT_CINDENT
10040 can_cindent = TRUE;
10041#endif
10042 }
10043 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010044 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045}
10046
10047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010048ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
10050 pos_T tpos;
10051
10052 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010053
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010054 if (mod_mask & MOD_MASK_CTRL)
10055 {
10056 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010057 if (first_tabpage->tp_next != NULL)
10058 {
10059 start_arrow(&curwin->w_cursor);
10060 goto_tabpage(-1);
10061 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010062 return;
10063 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010064
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 tpos = curwin->w_cursor;
10066 if (onepage(BACKWARD, 1L) == OK)
10067 {
10068 start_arrow(&tpos);
10069#ifdef FEAT_CINDENT
10070 can_cindent = TRUE;
10071#endif
10072 }
10073 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010074 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075}
10076
10077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010078ins_down(
10079 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080{
10081 pos_T tpos;
10082 linenr_T old_topline = curwin->w_topline;
10083#ifdef FEAT_DIFF
10084 int old_topfill = curwin->w_topfill;
10085#endif
10086
10087 undisplay_dollar();
10088 tpos = curwin->w_cursor;
10089 if (cursor_down(1L, TRUE) == OK)
10090 {
10091 if (startcol)
10092 coladvance(getvcol_nolist(&Insstart));
10093 if (old_topline != curwin->w_topline
10094#ifdef FEAT_DIFF
10095 || old_topfill != curwin->w_topfill
10096#endif
10097 )
10098 redraw_later(VALID);
10099 start_arrow(&tpos);
10100#ifdef FEAT_CINDENT
10101 can_cindent = TRUE;
10102#endif
10103 }
10104 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010105 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106}
10107
10108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010109ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110{
10111 pos_T tpos;
10112
10113 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010114
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010115 if (mod_mask & MOD_MASK_CTRL)
10116 {
10117 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010118 if (first_tabpage->tp_next != NULL)
10119 {
10120 start_arrow(&curwin->w_cursor);
10121 goto_tabpage(0);
10122 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010123 return;
10124 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010125
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 tpos = curwin->w_cursor;
10127 if (onepage(FORWARD, 1L) == OK)
10128 {
10129 start_arrow(&tpos);
10130#ifdef FEAT_CINDENT
10131 can_cindent = TRUE;
10132#endif
10133 }
10134 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010135 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136}
10137
10138#ifdef FEAT_DND
10139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010140ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141{
10142 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10143}
10144#endif
10145
10146/*
10147 * Handle TAB in Insert or Replace mode.
10148 * Return TRUE when the TAB needs to be inserted like a normal character.
10149 */
10150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010151ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152{
10153 int ind;
10154 int i;
10155 int temp;
10156
10157 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10158 Insstart_blank_vcol = get_nolist_virtcol();
10159 if (echeck_abbr(TAB + ABBR_OFF))
10160 return FALSE;
10161
10162 ind = inindent(0);
10163#ifdef FEAT_CINDENT
10164 if (ind)
10165 can_cindent = FALSE;
10166#endif
10167
10168 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010169 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 */
10171 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010172#ifdef FEAT_VARTABS
10173 && !(p_sta && ind
10174 /* These five lines mean 'tabstop' != 'shiftwidth' */
10175 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10176 || (tabstop_count(curbuf->b_p_vts_array) == 1
10177 && tabstop_first(curbuf->b_p_vts_array)
10178 != get_sw_value(curbuf))
10179 || (tabstop_count(curbuf->b_p_vts_array) == 0
10180 && curbuf->b_p_ts != get_sw_value(curbuf))))
10181 && tabstop_count(curbuf->b_p_vsts_array) == 0
10182#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010183 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010184#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010185 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 return TRUE;
10187
10188 if (stop_arrow() == FAIL)
10189 return TRUE;
10190
10191 did_ai = FALSE;
10192#ifdef FEAT_SMARTINDENT
10193 did_si = FALSE;
10194 can_si = FALSE;
10195 can_si_back = FALSE;
10196#endif
10197 AppendToRedobuff((char_u *)"\t");
10198
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010199#ifdef FEAT_VARTABS
10200 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10201 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010202 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010203 temp -= get_nolist_virtcol() % temp;
10204 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010205 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010206 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010207 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010208 curbuf->b_p_vsts_array);
10209 else /* otherwise use 'tabstop' */
10210 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10211 curbuf->b_p_vts_array);
10212#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010214 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010215 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10216 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else /* otherwise use 'tabstop' */
10218 temp = (int)curbuf->b_p_ts;
10219 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221
10222 /*
10223 * Insert the first space with ins_char(). It will delete one char in
10224 * replace mode. Insert the rest with ins_str(); it will not delete any
10225 * chars. For VREPLACE mode, we use ins_char() for all characters.
10226 */
10227 ins_char(' ');
10228 while (--temp > 0)
10229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (State & VREPLACE_FLAG)
10231 ins_char(' ');
10232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 {
10234 ins_str((char_u *)" ");
10235 if (State & REPLACE_FLAG) /* no char replaced */
10236 replace_push(NUL);
10237 }
10238 }
10239
10240 /*
10241 * When 'expandtab' not set: Replace spaces by TABs where possible.
10242 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010243#ifdef FEAT_VARTABS
10244 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10245 || get_sts_value() > 0
10246 || (p_sta && ind)))
10247#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010248 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 {
10251 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 char_u *saved_line = NULL; /* init for GCC */
10253 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 pos_T fpos;
10255 pos_T *cursor;
10256 colnr_T want_vcol, vcol;
10257 int change_col = -1;
10258 int save_list = curwin->w_p_list;
10259
10260 /*
10261 * Get the current line. For VREPLACE mode, don't make real changes
10262 * yet, just work on a copy of the line.
10263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 if (State & VREPLACE_FLAG)
10265 {
10266 pos = curwin->w_cursor;
10267 cursor = &pos;
10268 saved_line = vim_strsave(ml_get_curline());
10269 if (saved_line == NULL)
10270 return FALSE;
10271 ptr = saved_line + pos.col;
10272 }
10273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 {
10275 ptr = ml_get_cursor();
10276 cursor = &curwin->w_cursor;
10277 }
10278
10279 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10280 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10281 curwin->w_p_list = FALSE;
10282
10283 /* Find first white before the cursor */
10284 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010285 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 {
10287 --fpos.col;
10288 --ptr;
10289 }
10290
10291 /* In Replace mode, don't change characters before the insert point. */
10292 if ((State & REPLACE_FLAG)
10293 && fpos.lnum == Insstart.lnum
10294 && fpos.col < Insstart.col)
10295 {
10296 ptr += Insstart.col - fpos.col;
10297 fpos.col = Insstart.col;
10298 }
10299
10300 /* compute virtual column numbers of first white and cursor */
10301 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10302 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10303
Bram Moolenaar597a4222014-06-25 14:39:50 +020010304 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10305 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010306 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010308 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 if (vcol + i > want_vcol)
10310 break;
10311 if (*ptr != TAB)
10312 {
10313 *ptr = TAB;
10314 if (change_col < 0)
10315 {
10316 change_col = fpos.col; /* Column of first change */
10317 /* May have to adjust Insstart */
10318 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10319 Insstart.col = fpos.col;
10320 }
10321 }
10322 ++fpos.col;
10323 ++ptr;
10324 vcol += i;
10325 }
10326
10327 if (change_col >= 0)
10328 {
10329 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010330 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331
10332 /* Skip over the spaces we need. */
10333 while (vcol < want_vcol && *ptr == ' ')
10334 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010335 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 ++ptr;
10337 ++repl_off;
10338 }
10339 if (vcol > want_vcol)
10340 {
10341 /* Must have a char with 'showbreak' just before it. */
10342 --ptr;
10343 --repl_off;
10344 }
10345 fpos.col += repl_off;
10346
10347 /* Delete following spaces. */
10348 i = cursor->col - fpos.col;
10349 if (i > 0)
10350 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010351 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010353 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 for (temp = i; --temp >= 0; )
10355 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010010356#ifdef FEAT_TEXT_PROP
10357 curbuf->b_ml.ml_line_len -= i;
10358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010360#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010361 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010362 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010363 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010364 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10365 (char_u *)"\t", 1);
10366 }
10367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 cursor->col -= i;
10369
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 /*
10371 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10372 * backspacing over the changed spacing and then inserting the new
10373 * spacing.
10374 */
10375 if (State & VREPLACE_FLAG)
10376 {
10377 /* Backspace from real cursor to change_col */
10378 backspace_until_column(change_col);
10379
10380 /* Insert each char in saved_line from changed_col to
10381 * ptr-cursor */
10382 ins_bytes_len(saved_line + change_col,
10383 cursor->col - change_col);
10384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 }
10386
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 if (State & VREPLACE_FLAG)
10388 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 curwin->w_p_list = save_list;
10390 }
10391
10392 return FALSE;
10393}
10394
10395/*
10396 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010397 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 */
10399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010400ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401{
10402 int i;
10403
10404 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010405 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 undisplay_dollar();
10409
10410 /*
10411 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10412 * character under the cursor. Only push a NUL on the replace stack,
10413 * nothing to put back when the NL is deleted.
10414 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010415 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 replace_push(NUL);
10417
10418 /*
10419 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10420 * replacing the next line, so we push all of the characters left on the
10421 * line onto the replace stack. This is not done here though, it is done
10422 * in open_line().
10423 */
10424
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010425#ifdef FEAT_VIRTUALEDIT
10426 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10427 * CTRL-O). */
10428 if (virtual_active() && curwin->w_cursor.coladd > 0)
10429 coladvance(getviscol());
10430#endif
10431
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432#ifdef FEAT_RIGHTLEFT
10433# ifdef FEAT_FKMAP
10434 if (p_altkeymap && p_fkmap)
10435 fkmap(NL);
10436# endif
10437 /* NL in reverse insert will always start in the end of
10438 * current line. */
10439 if (revins_on)
10440 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10441#endif
10442
10443 AppendToRedobuff(NL_STR);
10444 i = open_line(FORWARD,
10445#ifdef FEAT_COMMENTS
10446 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10447#endif
10448 0, old_indent);
10449 old_indent = 0;
10450#ifdef FEAT_CINDENT
10451 can_cindent = TRUE;
10452#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010453#ifdef FEAT_FOLDING
10454 /* When inserting a line the cursor line must never be in a closed fold. */
10455 foldOpenCursor();
10456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010458 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459}
10460
10461#ifdef FEAT_DIGRAPHS
10462/*
10463 * Handle digraph in insert mode.
10464 * Returns character still to be inserted, or NUL when nothing remaining to be
10465 * done.
10466 */
10467 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010468ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469{
10470 int c;
10471 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010472 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473
10474 pc_status = PC_STATUS_UNSET;
10475 if (redrawing() && !char_avail())
10476 {
10477 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010478 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479
10480 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010481 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482#ifdef FEAT_CMDL_INFO
10483 add_to_showcmd_c(Ctrl_K);
10484#endif
10485 }
10486
10487#ifdef USE_ON_FLY_SCROLL
10488 dont_scroll = TRUE; /* disallow scrolling here */
10489#endif
10490
10491 /* don't map the digraph chars. This also prevents the
10492 * mode message to be deleted when ESC is hit */
10493 ++no_mapping;
10494 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010495 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 --no_mapping;
10497 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010498 if (did_putchar)
10499 /* when the line fits in 'columns' the '?' is at the start of the next
10500 * line and will not be removed by the redraw */
10501 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010502
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 if (IS_SPECIAL(c) || mod_mask) /* special key */
10504 {
10505#ifdef FEAT_CMDL_INFO
10506 clear_showcmd();
10507#endif
10508 insert_special(c, TRUE, FALSE);
10509 return NUL;
10510 }
10511 if (c != ESC)
10512 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010513 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 if (redrawing() && !char_avail())
10515 {
10516 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010517 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518
10519 if (char2cells(c) == 1)
10520 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010521 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010523 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 }
10525#ifdef FEAT_CMDL_INFO
10526 add_to_showcmd_c(c);
10527#endif
10528 }
10529 ++no_mapping;
10530 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010531 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 --no_mapping;
10533 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010534 if (did_putchar)
10535 /* when the line fits in 'columns' the '?' is at the start of the
10536 * next line and will not be removed by a redraw */
10537 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 if (cc != ESC)
10539 {
10540 AppendToRedobuff((char_u *)CTRL_V_STR);
10541 c = getdigraph(c, cc, TRUE);
10542#ifdef FEAT_CMDL_INFO
10543 clear_showcmd();
10544#endif
10545 return c;
10546 }
10547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548#ifdef FEAT_CMDL_INFO
10549 clear_showcmd();
10550#endif
10551 return NUL;
10552}
10553#endif
10554
10555/*
10556 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10557 * Returns the char to be inserted, or NUL if none found.
10558 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010559 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010560ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561{
10562 int c;
10563 int temp;
10564 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010565 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566
10567 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10568 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010569 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 return NUL;
10571 }
10572
10573 /* try to advance to the cursor column */
10574 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010575 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 prev_ptr = ptr;
10577 validate_virtcol();
10578 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10579 {
10580 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010581 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 }
10583 if ((colnr_T)temp > curwin->w_virtcol)
10584 ptr = prev_ptr;
10585
10586#ifdef FEAT_MBYTE
10587 c = (*mb_ptr2char)(ptr);
10588#else
10589 c = *ptr;
10590#endif
10591 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010592 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 return c;
10594}
10595
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010596/*
10597 * CTRL-Y or CTRL-E typed in Insert mode.
10598 */
10599 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010600ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010601{
10602 int c = tc;
10603
10604#ifdef FEAT_INS_EXPAND
10605 if (ctrl_x_mode == CTRL_X_SCROLL)
10606 {
10607 if (c == Ctrl_Y)
10608 scrolldown_clamp();
10609 else
10610 scrollup_clamp();
10611 redraw_later(VALID);
10612 }
10613 else
10614#endif
10615 {
10616 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10617 if (c != NUL)
10618 {
10619 long tw_save;
10620
10621 /* The character must be taken literally, insert like it
10622 * was typed after a CTRL-V, and pretend 'textwidth'
10623 * wasn't set. Digits, 'o' and 'x' are special after a
10624 * CTRL-V, don't use it for these. */
10625 if (c < 256 && !isalnum(c))
10626 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10627 tw_save = curbuf->b_p_tw;
10628 curbuf->b_p_tw = -1;
10629 insert_special(c, TRUE, FALSE);
10630 curbuf->b_p_tw = tw_save;
10631#ifdef FEAT_RIGHTLEFT
10632 revins_chars++;
10633 revins_legal++;
10634#endif
10635 c = Ctrl_V; /* pretend CTRL-V is last character */
10636 auto_format(FALSE, TRUE);
10637 }
10638 }
10639 return c;
10640}
10641
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642#ifdef FEAT_SMARTINDENT
10643/*
10644 * Try to do some very smart auto-indenting.
10645 * Used when inserting a "normal" character.
10646 */
10647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010648ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
10650 pos_T *pos, old_pos;
10651 char_u *ptr;
10652 int i;
10653 int temp;
10654
10655 /*
10656 * do some very smart indenting when entering '{' or '}'
10657 */
10658 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10659 {
10660 /*
10661 * for '}' set indent equal to indent of line containing matching '{'
10662 */
10663 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10664 {
10665 old_pos = curwin->w_cursor;
10666 /*
10667 * If the matching '{' has a ')' immediately before it (ignoring
10668 * white-space), then line up with the start of the line
10669 * containing the matching '(' if there is one. This handles the
10670 * case where an "if (..\n..) {" statement continues over multiple
10671 * lines -- webb
10672 */
10673 ptr = ml_get(pos->lnum);
10674 i = pos->col;
10675 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010676 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 ;
10678 curwin->w_cursor.lnum = pos->lnum;
10679 curwin->w_cursor.col = i;
10680 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10681 curwin->w_cursor = *pos;
10682 i = get_indent();
10683 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010685 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 (void)set_indent(i, SIN_CHANGED);
10688 }
10689 else if (curwin->w_cursor.col > 0)
10690 {
10691 /*
10692 * when inserting '{' after "O" reduce indent, but not
10693 * more than indent of previous line
10694 */
10695 temp = TRUE;
10696 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10697 {
10698 old_pos = curwin->w_cursor;
10699 i = get_indent();
10700 while (curwin->w_cursor.lnum > 1)
10701 {
10702 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10703
10704 /* ignore empty lines and lines starting with '#'. */
10705 if (*ptr != '#' && *ptr != NUL)
10706 break;
10707 }
10708 if (get_indent() >= i)
10709 temp = FALSE;
10710 curwin->w_cursor = old_pos;
10711 }
10712 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010713 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 }
10715 }
10716
10717 /*
10718 * set indent of '#' always to 0
10719 */
10720 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10721 {
10722 /* remember current indent for next line */
10723 old_indent = get_indent();
10724 (void)set_indent(0, SIN_CHANGED);
10725 }
10726
10727 /* Adjust ai_col, the char at this position can be deleted. */
10728 if (ai_col > curwin->w_cursor.col)
10729 ai_col = curwin->w_cursor.col;
10730}
10731#endif
10732
10733/*
10734 * Get the value that w_virtcol would have when 'list' is off.
10735 * Unless 'cpo' contains the 'L' flag.
10736 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010737 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010738get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010740 // check validity of cursor in current buffer
10741 if (curwin->w_buffer == NULL
10742 || curwin->w_buffer->b_ml.ml_mfp == NULL
10743 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10744 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10746 return getvcol_nolist(&curwin->w_cursor);
10747 validate_virtcol();
10748 return curwin->w_virtcol;
10749}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010750
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010751#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010752/*
10753 * Handle the InsertCharPre autocommand.
10754 * "c" is the character that was typed.
10755 * Return a pointer to allocated memory with the replacement string.
10756 * Return NULL to continue inserting "c".
10757 */
10758 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010759do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010760{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010761 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010762 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010763 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010764
10765 /* Return quickly when there is nothing to do. */
10766 if (!has_insertcharpre())
10767 return NULL;
10768
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010769# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010770 if (has_mbyte)
10771 buf[(*mb_char2bytes)(c, buf)] = NUL;
10772 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010773# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010774 {
10775 buf[0] = c;
10776 buf[1] = NUL;
10777 }
10778
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010779 /* Lock the text to avoid weird things from happening. */
10780 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010781 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010782
Bram Moolenaar704984a2012-06-01 14:57:51 +020010783 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010784 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010785 {
10786 /* Get the value of v:char. It may be empty or more than one
10787 * character. Only use it when changed, otherwise continue with the
10788 * original character to avoid breaking autoindent. */
10789 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10790 res = vim_strsave(get_vim_var_str(VV_CHAR));
10791 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010792
10793 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10794 --textlock;
10795
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010796 // Restore the State, it may have been changed.
10797 State = save_State;
10798
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010799 return res;
10800}
10801#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010802
10803/*
10804 * Trigger "event" and take care of fixing undo.
10805 */
10806 static int
10807ins_apply_autocmds(event_T event)
10808{
10809 varnumber_T tick = CHANGEDTICK(curbuf);
10810 int r;
10811
10812 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10813
10814 // If u_savesub() was called then we are not prepared to start
10815 // a new line. Call u_save() with no contents to fix that.
10816 if (tick != CHANGEDTICK(curbuf))
10817 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10818
10819 return r;
10820}