blob: 33e0e6708bed766f561d5e18350bdb27748c4f28 [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 Moolenaar1d2ba7f2006-02-14 22:29:30 +00006501
Bram Moolenaar97b98102009-11-17 16:41:01 +00006502 virtcol = get_nolist_virtcol()
6503 + char2cells(c != NUL ? c : gchar_cursor());
6504 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006505 break;
6506
6507#ifdef FEAT_COMMENTS
6508 if (no_leader)
6509 do_comments = FALSE;
6510 else if (!(flags & INSCHAR_FORMAT)
6511 && has_format_option(FO_WRAP_COMS))
6512 do_comments = TRUE;
6513
6514 /* Don't break until after the comment leader */
6515 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006516 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006517 else
6518 leader_len = 0;
6519
6520 /* If the line doesn't start with a comment leader, then don't
6521 * start one in a following broken line. Avoids that a %word
6522 * moved to the start of the next line causes all following lines
6523 * to start with %. */
6524 if (leader_len == 0)
6525 no_leader = TRUE;
6526#endif
6527 if (!(flags & INSCHAR_FORMAT)
6528#ifdef FEAT_COMMENTS
6529 && leader_len == 0
6530#endif
6531 && !has_format_option(FO_WRAP))
6532
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006533 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006534 if ((startcol = curwin->w_cursor.col) == 0)
6535 break;
6536
6537 /* find column of textwidth border */
6538 coladvance((colnr_T)textwidth);
6539 wantcol = curwin->w_cursor.col;
6540
Bram Moolenaar97b98102009-11-17 16:41:01 +00006541 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542 foundcol = 0;
6543
6544 /*
6545 * Find position to break at.
6546 * Stop at first entered white when 'formatoptions' has 'v'
6547 */
6548 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006549 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006550 || curwin->w_cursor.lnum != Insstart.lnum
6551 || curwin->w_cursor.col >= Insstart.col)
6552 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006553 if (curwin->w_cursor.col == startcol && c != NUL)
6554 cc = c;
6555 else
6556 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006557 if (WHITECHAR(cc))
6558 {
6559 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006560 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006561
6562 /* find start of sequence of blanks */
6563 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6564 {
6565 dec_cursor();
6566 cc = gchar_cursor();
6567 }
6568 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6569 break; /* only spaces in front of text */
6570#ifdef FEAT_COMMENTS
6571 /* Don't break until after the comment leader */
6572 if (curwin->w_cursor.col < leader_len)
6573 break;
6574#endif
6575 if (has_format_option(FO_ONE_LETTER))
6576 {
6577 /* do not break after one-letter words */
6578 if (curwin->w_cursor.col == 0)
6579 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006580#ifdef FEAT_COMMENTS
6581 /* do not break "#a b" when 'tw' is 2 */
6582 if (curwin->w_cursor.col <= leader_len)
6583 break;
6584#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006585 col = curwin->w_cursor.col;
6586 dec_cursor();
6587 cc = gchar_cursor();
6588
6589 if (WHITECHAR(cc))
6590 continue; /* one-letter, continue */
6591 curwin->w_cursor.col = col;
6592 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006593
6594 inc_cursor();
6595
6596 end_foundcol = end_col + 1;
6597 foundcol = curwin->w_cursor.col;
6598 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006599 break;
6600 }
6601#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006602 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006603 {
6604 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006605 if (curwin->w_cursor.col != startcol)
6606 {
6607#ifdef FEAT_COMMENTS
6608 /* Don't break until after the comment leader */
6609 if (curwin->w_cursor.col < leader_len)
6610 break;
6611#endif
6612 col = curwin->w_cursor.col;
6613 inc_cursor();
6614 /* Don't change end_foundcol if already set. */
6615 if (foundcol != curwin->w_cursor.col)
6616 {
6617 foundcol = curwin->w_cursor.col;
6618 end_foundcol = foundcol;
6619 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6620 break;
6621 }
6622 curwin->w_cursor.col = col;
6623 }
6624
6625 if (curwin->w_cursor.col == 0)
6626 break;
6627
6628 col = curwin->w_cursor.col;
6629
6630 dec_cursor();
6631 cc = gchar_cursor();
6632
6633 if (WHITECHAR(cc))
6634 continue; /* break with space */
6635#ifdef FEAT_COMMENTS
6636 /* Don't break until after the comment leader */
6637 if (curwin->w_cursor.col < leader_len)
6638 break;
6639#endif
6640
6641 curwin->w_cursor.col = col;
6642
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006643 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006644 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006645 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6646 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006647 }
6648#endif
6649 if (curwin->w_cursor.col == 0)
6650 break;
6651 dec_cursor();
6652 }
6653
6654 if (foundcol == 0) /* no spaces, cannot break line */
6655 {
6656 curwin->w_cursor.col = startcol;
6657 break;
6658 }
6659
6660 /* Going to break the line, remove any "$" now. */
6661 undisplay_dollar();
6662
6663 /*
6664 * Offset between cursor position and line break is used by replace
6665 * stack functions. VREPLACE does not use this, and backspaces
6666 * over the text instead.
6667 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006668 if (State & VREPLACE_FLAG)
6669 orig_col = startcol; /* Will start backspacing from here */
6670 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006671 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006672
6673 /*
6674 * adjust startcol for spaces that will be deleted and
6675 * characters that will remain on top line
6676 */
6677 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006678 while ((cc = gchar_cursor(), WHITECHAR(cc))
6679 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006680 inc_cursor();
6681 startcol -= curwin->w_cursor.col;
6682 if (startcol < 0)
6683 startcol = 0;
6684
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006685 if (State & VREPLACE_FLAG)
6686 {
6687 /*
6688 * In VREPLACE mode, we will backspace over the text to be
6689 * wrapped, so save a copy now to put on the next line.
6690 */
6691 saved_text = vim_strsave(ml_get_cursor());
6692 curwin->w_cursor.col = orig_col;
6693 if (saved_text == NULL)
6694 break; /* Can't do it, out of memory */
6695 saved_text[startcol] = NUL;
6696
6697 /* Backspace over characters that will move to the next line */
6698 if (!fo_white_par)
6699 backspace_until_column(foundcol);
6700 }
6701 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702 {
6703 /* put cursor after pos. to break line */
6704 if (!fo_white_par)
6705 curwin->w_cursor.col = foundcol;
6706 }
6707
6708 /*
6709 * Split the line just before the margin.
6710 * Only insert/delete lines, but don't really redraw the window.
6711 */
6712 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6713 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6714#ifdef FEAT_COMMENTS
6715 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006716 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006717#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006718 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6719 if (!(flags & INSCHAR_COM_LIST))
6720 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006721
6722 replace_offset = 0;
6723 if (first_line)
6724 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006725 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006726 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006727 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006728 * This section is for auto-wrap of numeric lists. When not
6729 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6730 * flag will be set and open_line() will handle it (as seen
6731 * above). The code here (and in get_number_indent()) will
6732 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006733 */
6734 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006735 second_indent =
6736 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006737 if (second_indent >= 0)
6738 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006739 if (State & VREPLACE_FLAG)
6740 change_indent(INDENT_SET, second_indent,
6741 FALSE, NUL, TRUE);
6742 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006743#ifdef FEAT_COMMENTS
6744 if (leader_len > 0 && second_indent - leader_len > 0)
6745 {
6746 int i;
6747 int padding = second_indent - leader_len;
6748
6749 /* We started at the first_line of a numbered list
6750 * that has a comment. the open_line() function has
6751 * inserted the proper comment leader and positioned
6752 * the cursor at the end of the split line. Now we
6753 * add the additional whitespace needed after the
6754 * comment leader for the numbered list. */
6755 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006756 ins_str((char_u *)" ");
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006757 }
6758 else
6759 {
6760#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006761 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006762#ifdef FEAT_COMMENTS
6763 }
6764#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006765 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006766 }
6767 first_line = FALSE;
6768 }
6769
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006770 if (State & VREPLACE_FLAG)
6771 {
6772 /*
6773 * In VREPLACE mode we have backspaced over the text to be
6774 * moved, now we re-insert it into the new line.
6775 */
6776 ins_bytes(saved_text);
6777 vim_free(saved_text);
6778 }
6779 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006780 {
6781 /*
6782 * Check if cursor is not past the NUL off the line, cindent
6783 * may have added or removed indent.
6784 */
6785 curwin->w_cursor.col += startcol;
6786 len = (colnr_T)STRLEN(ml_get_curline());
6787 if (curwin->w_cursor.col > len)
6788 curwin->w_cursor.col = len;
6789 }
6790
6791 haveto_redraw = TRUE;
6792#ifdef FEAT_CINDENT
6793 can_cindent = TRUE;
6794#endif
6795 /* moved the cursor, don't autoindent or cindent now */
6796 did_ai = FALSE;
6797#ifdef FEAT_SMARTINDENT
6798 did_si = FALSE;
6799 can_si = FALSE;
6800 can_si_back = FALSE;
6801#endif
6802 line_breakcheck();
6803 }
6804
6805 if (save_char != NUL) /* put back space after cursor */
6806 pchar_cursor(save_char);
6807
Bram Moolenaar0026d472014-09-09 16:32:39 +02006808#ifdef FEAT_LINEBREAK
6809 curwin->w_p_lbr = has_lbr;
6810#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006811 if (!format_only && haveto_redraw)
6812 {
6813 update_topline();
6814 redraw_curbuf_later(VALID);
6815 }
6816}
6817
6818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 * Called after inserting or deleting text: When 'formatoptions' includes the
6820 * 'a' flag format from the current line until the end of the paragraph.
6821 * Keep the cursor at the same position relative to the text.
6822 * The caller must have saved the cursor line for undo, following ones will be
6823 * saved here.
6824 */
6825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006826auto_format(
6827 int trailblank, /* when TRUE also format with trailing blank */
6828 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 pos_T pos;
6831 colnr_T len;
6832 char_u *old;
6833 char_u *new, *pnew;
6834 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006835 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836
6837 if (!has_format_option(FO_AUTO))
6838 return;
6839
6840 pos = curwin->w_cursor;
6841 old = ml_get_curline();
6842
6843 /* may remove added space */
6844 check_auto_format(FALSE);
6845
6846 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6847 * user might insert normal text next. Also skip formatting when "1" is
6848 * in 'formatoptions' and there is a single character before the cursor.
6849 * Otherwise the line would be broken and when typing another non-white
6850 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006851 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 if (*old != NUL && !trailblank && wasatend)
6853 {
6854 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006855 cc = gchar_cursor();
6856 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6857 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006859 cc = gchar_cursor();
6860 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 {
6862 curwin->w_cursor = pos;
6863 return;
6864 }
6865 curwin->w_cursor = pos;
6866 }
6867
6868#ifdef FEAT_COMMENTS
6869 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6870 * comments. */
6871 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006872 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 return;
6874#endif
6875
6876 /*
6877 * May start formatting in a previous line, so that after "x" a word is
6878 * moved to the previous line if it fits there now. Only when this is not
6879 * the start of a paragraph.
6880 */
6881 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6882 {
6883 --curwin->w_cursor.lnum;
6884 if (u_save_cursor() == FAIL)
6885 return;
6886 }
6887
6888 /*
6889 * Do the formatting and restore the cursor position. "saved_cursor" will
6890 * be adjusted for the text formatting.
6891 */
6892 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006893 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 curwin->w_cursor = saved_cursor;
6895 saved_cursor.lnum = 0;
6896
6897 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6898 {
6899 /* "cannot happen" */
6900 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6901 coladvance((colnr_T)MAXCOL);
6902 }
6903 else
6904 check_cursor_col();
6905
6906 /* Insert mode: If the cursor is now after the end of the line while it
6907 * previously wasn't, the line was broken. Because of the rule above we
6908 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6909 * formatted. */
6910 if (!wasatend && has_format_option(FO_WHITE_PAR))
6911 {
6912 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006913 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (curwin->w_cursor.col == len)
6915 {
6916 pnew = vim_strnsave(new, len + 2);
6917 pnew[len] = ' ';
6918 pnew[len + 1] = NUL;
6919 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6920 /* remove the space later */
6921 did_add_space = TRUE;
6922 }
6923 else
6924 /* may remove added space */
6925 check_auto_format(FALSE);
6926 }
6927
6928 check_cursor();
6929}
6930
6931/*
6932 * When an extra space was added to continue a paragraph for auto-formatting,
6933 * delete it now. The space must be under the cursor, just after the insert
6934 * position.
6935 */
6936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006937check_auto_format(
6938 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
6940 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006941 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942
6943 if (did_add_space)
6944 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006945 cc = gchar_cursor();
6946 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 /* Somehow the space was removed already. */
6948 did_add_space = FALSE;
6949 else
6950 {
6951 if (!end_insert)
6952 {
6953 inc_cursor();
6954 c = gchar_cursor();
6955 dec_cursor();
6956 }
6957 if (c != NUL)
6958 {
6959 /* The space is no longer at the end of the line, delete it. */
6960 del_char(FALSE);
6961 did_add_space = FALSE;
6962 }
6963 }
6964 }
6965}
6966
6967/*
6968 * Find out textwidth to be used for formatting:
6969 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006970 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 * if invalid value, use 0.
6972 * Set default to window width (maximum 79) for "gq" operator.
6973 */
6974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006975comp_textwidth(
6976 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977{
6978 int textwidth;
6979
6980 textwidth = curbuf->b_p_tw;
6981 if (textwidth == 0 && curbuf->b_p_wm)
6982 {
6983 /* The width is the window width minus 'wrapmargin' minus all the
6984 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006985 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986#ifdef FEAT_CMDWIN
6987 if (cmdwin_type != 0)
6988 textwidth -= 1;
6989#endif
6990#ifdef FEAT_FOLDING
6991 textwidth -= curwin->w_p_fdc;
6992#endif
6993#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006994 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 textwidth -= 1;
6996#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006997 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 textwidth -= 8;
6999 }
7000 if (textwidth < 0)
7001 textwidth = 0;
7002 if (ff && textwidth == 0)
7003 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007004 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 if (textwidth > 79)
7006 textwidth = 79;
7007 }
7008 return textwidth;
7009}
7010
7011/*
7012 * Put a character in the redo buffer, for when just after a CTRL-V.
7013 */
7014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007015redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 char_u buf[10];
7018
7019 /* Only digits need special treatment. Translate them into a string of
7020 * three digits. */
7021 if (VIM_ISDIGIT(c))
7022 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007023 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 AppendToRedobuff(buf);
7025 }
7026 else
7027 AppendCharToRedobuff(c);
7028}
7029
7030/*
7031 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007032 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 */
7034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007035start_arrow(
7036 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007038 start_arrow_common(end_insert_pos, TRUE);
7039}
7040
7041/*
7042 * Like start_arrow() but with end_change argument.
7043 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7044 */
7045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007046start_arrow_with_change(
7047 pos_T *end_insert_pos, /* can be NULL */
7048 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007049{
7050 start_arrow_common(end_insert_pos, end_change);
7051 if (!end_change)
7052 {
7053 AppendCharToRedobuff(Ctrl_G);
7054 AppendCharToRedobuff('U');
7055 }
7056}
7057
7058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007059start_arrow_common(
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 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {
7065 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007066 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 arrow_used = TRUE; /* this means we stopped the current insert */
7068 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007069#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007070 check_spell_redraw();
7071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072}
7073
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007074#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007075/*
7076 * If we skipped highlighting word at cursor, do it now.
7077 * It may be skipped again, thus reset spell_redraw_lnum first.
7078 */
7079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007080check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007081{
7082 if (spell_redraw_lnum != 0)
7083 {
7084 linenr_T lnum = spell_redraw_lnum;
7085
7086 spell_redraw_lnum = 0;
Bram Moolenaarae12f4b2019-01-09 20:51:04 +01007087 redrawWinline(curwin, lnum);
Bram Moolenaar217ad922005-03-20 22:37:15 +00007088 }
7089}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007090
7091/*
7092 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7093 * spelled word, if there is one.
7094 */
7095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007096spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007097{
7098 pos_T tpos = curwin->w_cursor;
7099
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007100 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007101 if (curwin->w_cursor.col != tpos.col)
7102 start_arrow(&tpos);
7103}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007104#endif
7105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106/*
7107 * stop_arrow() is called before a change is made in insert mode.
7108 * If an arrow key has been used, start a new insertion.
7109 * Returns FAIL if undo is impossible, shouldn't insert then.
7110 */
7111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007112stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114 if (arrow_used)
7115 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007116 Insstart = curwin->w_cursor; /* new insertion starts here */
7117 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7118 /* Don't update the original insert position when moved to the
7119 * right, except when nothing was inserted yet. */
7120 update_Insstart_orig = FALSE;
7121 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7122
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 if (u_save_cursor() == OK)
7124 {
7125 arrow_used = FALSE;
7126 ins_need_undo = FALSE;
7127 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 if (State & VREPLACE_FLAG)
7131 {
7132 orig_line_count = curbuf->b_ml.ml_line_count;
7133 vr_lines_changed = 1;
7134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 ResetRedobuff();
7136 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007137 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 }
7139 else if (ins_need_undo)
7140 {
7141 if (u_save_cursor() == OK)
7142 ins_need_undo = FALSE;
7143 }
7144
7145#ifdef FEAT_FOLDING
7146 /* Always open fold at the cursor line when inserting something. */
7147 foldOpenCursor();
7148#endif
7149
7150 return (arrow_used || ins_need_undo ? FAIL : OK);
7151}
7152
7153/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007154 * Do a few things to stop inserting.
7155 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7156 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 */
7158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007159stop_insert(
7160 pos_T *end_insert_pos,
7161 int esc, /* called by ins_esc() */
7162 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007164 int cc;
7165 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
7167 stop_redo_ins();
7168 replace_flush(); /* abandon replace stack */
7169
7170 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007171 * Save the inserted text for later redo with ^@ and CTRL-A.
7172 * Don't do it when "restart_edit" was set and nothing was inserted,
7173 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007175 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007176 if (did_restart_edit == 0 || (ptr != NULL
7177 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007178 {
7179 vim_free(last_insert);
7180 last_insert = ptr;
7181 last_insert_skip = new_insert_skip;
7182 }
7183 else
7184 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007186 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 /* Auto-format now. It may seem strange to do this when stopping an
7189 * insertion (or moving the cursor), but it's required when appending
7190 * a line and having it end in a space. But only do it when something
7191 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007192 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007194 pos_T tpos = curwin->w_cursor;
7195
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 /* When the cursor is at the end of the line after a space the
7197 * formatting will move it to the following word. Avoid that by
7198 * moving the cursor onto the space. */
7199 cc = 'x';
7200 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7201 {
7202 dec_cursor();
7203 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007204 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007205 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
7207
7208 auto_format(TRUE, FALSE);
7209
Bram Moolenaar1c465442017-03-12 20:10:05 +01007210 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007211 {
7212 if (gchar_cursor() != NUL)
7213 inc_cursor();
7214#ifdef FEAT_VIRTUALEDIT
7215 /* If the cursor is still at the same character, also keep
7216 * the "coladd". */
7217 if (gchar_cursor() == NUL
7218 && curwin->w_cursor.lnum == tpos.lnum
7219 && curwin->w_cursor.col == tpos.col)
7220 curwin->w_cursor.coladd = tpos.coladd;
7221#endif
7222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 }
7224
7225 /* If a space was inserted for auto-formatting, remove it now. */
7226 check_auto_format(TRUE);
7227
7228 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007229 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007230 * Do this when ESC was used or moving the cursor up/down.
7231 * Check for the old position still being valid, just in case the text
7232 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007233 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007234 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7235 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007237 pos_T tpos = curwin->w_cursor;
7238
7239 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007240 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007241 for (;;)
7242 {
7243 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7244 --curwin->w_cursor.col;
7245 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007246 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007247 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007248 if (del_char(TRUE) == FAIL)
7249 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007250 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007251 if (curwin->w_cursor.lnum != tpos.lnum)
7252 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007253 else
7254 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007255 /* reset tpos, could have been invalidated in the loop above */
7256 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007257 tpos.col++;
7258 if (cc != NUL && gchar_pos(&tpos) == NUL)
7259 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 /* <C-S-Right> may have started Visual mode, adjust the position for
7263 * deleted characters. */
7264 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7265 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007266 int len = (int)STRLEN(ml_get_curline());
7267
7268 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007270 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007271#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 }
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277 }
7278 did_ai = FALSE;
7279#ifdef FEAT_SMARTINDENT
7280 did_si = FALSE;
7281 can_si = FALSE;
7282 can_si_back = FALSE;
7283#endif
7284
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007285 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7286 * now in a different buffer. */
7287 if (end_insert_pos != NULL)
7288 {
7289 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007290 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007291 curbuf->b_op_end = *end_insert_pos;
7292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293}
7294
7295/*
7296 * Set the last inserted text to a single character.
7297 * Used for the replace command.
7298 */
7299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
7302 char_u *s;
7303
7304 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 if (last_insert != NULL)
7307 {
7308 s = last_insert;
7309 /* Use the CTRL-V only when entering a special char */
7310 if (c < ' ' || c == DEL)
7311 *s++ = Ctrl_V;
7312 s = add_char2buf(c, s);
7313 *s++ = ESC;
7314 *s++ = NUL;
7315 last_insert_skip = 0;
7316 }
7317}
7318
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007319#if defined(EXITFREE) || defined(PROTO)
7320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007321free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007322{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007323 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007324# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007325 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007326# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007327}
7328#endif
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330/*
7331 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7332 * and CSI. Handle multi-byte characters.
7333 * Returns a pointer to after the added bytes.
7334 */
7335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007336add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
7338#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007339 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 int i;
7341 int len;
7342
7343 len = (*mb_char2bytes)(c, temp);
7344 for (i = 0; i < len; ++i)
7345 {
7346 c = temp[i];
7347#endif
7348 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7349 if (c == K_SPECIAL)
7350 {
7351 *s++ = K_SPECIAL;
7352 *s++ = KS_SPECIAL;
7353 *s++ = KE_FILLER;
7354 }
7355#ifdef FEAT_GUI
7356 else if (c == CSI)
7357 {
7358 *s++ = CSI;
7359 *s++ = KS_EXTRA;
7360 *s++ = (int)KE_CSI;
7361 }
7362#endif
7363 else
7364 *s++ = c;
7365#ifdef FEAT_MBYTE
7366 }
7367#endif
7368 return s;
7369}
7370
7371/*
7372 * move cursor to start of line
7373 * if flags & BL_WHITE move to first non-white
7374 * if flags & BL_SOL move to first non-white if startofline is set,
7375 * otherwise keep "curswant" column
7376 * if flags & BL_FIX don't leave the cursor on a NUL.
7377 */
7378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007379beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 if ((flags & BL_SOL) && !p_sol)
7382 coladvance(curwin->w_curswant);
7383 else
7384 {
7385 curwin->w_cursor.col = 0;
7386#ifdef FEAT_VIRTUALEDIT
7387 curwin->w_cursor.coladd = 0;
7388#endif
7389
7390 if (flags & (BL_WHITE | BL_SOL))
7391 {
7392 char_u *ptr;
7393
Bram Moolenaar1c465442017-03-12 20:10:05 +01007394 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7396 ++curwin->w_cursor.col;
7397 }
7398 curwin->w_set_curswant = TRUE;
7399 }
7400}
7401
7402/*
7403 * oneright oneleft cursor_down cursor_up
7404 *
7405 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007406 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * Return OK when successful, FAIL when we hit a line of file boundary.
7408 */
7409
7410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
7416#ifdef FEAT_VIRTUALEDIT
7417 if (virtual_active())
7418 {
7419 pos_T prevpos = curwin->w_cursor;
7420
7421 /* Adjust for multi-wide char (excluding TAB) */
7422 ptr = ml_get_cursor();
7423 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007424# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007426# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 ))
7430 ? ptr2cells(ptr) : 1));
7431 curwin->w_set_curswant = TRUE;
7432 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7433 return (prevpos.col != curwin->w_cursor.col
7434 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7435 }
7436#endif
7437
7438 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007439 if (*ptr == NUL)
7440 return FAIL; /* already at the very end */
7441
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007443 if (has_mbyte)
7444 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 else
7446#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007447 l = 1;
7448
7449 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7450 * contains "onemore". */
7451 if (ptr[l] == NUL
7452#ifdef FEAT_VIRTUALEDIT
7453 && (ve_flags & VE_ONEMORE) == 0
7454#endif
7455 )
7456 return FAIL;
7457 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 curwin->w_set_curswant = TRUE;
7460 return OK;
7461}
7462
7463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007464oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465{
7466#ifdef FEAT_VIRTUALEDIT
7467 if (virtual_active())
7468 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007469# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 int v = getviscol();
7473
7474 if (v == 0)
7475 return FAIL;
7476
7477# ifdef FEAT_LINEBREAK
7478 /* We might get stuck on 'showbreak', skip over it. */
7479 width = 1;
7480 for (;;)
7481 {
7482 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007483 /* getviscol() is slow, skip it when 'showbreak' is empty,
7484 * 'breakindent' is not set and there are no multi-byte
7485 * characters */
7486 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487# ifdef FEAT_MBYTE
7488 && !has_mbyte
7489# endif
7490 ) || getviscol() < v)
7491 break;
7492 ++width;
7493 }
7494# else
7495 coladvance(v - 1);
7496# endif
7497
7498 if (curwin->w_cursor.coladd == 1)
7499 {
7500 char_u *ptr;
7501
7502 /* Adjust for multi-wide char (not a TAB) */
7503 ptr = ml_get_cursor();
7504 if (*ptr != TAB && vim_isprintc(
7505# ifdef FEAT_MBYTE
7506 (*mb_ptr2char)(ptr)
7507# else
7508 *ptr
7509# endif
7510 ) && ptr2cells(ptr) > 1)
7511 curwin->w_cursor.coladd = 0;
7512 }
7513
7514 curwin->w_set_curswant = TRUE;
7515 return OK;
7516 }
7517#endif
7518
7519 if (curwin->w_cursor.col == 0)
7520 return FAIL;
7521
7522 curwin->w_set_curswant = TRUE;
7523 --curwin->w_cursor.col;
7524
7525#ifdef FEAT_MBYTE
7526 /* if the character on the left of the current cursor is a multi-byte
7527 * character, move to its first byte */
7528 if (has_mbyte)
7529 mb_adjust_cursor();
7530#endif
7531 return OK;
7532}
7533
7534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535cursor_up(
7536 long n,
7537 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
7539 linenr_T lnum;
7540
7541 if (n > 0)
7542 {
7543 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007544 /* This fails if the cursor is already in the first line or the count
7545 * is larger than the line number and '-' is in 'cpoptions' */
7546 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 return FAIL;
7548 if (n >= lnum)
7549 lnum = 1;
7550 else
7551#ifdef FEAT_FOLDING
7552 if (hasAnyFolding(curwin))
7553 {
7554 /*
7555 * Count each sequence of folded lines as one logical line.
7556 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007557 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 (void)hasFolding(lnum, &lnum, NULL);
7559
7560 while (n--)
7561 {
7562 /* move up one line */
7563 --lnum;
7564 if (lnum <= 1)
7565 break;
7566 /* If we entered a fold, move to the beginning, unless in
7567 * Insert mode or when 'foldopen' contains "all": it will open
7568 * in a moment. */
7569 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7570 (void)hasFolding(lnum, &lnum, NULL);
7571 }
7572 if (lnum < 1)
7573 lnum = 1;
7574 }
7575 else
7576#endif
7577 lnum -= n;
7578 curwin->w_cursor.lnum = lnum;
7579 }
7580
7581 /* try to advance to the column we want to be at */
7582 coladvance(curwin->w_curswant);
7583
7584 if (upd_topline)
7585 update_topline(); /* make sure curwin->w_topline is valid */
7586
7587 return OK;
7588}
7589
7590/*
7591 * Cursor down a number of logical lines.
7592 */
7593 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007594cursor_down(
7595 long n,
7596 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598 linenr_T lnum;
7599
7600 if (n > 0)
7601 {
7602 lnum = curwin->w_cursor.lnum;
7603#ifdef FEAT_FOLDING
7604 /* Move to last line of fold, will fail if it's the end-of-file. */
7605 (void)hasFolding(lnum, NULL, &lnum);
7606#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007607 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007608 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007609 if (lnum >= curbuf->b_ml.ml_line_count
7610 || (lnum + n > curbuf->b_ml.ml_line_count
7611 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 return FAIL;
7613 if (lnum + n >= curbuf->b_ml.ml_line_count)
7614 lnum = curbuf->b_ml.ml_line_count;
7615 else
7616#ifdef FEAT_FOLDING
7617 if (hasAnyFolding(curwin))
7618 {
7619 linenr_T last;
7620
7621 /* count each sequence of folded lines as one logical line */
7622 while (n--)
7623 {
7624 if (hasFolding(lnum, NULL, &last))
7625 lnum = last + 1;
7626 else
7627 ++lnum;
7628 if (lnum >= curbuf->b_ml.ml_line_count)
7629 break;
7630 }
7631 if (lnum > curbuf->b_ml.ml_line_count)
7632 lnum = curbuf->b_ml.ml_line_count;
7633 }
7634 else
7635#endif
7636 lnum += n;
7637 curwin->w_cursor.lnum = lnum;
7638 }
7639
7640 /* try to advance to the column we want to be at */
7641 coladvance(curwin->w_curswant);
7642
7643 if (upd_topline)
7644 update_topline(); /* make sure curwin->w_topline is valid */
7645
7646 return OK;
7647}
7648
7649/*
7650 * Stuff the last inserted text in the read buffer.
7651 * Last_insert actually is a copy of the redo buffer, so we
7652 * first have to remove the command.
7653 */
7654 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007655stuff_inserted(
7656 int c, /* Command character to be inserted */
7657 long count, /* Repeat this many times */
7658 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
7660 char_u *esc_ptr;
7661 char_u *ptr;
7662 char_u *last_ptr;
7663 char_u last = NUL;
7664
7665 ptr = get_last_insert();
7666 if (ptr == NULL)
7667 {
7668 EMSG(_(e_noinstext));
7669 return FAIL;
7670 }
7671
7672 /* may want to stuff the command character, to start Insert mode */
7673 if (c != NUL)
7674 stuffcharReadbuff(c);
7675 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7676 *esc_ptr = NUL; /* remove the ESC */
7677
7678 /* when the last char is either "0" or "^" it will be quoted if no ESC
7679 * comes after it OR if it will inserted more than once and "ptr"
7680 * starts with ^D. -- Acevedo
7681 */
7682 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7683 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7684 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7685 {
7686 last = *last_ptr;
7687 *last_ptr = NUL;
7688 }
7689
7690 do
7691 {
7692 stuffReadbuff(ptr);
7693 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7694 if (last)
7695 stuffReadbuff((char_u *)(last == '0'
7696 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7697 : IF_EB("\026^", CTRL_V_STR "^")));
7698 }
7699 while (--count > 0);
7700
7701 if (last)
7702 *last_ptr = last;
7703
7704 if (esc_ptr != NULL)
7705 *esc_ptr = ESC; /* put the ESC back */
7706
7707 /* may want to stuff a trailing ESC, to get out of Insert mode */
7708 if (!no_esc)
7709 stuffcharReadbuff(ESC);
7710
7711 return OK;
7712}
7713
7714 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007715get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 if (last_insert == NULL)
7718 return NULL;
7719 return last_insert + last_insert_skip;
7720}
7721
7722/*
7723 * Get last inserted string, and remove trailing <Esc>.
7724 * Returns pointer to allocated memory (must be freed) or NULL.
7725 */
7726 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007727get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
7729 char_u *s;
7730 int len;
7731
7732 if (last_insert == NULL)
7733 return NULL;
7734 s = vim_strsave(last_insert + last_insert_skip);
7735 if (s != NULL)
7736 {
7737 len = (int)STRLEN(s);
7738 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7739 s[len - 1] = NUL;
7740 }
7741 return s;
7742}
7743
7744/*
7745 * Check the word in front of the cursor for an abbreviation.
7746 * Called when the non-id character "c" has been entered.
7747 * When an abbreviation is recognized it is removed from the text and
7748 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7749 */
7750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007751echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 /* Don't check for abbreviation in paste mode, when disabled and just
7754 * after moving around with cursor keys. */
7755 if (p_paste || no_abbr || arrow_used)
7756 return FALSE;
7757
7758 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7759 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7760}
7761
7762/*
7763 * replace-stack functions
7764 *
7765 * When replacing characters, the replaced characters are remembered for each
7766 * new character. This is used to re-insert the old text when backspacing.
7767 *
7768 * There is a NUL headed list of characters for each character that is
7769 * currently in the file after the insertion point. When BS is used, one NUL
7770 * headed list is put back for the deleted character.
7771 *
7772 * For a newline, there are two NUL headed lists. One contains the characters
7773 * that the NL replaced. The extra one stores the characters after the cursor
7774 * that were deleted (always white space).
7775 *
7776 * Replace_offset is normally 0, in which case replace_push will add a new
7777 * character at the end of the stack. If replace_offset is not 0, that many
7778 * characters will be left on the stack above the newly inserted character.
7779 */
7780
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007781static char_u *replace_stack = NULL;
7782static long replace_stack_nr = 0; /* next entry in replace stack */
7783static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
7785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007786replace_push(
7787 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 char_u *p;
7790
7791 if (replace_stack_nr < replace_offset) /* nothing to do */
7792 return;
7793 if (replace_stack_len <= replace_stack_nr)
7794 {
7795 replace_stack_len += 50;
7796 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7797 if (p == NULL) /* out of memory */
7798 {
7799 replace_stack_len -= 50;
7800 return;
7801 }
7802 if (replace_stack != NULL)
7803 {
7804 mch_memmove(p, replace_stack,
7805 (size_t)(replace_stack_nr * sizeof(char_u)));
7806 vim_free(replace_stack);
7807 }
7808 replace_stack = p;
7809 }
7810 p = replace_stack + replace_stack_nr - replace_offset;
7811 if (replace_offset)
7812 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7813 *p = c;
7814 ++replace_stack_nr;
7815}
7816
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007817#if defined(FEAT_MBYTE) || defined(PROTO)
7818/*
7819 * Push a character onto the replace stack. Handles a multi-byte character in
7820 * reverse byte order, so that the first byte is popped off first.
7821 * Return the number of bytes done (includes composing characters).
7822 */
7823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007824replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007825{
7826 int l = (*mb_ptr2len)(p);
7827 int j;
7828
7829 for (j = l - 1; j >= 0; --j)
7830 replace_push(p[j]);
7831 return l;
7832}
7833#endif
7834
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835/*
7836 * Pop one item from the replace stack.
7837 * return -1 if stack empty
7838 * return replaced character or NUL otherwise
7839 */
7840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007841replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 if (replace_stack_nr == 0)
7844 return -1;
7845 return (int)replace_stack[--replace_stack_nr];
7846}
7847
7848/*
7849 * Join the top two items on the replace stack. This removes to "off"'th NUL
7850 * encountered.
7851 */
7852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007853replace_join(
7854 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 int i;
7857
7858 for (i = replace_stack_nr; --i >= 0; )
7859 if (replace_stack[i] == NUL && off-- <= 0)
7860 {
7861 --replace_stack_nr;
7862 mch_memmove(replace_stack + i, replace_stack + i + 1,
7863 (size_t)(replace_stack_nr - i));
7864 return;
7865 }
7866}
7867
7868/*
7869 * Pop bytes from the replace stack until a NUL is found, and insert them
7870 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7871 */
7872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007873replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 int cc;
7876 int oldState = State;
7877
7878 State = NORMAL; /* don't want REPLACE here */
7879 while ((cc = replace_pop()) > 0)
7880 {
7881#ifdef FEAT_MBYTE
7882 mb_replace_pop_ins(cc);
7883#else
7884 ins_char(cc);
7885#endif
7886 dec_cursor();
7887 }
7888 State = oldState;
7889}
7890
7891#ifdef FEAT_MBYTE
7892/*
7893 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7894 * indicates a multi-byte char, pop the other bytes too.
7895 */
7896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007897mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007900 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 int i;
7902 int c;
7903
7904 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7905 {
7906 buf[0] = cc;
7907 for (i = 1; i < n; ++i)
7908 buf[i] = replace_pop();
7909 ins_bytes_len(buf, n);
7910 }
7911 else
7912 ins_char(cc);
7913
7914 if (enc_utf8)
7915 /* Handle composing chars. */
7916 for (;;)
7917 {
7918 c = replace_pop();
7919 if (c == -1) /* stack empty */
7920 break;
7921 if ((n = MB_BYTE2LEN(c)) == 1)
7922 {
7923 /* Not a multi-byte char, put it back. */
7924 replace_push(c);
7925 break;
7926 }
7927 else
7928 {
7929 buf[0] = c;
7930 for (i = 1; i < n; ++i)
7931 buf[i] = replace_pop();
7932 if (utf_iscomposing(utf_ptr2char(buf)))
7933 ins_bytes_len(buf, n);
7934 else
7935 {
7936 /* Not a composing char, put it back. */
7937 for (i = n - 1; i >= 0; --i)
7938 replace_push(buf[i]);
7939 break;
7940 }
7941 }
7942 }
7943}
7944#endif
7945
7946/*
7947 * make the replace stack empty
7948 * (called when exiting replace mode)
7949 */
7950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007951replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007953 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 replace_stack_len = 0;
7955 replace_stack_nr = 0;
7956}
7957
7958/*
7959 * Handle doing a BS for one character.
7960 * cc < 0: replace stack empty, just move cursor
7961 * cc == 0: character was inserted, delete it
7962 * cc > 0: character was replaced, put cc (first byte of original char) back
7963 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007964 * When "limit_col" is >= 0, don't delete before this column. Matters when
7965 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 */
7967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007968replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 int orig_len = 0;
7972 int ins_len;
7973 int orig_vcols = 0;
7974 colnr_T start_vcol;
7975 char_u *p;
7976 int i;
7977 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978
7979 cc = replace_pop();
7980 if (cc > 0)
7981 {
Bram Moolenaar196d1572019-01-02 23:47:18 +01007982#ifdef FEAT_TEXT_PROP
Bram Moolenaar6d11f3b2019-01-06 17:44:38 +01007983 size_t len_before = 0; // init to shut up GCC
Bram Moolenaar196d1572019-01-02 23:47:18 +01007984
7985 if (curbuf->b_has_textprop)
7986 {
7987 // Do not adjust text properties for individual delete and insert
7988 // operations, do it afterwards on the resulting text.
7989 len_before = STRLEN(ml_get_curline());
7990 ++text_prop_frozen;
7991 }
7992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (State & VREPLACE_FLAG)
7994 {
7995 /* Get the number of screen cells used by the character we are
7996 * going to delete. */
7997 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7998 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000#ifdef FEAT_MBYTE
8001 if (has_mbyte)
8002 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008003 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008005 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 replace_push(cc);
8007 }
8008 else
8009#endif
8010 {
8011 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008013 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 }
8015 replace_pop_ins();
8016
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (State & VREPLACE_FLAG)
8018 {
8019 /* Get the number of screen cells used by the inserted characters */
8020 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008021 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 vcol = start_vcol;
8023 for (i = 0; i < ins_len; ++i)
8024 {
8025 vcol += chartabsize(p + i, vcol);
8026#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008027 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028#endif
8029 }
8030 vcol -= start_vcol;
8031
8032 /* Delete spaces that were inserted after the cursor to keep the
8033 * text aligned. */
8034 curwin->w_cursor.col += ins_len;
8035 while (vcol > orig_vcols && gchar_cursor() == ' ')
8036 {
8037 del_char(FALSE);
8038 ++orig_vcols;
8039 }
8040 curwin->w_cursor.col -= ins_len;
8041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042
Bram Moolenaar196d1572019-01-02 23:47:18 +01008043 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar196d1572019-01-02 23:47:18 +01008045
8046#ifdef FEAT_TEXT_PROP
8047 if (curbuf->b_has_textprop)
8048 {
8049 size_t len_now = STRLEN(ml_get_curline());
8050
8051 --text_prop_frozen;
8052 adjust_prop_columns(curwin->w_cursor.lnum, curwin->w_cursor.col,
8053 (int)(len_now - len_before));
8054 }
8055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 }
8057 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008058 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059}
8060
8061#ifdef FEAT_CINDENT
8062/*
8063 * Return TRUE if C-indenting is on.
8064 */
8065 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008066cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067{
8068 return (!p_paste && (curbuf->b_p_cin
8069# ifdef FEAT_EVAL
8070 || *curbuf->b_p_inde != NUL
8071# endif
8072 ));
8073}
8074#endif
8075
8076#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8077/*
8078 * Re-indent the current line, based on the current contents of it and the
8079 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8080 * confused what all the part that handles Control-T is doing that I'm not.
8081 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8082 */
8083
8084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008087 int amount = get_the_indent();
8088
8089 if (amount >= 0)
8090 {
8091 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8092 if (linewhite(curwin->w_cursor.lnum))
8093 did_ai = TRUE; /* delete the indent if the line stays empty */
8094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095}
8096
8097 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008098fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 if (p_paste)
8101 return;
8102# ifdef FEAT_LISP
8103 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8104 fixthisline(get_lisp_indent);
8105# endif
8106# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8107 else
8108# endif
8109# ifdef FEAT_CINDENT
8110 if (cindent_on())
8111 do_c_expr_indent();
8112# endif
8113}
8114
8115#endif
8116
8117#ifdef FEAT_CINDENT
8118/*
8119 * return TRUE if 'cinkeys' contains the key "keytyped",
8120 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008121 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8123 *
8124 * "keytyped" can have a few special values:
8125 * KEY_OPEN_FORW
8126 * KEY_OPEN_BACK
8127 * KEY_COMPLETE just finished completion.
8128 *
8129 * If line_is_empty is TRUE accept keys with '0' before them.
8130 */
8131 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008132in_cinkeys(
8133 int keytyped,
8134 int when,
8135 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 char_u *look;
8138 int try_match;
8139 int try_match_word;
8140 char_u *p;
8141 char_u *line;
8142 int icase;
8143 int i;
8144
Bram Moolenaar30848942009-12-24 14:46:12 +00008145 if (keytyped == NUL)
8146 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8147 return FALSE;
8148
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149#ifdef FEAT_EVAL
8150 if (*curbuf->b_p_inde != NUL)
8151 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8152 else
8153#endif
8154 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8155 while (*look)
8156 {
8157 /*
8158 * Find out if we want to try a match with this key, depending on
8159 * 'when' and a '*' or '!' before the key.
8160 */
8161 switch (when)
8162 {
8163 case '*': try_match = (*look == '*'); break;
8164 case '!': try_match = (*look == '!'); break;
8165 default: try_match = (*look != '*'); break;
8166 }
8167 if (*look == '*' || *look == '!')
8168 ++look;
8169
8170 /*
8171 * If there is a '0', only accept a match if the line is empty.
8172 * But may still match when typing last char of a word.
8173 */
8174 if (*look == '0')
8175 {
8176 try_match_word = try_match;
8177 if (!line_is_empty)
8178 try_match = FALSE;
8179 ++look;
8180 }
8181 else
8182 try_match_word = FALSE;
8183
8184 /*
8185 * does it look like a control character?
8186 */
8187 if (*look == '^'
8188#ifdef EBCDIC
8189 && (Ctrl_chr(look[1]) != 0)
8190#else
8191 && look[1] >= '?' && look[1] <= '_'
8192#endif
8193 )
8194 {
8195 if (try_match && keytyped == Ctrl_chr(look[1]))
8196 return TRUE;
8197 look += 2;
8198 }
8199 /*
8200 * 'o' means "o" command, open forward.
8201 * 'O' means "O" command, open backward.
8202 */
8203 else if (*look == 'o')
8204 {
8205 if (try_match && keytyped == KEY_OPEN_FORW)
8206 return TRUE;
8207 ++look;
8208 }
8209 else if (*look == 'O')
8210 {
8211 if (try_match && keytyped == KEY_OPEN_BACK)
8212 return TRUE;
8213 ++look;
8214 }
8215
8216 /*
8217 * 'e' means to check for "else" at start of line and just before the
8218 * cursor.
8219 */
8220 else if (*look == 'e')
8221 {
8222 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8223 {
8224 p = ml_get_curline();
8225 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8226 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8227 return TRUE;
8228 }
8229 ++look;
8230 }
8231
8232 /*
8233 * ':' only causes an indent if it is at the end of a label or case
8234 * statement, or when it was before typing the ':' (to fix
8235 * class::method for C++).
8236 */
8237 else if (*look == ':')
8238 {
8239 if (try_match && keytyped == ':')
8240 {
8241 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008242 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008244 /* Need to get the line again after cin_islabel(). */
8245 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 if (curwin->w_cursor.col > 2
8247 && p[curwin->w_cursor.col - 1] == ':'
8248 && p[curwin->w_cursor.col - 2] == ':')
8249 {
8250 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008251 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008252 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 p = ml_get_curline();
8254 p[curwin->w_cursor.col - 1] = ':';
8255 if (i)
8256 return TRUE;
8257 }
8258 }
8259 ++look;
8260 }
8261
8262
8263 /*
8264 * Is it a key in <>, maybe?
8265 */
8266 else if (*look == '<')
8267 {
8268 if (try_match)
8269 {
8270 /*
8271 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8272 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8273 * >, *, : and ! keys if they really really want to.
8274 */
8275 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8276 && keytyped == look[1])
8277 return TRUE;
8278
8279 if (keytyped == get_special_key_code(look + 1))
8280 return TRUE;
8281 }
8282 while (*look && *look != '>')
8283 look++;
8284 while (*look == '>')
8285 look++;
8286 }
8287
8288 /*
8289 * Is it a word: "=word"?
8290 */
8291 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8292 {
8293 ++look;
8294 if (*look == '~')
8295 {
8296 icase = TRUE;
8297 ++look;
8298 }
8299 else
8300 icase = FALSE;
8301 p = vim_strchr(look, ',');
8302 if (p == NULL)
8303 p = look + STRLEN(look);
8304 if ((try_match || try_match_word)
8305 && curwin->w_cursor.col >= (colnr_T)(p - look))
8306 {
8307 int match = FALSE;
8308
8309#ifdef FEAT_INS_EXPAND
8310 if (keytyped == KEY_COMPLETE)
8311 {
8312 char_u *s;
8313
8314 /* Just completed a word, check if it starts with "look".
8315 * search back for the start of a word. */
8316 line = ml_get_curline();
8317# ifdef FEAT_MBYTE
8318 if (has_mbyte)
8319 {
8320 char_u *n;
8321
8322 for (s = line + curwin->w_cursor.col; s > line; s = n)
8323 {
8324 n = mb_prevptr(line, s);
8325 if (!vim_iswordp(n))
8326 break;
8327 }
8328 }
8329 else
8330# endif
8331 for (s = line + curwin->w_cursor.col; s > line; --s)
8332 if (!vim_iswordc(s[-1]))
8333 break;
8334 if (s + (p - look) <= line + curwin->w_cursor.col
8335 && (icase
8336 ? MB_STRNICMP(s, look, p - look)
8337 : STRNCMP(s, look, p - look)) == 0)
8338 match = TRUE;
8339 }
8340 else
8341#endif
8342 /* TODO: multi-byte */
8343 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8344 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8345 {
8346 line = ml_get_cursor();
8347 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8348 || !vim_iswordc(line[-(p - look) - 1]))
8349 && (icase
8350 ? MB_STRNICMP(line - (p - look), look, p - look)
8351 : STRNCMP(line - (p - look), look, p - look))
8352 == 0)
8353 match = TRUE;
8354 }
8355 if (match && try_match_word && !try_match)
8356 {
8357 /* "0=word": Check if there are only blanks before the
8358 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008359 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 (int)(curwin->w_cursor.col - (p - look)))
8361 match = FALSE;
8362 }
8363 if (match)
8364 return TRUE;
8365 }
8366 look = p;
8367 }
8368
8369 /*
8370 * ok, it's a boring generic character.
8371 */
8372 else
8373 {
8374 if (try_match && *look == keytyped)
8375 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008376 if (*look != NUL)
8377 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 }
8379
8380 /*
8381 * Skip over ", ".
8382 */
8383 look = skip_to_option_part(look);
8384 }
8385 return FALSE;
8386}
8387#endif /* FEAT_CINDENT */
8388
8389#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8390/*
8391 * Map Hebrew keyboard when in hkmap mode.
8392 */
8393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008394hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395{
8396 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8397 {
8398 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8399 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8400 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8401 static char_u map[26] =
8402 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8403 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8404 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8405 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8406 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8407 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8408 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8409 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8410 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8411
8412 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8413 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8414 /* '-1'='sofit' */
8415 else if (c == 'x')
8416 return 'X';
8417 else if (c == 'q')
8418 return '\''; /* {geresh}={'} */
8419 else if (c == 246)
8420 return ' '; /* \"o --> ' ' for a german keyboard */
8421 else if (c == 228)
8422 return ' '; /* \"a --> ' ' -- / -- */
8423 else if (c == 252)
8424 return ' '; /* \"u --> ' ' -- / -- */
8425#ifdef EBCDIC
8426 else if (islower(c))
8427#else
8428 /* NOTE: islower() does not do the right thing for us on Linux so we
8429 * do this the same was as 5.7 and previous, so it works correctly on
8430 * all systems. Specifically, the e.g. Delete and Arrow keys are
8431 * munged and won't work if e.g. searching for Hebrew text.
8432 */
8433 else if (c >= 'a' && c <= 'z')
8434#endif
8435 return (int)(map[CharOrdLow(c)] + p_aleph);
8436 else
8437 return c;
8438 }
8439 else
8440 {
8441 switch (c)
8442 {
8443 case '`': return ';';
8444 case '/': return '.';
8445 case '\'': return ',';
8446 case 'q': return '/';
8447 case 'w': return '\'';
8448
8449 /* Hebrew letters - set offset from 'a' */
8450 case ',': c = '{'; break;
8451 case '.': c = 'v'; break;
8452 case ';': c = 't'; break;
8453 default: {
8454 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8455
8456#ifdef EBCDIC
8457 /* see note about islower() above */
8458 if (!islower(c))
8459#else
8460 if (c < 'a' || c > 'z')
8461#endif
8462 return c;
8463 c = str[CharOrdLow(c)];
8464 break;
8465 }
8466 }
8467
8468 return (int)(CharOrdLow(c) + p_aleph);
8469 }
8470}
8471#endif
8472
8473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008474ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
8476 int need_redraw = FALSE;
8477 int regname;
8478 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008479 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480
8481 /*
8482 * If we are going to wait for a character, show a '"'.
8483 */
8484 pc_status = PC_STATUS_UNSET;
8485 if (redrawing() && !char_avail())
8486 {
8487 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008488 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
8490 edit_putchar('"', TRUE);
8491#ifdef FEAT_CMDL_INFO
8492 add_to_showcmd_c(Ctrl_R);
8493#endif
8494 }
8495
8496#ifdef USE_ON_FLY_SCROLL
8497 dont_scroll = TRUE; /* disallow scrolling here */
8498#endif
8499
8500 /*
8501 * Don't map the register name. This also prevents the mode message to be
8502 * deleted when ESC is hit.
8503 */
8504 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008505 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8508 {
8509 /* Get a third key for literal register insertion */
8510 literally = regname;
8511#ifdef FEAT_CMDL_INFO
8512 add_to_showcmd_c(literally);
8513#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008514 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 }
8517 --no_mapping;
8518
8519#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008520 /* Don't call u_sync() while typing the expression or giving an error
8521 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 ++no_u_sync;
8523 if (regname == '=')
8524 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008525# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008527# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008528 /* Sync undo when evaluating the expression calls setline() or
8529 * append(), so that it can be undone separately. */
8530 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008531
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008533# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 /* Restore the Input Method. */
8535 if (im_on)
8536 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008539 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8540 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008541 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 else
8545 {
8546#endif
8547 if (literally == Ctrl_O || literally == Ctrl_P)
8548 {
8549 /* Append the command to the redo buffer. */
8550 AppendCharToRedobuff(Ctrl_R);
8551 AppendCharToRedobuff(literally);
8552 AppendCharToRedobuff(regname);
8553
8554 do_put(regname, BACKWARD, 1L,
8555 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8556 }
8557 else if (insert_reg(regname, literally) == FAIL)
8558 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008559 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 need_redraw = TRUE; /* remove the '"' */
8561 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008562 else if (stop_insert_mode)
8563 /* When the '=' register was used and a function was invoked that
8564 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8565 * insert anything, need to remove the '"' */
8566 need_redraw = TRUE;
8567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568#ifdef FEAT_EVAL
8569 }
8570 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008571 if (u_sync_once == 1)
8572 ins_need_undo = TRUE;
8573 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574#endif
8575#ifdef FEAT_CMDL_INFO
8576 clear_showcmd();
8577#endif
8578
8579 /* If the inserted register is empty, we need to remove the '"' */
8580 if (need_redraw || stuff_empty())
8581 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008582
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008583 /* Disallow starting Visual mode here, would get a weird mode. */
8584 if (!vis_active && VIsual_active)
8585 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586}
8587
8588/*
8589 * CTRL-G commands in Insert mode.
8590 */
8591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008592ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 int c;
8595
8596#ifdef FEAT_INS_EXPAND
8597 /* Right after CTRL-X the cursor will be after the ruler. */
8598 setcursor();
8599#endif
8600
8601 /*
8602 * Don't map the second key. This also prevents the mode message to be
8603 * deleted when ESC is hit.
8604 */
8605 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008606 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 --no_mapping;
8608 switch (c)
8609 {
8610 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8611 case K_UP:
8612 case Ctrl_K:
8613 case 'k': ins_up(TRUE);
8614 break;
8615
8616 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8617 case K_DOWN:
8618 case Ctrl_J:
8619 case 'j': ins_down(TRUE);
8620 break;
8621
8622 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008623 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008625
8626 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008627 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008628 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008629 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 break;
8631
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008632 /* CTRL-G U: do not break undo with the next char */
8633 case 'U':
8634 /* Allow one left/right cursor movement with the next char,
8635 * without breaking undo. */
8636 dont_sync_undo = MAYBE;
8637 break;
8638
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008640 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
8642}
8643
8644/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008645 * CTRL-^ in Insert mode.
8646 */
8647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008648ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008649{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008650 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008651 {
8652 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8653 if (State & LANGMAP)
8654 {
8655 curbuf->b_p_iminsert = B_IMODE_NONE;
8656 State &= ~LANGMAP;
8657 }
8658 else
8659 {
8660 curbuf->b_p_iminsert = B_IMODE_LMAP;
8661 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008662#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008663 im_set_active(FALSE);
8664#endif
8665 }
8666 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008667#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008668 else
8669 {
8670 /* There are no ":lmap" mappings, toggle IM */
8671 if (im_get_status())
8672 {
8673 curbuf->b_p_iminsert = B_IMODE_NONE;
8674 im_set_active(FALSE);
8675 }
8676 else
8677 {
8678 curbuf->b_p_iminsert = B_IMODE_IM;
8679 State &= ~LANGMAP;
8680 im_set_active(TRUE);
8681 }
8682 }
8683#endif
8684 set_iminsert_global();
8685 showmode();
8686#ifdef FEAT_GUI
8687 /* may show different cursor shape or color */
8688 if (gui.in_use)
8689 gui_update_cursor(TRUE, FALSE);
8690#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008691#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008692 /* Show/unshow value of 'keymap' in status lines. */
8693 status_redraw_curbuf();
8694#endif
8695}
8696
8697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 * Handle ESC in insert mode.
8699 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8700 * insert.
8701 */
8702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008703ins_esc(
8704 long *count,
8705 int cmdchar,
8706 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707{
8708 int temp;
8709 static int disabled_redraw = FALSE;
8710
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008711#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008712 check_spell_redraw();
8713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714#if defined(FEAT_HANGULIN)
8715# if defined(ESC_CHG_TO_ENG_MODE)
8716 hangul_input_state_set(0);
8717# endif
8718 if (composing_hangul)
8719 {
8720 push_raw_key(composing_hangul_buffer, 2);
8721 composing_hangul = 0;
8722 }
8723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 temp = curwin->w_cursor.col;
8726 if (disabled_redraw)
8727 {
8728 --RedrawingDisabled;
8729 disabled_redraw = FALSE;
8730 }
8731 if (!arrow_used)
8732 {
8733 /*
8734 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008735 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8736 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 */
8738 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008739 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
8741 /*
8742 * Repeating insert may take a long time. Check for
8743 * interrupt now and then.
8744 */
8745 if (*count > 0)
8746 {
8747 line_breakcheck();
8748 if (got_int)
8749 *count = 0;
8750 }
8751
8752 if (--*count > 0) /* repeat what was typed */
8753 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008754 /* Vi repeats the insert without replacing characters. */
8755 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8756 State &= ~REPLACE_FLAG;
8757
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 (void)start_redo_ins();
8759 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008760 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 ++RedrawingDisabled;
8762 disabled_redraw = TRUE;
8763 return FALSE; /* repeat the insert */
8764 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008765 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 undisplay_dollar();
8767 }
8768
8769 /* When an autoindent was removed, curswant stays after the
8770 * indent */
8771 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8772 curwin->w_set_curswant = TRUE;
8773
8774 /* Remember the last Insert position in the '^ mark. */
8775 if (!cmdmod.keepjumps)
8776 curbuf->b_last_insert = curwin->w_cursor;
8777
8778 /*
8779 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008780 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008782 if (!nomove
8783 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784#ifdef FEAT_VIRTUALEDIT
8785 || curwin->w_cursor.coladd > 0
8786#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008787 )
8788 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008789 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790#ifdef FEAT_RIGHTLEFT
8791 && !revins_on
8792#endif
8793 )
8794 {
8795#ifdef FEAT_VIRTUALEDIT
8796 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8797 {
8798 oneleft();
8799 if (restart_edit != NUL)
8800 ++curwin->w_cursor.coladd;
8801 }
8802 else
8803#endif
8804 {
8805 --curwin->w_cursor.col;
8806#ifdef FEAT_MBYTE
8807 /* Correct cursor for multi-byte character. */
8808 if (has_mbyte)
8809 mb_adjust_cursor();
8810#endif
8811 }
8812 }
8813
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008814#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 /* Disable IM to allow typing English directly for Normal mode commands.
8816 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8817 * well). */
8818 if (!(State & LANGMAP))
8819 im_save_status(&curbuf->b_p_iminsert);
8820 im_set_active(FALSE);
8821#endif
8822
8823 State = NORMAL;
8824 /* need to position cursor again (e.g. when on a TAB ) */
8825 changed_cline_bef_curs();
8826
8827#ifdef FEAT_MOUSE
8828 setmouse();
8829#endif
8830#ifdef CURSOR_SHAPE
8831 ui_cursor_shape(); /* may show different cursor shape */
8832#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008833 if (!p_ek)
8834 /* Re-enable bracketed paste mode. */
8835 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
8837 /*
8838 * When recording or for CTRL-O, need to display the new mode.
8839 * Otherwise remove the mode message.
8840 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008841 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 showmode();
8843 else if (p_smd)
8844 MSG("");
8845
8846 return TRUE; /* exit Insert mode */
8847}
8848
8849#ifdef FEAT_RIGHTLEFT
8850/*
8851 * Toggle language: hkmap and revins_on.
8852 * Move to end of reverse inserted text.
8853 */
8854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008855ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857 if (revins_on && revins_chars && revins_scol >= 0)
8858 {
8859 while (gchar_cursor() != NUL && revins_chars--)
8860 ++curwin->w_cursor.col;
8861 }
8862 p_ri = !p_ri;
8863 revins_on = (State == INSERT && p_ri);
8864 if (revins_on)
8865 {
8866 revins_scol = curwin->w_cursor.col;
8867 revins_legal++;
8868 revins_chars = 0;
8869 undisplay_dollar();
8870 }
8871 else
8872 revins_scol = -1;
8873#ifdef FEAT_FKMAP
8874 if (p_altkeymap)
8875 {
8876 /*
8877 * to be consistent also for redo command, using '.'
8878 * set arrow_used to true and stop it - causing to redo
8879 * characters entered in one mode (normal/reverse insert).
8880 */
8881 arrow_used = TRUE;
8882 (void)stop_arrow();
8883 p_fkmap = curwin->w_p_rl ^ p_ri;
8884 if (p_fkmap && p_ri)
8885 State = INSERT;
8886 }
8887 else
8888#endif
8889 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8890 showmode();
8891}
8892#endif
8893
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894/*
8895 * If 'keymodel' contains "startsel", may start selection.
8896 * Returns TRUE when a CTRL-O and other keys stuffed.
8897 */
8898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008899ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
8901 if (km_startsel)
8902 switch (c)
8903 {
8904 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 case K_PAGEUP:
8907 case K_KPAGEUP:
8908 case K_PAGEDOWN:
8909 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008910# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 case K_LEFT:
8912 case K_RIGHT:
8913 case K_UP:
8914 case K_DOWN:
8915 case K_END:
8916 case K_HOME:
8917# endif
8918 if (!(mod_mask & MOD_MASK_SHIFT))
8919 break;
8920 /* FALLTHROUGH */
8921 case K_S_LEFT:
8922 case K_S_RIGHT:
8923 case K_S_UP:
8924 case K_S_DOWN:
8925 case K_S_END:
8926 case K_S_HOME:
8927 /* Start selection right away, the cursor can move with
8928 * CTRL-O when beyond the end of the line. */
8929 start_selection();
8930
8931 /* Execute the key in (insert) Select mode. */
8932 stuffcharReadbuff(Ctrl_O);
8933 if (mod_mask)
8934 {
8935 char_u buf[4];
8936
8937 buf[0] = K_SPECIAL;
8938 buf[1] = KS_MODIFIER;
8939 buf[2] = mod_mask;
8940 buf[3] = NUL;
8941 stuffReadbuff(buf);
8942 }
8943 stuffcharReadbuff(c);
8944 return TRUE;
8945 }
8946 return FALSE;
8947}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
8949/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008950 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008951 */
8952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008953ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008954{
8955#ifdef FEAT_FKMAP
8956 if (p_fkmap && p_ri)
8957 {
8958 beep_flush();
8959 EMSG(farsi_text_3); /* encoded in Farsi */
8960 return;
8961 }
8962#endif
8963
Bram Moolenaar1e015462005-09-25 22:16:38 +00008964# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008965 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008966 (char_u *)((State & REPLACE_FLAG) ? "i"
8967 : replaceState == VREPLACE ? "v"
8968 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008969# endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +02008970 ins_apply_autocmds(EVENT_INSERTCHANGE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008971 if (State & REPLACE_FLAG)
8972 State = INSERT | (State & LANGMAP);
8973 else
8974 State = replaceState | (State & LANGMAP);
8975 AppendCharToRedobuff(K_INS);
8976 showmode();
8977#ifdef CURSOR_SHAPE
8978 ui_cursor_shape(); /* may show different cursor shape */
8979#endif
8980}
8981
8982/*
8983 * Pressed CTRL-O in Insert mode.
8984 */
8985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008986ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008987{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008988 if (State & VREPLACE_FLAG)
8989 restart_edit = 'V';
8990 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008991 if (State & REPLACE_FLAG)
8992 restart_edit = 'R';
8993 else
8994 restart_edit = 'I';
8995#ifdef FEAT_VIRTUALEDIT
8996 if (virtual_active())
8997 ins_at_eol = FALSE; /* cursor always keeps its column */
8998 else
8999#endif
9000 ins_at_eol = (gchar_cursor() == NUL);
9001}
9002
9003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 * If the cursor is on an indent, ^T/^D insert/delete one
9005 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00009006 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 * with vi. But vi only supports ^T and ^D after an
9008 * autoindent, we support it everywhere.
9009 */
9010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009011ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
9013 if (stop_arrow() == FAIL)
9014 return;
9015 AppendCharToRedobuff(c);
9016
9017 /*
9018 * 0^D and ^^D: remove all indent.
9019 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009020 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9021 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 {
9023 --curwin->w_cursor.col;
9024 (void)del_char(FALSE); /* delete the '^' or '0' */
9025 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9026 if (State & REPLACE_FLAG)
9027 replace_pop_ins();
9028 if (lastc == '^')
9029 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009030 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 }
9032 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009033 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034
9035 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9036 did_ai = FALSE;
9037#ifdef FEAT_SMARTINDENT
9038 did_si = FALSE;
9039 can_si = FALSE;
9040 can_si_back = FALSE;
9041#endif
9042#ifdef FEAT_CINDENT
9043 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9044#endif
9045}
9046
9047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009048ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
9050 int temp;
9051
9052 if (stop_arrow() == FAIL)
9053 return;
9054 if (gchar_cursor() == NUL) /* delete newline */
9055 {
9056 temp = curwin->w_cursor.col;
9057 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009058 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009059 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009063 /* Adjust orig_line_count in case more lines have been deleted than
9064 * have been added. That makes sure, that open_line() later
9065 * can access all buffer lines correctly */
9066 if (State & VREPLACE_FLAG &&
9067 orig_line_count > curbuf->b_ml.ml_line_count)
9068 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009071 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9072 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 did_ai = FALSE;
9074#ifdef FEAT_SMARTINDENT
9075 did_si = FALSE;
9076 can_si = FALSE;
9077 can_si_back = FALSE;
9078#endif
9079 AppendCharToRedobuff(K_DEL);
9080}
9081
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009082/*
9083 * Delete one character for ins_bs().
9084 */
9085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009086ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009087{
9088 dec_cursor();
9089 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9090 if (State & REPLACE_FLAG)
9091 {
9092 /* Don't delete characters before the insert point when in
9093 * Replace mode */
9094 if (curwin->w_cursor.lnum != Insstart.lnum
9095 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009096 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009097 }
9098 else
9099 (void)del_char(FALSE);
9100}
9101
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102/*
9103 * Handle Backspace, delete-word and delete-line in Insert mode.
9104 * Return TRUE when backspace was actually used.
9105 */
9106 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009107ins_bs(
9108 int c,
9109 int mode,
9110 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
9112 linenr_T lnum;
9113 int cc;
9114 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009115 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 colnr_T mincol;
9117 int did_backspace = FALSE;
9118 int in_indent;
9119 int oldState;
9120#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009121 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122#endif
9123
9124 /*
9125 * can't delete anything in an empty file
9126 * can't backup past first character in buffer
9127 * can't backup past starting point unless 'backspace' > 1
9128 * can backup to a previous line if 'backspace' == 0
9129 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009130 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 || (
9132#ifdef FEAT_RIGHTLEFT
9133 !revins_on &&
9134#endif
9135 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9136 || (!can_bs(BS_START)
9137 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009138 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9139 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9141 && curwin->w_cursor.col <= ai_col)
9142 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9143 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009144 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 return FALSE;
9146 }
9147
9148 if (stop_arrow() == FAIL)
9149 return FALSE;
9150 in_indent = inindent(0);
9151#ifdef FEAT_CINDENT
9152 if (in_indent)
9153 can_cindent = FALSE;
9154#endif
9155#ifdef FEAT_COMMENTS
9156 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9157#endif
9158#ifdef FEAT_RIGHTLEFT
9159 if (revins_on) /* put cursor after last inserted char */
9160 inc_cursor();
9161#endif
9162
9163#ifdef FEAT_VIRTUALEDIT
9164 /* Virtualedit:
9165 * BACKSPACE_CHAR eats a virtual space
9166 * BACKSPACE_WORD eats all coladd
9167 * BACKSPACE_LINE eats all coladd and keeps going
9168 */
9169 if (curwin->w_cursor.coladd > 0)
9170 {
9171 if (mode == BACKSPACE_CHAR)
9172 {
9173 --curwin->w_cursor.coladd;
9174 return TRUE;
9175 }
9176 if (mode == BACKSPACE_WORD)
9177 {
9178 curwin->w_cursor.coladd = 0;
9179 return TRUE;
9180 }
9181 curwin->w_cursor.coladd = 0;
9182 }
9183#endif
9184
9185 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009186 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 */
9188 if (curwin->w_cursor.col == 0)
9189 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009190 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009191 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192#ifdef FEAT_RIGHTLEFT
9193 || revins_on
9194#endif
9195 )
9196 {
9197 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9198 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9199 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009200 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009201 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 }
9203 /*
9204 * In replace mode:
9205 * cc < 0: NL was inserted, delete it
9206 * cc >= 0: NL was replaced, put original characters back
9207 */
9208 cc = -1;
9209 if (State & REPLACE_FLAG)
9210 cc = replace_pop(); /* returns -1 if NL was inserted */
9211 /*
9212 * In replace mode, in the line we started replacing, we only move the
9213 * cursor.
9214 */
9215 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9216 {
9217 dec_cursor();
9218 }
9219 else
9220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 if (!(State & VREPLACE_FLAG)
9222 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 {
9224 temp = gchar_cursor(); /* remember current char */
9225 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009226
9227 /* When "aw" is in 'formatoptions' we must delete the space at
9228 * the end of the line, otherwise the line will be broken
9229 * again when auto-formatting. */
9230 if (has_format_option(FO_AUTO)
9231 && has_format_option(FO_WHITE_PAR))
9232 {
9233 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9234 TRUE);
9235 int len;
9236
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009237 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009238 if (len > 0 && ptr[len - 1] == ' ')
9239 ptr[len - 1] = NUL;
9240 }
9241
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009242 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 if (temp == NUL && gchar_cursor() != NUL)
9244 inc_cursor();
9245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 else
9247 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248
9249 /*
9250 * In REPLACE mode we have to put back the text that was replaced
9251 * by the NL. On the replace stack is first a NUL-terminated
9252 * sequence of characters that were deleted and then the
9253 * characters that NL replaced.
9254 */
9255 if (State & REPLACE_FLAG)
9256 {
9257 /*
9258 * Do the next ins_char() in NORMAL state, to
9259 * prevent ins_char() from replacing characters and
9260 * avoiding showmatch().
9261 */
9262 oldState = State;
9263 State = NORMAL;
9264 /*
9265 * restore characters (blanks) deleted after cursor
9266 */
9267 while (cc > 0)
9268 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009269 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270#ifdef FEAT_MBYTE
9271 mb_replace_pop_ins(cc);
9272#else
9273 ins_char(cc);
9274#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009275 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 cc = replace_pop();
9277 }
9278 /* restore the characters that NL replaced */
9279 replace_pop_ins();
9280 State = oldState;
9281 }
9282 }
9283 did_ai = FALSE;
9284 }
9285 else
9286 {
9287 /*
9288 * Delete character(s) before the cursor.
9289 */
9290#ifdef FEAT_RIGHTLEFT
9291 if (revins_on) /* put cursor on last inserted char */
9292 dec_cursor();
9293#endif
9294 mincol = 0;
9295 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009296 if (mode == BACKSPACE_LINE
9297 && (curbuf->b_p_ai
9298#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009299 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009300#endif
9301 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302#ifdef FEAT_RIGHTLEFT
9303 && !revins_on
9304#endif
9305 )
9306 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009307 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009309 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009311 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 }
9313
9314 /*
9315 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9316 */
9317 if ( mode == BACKSPACE_CHAR
9318 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009319 || ((get_sts_value() != 0
9320#ifdef FEAT_VARTABS
9321 || tabstop_count(curbuf->b_p_vsts_array)
9322#endif
9323 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009324 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 && (*(ml_get_cursor() - 1) == TAB
9326 || (*(ml_get_cursor() - 1) == ' '
9327 && (!*inserted_space_p
9328 || arrow_used))))))
9329 {
9330 int ts;
9331 colnr_T vcol;
9332 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009333 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
9335 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 /* Compute the virtual column where we want to be. Since
9337 * 'showbreak' may get in the way, need to get the last column of
9338 * the previous character. */
9339 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009340 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 dec_cursor();
9342 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9343 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009344#ifdef FEAT_VARTABS
9345 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009346 {
9347 ts = (int)get_sw_value(curbuf);
9348 want_vcol = (want_vcol / ts) * ts;
9349 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009350 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009351 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009352 curbuf->b_p_vsts_array);
9353#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009354 if (p_sta && in_indent)
9355 ts = (int)get_sw_value(curbuf);
9356 else
9357 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
9361 /* delete characters until we are at or before want_vcol */
9362 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009363 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009364 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365
9366 /* insert extra spaces until we are at want_vcol */
9367 while (vcol < want_vcol)
9368 {
9369 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009370 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9371 && curwin->w_cursor.col < Insstart_orig.col)
9372 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 if (State & VREPLACE_FLAG)
9375 ins_char(' ');
9376 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 {
9378 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009379 if ((State & REPLACE_FLAG))
9380 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 }
9382 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9383 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009384
9385 /* If we are now back where we started delete one character. Can
9386 * happen when using 'sts' and 'linebreak'. */
9387 if (vcol >= start_vcol)
9388 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 }
9390
9391 /*
9392 * Delete upto starting point, start of line or previous word.
9393 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009394 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009396#ifdef FEAT_MBYTE
9397 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009399 if (has_mbyte)
9400 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009402 do
9403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009405 if (!revins_on) /* put cursor on char to be deleted */
9406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009408
9409 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009411 /* look multi-byte character class */
9412 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009414 prev_cclass = cclass;
9415 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009418
9419 /* start of word? */
9420 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9421 {
9422 mode = BACKSPACE_WORD_NOT_SPACE;
9423 temp = vim_iswordc(cc);
9424 }
9425 /* end of word? */
9426 else if (mode == BACKSPACE_WORD_NOT_SPACE
9427 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9428#ifdef FEAT_MBYTE
9429 || prev_cclass != cclass
9430#endif
9431 ))
9432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009434 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009436 inc_cursor();
9437#ifdef FEAT_RIGHTLEFT
9438 else if (State & REPLACE_FLAG)
9439 dec_cursor();
9440#endif
9441 break;
9442 }
9443 if (State & REPLACE_FLAG)
9444 replace_do_bs(-1);
9445 else
9446 {
9447#ifdef FEAT_MBYTE
9448 if (enc_utf8 && p_deco)
9449 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9450#endif
9451 (void)del_char(FALSE);
9452#ifdef FEAT_MBYTE
9453 /*
9454 * If there are combining characters and 'delcombine' is set
9455 * move the cursor back. Don't back up before the base
9456 * character.
9457 */
9458 if (enc_utf8 && p_deco && cpc[0] != NUL)
9459 inc_cursor();
9460#endif
9461#ifdef FEAT_RIGHTLEFT
9462 if (revins_chars)
9463 {
9464 revins_chars--;
9465 revins_legal++;
9466 }
9467 if (revins_on && gchar_cursor() == NUL)
9468 break;
9469#endif
9470 }
9471 /* Just a single backspace?: */
9472 if (mode == BACKSPACE_CHAR)
9473 break;
9474 } while (
9475#ifdef FEAT_RIGHTLEFT
9476 revins_on ||
9477#endif
9478 (curwin->w_cursor.col > mincol
9479 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9480 || curwin->w_cursor.col != Insstart_orig.col)));
9481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 did_backspace = TRUE;
9483 }
9484#ifdef FEAT_SMARTINDENT
9485 did_si = FALSE;
9486 can_si = FALSE;
9487 can_si_back = FALSE;
9488#endif
9489 if (curwin->w_cursor.col <= 1)
9490 did_ai = FALSE;
9491 /*
9492 * It's a little strange to put backspaces into the redo
9493 * buffer, but it makes auto-indent a lot easier to deal
9494 * with.
9495 */
9496 AppendCharToRedobuff(c);
9497
9498 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009499 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009500 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009501 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502
9503 /* vi behaviour: the cursor moves backward but the character that
9504 * was there remains visible
9505 * Vim behaviour: the cursor moves backward and the character that
9506 * was there is erased from the screen.
9507 * We can emulate the vi behaviour by pretending there is a dollar
9508 * displayed even when there isn't.
9509 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009510 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 dollar_vcol = curwin->w_virtcol;
9512
Bram Moolenaarce3be472008-01-14 19:12:28 +00009513#ifdef FEAT_FOLDING
9514 /* When deleting a char the cursor line must never be in a closed fold.
9515 * E.g., when 'foldmethod' is indent and deleting the first non-white
9516 * char before a Tab. */
9517 if (did_backspace)
9518 foldOpenCursor();
9519#endif
9520
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 return did_backspace;
9522}
9523
9524#ifdef FEAT_MOUSE
9525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009526ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
9528 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009529 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530
9531# ifdef FEAT_GUI
9532 /* When GUI is active, also move/paste when 'mouse' is empty */
9533 if (!gui.in_use)
9534# endif
9535 if (!mouse_has(MOUSE_INSERT))
9536 return;
9537
9538 undisplay_dollar();
9539 tpos = curwin->w_cursor;
9540 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9541 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009542 win_T *new_curwin = curwin;
9543
9544 if (curwin != old_curwin && win_valid(old_curwin))
9545 {
9546 /* Mouse took us to another window. We need to go back to the
9547 * previous one to stop insert there properly. */
9548 curwin = old_curwin;
9549 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009550#ifdef FEAT_JOB_CHANNEL
9551 if (bt_prompt(curbuf))
9552 // Restart Insert mode when re-entering the prompt buffer.
9553 curbuf->b_prompt_insert = 'A';
9554#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009555 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009556 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009557 if (curwin != new_curwin && win_valid(new_curwin))
9558 {
9559 curwin = new_curwin;
9560 curbuf = curwin->w_buffer;
9561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562# ifdef FEAT_CINDENT
9563 can_cindent = TRUE;
9564# endif
9565 }
9566
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 /* redraw status lines (in case another window became active) */
9568 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009572ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
9574 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009575 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009576# ifdef FEAT_INS_EXPAND
9577 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578# endif
9579
9580 tpos = curwin->w_cursor;
9581
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009582 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
9584 int row, col;
9585
9586 row = mouse_row;
9587 col = mouse_col;
9588
9589 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009590 wp = mouse_find_win(&row, &col);
9591 if (wp == NULL)
9592 return;
9593 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 curbuf = curwin->w_buffer;
9595 }
9596 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 undisplay_dollar();
9598
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009599# ifdef FEAT_INS_EXPAND
9600 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009601 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009602# endif
9603 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009604 if (dir == MSCR_DOWN || dir == MSCR_UP)
9605 {
9606 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9607 scroll_redraw(dir,
9608 (long)(curwin->w_botline - curwin->w_topline));
9609 else
9610 scroll_redraw(dir, 3L);
9611 }
9612#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009613 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009614 {
9615 int val, step = 6;
9616
9617 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009618 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009619 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9620 if (val < 0)
9621 val = 0;
9622 gui_do_horiz_scroll(val, TRUE);
9623 }
9624#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009625# ifdef FEAT_INS_EXPAND
9626 did_scroll = TRUE;
9627# endif
9628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 curwin->w_redr_status = TRUE;
9631
9632 curwin = old_curwin;
9633 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009635# ifdef FEAT_INS_EXPAND
9636 /* The popup menu may overlay the window, need to redraw it.
9637 * TODO: Would be more efficient to only redraw the windows that are
9638 * overlapped by the popup menu. */
9639 if (pum_visible() && did_scroll)
9640 {
9641 redraw_all_later(NOT_VALID);
9642 ins_compl_show_pum();
9643 }
9644# endif
9645
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009646 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 {
9648 start_arrow(&tpos);
9649# ifdef FEAT_CINDENT
9650 can_cindent = TRUE;
9651# endif
9652 }
9653}
9654#endif
9655
Bram Moolenaarec2da362017-01-21 20:04:22 +01009656/*
9657 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9658 * P_PE literally.
9659 * When "drop" is TRUE then consume the text and drop it.
9660 */
9661 int
9662bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9663{
9664 int c;
9665 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9666 int idx = 0;
9667 char_u *end = find_termcode((char_u *)"PE");
9668 int ret_char = -1;
9669 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009670 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009671
9672 /* If the end code is too long we can't detect it, read everything. */
9673 if (STRLEN(end) >= NUMBUFLEN)
9674 end = NULL;
9675 ++no_mapping;
9676 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009677 if (!p_paste)
9678 // Also have the side effects of setting 'paste' to make it work much
9679 // faster.
9680 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009681
Bram Moolenaarec2da362017-01-21 20:04:22 +01009682 for (;;)
9683 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009684 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009685 if (end == NULL && vpeekc() == NUL)
9686 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009687 do
9688 {
9689 c = vgetc();
9690 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9691 if (c == NUL || got_int)
9692 // When CTRL-C was encountered the typeahead will be flushed and we
9693 // won't get the end sequence.
9694 break;
9695
Bram Moolenaarec2da362017-01-21 20:04:22 +01009696#ifdef FEAT_MBYTE
9697 if (has_mbyte)
9698 idx += (*mb_char2bytes)(c, buf + idx);
9699 else
9700#endif
9701 buf[idx++] = c;
9702 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009703 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009704 {
9705 if (end[idx] == NUL)
9706 break; /* Found the end of paste code. */
9707 continue;
9708 }
9709 if (!drop)
9710 {
9711 switch (mode)
9712 {
9713 case PASTE_CMDLINE:
9714 put_on_cmdline(buf, idx, TRUE);
9715 break;
9716
9717 case PASTE_EX:
9718 if (gap != NULL && ga_grow(gap, idx) == OK)
9719 {
9720 mch_memmove((char *)gap->ga_data + gap->ga_len,
9721 buf, (size_t)idx);
9722 gap->ga_len += idx;
9723 }
9724 break;
9725
9726 case PASTE_INSERT:
9727 if (stop_arrow() == OK)
9728 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009729 c = buf[0];
9730 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9731 ins_eol(c);
9732 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009733 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009734 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009735 AppendToRedobuffLit(buf, idx);
9736 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009737 }
9738 break;
9739
9740 case PASTE_ONE_CHAR:
9741 if (ret_char == -1)
9742 {
9743#ifdef FEAT_MBYTE
9744 if (has_mbyte)
9745 ret_char = (*mb_ptr2char)(buf);
9746 else
9747#endif
9748 ret_char = buf[0];
9749 }
9750 break;
9751 }
9752 }
9753 idx = 0;
9754 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009755
Bram Moolenaarec2da362017-01-21 20:04:22 +01009756 --no_mapping;
9757 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009758 if (!save_paste)
9759 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009760
9761 return ret_char;
9762}
9763
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009764#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009766ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009767{
9768 /* We will be leaving the current window, unless closing another tab. */
9769 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9770 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9771 {
9772 undisplay_dollar();
9773 start_arrow(&curwin->w_cursor);
9774# ifdef FEAT_CINDENT
9775 can_cindent = TRUE;
9776# endif
9777 }
9778
9779 if (c == K_TABLINE)
9780 goto_tabpage(current_tab);
9781 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009782 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009783 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009784 redraw_statuslines(); /* will redraw the tabline when needed */
9785 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009786}
9787#endif
9788
9789#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009791ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
9793 pos_T tpos;
9794
9795 undisplay_dollar();
9796 tpos = curwin->w_cursor;
9797 if (gui_do_scroll())
9798 {
9799 start_arrow(&tpos);
9800# ifdef FEAT_CINDENT
9801 can_cindent = TRUE;
9802# endif
9803 }
9804}
9805
9806 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009807ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
9809 pos_T tpos;
9810
9811 undisplay_dollar();
9812 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009813 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 {
9815 start_arrow(&tpos);
9816# ifdef FEAT_CINDENT
9817 can_cindent = TRUE;
9818# endif
9819 }
9820}
9821#endif
9822
9823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009824ins_left(
9825 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826{
9827 pos_T tpos;
9828
9829#ifdef FEAT_FOLDING
9830 if ((fdo_flags & FDO_HOR) && KeyTyped)
9831 foldOpenCursor();
9832#endif
9833 undisplay_dollar();
9834 tpos = curwin->w_cursor;
9835 if (oneleft() == OK)
9836 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009837#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9838 /* Only call start_arrow() when not busy with preediting, it will
9839 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009840 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009841#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009842 {
9843 start_arrow_with_change(&tpos, end_change);
9844 if (!end_change)
9845 AppendCharToRedobuff(K_LEFT);
9846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847#ifdef FEAT_RIGHTLEFT
9848 /* If exit reversed string, position is fixed */
9849 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9850 revins_legal++;
9851 revins_chars++;
9852#endif
9853 }
9854
9855 /*
9856 * if 'whichwrap' set for cursor in insert mode may go to
9857 * previous line
9858 */
9859 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9860 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009861 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 start_arrow(&tpos);
9863 --(curwin->w_cursor.lnum);
9864 coladvance((colnr_T)MAXCOL);
9865 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9866 }
9867 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009868 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009869 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870}
9871
9872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009873ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874{
9875 pos_T tpos;
9876
9877#ifdef FEAT_FOLDING
9878 if ((fdo_flags & FDO_HOR) && KeyTyped)
9879 foldOpenCursor();
9880#endif
9881 undisplay_dollar();
9882 tpos = curwin->w_cursor;
9883 if (c == K_C_HOME)
9884 curwin->w_cursor.lnum = 1;
9885 curwin->w_cursor.col = 0;
9886#ifdef FEAT_VIRTUALEDIT
9887 curwin->w_cursor.coladd = 0;
9888#endif
9889 curwin->w_curswant = 0;
9890 start_arrow(&tpos);
9891}
9892
9893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009894ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896 pos_T tpos;
9897
9898#ifdef FEAT_FOLDING
9899 if ((fdo_flags & FDO_HOR) && KeyTyped)
9900 foldOpenCursor();
9901#endif
9902 undisplay_dollar();
9903 tpos = curwin->w_cursor;
9904 if (c == K_C_END)
9905 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9906 coladvance((colnr_T)MAXCOL);
9907 curwin->w_curswant = MAXCOL;
9908
9909 start_arrow(&tpos);
9910}
9911
9912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009913ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914{
9915#ifdef FEAT_FOLDING
9916 if ((fdo_flags & FDO_HOR) && KeyTyped)
9917 foldOpenCursor();
9918#endif
9919 undisplay_dollar();
9920 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9921 {
9922 start_arrow(&curwin->w_cursor);
9923 (void)bck_word(1L, FALSE, FALSE);
9924 curwin->w_set_curswant = TRUE;
9925 }
9926 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009927 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928}
9929
9930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009931ins_right(
9932 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933{
9934#ifdef FEAT_FOLDING
9935 if ((fdo_flags & FDO_HOR) && KeyTyped)
9936 foldOpenCursor();
9937#endif
9938 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009939 if (gchar_cursor() != NUL
9940#ifdef FEAT_VIRTUALEDIT
9941 || virtual_active()
9942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 )
9944 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009945 start_arrow_with_change(&curwin->w_cursor, end_change);
9946 if (!end_change)
9947 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 curwin->w_set_curswant = TRUE;
9949#ifdef FEAT_VIRTUALEDIT
9950 if (virtual_active())
9951 oneright();
9952 else
9953#endif
9954 {
9955#ifdef FEAT_MBYTE
9956 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009957 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 else
9959#endif
9960 ++curwin->w_cursor.col;
9961 }
9962
9963#ifdef FEAT_RIGHTLEFT
9964 revins_legal++;
9965 if (revins_chars)
9966 revins_chars--;
9967#endif
9968 }
9969 /* if 'whichwrap' set for cursor in insert mode, may move the
9970 * cursor to the next line */
9971 else if (vim_strchr(p_ww, ']') != NULL
9972 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9973 {
9974 start_arrow(&curwin->w_cursor);
9975 curwin->w_set_curswant = TRUE;
9976 ++curwin->w_cursor.lnum;
9977 curwin->w_cursor.col = 0;
9978 }
9979 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009980 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009981 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982}
9983
9984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009985ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
9987#ifdef FEAT_FOLDING
9988 if ((fdo_flags & FDO_HOR) && KeyTyped)
9989 foldOpenCursor();
9990#endif
9991 undisplay_dollar();
9992 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9993 || gchar_cursor() != NUL)
9994 {
9995 start_arrow(&curwin->w_cursor);
9996 (void)fwd_word(1L, FALSE, 0);
9997 curwin->w_set_curswant = TRUE;
9998 }
9999 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010000 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001}
10002
10003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010004ins_up(
10005 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 pos_T tpos;
10008 linenr_T old_topline = curwin->w_topline;
10009#ifdef FEAT_DIFF
10010 int old_topfill = curwin->w_topfill;
10011#endif
10012
10013 undisplay_dollar();
10014 tpos = curwin->w_cursor;
10015 if (cursor_up(1L, TRUE) == OK)
10016 {
10017 if (startcol)
10018 coladvance(getvcol_nolist(&Insstart));
10019 if (old_topline != curwin->w_topline
10020#ifdef FEAT_DIFF
10021 || old_topfill != curwin->w_topfill
10022#endif
10023 )
10024 redraw_later(VALID);
10025 start_arrow(&tpos);
10026#ifdef FEAT_CINDENT
10027 can_cindent = TRUE;
10028#endif
10029 }
10030 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010031 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032}
10033
10034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010035ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037 pos_T tpos;
10038
10039 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010040
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010041 if (mod_mask & MOD_MASK_CTRL)
10042 {
10043 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010044 if (first_tabpage->tp_next != NULL)
10045 {
10046 start_arrow(&curwin->w_cursor);
10047 goto_tabpage(-1);
10048 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010049 return;
10050 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010051
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 tpos = curwin->w_cursor;
10053 if (onepage(BACKWARD, 1L) == OK)
10054 {
10055 start_arrow(&tpos);
10056#ifdef FEAT_CINDENT
10057 can_cindent = TRUE;
10058#endif
10059 }
10060 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010061 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062}
10063
10064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010065ins_down(
10066 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067{
10068 pos_T tpos;
10069 linenr_T old_topline = curwin->w_topline;
10070#ifdef FEAT_DIFF
10071 int old_topfill = curwin->w_topfill;
10072#endif
10073
10074 undisplay_dollar();
10075 tpos = curwin->w_cursor;
10076 if (cursor_down(1L, TRUE) == OK)
10077 {
10078 if (startcol)
10079 coladvance(getvcol_nolist(&Insstart));
10080 if (old_topline != curwin->w_topline
10081#ifdef FEAT_DIFF
10082 || old_topfill != curwin->w_topfill
10083#endif
10084 )
10085 redraw_later(VALID);
10086 start_arrow(&tpos);
10087#ifdef FEAT_CINDENT
10088 can_cindent = TRUE;
10089#endif
10090 }
10091 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010092 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093}
10094
10095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010096ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
10098 pos_T tpos;
10099
10100 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010101
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010102 if (mod_mask & MOD_MASK_CTRL)
10103 {
10104 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010105 if (first_tabpage->tp_next != NULL)
10106 {
10107 start_arrow(&curwin->w_cursor);
10108 goto_tabpage(0);
10109 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010110 return;
10111 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010112
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 tpos = curwin->w_cursor;
10114 if (onepage(FORWARD, 1L) == OK)
10115 {
10116 start_arrow(&tpos);
10117#ifdef FEAT_CINDENT
10118 can_cindent = TRUE;
10119#endif
10120 }
10121 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010122 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123}
10124
10125#ifdef FEAT_DND
10126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010127ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128{
10129 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10130}
10131#endif
10132
10133/*
10134 * Handle TAB in Insert or Replace mode.
10135 * Return TRUE when the TAB needs to be inserted like a normal character.
10136 */
10137 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010138ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
10140 int ind;
10141 int i;
10142 int temp;
10143
10144 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10145 Insstart_blank_vcol = get_nolist_virtcol();
10146 if (echeck_abbr(TAB + ABBR_OFF))
10147 return FALSE;
10148
10149 ind = inindent(0);
10150#ifdef FEAT_CINDENT
10151 if (ind)
10152 can_cindent = FALSE;
10153#endif
10154
10155 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010156 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 */
10158 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010159#ifdef FEAT_VARTABS
10160 && !(p_sta && ind
10161 /* These five lines mean 'tabstop' != 'shiftwidth' */
10162 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10163 || (tabstop_count(curbuf->b_p_vts_array) == 1
10164 && tabstop_first(curbuf->b_p_vts_array)
10165 != get_sw_value(curbuf))
10166 || (tabstop_count(curbuf->b_p_vts_array) == 0
10167 && curbuf->b_p_ts != get_sw_value(curbuf))))
10168 && tabstop_count(curbuf->b_p_vsts_array) == 0
10169#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010170 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010171#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010172 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 return TRUE;
10174
10175 if (stop_arrow() == FAIL)
10176 return TRUE;
10177
10178 did_ai = FALSE;
10179#ifdef FEAT_SMARTINDENT
10180 did_si = FALSE;
10181 can_si = FALSE;
10182 can_si_back = FALSE;
10183#endif
10184 AppendToRedobuff((char_u *)"\t");
10185
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010186#ifdef FEAT_VARTABS
10187 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10188 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010189 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010190 temp -= get_nolist_virtcol() % temp;
10191 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010192 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010193 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010194 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010195 curbuf->b_p_vsts_array);
10196 else /* otherwise use 'tabstop' */
10197 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10198 curbuf->b_p_vts_array);
10199#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010201 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010202 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10203 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 else /* otherwise use 'tabstop' */
10205 temp = (int)curbuf->b_p_ts;
10206 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208
10209 /*
10210 * Insert the first space with ins_char(). It will delete one char in
10211 * replace mode. Insert the rest with ins_str(); it will not delete any
10212 * chars. For VREPLACE mode, we use ins_char() for all characters.
10213 */
10214 ins_char(' ');
10215 while (--temp > 0)
10216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 if (State & VREPLACE_FLAG)
10218 ins_char(' ');
10219 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 {
10221 ins_str((char_u *)" ");
10222 if (State & REPLACE_FLAG) /* no char replaced */
10223 replace_push(NUL);
10224 }
10225 }
10226
10227 /*
10228 * When 'expandtab' not set: Replace spaces by TABs where possible.
10229 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010230#ifdef FEAT_VARTABS
10231 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10232 || get_sts_value() > 0
10233 || (p_sta && ind)))
10234#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010235 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 {
10238 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 char_u *saved_line = NULL; /* init for GCC */
10240 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 pos_T fpos;
10242 pos_T *cursor;
10243 colnr_T want_vcol, vcol;
10244 int change_col = -1;
10245 int save_list = curwin->w_p_list;
10246
10247 /*
10248 * Get the current line. For VREPLACE mode, don't make real changes
10249 * yet, just work on a copy of the line.
10250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 if (State & VREPLACE_FLAG)
10252 {
10253 pos = curwin->w_cursor;
10254 cursor = &pos;
10255 saved_line = vim_strsave(ml_get_curline());
10256 if (saved_line == NULL)
10257 return FALSE;
10258 ptr = saved_line + pos.col;
10259 }
10260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 {
10262 ptr = ml_get_cursor();
10263 cursor = &curwin->w_cursor;
10264 }
10265
10266 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10267 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10268 curwin->w_p_list = FALSE;
10269
10270 /* Find first white before the cursor */
10271 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010272 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 {
10274 --fpos.col;
10275 --ptr;
10276 }
10277
10278 /* In Replace mode, don't change characters before the insert point. */
10279 if ((State & REPLACE_FLAG)
10280 && fpos.lnum == Insstart.lnum
10281 && fpos.col < Insstart.col)
10282 {
10283 ptr += Insstart.col - fpos.col;
10284 fpos.col = Insstart.col;
10285 }
10286
10287 /* compute virtual column numbers of first white and cursor */
10288 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10289 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10290
Bram Moolenaar597a4222014-06-25 14:39:50 +020010291 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10292 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010293 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010295 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 if (vcol + i > want_vcol)
10297 break;
10298 if (*ptr != TAB)
10299 {
10300 *ptr = TAB;
10301 if (change_col < 0)
10302 {
10303 change_col = fpos.col; /* Column of first change */
10304 /* May have to adjust Insstart */
10305 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10306 Insstart.col = fpos.col;
10307 }
10308 }
10309 ++fpos.col;
10310 ++ptr;
10311 vcol += i;
10312 }
10313
10314 if (change_col >= 0)
10315 {
10316 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010317 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318
10319 /* Skip over the spaces we need. */
10320 while (vcol < want_vcol && *ptr == ' ')
10321 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010322 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 ++ptr;
10324 ++repl_off;
10325 }
10326 if (vcol > want_vcol)
10327 {
10328 /* Must have a char with 'showbreak' just before it. */
10329 --ptr;
10330 --repl_off;
10331 }
10332 fpos.col += repl_off;
10333
10334 /* Delete following spaces. */
10335 i = cursor->col - fpos.col;
10336 if (i > 0)
10337 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010338 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010340 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 for (temp = i; --temp >= 0; )
10342 replace_join(repl_off);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010010343#ifdef FEAT_TEXT_PROP
10344 curbuf->b_ml.ml_line_len -= i;
10345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010347#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010348 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010349 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010350 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010351 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10352 (char_u *)"\t", 1);
10353 }
10354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 cursor->col -= i;
10356
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 /*
10358 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10359 * backspacing over the changed spacing and then inserting the new
10360 * spacing.
10361 */
10362 if (State & VREPLACE_FLAG)
10363 {
10364 /* Backspace from real cursor to change_col */
10365 backspace_until_column(change_col);
10366
10367 /* Insert each char in saved_line from changed_col to
10368 * ptr-cursor */
10369 ins_bytes_len(saved_line + change_col,
10370 cursor->col - change_col);
10371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 }
10373
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 if (State & VREPLACE_FLAG)
10375 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 curwin->w_p_list = save_list;
10377 }
10378
10379 return FALSE;
10380}
10381
10382/*
10383 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010384 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 */
10386 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010387ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388{
10389 int i;
10390
10391 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010392 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010394 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 undisplay_dollar();
10396
10397 /*
10398 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10399 * character under the cursor. Only push a NUL on the replace stack,
10400 * nothing to put back when the NL is deleted.
10401 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010402 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 replace_push(NUL);
10404
10405 /*
10406 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10407 * replacing the next line, so we push all of the characters left on the
10408 * line onto the replace stack. This is not done here though, it is done
10409 * in open_line().
10410 */
10411
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010412#ifdef FEAT_VIRTUALEDIT
10413 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10414 * CTRL-O). */
10415 if (virtual_active() && curwin->w_cursor.coladd > 0)
10416 coladvance(getviscol());
10417#endif
10418
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419#ifdef FEAT_RIGHTLEFT
10420# ifdef FEAT_FKMAP
10421 if (p_altkeymap && p_fkmap)
10422 fkmap(NL);
10423# endif
10424 /* NL in reverse insert will always start in the end of
10425 * current line. */
10426 if (revins_on)
10427 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10428#endif
10429
10430 AppendToRedobuff(NL_STR);
10431 i = open_line(FORWARD,
10432#ifdef FEAT_COMMENTS
10433 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10434#endif
10435 0, old_indent);
10436 old_indent = 0;
10437#ifdef FEAT_CINDENT
10438 can_cindent = TRUE;
10439#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010440#ifdef FEAT_FOLDING
10441 /* When inserting a line the cursor line must never be in a closed fold. */
10442 foldOpenCursor();
10443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010445 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446}
10447
10448#ifdef FEAT_DIGRAPHS
10449/*
10450 * Handle digraph in insert mode.
10451 * Returns character still to be inserted, or NUL when nothing remaining to be
10452 * done.
10453 */
10454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010455ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456{
10457 int c;
10458 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010459 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460
10461 pc_status = PC_STATUS_UNSET;
10462 if (redrawing() && !char_avail())
10463 {
10464 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010465 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466
10467 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010468 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469#ifdef FEAT_CMDL_INFO
10470 add_to_showcmd_c(Ctrl_K);
10471#endif
10472 }
10473
10474#ifdef USE_ON_FLY_SCROLL
10475 dont_scroll = TRUE; /* disallow scrolling here */
10476#endif
10477
10478 /* don't map the digraph chars. This also prevents the
10479 * mode message to be deleted when ESC is hit */
10480 ++no_mapping;
10481 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010482 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 --no_mapping;
10484 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010485 if (did_putchar)
10486 /* when the line fits in 'columns' the '?' is at the start of the next
10487 * line and will not be removed by the redraw */
10488 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010489
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 if (IS_SPECIAL(c) || mod_mask) /* special key */
10491 {
10492#ifdef FEAT_CMDL_INFO
10493 clear_showcmd();
10494#endif
10495 insert_special(c, TRUE, FALSE);
10496 return NUL;
10497 }
10498 if (c != ESC)
10499 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010500 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 if (redrawing() && !char_avail())
10502 {
10503 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010504 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505
10506 if (char2cells(c) == 1)
10507 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010508 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010510 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 }
10512#ifdef FEAT_CMDL_INFO
10513 add_to_showcmd_c(c);
10514#endif
10515 }
10516 ++no_mapping;
10517 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010518 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 --no_mapping;
10520 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010521 if (did_putchar)
10522 /* when the line fits in 'columns' the '?' is at the start of the
10523 * next line and will not be removed by a redraw */
10524 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 if (cc != ESC)
10526 {
10527 AppendToRedobuff((char_u *)CTRL_V_STR);
10528 c = getdigraph(c, cc, TRUE);
10529#ifdef FEAT_CMDL_INFO
10530 clear_showcmd();
10531#endif
10532 return c;
10533 }
10534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535#ifdef FEAT_CMDL_INFO
10536 clear_showcmd();
10537#endif
10538 return NUL;
10539}
10540#endif
10541
10542/*
10543 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10544 * Returns the char to be inserted, or NUL if none found.
10545 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010546 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010547ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
10549 int c;
10550 int temp;
10551 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010552 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553
10554 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10555 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010556 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 return NUL;
10558 }
10559
10560 /* try to advance to the cursor column */
10561 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010562 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 prev_ptr = ptr;
10564 validate_virtcol();
10565 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10566 {
10567 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010568 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 }
10570 if ((colnr_T)temp > curwin->w_virtcol)
10571 ptr = prev_ptr;
10572
10573#ifdef FEAT_MBYTE
10574 c = (*mb_ptr2char)(ptr);
10575#else
10576 c = *ptr;
10577#endif
10578 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010579 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 return c;
10581}
10582
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010583/*
10584 * CTRL-Y or CTRL-E typed in Insert mode.
10585 */
10586 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010587ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010588{
10589 int c = tc;
10590
10591#ifdef FEAT_INS_EXPAND
10592 if (ctrl_x_mode == CTRL_X_SCROLL)
10593 {
10594 if (c == Ctrl_Y)
10595 scrolldown_clamp();
10596 else
10597 scrollup_clamp();
10598 redraw_later(VALID);
10599 }
10600 else
10601#endif
10602 {
10603 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10604 if (c != NUL)
10605 {
10606 long tw_save;
10607
10608 /* The character must be taken literally, insert like it
10609 * was typed after a CTRL-V, and pretend 'textwidth'
10610 * wasn't set. Digits, 'o' and 'x' are special after a
10611 * CTRL-V, don't use it for these. */
10612 if (c < 256 && !isalnum(c))
10613 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10614 tw_save = curbuf->b_p_tw;
10615 curbuf->b_p_tw = -1;
10616 insert_special(c, TRUE, FALSE);
10617 curbuf->b_p_tw = tw_save;
10618#ifdef FEAT_RIGHTLEFT
10619 revins_chars++;
10620 revins_legal++;
10621#endif
10622 c = Ctrl_V; /* pretend CTRL-V is last character */
10623 auto_format(FALSE, TRUE);
10624 }
10625 }
10626 return c;
10627}
10628
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629#ifdef FEAT_SMARTINDENT
10630/*
10631 * Try to do some very smart auto-indenting.
10632 * Used when inserting a "normal" character.
10633 */
10634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010635ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636{
10637 pos_T *pos, old_pos;
10638 char_u *ptr;
10639 int i;
10640 int temp;
10641
10642 /*
10643 * do some very smart indenting when entering '{' or '}'
10644 */
10645 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10646 {
10647 /*
10648 * for '}' set indent equal to indent of line containing matching '{'
10649 */
10650 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10651 {
10652 old_pos = curwin->w_cursor;
10653 /*
10654 * If the matching '{' has a ')' immediately before it (ignoring
10655 * white-space), then line up with the start of the line
10656 * containing the matching '(' if there is one. This handles the
10657 * case where an "if (..\n..) {" statement continues over multiple
10658 * lines -- webb
10659 */
10660 ptr = ml_get(pos->lnum);
10661 i = pos->col;
10662 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010663 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 ;
10665 curwin->w_cursor.lnum = pos->lnum;
10666 curwin->w_cursor.col = i;
10667 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10668 curwin->w_cursor = *pos;
10669 i = get_indent();
10670 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010672 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 (void)set_indent(i, SIN_CHANGED);
10675 }
10676 else if (curwin->w_cursor.col > 0)
10677 {
10678 /*
10679 * when inserting '{' after "O" reduce indent, but not
10680 * more than indent of previous line
10681 */
10682 temp = TRUE;
10683 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10684 {
10685 old_pos = curwin->w_cursor;
10686 i = get_indent();
10687 while (curwin->w_cursor.lnum > 1)
10688 {
10689 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10690
10691 /* ignore empty lines and lines starting with '#'. */
10692 if (*ptr != '#' && *ptr != NUL)
10693 break;
10694 }
10695 if (get_indent() >= i)
10696 temp = FALSE;
10697 curwin->w_cursor = old_pos;
10698 }
10699 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010700 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 }
10702 }
10703
10704 /*
10705 * set indent of '#' always to 0
10706 */
10707 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10708 {
10709 /* remember current indent for next line */
10710 old_indent = get_indent();
10711 (void)set_indent(0, SIN_CHANGED);
10712 }
10713
10714 /* Adjust ai_col, the char at this position can be deleted. */
10715 if (ai_col > curwin->w_cursor.col)
10716 ai_col = curwin->w_cursor.col;
10717}
10718#endif
10719
10720/*
10721 * Get the value that w_virtcol would have when 'list' is off.
10722 * Unless 'cpo' contains the 'L' flag.
10723 */
Bram Moolenaarf9514162018-11-22 03:08:29 +010010724 colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010725get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010727 // check validity of cursor in current buffer
10728 if (curwin->w_buffer == NULL
10729 || curwin->w_buffer->b_ml.ml_mfp == NULL
10730 || curwin->w_cursor.lnum > curwin->w_buffer->b_ml.ml_line_count)
10731 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10733 return getvcol_nolist(&curwin->w_cursor);
10734 validate_virtcol();
10735 return curwin->w_virtcol;
10736}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010737
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010738#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010739/*
10740 * Handle the InsertCharPre autocommand.
10741 * "c" is the character that was typed.
10742 * Return a pointer to allocated memory with the replacement string.
10743 * Return NULL to continue inserting "c".
10744 */
10745 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010746do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010747{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010748 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010749 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010750 int save_State = State;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010751
10752 /* Return quickly when there is nothing to do. */
10753 if (!has_insertcharpre())
10754 return NULL;
10755
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010756# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010757 if (has_mbyte)
10758 buf[(*mb_char2bytes)(c, buf)] = NUL;
10759 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010760# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010761 {
10762 buf[0] = c;
10763 buf[1] = NUL;
10764 }
10765
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010766 /* Lock the text to avoid weird things from happening. */
10767 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010768 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010769
Bram Moolenaar704984a2012-06-01 14:57:51 +020010770 res = NULL;
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010771 if (ins_apply_autocmds(EVENT_INSERTCHARPRE))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010772 {
10773 /* Get the value of v:char. It may be empty or more than one
10774 * character. Only use it when changed, otherwise continue with the
10775 * original character to avoid breaking autoindent. */
10776 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10777 res = vim_strsave(get_vim_var_str(VV_CHAR));
10778 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010779
10780 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10781 --textlock;
10782
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010010783 // Restore the State, it may have been changed.
10784 State = save_State;
10785
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010786 return res;
10787}
10788#endif
Bram Moolenaar9fa95062018-08-08 22:08:32 +020010789
10790/*
10791 * Trigger "event" and take care of fixing undo.
10792 */
10793 static int
10794ins_apply_autocmds(event_T event)
10795{
10796 varnumber_T tick = CHANGEDTICK(curbuf);
10797 int r;
10798
10799 r = apply_autocmds(event, NULL, NULL, FALSE, curbuf);
10800
10801 // If u_savesub() was called then we are not prepared to start
10802 // a new line. Call u_save() with no contents to fix that.
10803 if (tick != CHANGEDTICK(curbuf))
10804 u_save(curwin->w_cursor.lnum, (linenr_T)(curwin->w_cursor.lnum + 1));
10805
10806 return r;
10807}