blob: b0b44e606cd5710dc91c14c574266e772df552de [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;
153
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static void ins_ctrl_x(void);
155static int has_compl_option(int dict_opt);
156static int ins_compl_accept_char(int c);
157static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
158static int ins_compl_equal(compl_T *match, char_u *str, int len);
159static void ins_compl_longest_match(compl_T *match);
160static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
161static int ins_compl_make_cyclic(void);
162static void ins_compl_upd_pum(void);
163static void ins_compl_del_pum(void);
164static int pum_wanted(void);
165static int pum_enough_matches(void);
166static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
167static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
168static char_u *find_line_end(char_u *ptr);
169static void ins_compl_free(void);
170static void ins_compl_clear(void);
171static int ins_compl_bs(void);
172static int ins_compl_need_restart(void);
173static void ins_compl_new_leader(void);
174static void ins_compl_addleader(int c);
175static int ins_compl_len(void);
176static void ins_compl_restart(void);
177static void ins_compl_set_original_text(char_u *str);
178static void ins_compl_addfrommatch(void);
179static int ins_compl_prep(int c);
180static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
181static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100182# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100183static void ins_compl_add_list(list_T *list);
184static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100185# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static int ins_compl_get_exp(pos_T *ini);
187static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200188static void ins_compl_insert(int in_compl_func);
189static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static int ins_compl_key2dir(int c);
191static int ins_compl_pum_key(int c);
192static int ins_compl_key2count(int c);
193static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100194static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100195static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100196static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif /* FEAT_INS_EXPAND */
198
199#define BACKSPACE_CHAR 1
200#define BACKSPACE_WORD 2
201#define BACKSPACE_WORD_NOT_SPACE 3
202#define BACKSPACE_LINE 4
203
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ins_redraw(int ready);
205static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200206#ifdef FEAT_JOB_CHANNEL
207static void init_prompt(int cmdchar_todo);
208#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void undisplay_dollar(void);
210static void insert_special(int, int, int);
211static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
212static void check_auto_format(int);
213static void redo_literal(int c);
214static void start_arrow(pos_T *end_insert_pos);
215static void start_arrow_with_change(pos_T *end_insert_pos, int change);
216static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000217#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100218static void check_spell_redraw(void);
219static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000220static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000221#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
223static int echeck_abbr(int);
224static int replace_pop(void);
225static void replace_join(int off);
226static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void replace_flush(void);
231static void replace_do_bs(int limit_col);
232static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ins_reg(void);
237static void ins_ctrl_g(void);
238static void ins_ctrl_hat(void);
239static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static int ins_start_select(int c);
244static void ins_insert(int replaceState);
245static void ins_ctrl_o(void);
246static void ins_shift(int c, int lastc);
247static void ins_del(void);
248static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ins_mouse(int c);
251static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000253#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000255#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ins_left(int end_change);
257static void ins_home(int c);
258static void ins_end(int c);
259static void ins_s_left(void);
260static void ins_right(int end_change);
261static void ins_s_right(void);
262static void ins_up(int startcol);
263static void ins_pageup(void);
264static void ins_down(int startcol);
265static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static int ins_tab(void);
270static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100272static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100274static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100276static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100278static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100279#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100280static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
283static colnr_T Insstart_textlen; /* length of line when insert started */
284static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100285static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286
287static char_u *last_insert = NULL; /* the text of the previous insert,
288 K_SPECIAL and CSI are escaped */
289static int last_insert_skip; /* nr of chars in front of previous insert */
290static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000291static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
293#ifdef FEAT_CINDENT
294static int can_cindent; /* may do cindenting on this line */
295#endif
296
297static int old_indent = 0; /* for ^^D command in insert mode */
298
299#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000300static int revins_on; /* reverse insert mode on */
301static int revins_chars; /* how much to skip after edit */
302static int revins_legal; /* was the last char 'legal'? */
303static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#endif
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static int ins_need_undo; /* call u_save() before inserting a
307 char. Set when edit() is called.
308 after that arrow_used is used. */
309
310static int did_add_space = FALSE; /* auto_format() added an extra space
311 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200312static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
313 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * edit(): Start inserting text.
317 *
318 * "cmdchar" can be:
319 * 'i' normal insert command
320 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100321 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 * 'R' replace command
323 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
324 * but still only one <CR> is inserted. The <Esc> is not used for redo.
325 * 'g' "gI" command.
326 * 'V' "gR" command for Virtual Replace mode.
327 * 'v' "gr" command for single character Virtual Replace mode.
328 *
329 * This function is not called recursively. For CTRL-O commands, it returns
330 * and lets the caller handle the Normal-mode command.
331 *
332 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
333 */
334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100335edit(
336 int cmdchar,
337 int startln, /* if set, insert at start of line */
338 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 int c = 0;
341 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100342 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000343 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 int i;
346 int did_backspace = TRUE; /* previous char was backspace */
347#ifdef FEAT_CINDENT
348 int line_is_white = FALSE; /* line is empty before insert */
349#endif
350 linenr_T old_topline = 0; /* topline before insertion */
351#ifdef FEAT_DIFF
352 int old_topfill = -1;
353#endif
354 int inserted_space = FALSE; /* just inserted a space */
355 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000356 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200357#ifdef FEAT_JOB_CHANNEL
358 int cmdchar_todo = cmdchar;
359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000361 /* Remember whether editing was restarted after CTRL-O. */
362 did_restart_edit = restart_edit;
363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 /* sleep before redrawing, needed for "CTRL-O :" that results in an
365 * error message */
366 check_for_delay(TRUE);
367
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100368 /* set Insstart_orig to Insstart */
369 update_Insstart_orig = TRUE;
370
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371#ifdef HAVE_SANDBOX
372 /* Don't allow inserting in the sandbox. */
373 if (sandbox != 0)
374 {
375 EMSG(_(e_sandbox));
376 return FALSE;
377 }
378#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000379 /* Don't allow changes in the buffer while editing the cmdline. The
380 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000381 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000382 {
383 EMSG(_(e_secure));
384 return FALSE;
385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
387#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000388 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000389 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000390 {
391 EMSG(_(e_secure));
392 return FALSE;
393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 ins_compl_clear(); /* clear stuff for CTRL-X mode */
395#endif
396
Bram Moolenaar843ee412004-06-30 16:16:41 +0000397 /*
398 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
399 */
400 if (cmdchar != 'r' && cmdchar != 'v')
401 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100402 pos_T save_cursor = curwin->w_cursor;
403
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100404#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000405 if (cmdchar == 'R')
406 ptr = (char_u *)"r";
407 else if (cmdchar == 'V')
408 ptr = (char_u *)"v";
409 else
410 ptr = (char_u *)"i";
411 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200412 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100413#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000414 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100415
Bram Moolenaar097c9922013-05-19 21:15:15 +0200416 /* Make sure the cursor didn't move. Do call check_cursor_col() in
417 * case the text was modified. Since Insert mode was not started yet
418 * a call to check_cursor_col() may move the cursor, especially with
419 * the "A" command, thus set State to avoid that. Also check that the
420 * line number is still valid (lines may have been deleted).
421 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100422 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100423#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200424 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100425#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200426 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100427 {
428 int save_state = State;
429
430 curwin->w_cursor = save_cursor;
431 State = INSERT;
432 check_cursor_col();
433 State = save_state;
434 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000435 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000436
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200437#ifdef FEAT_CONCEAL
438 /* Check if the cursor line needs redrawing before changing State. If
439 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200440 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200441#endif
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#ifdef FEAT_MOUSE
444 /*
445 * When doing a paste with the middle mouse button, Insstart is set to
446 * where the paste started.
447 */
448 if (where_paste_started.lnum != 0)
449 Insstart = where_paste_started;
450 else
451#endif
452 {
453 Insstart = curwin->w_cursor;
454 if (startln)
455 Insstart.col = 0;
456 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000457 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 Insstart_blank_vcol = MAXCOL;
459 if (!did_ai)
460 ai_col = 0;
461
462 if (cmdchar != NUL && restart_edit == 0)
463 {
464 ResetRedobuff();
465 AppendNumberToRedobuff(count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 if (cmdchar == 'V' || cmdchar == 'v')
467 {
468 /* "gR" or "gr" command */
469 AppendCharToRedobuff('g');
470 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
471 }
472 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100474 if (cmdchar == K_PS)
475 AppendCharToRedobuff('a');
476 else
477 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (cmdchar == 'g') /* "gI" command */
479 AppendCharToRedobuff('I');
480 else if (cmdchar == 'r') /* "r<CR>" command */
481 count = 1; /* insert only one <CR> */
482 }
483 }
484
485 if (cmdchar == 'R')
486 {
487#ifdef FEAT_FKMAP
488 if (p_fkmap && p_ri)
489 {
490 beep_flush();
491 EMSG(farsi_text_3); /* encoded in Farsi */
492 State = INSERT;
493 }
494 else
495#endif
496 State = REPLACE;
497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 else if (cmdchar == 'V' || cmdchar == 'v')
499 {
500 State = VREPLACE;
501 replaceState = VREPLACE;
502 orig_line_count = curbuf->b_ml.ml_line_count;
503 vr_lines_changed = 1;
504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 else
506 State = INSERT;
507
508 stop_insert_mode = FALSE;
509
510 /*
511 * Need to recompute the cursor position, it might move when the cursor is
512 * on a TAB or special character.
513 */
514 curs_columns(TRUE);
515
516 /*
517 * Enable langmap or IME, indicated by 'iminsert'.
518 * Note that IME may enabled/disabled without us noticing here, thus the
519 * 'iminsert' value may not reflect what is actually used. It is updated
520 * when hitting <Esc>.
521 */
522 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
523 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100524#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
526#endif
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#ifdef FEAT_MOUSE
529 setmouse();
530#endif
531#ifdef FEAT_CMDL_INFO
532 clear_showcmd();
533#endif
534#ifdef FEAT_RIGHTLEFT
535 /* there is no reverse replace mode */
536 revins_on = (State == INSERT && p_ri);
537 if (revins_on)
538 undisplay_dollar();
539 revins_chars = 0;
540 revins_legal = 0;
541 revins_scol = -1;
542#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100543 if (!p_ek)
544 /* Disable bracketed paste mode, we won't recognize the escape
545 * sequences. */
546 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 /*
549 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100550 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
551 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 if (restart_edit != 0 && stuff_empty())
554 {
555#ifdef FEAT_MOUSE
556 /*
557 * After a paste we consider text typed to be part of the insert for
558 * the pasted text. You can backspace over the pasted text too.
559 */
560 if (where_paste_started.lnum)
561 arrow_used = FALSE;
562 else
563#endif
564 arrow_used = TRUE;
565 restart_edit = 0;
566
567 /*
568 * If the cursor was after the end-of-line before the CTRL-O and it is
569 * now at the end-of-line, put it after the end-of-line (this is not
570 * correct in very rare cases).
571 * Also do this if curswant is greater than the current virtual
572 * column. Eg after "^O$" or "^O80|".
573 */
574 validate_virtcol();
575 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 || curwin->w_curswant > curwin->w_virtcol)
578 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
579 {
580 if (ptr[1] == NUL)
581 ++curwin->w_cursor.col;
582#ifdef FEAT_MBYTE
583 else if (has_mbyte)
584 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000585 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (ptr[i] == NUL)
587 curwin->w_cursor.col += i;
588 }
589#endif
590 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000591 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 else
594 arrow_used = FALSE;
595
596 /* we are in insert mode now, don't need to start it anymore */
597 need_start_insertmode = FALSE;
598
599 /* Need to save the line for undo before inserting the first char. */
600 ins_need_undo = TRUE;
601
602#ifdef FEAT_MOUSE
603 where_paste_started.lnum = 0;
604#endif
605#ifdef FEAT_CINDENT
606 can_cindent = TRUE;
607#endif
608#ifdef FEAT_FOLDING
609 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
610 * restarting. */
611 if (!p_im && did_restart_edit == 0)
612 foldOpenCursor();
613#endif
614
615 /*
616 * If 'showmode' is set, show the current (insert/replace/..) mode.
617 * A warning message for changing a readonly file is given here, before
618 * actually changing anything. It's put after the mode, if any.
619 */
620 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000621 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 i = showmode();
623
624 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000625 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627#ifdef CURSOR_SHAPE
628 ui_cursor_shape(); /* may show different cursor shape */
629#endif
630#ifdef FEAT_DIGRAPHS
631 do_digraph(-1); /* clear digraphs */
632#endif
633
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000634 /*
635 * Get the current length of the redo buffer, those characters have to be
636 * skipped if we want to get to the inserted characters.
637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ptr = get_inserted();
639 if (ptr == NULL)
640 new_insert_skip = 0;
641 else
642 {
643 new_insert_skip = (int)STRLEN(ptr);
644 vim_free(ptr);
645 }
646
647 old_indent = 0;
648
649 /*
650 * Main loop in Insert mode: repeat until Insert mode is left.
651 */
652 for (;;)
653 {
654#ifdef FEAT_RIGHTLEFT
655 if (!revins_legal)
656 revins_scol = -1; /* reset on illegal motions */
657 else
658 revins_legal = 0;
659#endif
660 if (arrow_used) /* don't repeat insert when arrow key used */
661 count = 0;
662
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100663 if (update_Insstart_orig)
664 Insstart_orig = Insstart;
665
Bram Moolenaar00672e12016-06-26 18:38:13 +0200666 if (stop_insert_mode
667#ifdef FEAT_INS_EXPAND
668 && !pum_visible()
669#endif
670 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
672 /* ":stopinsert" used or 'insertmode' reset */
673 count = 0;
674 goto doESCkey;
675 }
676
677 /* set curwin->w_curswant for next K_DOWN or K_UP */
678 if (!arrow_used)
679 curwin->w_set_curswant = TRUE;
680
681 /* If there is no typeahead may check for timestamps (e.g., for when a
682 * menu invoked a shell command). */
683 if (stuff_empty())
684 {
685 did_check_timestamps = FALSE;
686 if (need_check_timestamps)
687 check_timestamps(FALSE);
688 }
689
690 /*
691 * When emsg() was called msg_scroll will have been set.
692 */
693 msg_scroll = FALSE;
694
695#ifdef FEAT_GUI
696 /* When 'mousefocus' is set a mouse movement may have taken us to
697 * another window. "need_mouse_correct" may then be set because of an
698 * autocommand. */
699 if (need_mouse_correct)
700 gui_mouse_correct();
701#endif
702
703#ifdef FEAT_FOLDING
704 /* Open fold at the cursor line, according to 'foldopen'. */
705 if (fdo_flags & FDO_INSERT)
706 foldOpenCursor();
707 /* Close folds where the cursor isn't, according to 'foldclose' */
708 if (!char_avail())
709 foldCheckClose();
710#endif
711
Bram Moolenaarf2732452018-06-03 14:47:35 +0200712#ifdef FEAT_JOB_CHANNEL
713 if (bt_prompt(curbuf))
714 {
715 init_prompt(cmdchar_todo);
716 cmdchar_todo = NUL;
717 }
718#endif
719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 /*
721 * If we inserted a character at the last position of the last line in
722 * the window, scroll the window one line up. This avoids an extra
723 * redraw.
724 * This is detected when the cursor column is smaller after inserting
725 * something.
726 * Don't do this when the topline changed already, it has
727 * already been adjusted (by insertchar() calling open_line())).
728 */
729 if (curbuf->b_mod_set
730 && curwin->w_p_wrap
731 && !did_backspace
732 && curwin->w_topline == old_topline
733#ifdef FEAT_DIFF
734 && curwin->w_topfill == old_topfill
735#endif
736 )
737 {
738 mincol = curwin->w_wcol;
739 validate_cursor_col();
740
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200741 if (
742#ifdef FEAT_VARTABS
743 (int)curwin->w_wcol < mincol - tabstop_at(
744 get_nolist_virtcol(), curbuf->b_p_ts,
745 curbuf->b_p_vts_array)
746#else
747 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 && curwin->w_wrow == W_WINROW(curwin)
750 + curwin->w_height - 1 - p_so
751 && (curwin->w_cursor.lnum != curwin->w_topline
752#ifdef FEAT_DIFF
753 || curwin->w_topfill > 0
754#endif
755 ))
756 {
757#ifdef FEAT_DIFF
758 if (curwin->w_topfill > 0)
759 --curwin->w_topfill;
760 else
761#endif
762#ifdef FEAT_FOLDING
763 if (hasFolding(curwin->w_topline, NULL, &old_topline))
764 set_topline(curwin, old_topline + 1);
765 else
766#endif
767 set_topline(curwin, curwin->w_topline + 1);
768 }
769 }
770
771 /* May need to adjust w_topline to show the cursor. */
772 update_topline();
773
774 did_backspace = FALSE;
775
776 validate_cursor(); /* may set must_redraw */
777
778 /*
779 * Redraw the display when no characters are waiting.
780 * Also shows mode, ruler and positions cursor.
781 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000782 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (curwin->w_p_scb)
785 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
Bram Moolenaar860cae12010-06-05 23:22:07 +0200787 if (curwin->w_p_crb)
788 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 update_curswant();
790 old_topline = curwin->w_topline;
791#ifdef FEAT_DIFF
792 old_topfill = curwin->w_topfill;
793#endif
794
795#ifdef USE_ON_FLY_SCROLL
796 dont_scroll = FALSE; /* allow scrolling here */
797#endif
798
799 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100800 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100802 if (c != K_CURSORHOLD)
803 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200804
805 /* After using CTRL-G U the next cursor key will not break undo. */
806 if (dont_sync_undo == MAYBE)
807 dont_sync_undo = TRUE;
808 else
809 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100810 if (cmdchar == K_PS)
811 /* Got here from normal mode when bracketed paste started. */
812 c = K_PS;
813 else
814 do
815 {
816 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200817
818 if (stop_insert_mode)
819 {
820 // Insert mode ended, possibly from a callback.
821 count = 0;
822 nomove = TRUE;
823 goto doESCkey;
824 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100825 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000827 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
828 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef FEAT_RIGHTLEFT
831 if (p_hkmap && KeyTyped)
832 c = hkmap(c); /* Hebrew mode mapping */
833#endif
834#ifdef FEAT_FKMAP
835 if (p_fkmap && KeyTyped)
836 c = fkmap(c); /* Farsi mode mapping */
837#endif
838
839#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 /*
841 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000842 * and the cursor is still in the completed word. Only when there is
843 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000845 if (compl_started
846 && pum_wanted()
847 && curwin->w_cursor.col >= compl_col
848 && (compl_shown_match == NULL
849 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000850 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000851 /* BS: Delete one character from "compl_leader". */
852 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000853 && curwin->w_cursor.col > compl_col
854 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000855 continue;
856
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000857 /* When no match was selected or it was edited. */
858 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000859 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000860 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000861 * "compl_leader". Except when at the original match and
862 * there is nothing to add, CTRL-L works like CTRL-P then. */
863 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100864 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000865 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000866 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000867 {
868 ins_compl_addfrommatch();
869 continue;
870 }
871
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000872 /* A non-white character that fits in with the current
873 * completion: Add to "compl_leader". */
874 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000875 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100876#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100877 /* Trigger InsertCharPre. */
878 char_u *str = do_insert_char_pre(c);
879 char_u *p;
880
881 if (str != NULL)
882 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100883 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100884 ins_compl_addleader(PTR2CHAR(p));
885 vim_free(str);
886 }
887 else
888#endif
889 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000890 continue;
891 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000892
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000893 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000894 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200895 if ((c == Ctrl_Y || (compl_enter_selects
896 && (c == CAR || c == K_KENTER || c == NL)))
897 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000898 {
899 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200900 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000901 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000902 }
903 }
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
906 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000907 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000908 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000909 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
911
Bram Moolenaar488c6512005-08-11 20:09:58 +0000912 /* CTRL-\ CTRL-N goes to Normal mode,
913 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
914 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (c == Ctrl_BSL)
916 {
917 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000918 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 ++no_mapping;
920 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000921 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 --no_mapping;
923 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000924 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000926 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 vungetc(c);
928 c = Ctrl_BSL;
929 }
930 else if (c == Ctrl_G && p_im)
931 continue;
932 else
933 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000934 if (c == Ctrl_O)
935 {
936 ins_ctrl_o();
937 ins_at_eol = FALSE; /* cursor keeps its column */
938 nomove = TRUE;
939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 count = 0;
941 goto doESCkey;
942 }
943 }
944
945#ifdef FEAT_DIGRAPHS
946 c = do_digraph(c);
947#endif
948
949#ifdef FEAT_INS_EXPAND
950 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
951 goto docomplete;
952#endif
953 if (c == Ctrl_V || c == Ctrl_Q)
954 {
955 ins_ctrl_v();
956 c = Ctrl_V; /* pretend CTRL-V is last typed character */
957 continue;
958 }
959
960#ifdef FEAT_CINDENT
961 if (cindent_on()
962# ifdef FEAT_INS_EXPAND
963 && ctrl_x_mode == 0
964# endif
965 )
966 {
967 /* A key name preceded by a bang means this key is not to be
968 * inserted. Skip ahead to the re-indenting below.
969 * A key name preceded by a star means that indenting has to be
970 * done before inserting the key. */
971 line_is_white = inindent(0);
972 if (in_cinkeys(c, '!', line_is_white))
973 goto force_cindent;
974 if (can_cindent && in_cinkeys(c, '*', line_is_white)
975 && stop_arrow() == OK)
976 do_c_expr_indent();
977 }
978#endif
979
980#ifdef FEAT_RIGHTLEFT
981 if (curwin->w_p_rl)
982 switch (c)
983 {
984 case K_LEFT: c = K_RIGHT; break;
985 case K_S_LEFT: c = K_S_RIGHT; break;
986 case K_C_LEFT: c = K_C_RIGHT; break;
987 case K_RIGHT: c = K_LEFT; break;
988 case K_S_RIGHT: c = K_S_LEFT; break;
989 case K_C_RIGHT: c = K_C_LEFT; break;
990 }
991#endif
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 /*
994 * If 'keymodel' contains "startsel", may start selection. If it
995 * does, a CTRL-O and c will be stuffed, we need to get these
996 * characters.
997 */
998 if (ins_start_select(c))
999 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 /*
1002 * The big switch to handle a character in insert mode.
1003 */
1004 switch (c)
1005 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001006 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (echeck_abbr(ESC + ABBR_OFF))
1008 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001009 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001011 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#ifdef FEAT_CMDWIN
1013 if (c == Ctrl_C && cmdwin_type != 0)
1014 {
1015 /* Close the cmdline window. */
1016 cmdwin_result = K_IGNORE;
1017 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001018 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 goto doESCkey;
1020 }
1021#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001022#ifdef FEAT_JOB_CHANNEL
1023 if (c == Ctrl_C && bt_prompt(curbuf))
1024 {
1025 if (invoke_prompt_interrupt())
1026 {
1027 if (!bt_prompt(curbuf))
1028 // buffer changed to a non-prompt buffer, get out of
1029 // Insert mode
1030 goto doESCkey;
1031 break;
1032 }
1033 }
1034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036#ifdef UNIX
1037do_intr:
1038#endif
1039 /* when 'insertmode' set, and not halfway a mapping, don't leave
1040 * Insert mode */
1041 if (goto_im())
1042 {
1043 if (got_int)
1044 {
1045 (void)vgetc(); /* flush all buffers */
1046 got_int = FALSE;
1047 }
1048 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001049 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 break;
1051 }
1052doESCkey:
1053 /*
1054 * This is the ONLY return from edit()!
1055 */
1056 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1057 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001058 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 o_lnum = curwin->w_cursor.lnum;
1060
Bram Moolenaar488c6512005-08-11 20:09:58 +00001061 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001062 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00001063 if (cmdchar != 'r' && cmdchar != 'v')
1064 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1065 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001066 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 continue;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case Ctrl_Z: /* suspend when 'insertmode' set */
1072 if (!p_im)
1073 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001074 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001075#ifdef CURSOR_SHAPE
1076 ui_cursor_shape(); /* may need to update cursor shape */
1077#endif
1078 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079
1080 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001081#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001082 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 goto docomplete;
1084#endif
1085 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1086 break;
1087 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001088
1089#ifdef FEAT_VIRTUALEDIT
1090 /* don't move the cursor left when 'virtualedit' has "onemore". */
1091 if (ve_flags & VE_ONEMORE)
1092 {
1093 ins_at_eol = FALSE;
1094 nomove = TRUE;
1095 }
1096#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 count = 0;
1098 goto doESCkey;
1099
Bram Moolenaar572cb562005-08-05 21:35:02 +00001100 case K_INS: /* toggle insert/replace mode */
1101 case K_KINS:
1102 ins_insert(replaceState);
1103 break;
1104
1105 case K_SELECT: /* end of Select mode mapping - ignore */
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_HELP: /* Help key works like <ESC> <Help> */
1109 case K_F1:
1110 case K_XF1:
1111 stuffcharReadbuff(K_HELP);
1112 if (p_im)
1113 need_start_insertmode = TRUE;
1114 goto doESCkey;
1115
1116#ifdef FEAT_NETBEANS_INTG
1117 case K_F21: /* NetBeans command */
1118 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001119 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 --no_mapping;
1121 netbeans_keycommand(i);
1122 break;
1123#endif
1124
1125 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 case NUL:
1127 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 /* For ^@ the trailing ESC will end the insert, unless there is an
1129 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1131 && c != Ctrl_A && !p_im)
1132 goto doESCkey; /* quit insert mode */
1133 inserted_space = FALSE;
1134 break;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 ins_reg();
1138 auto_format(FALSE, TRUE);
1139 inserted_space = FALSE;
1140 break;
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 ins_ctrl_g();
1144 break;
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case Ctrl_HAT: /* switch input mode and/or langmap */
1147 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 break;
1149
1150#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (!p_ari)
1153 goto normalchar;
1154 ins_ctrl_();
1155 break;
1156#endif
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1160 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1161 goto docomplete;
1162#endif
1163 /* FALLTHROUGH */
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166# ifdef FEAT_INS_EXPAND
1167 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1168 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 if (has_compl_option(FALSE))
1170 goto docomplete;
1171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173# endif
1174 ins_shift(c, lastc);
1175 auto_format(FALSE, TRUE);
1176 inserted_space = FALSE;
1177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 case K_KDEL:
1181 ins_del();
1182 auto_format(FALSE, TRUE);
1183 break;
1184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case Ctrl_H:
1187 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1188 auto_format(FALSE, TRUE);
1189 break;
1190
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001191 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001192#ifdef FEAT_JOB_CHANNEL
1193 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1194 {
1195 // In a prompt window CTRL-W is used for window commands.
1196 // Use Shift-CTRL-W to delete a word.
1197 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001198 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001199 nomove = TRUE;
1200 count = 0;
1201 goto doESCkey;
1202 }
1203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1205 auto_format(FALSE, TRUE);
1206 break;
1207
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001208 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001209# ifdef FEAT_COMPL_FUNC
1210 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001212 goto docomplete;
1213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1215 auto_format(FALSE, TRUE);
1216 inserted_space = FALSE;
1217 break;
1218
1219#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 case K_LEFTMOUSE_NM:
1222 case K_LEFTDRAG:
1223 case K_LEFTRELEASE:
1224 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001225 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 case K_MIDDLEMOUSE:
1227 case K_MIDDLEDRAG:
1228 case K_MIDDLERELEASE:
1229 case K_RIGHTMOUSE:
1230 case K_RIGHTDRAG:
1231 case K_RIGHTRELEASE:
1232 case K_X1MOUSE:
1233 case K_X1DRAG:
1234 case K_X1RELEASE:
1235 case K_X2MOUSE:
1236 case K_X2DRAG:
1237 case K_X2RELEASE:
1238 ins_mouse(c);
1239 break;
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001242 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 break;
1244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001245 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001246 ins_mousescroll(MSCR_UP);
1247 break;
1248
1249 case K_MOUSELEFT: /* Scroll wheel left */
1250 ins_mousescroll(MSCR_LEFT);
1251 break;
1252
1253 case K_MOUSERIGHT: /* Scroll wheel right */
1254 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 break;
1256#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001257 case K_PS:
1258 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1259 if (cmdchar == K_PS)
1260 /* invoked from normal mode, bail out */
1261 goto doESCkey;
1262 break;
1263 case K_PE:
1264 /* Got K_PE without K_PS, ignore. */
1265 break;
1266
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001267#ifdef FEAT_GUI_TABLINE
1268 case K_TABLINE:
1269 case K_TABMENU:
1270 ins_tabline(c);
1271 break;
1272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001274 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 break;
1276
Bram Moolenaar754b5602006-02-09 23:53:20 +00001277 case K_CURSORHOLD: /* Didn't type something for a while. */
1278 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1279 did_cursorhold = TRUE;
1280 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001281
Bram Moolenaar4770d092006-01-12 23:22:24 +00001282#ifdef FEAT_GUI_W32
1283 /* On Win32 ignore <M-F4>, we get it when closing the window was
1284 * cancelled. */
1285 case K_F4:
1286 if (mod_mask != MOD_MASK_ALT)
1287 goto normalchar;
1288 break;
1289#endif
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#ifdef FEAT_GUI
1292 case K_VER_SCROLLBAR:
1293 ins_scroll();
1294 break;
1295
1296 case K_HOR_SCROLLBAR:
1297 ins_horscroll();
1298 break;
1299#endif
1300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001301 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 case K_S_HOME:
1304 case K_C_HOME:
1305 ins_home(c);
1306 break;
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 case K_S_END:
1311 case K_C_END:
1312 ins_end(c);
1313 break;
1314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001315 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001316 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1317 ins_s_left();
1318 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001319 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 break;
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 case K_C_LEFT:
1324 ins_s_left();
1325 break;
1326
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001327 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001328 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1329 ins_s_right();
1330 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001331 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 case K_C_RIGHT:
1336 ins_s_right();
1337 break;
1338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001340#ifdef FEAT_INS_EXPAND
1341 if (pum_visible())
1342 goto docomplete;
1343#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001344 if (mod_mask & MOD_MASK_SHIFT)
1345 ins_pageup();
1346 else
1347 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 break;
1349
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001350 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 case K_PAGEUP:
1352 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001353#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001354 if (pum_visible())
1355 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 ins_pageup();
1358 break;
1359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001361#ifdef FEAT_INS_EXPAND
1362 if (pum_visible())
1363 goto docomplete;
1364#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001365 if (mod_mask & MOD_MASK_SHIFT)
1366 ins_pagedown();
1367 else
1368 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 break;
1370
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001371 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 case K_PAGEDOWN:
1373 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001374#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001375 if (pum_visible())
1376 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 ins_pagedown();
1379 break;
1380
1381#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 ins_drop();
1384 break;
1385#endif
1386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 c = TAB;
1389 /* FALLTHROUGH */
1390
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1393 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1394 goto docomplete;
1395#endif
1396 inserted_space = FALSE;
1397 if (ins_tab())
1398 goto normalchar; /* insert TAB as a normal char */
1399 auto_format(FALSE, TRUE);
1400 break;
1401
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001402 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 c = CAR;
1404 /* FALLTHROUGH */
1405 case CAR:
1406 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001407#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /* In a quickfix window a <CR> jumps to the error under the
1409 * cursor. */
1410 if (bt_quickfix(curbuf) && c == CAR)
1411 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001412 if (curwin->w_llist_ref == NULL) /* quickfix window */
1413 do_cmdline_cmd((char_u *)".cc");
1414 else /* location list window */
1415 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 break;
1417 }
1418#endif
1419#ifdef FEAT_CMDWIN
1420 if (cmdwin_type != 0)
1421 {
1422 /* Execute the command in the cmdline window. */
1423 cmdwin_result = CAR;
1424 goto doESCkey;
1425 }
1426#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001427#ifdef FEAT_JOB_CHANNEL
1428 if (bt_prompt(curbuf))
1429 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001430 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001431 if (!bt_prompt(curbuf))
1432 // buffer changed to a non-prompt buffer, get out of
1433 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001434 goto doESCkey;
1435 break;
1436 }
1437#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001438 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 goto doESCkey; /* out of memory */
1440 auto_format(FALSE, FALSE);
1441 inserted_space = FALSE;
1442 break;
1443
Bram Moolenaar860cae12010-06-05 23:22:07 +02001444#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001445 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446# ifdef FEAT_INS_EXPAND
1447 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1448 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449 if (has_compl_option(TRUE))
1450 goto docomplete;
1451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453# endif
1454# ifdef FEAT_DIGRAPHS
1455 c = ins_digraph();
1456 if (c == NUL)
1457 break;
1458# endif
1459 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001463 case Ctrl_X: /* Enter CTRL-X mode */
1464 ins_ctrl_x();
1465 break;
1466
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001467 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (ctrl_x_mode != CTRL_X_TAGS)
1469 goto normalchar;
1470 goto docomplete;
1471
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001472 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (ctrl_x_mode != CTRL_X_FILES)
1474 goto normalchar;
1475 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001476
1477 case 's': /* Spelling completion after ^X */
1478 case Ctrl_S:
1479 if (ctrl_x_mode != CTRL_X_SPELL)
1480 goto normalchar;
1481 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#endif
1483
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001484 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#ifdef FEAT_INS_EXPAND
1486 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1487#endif
1488 {
1489 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1490 if (p_im)
1491 {
1492 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1493 break;
1494 goto doESCkey;
1495 }
1496 goto normalchar;
1497 }
1498#ifdef FEAT_INS_EXPAND
1499 /* FALLTHROUGH */
1500
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001501 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 case Ctrl_N:
1503 /* if 'complete' is empty then plain ^P is no longer special,
1504 * but it is under other ^X modes */
1505 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001506 && (ctrl_x_mode == CTRL_X_NORMAL
1507 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001508 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 goto normalchar;
1510
1511docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001512 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001513#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001514 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001515#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001516 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001517 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001518#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001519 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001520#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001521 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 break;
1523#endif /* FEAT_INS_EXPAND */
1524
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001525 case Ctrl_Y: /* copy from previous line or scroll down */
1526 case Ctrl_E: /* copy from next line or scroll up */
1527 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 break;
1529
1530 default:
1531#ifdef UNIX
1532 if (c == intr_char) /* special interrupt char */
1533 goto do_intr;
1534#endif
1535
Bram Moolenaare659c952011-05-19 17:25:41 +02001536normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001538 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001540#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001541 if (!p_paste)
1542 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001543 /* Trigger InsertCharPre. */
1544 char_u *str = do_insert_char_pre(c);
1545 char_u *p;
1546
1547 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001548 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001549 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001550 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001551 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001552 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001553 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001554 c = PTR2CHAR(p);
1555 if (c == CAR || c == K_KENTER || c == NL)
1556 ins_eol(c);
1557 else
1558 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001559 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001560 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001561 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001562 vim_free(str);
1563 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001564 }
1565
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001566 /* If the new value is already inserted or an empty string
1567 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001568 if (c == NUL)
1569 break;
1570 }
1571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572#ifdef FEAT_SMARTINDENT
1573 /* Try to perform smart-indenting. */
1574 ins_try_si(c);
1575#endif
1576
1577 if (c == ' ')
1578 {
1579 inserted_space = TRUE;
1580#ifdef FEAT_CINDENT
1581 if (inindent(0))
1582 can_cindent = FALSE;
1583#endif
1584 if (Insstart_blank_vcol == MAXCOL
1585 && curwin->w_cursor.lnum == Insstart.lnum)
1586 Insstart_blank_vcol = get_nolist_virtcol();
1587 }
1588
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001589 /* Insert a normal character and check for abbreviations on a
1590 * special character. Let CTRL-] expand abbreviations without
1591 * inserting it. */
1592 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#ifdef FEAT_MBYTE
1594 /* Add ABBR_OFF for characters above 0x100, this is
1595 * what check_abbr() expects. */
1596 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1597#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001598 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 insert_special(c, FALSE, FALSE);
1601#ifdef FEAT_RIGHTLEFT
1602 revins_legal++;
1603 revins_chars++;
1604#endif
1605 }
1606
1607 auto_format(FALSE, TRUE);
1608
1609#ifdef FEAT_FOLDING
1610 /* When inserting a character the cursor line must never be in a
1611 * closed fold. */
1612 foldOpenCursor();
1613#endif
1614 break;
1615 } /* end of switch (c) */
1616
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001617 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001618 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001619#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001620 /* but not in CTRL-X mode, a script can't restore the state */
1621 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001622#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001623 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001624 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 /* If the cursor was moved we didn't just insert a space */
1627 if (arrow_used)
1628 inserted_space = FALSE;
1629
1630#ifdef FEAT_CINDENT
1631 if (can_cindent && cindent_on()
1632# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001633 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634# endif
1635 )
1636 {
1637force_cindent:
1638 /*
1639 * Indent now if a key was typed that is in 'cinkeys'.
1640 */
1641 if (in_cinkeys(c, ' ', line_is_white))
1642 {
1643 if (stop_arrow() == OK)
1644 /* re-indent the current line */
1645 do_c_expr_indent();
1646 }
1647 }
1648#endif /* FEAT_CINDENT */
1649
1650 } /* for (;;) */
1651 /* NOTREACHED */
1652}
1653
1654/*
1655 * Redraw for Insert mode.
1656 * This is postponed until getting the next character to make '$' in the 'cpo'
1657 * option work correctly.
1658 * Only redraw when there are no characters available. This speeds up
1659 * inserting sequences of characters (e.g., for CTRL-R).
1660 */
1661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662ins_redraw(
1663 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001665#ifdef FEAT_CONCEAL
1666 linenr_T conceal_old_cursor_line = 0;
1667 linenr_T conceal_new_cursor_line = 0;
1668 int conceal_update_lines = FALSE;
1669#endif
1670
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 if (char_avail())
1672 return;
1673
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1676 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001677 if (ready && (has_cursormovedI()
1678# if defined(FEAT_CONCEAL)
1679 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001680# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001681 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001682 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001683# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001684 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001685# endif
1686 )
1687 {
1688# ifdef FEAT_SYN_HL
1689 /* Need to update the screen first, to make sure syntax
1690 * highlighting is correct after making a change (e.g., inserting
1691 * a "(". The autocommand may also require a redraw, so it's done
1692 * again below, unfortunately. */
1693 if (syntax_present(curwin) && must_redraw)
1694 update_screen(0);
1695# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001696 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001697 {
1698 /* Make sure curswant is correct, an autocommand may call
1699 * getcurpos(). */
1700 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001701 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001702 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001703# ifdef FEAT_CONCEAL
1704 if (curwin->w_p_cole > 0)
1705 {
1706 conceal_old_cursor_line = last_cursormoved.lnum;
1707 conceal_new_cursor_line = curwin->w_cursor.lnum;
1708 conceal_update_lines = TRUE;
1709 }
1710# endif
1711 last_cursormoved = curwin->w_cursor;
1712 }
1713#endif
1714
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001715 /* Trigger TextChangedI if b_changedtick differs. */
1716 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001717 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001718#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001719 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001721 )
1722 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001723 aco_save_T aco;
1724
1725 // save and restore curwin and curbuf, in case the autocmd changes them
1726 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001727 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001728 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001729 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001731
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001732#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001733 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1734 * TextChangedI will need to trigger for backwards compatibility, thus use
1735 * different b_last_changedtick* variables. */
1736 if (ready && has_textchangedP()
1737 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1738 && pum_visible())
1739 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001740 aco_save_T aco;
1741
1742 // save and restore curwin and curbuf, in case the autocmd changes them
1743 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001744 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001745 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001746 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1747 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001748#endif
1749
1750 if (must_redraw)
1751 update_screen(0);
1752 else if (clear_cmdline || redraw_cmdline)
1753 showmode(); /* clear cmdline and show mode */
1754# if defined(FEAT_CONCEAL)
1755 if ((conceal_update_lines
1756 && (conceal_old_cursor_line != conceal_new_cursor_line
1757 || conceal_cursor_line(curwin)))
1758 || need_cursor_line_redraw)
1759 {
1760 if (conceal_old_cursor_line != conceal_new_cursor_line)
1761 update_single_line(curwin, conceal_old_cursor_line);
1762 update_single_line(curwin, conceal_new_cursor_line == 0
1763 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1764 curwin->w_valid &= ~VALID_CROW;
1765 }
1766# endif
1767 showruler(FALSE);
1768 setcursor();
1769 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770}
1771
1772/*
1773 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1774 */
1775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001779 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001782 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001787 did_putchar = TRUE;
1788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1790
1791#ifdef FEAT_CMDL_INFO
1792 add_to_showcmd_c(Ctrl_V);
1793#endif
1794
1795 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001796 if (did_putchar)
1797 /* when the line fits in 'columns' the '^' is at the start of the next
1798 * line and will not removed by the redraw */
1799 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#ifdef FEAT_CMDL_INFO
1801 clear_showcmd();
1802#endif
1803 insert_special(c, FALSE, TRUE);
1804#ifdef FEAT_RIGHTLEFT
1805 revins_chars++;
1806 revins_legal++;
1807#endif
1808}
1809
1810/*
1811 * Put a character directly onto the screen. It's not stored in a buffer.
1812 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1813 */
1814static int pc_status;
1815#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1816#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1817#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1818#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820static int pc_attr;
1821static int pc_row;
1822static int pc_col;
1823
1824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001825edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 int attr;
1828
1829 if (ScreenLines != NULL)
1830 {
1831 update_topline(); /* just in case w_topline isn't valid */
1832 validate_cursor();
1833 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001834 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 else
1836 attr = 0;
1837 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001838 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1840 pc_status = PC_STATUS_UNSET;
1841#endif
1842#ifdef FEAT_RIGHTLEFT
1843 if (curwin->w_p_rl)
1844 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001845 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846# ifdef FEAT_MBYTE
1847 if (has_mbyte)
1848 {
1849 int fix_col = mb_fix_col(pc_col, pc_row);
1850
1851 if (fix_col != pc_col)
1852 {
1853 screen_putchar(' ', pc_row, fix_col, attr);
1854 --curwin->w_wcol;
1855 pc_status = PC_STATUS_RIGHT;
1856 }
1857 }
1858# endif
1859 }
1860 else
1861#endif
1862 {
1863 pc_col += curwin->w_wcol;
1864#ifdef FEAT_MBYTE
1865 if (mb_lefthalve(pc_row, pc_col))
1866 pc_status = PC_STATUS_LEFT;
1867#endif
1868 }
1869
1870 /* save the character to be able to put it back */
1871#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1872 if (pc_status == PC_STATUS_UNSET)
1873#endif
1874 {
1875 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1876 pc_status = PC_STATUS_SET;
1877 }
1878 screen_putchar(c, pc_row, pc_col, attr);
1879 }
1880}
1881
Bram Moolenaarf2732452018-06-03 14:47:35 +02001882#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1883/*
1884 * Return the effective prompt for the current buffer.
1885 */
1886 char_u *
1887prompt_text(void)
1888{
1889 if (curbuf->b_prompt_text == NULL)
1890 return (char_u *)"% ";
1891 return curbuf->b_prompt_text;
1892}
1893
1894/*
1895 * Prepare for prompt mode: Make sure the last line has the prompt text.
1896 * Move the cursor to this line.
1897 */
1898 static void
1899init_prompt(int cmdchar_todo)
1900{
1901 char_u *prompt = prompt_text();
1902 char_u *text;
1903
1904 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1905 text = ml_get_curline();
1906 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1907 {
1908 // prompt is missing, insert it or append a line with it
1909 if (*text == NUL)
1910 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1911 else
1912 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1913 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1914 coladvance((colnr_T)MAXCOL);
1915 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1916 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001917
1918 // Insert always starts after the prompt, allow editing text after it.
1919 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1920 || Insstart_orig.col != (int)STRLEN(prompt))
1921 {
1922 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001923 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001924 Insstart_orig = Insstart;
1925 Insstart_textlen = Insstart.col;
1926 Insstart_blank_vcol = MAXCOL;
1927 arrow_used = FALSE;
1928 }
1929
Bram Moolenaarf2732452018-06-03 14:47:35 +02001930 if (cmdchar_todo == 'A')
1931 coladvance((colnr_T)MAXCOL);
1932 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001933 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001934 /* Make sure the cursor is in a valid position. */
1935 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001936}
1937
1938/*
1939 * Return TRUE if the cursor is in the editable position of the prompt line.
1940 */
1941 int
1942prompt_curpos_editable()
1943{
1944 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1945 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1946}
1947#endif
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949/*
1950 * Undo the previous edit_putchar().
1951 */
1952 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001953edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
1955 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1956 {
1957#if defined(FEAT_MBYTE)
1958 if (pc_status == PC_STATUS_RIGHT)
1959 ++curwin->w_wcol;
1960 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1961 redrawWinline(curwin->w_cursor.lnum, FALSE);
1962 else
1963#endif
1964 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1965 }
1966}
1967
1968/*
1969 * Called when p_dollar is set: display a '$' at the end of the changed text
1970 * Only works when cursor is in the line that changes.
1971 */
1972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001973display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 colnr_T save_col;
1976
1977 if (!redrawing())
1978 return;
1979
1980 cursor_off();
1981 save_col = curwin->w_cursor.col;
1982 curwin->w_cursor.col = col;
1983#ifdef FEAT_MBYTE
1984 if (has_mbyte)
1985 {
1986 char_u *p;
1987
1988 /* If on the last byte of a multi-byte move to the first byte. */
1989 p = ml_get_curline();
1990 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1991 }
1992#endif
1993 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001994 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
1996 edit_putchar('$', FALSE);
1997 dollar_vcol = curwin->w_virtcol;
1998 }
1999 curwin->w_cursor.col = save_col;
2000}
2001
2002/*
2003 * Call this function before moving the cursor from the normal insert position
2004 * in insert mode.
2005 */
2006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002007undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002009 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002011 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 redrawWinline(curwin->w_cursor.lnum, FALSE);
2013 }
2014}
2015
2016/*
2017 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2018 * Keep the cursor on the same character.
2019 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2020 * type == INDENT_DEC decrease indent (for CTRL-D)
2021 * type == INDENT_SET set indent to "amount"
2022 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2023 */
2024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002025change_indent(
2026 int type,
2027 int amount,
2028 int round,
2029 int replaced, /* replaced character, put on replace stack */
2030 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 int vcol;
2033 int last_vcol;
2034 int insstart_less; /* reduction for Insstart.col */
2035 int new_cursor_col;
2036 int i;
2037 char_u *ptr;
2038 int save_p_list;
2039 int start_col;
2040 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 colnr_T orig_col = 0; /* init for GCC */
2042 char_u *new_line, *orig_line = NULL; /* init for GCC */
2043
2044 /* VREPLACE mode needs to know what the line was like before changing */
2045 if (State & VREPLACE_FLAG)
2046 {
2047 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2048 orig_col = curwin->w_cursor.col;
2049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 /* for the following tricks we don't want list mode */
2052 save_p_list = curwin->w_p_list;
2053 curwin->w_p_list = FALSE;
2054 vc = getvcol_nolist(&curwin->w_cursor);
2055 vcol = vc;
2056
2057 /*
2058 * For Replace mode we need to fix the replace stack later, which is only
2059 * possible when the cursor is in the indent. Remember the number of
2060 * characters before the cursor if it's possible.
2061 */
2062 start_col = curwin->w_cursor.col;
2063
2064 /* determine offset from first non-blank */
2065 new_cursor_col = curwin->w_cursor.col;
2066 beginline(BL_WHITE);
2067 new_cursor_col -= curwin->w_cursor.col;
2068
2069 insstart_less = curwin->w_cursor.col;
2070
2071 /*
2072 * If the cursor is in the indent, compute how many screen columns the
2073 * cursor is to the left of the first non-blank.
2074 */
2075 if (new_cursor_col < 0)
2076 vcol = get_indent() - vcol;
2077
2078 if (new_cursor_col > 0) /* can't fix replace stack */
2079 start_col = -1;
2080
2081 /*
2082 * Set the new indent. The cursor will be put on the first non-blank.
2083 */
2084 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002085 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 else
2087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 int save_State = State;
2089
2090 /* Avoid being called recursively. */
2091 if (State & VREPLACE_FLAG)
2092 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002093 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096 insstart_less -= curwin->w_cursor.col;
2097
2098 /*
2099 * Try to put cursor on same character.
2100 * If the cursor is at or after the first non-blank in the line,
2101 * compute the cursor column relative to the column of the first
2102 * non-blank character.
2103 * If we are not in insert mode, leave the cursor on the first non-blank.
2104 * If the cursor is before the first non-blank, position it relative
2105 * to the first non-blank, counted in screen columns.
2106 */
2107 if (new_cursor_col >= 0)
2108 {
2109 /*
2110 * When changing the indent while the cursor is touching it, reset
2111 * Insstart_col to 0.
2112 */
2113 if (new_cursor_col == 0)
2114 insstart_less = MAXCOL;
2115 new_cursor_col += curwin->w_cursor.col;
2116 }
2117 else if (!(State & INSERT))
2118 new_cursor_col = curwin->w_cursor.col;
2119 else
2120 {
2121 /*
2122 * Compute the screen column where the cursor should be.
2123 */
2124 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002125 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
2127 /*
2128 * Advance the cursor until we reach the right screen column.
2129 */
2130 vcol = last_vcol = 0;
2131 new_cursor_col = -1;
2132 ptr = ml_get_curline();
2133 while (vcol <= (int)curwin->w_virtcol)
2134 {
2135 last_vcol = vcol;
2136#ifdef FEAT_MBYTE
2137 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002138 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 else
2140#endif
2141 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002142 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144 vcol = last_vcol;
2145
2146 /*
2147 * May need to insert spaces to be able to position the cursor on
2148 * the right screen column.
2149 */
2150 if (vcol != (int)curwin->w_virtcol)
2151 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002152 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002154 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (ptr != NULL)
2156 {
2157 new_cursor_col += i;
2158 ptr[i] = NUL;
2159 while (--i >= 0)
2160 ptr[i] = ' ';
2161 ins_str(ptr);
2162 vim_free(ptr);
2163 }
2164 }
2165
2166 /*
2167 * When changing the indent while the cursor is in it, reset
2168 * Insstart_col to 0.
2169 */
2170 insstart_less = MAXCOL;
2171 }
2172
2173 curwin->w_p_list = save_p_list;
2174
2175 if (new_cursor_col <= 0)
2176 curwin->w_cursor.col = 0;
2177 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002178 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 curwin->w_set_curswant = TRUE;
2180 changed_cline_bef_curs();
2181
2182 /*
2183 * May have to adjust the start of the insert.
2184 */
2185 if (State & INSERT)
2186 {
2187 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2188 {
2189 if ((int)Insstart.col <= insstart_less)
2190 Insstart.col = 0;
2191 else
2192 Insstart.col -= insstart_less;
2193 }
2194 if ((int)ai_col <= insstart_less)
2195 ai_col = 0;
2196 else
2197 ai_col -= insstart_less;
2198 }
2199
2200 /*
2201 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2202 * If the number of characters before the cursor decreased, need to pop a
2203 * few characters from the replace stack.
2204 * If the number of characters before the cursor increased, need to push a
2205 * few NULs onto the replace stack.
2206 */
2207 if (REPLACE_NORMAL(State) && start_col >= 0)
2208 {
2209 while (start_col > (int)curwin->w_cursor.col)
2210 {
2211 replace_join(0); /* remove a NUL from the replace stack */
2212 --start_col;
2213 }
2214 while (start_col < (int)curwin->w_cursor.col || replaced)
2215 {
2216 replace_push(NUL);
2217 if (replaced)
2218 {
2219 replace_push(replaced);
2220 replaced = NUL;
2221 }
2222 ++start_col;
2223 }
2224 }
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 /*
2227 * For VREPLACE mode, we also have to fix the replace stack. In this case
2228 * it is always possible because we backspace over the whole line and then
2229 * put it back again the way we wanted it.
2230 */
2231 if (State & VREPLACE_FLAG)
2232 {
2233 /* If orig_line didn't allocate, just return. At least we did the job,
2234 * even if you can't backspace. */
2235 if (orig_line == NULL)
2236 return;
2237
2238 /* Save new line */
2239 new_line = vim_strsave(ml_get_curline());
2240 if (new_line == NULL)
2241 return;
2242
2243 /* We only put back the new line up to the cursor */
2244 new_line[curwin->w_cursor.col] = NUL;
2245
2246 /* Put back original line */
2247 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2248 curwin->w_cursor.col = orig_col;
2249
2250 /* Backspace from cursor to start of line */
2251 backspace_until_column(0);
2252
2253 /* Insert new stuff into line again */
2254 ins_bytes(new_line);
2255
2256 vim_free(new_line);
2257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258}
2259
2260/*
2261 * Truncate the space at the end of a line. This is to be used only in an
2262 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2263 * modes.
2264 */
2265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002266truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 int i;
2269
2270 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002271 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 if (State & REPLACE_FLAG)
2274 replace_join(0); /* remove a NUL from the replace stack */
2275 }
2276 line[i + 1] = NUL;
2277}
2278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279/*
2280 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2281 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002282 * Will attempt not to go before "col" even when there is a composing
2283 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 */
2285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002286backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 while ((int)curwin->w_cursor.col > col)
2289 {
2290 curwin->w_cursor.col--;
2291 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002292 replace_do_bs(col);
2293 else if (!del_char_after_col(col))
2294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002298/*
2299 * Like del_char(), but make sure not to go before column "limit_col".
2300 * Only matters when there are composing characters.
2301 * Return TRUE when something was deleted.
2302 */
2303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002304del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002305{
2306#ifdef FEAT_MBYTE
2307 if (enc_utf8 && limit_col >= 0)
2308 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002309 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002310
2311 /* Make sure the cursor is at the start of a character, but
2312 * skip forward again when going too far back because of a
2313 * composing character. */
2314 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002315 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002316 {
2317 int l = utf_ptr2len(ml_get_cursor());
2318
2319 if (l == 0) /* end of line */
2320 break;
2321 curwin->w_cursor.col += l;
2322 }
2323 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2324 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002325 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002326 }
2327 else
2328#endif
2329 (void)del_char(FALSE);
2330 return TRUE;
2331}
2332
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2334/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002335 * CTRL-X pressed in Insert mode.
2336 */
2337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002338ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002339{
2340 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2341 * CTRL-V works like CTRL-N */
2342 if (ctrl_x_mode != CTRL_X_CMDLINE)
2343 {
2344 /* if the next ^X<> won't ADD nothing, then reset
2345 * compl_cont_status */
2346 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002347 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002348 else
2349 compl_cont_status = 0;
2350 /* We're not sure which CTRL-X mode it will be yet */
2351 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2352 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2353 edit_submode_pre = NULL;
2354 showmode();
2355 }
2356}
2357
2358/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002359 * Whether other than default completion has been selected.
2360 */
2361 int
2362ctrl_x_mode_not_default(void)
2363{
2364 return ctrl_x_mode != CTRL_X_NORMAL;
2365}
2366
2367/*
2368 * Whether CTRL-X was typed without a following character.
2369 */
2370 int
2371ctrl_x_mode_not_defined_yet(void)
2372{
2373 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2374}
2375
2376/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002377 * Return TRUE if the 'dict' or 'tsr' option can be used.
2378 */
2379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002380has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002381{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002382 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002383# ifdef FEAT_SPELL
2384 && !curwin->w_p_spell
2385# endif
2386 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002387 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2388 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002389 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002390 edit_submode = NULL;
2391 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2392 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002393 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002394 if (emsg_silent == 0)
2395 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002396 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002397 setcursor();
2398 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002399#ifdef FEAT_EVAL
2400 if (!get_vim_var_nr(VV_TESTING))
2401#endif
2402 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002403 }
2404 return FALSE;
2405 }
2406 return TRUE;
2407}
2408
2409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2411 * This depends on the current mode.
2412 */
2413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002414vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 /* Always allow ^R - let it's results then be checked */
2417 if (c == Ctrl_R)
2418 return TRUE;
2419
Bram Moolenaare3226be2005-12-18 22:10:00 +00002420 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002421 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002422 return TRUE;
2423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 switch (ctrl_x_mode)
2425 {
2426 case 0: /* Not in any CTRL-X mode */
2427 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2428 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002429 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2431 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2432 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002433 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002434 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CTRL_X_SCROLL:
2436 return (c == Ctrl_Y || c == Ctrl_E);
2437 case CTRL_X_WHOLE_LINE:
2438 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2439 case CTRL_X_FILES:
2440 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2441 case CTRL_X_DICTIONARY:
2442 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2443 case CTRL_X_THESAURUS:
2444 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2445 case CTRL_X_TAGS:
2446 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2447#ifdef FEAT_FIND_ID
2448 case CTRL_X_PATH_PATTERNS:
2449 return (c == Ctrl_P || c == Ctrl_N);
2450 case CTRL_X_PATH_DEFINES:
2451 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2452#endif
2453 case CTRL_X_CMDLINE:
2454 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2455 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002456#ifdef FEAT_COMPL_FUNC
2457 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002458 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002459 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002460 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002461#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002462 case CTRL_X_SPELL:
2463 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002464 case CTRL_X_EVAL:
2465 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002467 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 return FALSE;
2469}
2470
2471/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002472 * Return TRUE when character "c" is part of the item currently being
2473 * completed. Used to decide whether to abandon complete mode when the menu
2474 * is visible.
2475 */
2476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002477ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002478{
2479 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2480 /* When expanding an identifier only accept identifier chars. */
2481 return vim_isIDc(c);
2482
2483 switch (ctrl_x_mode)
2484 {
2485 case CTRL_X_FILES:
2486 /* When expanding file name only accept file name chars. But not
2487 * path separators, so that "proto/<Tab>" expands files in
2488 * "proto", not "proto/" as a whole */
2489 return vim_isfilec(c) && !vim_ispathsep(c);
2490
2491 case CTRL_X_CMDLINE:
2492 case CTRL_X_OMNI:
2493 /* Command line and Omni completion can work with just about any
2494 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002495 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002496
2497 case CTRL_X_WHOLE_LINE:
2498 /* For while line completion a space can be part of the line. */
2499 return vim_isprintc(c);
2500 }
2501 return vim_iswordc(c);
2502}
2503
2504/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002505 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002507 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 * the rest of the word to be in -- webb
2509 */
2510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002511ins_compl_add_infercase(
2512 char_u *str,
2513 int len,
2514 int icase,
2515 char_u *fname,
2516 int dir,
2517 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002519 char_u *p;
2520 int i, c;
2521 int actual_len; /* Take multi-byte characters */
2522 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002523 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002524 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 int has_lower = FALSE;
2526 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002528 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002530 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
Bram Moolenaara2993e12007-08-12 14:38:46 +00002532 /* Find actual length of completion. */
2533#ifdef FEAT_MBYTE
2534 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002536 p = str;
2537 actual_len = 0;
2538 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002540 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002541 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002544 else
2545#endif
2546 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
Bram Moolenaara2993e12007-08-12 14:38:46 +00002548 /* Find actual length of original text. */
2549#ifdef FEAT_MBYTE
2550 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002552 p = compl_orig_text;
2553 actual_compl_length = 0;
2554 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002556 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002557 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
2559 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002560 else
2561#endif
2562 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002564 /* "actual_len" may be smaller than "actual_compl_length" when using
2565 * thesaurus, only use the minimum when comparing. */
2566 min_len = actual_len < actual_compl_length
2567 ? actual_len : actual_compl_length;
2568
Bram Moolenaara2993e12007-08-12 14:38:46 +00002569 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002570 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002571 if (wca != NULL)
2572 {
2573 p = str;
2574 for (i = 0; i < actual_len; ++i)
2575#ifdef FEAT_MBYTE
2576 if (has_mbyte)
2577 wca[i] = mb_ptr2char_adv(&p);
2578 else
2579#endif
2580 wca[i] = *(p++);
2581
2582 /* Rule 1: Were any chars converted to lower? */
2583 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002584 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002585 {
2586#ifdef FEAT_MBYTE
2587 if (has_mbyte)
2588 c = mb_ptr2char_adv(&p);
2589 else
2590#endif
2591 c = *(p++);
2592 if (MB_ISLOWER(c))
2593 {
2594 has_lower = TRUE;
2595 if (MB_ISUPPER(wca[i]))
2596 {
2597 /* Rule 1 is satisfied. */
2598 for (i = actual_compl_length; i < actual_len; ++i)
2599 wca[i] = MB_TOLOWER(wca[i]);
2600 break;
2601 }
2602 }
2603 }
2604
2605 /*
2606 * Rule 2: No lower case, 2nd consecutive letter converted to
2607 * upper case.
2608 */
2609 if (!has_lower)
2610 {
2611 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002612 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002613 {
2614#ifdef FEAT_MBYTE
2615 if (has_mbyte)
2616 c = mb_ptr2char_adv(&p);
2617 else
2618#endif
2619 c = *(p++);
2620 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2621 {
2622 /* Rule 2 is satisfied. */
2623 for (i = actual_compl_length; i < actual_len; ++i)
2624 wca[i] = MB_TOUPPER(wca[i]);
2625 break;
2626 }
2627 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2628 }
2629 }
2630
2631 /* Copy the original case of the part we typed. */
2632 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002633 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002634 {
2635#ifdef FEAT_MBYTE
2636 if (has_mbyte)
2637 c = mb_ptr2char_adv(&p);
2638 else
2639#endif
2640 c = *(p++);
2641 if (MB_ISLOWER(c))
2642 wca[i] = MB_TOLOWER(wca[i]);
2643 else if (MB_ISUPPER(c))
2644 wca[i] = MB_TOUPPER(wca[i]);
2645 }
2646
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002647 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002648 * Generate encoding specific output from wide character array.
2649 * Multi-byte characters can occupy up to five bytes more than
2650 * ASCII characters, and we also need one byte for NUL, so stay
2651 * six bytes away from the edge of IObuff.
2652 */
2653 p = IObuff;
2654 i = 0;
2655 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2656#ifdef FEAT_MBYTE
2657 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002658 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002659 else
2660#endif
2661 *(p++) = wca[i++];
2662 *p = NUL;
2663
2664 vim_free(wca);
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002667 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2668 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002670 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671}
2672
2673/*
2674 * Add a match to the list of matches.
2675 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002676 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002677 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002679 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002680ins_compl_add(
2681 char_u *str,
2682 int len,
2683 int icase,
2684 char_u *fname,
2685 char_u **cptext, /* extra text for popup menu or NULL */
2686 int cdir,
2687 int flags,
2688 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002690 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002691 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 ui_breakcheck();
2694 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002695 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (len < 0)
2697 len = (int)STRLEN(str);
2698
2699 /*
2700 * If the same match is already present, don't add it.
2701 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002704 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 do
2706 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002707 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002708 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002709 && match->cp_str[len] == NUL)
2710 return NOTDONE;
2711 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002712 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002715 /* Remove any popup menu before changing the list of matches. */
2716 ins_compl_del_pum();
2717
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 /*
2719 * Allocate a new match structure.
2720 * Copy the values to the new match structure.
2721 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002722 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002724 return FAIL;
2725 match->cp_number = -1;
2726 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002727 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002728 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
2730 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002731 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002733 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002736 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2738 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002739 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002740 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002741 && compl_curr_match->cp_fname != NULL
2742 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002743 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002744 else if (fname != NULL)
2745 {
2746 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002747 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002750 match->cp_fname = NULL;
2751 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002752
2753 if (cptext != NULL)
2754 {
2755 int i;
2756
2757 for (i = 0; i < CPT_COUNT; ++i)
2758 if (cptext[i] != NULL && *cptext[i] != NUL)
2759 match->cp_text[i] = vim_strsave(cptext[i]);
2760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 /*
2763 * Link the new match structure in the list of matches.
2764 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002765 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002766 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 else if (dir == FORWARD)
2768 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002769 match->cp_next = compl_curr_match->cp_next;
2770 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772 else /* BACKWARD */
2773 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002774 match->cp_next = compl_curr_match;
2775 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002777 if (match->cp_next)
2778 match->cp_next->cp_prev = match;
2779 if (match->cp_prev)
2780 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002782 compl_first_match = match;
2783 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002785 /*
2786 * Find the longest common string if still doing that.
2787 */
2788 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2789 ins_compl_longest_match(match);
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 return OK;
2792}
2793
2794/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002795 * Return TRUE if "str[len]" matches with match->cp_str, considering
2796 * match->cp_icase.
2797 */
2798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002799ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002800{
2801 if (match->cp_icase)
2802 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2803 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2804}
2805
2806/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002807 * Reduce the longest common string for match "match".
2808 */
2809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002810ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002811{
2812 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002813 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002814 int had_match;
2815
2816 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002817 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002818 /* First match, use it as a whole. */
2819 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002820 if (compl_leader != NULL)
2821 {
2822 had_match = (curwin->w_cursor.col > compl_col);
2823 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002824 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002825 ins_redraw(FALSE);
2826
2827 /* When the match isn't there (to avoid matching itself) remove it
2828 * again after redrawing. */
2829 if (!had_match)
2830 ins_compl_delete();
2831 compl_used_match = FALSE;
2832 }
2833 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002834 else
2835 {
2836 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002837 p = compl_leader;
2838 s = match->cp_str;
2839 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002840 {
2841#ifdef FEAT_MBYTE
2842 if (has_mbyte)
2843 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002844 c1 = mb_ptr2char(p);
2845 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002846 }
2847 else
2848#endif
2849 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002850 c1 = *p;
2851 c2 = *s;
2852 }
2853 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2854 : (c1 != c2))
2855 break;
2856#ifdef FEAT_MBYTE
2857 if (has_mbyte)
2858 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002859 MB_PTR_ADV(p);
2860 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002861 }
2862 else
2863#endif
2864 {
2865 ++p;
2866 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002867 }
2868 }
2869
2870 if (*p != NUL)
2871 {
2872 /* Leader was shortened, need to change the inserted text. */
2873 *p = NUL;
2874 had_match = (curwin->w_cursor.col > compl_col);
2875 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002876 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002877 ins_redraw(FALSE);
2878
2879 /* When the match isn't there (to avoid matching itself) remove it
2880 * again after redrawing. */
2881 if (!had_match)
2882 ins_compl_delete();
2883 }
2884
2885 compl_used_match = FALSE;
2886 }
2887}
2888
2889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 * Add an array of matches to the list of matches.
2891 * Frees matches[].
2892 */
2893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002894ins_compl_add_matches(
2895 int num_matches,
2896 char_u **matches,
2897 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
2899 int i;
2900 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002901 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
Bram Moolenaar572cb562005-08-05 21:35:02 +00002903 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002904 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002905 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002907 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 FreeWild(num_matches, matches);
2909}
2910
2911/* Make the completion list cyclic.
2912 * Return the number of matches (excluding the original).
2913 */
2914 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002915ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002917 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 int count = 0;
2919
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002920 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 /*
2923 * Find the end of the list.
2924 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002925 match = compl_first_match;
2926 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002927 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002929 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 ++count;
2931 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002932 match->cp_next = compl_first_match;
2933 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 }
2935 return count;
2936}
2937
Bram Moolenaara94bc432006-03-10 21:42:59 +00002938/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002939 * Set variables that store noselect and noinsert behavior from the
2940 * 'completeopt' value.
2941 */
2942 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002943completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002944{
2945 compl_no_insert = FALSE;
2946 compl_no_select = FALSE;
2947 if (strstr((char *)p_cot, "noselect") != NULL)
2948 compl_no_select = TRUE;
2949 if (strstr((char *)p_cot, "noinsert") != NULL)
2950 compl_no_insert = TRUE;
2951}
2952
2953/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002954 * Start completion for the complete() function.
2955 * "startcol" is where the matched text starts (1 is first column).
2956 * "list" is the list of matches.
2957 */
2958 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002959set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002960{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002961 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002962 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002963
Bram Moolenaara94bc432006-03-10 21:42:59 +00002964 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002965 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002966 ins_compl_prep(' ');
2967 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002968 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002969
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002970 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002971 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002972 startcol = curwin->w_cursor.col;
2973 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002974 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002975 /* compl_pattern doesn't need to be set */
2976 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2977 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002978 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002979 return;
2980
Bram Moolenaare4214502015-03-05 18:08:43 +01002981 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002982
2983 ins_compl_add_list(list);
2984 compl_matches = ins_compl_make_cyclic();
2985 compl_started = TRUE;
2986 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002987 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002988
2989 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002990 if (compl_no_insert || compl_no_select)
2991 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002992 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002993 if (compl_no_select)
2994 /* Down/Up has no real effect. */
2995 ins_complete(K_UP, FALSE);
2996 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002997 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002998 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002999 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003000
3001 /* Lazily show the popup menu, unless we got interrupted. */
3002 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003003 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003004 out_flush();
3005}
3006
3007
Bram Moolenaar9372a112005-12-06 19:59:18 +00003008/* "compl_match_array" points the currently displayed list of entries in the
3009 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003010static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003011static int compl_match_arraysize;
3012
3013/*
3014 * Update the screen and when there is any scrolling remove the popup menu.
3015 */
3016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003017ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003018{
3019 int h;
3020
3021 if (compl_match_array != NULL)
3022 {
3023 h = curwin->w_cline_height;
3024 update_screen(0);
3025 if (h != curwin->w_cline_height)
3026 ins_compl_del_pum();
3027 }
3028}
3029
3030/*
3031 * Remove any popup menu.
3032 */
3033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003034ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003035{
3036 if (compl_match_array != NULL)
3037 {
3038 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003039 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003040 }
3041}
3042
3043/*
3044 * Return TRUE if the popup menu should be displayed.
3045 */
3046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003047pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003048{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003049 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003050 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003051 return FALSE;
3052
3053 /* The display looks bad on a B&W display. */
3054 if (t_colors < 8
3055#ifdef FEAT_GUI
3056 && !gui.in_use
3057#endif
3058 )
3059 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003060 return TRUE;
3061}
3062
3063/*
3064 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003065 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003066 */
3067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003068pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003069{
3070 compl_T *compl;
3071 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003072
3073 /* Don't display the popup menu if there are no matches or there is only
3074 * one (ignoring the original text). */
3075 compl = compl_first_match;
3076 i = 0;
3077 do
3078 {
3079 if (compl == NULL
3080 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3081 break;
3082 compl = compl->cp_next;
3083 } while (compl != compl_first_match);
3084
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003085 if (strstr((char *)p_cot, "menuone") != NULL)
3086 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003087 return (i >= 2);
3088}
3089
3090/*
3091 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003092 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003093 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003095ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003096{
3097 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003098 compl_T *shown_compl = NULL;
3099 int did_find_shown_match = FALSE;
3100 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003101 int i;
3102 int cur = -1;
3103 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003104 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003105
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003106 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003107 return;
3108
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003109#if defined(FEAT_EVAL)
3110 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3111 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3112#endif
3113
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003114 /* Update the screen before drawing the popup menu over it. */
3115 update_screen(0);
3116
3117 if (compl_match_array == NULL)
3118 {
3119 /* Need to build the popup menu list. */
3120 compl_match_arraysize = 0;
3121 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003122 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003123 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003124 do
3125 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003126 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3127 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003128 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003129 ++compl_match_arraysize;
3130 compl = compl->cp_next;
3131 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003132 if (compl_match_arraysize == 0)
3133 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003134 compl_match_array = (pumitem_T *)alloc_clear(
3135 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003136 * compl_match_arraysize));
3137 if (compl_match_array != NULL)
3138 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003139 /* If the current match is the original text don't find the first
3140 * match after it, don't highlight anything. */
3141 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3142 shown_match_ok = TRUE;
3143
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003144 i = 0;
3145 compl = compl_first_match;
3146 do
3147 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003148 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3149 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003150 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003151 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003152 if (!shown_match_ok)
3153 {
3154 if (compl == compl_shown_match || did_find_shown_match)
3155 {
3156 /* This item is the shown match or this is the
3157 * first displayed item after the shown match. */
3158 compl_shown_match = compl;
3159 did_find_shown_match = TRUE;
3160 shown_match_ok = TRUE;
3161 }
3162 else
3163 /* Remember this displayed match for when the
3164 * shown match is just below it. */
3165 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003166 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003167 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003168
3169 if (compl->cp_text[CPT_ABBR] != NULL)
3170 compl_match_array[i].pum_text =
3171 compl->cp_text[CPT_ABBR];
3172 else
3173 compl_match_array[i].pum_text = compl->cp_str;
3174 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3175 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3176 if (compl->cp_text[CPT_MENU] != NULL)
3177 compl_match_array[i++].pum_extra =
3178 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003179 else
3180 compl_match_array[i++].pum_extra = compl->cp_fname;
3181 }
3182
3183 if (compl == compl_shown_match)
3184 {
3185 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003186
3187 /* When the original text is the shown match don't set
3188 * compl_shown_match. */
3189 if (compl->cp_flags & ORIGINAL_TEXT)
3190 shown_match_ok = TRUE;
3191
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003192 if (!shown_match_ok && shown_compl != NULL)
3193 {
3194 /* The shown match isn't displayed, set it to the
3195 * previously displayed match. */
3196 compl_shown_match = shown_compl;
3197 shown_match_ok = TRUE;
3198 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003199 }
3200 compl = compl->cp_next;
3201 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003202
3203 if (!shown_match_ok) /* no displayed match at all */
3204 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003205 }
3206 }
3207 else
3208 {
3209 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003210 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003211 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3212 || compl_match_array[i].pum_text
3213 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003214 {
3215 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003216 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003217 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003218 }
3219
3220 if (compl_match_array != NULL)
3221 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003222 /* In Replace mode when a $ is displayed at the end of the line only
3223 * part of the screen would be updated. We do need to redraw here. */
3224 dollar_vcol = -1;
3225
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003226 /* Compute the screen column of the start of the completed text.
3227 * Use the cursor to get all wrapping and other settings right. */
3228 col = curwin->w_cursor.col;
3229 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003230 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003231 curwin->w_cursor.col = col;
3232 }
3233}
3234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235#define DICT_FIRST (1) /* use just first element in "dict" */
3236#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003239 * Add any identifiers that match the given pattern in the list of dictionary
3240 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 */
3242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243ins_compl_dictionaries(
3244 char_u *dict_start,
3245 char_u *pat,
3246 int flags, /* DICT_FIRST and/or DICT_EXACT */
3247 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003249 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 char_u *ptr;
3251 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 char_u **files;
3254 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003256 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258 if (*dict == NUL)
3259 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003260#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003261 /* When 'dictionary' is empty and spell checking is enabled use
3262 * "spell". */
3263 if (!thesaurus && curwin->w_p_spell)
3264 dict = (char_u *)"spell";
3265 else
3266#endif
3267 return;
3268 }
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003271 if (buf == NULL)
3272 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003273 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 /* If 'infercase' is set, don't use 'smartcase' here */
3276 save_p_scs = p_scs;
3277 if (curbuf->b_p_inf)
3278 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003279
3280 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3281 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003282 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003283 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003284 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003285 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003286 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003287
3288 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003289 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003290 len = STRLEN(pat_esc) + 10;
3291 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003292 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003293 {
3294 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003295 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003296 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003297 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003298 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3299 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003300 vim_free(ptr);
3301 }
3302 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003303 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003304 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003305 if (regmatch.regprog == NULL)
3306 goto theend;
3307 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3310 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003311 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 /* copy one dictionary file name into buf */
3314 if (flags == DICT_EXACT)
3315 {
3316 count = 1;
3317 files = &dict;
3318 }
3319 else
3320 {
3321 /* Expand wildcards in the dictionary name, but do not allow
3322 * backticks (for security, the 'dict' option may have been set in
3323 * a modeline). */
3324 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003325# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003326 if (!thesaurus && STRCMP(buf, "spell") == 0)
3327 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003328 else
3329# endif
3330 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 || expand_wildcards(1, &buf, &count, &files,
3332 EW_FILE|EW_SILENT) != OK)
3333 count = 0;
3334 }
3335
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003336# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003337 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003339 /* Complete from active spelling. Skip "\<" in the pattern, we
3340 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003341 if (pat[0] == '\\' && pat[1] == '<')
3342 ptr = pat + 2;
3343 else
3344 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003345 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003347 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003348# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003349 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003350 {
3351 ins_compl_files(count, files, thesaurus, flags,
3352 &regmatch, buf, &dir);
3353 if (flags != DICT_EXACT)
3354 FreeWild(count, files);
3355 }
3356 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 break;
3358 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003359
3360theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003362 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 vim_free(buf);
3364}
3365
Bram Moolenaar0b238792006-03-02 22:49:12 +00003366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003367ins_compl_files(
3368 int count,
3369 char_u **files,
3370 int thesaurus,
3371 int flags,
3372 regmatch_T *regmatch,
3373 char_u *buf,
3374 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003375{
3376 char_u *ptr;
3377 int i;
3378 FILE *fp;
3379 int add_r;
3380
3381 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3382 {
3383 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3384 if (flags != DICT_EXACT)
3385 {
3386 vim_snprintf((char *)IObuff, IOSIZE,
3387 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003388 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003389 }
3390
3391 if (fp != NULL)
3392 {
3393 /*
3394 * Read dictionary file line by line.
3395 * Check each line for a match.
3396 */
3397 while (!got_int && !compl_interrupted
3398 && !vim_fgets(buf, LSIZE, fp))
3399 {
3400 ptr = buf;
3401 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3402 {
3403 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003404 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003405 ptr = find_line_end(ptr);
3406 else
3407 ptr = find_word_end(ptr);
3408 add_r = ins_compl_add_infercase(regmatch->startp[0],
3409 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003410 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003411 if (thesaurus)
3412 {
3413 char_u *wstart;
3414
3415 /*
3416 * Add the other matches on the line
3417 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003418 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003419 while (!got_int)
3420 {
3421 /* Find start of the next word. Skip white
3422 * space and punctuation. */
3423 ptr = find_word_start(ptr);
3424 if (*ptr == NUL || *ptr == NL)
3425 break;
3426 wstart = ptr;
3427
Bram Moolenaara2993e12007-08-12 14:38:46 +00003428 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003429#ifdef FEAT_MBYTE
3430 if (has_mbyte)
3431 /* Japanese words may have characters in
3432 * different classes, only separate words
3433 * with single-byte non-word characters. */
3434 while (*ptr != NUL)
3435 {
3436 int l = (*mb_ptr2len)(ptr);
3437
3438 if (l < 2 && !vim_iswordc(*ptr))
3439 break;
3440 ptr += l;
3441 }
3442 else
3443#endif
3444 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003445
3446 /* Add the word. Skip the regexp match. */
3447 if (wstart != regmatch->startp[0])
3448 add_r = ins_compl_add_infercase(wstart,
3449 (int)(ptr - wstart),
3450 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003451 }
3452 }
3453 if (add_r == OK)
3454 /* if dir was BACKWARD then honor it just once */
3455 *dir = FORWARD;
3456 else if (add_r == FAIL)
3457 break;
3458 /* avoid expensive call to vim_regexec() when at end
3459 * of line */
3460 if (*ptr == '\n' || got_int)
3461 break;
3462 }
3463 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003464 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003465 }
3466 fclose(fp);
3467 }
3468 }
3469}
3470
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471/*
3472 * Find the start of the next word.
3473 * Returns a pointer to the first char of the word. Also stops at a NUL.
3474 */
3475 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003476find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478#ifdef FEAT_MBYTE
3479 if (has_mbyte)
3480 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003481 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else
3483#endif
3484 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3485 ++ptr;
3486 return ptr;
3487}
3488
3489/*
3490 * Find the end of the word. Assumes it starts inside a word.
3491 * Returns a pointer to just after the word.
3492 */
3493 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496#ifdef FEAT_MBYTE
3497 int start_class;
3498
3499 if (has_mbyte)
3500 {
3501 start_class = mb_get_class(ptr);
3502 if (start_class > 1)
3503 while (*ptr != NUL)
3504 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003505 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (mb_get_class(ptr) != start_class)
3507 break;
3508 }
3509 }
3510 else
3511#endif
3512 while (vim_iswordc(*ptr))
3513 ++ptr;
3514 return ptr;
3515}
3516
3517/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003518 * Find the end of the line, omitting CR and NL at the end.
3519 * Returns a pointer to just after the line.
3520 */
3521 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003522find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003523{
3524 char_u *s;
3525
3526 s = ptr + STRLEN(ptr);
3527 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3528 --s;
3529 return s;
3530}
3531
3532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 * Free the list of completions
3534 */
3535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003536ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003538 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003539 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaard23a8232018-02-10 18:45:26 +01003541 VIM_CLEAR(compl_pattern);
3542 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003544 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003546
3547 ins_compl_del_pum();
3548 pum_clear();
3549
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 do
3552 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003554 compl_curr_match = compl_curr_match->cp_next;
3555 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003557 if (match->cp_flags & FREE_FNAME)
3558 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003559 for (i = 0; i < CPT_COUNT; ++i)
3560 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3563 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003564 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003565 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003569ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003571 compl_cont_status = 0;
3572 compl_started = FALSE;
3573 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003574 VIM_CLEAR(compl_pattern);
3575 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003577 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003578 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003579 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003580 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
3582
3583/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003584 * Return TRUE when Insert completion is active.
3585 */
3586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003588{
3589 return compl_started;
3590}
3591
3592/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003593 * Delete one character before the cursor and show the subset of the matches
3594 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003595 * Returns the character to be used, NUL if the work is done and another char
3596 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003597 */
3598 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003599ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003600{
3601 char_u *line;
3602 char_u *p;
3603
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003604 line = ml_get_curline();
3605 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003606 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003607
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003608 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003609 * allow the word to be deleted, we won't match everything.
3610 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003611 if ((int)(p - line) - (int)compl_col < 0
3612 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003613 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3614 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3615 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003616 return K_BS;
3617
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003618 /* Deleted more than what was used to find matches or didn't finish
3619 * finding all matches: need to look for matches all over again. */
3620 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003621 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003622 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003623
Bram Moolenaara6557602006-02-04 22:43:20 +00003624 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003625 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003626 if (compl_leader != NULL)
3627 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003628 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003629 if (compl_shown_match != NULL)
3630 /* Make sure current match is not a hidden item. */
3631 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003632 return NUL;
3633 }
3634 return K_BS;
3635}
Bram Moolenaara6557602006-02-04 22:43:20 +00003636
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003637/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003638 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3639 * be called.
3640 */
3641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003643{
3644 /* Return TRUE if we didn't complete finding matches or when the
3645 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3646 return compl_was_interrupted
3647 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3648 && compl_opt_refresh_always);
3649}
3650
3651/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003652 * Called after changing "compl_leader".
3653 * Show the popup menu with a different set of matches.
3654 * May also search for matches again if the previous search was interrupted.
3655 */
3656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003658{
3659 ins_compl_del_pum();
3660 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003661 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003662 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003663
3664 if (compl_started)
3665 ins_compl_set_original_text(compl_leader);
3666 else
3667 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003668#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003669 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003670#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003671 /*
3672 * Matches were cleared, need to search for them now. First display
3673 * the changed text before the cursor. Set "compl_restarting" to
3674 * avoid that the first match is inserted.
3675 */
3676 update_screen(0);
3677#ifdef FEAT_GUI
3678 if (gui.in_use)
3679 {
3680 /* Show the cursor after the match, not after the redrawn text. */
3681 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003682 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003683 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003684#endif
3685 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003686 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003687 compl_cont_status = 0;
3688 compl_restarting = FALSE;
3689 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003690
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003691 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003692
3693 /* Show the popup menu with a different set of matches. */
3694 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003695
3696 /* Don't let Enter select the original text when there is no popup menu. */
3697 if (compl_match_array == NULL)
3698 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003699}
3700
3701/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003702 * Return the length of the completion, from the completion start column to
3703 * the cursor column. Making sure it never goes below zero.
3704 */
3705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003706ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003707{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003708 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003709
3710 if (off < 0)
3711 return 0;
3712 return off;
3713}
3714
3715/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003716 * Append one character to the match leader. May reduce the number of
3717 * matches.
3718 */
3719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003721{
3722#ifdef FEAT_MBYTE
3723 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003724#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003725
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003726 if (stop_arrow() == FAIL)
3727 return;
3728#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003729 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3730 {
3731 char_u buf[MB_MAXBYTES + 1];
3732
3733 (*mb_char2bytes)(c, buf);
3734 buf[cc] = NUL;
3735 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003736 if (compl_opt_refresh_always)
3737 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003738 }
3739 else
3740#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003741 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003742 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003743 if (compl_opt_refresh_always)
3744 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003745 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003746
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003747 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003748 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003749 ins_compl_restart();
3750
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003751 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003752 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003753 * break redo. */
3754 if (!compl_opt_refresh_always)
3755 {
3756 vim_free(compl_leader);
3757 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003758 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003759 if (compl_leader != NULL)
3760 ins_compl_new_leader();
3761 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003762}
3763
3764/*
3765 * Setup for finding completions again without leaving CTRL-X mode. Used when
3766 * BS or a key was typed while still searching for matches.
3767 */
3768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003769ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003770{
3771 ins_compl_free();
3772 compl_started = FALSE;
3773 compl_matches = 0;
3774 compl_cont_status = 0;
3775 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003776}
3777
3778/*
3779 * Set the first match, the original text.
3780 */
3781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003783{
3784 char_u *p;
3785
Bram Moolenaare87edf32018-04-17 22:14:32 +02003786 /* Replace the original text entry.
3787 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3788 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003789 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3790 {
3791 p = vim_strsave(str);
3792 if (p != NULL)
3793 {
3794 vim_free(compl_first_match->cp_str);
3795 compl_first_match->cp_str = p;
3796 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003797 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003798 else if (compl_first_match->cp_prev != NULL
3799 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3800 {
3801 p = vim_strsave(str);
3802 if (p != NULL)
3803 {
3804 vim_free(compl_first_match->cp_prev->cp_str);
3805 compl_first_match->cp_prev->cp_str = p;
3806 }
3807 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003808}
3809
3810/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003811 * Append one character to the match leader. May reduce the number of
3812 * matches.
3813 */
3814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003815ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003816{
3817 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003818 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003819 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003820 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003821
3822 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003823 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003824 {
3825 /* When still at the original match use the first entry that matches
3826 * the leader. */
3827 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3828 {
3829 p = NULL;
3830 for (cp = compl_shown_match->cp_next; cp != NULL
3831 && cp != compl_first_match; cp = cp->cp_next)
3832 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003833 if (compl_leader == NULL
3834 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003835 (int)STRLEN(compl_leader)))
3836 {
3837 p = cp->cp_str;
3838 break;
3839 }
3840 }
3841 if (p == NULL || (int)STRLEN(p) <= len)
3842 return;
3843 }
3844 else
3845 return;
3846 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003847 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003848 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003849 ins_compl_addleader(c);
3850}
3851
3852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003854 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003855 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003858ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003862 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 /* Forget any previous 'special' messages if this is actually
3865 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3866 */
3867 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3868 edit_submode_extra = NULL;
3869
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003870 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003871 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3872 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003873 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003875 /* Set "compl_get_longest" when finding the first matches. */
3876 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003877 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003878 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003879 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003880 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003881
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003882 }
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3885 {
3886 /*
3887 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3888 * it will be yet. Now we decide.
3889 */
3890 switch (c)
3891 {
3892 case Ctrl_E:
3893 case Ctrl_Y:
3894 ctrl_x_mode = CTRL_X_SCROLL;
3895 if (!(State & REPLACE_FLAG))
3896 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3897 else
3898 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3899 edit_submode_pre = NULL;
3900 showmode();
3901 break;
3902 case Ctrl_L:
3903 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3904 break;
3905 case Ctrl_F:
3906 ctrl_x_mode = CTRL_X_FILES;
3907 break;
3908 case Ctrl_K:
3909 ctrl_x_mode = CTRL_X_DICTIONARY;
3910 break;
3911 case Ctrl_R:
3912 /* Simply allow ^R to happen without affecting ^X mode */
3913 break;
3914 case Ctrl_T:
3915 ctrl_x_mode = CTRL_X_THESAURUS;
3916 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003917#ifdef FEAT_COMPL_FUNC
3918 case Ctrl_U:
3919 ctrl_x_mode = CTRL_X_FUNCTION;
3920 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003922 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003923 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003924#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003925 case 's':
3926 case Ctrl_S:
3927 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003928#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003929 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003930 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003932#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 case Ctrl_RSB:
3935 ctrl_x_mode = CTRL_X_TAGS;
3936 break;
3937#ifdef FEAT_FIND_ID
3938 case Ctrl_I:
3939 case K_S_TAB:
3940 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3941 break;
3942 case Ctrl_D:
3943 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3944 break;
3945#endif
3946 case Ctrl_V:
3947 case Ctrl_Q:
3948 ctrl_x_mode = CTRL_X_CMDLINE;
3949 break;
3950 case Ctrl_P:
3951 case Ctrl_N:
3952 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3953 * just started ^X mode, or there were enough ^X's to cancel
3954 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3955 * do normal expansion when interrupting a different mode (say
3956 * ^X^F^X^P or ^P^X^X^P, see below)
3957 * nothing changes if interrupting mode 0, (eg, the flag
3958 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003959 if (!(compl_cont_status & CONT_INTRPT))
3960 compl_cont_status |= CONT_LOCAL;
3961 else if (compl_cont_mode != 0)
3962 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 /* FALLTHROUGH */
3964 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003965 /* If we have typed at least 2 ^X's... for modes != 0, we set
3966 * compl_cont_status = 0 (eg, as if we had just started ^X
3967 * mode).
3968 * For mode 0, we set "compl_cont_mode" to an impossible
3969 * value, in both cases ^X^X can be used to restart the same
3970 * mode (avoiding ADDING mode).
3971 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3972 * 'complete' and local ^P expansions respectively.
3973 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3974 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 if (c == Ctrl_X)
3976 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003977 if (compl_cont_mode != 0)
3978 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003980 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003982 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 edit_submode = NULL;
3984 showmode();
3985 break;
3986 }
3987 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003988 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
3990 /* We're already in CTRL-X mode, do we stay in it? */
3991 if (!vim_is_ctrl_x_key(c))
3992 {
3993 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003994 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
3996 ctrl_x_mode = CTRL_X_FINISHED;
3997 edit_submode = NULL;
3998 }
3999 showmode();
4000 }
4001
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004002 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
4004 /* Show error message from attempted keyword completion (probably
4005 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004006 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004008 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4009 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 || ctrl_x_mode == CTRL_X_FINISHED)
4011 {
4012 /* Get here when we have finished typing a sequence of ^N and
4013 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004014 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004015 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
4017 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004018 * If any of the original typed text has been changed, eg when
4019 * ignorecase is set, we must add back-spaces to the redo
4020 * buffer. We add as few as necessary to delete just the part
4021 * of the original text that has changed.
4022 * When using the longest match, edited the match or used
4023 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004025 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4026 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004027 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004028 ptr = NULL;
4029 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031
4032#ifdef FEAT_CINDENT
4033 want_cindent = (can_cindent && cindent_on());
4034#endif
4035 /*
4036 * When completing whole lines: fix indent for 'cindent'.
4037 * Otherwise, break line if it's too long.
4038 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
4041#ifdef FEAT_CINDENT
4042 /* re-indent the current line */
4043 if (want_cindent)
4044 {
4045 do_c_expr_indent();
4046 want_cindent = FALSE; /* don't do it again */
4047 }
4048#endif
4049 }
4050 else
4051 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004052 int prev_col = curwin->w_cursor.col;
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004055 if (prev_col > 0)
4056 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004057 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004058 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004060 if (prev_col > 0
4061 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4062 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004065 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004066 * the selection without inserting anything. When
4067 * compl_enter_selects is set the Enter key does the same. */
4068 if ((c == Ctrl_Y || (compl_enter_selects
4069 && (c == CAR || c == K_KENTER || c == NL)))
4070 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004071 retval = TRUE;
4072
Bram Moolenaar47247282016-08-02 22:36:02 +02004073 /* CTRL-E means completion is Ended, go back to the typed text.
4074 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004075 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004076 {
4077 ins_compl_delete();
4078 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004079 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004080 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004081 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004082 retval = TRUE;
4083 }
4084
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004085 auto_format(FALSE, TRUE);
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004088 compl_started = FALSE;
4089 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004090 if (!shortmess(SHM_COMPLETIONMENU))
4091 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004092 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004093 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (edit_submode != NULL)
4095 {
4096 edit_submode = NULL;
4097 showmode();
4098 }
4099
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004100#ifdef FEAT_CMDWIN
4101 if (c == Ctrl_C && cmdwin_type != 0)
4102 /* Avoid the popup menu remains displayed when leaving the
4103 * command line window. */
4104 update_screen(0);
4105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#ifdef FEAT_CINDENT
4107 /*
4108 * Indent now if a key was typed that is in 'cinkeys'.
4109 */
4110 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4111 do_c_expr_indent();
4112#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004113 /* Trigger the CompleteDone event to give scripts a chance to act
4114 * upon the completion. */
4115 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004118 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4119 /* Trigger the CompleteDone event to give scripts a chance to act
4120 * upon the (possibly failed) completion. */
4121 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 /* reset continue_* if we left expansion-mode, if we stay they'll be
4124 * (re)set properly in ins_complete() */
4125 if (!vim_is_ctrl_x_key(c))
4126 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 compl_cont_status = 0;
4128 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004130
4131 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132}
4133
4134/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004135 * Fix the redo buffer for the completion leader replacing some of the typed
4136 * text. This inserts backspaces and appends the changed text.
4137 * "ptr" is the known leader text or NUL.
4138 */
4139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004140ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004141{
4142 int len;
4143 char_u *p;
4144 char_u *ptr = ptr_arg;
4145
4146 if (ptr == NULL)
4147 {
4148 if (compl_leader != NULL)
4149 ptr = compl_leader;
4150 else
4151 return; /* nothing to do */
4152 }
4153 if (compl_orig_text != NULL)
4154 {
4155 p = compl_orig_text;
4156 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4157 ;
4158#ifdef FEAT_MBYTE
4159 if (len > 0)
4160 len -= (*mb_head_off)(p, p + len);
4161#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004162 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004163 AppendCharToRedobuff(K_BS);
4164 }
4165 else
4166 len = 0;
4167 if (ptr != NULL)
4168 AppendToRedobuffLit(ptr + len, -1);
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4173 * (depending on flag) starting from buf and looking for a non-scanned
4174 * buffer (other than curbuf). curbuf is special, if it is called with
4175 * buf=curbuf then it has to be the first call for a given flag/expansion.
4176 *
4177 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4178 */
4179 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004180ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 if (flag == 'w') /* just windows */
4185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 if (buf == curbuf) /* first call for this flag/expansion */
4187 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004188 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 && wp->w_buffer->b_scanned)
4190 ;
4191 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 else
4194 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4195 * (unlisted buffers)
4196 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004197 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 && ((flag == 'U'
4199 ? buf->b_p_bl
4200 : (!buf->b_p_bl
4201 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004202 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 ;
4204 return buf;
4205}
4206
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004207#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004208/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004209 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004210 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004211 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004213expand_by_function(
4214 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4215 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004216{
Bram Moolenaar82139082011-09-14 16:52:09 +02004217 list_T *matchlist = NULL;
4218 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004219 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004220 char_u *funcname;
4221 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004222 win_T *curwin_save;
4223 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004224 typval_T rettv;
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 Moolenaarffa96842018-06-12 22:05:14 +02004242 if (call_vim_function(funcname, 2, args, &rettv, FALSE) == 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;
4252 default:
4253 /* TODO: Give error message? */
4254 clear_tv(&rettv);
4255 break;
4256 }
4257 }
4258
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004259 if (curwin_save != curwin || curbuf_save != curbuf)
4260 {
4261 EMSG(_(e_complwin));
4262 goto theend;
4263 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004264 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004265 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004266 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004267 {
4268 EMSG(_(e_compldel));
4269 goto theend;
4270 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004271
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004272 if (matchlist != NULL)
4273 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004274 else if (matchdict != NULL)
4275 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004276
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004277theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004278 if (matchdict != NULL)
4279 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004280 if (matchlist != NULL)
4281 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004282}
4283#endif /* FEAT_COMPL_FUNC */
4284
Bram Moolenaar39f05632006-03-19 22:15:26 +00004285#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004286/*
4287 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004288 */
4289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004290ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004291{
4292 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004293 int dir = compl_direction;
4294
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004295 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004296 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004297 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004298 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4299 /* if dir was BACKWARD then honor it just once */
4300 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004301 else if (did_emsg)
4302 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004303 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004304}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004305
4306/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004307 * Add completions from a dict.
4308 */
4309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004310ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004311{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004312 dictitem_T *di_refresh;
4313 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004314
4315 /* Check for optional "refresh" item. */
4316 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004317 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4318 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004319 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004320 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004321
4322 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4323 compl_opt_refresh_always = TRUE;
4324 }
4325
4326 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004327 di_words = dict_find(dict, (char_u *)"words", 5);
4328 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4329 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004330}
4331
4332/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004333 * Add a match to the list of matches from a typeval_T.
4334 * If the given string is already in the list of completions, then return
4335 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4336 * maybe because alloc() returns NULL, then FAIL is returned.
4337 */
4338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004339ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004340{
4341 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004342 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004343 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004344 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004345 char_u *(cptext[CPT_COUNT]);
4346
4347 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4348 {
4349 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4350 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4351 (char_u *)"abbr", FALSE);
4352 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4353 (char_u *)"menu", FALSE);
4354 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4355 (char_u *)"kind", FALSE);
4356 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4357 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004358 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4359 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004360 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4361 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004362 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004363 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004364 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4365 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004366 }
4367 else
4368 {
4369 word = get_tv_string_chk(tv);
4370 vim_memset(cptext, 0, sizeof(cptext));
4371 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004372 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004373 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004374 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004375}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004376#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004377
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004378/*
4379 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004380 * The search starts at position "ini" in curbuf and in the direction
4381 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004382 * When "compl_started" is FALSE start at that position, otherwise continue
4383 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 * This may return before finding all the matches.
4385 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 */
4387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004388ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 static pos_T first_match_pos;
4391 static pos_T last_match_pos;
4392 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393 static int found_all = FALSE; /* Found all matches of a
4394 certain type. */
4395 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
Bram Moolenaar572cb562005-08-05 21:35:02 +00004397 pos_T *pos;
4398 char_u **matches;
4399 int save_p_scs;
4400 int save_p_ws;
4401 int save_p_ic;
4402 int i;
4403 int num_matches;
4404 int len;
4405 int found_new_match;
4406 int type = ctrl_x_mode;
4407 char_u *ptr;
4408 char_u *dict = NULL;
4409 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004410 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004414 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 ins_buf->b_scanned = 0;
4416 found_all = FALSE;
4417 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004419 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 last_match_pos = first_match_pos = *ini;
4421 }
4422
Bram Moolenaar4475b622017-05-01 20:46:52 +02004423 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004424 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4426 for (;;)
4427 {
4428 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004429 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 * or if found_all says this entry is done. For ^X^L only use the
4433 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004434 if ((ctrl_x_mode == CTRL_X_NORMAL
4435 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004436 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
4438 found_all = FALSE;
4439 while (*e_cpt == ',' || *e_cpt == ' ')
4440 e_cpt++;
4441 if (*e_cpt == '.' && !curbuf->b_scanned)
4442 {
4443 ins_buf = curbuf;
4444 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004445 /* Move the cursor back one character so that ^N can match the
4446 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004447 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004448 {
4449 /* Move the cursor to after the last character in the
4450 * buffer, so that word at start of buffer is found
4451 * correctly. */
4452 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4453 first_match_pos.col =
4454 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 last_match_pos = first_match_pos;
4457 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004458
4459 /* Remember the first match so that the loop stops when we
4460 * wrap and come back there a second time. */
4461 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4464 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4465 {
4466 /* Scan a buffer, but not the current one. */
4467 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4468 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 first_match_pos.col = last_match_pos.col = 0;
4471 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4472 last_match_pos.lnum = 0;
4473 type = 0;
4474 }
4475 else /* unloaded buffer, scan like dictionary */
4476 {
4477 found_all = TRUE;
4478 if (ins_buf->b_fname == NULL)
4479 continue;
4480 type = CTRL_X_DICTIONARY;
4481 dict = ins_buf->b_fname;
4482 dict_f = DICT_EXACT;
4483 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004484 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 ins_buf->b_fname == NULL
4486 ? buf_spname(ins_buf)
4487 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004488 ? ins_buf->b_fname
4489 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004490 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492 else if (*e_cpt == NUL)
4493 break;
4494 else
4495 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004496 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 type = -1;
4498 else if (*e_cpt == 'k' || *e_cpt == 's')
4499 {
4500 if (*e_cpt == 'k')
4501 type = CTRL_X_DICTIONARY;
4502 else
4503 type = CTRL_X_THESAURUS;
4504 if (*++e_cpt != ',' && *e_cpt != NUL)
4505 {
4506 dict = e_cpt;
4507 dict_f = DICT_FIRST;
4508 }
4509 }
4510#ifdef FEAT_FIND_ID
4511 else if (*e_cpt == 'i')
4512 type = CTRL_X_PATH_PATTERNS;
4513 else if (*e_cpt == 'd')
4514 type = CTRL_X_PATH_DEFINES;
4515#endif
4516 else if (*e_cpt == ']' || *e_cpt == 't')
4517 {
4518 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004519 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004520 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522 else
4523 type = -1;
4524
4525 /* in any case e_cpt is advanced to the next entry */
4526 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4527
4528 found_all = TRUE;
4529 if (type == -1)
4530 continue;
4531 }
4532 }
4533
Bram Moolenaar4475b622017-05-01 20:46:52 +02004534 /* If complete() was called then compl_pattern has been reset. The
4535 * following won't work then, bail out. */
4536 if (compl_pattern == NULL)
4537 break;
4538
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 switch (type)
4540 {
4541 case -1:
4542 break;
4543#ifdef FEAT_FIND_ID
4544 case CTRL_X_PATH_PATTERNS:
4545 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004546 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004549 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4551 (linenr_T)1, (linenr_T)MAXLNUM);
4552 break;
4553#endif
4554
4555 case CTRL_X_DICTIONARY:
4556 case CTRL_X_THESAURUS:
4557 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004558 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 : (type == CTRL_X_THESAURUS
4560 ? (*curbuf->b_p_tsr == NUL
4561 ? p_tsr
4562 : curbuf->b_p_tsr)
4563 : (*curbuf->b_p_dict == NUL
4564 ? p_dict
4565 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004566 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004567 dict != NULL ? dict_f
4568 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 dict = NULL;
4570 break;
4571
4572 case CTRL_X_TAGS:
4573 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4574 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004575 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004577 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004578 * of matches is found when compl_pattern is empty */
4579 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004580 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4581 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4583 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004584 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 p_ic = save_p_ic;
4587 break;
4588
4589 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4592 {
4593
4594 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004596 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 break;
4599
4600 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601 if (expand_cmdline(&compl_xp, compl_pattern,
4602 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004604 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 break;
4606
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004607#ifdef FEAT_COMPL_FUNC
4608 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004609 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004610 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004611 break;
4612#endif
4613
Bram Moolenaar488c6512005-08-11 20:09:58 +00004614 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004615#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004616 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004617 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004618 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004619 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004620#endif
4621 break;
4622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 default: /* normal ^P/^N and ^X^L */
4624 /*
4625 * If 'infercase' is set, don't use 'smartcase' here
4626 */
4627 save_p_scs = p_scs;
4628 if (ins_buf->b_p_inf)
4629 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004630
Bram Moolenaar361aa502014-01-23 22:45:58 +01004631 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 * end but never from the middle, thus setting nowrapscan in this
4633 * buffers is a good idea, on the other hand, we always set
4634 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4635 save_p_ws = p_ws;
4636 if (ins_buf != curbuf)
4637 p_ws = FALSE;
4638 else if (*e_cpt == '.')
4639 p_ws = TRUE;
4640 for (;;)
4641 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004642 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004644 ++msg_silent; /* Don't want messages for wrapscan. */
4645
Bram Moolenaare4214502015-03-05 18:08:43 +01004646 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4647 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004648 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004649 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004650 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004652 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004654 found_new_match = searchit(NULL, ins_buf, pos,
4655 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004656 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004657 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004658 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004659 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004661 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004662 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 first_match_pos = *pos;
4664 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004665 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004668 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 found_new_match = FAIL;
4670 if (found_new_match == FAIL)
4671 {
4672 if (ins_buf == curbuf)
4673 found_all = TRUE;
4674 break;
4675 }
4676
4677 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004678 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 && ini->lnum == pos->lnum
4680 && ini->col == pos->col)
4681 continue;
4682 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004683 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004685 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
4687 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4688 continue;
4689 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4690 if (!p_paste)
4691 ptr = skipwhite(ptr);
4692 }
4693 len = (int)STRLEN(ptr);
4694 }
4695 else
4696 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004697 char_u *tmp_ptr = ptr;
4698
4699 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004701 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 /* Skip if already inside a word. */
4703 if (vim_iswordp(tmp_ptr))
4704 continue;
4705 /* Find start of next word. */
4706 tmp_ptr = find_word_start(tmp_ptr);
4707 }
4708 /* Find end of this word. */
4709 tmp_ptr = find_word_end(tmp_ptr);
4710 len = (int)(tmp_ptr - ptr);
4711
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004712 if ((compl_cont_status & CONT_ADDING)
4713 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
4715 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4716 {
4717 /* Try next line, if any. the new word will be
4718 * "join" as if the normal command "J" was used.
4719 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004720 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 * works -- Acevedo */
4722 STRNCPY(IObuff, ptr, len);
4723 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4724 tmp_ptr = ptr = skipwhite(ptr);
4725 /* Find start of next word. */
4726 tmp_ptr = find_word_start(tmp_ptr);
4727 /* Find end of next word. */
4728 tmp_ptr = find_word_end(tmp_ptr);
4729 if (tmp_ptr > ptr)
4730 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004731 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004733 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 IObuff[len++] = ' ';
4735 /* IObuf =~ "\k.* ", thus len >= 2 */
4736 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004737 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 || (vim_strchr(p_cpo, CPO_JOINSP)
4739 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004740 && (IObuff[len - 2] == '?'
4741 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 IObuff[len++] = ' ';
4743 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004744 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 if (tmp_ptr - ptr >= IOSIZE - len)
4746 tmp_ptr = ptr + IOSIZE - len - 1;
4747 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4748 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004749 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
4751 IObuff[len] = NUL;
4752 ptr = IObuff;
4753 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004754 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 continue;
4756 }
4757 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004758 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004759 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004760 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
4762 found_new_match = OK;
4763 break;
4764 }
4765 }
4766 p_scs = save_p_scs;
4767 p_ws = save_p_ws;
4768 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004769
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004770 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004771 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004772 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 found_new_match = OK;
4774
4775 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004776 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4777 * match */
4778 if ((ctrl_x_mode != CTRL_X_NORMAL
4779 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004780 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004781 {
4782 if (got_int)
4783 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004784 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004785 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004786 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004787
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004788 if ((ctrl_x_mode != CTRL_X_NORMAL
4789 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004790 || compl_interrupted)
4791 break;
4792 compl_started = TRUE;
4793 }
4794 else
4795 {
4796 /* Mark a buffer scanned when it has been scanned completely */
4797 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4798 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004800 compl_started = FALSE;
4801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004803 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004805 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 && *e_cpt == NUL) /* Got to end of 'complete' */
4807 found_new_match = FAIL;
4808
4809 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004810 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4811 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 i = ins_compl_make_cyclic();
4813
Bram Moolenaar4475b622017-05-01 20:46:52 +02004814 if (compl_old_match != NULL)
4815 {
4816 /* If several matches were added (FORWARD) or the search failed and has
4817 * just been made cyclic then we have to move compl_curr_match to the
4818 * next or previous entry (if any) -- Acevedo */
4819 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4820 : compl_old_match->cp_prev;
4821 if (compl_curr_match == NULL)
4822 compl_curr_match = compl_old_match;
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return i;
4825}
4826
4827/* Delete the old text being completed. */
4828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004829ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004831 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
4833 /*
4834 * In insert mode: Delete the typed part.
4835 * In replace mode: Put the old characters back, if any.
4836 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004837 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4838 if ((int)curwin->w_cursor.col > col)
4839 {
4840 if (stop_arrow() == FAIL)
4841 return;
4842 backspace_until_column(col);
4843 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004844
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004845 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4846 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004848 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004849 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850}
4851
Bram Moolenaar472e8592016-10-15 17:06:47 +02004852/*
4853 * Insert the new text being completed.
4854 * "in_compl_func" is TRUE when called from complete_check().
4855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004857ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004859 dict_T *dict;
4860
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004861 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004862 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4863 compl_used_match = FALSE;
4864 else
4865 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004866
4867 /* Set completed item. */
4868 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004869 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004870 if (dict != NULL)
4871 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004872 dict_add_string(dict, "word", compl_shown_match->cp_str);
4873 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4874 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4875 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4876 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4877 dict_add_string(dict, "user_data",
4878 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004879 }
4880 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004881 if (!in_compl_func)
4882 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883}
4884
4885/*
4886 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004887 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4888 * get more completions. If it is FALSE, then we just do nothing when there
4889 * are no more completions in a given direction. The latter case is used when
4890 * we are still in the middle of finding completions, to allow browsing
4891 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 * Return the total number of matches, or -1 if still unknown -- webb.
4893 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004894 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4895 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 *
4897 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004898 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4899 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
4901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004902ins_compl_next(
4903 int allow_get_expansion,
4904 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004905 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004906 int insert_match, /* Insert the newly selected match */
4907 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908{
4909 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004910 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004911 compl_T *found_compl = NULL;
4912 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004913 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004914 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004916 /* When user complete function return -1 for findstart which is next
4917 * time of 'always', compl_shown_match become NULL. */
4918 if (compl_shown_match == NULL)
4919 return -1;
4920
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004921 if (compl_leader != NULL
4922 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004924 /* Set "compl_shown_match" to the actually shown match, it may differ
4925 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004926 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004927 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004928 && compl_shown_match->cp_next != NULL
4929 && compl_shown_match->cp_next != compl_first_match)
4930 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004931
4932 /* If we didn't find it searching forward, and compl_shows_dir is
4933 * backward, find the last match. */
4934 if (compl_shows_dir == BACKWARD
4935 && !ins_compl_equal(compl_shown_match,
4936 compl_leader, (int)STRLEN(compl_leader))
4937 && (compl_shown_match->cp_next == NULL
4938 || compl_shown_match->cp_next == compl_first_match))
4939 {
4940 while (!ins_compl_equal(compl_shown_match,
4941 compl_leader, (int)STRLEN(compl_leader))
4942 && compl_shown_match->cp_prev != NULL
4943 && compl_shown_match->cp_prev != compl_first_match)
4944 compl_shown_match = compl_shown_match->cp_prev;
4945 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004946 }
4947
4948 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004949 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 /* Delete old text to be replaced */
4951 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004952
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004953 /* When finding the longest common text we stick at the original text,
4954 * don't let CTRL-N or CTRL-P move to the first match. */
4955 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4956
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004957 /* When restarting the search don't insert the first match either. */
4958 if (compl_restarting)
4959 {
4960 advance = FALSE;
4961 compl_restarting = FALSE;
4962 }
4963
Bram Moolenaare3226be2005-12-18 22:10:00 +00004964 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4965 * around. */
4966 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004968 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004970 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004971 found_end = (compl_first_match != NULL
4972 && (compl_shown_match->cp_next == compl_first_match
4973 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004974 }
4975 else if (compl_shows_dir == BACKWARD
4976 && compl_shown_match->cp_prev != NULL)
4977 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004978 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004979 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004980 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004983 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004984 if (!allow_get_expansion)
4985 {
4986 if (advance)
4987 {
4988 if (compl_shows_dir == BACKWARD)
4989 compl_pending -= todo + 1;
4990 else
4991 compl_pending += todo + 1;
4992 }
4993 return -1;
4994 }
4995
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004996 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004997 {
4998 if (compl_shows_dir == BACKWARD)
4999 --compl_pending;
5000 else
5001 ++compl_pending;
5002 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005003
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005004 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005005 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005006
5007 /* handle any pending completions */
5008 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005009 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005010 {
5011 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5012 {
5013 compl_shown_match = compl_shown_match->cp_next;
5014 --compl_pending;
5015 }
5016 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5017 {
5018 compl_shown_match = compl_shown_match->cp_prev;
5019 ++compl_pending;
5020 }
5021 else
5022 break;
5023 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005024 found_end = FALSE;
5025 }
5026 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5027 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005028 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005029 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005030 ++todo;
5031 else
5032 /* Remember a matching item. */
5033 found_compl = compl_shown_match;
5034
5035 /* Stop at the end of the list when we found a usable match. */
5036 if (found_end)
5037 {
5038 if (found_compl != NULL)
5039 {
5040 compl_shown_match = found_compl;
5041 break;
5042 }
5043 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005047 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005048 if (compl_no_insert && !started)
5049 {
5050 ins_bytes(compl_orig_text + ins_compl_len());
5051 compl_used_match = FALSE;
5052 }
5053 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005054 {
5055 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005056 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005057 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005058 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005059 }
5060 else
5061 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 if (!allow_get_expansion)
5064 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005065 /* may undisplay the popup menu first */
5066 ins_compl_upd_pum();
5067
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005068 /* redraw to show the user what was inserted */
5069 update_screen(0);
5070
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005071 /* display the updated popup menu */
5072 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005073#ifdef FEAT_GUI
5074 if (gui.in_use)
5075 {
5076 /* Show the cursor after the match, not after the redrawn text. */
5077 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005078 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005079 }
5080#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 /* Delete old text to be replaced, since we're still searching and
5083 * don't want to match ourselves! */
5084 ins_compl_delete();
5085 }
5086
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005087 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005088 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005089 if (compl_no_insert && !started)
5090 compl_enter_selects = TRUE;
5091 else
5092 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 /*
5095 * Show the file name for the match (if any)
5096 * Truncate the file name to avoid a wait for return.
5097 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005098 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005100 char *lead = _("match in file");
5101 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5102 char_u *s;
5103 char_u *e;
5104
5105 if (space > 0)
5106 {
5107 /* We need the tail that fits. With double-byte encoding going
5108 * back from the end is very slow, thus go from the start and keep
5109 * the text that fits in "space" between "s" and "e". */
5110 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5111 {
5112 space -= ptr2cells(e);
5113 while (space < 0)
5114 {
5115 space += ptr2cells(s);
5116 MB_PTR_ADV(s);
5117 }
5118 }
5119 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5120 s > compl_shown_match->cp_fname ? "<" : "", s);
5121 msg(IObuff);
5122 redraw_cmdline = FALSE; /* don't overwrite! */
5123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125
5126 return num_matches;
5127}
5128
5129/*
5130 * Call this while finding completions, to check whether the user has hit a key
5131 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005132 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005134 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005135 * "in_compl_func" is TRUE when called from complete_check(), don't set
5136 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 */
5138 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005139ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005142 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005144 /* Don't check when reading keys from a script, :normal or feedkeys().
5145 * That would break the test scripts. But do check for keys when called
5146 * from complete_check(). */
5147 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 return;
5149
5150 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005151 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 return;
5153 count = 0;
5154
Bram Moolenaara260a972006-06-23 19:36:29 +00005155 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5156 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (c != NUL)
5159 {
5160 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5161 {
5162 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005163 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005164 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005165 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005167 else
5168 {
5169 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005170 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005171 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005172 if (c != K_IGNORE)
5173 {
5174 /* Don't interrupt completion when the character wasn't typed,
5175 * e.g., when doing @q to replay keys. */
5176 if (c != Ctrl_R && KeyTyped)
5177 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005178
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005179 vungetc(c);
5180 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005183 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005184 {
5185 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5186
5187 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005188 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005189 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005190}
5191
5192/*
5193 * Decide the direction of Insert mode complete from the key typed.
5194 * Returns BACKWARD or FORWARD.
5195 */
5196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005197ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005198{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005199 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005200 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005201 return BACKWARD;
5202 return FORWARD;
5203}
5204
5205/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005206 * Return TRUE for keys that are used for completion only when the popup menu
5207 * is visible.
5208 */
5209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005210ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005211{
5212 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005213 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5214 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005215}
5216
5217/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005218 * Decide the number of completions to move forward.
5219 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5220 */
5221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005222ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005223{
5224 int h;
5225
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005226 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005227 {
5228 h = pum_get_height();
5229 if (h > 3)
5230 h -= 2; /* keep some context */
5231 return h;
5232 }
5233 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234}
5235
5236/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005237 * Return TRUE if completion with "c" should insert the match, FALSE if only
5238 * to change the currently selected completion.
5239 */
5240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005241ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005242{
5243 switch (c)
5244 {
5245 case K_UP:
5246 case K_DOWN:
5247 case K_PAGEDOWN:
5248 case K_KPAGEDOWN:
5249 case K_S_DOWN:
5250 case K_PAGEUP:
5251 case K_KPAGEUP:
5252 case K_S_UP:
5253 return FALSE;
5254 }
5255 return TRUE;
5256}
5257
5258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 * Do Insert mode completion.
5260 * Called when character "c" was typed, which has a meaning for completion.
5261 * Returns OK if completion was done, FAIL if something failed (out of mem).
5262 */
5263 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005264ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005266 char_u *line;
5267 int startcol = 0; /* column where searched text starts */
5268 colnr_T curs_col; /* cursor column */
5269 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005270 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005271 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005272 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005273 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
Bram Moolenaare3226be2005-12-18 22:10:00 +00005275 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005276 insert_match = ins_compl_use_match(c);
5277
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 /* First time we hit ^N or ^P (in a row, I mean) */
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 did_ai = FALSE;
5283#ifdef FEAT_SMARTINDENT
5284 did_si = FALSE;
5285 can_si = FALSE;
5286 can_si_back = FALSE;
5287#endif
5288 if (stop_arrow() == FAIL)
5289 return FAIL;
5290
5291 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005292 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005293 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005295 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 * "compl_startpos" to the cursor as a pattern to add a new word
5297 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005298 * "compl_startpos" is not in the same line as the cursor then fix it
5299 * (the line has been split because it was longer than 'tw'). if SOL
5300 * is set then skip the previous pattern, a word at the beginning of
5301 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005302 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5303 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
5305 /*
5306 * it is a continued search
5307 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005309 if (ctrl_x_mode == CTRL_X_NORMAL
5310 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5311 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 /* line (probably) wrapped, set compl_startpos to the
5316 * first non_blank in the line, if it is not a wordchar
5317 * include it to get a better pattern, but then we don't
5318 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005319 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005320 compl_startpos.col = compl_col;
5321 compl_startpos.lnum = curwin->w_cursor.lnum;
5322 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324 else
5325 {
5326 /* S_IPOS was set when we inserted a word that was at the
5327 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 * mode but first we need to redefine compl_startpos */
5329 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005331 compl_cont_status |= CONT_SOL;
5332 compl_startpos.col = (colnr_T)(skipwhite(
5333 line + compl_length
5334 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005339 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005340 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 compl_cont_status &= ~CONT_SOL;
5345 compl_length = (IOSIZE - MIN_SPACE);
5346 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5349 if (compl_length < 1)
5350 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005352 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005363 if (ctrl_x_mode != CTRL_X_NORMAL)
5364 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 compl_cont_status = 0;
5366 compl_cont_status |= CONT_N_ADDS;
5367 compl_startpos = curwin->w_cursor;
5368 startcol = (int)curs_col;
5369 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371
5372 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005373 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5377 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005378 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 compl_col += ++startcol;
5383 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
5385 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005386 compl_pattern = str_foldcase(line + compl_col,
5387 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 compl_pattern = vim_strnsave(line + compl_col,
5390 compl_length);
5391 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 return FAIL;
5393 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 char_u *prefix = (char_u *)"\\<";
5397
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005398 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005400 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005401 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 if (!vim_iswordp(line + compl_col)
5404 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 && (
5406#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#endif
5411 )))
5412 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 STRCPY((char *)compl_pattern, prefix);
5414 (void)quote_meta(compl_pattern + STRLEN(prefix),
5415 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005417 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#endif
5423 )
5424 {
5425 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5427 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 compl_col += curs_col;
5430 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432 else
5433 {
5434#ifdef FEAT_MBYTE
5435 /* Search the point of change class of multibyte character
5436 * or not a word single byte character backward. */
5437 if (has_mbyte)
5438 {
5439 int base_class;
5440 int head_off;
5441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005442 startcol -= (*mb_head_off)(line, line + startcol);
5443 base_class = mb_get_class(line + startcol);
5444 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005446 head_off = (*mb_head_off)(line, line + startcol);
5447 if (base_class != mb_get_class(line + startcol
5448 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
5452 }
5453 else
5454#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005457 compl_col += ++startcol;
5458 compl_length = (int)curs_col - startcol;
5459 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
5461 /* Only match word with at least two chars -- webb
5462 * there's no need to call quote_meta,
5463 * alloc(7) is enough -- Acevedo
5464 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005465 compl_pattern = alloc(7);
5466 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 STRCPY((char *)compl_pattern, "\\<");
5469 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5470 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 else
5473 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005475 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005478 STRCPY((char *)compl_pattern, "\\<");
5479 (void)quote_meta(compl_pattern + 2, line + compl_col,
5480 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
5482 }
5483 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005484 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005486 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005487 compl_length = (int)curs_col - (int)compl_col;
5488 if (compl_length < 0) /* cursor in indent: empty pattern */
5489 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005491 compl_pattern = str_foldcase(line + compl_col, compl_length,
5492 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005494 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5495 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 return FAIL;
5497 }
5498 else if (ctrl_x_mode == CTRL_X_FILES)
5499 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005500 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005501 if (startcol > 0)
5502 {
5503 char_u *p = line + startcol;
5504
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005505 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005506 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005507 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005508 if (p == line && vim_isfilec(PTR2CHAR(p)))
5509 startcol = 0;
5510 else
5511 startcol = (int)(p - line) + 1;
5512 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005513
Bram Moolenaar0300e462013-09-08 16:03:45 +02005514 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 compl_length = (int)curs_col - startcol;
5516 compl_pattern = addstar(line + compl_col, compl_length,
5517 EXPAND_FILES);
5518 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 return FAIL;
5520 }
5521 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5522 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005523 compl_pattern = vim_strnsave(line, curs_col);
5524 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005526 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005527 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005528 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5529 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005530 /* No completion possible, use an empty pattern to get a
5531 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005532 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005533 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005534 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5535 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005537 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005538 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005539#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005540 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005541 * Call user defined function 'completefunc' with "a:findstart"
5542 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005543 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005544 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005545 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005546 char_u *funcname;
5547 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005548 win_T *curwin_save;
5549 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005550
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005551 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005552 * string */
5553 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5554 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5555 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005556 {
5557 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5558 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005559 /* restore did_ai, so that adding comment leader works */
5560 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005561 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005562 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005563
Bram Moolenaarffa96842018-06-12 22:05:14 +02005564 args[0].v_type = VAR_NUMBER;
5565 args[0].vval.v_number = 1;
5566 args[1].v_type = VAR_STRING;
5567 args[1].vval.v_string = (char_u *)"";
5568 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005569 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005570 curwin_save = curwin;
5571 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005572 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005573 if (curwin_save != curwin || curbuf_save != curbuf)
5574 {
5575 EMSG(_(e_complwin));
5576 return FAIL;
5577 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005578 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005579 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005580 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005581 {
5582 EMSG(_(e_compldel));
5583 return FAIL;
5584 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005585
Bram Moolenaar6110a002012-01-26 18:58:38 +01005586 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005587 * cancel the complete without an error.
5588 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005589 if (col == -2)
5590 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005591 if (col == -3)
5592 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005593 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005594 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005595 if (!shortmess(SHM_COMPLETIONMENU))
5596 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005597 return FAIL;
5598 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005599
Bram Moolenaar82139082011-09-14 16:52:09 +02005600 /*
5601 * Reset extended parameters of completion, when start new
5602 * completion.
5603 */
5604 compl_opt_refresh_always = FALSE;
5605
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005606 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005607 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005608 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005609 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005610 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005611
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005612 /* Setup variables for completion. Need to obtain "line" again,
5613 * it may have become invalid. */
5614 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005615 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005616 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5617 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005618#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005619 return FAIL;
5620 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005621 else if (ctrl_x_mode == CTRL_X_SPELL)
5622 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005623#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005624 if (spell_bad_len > 0)
5625 compl_col = curs_col - spell_bad_len;
5626 else
5627 compl_col = spell_word_start(startcol);
5628 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005629 {
5630 compl_length = 0;
5631 compl_col = curs_col;
5632 }
5633 else
5634 {
5635 spell_expand_check_cap(compl_col);
5636 compl_length = (int)curs_col - compl_col;
5637 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005638 /* Need to obtain "line" again, it may have become invalid. */
5639 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005640 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5641 if (compl_pattern == NULL)
5642#endif
5643 return FAIL;
5644 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005645 else
5646 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005647 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005648 return FAIL;
5649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005651 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005654 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 /* Insert a new line, keep indentation but ignore 'comments' */
5657#ifdef FEAT_COMMENTS
5658 char_u *old = curbuf->b_p_com;
5659
5660 curbuf->b_p_com = (char_u *)"";
5661#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005662 compl_startpos.lnum = curwin->w_cursor.lnum;
5663 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 ins_eol('\r');
5665#ifdef FEAT_COMMENTS
5666 curbuf->b_p_com = old;
5667#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005668 compl_length = 0;
5669 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 }
5671 }
5672 else
5673 {
5674 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005675 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005678 if (compl_cont_status & CONT_LOCAL)
5679 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 else
5681 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5682
Bram Moolenaar62951b12011-09-21 18:23:05 +02005683 /* If any of the original typed text has been changed we need to fix
5684 * the redo buffer. */
5685 ins_compl_fixRedoBufForLeader(NULL);
5686
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005687 /* Always add completion for the original text. */
5688 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005689 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5690 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005691 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005693 VIM_CLEAR(compl_pattern);
5694 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 return FAIL;
5696 }
5697
5698 /* showmode might reset the internal line pointers, so it must
5699 * be called before line = ml_get(), or when this address is no
5700 * longer needed. -- Acevedo.
5701 */
5702 edit_submode_extra = (char_u *)_("-- Searching...");
5703 edit_submode_highl = HLF_COUNT;
5704 showmode();
5705 edit_submode_extra = NULL;
5706 out_flush();
5707 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005708 else if (insert_match && stop_arrow() == FAIL)
5709 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005711 compl_shown_match = compl_curr_match;
5712 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
5714 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005715 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005717 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005718 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005719 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005721 /* may undisplay the popup menu */
5722 ins_compl_upd_pum();
5723
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005724 if (n > 1) /* all matches have been found */
5725 compl_matches = n;
5726 compl_curr_match = compl_shown_match;
5727 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
Bram Moolenaard68071d2006-05-02 22:08:30 +00005729 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5730 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (got_int && !global_busy)
5732 {
5733 (void)vgetc();
5734 got_int = FALSE;
5735 }
5736
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005737 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005738 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005740 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5741 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5743 edit_submode_highl = HLF_E;
5744 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5745 * because we couldn't expand anything at first place, but if we used
5746 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5747 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005748 if ( compl_length > 1
5749 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005750 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5752 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005753 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755
Bram Moolenaar572cb562005-08-05 21:35:02 +00005756 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005757 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005759 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 if (edit_submode_extra == NULL)
5762 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005763 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
5765 edit_submode_extra = (char_u *)_("Back at original");
5766 edit_submode_highl = HLF_W;
5767 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005768 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
5770 edit_submode_extra = (char_u *)_("Word from other line");
5771 edit_submode_highl = HLF_COUNT;
5772 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005773 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 edit_submode_extra = (char_u *)_("The only match");
5776 edit_submode_highl = HLF_COUNT;
5777 }
5778 else
5779 {
5780 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005781 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005783 int number = 0;
5784 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005786 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 /* search backwards for the first valid (!= -1) number.
5789 * This should normally succeed already at the first loop
5790 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005791 for (match = compl_curr_match->cp_prev; match != NULL
5792 && match != compl_first_match;
5793 match = match->cp_prev)
5794 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005796 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 break;
5798 }
5799 if (match != NULL)
5800 /* go up and assign all numbers which are not assigned
5801 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005802 for (match = match->cp_next;
5803 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005804 match = match->cp_next)
5805 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807 else /* BACKWARD */
5808 {
5809 /* search forwards (upwards) for the first valid (!= -1)
5810 * number. This should normally succeed already at the
5811 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005812 for (match = compl_curr_match->cp_next; match != NULL
5813 && match != compl_first_match;
5814 match = match->cp_next)
5815 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005817 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819 }
5820 if (match != NULL)
5821 /* go down and assign all numbers which are not
5822 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005823 for (match = match->cp_prev; match
5824 && match->cp_number == -1;
5825 match = match->cp_prev)
5826 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 }
5828 }
5829
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005830 /* The match should always have a sequence number now, this is
5831 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005832 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005834 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5835 * Translations may need more than twice that. */
5836 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005838 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005839 vim_snprintf((char *)match_ref, sizeof(match_ref),
5840 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005841 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005843 vim_snprintf((char *)match_ref, sizeof(match_ref),
5844 _("match %d"),
5845 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 edit_submode_extra = match_ref;
5847 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005848 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 curs_columns(FALSE);
5850 }
5851 }
5852 }
5853
5854 /* Show a message about what (completion) mode we're in. */
5855 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005856 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005858 if (edit_submode_extra != NULL)
5859 {
5860 if (!p_smd)
5861 msg_attr(edit_submode_extra,
5862 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005863 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005864 }
5865 else
5866 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
Bram Moolenaard68071d2006-05-02 22:08:30 +00005869 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005870 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005871 show_pum(save_w_wrow, save_w_leftcol);
5872
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005873 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005874 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005875
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 return OK;
5877}
5878
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005879 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005880show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005881{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005882 /* RedrawingDisabled may be set when invoked through complete(). */
5883 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005884
Bram Moolenaarcb036422017-03-01 12:29:10 +01005885 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005886
Bram Moolenaarcb036422017-03-01 12:29:10 +01005887 /* If the cursor moved or the display scrolled we need to remove the pum
5888 * first. */
5889 setcursor();
5890 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5891 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005892
Bram Moolenaarcb036422017-03-01 12:29:10 +01005893 ins_compl_show_pum();
5894 setcursor();
5895 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005896}
5897
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898/*
5899 * Looks in the first "len" chars. of "src" for search-metachars.
5900 * If dest is not NULL the chars. are copied there quoting (with
5901 * a backslash) the metachars, and dest would be NUL terminated.
5902 * Returns the length (needed) of dest
5903 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005904 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005905quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005907 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005909 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
5911 switch (*src)
5912 {
5913 case '.':
5914 case '*':
5915 case '[':
5916 if (ctrl_x_mode == CTRL_X_DICTIONARY
5917 || ctrl_x_mode == CTRL_X_THESAURUS)
5918 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005919 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 case '~':
5921 if (!p_magic) /* quote these only if magic is set */
5922 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005923 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 case '\\':
5925 if (ctrl_x_mode == CTRL_X_DICTIONARY
5926 || ctrl_x_mode == CTRL_X_THESAURUS)
5927 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005928 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 case '^': /* currently it's not needed. */
5930 case '$':
5931 m++;
5932 if (dest != NULL)
5933 *dest++ = '\\';
5934 break;
5935 }
5936 if (dest != NULL)
5937 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005938# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 /* Copy remaining bytes of a multibyte character. */
5940 if (has_mbyte)
5941 {
5942 int i, mb_len;
5943
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005944 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 if (mb_len > 0 && len >= mb_len)
5946 for (i = 0; i < mb_len; ++i)
5947 {
5948 --len;
5949 ++src;
5950 if (dest != NULL)
5951 *dest++ = *src;
5952 }
5953 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 }
5956 if (dest != NULL)
5957 *dest = NUL;
5958
5959 return m;
5960}
5961#endif /* FEAT_INS_EXPAND */
5962
5963/*
5964 * Next character is interpreted literally.
5965 * A one, two or three digit decimal number is interpreted as its byte value.
5966 * If one or two digits are entered, the next character is given to vungetc().
5967 * For Unicode a character > 255 may be returned.
5968 */
5969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005970get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
5972 int cc;
5973 int nc;
5974 int i;
5975 int hex = FALSE;
5976 int octal = FALSE;
5977#ifdef FEAT_MBYTE
5978 int unicode = 0;
5979#endif
5980
5981 if (got_int)
5982 return Ctrl_C;
5983
5984#ifdef FEAT_GUI
5985 /*
5986 * In GUI there is no point inserting the internal code for a special key.
5987 * It is more useful to insert the string "<KEY>" instead. This would
5988 * probably be useful in a text window too, but it would not be
5989 * vi-compatible (maybe there should be an option for it?) -- webb
5990 */
5991 if (gui.in_use)
5992 ++allow_keys;
5993#endif
5994#ifdef USE_ON_FLY_SCROLL
5995 dont_scroll = TRUE; /* disallow scrolling here */
5996#endif
5997 ++no_mapping; /* don't map the next key hits */
5998 cc = 0;
5999 i = 0;
6000 for (;;)
6001 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006002 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003#ifdef FEAT_CMDL_INFO
6004 if (!(State & CMDLINE)
6005# ifdef FEAT_MBYTE
6006 && MB_BYTE2LEN_CHECK(nc) == 1
6007# endif
6008 )
6009 add_to_showcmd(nc);
6010#endif
6011 if (nc == 'x' || nc == 'X')
6012 hex = TRUE;
6013 else if (nc == 'o' || nc == 'O')
6014 octal = TRUE;
6015#ifdef FEAT_MBYTE
6016 else if (nc == 'u' || nc == 'U')
6017 unicode = nc;
6018#endif
6019 else
6020 {
6021 if (hex
6022#ifdef FEAT_MBYTE
6023 || unicode != 0
6024#endif
6025 )
6026 {
6027 if (!vim_isxdigit(nc))
6028 break;
6029 cc = cc * 16 + hex2nr(nc);
6030 }
6031 else if (octal)
6032 {
6033 if (nc < '0' || nc > '7')
6034 break;
6035 cc = cc * 8 + nc - '0';
6036 }
6037 else
6038 {
6039 if (!VIM_ISDIGIT(nc))
6040 break;
6041 cc = cc * 10 + nc - '0';
6042 }
6043
6044 ++i;
6045 }
6046
6047 if (cc > 255
6048#ifdef FEAT_MBYTE
6049 && unicode == 0
6050#endif
6051 )
6052 cc = 255; /* limit range to 0-255 */
6053 nc = 0;
6054
6055 if (hex) /* hex: up to two chars */
6056 {
6057 if (i >= 2)
6058 break;
6059 }
6060#ifdef FEAT_MBYTE
6061 else if (unicode) /* Unicode: up to four or eight chars */
6062 {
6063 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6064 break;
6065 }
6066#endif
6067 else if (i >= 3) /* decimal or octal: up to three chars */
6068 break;
6069 }
6070 if (i == 0) /* no number entered */
6071 {
6072 if (nc == K_ZERO) /* NUL is stored as NL */
6073 {
6074 cc = '\n';
6075 nc = 0;
6076 }
6077 else
6078 {
6079 cc = nc;
6080 nc = 0;
6081 }
6082 }
6083
6084 if (cc == 0) /* NUL is stored as NL */
6085 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006086#ifdef FEAT_MBYTE
6087 if (enc_dbcs && (cc & 0xff) == 0)
6088 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6089 second byte will cause trouble! */
6090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092 --no_mapping;
6093#ifdef FEAT_GUI
6094 if (gui.in_use)
6095 --allow_keys;
6096#endif
6097 if (nc)
6098 vungetc(nc);
6099 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6100 return cc;
6101}
6102
6103/*
6104 * Insert character, taking care of special keys and mod_mask
6105 */
6106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107insert_special(
6108 int c,
6109 int allow_modmask,
6110 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
6112 char_u *p;
6113 int len;
6114
6115 /*
6116 * Special function key, translate into "<Key>". Up to the last '>' is
6117 * inserted with ins_str(), so as not to replace characters in replace
6118 * mode.
6119 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6120 * unless 'allow_modmask' is TRUE.
6121 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006122#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 /* Command-key never produces a normal key */
6124 if (mod_mask & MOD_MASK_CMD)
6125 allow_modmask = TRUE;
6126#endif
6127 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6128 {
6129 p = get_special_key_name(c, mod_mask);
6130 len = (int)STRLEN(p);
6131 c = p[len - 1];
6132 if (len > 2)
6133 {
6134 if (stop_arrow() == FAIL)
6135 return;
6136 p[len - 1] = NUL;
6137 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006138 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 ctrlv = FALSE;
6140 }
6141 }
6142 if (stop_arrow() == OK)
6143 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6144}
6145
6146/*
6147 * Special characters in this context are those that need processing other
6148 * than the simple insertion that can be performed here. This includes ESC
6149 * which terminates the insert, and CR/NL which need special processing to
6150 * open up a new line. This routine tries to optimize insertions performed by
6151 * the "redo", "undo" or "put" commands, so it needs to know when it should
6152 * stop and defer processing to the "normal" mechanism.
6153 * '0' and '^' are special, because they can be followed by CTRL-D.
6154 */
6155#ifdef EBCDIC
6156# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6157#else
6158# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6159#endif
6160
6161#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006162# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006164# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165#endif
6166
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006167/*
6168 * "flags": INSCHAR_FORMAT - force formatting
6169 * INSCHAR_CTRLV - char typed just after CTRL-V
6170 * INSCHAR_NO_FEX - don't use 'formatexpr'
6171 *
6172 * NOTE: passes the flags value straight through to internal_format() which,
6173 * beside INSCHAR_FORMAT (above), is also looking for these:
6174 * INSCHAR_DO_COM - format comments
6175 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178insertchar(
6179 int c, /* character to insert or NUL */
6180 int flags, /* INSCHAR_FORMAT, etc. */
6181 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 int textwidth;
6184#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006188 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006190 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192
6193 /*
6194 * Try to break the line in two or more pieces when:
6195 * - Always do this if we have been called to do formatting only.
6196 * - Always do this when 'formatoptions' has the 'a' flag and the line
6197 * ends in white space.
6198 * - Otherwise:
6199 * - Don't do this if inserting a blank
6200 * - Don't do this if an existing character is being replaced, unless
6201 * we're in VREPLACE mode.
6202 * - Do this if the cursor is not on the line where insert started
6203 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6204 * before the insert.
6205 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6206 * before 'textwidth'
6207 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006208 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006209 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006210 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 && *ml_get_cursor() != NUL)
6214 && (curwin->w_cursor.lnum != Insstart.lnum
6215 || ((!has_format_option(FO_INS_LONG)
6216 || Insstart_textlen <= (colnr_T)textwidth)
6217 && (!fo_ins_blank
6218 || Insstart_blank_vcol <= (colnr_T)textwidth
6219 ))))))
6220 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221 /* Format with 'formatexpr' when it's set. Use internal formatting
6222 * when 'formatexpr' isn't set or it returns non-zero. */
6223#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006224 int do_internal = TRUE;
6225 colnr_T virtcol = get_nolist_virtcol()
6226 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006227
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006228 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6229 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006230 {
6231 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6232 /* It may be required to save for undo again, e.g. when setline()
6233 * was called. */
6234 ins_need_undo = TRUE;
6235 }
6236 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006238 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (c == NUL) /* only formatting was wanted */
6242 return;
6243
6244#ifdef FEAT_COMMENTS
6245 /* Check whether this character should end a comment. */
6246 if (did_ai && (int)c == end_comment_pending)
6247 {
6248 char_u *line;
6249 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6250 int middle_len, end_len;
6251 int i;
6252
6253 /*
6254 * Need to remove existing (middle) comment leader and insert end
6255 * comment leader. First, check what comment leader we can find.
6256 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006257 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6259 {
6260 /* Skip middle-comment string */
6261 while (*p && p[-1] != ':') /* find end of middle flags */
6262 ++p;
6263 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6264 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006265 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 --middle_len;
6267
6268 /* Find the end-comment string */
6269 while (*p && p[-1] != ':') /* find end of end flags */
6270 ++p;
6271 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6272
6273 /* Skip white space before the cursor */
6274 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006275 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 ;
6277 i++;
6278
6279 /* Skip to before the middle leader */
6280 i -= middle_len;
6281
6282 /* Check some expected things before we go on */
6283 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6284 {
6285 /* Backspace over all the stuff we want to replace */
6286 backspace_until_column(i);
6287
6288 /*
6289 * Insert the end-comment string, except for the last
6290 * character, which will get inserted as normal later.
6291 */
6292 ins_bytes_len(lead_end, end_len - 1);
6293 }
6294 }
6295 }
6296 end_comment_pending = NUL;
6297#endif
6298
6299 did_ai = FALSE;
6300#ifdef FEAT_SMARTINDENT
6301 did_si = FALSE;
6302 can_si = FALSE;
6303 can_si_back = FALSE;
6304#endif
6305
6306 /*
6307 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6308 * This speeds up normal text input considerably.
6309 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6310 * need to re-indent at a ':', or any other character (but not what
6311 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006312 * Don't do this when there an InsertCharPre autocommand is defined,
6313 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006314 * Do the check for InsertCharPre before the call to vpeekc() because the
6315 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 */
6317#ifdef USE_ON_FLY_SCROLL
6318 dont_scroll = FALSE; /* allow scrolling here */
6319#endif
6320
6321 if ( !ISSPECIAL(c)
6322#ifdef FEAT_MBYTE
6323 && (!has_mbyte || (*mb_char2len)(c) == 1)
6324#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006325 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 && vpeekc() != NUL
6327 && !(State & REPLACE_FLAG)
6328#ifdef FEAT_CINDENT
6329 && !cindent_on()
6330#endif
6331#ifdef FEAT_RIGHTLEFT
6332 && !p_ri
6333#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006334 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
6336#define INPUT_BUFLEN 100
6337 char_u buf[INPUT_BUFLEN + 1];
6338 int i;
6339 colnr_T virtcol = 0;
6340
6341 buf[0] = c;
6342 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006343 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 virtcol = get_nolist_virtcol();
6345 /*
6346 * Stop the string when:
6347 * - no more chars available
6348 * - finding a special character (command key)
6349 * - buffer is full
6350 * - running into the 'textwidth' boundary
6351 * - need to check for abbreviation: A non-word char after a word-char
6352 */
6353 while ( (c = vpeekc()) != NUL
6354 && !ISSPECIAL(c)
6355#ifdef FEAT_MBYTE
6356 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6357#endif
6358 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006359# ifdef FEAT_FKMAP
6360 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 && (textwidth == 0
6363 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6364 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6365 {
6366#ifdef FEAT_RIGHTLEFT
6367 c = vgetc();
6368 if (p_hkmap && KeyTyped)
6369 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 buf[i++] = c;
6371#else
6372 buf[i++] = vgetc();
6373#endif
6374 }
6375
6376#ifdef FEAT_DIGRAPHS
6377 do_digraph(-1); /* clear digraphs */
6378 do_digraph(buf[i-1]); /* may be the start of a digraph */
6379#endif
6380 buf[i] = NUL;
6381 ins_str(buf);
6382 if (flags & INSCHAR_CTRLV)
6383 {
6384 redo_literal(*buf);
6385 i = 1;
6386 }
6387 else
6388 i = 0;
6389 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006390 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 }
6392 else
6393 {
6394#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006395 int cc;
6396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6398 {
6399 char_u buf[MB_MAXBYTES + 1];
6400
6401 (*mb_char2bytes)(c, buf);
6402 buf[cc] = NUL;
6403 ins_char_bytes(buf, cc);
6404 AppendCharToRedobuff(c);
6405 }
6406 else
6407#endif
6408 {
6409 ins_char(c);
6410 if (flags & INSCHAR_CTRLV)
6411 redo_literal(c);
6412 else
6413 AppendCharToRedobuff(c);
6414 }
6415 }
6416}
6417
6418/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006419 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006420 *
6421 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6422 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006423 */
6424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006425internal_format(
6426 int textwidth,
6427 int second_indent,
6428 int flags,
6429 int format_only,
6430 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006431{
6432 int cc;
6433 int save_char = NUL;
6434 int haveto_redraw = FALSE;
6435 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6436#ifdef FEAT_MBYTE
6437 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6438#endif
6439 int fo_white_par = has_format_option(FO_WHITE_PAR);
6440 int first_line = TRUE;
6441#ifdef FEAT_COMMENTS
6442 colnr_T leader_len;
6443 int no_leader = FALSE;
6444 int do_comments = (flags & INSCHAR_DO_COM);
6445#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006446#ifdef FEAT_LINEBREAK
6447 int has_lbr = curwin->w_p_lbr;
6448
6449 /* make sure win_lbr_chartabsize() counts correctly */
6450 curwin->w_p_lbr = FALSE;
6451#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452
6453 /*
6454 * When 'ai' is off we don't want a space under the cursor to be
6455 * deleted. Replace it with an 'x' temporarily.
6456 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006457 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006458 {
6459 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006460 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006461 {
6462 save_char = cc;
6463 pchar_cursor('x');
6464 }
6465 }
6466
6467 /*
6468 * Repeat breaking lines, until the current line is not too long.
6469 */
6470 while (!got_int)
6471 {
6472 int startcol; /* Cursor column at entry */
6473 int wantcol; /* column at textwidth border */
6474 int foundcol; /* column for start of spaces */
6475 int end_foundcol = 0; /* column for start of word */
6476 colnr_T len;
6477 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006478 int orig_col = 0;
6479 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006480 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006481 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006482
Bram Moolenaar97b98102009-11-17 16:41:01 +00006483 virtcol = get_nolist_virtcol()
6484 + char2cells(c != NUL ? c : gchar_cursor());
6485 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006486 break;
6487
6488#ifdef FEAT_COMMENTS
6489 if (no_leader)
6490 do_comments = FALSE;
6491 else if (!(flags & INSCHAR_FORMAT)
6492 && has_format_option(FO_WRAP_COMS))
6493 do_comments = TRUE;
6494
6495 /* Don't break until after the comment leader */
6496 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006497 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006498 else
6499 leader_len = 0;
6500
6501 /* If the line doesn't start with a comment leader, then don't
6502 * start one in a following broken line. Avoids that a %word
6503 * moved to the start of the next line causes all following lines
6504 * to start with %. */
6505 if (leader_len == 0)
6506 no_leader = TRUE;
6507#endif
6508 if (!(flags & INSCHAR_FORMAT)
6509#ifdef FEAT_COMMENTS
6510 && leader_len == 0
6511#endif
6512 && !has_format_option(FO_WRAP))
6513
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515 if ((startcol = curwin->w_cursor.col) == 0)
6516 break;
6517
6518 /* find column of textwidth border */
6519 coladvance((colnr_T)textwidth);
6520 wantcol = curwin->w_cursor.col;
6521
Bram Moolenaar97b98102009-11-17 16:41:01 +00006522 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523 foundcol = 0;
6524
6525 /*
6526 * Find position to break at.
6527 * Stop at first entered white when 'formatoptions' has 'v'
6528 */
6529 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006530 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006531 || curwin->w_cursor.lnum != Insstart.lnum
6532 || curwin->w_cursor.col >= Insstart.col)
6533 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006534 if (curwin->w_cursor.col == startcol && c != NUL)
6535 cc = c;
6536 else
6537 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006538 if (WHITECHAR(cc))
6539 {
6540 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006541 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542
6543 /* find start of sequence of blanks */
6544 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6545 {
6546 dec_cursor();
6547 cc = gchar_cursor();
6548 }
6549 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6550 break; /* only spaces in front of text */
6551#ifdef FEAT_COMMENTS
6552 /* Don't break until after the comment leader */
6553 if (curwin->w_cursor.col < leader_len)
6554 break;
6555#endif
6556 if (has_format_option(FO_ONE_LETTER))
6557 {
6558 /* do not break after one-letter words */
6559 if (curwin->w_cursor.col == 0)
6560 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006561#ifdef FEAT_COMMENTS
6562 /* do not break "#a b" when 'tw' is 2 */
6563 if (curwin->w_cursor.col <= leader_len)
6564 break;
6565#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006566 col = curwin->w_cursor.col;
6567 dec_cursor();
6568 cc = gchar_cursor();
6569
6570 if (WHITECHAR(cc))
6571 continue; /* one-letter, continue */
6572 curwin->w_cursor.col = col;
6573 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006574
6575 inc_cursor();
6576
6577 end_foundcol = end_col + 1;
6578 foundcol = curwin->w_cursor.col;
6579 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006580 break;
6581 }
6582#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006583 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006584 {
6585 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006586 if (curwin->w_cursor.col != startcol)
6587 {
6588#ifdef FEAT_COMMENTS
6589 /* Don't break until after the comment leader */
6590 if (curwin->w_cursor.col < leader_len)
6591 break;
6592#endif
6593 col = curwin->w_cursor.col;
6594 inc_cursor();
6595 /* Don't change end_foundcol if already set. */
6596 if (foundcol != curwin->w_cursor.col)
6597 {
6598 foundcol = curwin->w_cursor.col;
6599 end_foundcol = foundcol;
6600 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6601 break;
6602 }
6603 curwin->w_cursor.col = col;
6604 }
6605
6606 if (curwin->w_cursor.col == 0)
6607 break;
6608
6609 col = curwin->w_cursor.col;
6610
6611 dec_cursor();
6612 cc = gchar_cursor();
6613
6614 if (WHITECHAR(cc))
6615 continue; /* break with space */
6616#ifdef FEAT_COMMENTS
6617 /* Don't break until after the comment leader */
6618 if (curwin->w_cursor.col < leader_len)
6619 break;
6620#endif
6621
6622 curwin->w_cursor.col = col;
6623
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006624 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006625 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006626 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6627 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006628 }
6629#endif
6630 if (curwin->w_cursor.col == 0)
6631 break;
6632 dec_cursor();
6633 }
6634
6635 if (foundcol == 0) /* no spaces, cannot break line */
6636 {
6637 curwin->w_cursor.col = startcol;
6638 break;
6639 }
6640
6641 /* Going to break the line, remove any "$" now. */
6642 undisplay_dollar();
6643
6644 /*
6645 * Offset between cursor position and line break is used by replace
6646 * stack functions. VREPLACE does not use this, and backspaces
6647 * over the text instead.
6648 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006649 if (State & VREPLACE_FLAG)
6650 orig_col = startcol; /* Will start backspacing from here */
6651 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006652 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006653
6654 /*
6655 * adjust startcol for spaces that will be deleted and
6656 * characters that will remain on top line
6657 */
6658 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006659 while ((cc = gchar_cursor(), WHITECHAR(cc))
6660 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006661 inc_cursor();
6662 startcol -= curwin->w_cursor.col;
6663 if (startcol < 0)
6664 startcol = 0;
6665
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006666 if (State & VREPLACE_FLAG)
6667 {
6668 /*
6669 * In VREPLACE mode, we will backspace over the text to be
6670 * wrapped, so save a copy now to put on the next line.
6671 */
6672 saved_text = vim_strsave(ml_get_cursor());
6673 curwin->w_cursor.col = orig_col;
6674 if (saved_text == NULL)
6675 break; /* Can't do it, out of memory */
6676 saved_text[startcol] = NUL;
6677
6678 /* Backspace over characters that will move to the next line */
6679 if (!fo_white_par)
6680 backspace_until_column(foundcol);
6681 }
6682 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006683 {
6684 /* put cursor after pos. to break line */
6685 if (!fo_white_par)
6686 curwin->w_cursor.col = foundcol;
6687 }
6688
6689 /*
6690 * Split the line just before the margin.
6691 * Only insert/delete lines, but don't really redraw the window.
6692 */
6693 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6694 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6695#ifdef FEAT_COMMENTS
6696 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006697 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006699 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6700 if (!(flags & INSCHAR_COM_LIST))
6701 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702
6703 replace_offset = 0;
6704 if (first_line)
6705 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006706 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006708 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006709 * This section is for auto-wrap of numeric lists. When not
6710 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6711 * flag will be set and open_line() will handle it (as seen
6712 * above). The code here (and in get_number_indent()) will
6713 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006714 */
6715 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006716 second_indent =
6717 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006718 if (second_indent >= 0)
6719 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006720 if (State & VREPLACE_FLAG)
6721 change_indent(INDENT_SET, second_indent,
6722 FALSE, NUL, TRUE);
6723 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006724#ifdef FEAT_COMMENTS
6725 if (leader_len > 0 && second_indent - leader_len > 0)
6726 {
6727 int i;
6728 int padding = second_indent - leader_len;
6729
6730 /* We started at the first_line of a numbered list
6731 * that has a comment. the open_line() function has
6732 * inserted the proper comment leader and positioned
6733 * the cursor at the end of the split line. Now we
6734 * add the additional whitespace needed after the
6735 * comment leader for the numbered list. */
6736 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006737 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006738 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006739 }
6740 else
6741 {
6742#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006743 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006744#ifdef FEAT_COMMENTS
6745 }
6746#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006747 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006748 }
6749 first_line = FALSE;
6750 }
6751
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006752 if (State & VREPLACE_FLAG)
6753 {
6754 /*
6755 * In VREPLACE mode we have backspaced over the text to be
6756 * moved, now we re-insert it into the new line.
6757 */
6758 ins_bytes(saved_text);
6759 vim_free(saved_text);
6760 }
6761 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006762 {
6763 /*
6764 * Check if cursor is not past the NUL off the line, cindent
6765 * may have added or removed indent.
6766 */
6767 curwin->w_cursor.col += startcol;
6768 len = (colnr_T)STRLEN(ml_get_curline());
6769 if (curwin->w_cursor.col > len)
6770 curwin->w_cursor.col = len;
6771 }
6772
6773 haveto_redraw = TRUE;
6774#ifdef FEAT_CINDENT
6775 can_cindent = TRUE;
6776#endif
6777 /* moved the cursor, don't autoindent or cindent now */
6778 did_ai = FALSE;
6779#ifdef FEAT_SMARTINDENT
6780 did_si = FALSE;
6781 can_si = FALSE;
6782 can_si_back = FALSE;
6783#endif
6784 line_breakcheck();
6785 }
6786
6787 if (save_char != NUL) /* put back space after cursor */
6788 pchar_cursor(save_char);
6789
Bram Moolenaar0026d472014-09-09 16:32:39 +02006790#ifdef FEAT_LINEBREAK
6791 curwin->w_p_lbr = has_lbr;
6792#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006793 if (!format_only && haveto_redraw)
6794 {
6795 update_topline();
6796 redraw_curbuf_later(VALID);
6797 }
6798}
6799
6800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 * Called after inserting or deleting text: When 'formatoptions' includes the
6802 * 'a' flag format from the current line until the end of the paragraph.
6803 * Keep the cursor at the same position relative to the text.
6804 * The caller must have saved the cursor line for undo, following ones will be
6805 * saved here.
6806 */
6807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006808auto_format(
6809 int trailblank, /* when TRUE also format with trailing blank */
6810 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
6812 pos_T pos;
6813 colnr_T len;
6814 char_u *old;
6815 char_u *new, *pnew;
6816 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006817 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819 if (!has_format_option(FO_AUTO))
6820 return;
6821
6822 pos = curwin->w_cursor;
6823 old = ml_get_curline();
6824
6825 /* may remove added space */
6826 check_auto_format(FALSE);
6827
6828 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6829 * user might insert normal text next. Also skip formatting when "1" is
6830 * in 'formatoptions' and there is a single character before the cursor.
6831 * Otherwise the line would be broken and when typing another non-white
6832 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006833 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (*old != NUL && !trailblank && wasatend)
6835 {
6836 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006837 cc = gchar_cursor();
6838 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6839 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006841 cc = gchar_cursor();
6842 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 {
6844 curwin->w_cursor = pos;
6845 return;
6846 }
6847 curwin->w_cursor = pos;
6848 }
6849
6850#ifdef FEAT_COMMENTS
6851 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6852 * comments. */
6853 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006854 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 return;
6856#endif
6857
6858 /*
6859 * May start formatting in a previous line, so that after "x" a word is
6860 * moved to the previous line if it fits there now. Only when this is not
6861 * the start of a paragraph.
6862 */
6863 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6864 {
6865 --curwin->w_cursor.lnum;
6866 if (u_save_cursor() == FAIL)
6867 return;
6868 }
6869
6870 /*
6871 * Do the formatting and restore the cursor position. "saved_cursor" will
6872 * be adjusted for the text formatting.
6873 */
6874 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006875 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 curwin->w_cursor = saved_cursor;
6877 saved_cursor.lnum = 0;
6878
6879 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6880 {
6881 /* "cannot happen" */
6882 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6883 coladvance((colnr_T)MAXCOL);
6884 }
6885 else
6886 check_cursor_col();
6887
6888 /* Insert mode: If the cursor is now after the end of the line while it
6889 * previously wasn't, the line was broken. Because of the rule above we
6890 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6891 * formatted. */
6892 if (!wasatend && has_format_option(FO_WHITE_PAR))
6893 {
6894 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006895 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 if (curwin->w_cursor.col == len)
6897 {
6898 pnew = vim_strnsave(new, len + 2);
6899 pnew[len] = ' ';
6900 pnew[len + 1] = NUL;
6901 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6902 /* remove the space later */
6903 did_add_space = TRUE;
6904 }
6905 else
6906 /* may remove added space */
6907 check_auto_format(FALSE);
6908 }
6909
6910 check_cursor();
6911}
6912
6913/*
6914 * When an extra space was added to continue a paragraph for auto-formatting,
6915 * delete it now. The space must be under the cursor, just after the insert
6916 * position.
6917 */
6918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006919check_auto_format(
6920 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
6922 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006923 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
6925 if (did_add_space)
6926 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006927 cc = gchar_cursor();
6928 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 /* Somehow the space was removed already. */
6930 did_add_space = FALSE;
6931 else
6932 {
6933 if (!end_insert)
6934 {
6935 inc_cursor();
6936 c = gchar_cursor();
6937 dec_cursor();
6938 }
6939 if (c != NUL)
6940 {
6941 /* The space is no longer at the end of the line, delete it. */
6942 del_char(FALSE);
6943 did_add_space = FALSE;
6944 }
6945 }
6946 }
6947}
6948
6949/*
6950 * Find out textwidth to be used for formatting:
6951 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006952 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 * if invalid value, use 0.
6954 * Set default to window width (maximum 79) for "gq" operator.
6955 */
6956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006957comp_textwidth(
6958 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 int textwidth;
6961
6962 textwidth = curbuf->b_p_tw;
6963 if (textwidth == 0 && curbuf->b_p_wm)
6964 {
6965 /* The width is the window width minus 'wrapmargin' minus all the
6966 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006967 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968#ifdef FEAT_CMDWIN
6969 if (cmdwin_type != 0)
6970 textwidth -= 1;
6971#endif
6972#ifdef FEAT_FOLDING
6973 textwidth -= curwin->w_p_fdc;
6974#endif
6975#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006976 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 textwidth -= 1;
6978#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006979 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 textwidth -= 8;
6981 }
6982 if (textwidth < 0)
6983 textwidth = 0;
6984 if (ff && textwidth == 0)
6985 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006986 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (textwidth > 79)
6988 textwidth = 79;
6989 }
6990 return textwidth;
6991}
6992
6993/*
6994 * Put a character in the redo buffer, for when just after a CTRL-V.
6995 */
6996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006997redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 char_u buf[10];
7000
7001 /* Only digits need special treatment. Translate them into a string of
7002 * three digits. */
7003 if (VIM_ISDIGIT(c))
7004 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007005 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 AppendToRedobuff(buf);
7007 }
7008 else
7009 AppendCharToRedobuff(c);
7010}
7011
7012/*
7013 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007014 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 */
7016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007017start_arrow(
7018 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007020 start_arrow_common(end_insert_pos, TRUE);
7021}
7022
7023/*
7024 * Like start_arrow() but with end_change argument.
7025 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7026 */
7027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007028start_arrow_with_change(
7029 pos_T *end_insert_pos, /* can be NULL */
7030 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007031{
7032 start_arrow_common(end_insert_pos, end_change);
7033 if (!end_change)
7034 {
7035 AppendCharToRedobuff(Ctrl_G);
7036 AppendCharToRedobuff('U');
7037 }
7038}
7039
7040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007041start_arrow_common(
7042 pos_T *end_insert_pos, /* can be NULL */
7043 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007044{
7045 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {
7047 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007048 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 arrow_used = TRUE; /* this means we stopped the current insert */
7050 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007051#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007052 check_spell_redraw();
7053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007056#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007057/*
7058 * If we skipped highlighting word at cursor, do it now.
7059 * It may be skipped again, thus reset spell_redraw_lnum first.
7060 */
7061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007062check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007063{
7064 if (spell_redraw_lnum != 0)
7065 {
7066 linenr_T lnum = spell_redraw_lnum;
7067
7068 spell_redraw_lnum = 0;
7069 redrawWinline(lnum, FALSE);
7070 }
7071}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007072
7073/*
7074 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7075 * spelled word, if there is one.
7076 */
7077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007079{
7080 pos_T tpos = curwin->w_cursor;
7081
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007082 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007083 if (curwin->w_cursor.col != tpos.col)
7084 start_arrow(&tpos);
7085}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007086#endif
7087
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088/*
7089 * stop_arrow() is called before a change is made in insert mode.
7090 * If an arrow key has been used, start a new insertion.
7091 * Returns FAIL if undo is impossible, shouldn't insert then.
7092 */
7093 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007094stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
7096 if (arrow_used)
7097 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007098 Insstart = curwin->w_cursor; /* new insertion starts here */
7099 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7100 /* Don't update the original insert position when moved to the
7101 * right, except when nothing was inserted yet. */
7102 update_Insstart_orig = FALSE;
7103 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (u_save_cursor() == OK)
7106 {
7107 arrow_used = FALSE;
7108 ins_need_undo = FALSE;
7109 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 if (State & VREPLACE_FLAG)
7113 {
7114 orig_line_count = curbuf->b_ml.ml_line_count;
7115 vr_lines_changed = 1;
7116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 ResetRedobuff();
7118 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007119 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 }
7121 else if (ins_need_undo)
7122 {
7123 if (u_save_cursor() == OK)
7124 ins_need_undo = FALSE;
7125 }
7126
7127#ifdef FEAT_FOLDING
7128 /* Always open fold at the cursor line when inserting something. */
7129 foldOpenCursor();
7130#endif
7131
7132 return (arrow_used || ins_need_undo ? FAIL : OK);
7133}
7134
7135/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007136 * Do a few things to stop inserting.
7137 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7138 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 */
7140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007141stop_insert(
7142 pos_T *end_insert_pos,
7143 int esc, /* called by ins_esc() */
7144 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007146 int cc;
7147 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
7149 stop_redo_ins();
7150 replace_flush(); /* abandon replace stack */
7151
7152 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007153 * Save the inserted text for later redo with ^@ and CTRL-A.
7154 * Don't do it when "restart_edit" was set and nothing was inserted,
7155 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007157 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007158 if (did_restart_edit == 0 || (ptr != NULL
7159 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007160 {
7161 vim_free(last_insert);
7162 last_insert = ptr;
7163 last_insert_skip = new_insert_skip;
7164 }
7165 else
7166 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007168 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {
7170 /* Auto-format now. It may seem strange to do this when stopping an
7171 * insertion (or moving the cursor), but it's required when appending
7172 * a line and having it end in a space. But only do it when something
7173 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007174 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007176 pos_T tpos = curwin->w_cursor;
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 /* When the cursor is at the end of the line after a space the
7179 * formatting will move it to the following word. Avoid that by
7180 * moving the cursor onto the space. */
7181 cc = 'x';
7182 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7183 {
7184 dec_cursor();
7185 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007186 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007187 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189
7190 auto_format(TRUE, FALSE);
7191
Bram Moolenaar1c465442017-03-12 20:10:05 +01007192 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007193 {
7194 if (gchar_cursor() != NUL)
7195 inc_cursor();
7196#ifdef FEAT_VIRTUALEDIT
7197 /* If the cursor is still at the same character, also keep
7198 * the "coladd". */
7199 if (gchar_cursor() == NUL
7200 && curwin->w_cursor.lnum == tpos.lnum
7201 && curwin->w_cursor.col == tpos.col)
7202 curwin->w_cursor.coladd = tpos.coladd;
7203#endif
7204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206
7207 /* If a space was inserted for auto-formatting, remove it now. */
7208 check_auto_format(TRUE);
7209
7210 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007211 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007212 * Do this when ESC was used or moving the cursor up/down.
7213 * Check for the old position still being valid, just in case the text
7214 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007215 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007216 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7217 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007219 pos_T tpos = curwin->w_cursor;
7220
7221 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007222 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007223 for (;;)
7224 {
7225 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7226 --curwin->w_cursor.col;
7227 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007228 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007229 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007230 if (del_char(TRUE) == FAIL)
7231 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007232 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007233 if (curwin->w_cursor.lnum != tpos.lnum)
7234 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007235 else
7236 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007237 /* reset tpos, could have been invalidated in the loop above */
7238 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007239 tpos.col++;
7240 if (cc != NUL && gchar_pos(&tpos) == NUL)
7241 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 /* <C-S-Right> may have started Visual mode, adjust the position for
7245 * deleted characters. */
7246 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7247 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007248 int len = (int)STRLEN(ml_get_curline());
7249
7250 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007252 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007253#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 }
7260 did_ai = FALSE;
7261#ifdef FEAT_SMARTINDENT
7262 did_si = FALSE;
7263 can_si = FALSE;
7264 can_si_back = FALSE;
7265#endif
7266
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007267 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7268 * now in a different buffer. */
7269 if (end_insert_pos != NULL)
7270 {
7271 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007272 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007273 curbuf->b_op_end = *end_insert_pos;
7274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275}
7276
7277/*
7278 * Set the last inserted text to a single character.
7279 * Used for the replace command.
7280 */
7281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
7284 char_u *s;
7285
7286 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (last_insert != NULL)
7289 {
7290 s = last_insert;
7291 /* Use the CTRL-V only when entering a special char */
7292 if (c < ' ' || c == DEL)
7293 *s++ = Ctrl_V;
7294 s = add_char2buf(c, s);
7295 *s++ = ESC;
7296 *s++ = NUL;
7297 last_insert_skip = 0;
7298 }
7299}
7300
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007301#if defined(EXITFREE) || defined(PROTO)
7302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007303free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007304{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007305 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007306# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007307 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007308# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007309}
7310#endif
7311
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312/*
7313 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7314 * and CSI. Handle multi-byte characters.
7315 * Returns a pointer to after the added bytes.
7316 */
7317 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007318add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
7320#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007321 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 int i;
7323 int len;
7324
7325 len = (*mb_char2bytes)(c, temp);
7326 for (i = 0; i < len; ++i)
7327 {
7328 c = temp[i];
7329#endif
7330 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7331 if (c == K_SPECIAL)
7332 {
7333 *s++ = K_SPECIAL;
7334 *s++ = KS_SPECIAL;
7335 *s++ = KE_FILLER;
7336 }
7337#ifdef FEAT_GUI
7338 else if (c == CSI)
7339 {
7340 *s++ = CSI;
7341 *s++ = KS_EXTRA;
7342 *s++ = (int)KE_CSI;
7343 }
7344#endif
7345 else
7346 *s++ = c;
7347#ifdef FEAT_MBYTE
7348 }
7349#endif
7350 return s;
7351}
7352
7353/*
7354 * move cursor to start of line
7355 * if flags & BL_WHITE move to first non-white
7356 * if flags & BL_SOL move to first non-white if startofline is set,
7357 * otherwise keep "curswant" column
7358 * if flags & BL_FIX don't leave the cursor on a NUL.
7359 */
7360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007361beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
7363 if ((flags & BL_SOL) && !p_sol)
7364 coladvance(curwin->w_curswant);
7365 else
7366 {
7367 curwin->w_cursor.col = 0;
7368#ifdef FEAT_VIRTUALEDIT
7369 curwin->w_cursor.coladd = 0;
7370#endif
7371
7372 if (flags & (BL_WHITE | BL_SOL))
7373 {
7374 char_u *ptr;
7375
Bram Moolenaar1c465442017-03-12 20:10:05 +01007376 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7378 ++curwin->w_cursor.col;
7379 }
7380 curwin->w_set_curswant = TRUE;
7381 }
7382}
7383
7384/*
7385 * oneright oneleft cursor_down cursor_up
7386 *
7387 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007388 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 * Return OK when successful, FAIL when we hit a line of file boundary.
7390 */
7391
7392 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007393oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
7398#ifdef FEAT_VIRTUALEDIT
7399 if (virtual_active())
7400 {
7401 pos_T prevpos = curwin->w_cursor;
7402
7403 /* Adjust for multi-wide char (excluding TAB) */
7404 ptr = ml_get_cursor();
7405 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007406# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007408# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 ))
7412 ? ptr2cells(ptr) : 1));
7413 curwin->w_set_curswant = TRUE;
7414 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7415 return (prevpos.col != curwin->w_cursor.col
7416 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7417 }
7418#endif
7419
7420 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007421 if (*ptr == NUL)
7422 return FAIL; /* already at the very end */
7423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007425 if (has_mbyte)
7426 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 else
7428#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007429 l = 1;
7430
7431 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7432 * contains "onemore". */
7433 if (ptr[l] == NUL
7434#ifdef FEAT_VIRTUALEDIT
7435 && (ve_flags & VE_ONEMORE) == 0
7436#endif
7437 )
7438 return FAIL;
7439 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
7441 curwin->w_set_curswant = TRUE;
7442 return OK;
7443}
7444
7445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007446oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447{
7448#ifdef FEAT_VIRTUALEDIT
7449 if (virtual_active())
7450 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007451# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 int v = getviscol();
7455
7456 if (v == 0)
7457 return FAIL;
7458
7459# ifdef FEAT_LINEBREAK
7460 /* We might get stuck on 'showbreak', skip over it. */
7461 width = 1;
7462 for (;;)
7463 {
7464 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007465 /* getviscol() is slow, skip it when 'showbreak' is empty,
7466 * 'breakindent' is not set and there are no multi-byte
7467 * characters */
7468 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469# ifdef FEAT_MBYTE
7470 && !has_mbyte
7471# endif
7472 ) || getviscol() < v)
7473 break;
7474 ++width;
7475 }
7476# else
7477 coladvance(v - 1);
7478# endif
7479
7480 if (curwin->w_cursor.coladd == 1)
7481 {
7482 char_u *ptr;
7483
7484 /* Adjust for multi-wide char (not a TAB) */
7485 ptr = ml_get_cursor();
7486 if (*ptr != TAB && vim_isprintc(
7487# ifdef FEAT_MBYTE
7488 (*mb_ptr2char)(ptr)
7489# else
7490 *ptr
7491# endif
7492 ) && ptr2cells(ptr) > 1)
7493 curwin->w_cursor.coladd = 0;
7494 }
7495
7496 curwin->w_set_curswant = TRUE;
7497 return OK;
7498 }
7499#endif
7500
7501 if (curwin->w_cursor.col == 0)
7502 return FAIL;
7503
7504 curwin->w_set_curswant = TRUE;
7505 --curwin->w_cursor.col;
7506
7507#ifdef FEAT_MBYTE
7508 /* if the character on the left of the current cursor is a multi-byte
7509 * character, move to its first byte */
7510 if (has_mbyte)
7511 mb_adjust_cursor();
7512#endif
7513 return OK;
7514}
7515
7516 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007517cursor_up(
7518 long n,
7519 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 linenr_T lnum;
7522
7523 if (n > 0)
7524 {
7525 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007526 /* This fails if the cursor is already in the first line or the count
7527 * is larger than the line number and '-' is in 'cpoptions' */
7528 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 return FAIL;
7530 if (n >= lnum)
7531 lnum = 1;
7532 else
7533#ifdef FEAT_FOLDING
7534 if (hasAnyFolding(curwin))
7535 {
7536 /*
7537 * Count each sequence of folded lines as one logical line.
7538 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007539 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 (void)hasFolding(lnum, &lnum, NULL);
7541
7542 while (n--)
7543 {
7544 /* move up one line */
7545 --lnum;
7546 if (lnum <= 1)
7547 break;
7548 /* If we entered a fold, move to the beginning, unless in
7549 * Insert mode or when 'foldopen' contains "all": it will open
7550 * in a moment. */
7551 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7552 (void)hasFolding(lnum, &lnum, NULL);
7553 }
7554 if (lnum < 1)
7555 lnum = 1;
7556 }
7557 else
7558#endif
7559 lnum -= n;
7560 curwin->w_cursor.lnum = lnum;
7561 }
7562
7563 /* try to advance to the column we want to be at */
7564 coladvance(curwin->w_curswant);
7565
7566 if (upd_topline)
7567 update_topline(); /* make sure curwin->w_topline is valid */
7568
7569 return OK;
7570}
7571
7572/*
7573 * Cursor down a number of logical lines.
7574 */
7575 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007576cursor_down(
7577 long n,
7578 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
7580 linenr_T lnum;
7581
7582 if (n > 0)
7583 {
7584 lnum = curwin->w_cursor.lnum;
7585#ifdef FEAT_FOLDING
7586 /* Move to last line of fold, will fail if it's the end-of-file. */
7587 (void)hasFolding(lnum, NULL, &lnum);
7588#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007589 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007590 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007591 if (lnum >= curbuf->b_ml.ml_line_count
7592 || (lnum + n > curbuf->b_ml.ml_line_count
7593 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 return FAIL;
7595 if (lnum + n >= curbuf->b_ml.ml_line_count)
7596 lnum = curbuf->b_ml.ml_line_count;
7597 else
7598#ifdef FEAT_FOLDING
7599 if (hasAnyFolding(curwin))
7600 {
7601 linenr_T last;
7602
7603 /* count each sequence of folded lines as one logical line */
7604 while (n--)
7605 {
7606 if (hasFolding(lnum, NULL, &last))
7607 lnum = last + 1;
7608 else
7609 ++lnum;
7610 if (lnum >= curbuf->b_ml.ml_line_count)
7611 break;
7612 }
7613 if (lnum > curbuf->b_ml.ml_line_count)
7614 lnum = curbuf->b_ml.ml_line_count;
7615 }
7616 else
7617#endif
7618 lnum += n;
7619 curwin->w_cursor.lnum = lnum;
7620 }
7621
7622 /* try to advance to the column we want to be at */
7623 coladvance(curwin->w_curswant);
7624
7625 if (upd_topline)
7626 update_topline(); /* make sure curwin->w_topline is valid */
7627
7628 return OK;
7629}
7630
7631/*
7632 * Stuff the last inserted text in the read buffer.
7633 * Last_insert actually is a copy of the redo buffer, so we
7634 * first have to remove the command.
7635 */
7636 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007637stuff_inserted(
7638 int c, /* Command character to be inserted */
7639 long count, /* Repeat this many times */
7640 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
7642 char_u *esc_ptr;
7643 char_u *ptr;
7644 char_u *last_ptr;
7645 char_u last = NUL;
7646
7647 ptr = get_last_insert();
7648 if (ptr == NULL)
7649 {
7650 EMSG(_(e_noinstext));
7651 return FAIL;
7652 }
7653
7654 /* may want to stuff the command character, to start Insert mode */
7655 if (c != NUL)
7656 stuffcharReadbuff(c);
7657 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7658 *esc_ptr = NUL; /* remove the ESC */
7659
7660 /* when the last char is either "0" or "^" it will be quoted if no ESC
7661 * comes after it OR if it will inserted more than once and "ptr"
7662 * starts with ^D. -- Acevedo
7663 */
7664 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7665 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7666 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7667 {
7668 last = *last_ptr;
7669 *last_ptr = NUL;
7670 }
7671
7672 do
7673 {
7674 stuffReadbuff(ptr);
7675 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7676 if (last)
7677 stuffReadbuff((char_u *)(last == '0'
7678 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7679 : IF_EB("\026^", CTRL_V_STR "^")));
7680 }
7681 while (--count > 0);
7682
7683 if (last)
7684 *last_ptr = last;
7685
7686 if (esc_ptr != NULL)
7687 *esc_ptr = ESC; /* put the ESC back */
7688
7689 /* may want to stuff a trailing ESC, to get out of Insert mode */
7690 if (!no_esc)
7691 stuffcharReadbuff(ESC);
7692
7693 return OK;
7694}
7695
7696 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007697get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
7699 if (last_insert == NULL)
7700 return NULL;
7701 return last_insert + last_insert_skip;
7702}
7703
7704/*
7705 * Get last inserted string, and remove trailing <Esc>.
7706 * Returns pointer to allocated memory (must be freed) or NULL.
7707 */
7708 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007709get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 char_u *s;
7712 int len;
7713
7714 if (last_insert == NULL)
7715 return NULL;
7716 s = vim_strsave(last_insert + last_insert_skip);
7717 if (s != NULL)
7718 {
7719 len = (int)STRLEN(s);
7720 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7721 s[len - 1] = NUL;
7722 }
7723 return s;
7724}
7725
7726/*
7727 * Check the word in front of the cursor for an abbreviation.
7728 * Called when the non-id character "c" has been entered.
7729 * When an abbreviation is recognized it is removed from the text and
7730 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7731 */
7732 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007733echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
7735 /* Don't check for abbreviation in paste mode, when disabled and just
7736 * after moving around with cursor keys. */
7737 if (p_paste || no_abbr || arrow_used)
7738 return FALSE;
7739
7740 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7741 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7742}
7743
7744/*
7745 * replace-stack functions
7746 *
7747 * When replacing characters, the replaced characters are remembered for each
7748 * new character. This is used to re-insert the old text when backspacing.
7749 *
7750 * There is a NUL headed list of characters for each character that is
7751 * currently in the file after the insertion point. When BS is used, one NUL
7752 * headed list is put back for the deleted character.
7753 *
7754 * For a newline, there are two NUL headed lists. One contains the characters
7755 * that the NL replaced. The extra one stores the characters after the cursor
7756 * that were deleted (always white space).
7757 *
7758 * Replace_offset is normally 0, in which case replace_push will add a new
7759 * character at the end of the stack. If replace_offset is not 0, that many
7760 * characters will be left on the stack above the newly inserted character.
7761 */
7762
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007763static char_u *replace_stack = NULL;
7764static long replace_stack_nr = 0; /* next entry in replace stack */
7765static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766
7767 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007768replace_push(
7769 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771 char_u *p;
7772
7773 if (replace_stack_nr < replace_offset) /* nothing to do */
7774 return;
7775 if (replace_stack_len <= replace_stack_nr)
7776 {
7777 replace_stack_len += 50;
7778 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7779 if (p == NULL) /* out of memory */
7780 {
7781 replace_stack_len -= 50;
7782 return;
7783 }
7784 if (replace_stack != NULL)
7785 {
7786 mch_memmove(p, replace_stack,
7787 (size_t)(replace_stack_nr * sizeof(char_u)));
7788 vim_free(replace_stack);
7789 }
7790 replace_stack = p;
7791 }
7792 p = replace_stack + replace_stack_nr - replace_offset;
7793 if (replace_offset)
7794 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7795 *p = c;
7796 ++replace_stack_nr;
7797}
7798
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007799#if defined(FEAT_MBYTE) || defined(PROTO)
7800/*
7801 * Push a character onto the replace stack. Handles a multi-byte character in
7802 * reverse byte order, so that the first byte is popped off first.
7803 * Return the number of bytes done (includes composing characters).
7804 */
7805 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007806replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007807{
7808 int l = (*mb_ptr2len)(p);
7809 int j;
7810
7811 for (j = l - 1; j >= 0; --j)
7812 replace_push(p[j]);
7813 return l;
7814}
7815#endif
7816
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817/*
7818 * Pop one item from the replace stack.
7819 * return -1 if stack empty
7820 * return replaced character or NUL otherwise
7821 */
7822 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007823replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824{
7825 if (replace_stack_nr == 0)
7826 return -1;
7827 return (int)replace_stack[--replace_stack_nr];
7828}
7829
7830/*
7831 * Join the top two items on the replace stack. This removes to "off"'th NUL
7832 * encountered.
7833 */
7834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007835replace_join(
7836 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
7838 int i;
7839
7840 for (i = replace_stack_nr; --i >= 0; )
7841 if (replace_stack[i] == NUL && off-- <= 0)
7842 {
7843 --replace_stack_nr;
7844 mch_memmove(replace_stack + i, replace_stack + i + 1,
7845 (size_t)(replace_stack_nr - i));
7846 return;
7847 }
7848}
7849
7850/*
7851 * Pop bytes from the replace stack until a NUL is found, and insert them
7852 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7853 */
7854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007855replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857 int cc;
7858 int oldState = State;
7859
7860 State = NORMAL; /* don't want REPLACE here */
7861 while ((cc = replace_pop()) > 0)
7862 {
7863#ifdef FEAT_MBYTE
7864 mb_replace_pop_ins(cc);
7865#else
7866 ins_char(cc);
7867#endif
7868 dec_cursor();
7869 }
7870 State = oldState;
7871}
7872
7873#ifdef FEAT_MBYTE
7874/*
7875 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7876 * indicates a multi-byte char, pop the other bytes too.
7877 */
7878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007879mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007882 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 int i;
7884 int c;
7885
7886 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7887 {
7888 buf[0] = cc;
7889 for (i = 1; i < n; ++i)
7890 buf[i] = replace_pop();
7891 ins_bytes_len(buf, n);
7892 }
7893 else
7894 ins_char(cc);
7895
7896 if (enc_utf8)
7897 /* Handle composing chars. */
7898 for (;;)
7899 {
7900 c = replace_pop();
7901 if (c == -1) /* stack empty */
7902 break;
7903 if ((n = MB_BYTE2LEN(c)) == 1)
7904 {
7905 /* Not a multi-byte char, put it back. */
7906 replace_push(c);
7907 break;
7908 }
7909 else
7910 {
7911 buf[0] = c;
7912 for (i = 1; i < n; ++i)
7913 buf[i] = replace_pop();
7914 if (utf_iscomposing(utf_ptr2char(buf)))
7915 ins_bytes_len(buf, n);
7916 else
7917 {
7918 /* Not a composing char, put it back. */
7919 for (i = n - 1; i >= 0; --i)
7920 replace_push(buf[i]);
7921 break;
7922 }
7923 }
7924 }
7925}
7926#endif
7927
7928/*
7929 * make the replace stack empty
7930 * (called when exiting replace mode)
7931 */
7932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007933replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007935 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 replace_stack_len = 0;
7937 replace_stack_nr = 0;
7938}
7939
7940/*
7941 * Handle doing a BS for one character.
7942 * cc < 0: replace stack empty, just move cursor
7943 * cc == 0: character was inserted, delete it
7944 * cc > 0: character was replaced, put cc (first byte of original char) back
7945 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007946 * When "limit_col" is >= 0, don't delete before this column. Matters when
7947 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 */
7949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007950replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 int orig_len = 0;
7954 int ins_len;
7955 int orig_vcols = 0;
7956 colnr_T start_vcol;
7957 char_u *p;
7958 int i;
7959 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960
7961 cc = replace_pop();
7962 if (cc > 0)
7963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (State & VREPLACE_FLAG)
7965 {
7966 /* Get the number of screen cells used by the character we are
7967 * going to delete. */
7968 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7969 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971#ifdef FEAT_MBYTE
7972 if (has_mbyte)
7973 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007974 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007976 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 replace_push(cc);
7978 }
7979 else
7980#endif
7981 {
7982 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007984 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 }
7986 replace_pop_ins();
7987
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 if (State & VREPLACE_FLAG)
7989 {
7990 /* Get the number of screen cells used by the inserted characters */
7991 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007992 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 vcol = start_vcol;
7994 for (i = 0; i < ins_len; ++i)
7995 {
7996 vcol += chartabsize(p + i, vcol);
7997#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007998 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999#endif
8000 }
8001 vcol -= start_vcol;
8002
8003 /* Delete spaces that were inserted after the cursor to keep the
8004 * text aligned. */
8005 curwin->w_cursor.col += ins_len;
8006 while (vcol > orig_vcols && gchar_cursor() == ' ')
8007 {
8008 del_char(FALSE);
8009 ++orig_vcols;
8010 }
8011 curwin->w_cursor.col -= ins_len;
8012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
8014 /* mark the buffer as changed and prepare for displaying */
8015 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8016 }
8017 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008018 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019}
8020
8021#ifdef FEAT_CINDENT
8022/*
8023 * Return TRUE if C-indenting is on.
8024 */
8025 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008026cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
8028 return (!p_paste && (curbuf->b_p_cin
8029# ifdef FEAT_EVAL
8030 || *curbuf->b_p_inde != NUL
8031# endif
8032 ));
8033}
8034#endif
8035
8036#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8037/*
8038 * Re-indent the current line, based on the current contents of it and the
8039 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8040 * confused what all the part that handles Control-T is doing that I'm not.
8041 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8042 */
8043
8044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008045fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008047 int amount = get_the_indent();
8048
8049 if (amount >= 0)
8050 {
8051 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8052 if (linewhite(curwin->w_cursor.lnum))
8053 did_ai = TRUE; /* delete the indent if the line stays empty */
8054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
8056
8057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008058fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 if (p_paste)
8061 return;
8062# ifdef FEAT_LISP
8063 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8064 fixthisline(get_lisp_indent);
8065# endif
8066# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8067 else
8068# endif
8069# ifdef FEAT_CINDENT
8070 if (cindent_on())
8071 do_c_expr_indent();
8072# endif
8073}
8074
8075#endif
8076
8077#ifdef FEAT_CINDENT
8078/*
8079 * return TRUE if 'cinkeys' contains the key "keytyped",
8080 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008081 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8083 *
8084 * "keytyped" can have a few special values:
8085 * KEY_OPEN_FORW
8086 * KEY_OPEN_BACK
8087 * KEY_COMPLETE just finished completion.
8088 *
8089 * If line_is_empty is TRUE accept keys with '0' before them.
8090 */
8091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008092in_cinkeys(
8093 int keytyped,
8094 int when,
8095 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 char_u *look;
8098 int try_match;
8099 int try_match_word;
8100 char_u *p;
8101 char_u *line;
8102 int icase;
8103 int i;
8104
Bram Moolenaar30848942009-12-24 14:46:12 +00008105 if (keytyped == NUL)
8106 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8107 return FALSE;
8108
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109#ifdef FEAT_EVAL
8110 if (*curbuf->b_p_inde != NUL)
8111 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8112 else
8113#endif
8114 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8115 while (*look)
8116 {
8117 /*
8118 * Find out if we want to try a match with this key, depending on
8119 * 'when' and a '*' or '!' before the key.
8120 */
8121 switch (when)
8122 {
8123 case '*': try_match = (*look == '*'); break;
8124 case '!': try_match = (*look == '!'); break;
8125 default: try_match = (*look != '*'); break;
8126 }
8127 if (*look == '*' || *look == '!')
8128 ++look;
8129
8130 /*
8131 * If there is a '0', only accept a match if the line is empty.
8132 * But may still match when typing last char of a word.
8133 */
8134 if (*look == '0')
8135 {
8136 try_match_word = try_match;
8137 if (!line_is_empty)
8138 try_match = FALSE;
8139 ++look;
8140 }
8141 else
8142 try_match_word = FALSE;
8143
8144 /*
8145 * does it look like a control character?
8146 */
8147 if (*look == '^'
8148#ifdef EBCDIC
8149 && (Ctrl_chr(look[1]) != 0)
8150#else
8151 && look[1] >= '?' && look[1] <= '_'
8152#endif
8153 )
8154 {
8155 if (try_match && keytyped == Ctrl_chr(look[1]))
8156 return TRUE;
8157 look += 2;
8158 }
8159 /*
8160 * 'o' means "o" command, open forward.
8161 * 'O' means "O" command, open backward.
8162 */
8163 else if (*look == 'o')
8164 {
8165 if (try_match && keytyped == KEY_OPEN_FORW)
8166 return TRUE;
8167 ++look;
8168 }
8169 else if (*look == 'O')
8170 {
8171 if (try_match && keytyped == KEY_OPEN_BACK)
8172 return TRUE;
8173 ++look;
8174 }
8175
8176 /*
8177 * 'e' means to check for "else" at start of line and just before the
8178 * cursor.
8179 */
8180 else if (*look == 'e')
8181 {
8182 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8183 {
8184 p = ml_get_curline();
8185 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8186 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8187 return TRUE;
8188 }
8189 ++look;
8190 }
8191
8192 /*
8193 * ':' only causes an indent if it is at the end of a label or case
8194 * statement, or when it was before typing the ':' (to fix
8195 * class::method for C++).
8196 */
8197 else if (*look == ':')
8198 {
8199 if (try_match && keytyped == ':')
8200 {
8201 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008202 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008204 /* Need to get the line again after cin_islabel(). */
8205 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 if (curwin->w_cursor.col > 2
8207 && p[curwin->w_cursor.col - 1] == ':'
8208 && p[curwin->w_cursor.col - 2] == ':')
8209 {
8210 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008211 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008212 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 p = ml_get_curline();
8214 p[curwin->w_cursor.col - 1] = ':';
8215 if (i)
8216 return TRUE;
8217 }
8218 }
8219 ++look;
8220 }
8221
8222
8223 /*
8224 * Is it a key in <>, maybe?
8225 */
8226 else if (*look == '<')
8227 {
8228 if (try_match)
8229 {
8230 /*
8231 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8232 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8233 * >, *, : and ! keys if they really really want to.
8234 */
8235 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8236 && keytyped == look[1])
8237 return TRUE;
8238
8239 if (keytyped == get_special_key_code(look + 1))
8240 return TRUE;
8241 }
8242 while (*look && *look != '>')
8243 look++;
8244 while (*look == '>')
8245 look++;
8246 }
8247
8248 /*
8249 * Is it a word: "=word"?
8250 */
8251 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8252 {
8253 ++look;
8254 if (*look == '~')
8255 {
8256 icase = TRUE;
8257 ++look;
8258 }
8259 else
8260 icase = FALSE;
8261 p = vim_strchr(look, ',');
8262 if (p == NULL)
8263 p = look + STRLEN(look);
8264 if ((try_match || try_match_word)
8265 && curwin->w_cursor.col >= (colnr_T)(p - look))
8266 {
8267 int match = FALSE;
8268
8269#ifdef FEAT_INS_EXPAND
8270 if (keytyped == KEY_COMPLETE)
8271 {
8272 char_u *s;
8273
8274 /* Just completed a word, check if it starts with "look".
8275 * search back for the start of a word. */
8276 line = ml_get_curline();
8277# ifdef FEAT_MBYTE
8278 if (has_mbyte)
8279 {
8280 char_u *n;
8281
8282 for (s = line + curwin->w_cursor.col; s > line; s = n)
8283 {
8284 n = mb_prevptr(line, s);
8285 if (!vim_iswordp(n))
8286 break;
8287 }
8288 }
8289 else
8290# endif
8291 for (s = line + curwin->w_cursor.col; s > line; --s)
8292 if (!vim_iswordc(s[-1]))
8293 break;
8294 if (s + (p - look) <= line + curwin->w_cursor.col
8295 && (icase
8296 ? MB_STRNICMP(s, look, p - look)
8297 : STRNCMP(s, look, p - look)) == 0)
8298 match = TRUE;
8299 }
8300 else
8301#endif
8302 /* TODO: multi-byte */
8303 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8304 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8305 {
8306 line = ml_get_cursor();
8307 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8308 || !vim_iswordc(line[-(p - look) - 1]))
8309 && (icase
8310 ? MB_STRNICMP(line - (p - look), look, p - look)
8311 : STRNCMP(line - (p - look), look, p - look))
8312 == 0)
8313 match = TRUE;
8314 }
8315 if (match && try_match_word && !try_match)
8316 {
8317 /* "0=word": Check if there are only blanks before the
8318 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008319 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 (int)(curwin->w_cursor.col - (p - look)))
8321 match = FALSE;
8322 }
8323 if (match)
8324 return TRUE;
8325 }
8326 look = p;
8327 }
8328
8329 /*
8330 * ok, it's a boring generic character.
8331 */
8332 else
8333 {
8334 if (try_match && *look == keytyped)
8335 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008336 if (*look != NUL)
8337 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 }
8339
8340 /*
8341 * Skip over ", ".
8342 */
8343 look = skip_to_option_part(look);
8344 }
8345 return FALSE;
8346}
8347#endif /* FEAT_CINDENT */
8348
8349#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8350/*
8351 * Map Hebrew keyboard when in hkmap mode.
8352 */
8353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008354hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355{
8356 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8357 {
8358 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8359 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8360 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8361 static char_u map[26] =
8362 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8363 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8364 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8365 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8366 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8367 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8368 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8369 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8370 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8371
8372 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8373 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8374 /* '-1'='sofit' */
8375 else if (c == 'x')
8376 return 'X';
8377 else if (c == 'q')
8378 return '\''; /* {geresh}={'} */
8379 else if (c == 246)
8380 return ' '; /* \"o --> ' ' for a german keyboard */
8381 else if (c == 228)
8382 return ' '; /* \"a --> ' ' -- / -- */
8383 else if (c == 252)
8384 return ' '; /* \"u --> ' ' -- / -- */
8385#ifdef EBCDIC
8386 else if (islower(c))
8387#else
8388 /* NOTE: islower() does not do the right thing for us on Linux so we
8389 * do this the same was as 5.7 and previous, so it works correctly on
8390 * all systems. Specifically, the e.g. Delete and Arrow keys are
8391 * munged and won't work if e.g. searching for Hebrew text.
8392 */
8393 else if (c >= 'a' && c <= 'z')
8394#endif
8395 return (int)(map[CharOrdLow(c)] + p_aleph);
8396 else
8397 return c;
8398 }
8399 else
8400 {
8401 switch (c)
8402 {
8403 case '`': return ';';
8404 case '/': return '.';
8405 case '\'': return ',';
8406 case 'q': return '/';
8407 case 'w': return '\'';
8408
8409 /* Hebrew letters - set offset from 'a' */
8410 case ',': c = '{'; break;
8411 case '.': c = 'v'; break;
8412 case ';': c = 't'; break;
8413 default: {
8414 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8415
8416#ifdef EBCDIC
8417 /* see note about islower() above */
8418 if (!islower(c))
8419#else
8420 if (c < 'a' || c > 'z')
8421#endif
8422 return c;
8423 c = str[CharOrdLow(c)];
8424 break;
8425 }
8426 }
8427
8428 return (int)(CharOrdLow(c) + p_aleph);
8429 }
8430}
8431#endif
8432
8433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008434ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435{
8436 int need_redraw = FALSE;
8437 int regname;
8438 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008439 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440
8441 /*
8442 * If we are going to wait for a character, show a '"'.
8443 */
8444 pc_status = PC_STATUS_UNSET;
8445 if (redrawing() && !char_avail())
8446 {
8447 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008448 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449
8450 edit_putchar('"', TRUE);
8451#ifdef FEAT_CMDL_INFO
8452 add_to_showcmd_c(Ctrl_R);
8453#endif
8454 }
8455
8456#ifdef USE_ON_FLY_SCROLL
8457 dont_scroll = TRUE; /* disallow scrolling here */
8458#endif
8459
8460 /*
8461 * Don't map the register name. This also prevents the mode message to be
8462 * deleted when ESC is hit.
8463 */
8464 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008465 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8468 {
8469 /* Get a third key for literal register insertion */
8470 literally = regname;
8471#ifdef FEAT_CMDL_INFO
8472 add_to_showcmd_c(literally);
8473#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008474 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 }
8477 --no_mapping;
8478
8479#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008480 /* Don't call u_sync() while typing the expression or giving an error
8481 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 ++no_u_sync;
8483 if (regname == '=')
8484 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008485# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008487# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008488 /* Sync undo when evaluating the expression calls setline() or
8489 * append(), so that it can be undone separately. */
8490 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008491
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008493# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 /* Restore the Input Method. */
8495 if (im_on)
8496 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008499 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8500 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008501 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 else
8505 {
8506#endif
8507 if (literally == Ctrl_O || literally == Ctrl_P)
8508 {
8509 /* Append the command to the redo buffer. */
8510 AppendCharToRedobuff(Ctrl_R);
8511 AppendCharToRedobuff(literally);
8512 AppendCharToRedobuff(regname);
8513
8514 do_put(regname, BACKWARD, 1L,
8515 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8516 }
8517 else if (insert_reg(regname, literally) == FAIL)
8518 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008519 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 need_redraw = TRUE; /* remove the '"' */
8521 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008522 else if (stop_insert_mode)
8523 /* When the '=' register was used and a function was invoked that
8524 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8525 * insert anything, need to remove the '"' */
8526 need_redraw = TRUE;
8527
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528#ifdef FEAT_EVAL
8529 }
8530 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008531 if (u_sync_once == 1)
8532 ins_need_undo = TRUE;
8533 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534#endif
8535#ifdef FEAT_CMDL_INFO
8536 clear_showcmd();
8537#endif
8538
8539 /* If the inserted register is empty, we need to remove the '"' */
8540 if (need_redraw || stuff_empty())
8541 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008542
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008543 /* Disallow starting Visual mode here, would get a weird mode. */
8544 if (!vis_active && VIsual_active)
8545 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546}
8547
8548/*
8549 * CTRL-G commands in Insert mode.
8550 */
8551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008552ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
8554 int c;
8555
8556#ifdef FEAT_INS_EXPAND
8557 /* Right after CTRL-X the cursor will be after the ruler. */
8558 setcursor();
8559#endif
8560
8561 /*
8562 * Don't map the second key. This also prevents the mode message to be
8563 * deleted when ESC is hit.
8564 */
8565 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008566 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 --no_mapping;
8568 switch (c)
8569 {
8570 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8571 case K_UP:
8572 case Ctrl_K:
8573 case 'k': ins_up(TRUE);
8574 break;
8575
8576 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8577 case K_DOWN:
8578 case Ctrl_J:
8579 case 'j': ins_down(TRUE);
8580 break;
8581
8582 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008583 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008585
8586 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008587 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008588 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008589 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 break;
8591
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008592 /* CTRL-G U: do not break undo with the next char */
8593 case 'U':
8594 /* Allow one left/right cursor movement with the next char,
8595 * without breaking undo. */
8596 dont_sync_undo = MAYBE;
8597 break;
8598
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008600 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 }
8602}
8603
8604/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008605 * CTRL-^ in Insert mode.
8606 */
8607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008608ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008609{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008610 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008611 {
8612 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8613 if (State & LANGMAP)
8614 {
8615 curbuf->b_p_iminsert = B_IMODE_NONE;
8616 State &= ~LANGMAP;
8617 }
8618 else
8619 {
8620 curbuf->b_p_iminsert = B_IMODE_LMAP;
8621 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008622#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008623 im_set_active(FALSE);
8624#endif
8625 }
8626 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008627#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008628 else
8629 {
8630 /* There are no ":lmap" mappings, toggle IM */
8631 if (im_get_status())
8632 {
8633 curbuf->b_p_iminsert = B_IMODE_NONE;
8634 im_set_active(FALSE);
8635 }
8636 else
8637 {
8638 curbuf->b_p_iminsert = B_IMODE_IM;
8639 State &= ~LANGMAP;
8640 im_set_active(TRUE);
8641 }
8642 }
8643#endif
8644 set_iminsert_global();
8645 showmode();
8646#ifdef FEAT_GUI
8647 /* may show different cursor shape or color */
8648 if (gui.in_use)
8649 gui_update_cursor(TRUE, FALSE);
8650#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008651#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008652 /* Show/unshow value of 'keymap' in status lines. */
8653 status_redraw_curbuf();
8654#endif
8655}
8656
8657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 * Handle ESC in insert mode.
8659 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8660 * insert.
8661 */
8662 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008663ins_esc(
8664 long *count,
8665 int cmdchar,
8666 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
8668 int temp;
8669 static int disabled_redraw = FALSE;
8670
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008671#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008672 check_spell_redraw();
8673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674#if defined(FEAT_HANGULIN)
8675# if defined(ESC_CHG_TO_ENG_MODE)
8676 hangul_input_state_set(0);
8677# endif
8678 if (composing_hangul)
8679 {
8680 push_raw_key(composing_hangul_buffer, 2);
8681 composing_hangul = 0;
8682 }
8683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684
8685 temp = curwin->w_cursor.col;
8686 if (disabled_redraw)
8687 {
8688 --RedrawingDisabled;
8689 disabled_redraw = FALSE;
8690 }
8691 if (!arrow_used)
8692 {
8693 /*
8694 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008695 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8696 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 */
8698 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008699 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700
8701 /*
8702 * Repeating insert may take a long time. Check for
8703 * interrupt now and then.
8704 */
8705 if (*count > 0)
8706 {
8707 line_breakcheck();
8708 if (got_int)
8709 *count = 0;
8710 }
8711
8712 if (--*count > 0) /* repeat what was typed */
8713 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008714 /* Vi repeats the insert without replacing characters. */
8715 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8716 State &= ~REPLACE_FLAG;
8717
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 (void)start_redo_ins();
8719 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008720 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 ++RedrawingDisabled;
8722 disabled_redraw = TRUE;
8723 return FALSE; /* repeat the insert */
8724 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008725 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 undisplay_dollar();
8727 }
8728
8729 /* When an autoindent was removed, curswant stays after the
8730 * indent */
8731 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8732 curwin->w_set_curswant = TRUE;
8733
8734 /* Remember the last Insert position in the '^ mark. */
8735 if (!cmdmod.keepjumps)
8736 curbuf->b_last_insert = curwin->w_cursor;
8737
8738 /*
8739 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008740 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008742 if (!nomove
8743 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744#ifdef FEAT_VIRTUALEDIT
8745 || curwin->w_cursor.coladd > 0
8746#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008747 )
8748 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008749 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750#ifdef FEAT_RIGHTLEFT
8751 && !revins_on
8752#endif
8753 )
8754 {
8755#ifdef FEAT_VIRTUALEDIT
8756 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8757 {
8758 oneleft();
8759 if (restart_edit != NUL)
8760 ++curwin->w_cursor.coladd;
8761 }
8762 else
8763#endif
8764 {
8765 --curwin->w_cursor.col;
8766#ifdef FEAT_MBYTE
8767 /* Correct cursor for multi-byte character. */
8768 if (has_mbyte)
8769 mb_adjust_cursor();
8770#endif
8771 }
8772 }
8773
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008774#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 /* Disable IM to allow typing English directly for Normal mode commands.
8776 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8777 * well). */
8778 if (!(State & LANGMAP))
8779 im_save_status(&curbuf->b_p_iminsert);
8780 im_set_active(FALSE);
8781#endif
8782
8783 State = NORMAL;
8784 /* need to position cursor again (e.g. when on a TAB ) */
8785 changed_cline_bef_curs();
8786
8787#ifdef FEAT_MOUSE
8788 setmouse();
8789#endif
8790#ifdef CURSOR_SHAPE
8791 ui_cursor_shape(); /* may show different cursor shape */
8792#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008793 if (!p_ek)
8794 /* Re-enable bracketed paste mode. */
8795 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796
8797 /*
8798 * When recording or for CTRL-O, need to display the new mode.
8799 * Otherwise remove the mode message.
8800 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008801 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 showmode();
8803 else if (p_smd)
8804 MSG("");
8805
8806 return TRUE; /* exit Insert mode */
8807}
8808
8809#ifdef FEAT_RIGHTLEFT
8810/*
8811 * Toggle language: hkmap and revins_on.
8812 * Move to end of reverse inserted text.
8813 */
8814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008815ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
8817 if (revins_on && revins_chars && revins_scol >= 0)
8818 {
8819 while (gchar_cursor() != NUL && revins_chars--)
8820 ++curwin->w_cursor.col;
8821 }
8822 p_ri = !p_ri;
8823 revins_on = (State == INSERT && p_ri);
8824 if (revins_on)
8825 {
8826 revins_scol = curwin->w_cursor.col;
8827 revins_legal++;
8828 revins_chars = 0;
8829 undisplay_dollar();
8830 }
8831 else
8832 revins_scol = -1;
8833#ifdef FEAT_FKMAP
8834 if (p_altkeymap)
8835 {
8836 /*
8837 * to be consistent also for redo command, using '.'
8838 * set arrow_used to true and stop it - causing to redo
8839 * characters entered in one mode (normal/reverse insert).
8840 */
8841 arrow_used = TRUE;
8842 (void)stop_arrow();
8843 p_fkmap = curwin->w_p_rl ^ p_ri;
8844 if (p_fkmap && p_ri)
8845 State = INSERT;
8846 }
8847 else
8848#endif
8849 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8850 showmode();
8851}
8852#endif
8853
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854/*
8855 * If 'keymodel' contains "startsel", may start selection.
8856 * Returns TRUE when a CTRL-O and other keys stuffed.
8857 */
8858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008859ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
8861 if (km_startsel)
8862 switch (c)
8863 {
8864 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 case K_PAGEUP:
8867 case K_KPAGEUP:
8868 case K_PAGEDOWN:
8869 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008870# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 case K_LEFT:
8872 case K_RIGHT:
8873 case K_UP:
8874 case K_DOWN:
8875 case K_END:
8876 case K_HOME:
8877# endif
8878 if (!(mod_mask & MOD_MASK_SHIFT))
8879 break;
8880 /* FALLTHROUGH */
8881 case K_S_LEFT:
8882 case K_S_RIGHT:
8883 case K_S_UP:
8884 case K_S_DOWN:
8885 case K_S_END:
8886 case K_S_HOME:
8887 /* Start selection right away, the cursor can move with
8888 * CTRL-O when beyond the end of the line. */
8889 start_selection();
8890
8891 /* Execute the key in (insert) Select mode. */
8892 stuffcharReadbuff(Ctrl_O);
8893 if (mod_mask)
8894 {
8895 char_u buf[4];
8896
8897 buf[0] = K_SPECIAL;
8898 buf[1] = KS_MODIFIER;
8899 buf[2] = mod_mask;
8900 buf[3] = NUL;
8901 stuffReadbuff(buf);
8902 }
8903 stuffcharReadbuff(c);
8904 return TRUE;
8905 }
8906 return FALSE;
8907}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
8909/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008910 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008911 */
8912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008913ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008914{
8915#ifdef FEAT_FKMAP
8916 if (p_fkmap && p_ri)
8917 {
8918 beep_flush();
8919 EMSG(farsi_text_3); /* encoded in Farsi */
8920 return;
8921 }
8922#endif
8923
Bram Moolenaar1e015462005-09-25 22:16:38 +00008924# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008925 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008926 (char_u *)((State & REPLACE_FLAG) ? "i"
8927 : replaceState == VREPLACE ? "v"
8928 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008929# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008930 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008931 if (State & REPLACE_FLAG)
8932 State = INSERT | (State & LANGMAP);
8933 else
8934 State = replaceState | (State & LANGMAP);
8935 AppendCharToRedobuff(K_INS);
8936 showmode();
8937#ifdef CURSOR_SHAPE
8938 ui_cursor_shape(); /* may show different cursor shape */
8939#endif
8940}
8941
8942/*
8943 * Pressed CTRL-O in Insert mode.
8944 */
8945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008946ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008947{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008948 if (State & VREPLACE_FLAG)
8949 restart_edit = 'V';
8950 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008951 if (State & REPLACE_FLAG)
8952 restart_edit = 'R';
8953 else
8954 restart_edit = 'I';
8955#ifdef FEAT_VIRTUALEDIT
8956 if (virtual_active())
8957 ins_at_eol = FALSE; /* cursor always keeps its column */
8958 else
8959#endif
8960 ins_at_eol = (gchar_cursor() == NUL);
8961}
8962
8963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 * If the cursor is on an indent, ^T/^D insert/delete one
8965 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008966 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 * with vi. But vi only supports ^T and ^D after an
8968 * autoindent, we support it everywhere.
8969 */
8970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008971ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
8973 if (stop_arrow() == FAIL)
8974 return;
8975 AppendCharToRedobuff(c);
8976
8977 /*
8978 * 0^D and ^^D: remove all indent.
8979 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008980 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8981 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 {
8983 --curwin->w_cursor.col;
8984 (void)del_char(FALSE); /* delete the '^' or '0' */
8985 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8986 if (State & REPLACE_FLAG)
8987 replace_pop_ins();
8988 if (lastc == '^')
8989 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008990 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 }
8992 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008993 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994
8995 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8996 did_ai = FALSE;
8997#ifdef FEAT_SMARTINDENT
8998 did_si = FALSE;
8999 can_si = FALSE;
9000 can_si_back = FALSE;
9001#endif
9002#ifdef FEAT_CINDENT
9003 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9004#endif
9005}
9006
9007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009008ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 int temp;
9011
9012 if (stop_arrow() == FAIL)
9013 return;
9014 if (gchar_cursor() == NUL) /* delete newline */
9015 {
9016 temp = curwin->w_cursor.col;
9017 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009018 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009019 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009023 /* Adjust orig_line_count in case more lines have been deleted than
9024 * have been added. That makes sure, that open_line() later
9025 * can access all buffer lines correctly */
9026 if (State & VREPLACE_FLAG &&
9027 orig_line_count > curbuf->b_ml.ml_line_count)
9028 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009031 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9032 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 did_ai = FALSE;
9034#ifdef FEAT_SMARTINDENT
9035 did_si = FALSE;
9036 can_si = FALSE;
9037 can_si_back = FALSE;
9038#endif
9039 AppendCharToRedobuff(K_DEL);
9040}
9041
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009042static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009043
9044/*
9045 * Delete one character for ins_bs().
9046 */
9047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009048ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009049{
9050 dec_cursor();
9051 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9052 if (State & REPLACE_FLAG)
9053 {
9054 /* Don't delete characters before the insert point when in
9055 * Replace mode */
9056 if (curwin->w_cursor.lnum != Insstart.lnum
9057 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009058 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009059 }
9060 else
9061 (void)del_char(FALSE);
9062}
9063
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064/*
9065 * Handle Backspace, delete-word and delete-line in Insert mode.
9066 * Return TRUE when backspace was actually used.
9067 */
9068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009069ins_bs(
9070 int c,
9071 int mode,
9072 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 linenr_T lnum;
9075 int cc;
9076 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009077 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 colnr_T mincol;
9079 int did_backspace = FALSE;
9080 int in_indent;
9081 int oldState;
9082#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009083 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084#endif
9085
9086 /*
9087 * can't delete anything in an empty file
9088 * can't backup past first character in buffer
9089 * can't backup past starting point unless 'backspace' > 1
9090 * can backup to a previous line if 'backspace' == 0
9091 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009092 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 || (
9094#ifdef FEAT_RIGHTLEFT
9095 !revins_on &&
9096#endif
9097 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9098 || (!can_bs(BS_START)
9099 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009100 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9101 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9103 && curwin->w_cursor.col <= ai_col)
9104 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9105 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009106 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 return FALSE;
9108 }
9109
9110 if (stop_arrow() == FAIL)
9111 return FALSE;
9112 in_indent = inindent(0);
9113#ifdef FEAT_CINDENT
9114 if (in_indent)
9115 can_cindent = FALSE;
9116#endif
9117#ifdef FEAT_COMMENTS
9118 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9119#endif
9120#ifdef FEAT_RIGHTLEFT
9121 if (revins_on) /* put cursor after last inserted char */
9122 inc_cursor();
9123#endif
9124
9125#ifdef FEAT_VIRTUALEDIT
9126 /* Virtualedit:
9127 * BACKSPACE_CHAR eats a virtual space
9128 * BACKSPACE_WORD eats all coladd
9129 * BACKSPACE_LINE eats all coladd and keeps going
9130 */
9131 if (curwin->w_cursor.coladd > 0)
9132 {
9133 if (mode == BACKSPACE_CHAR)
9134 {
9135 --curwin->w_cursor.coladd;
9136 return TRUE;
9137 }
9138 if (mode == BACKSPACE_WORD)
9139 {
9140 curwin->w_cursor.coladd = 0;
9141 return TRUE;
9142 }
9143 curwin->w_cursor.coladd = 0;
9144 }
9145#endif
9146
9147 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009148 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 */
9150 if (curwin->w_cursor.col == 0)
9151 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009152 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009153 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154#ifdef FEAT_RIGHTLEFT
9155 || revins_on
9156#endif
9157 )
9158 {
9159 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9160 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9161 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009162 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009163 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 }
9165 /*
9166 * In replace mode:
9167 * cc < 0: NL was inserted, delete it
9168 * cc >= 0: NL was replaced, put original characters back
9169 */
9170 cc = -1;
9171 if (State & REPLACE_FLAG)
9172 cc = replace_pop(); /* returns -1 if NL was inserted */
9173 /*
9174 * In replace mode, in the line we started replacing, we only move the
9175 * cursor.
9176 */
9177 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9178 {
9179 dec_cursor();
9180 }
9181 else
9182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 if (!(State & VREPLACE_FLAG)
9184 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 {
9186 temp = gchar_cursor(); /* remember current char */
9187 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009188
9189 /* When "aw" is in 'formatoptions' we must delete the space at
9190 * the end of the line, otherwise the line will be broken
9191 * again when auto-formatting. */
9192 if (has_format_option(FO_AUTO)
9193 && has_format_option(FO_WHITE_PAR))
9194 {
9195 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9196 TRUE);
9197 int len;
9198
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009199 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009200 if (len > 0 && ptr[len - 1] == ' ')
9201 ptr[len - 1] = NUL;
9202 }
9203
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009204 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 if (temp == NUL && gchar_cursor() != NUL)
9206 inc_cursor();
9207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 else
9209 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
9211 /*
9212 * In REPLACE mode we have to put back the text that was replaced
9213 * by the NL. On the replace stack is first a NUL-terminated
9214 * sequence of characters that were deleted and then the
9215 * characters that NL replaced.
9216 */
9217 if (State & REPLACE_FLAG)
9218 {
9219 /*
9220 * Do the next ins_char() in NORMAL state, to
9221 * prevent ins_char() from replacing characters and
9222 * avoiding showmatch().
9223 */
9224 oldState = State;
9225 State = NORMAL;
9226 /*
9227 * restore characters (blanks) deleted after cursor
9228 */
9229 while (cc > 0)
9230 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009231 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232#ifdef FEAT_MBYTE
9233 mb_replace_pop_ins(cc);
9234#else
9235 ins_char(cc);
9236#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009237 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 cc = replace_pop();
9239 }
9240 /* restore the characters that NL replaced */
9241 replace_pop_ins();
9242 State = oldState;
9243 }
9244 }
9245 did_ai = FALSE;
9246 }
9247 else
9248 {
9249 /*
9250 * Delete character(s) before the cursor.
9251 */
9252#ifdef FEAT_RIGHTLEFT
9253 if (revins_on) /* put cursor on last inserted char */
9254 dec_cursor();
9255#endif
9256 mincol = 0;
9257 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009258 if (mode == BACKSPACE_LINE
9259 && (curbuf->b_p_ai
9260#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009261 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009262#endif
9263 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264#ifdef FEAT_RIGHTLEFT
9265 && !revins_on
9266#endif
9267 )
9268 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009269 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009271 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009273 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 }
9275
9276 /*
9277 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9278 */
9279 if ( mode == BACKSPACE_CHAR
9280 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009281 || ((get_sts_value() != 0
9282#ifdef FEAT_VARTABS
9283 || tabstop_count(curbuf->b_p_vsts_array)
9284#endif
9285 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009286 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 && (*(ml_get_cursor() - 1) == TAB
9288 || (*(ml_get_cursor() - 1) == ' '
9289 && (!*inserted_space_p
9290 || arrow_used))))))
9291 {
9292 int ts;
9293 colnr_T vcol;
9294 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009295 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
9297 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 /* Compute the virtual column where we want to be. Since
9299 * 'showbreak' may get in the way, need to get the last column of
9300 * the previous character. */
9301 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009302 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 dec_cursor();
9304 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9305 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009306#ifdef FEAT_VARTABS
9307 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009308 {
9309 ts = (int)get_sw_value(curbuf);
9310 want_vcol = (want_vcol / ts) * ts;
9311 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009312 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009313 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009314 curbuf->b_p_vsts_array);
9315#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009316 if (p_sta && in_indent)
9317 ts = (int)get_sw_value(curbuf);
9318 else
9319 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322
9323 /* delete characters until we are at or before want_vcol */
9324 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009325 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009326 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327
9328 /* insert extra spaces until we are at want_vcol */
9329 while (vcol < want_vcol)
9330 {
9331 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009332 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9333 && curwin->w_cursor.col < Insstart_orig.col)
9334 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 if (State & VREPLACE_FLAG)
9337 ins_char(' ');
9338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 {
9340 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009341 if ((State & REPLACE_FLAG))
9342 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 }
9344 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9345 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009346
9347 /* If we are now back where we started delete one character. Can
9348 * happen when using 'sts' and 'linebreak'. */
9349 if (vcol >= start_vcol)
9350 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 }
9352
9353 /*
9354 * Delete upto starting point, start of line or previous word.
9355 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009358#ifdef FEAT_MBYTE
9359 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009361 if (has_mbyte)
9362 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009364 do
9365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009367 if (!revins_on) /* put cursor on char to be deleted */
9368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009370
9371 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009373 /* look multi-byte character class */
9374 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009376 prev_cclass = cclass;
9377 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009380
9381 /* start of word? */
9382 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9383 {
9384 mode = BACKSPACE_WORD_NOT_SPACE;
9385 temp = vim_iswordc(cc);
9386 }
9387 /* end of word? */
9388 else if (mode == BACKSPACE_WORD_NOT_SPACE
9389 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9390#ifdef FEAT_MBYTE
9391 || prev_cclass != cclass
9392#endif
9393 ))
9394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009396 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009398 inc_cursor();
9399#ifdef FEAT_RIGHTLEFT
9400 else if (State & REPLACE_FLAG)
9401 dec_cursor();
9402#endif
9403 break;
9404 }
9405 if (State & REPLACE_FLAG)
9406 replace_do_bs(-1);
9407 else
9408 {
9409#ifdef FEAT_MBYTE
9410 if (enc_utf8 && p_deco)
9411 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9412#endif
9413 (void)del_char(FALSE);
9414#ifdef FEAT_MBYTE
9415 /*
9416 * If there are combining characters and 'delcombine' is set
9417 * move the cursor back. Don't back up before the base
9418 * character.
9419 */
9420 if (enc_utf8 && p_deco && cpc[0] != NUL)
9421 inc_cursor();
9422#endif
9423#ifdef FEAT_RIGHTLEFT
9424 if (revins_chars)
9425 {
9426 revins_chars--;
9427 revins_legal++;
9428 }
9429 if (revins_on && gchar_cursor() == NUL)
9430 break;
9431#endif
9432 }
9433 /* Just a single backspace?: */
9434 if (mode == BACKSPACE_CHAR)
9435 break;
9436 } while (
9437#ifdef FEAT_RIGHTLEFT
9438 revins_on ||
9439#endif
9440 (curwin->w_cursor.col > mincol
9441 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9442 || curwin->w_cursor.col != Insstart_orig.col)));
9443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 did_backspace = TRUE;
9445 }
9446#ifdef FEAT_SMARTINDENT
9447 did_si = FALSE;
9448 can_si = FALSE;
9449 can_si_back = FALSE;
9450#endif
9451 if (curwin->w_cursor.col <= 1)
9452 did_ai = FALSE;
9453 /*
9454 * It's a little strange to put backspaces into the redo
9455 * buffer, but it makes auto-indent a lot easier to deal
9456 * with.
9457 */
9458 AppendCharToRedobuff(c);
9459
9460 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009461 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009462 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009463 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464
9465 /* vi behaviour: the cursor moves backward but the character that
9466 * was there remains visible
9467 * Vim behaviour: the cursor moves backward and the character that
9468 * was there is erased from the screen.
9469 * We can emulate the vi behaviour by pretending there is a dollar
9470 * displayed even when there isn't.
9471 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009472 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 dollar_vcol = curwin->w_virtcol;
9474
Bram Moolenaarce3be472008-01-14 19:12:28 +00009475#ifdef FEAT_FOLDING
9476 /* When deleting a char the cursor line must never be in a closed fold.
9477 * E.g., when 'foldmethod' is indent and deleting the first non-white
9478 * char before a Tab. */
9479 if (did_backspace)
9480 foldOpenCursor();
9481#endif
9482
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 return did_backspace;
9484}
9485
9486#ifdef FEAT_MOUSE
9487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009488ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
9490 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009491 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492
9493# ifdef FEAT_GUI
9494 /* When GUI is active, also move/paste when 'mouse' is empty */
9495 if (!gui.in_use)
9496# endif
9497 if (!mouse_has(MOUSE_INSERT))
9498 return;
9499
9500 undisplay_dollar();
9501 tpos = curwin->w_cursor;
9502 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9503 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009504 win_T *new_curwin = curwin;
9505
9506 if (curwin != old_curwin && win_valid(old_curwin))
9507 {
9508 /* Mouse took us to another window. We need to go back to the
9509 * previous one to stop insert there properly. */
9510 curwin = old_curwin;
9511 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009512#ifdef FEAT_JOB_CHANNEL
9513 if (bt_prompt(curbuf))
9514 // Restart Insert mode when re-entering the prompt buffer.
9515 curbuf->b_prompt_insert = 'A';
9516#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009517 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009518 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009519 if (curwin != new_curwin && win_valid(new_curwin))
9520 {
9521 curwin = new_curwin;
9522 curbuf = curwin->w_buffer;
9523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524# ifdef FEAT_CINDENT
9525 can_cindent = TRUE;
9526# endif
9527 }
9528
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 /* redraw status lines (in case another window became active) */
9530 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531}
9532
9533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009537 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009538# ifdef FEAT_INS_EXPAND
9539 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540# endif
9541
9542 tpos = curwin->w_cursor;
9543
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009544 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 {
9546 int row, col;
9547
9548 row = mouse_row;
9549 col = mouse_col;
9550
9551 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009552 wp = mouse_find_win(&row, &col);
9553 if (wp == NULL)
9554 return;
9555 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 curbuf = curwin->w_buffer;
9557 }
9558 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 undisplay_dollar();
9560
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009561# ifdef FEAT_INS_EXPAND
9562 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009563 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009564# endif
9565 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009566 if (dir == MSCR_DOWN || dir == MSCR_UP)
9567 {
9568 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9569 scroll_redraw(dir,
9570 (long)(curwin->w_botline - curwin->w_topline));
9571 else
9572 scroll_redraw(dir, 3L);
9573 }
9574#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009575 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009576 {
9577 int val, step = 6;
9578
9579 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009580 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009581 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9582 if (val < 0)
9583 val = 0;
9584 gui_do_horiz_scroll(val, TRUE);
9585 }
9586#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009587# ifdef FEAT_INS_EXPAND
9588 did_scroll = TRUE;
9589# endif
9590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 curwin->w_redr_status = TRUE;
9593
9594 curwin = old_curwin;
9595 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009597# ifdef FEAT_INS_EXPAND
9598 /* The popup menu may overlay the window, need to redraw it.
9599 * TODO: Would be more efficient to only redraw the windows that are
9600 * overlapped by the popup menu. */
9601 if (pum_visible() && did_scroll)
9602 {
9603 redraw_all_later(NOT_VALID);
9604 ins_compl_show_pum();
9605 }
9606# endif
9607
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009608 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 {
9610 start_arrow(&tpos);
9611# ifdef FEAT_CINDENT
9612 can_cindent = TRUE;
9613# endif
9614 }
9615}
9616#endif
9617
Bram Moolenaarec2da362017-01-21 20:04:22 +01009618/*
9619 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9620 * P_PE literally.
9621 * When "drop" is TRUE then consume the text and drop it.
9622 */
9623 int
9624bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9625{
9626 int c;
9627 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9628 int idx = 0;
9629 char_u *end = find_termcode((char_u *)"PE");
9630 int ret_char = -1;
9631 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009632 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009633
9634 /* If the end code is too long we can't detect it, read everything. */
9635 if (STRLEN(end) >= NUMBUFLEN)
9636 end = NULL;
9637 ++no_mapping;
9638 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009639 if (!p_paste)
9640 // Also have the side effects of setting 'paste' to make it work much
9641 // faster.
9642 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009643
Bram Moolenaarec2da362017-01-21 20:04:22 +01009644 for (;;)
9645 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009646 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009647 if (end == NULL && vpeekc() == NUL)
9648 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009649 do
9650 {
9651 c = vgetc();
9652 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9653 if (c == NUL || got_int)
9654 // When CTRL-C was encountered the typeahead will be flushed and we
9655 // won't get the end sequence.
9656 break;
9657
Bram Moolenaarec2da362017-01-21 20:04:22 +01009658#ifdef FEAT_MBYTE
9659 if (has_mbyte)
9660 idx += (*mb_char2bytes)(c, buf + idx);
9661 else
9662#endif
9663 buf[idx++] = c;
9664 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009665 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009666 {
9667 if (end[idx] == NUL)
9668 break; /* Found the end of paste code. */
9669 continue;
9670 }
9671 if (!drop)
9672 {
9673 switch (mode)
9674 {
9675 case PASTE_CMDLINE:
9676 put_on_cmdline(buf, idx, TRUE);
9677 break;
9678
9679 case PASTE_EX:
9680 if (gap != NULL && ga_grow(gap, idx) == OK)
9681 {
9682 mch_memmove((char *)gap->ga_data + gap->ga_len,
9683 buf, (size_t)idx);
9684 gap->ga_len += idx;
9685 }
9686 break;
9687
9688 case PASTE_INSERT:
9689 if (stop_arrow() == OK)
9690 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009691 c = buf[0];
9692 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9693 ins_eol(c);
9694 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009695 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009696 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009697 AppendToRedobuffLit(buf, idx);
9698 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009699 }
9700 break;
9701
9702 case PASTE_ONE_CHAR:
9703 if (ret_char == -1)
9704 {
9705#ifdef FEAT_MBYTE
9706 if (has_mbyte)
9707 ret_char = (*mb_ptr2char)(buf);
9708 else
9709#endif
9710 ret_char = buf[0];
9711 }
9712 break;
9713 }
9714 }
9715 idx = 0;
9716 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009717
Bram Moolenaarec2da362017-01-21 20:04:22 +01009718 --no_mapping;
9719 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009720 if (!save_paste)
9721 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009722
9723 return ret_char;
9724}
9725
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009726#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009728ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009729{
9730 /* We will be leaving the current window, unless closing another tab. */
9731 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9732 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9733 {
9734 undisplay_dollar();
9735 start_arrow(&curwin->w_cursor);
9736# ifdef FEAT_CINDENT
9737 can_cindent = TRUE;
9738# endif
9739 }
9740
9741 if (c == K_TABLINE)
9742 goto_tabpage(current_tab);
9743 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009744 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009745 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009746 redraw_statuslines(); /* will redraw the tabline when needed */
9747 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009748}
9749#endif
9750
9751#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009753ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754{
9755 pos_T tpos;
9756
9757 undisplay_dollar();
9758 tpos = curwin->w_cursor;
9759 if (gui_do_scroll())
9760 {
9761 start_arrow(&tpos);
9762# ifdef FEAT_CINDENT
9763 can_cindent = TRUE;
9764# endif
9765 }
9766}
9767
9768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009769ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 pos_T tpos;
9772
9773 undisplay_dollar();
9774 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009775 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 {
9777 start_arrow(&tpos);
9778# ifdef FEAT_CINDENT
9779 can_cindent = TRUE;
9780# endif
9781 }
9782}
9783#endif
9784
9785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009786ins_left(
9787 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788{
9789 pos_T tpos;
9790
9791#ifdef FEAT_FOLDING
9792 if ((fdo_flags & FDO_HOR) && KeyTyped)
9793 foldOpenCursor();
9794#endif
9795 undisplay_dollar();
9796 tpos = curwin->w_cursor;
9797 if (oneleft() == OK)
9798 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009799#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9800 /* Only call start_arrow() when not busy with preediting, it will
9801 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009802 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009803#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009804 {
9805 start_arrow_with_change(&tpos, end_change);
9806 if (!end_change)
9807 AppendCharToRedobuff(K_LEFT);
9808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809#ifdef FEAT_RIGHTLEFT
9810 /* If exit reversed string, position is fixed */
9811 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9812 revins_legal++;
9813 revins_chars++;
9814#endif
9815 }
9816
9817 /*
9818 * if 'whichwrap' set for cursor in insert mode may go to
9819 * previous line
9820 */
9821 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9822 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009823 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 start_arrow(&tpos);
9825 --(curwin->w_cursor.lnum);
9826 coladvance((colnr_T)MAXCOL);
9827 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9828 }
9829 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009830 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009831 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832}
9833
9834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009835ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 pos_T tpos;
9838
9839#ifdef FEAT_FOLDING
9840 if ((fdo_flags & FDO_HOR) && KeyTyped)
9841 foldOpenCursor();
9842#endif
9843 undisplay_dollar();
9844 tpos = curwin->w_cursor;
9845 if (c == K_C_HOME)
9846 curwin->w_cursor.lnum = 1;
9847 curwin->w_cursor.col = 0;
9848#ifdef FEAT_VIRTUALEDIT
9849 curwin->w_cursor.coladd = 0;
9850#endif
9851 curwin->w_curswant = 0;
9852 start_arrow(&tpos);
9853}
9854
9855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009856ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857{
9858 pos_T tpos;
9859
9860#ifdef FEAT_FOLDING
9861 if ((fdo_flags & FDO_HOR) && KeyTyped)
9862 foldOpenCursor();
9863#endif
9864 undisplay_dollar();
9865 tpos = curwin->w_cursor;
9866 if (c == K_C_END)
9867 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9868 coladvance((colnr_T)MAXCOL);
9869 curwin->w_curswant = MAXCOL;
9870
9871 start_arrow(&tpos);
9872}
9873
9874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009875ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
9877#ifdef FEAT_FOLDING
9878 if ((fdo_flags & FDO_HOR) && KeyTyped)
9879 foldOpenCursor();
9880#endif
9881 undisplay_dollar();
9882 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9883 {
9884 start_arrow(&curwin->w_cursor);
9885 (void)bck_word(1L, FALSE, FALSE);
9886 curwin->w_set_curswant = TRUE;
9887 }
9888 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009889 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
9891
9892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009893ins_right(
9894 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896#ifdef FEAT_FOLDING
9897 if ((fdo_flags & FDO_HOR) && KeyTyped)
9898 foldOpenCursor();
9899#endif
9900 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009901 if (gchar_cursor() != NUL
9902#ifdef FEAT_VIRTUALEDIT
9903 || virtual_active()
9904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 )
9906 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009907 start_arrow_with_change(&curwin->w_cursor, end_change);
9908 if (!end_change)
9909 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 curwin->w_set_curswant = TRUE;
9911#ifdef FEAT_VIRTUALEDIT
9912 if (virtual_active())
9913 oneright();
9914 else
9915#endif
9916 {
9917#ifdef FEAT_MBYTE
9918 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009919 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 else
9921#endif
9922 ++curwin->w_cursor.col;
9923 }
9924
9925#ifdef FEAT_RIGHTLEFT
9926 revins_legal++;
9927 if (revins_chars)
9928 revins_chars--;
9929#endif
9930 }
9931 /* if 'whichwrap' set for cursor in insert mode, may move the
9932 * cursor to the next line */
9933 else if (vim_strchr(p_ww, ']') != NULL
9934 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9935 {
9936 start_arrow(&curwin->w_cursor);
9937 curwin->w_set_curswant = TRUE;
9938 ++curwin->w_cursor.lnum;
9939 curwin->w_cursor.col = 0;
9940 }
9941 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009942 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009943 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944}
9945
9946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948{
9949#ifdef FEAT_FOLDING
9950 if ((fdo_flags & FDO_HOR) && KeyTyped)
9951 foldOpenCursor();
9952#endif
9953 undisplay_dollar();
9954 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9955 || gchar_cursor() != NUL)
9956 {
9957 start_arrow(&curwin->w_cursor);
9958 (void)fwd_word(1L, FALSE, 0);
9959 curwin->w_set_curswant = TRUE;
9960 }
9961 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009962 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963}
9964
9965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009966ins_up(
9967 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968{
9969 pos_T tpos;
9970 linenr_T old_topline = curwin->w_topline;
9971#ifdef FEAT_DIFF
9972 int old_topfill = curwin->w_topfill;
9973#endif
9974
9975 undisplay_dollar();
9976 tpos = curwin->w_cursor;
9977 if (cursor_up(1L, TRUE) == OK)
9978 {
9979 if (startcol)
9980 coladvance(getvcol_nolist(&Insstart));
9981 if (old_topline != curwin->w_topline
9982#ifdef FEAT_DIFF
9983 || old_topfill != curwin->w_topfill
9984#endif
9985 )
9986 redraw_later(VALID);
9987 start_arrow(&tpos);
9988#ifdef FEAT_CINDENT
9989 can_cindent = TRUE;
9990#endif
9991 }
9992 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009993 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994}
9995
9996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009997ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998{
9999 pos_T tpos;
10000
10001 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010002
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010003 if (mod_mask & MOD_MASK_CTRL)
10004 {
10005 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010006 if (first_tabpage->tp_next != NULL)
10007 {
10008 start_arrow(&curwin->w_cursor);
10009 goto_tabpage(-1);
10010 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010011 return;
10012 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010013
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 tpos = curwin->w_cursor;
10015 if (onepage(BACKWARD, 1L) == OK)
10016 {
10017 start_arrow(&tpos);
10018#ifdef FEAT_CINDENT
10019 can_cindent = TRUE;
10020#endif
10021 }
10022 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010023 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024}
10025
10026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010027ins_down(
10028 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
10030 pos_T tpos;
10031 linenr_T old_topline = curwin->w_topline;
10032#ifdef FEAT_DIFF
10033 int old_topfill = curwin->w_topfill;
10034#endif
10035
10036 undisplay_dollar();
10037 tpos = curwin->w_cursor;
10038 if (cursor_down(1L, TRUE) == OK)
10039 {
10040 if (startcol)
10041 coladvance(getvcol_nolist(&Insstart));
10042 if (old_topline != curwin->w_topline
10043#ifdef FEAT_DIFF
10044 || old_topfill != curwin->w_topfill
10045#endif
10046 )
10047 redraw_later(VALID);
10048 start_arrow(&tpos);
10049#ifdef FEAT_CINDENT
10050 can_cindent = TRUE;
10051#endif
10052 }
10053 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010054 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055}
10056
10057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010058ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059{
10060 pos_T tpos;
10061
10062 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010063
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010064 if (mod_mask & MOD_MASK_CTRL)
10065 {
10066 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010067 if (first_tabpage->tp_next != NULL)
10068 {
10069 start_arrow(&curwin->w_cursor);
10070 goto_tabpage(0);
10071 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010072 return;
10073 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010074
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 tpos = curwin->w_cursor;
10076 if (onepage(FORWARD, 1L) == OK)
10077 {
10078 start_arrow(&tpos);
10079#ifdef FEAT_CINDENT
10080 can_cindent = TRUE;
10081#endif
10082 }
10083 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010084 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085}
10086
10087#ifdef FEAT_DND
10088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010089ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
10091 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10092}
10093#endif
10094
10095/*
10096 * Handle TAB in Insert or Replace mode.
10097 * Return TRUE when the TAB needs to be inserted like a normal character.
10098 */
10099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010100ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101{
10102 int ind;
10103 int i;
10104 int temp;
10105
10106 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10107 Insstart_blank_vcol = get_nolist_virtcol();
10108 if (echeck_abbr(TAB + ABBR_OFF))
10109 return FALSE;
10110
10111 ind = inindent(0);
10112#ifdef FEAT_CINDENT
10113 if (ind)
10114 can_cindent = FALSE;
10115#endif
10116
10117 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010118 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 */
10120 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010121#ifdef FEAT_VARTABS
10122 && !(p_sta && ind
10123 /* These five lines mean 'tabstop' != 'shiftwidth' */
10124 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10125 || (tabstop_count(curbuf->b_p_vts_array) == 1
10126 && tabstop_first(curbuf->b_p_vts_array)
10127 != get_sw_value(curbuf))
10128 || (tabstop_count(curbuf->b_p_vts_array) == 0
10129 && curbuf->b_p_ts != get_sw_value(curbuf))))
10130 && tabstop_count(curbuf->b_p_vsts_array) == 0
10131#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010132 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010133#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010134 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 return TRUE;
10136
10137 if (stop_arrow() == FAIL)
10138 return TRUE;
10139
10140 did_ai = FALSE;
10141#ifdef FEAT_SMARTINDENT
10142 did_si = FALSE;
10143 can_si = FALSE;
10144 can_si_back = FALSE;
10145#endif
10146 AppendToRedobuff((char_u *)"\t");
10147
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010148#ifdef FEAT_VARTABS
10149 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10150 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010151 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010152 temp -= get_nolist_virtcol() % temp;
10153 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010154 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010155 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010156 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010157 curbuf->b_p_vsts_array);
10158 else /* otherwise use 'tabstop' */
10159 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10160 curbuf->b_p_vts_array);
10161#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010163 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010164 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10165 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 else /* otherwise use 'tabstop' */
10167 temp = (int)curbuf->b_p_ts;
10168 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170
10171 /*
10172 * Insert the first space with ins_char(). It will delete one char in
10173 * replace mode. Insert the rest with ins_str(); it will not delete any
10174 * chars. For VREPLACE mode, we use ins_char() for all characters.
10175 */
10176 ins_char(' ');
10177 while (--temp > 0)
10178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 if (State & VREPLACE_FLAG)
10180 ins_char(' ');
10181 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 {
10183 ins_str((char_u *)" ");
10184 if (State & REPLACE_FLAG) /* no char replaced */
10185 replace_push(NUL);
10186 }
10187 }
10188
10189 /*
10190 * When 'expandtab' not set: Replace spaces by TABs where possible.
10191 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010192#ifdef FEAT_VARTABS
10193 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10194 || get_sts_value() > 0
10195 || (p_sta && ind)))
10196#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010197 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 {
10200 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 char_u *saved_line = NULL; /* init for GCC */
10202 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 pos_T fpos;
10204 pos_T *cursor;
10205 colnr_T want_vcol, vcol;
10206 int change_col = -1;
10207 int save_list = curwin->w_p_list;
10208
10209 /*
10210 * Get the current line. For VREPLACE mode, don't make real changes
10211 * yet, just work on a copy of the line.
10212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 if (State & VREPLACE_FLAG)
10214 {
10215 pos = curwin->w_cursor;
10216 cursor = &pos;
10217 saved_line = vim_strsave(ml_get_curline());
10218 if (saved_line == NULL)
10219 return FALSE;
10220 ptr = saved_line + pos.col;
10221 }
10222 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 {
10224 ptr = ml_get_cursor();
10225 cursor = &curwin->w_cursor;
10226 }
10227
10228 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10229 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10230 curwin->w_p_list = FALSE;
10231
10232 /* Find first white before the cursor */
10233 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010234 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 {
10236 --fpos.col;
10237 --ptr;
10238 }
10239
10240 /* In Replace mode, don't change characters before the insert point. */
10241 if ((State & REPLACE_FLAG)
10242 && fpos.lnum == Insstart.lnum
10243 && fpos.col < Insstart.col)
10244 {
10245 ptr += Insstart.col - fpos.col;
10246 fpos.col = Insstart.col;
10247 }
10248
10249 /* compute virtual column numbers of first white and cursor */
10250 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10251 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10252
Bram Moolenaar597a4222014-06-25 14:39:50 +020010253 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10254 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010255 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010257 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 if (vcol + i > want_vcol)
10259 break;
10260 if (*ptr != TAB)
10261 {
10262 *ptr = TAB;
10263 if (change_col < 0)
10264 {
10265 change_col = fpos.col; /* Column of first change */
10266 /* May have to adjust Insstart */
10267 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10268 Insstart.col = fpos.col;
10269 }
10270 }
10271 ++fpos.col;
10272 ++ptr;
10273 vcol += i;
10274 }
10275
10276 if (change_col >= 0)
10277 {
10278 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010279 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280
10281 /* Skip over the spaces we need. */
10282 while (vcol < want_vcol && *ptr == ' ')
10283 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010284 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 ++ptr;
10286 ++repl_off;
10287 }
10288 if (vcol > want_vcol)
10289 {
10290 /* Must have a char with 'showbreak' just before it. */
10291 --ptr;
10292 --repl_off;
10293 }
10294 fpos.col += repl_off;
10295
10296 /* Delete following spaces. */
10297 i = cursor->col - fpos.col;
10298 if (i > 0)
10299 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010300 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010302 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 for (temp = i; --temp >= 0; )
10304 replace_join(repl_off);
10305 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010306#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010307 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010308 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010309 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010310 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10311 (char_u *)"\t", 1);
10312 }
10313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 cursor->col -= i;
10315
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 /*
10317 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10318 * backspacing over the changed spacing and then inserting the new
10319 * spacing.
10320 */
10321 if (State & VREPLACE_FLAG)
10322 {
10323 /* Backspace from real cursor to change_col */
10324 backspace_until_column(change_col);
10325
10326 /* Insert each char in saved_line from changed_col to
10327 * ptr-cursor */
10328 ins_bytes_len(saved_line + change_col,
10329 cursor->col - change_col);
10330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 }
10332
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 if (State & VREPLACE_FLAG)
10334 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 curwin->w_p_list = save_list;
10336 }
10337
10338 return FALSE;
10339}
10340
10341/*
10342 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010343 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 */
10345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010346ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347{
10348 int i;
10349
10350 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010351 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 undisplay_dollar();
10355
10356 /*
10357 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10358 * character under the cursor. Only push a NUL on the replace stack,
10359 * nothing to put back when the NL is deleted.
10360 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010361 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 replace_push(NUL);
10363
10364 /*
10365 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10366 * replacing the next line, so we push all of the characters left on the
10367 * line onto the replace stack. This is not done here though, it is done
10368 * in open_line().
10369 */
10370
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010371#ifdef FEAT_VIRTUALEDIT
10372 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10373 * CTRL-O). */
10374 if (virtual_active() && curwin->w_cursor.coladd > 0)
10375 coladvance(getviscol());
10376#endif
10377
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378#ifdef FEAT_RIGHTLEFT
10379# ifdef FEAT_FKMAP
10380 if (p_altkeymap && p_fkmap)
10381 fkmap(NL);
10382# endif
10383 /* NL in reverse insert will always start in the end of
10384 * current line. */
10385 if (revins_on)
10386 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10387#endif
10388
10389 AppendToRedobuff(NL_STR);
10390 i = open_line(FORWARD,
10391#ifdef FEAT_COMMENTS
10392 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10393#endif
10394 0, old_indent);
10395 old_indent = 0;
10396#ifdef FEAT_CINDENT
10397 can_cindent = TRUE;
10398#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010399#ifdef FEAT_FOLDING
10400 /* When inserting a line the cursor line must never be in a closed fold. */
10401 foldOpenCursor();
10402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010404 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405}
10406
10407#ifdef FEAT_DIGRAPHS
10408/*
10409 * Handle digraph in insert mode.
10410 * Returns character still to be inserted, or NUL when nothing remaining to be
10411 * done.
10412 */
10413 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010414ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415{
10416 int c;
10417 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010418 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419
10420 pc_status = PC_STATUS_UNSET;
10421 if (redrawing() && !char_avail())
10422 {
10423 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010424 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425
10426 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010427 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428#ifdef FEAT_CMDL_INFO
10429 add_to_showcmd_c(Ctrl_K);
10430#endif
10431 }
10432
10433#ifdef USE_ON_FLY_SCROLL
10434 dont_scroll = TRUE; /* disallow scrolling here */
10435#endif
10436
10437 /* don't map the digraph chars. This also prevents the
10438 * mode message to be deleted when ESC is hit */
10439 ++no_mapping;
10440 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010441 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 --no_mapping;
10443 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010444 if (did_putchar)
10445 /* when the line fits in 'columns' the '?' is at the start of the next
10446 * line and will not be removed by the redraw */
10447 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010448
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 if (IS_SPECIAL(c) || mod_mask) /* special key */
10450 {
10451#ifdef FEAT_CMDL_INFO
10452 clear_showcmd();
10453#endif
10454 insert_special(c, TRUE, FALSE);
10455 return NUL;
10456 }
10457 if (c != ESC)
10458 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010459 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 if (redrawing() && !char_avail())
10461 {
10462 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010463 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464
10465 if (char2cells(c) == 1)
10466 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010467 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010469 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 }
10471#ifdef FEAT_CMDL_INFO
10472 add_to_showcmd_c(c);
10473#endif
10474 }
10475 ++no_mapping;
10476 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010477 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 --no_mapping;
10479 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010480 if (did_putchar)
10481 /* when the line fits in 'columns' the '?' is at the start of the
10482 * next line and will not be removed by a redraw */
10483 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 if (cc != ESC)
10485 {
10486 AppendToRedobuff((char_u *)CTRL_V_STR);
10487 c = getdigraph(c, cc, TRUE);
10488#ifdef FEAT_CMDL_INFO
10489 clear_showcmd();
10490#endif
10491 return c;
10492 }
10493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494#ifdef FEAT_CMDL_INFO
10495 clear_showcmd();
10496#endif
10497 return NUL;
10498}
10499#endif
10500
10501/*
10502 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10503 * Returns the char to be inserted, or NUL if none found.
10504 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010506ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507{
10508 int c;
10509 int temp;
10510 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010511 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512
10513 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10514 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010515 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 return NUL;
10517 }
10518
10519 /* try to advance to the cursor column */
10520 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010521 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 prev_ptr = ptr;
10523 validate_virtcol();
10524 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10525 {
10526 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010527 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 }
10529 if ((colnr_T)temp > curwin->w_virtcol)
10530 ptr = prev_ptr;
10531
10532#ifdef FEAT_MBYTE
10533 c = (*mb_ptr2char)(ptr);
10534#else
10535 c = *ptr;
10536#endif
10537 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010538 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 return c;
10540}
10541
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010542/*
10543 * CTRL-Y or CTRL-E typed in Insert mode.
10544 */
10545 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010546ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010547{
10548 int c = tc;
10549
10550#ifdef FEAT_INS_EXPAND
10551 if (ctrl_x_mode == CTRL_X_SCROLL)
10552 {
10553 if (c == Ctrl_Y)
10554 scrolldown_clamp();
10555 else
10556 scrollup_clamp();
10557 redraw_later(VALID);
10558 }
10559 else
10560#endif
10561 {
10562 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10563 if (c != NUL)
10564 {
10565 long tw_save;
10566
10567 /* The character must be taken literally, insert like it
10568 * was typed after a CTRL-V, and pretend 'textwidth'
10569 * wasn't set. Digits, 'o' and 'x' are special after a
10570 * CTRL-V, don't use it for these. */
10571 if (c < 256 && !isalnum(c))
10572 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10573 tw_save = curbuf->b_p_tw;
10574 curbuf->b_p_tw = -1;
10575 insert_special(c, TRUE, FALSE);
10576 curbuf->b_p_tw = tw_save;
10577#ifdef FEAT_RIGHTLEFT
10578 revins_chars++;
10579 revins_legal++;
10580#endif
10581 c = Ctrl_V; /* pretend CTRL-V is last character */
10582 auto_format(FALSE, TRUE);
10583 }
10584 }
10585 return c;
10586}
10587
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588#ifdef FEAT_SMARTINDENT
10589/*
10590 * Try to do some very smart auto-indenting.
10591 * Used when inserting a "normal" character.
10592 */
10593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010594ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596 pos_T *pos, old_pos;
10597 char_u *ptr;
10598 int i;
10599 int temp;
10600
10601 /*
10602 * do some very smart indenting when entering '{' or '}'
10603 */
10604 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10605 {
10606 /*
10607 * for '}' set indent equal to indent of line containing matching '{'
10608 */
10609 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10610 {
10611 old_pos = curwin->w_cursor;
10612 /*
10613 * If the matching '{' has a ')' immediately before it (ignoring
10614 * white-space), then line up with the start of the line
10615 * containing the matching '(' if there is one. This handles the
10616 * case where an "if (..\n..) {" statement continues over multiple
10617 * lines -- webb
10618 */
10619 ptr = ml_get(pos->lnum);
10620 i = pos->col;
10621 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010622 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 ;
10624 curwin->w_cursor.lnum = pos->lnum;
10625 curwin->w_cursor.col = i;
10626 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10627 curwin->w_cursor = *pos;
10628 i = get_indent();
10629 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010631 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 (void)set_indent(i, SIN_CHANGED);
10634 }
10635 else if (curwin->w_cursor.col > 0)
10636 {
10637 /*
10638 * when inserting '{' after "O" reduce indent, but not
10639 * more than indent of previous line
10640 */
10641 temp = TRUE;
10642 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10643 {
10644 old_pos = curwin->w_cursor;
10645 i = get_indent();
10646 while (curwin->w_cursor.lnum > 1)
10647 {
10648 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10649
10650 /* ignore empty lines and lines starting with '#'. */
10651 if (*ptr != '#' && *ptr != NUL)
10652 break;
10653 }
10654 if (get_indent() >= i)
10655 temp = FALSE;
10656 curwin->w_cursor = old_pos;
10657 }
10658 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010659 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 }
10661 }
10662
10663 /*
10664 * set indent of '#' always to 0
10665 */
10666 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10667 {
10668 /* remember current indent for next line */
10669 old_indent = get_indent();
10670 (void)set_indent(0, SIN_CHANGED);
10671 }
10672
10673 /* Adjust ai_col, the char at this position can be deleted. */
10674 if (ai_col > curwin->w_cursor.col)
10675 ai_col = curwin->w_cursor.col;
10676}
10677#endif
10678
10679/*
10680 * Get the value that w_virtcol would have when 'list' is off.
10681 * Unless 'cpo' contains the 'L' flag.
10682 */
10683 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010684get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
10686 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10687 return getvcol_nolist(&curwin->w_cursor);
10688 validate_virtcol();
10689 return curwin->w_virtcol;
10690}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010691
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010692#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010693/*
10694 * Handle the InsertCharPre autocommand.
10695 * "c" is the character that was typed.
10696 * Return a pointer to allocated memory with the replacement string.
10697 * Return NULL to continue inserting "c".
10698 */
10699 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010700do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010701{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010702 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010703 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010704
10705 /* Return quickly when there is nothing to do. */
10706 if (!has_insertcharpre())
10707 return NULL;
10708
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010709# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010710 if (has_mbyte)
10711 buf[(*mb_char2bytes)(c, buf)] = NUL;
10712 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010713# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010714 {
10715 buf[0] = c;
10716 buf[1] = NUL;
10717 }
10718
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010719 /* Lock the text to avoid weird things from happening. */
10720 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010721 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010722
Bram Moolenaar704984a2012-06-01 14:57:51 +020010723 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010724 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010725 {
10726 /* Get the value of v:char. It may be empty or more than one
10727 * character. Only use it when changed, otherwise continue with the
10728 * original character to avoid breaking autoindent. */
10729 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10730 res = vim_strsave(get_vim_var_str(VV_CHAR));
10731 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010732
10733 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10734 --textlock;
10735
10736 return res;
10737}
10738#endif