blob: 5f6ac19a06c8e03c92f216a2459f82d289922812 [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);
466#ifdef FEAT_VREPLACE
467 if (cmdchar == 'V' || cmdchar == 'v')
468 {
469 /* "gR" or "gr" command */
470 AppendCharToRedobuff('g');
471 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
472 }
473 else
474#endif
475 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100476 if (cmdchar == K_PS)
477 AppendCharToRedobuff('a');
478 else
479 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 if (cmdchar == 'g') /* "gI" command */
481 AppendCharToRedobuff('I');
482 else if (cmdchar == 'r') /* "r<CR>" command */
483 count = 1; /* insert only one <CR> */
484 }
485 }
486
487 if (cmdchar == 'R')
488 {
489#ifdef FEAT_FKMAP
490 if (p_fkmap && p_ri)
491 {
492 beep_flush();
493 EMSG(farsi_text_3); /* encoded in Farsi */
494 State = INSERT;
495 }
496 else
497#endif
498 State = REPLACE;
499 }
500#ifdef FEAT_VREPLACE
501 else if (cmdchar == 'V' || cmdchar == 'v')
502 {
503 State = VREPLACE;
504 replaceState = VREPLACE;
505 orig_line_count = curbuf->b_ml.ml_line_count;
506 vr_lines_changed = 1;
507 }
508#endif
509 else
510 State = INSERT;
511
512 stop_insert_mode = FALSE;
513
514 /*
515 * Need to recompute the cursor position, it might move when the cursor is
516 * on a TAB or special character.
517 */
518 curs_columns(TRUE);
519
520 /*
521 * Enable langmap or IME, indicated by 'iminsert'.
522 * Note that IME may enabled/disabled without us noticing here, thus the
523 * 'iminsert' value may not reflect what is actually used. It is updated
524 * when hitting <Esc>.
525 */
526 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
527 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100528#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532#ifdef FEAT_MOUSE
533 setmouse();
534#endif
535#ifdef FEAT_CMDL_INFO
536 clear_showcmd();
537#endif
538#ifdef FEAT_RIGHTLEFT
539 /* there is no reverse replace mode */
540 revins_on = (State == INSERT && p_ri);
541 if (revins_on)
542 undisplay_dollar();
543 revins_chars = 0;
544 revins_legal = 0;
545 revins_scol = -1;
546#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100547 if (!p_ek)
548 /* Disable bracketed paste mode, we won't recognize the escape
549 * sequences. */
550 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552 /*
553 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100554 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
555 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 */
557 if (restart_edit != 0 && stuff_empty())
558 {
559#ifdef FEAT_MOUSE
560 /*
561 * After a paste we consider text typed to be part of the insert for
562 * the pasted text. You can backspace over the pasted text too.
563 */
564 if (where_paste_started.lnum)
565 arrow_used = FALSE;
566 else
567#endif
568 arrow_used = TRUE;
569 restart_edit = 0;
570
571 /*
572 * If the cursor was after the end-of-line before the CTRL-O and it is
573 * now at the end-of-line, put it after the end-of-line (this is not
574 * correct in very rare cases).
575 * Also do this if curswant is greater than the current virtual
576 * column. Eg after "^O$" or "^O80|".
577 */
578 validate_virtcol();
579 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000580 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 || curwin->w_curswant > curwin->w_virtcol)
582 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
583 {
584 if (ptr[1] == NUL)
585 ++curwin->w_cursor.col;
586#ifdef FEAT_MBYTE
587 else if (has_mbyte)
588 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000589 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (ptr[i] == NUL)
591 curwin->w_cursor.col += i;
592 }
593#endif
594 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000595 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 else
598 arrow_used = FALSE;
599
600 /* we are in insert mode now, don't need to start it anymore */
601 need_start_insertmode = FALSE;
602
603 /* Need to save the line for undo before inserting the first char. */
604 ins_need_undo = TRUE;
605
606#ifdef FEAT_MOUSE
607 where_paste_started.lnum = 0;
608#endif
609#ifdef FEAT_CINDENT
610 can_cindent = TRUE;
611#endif
612#ifdef FEAT_FOLDING
613 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
614 * restarting. */
615 if (!p_im && did_restart_edit == 0)
616 foldOpenCursor();
617#endif
618
619 /*
620 * If 'showmode' is set, show the current (insert/replace/..) mode.
621 * A warning message for changing a readonly file is given here, before
622 * actually changing anything. It's put after the mode, if any.
623 */
624 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000625 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 i = showmode();
627
628 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000629 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631#ifdef CURSOR_SHAPE
632 ui_cursor_shape(); /* may show different cursor shape */
633#endif
634#ifdef FEAT_DIGRAPHS
635 do_digraph(-1); /* clear digraphs */
636#endif
637
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000638 /*
639 * Get the current length of the redo buffer, those characters have to be
640 * skipped if we want to get to the inserted characters.
641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 ptr = get_inserted();
643 if (ptr == NULL)
644 new_insert_skip = 0;
645 else
646 {
647 new_insert_skip = (int)STRLEN(ptr);
648 vim_free(ptr);
649 }
650
651 old_indent = 0;
652
653 /*
654 * Main loop in Insert mode: repeat until Insert mode is left.
655 */
656 for (;;)
657 {
658#ifdef FEAT_RIGHTLEFT
659 if (!revins_legal)
660 revins_scol = -1; /* reset on illegal motions */
661 else
662 revins_legal = 0;
663#endif
664 if (arrow_used) /* don't repeat insert when arrow key used */
665 count = 0;
666
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100667 if (update_Insstart_orig)
668 Insstart_orig = Insstart;
669
Bram Moolenaar00672e12016-06-26 18:38:13 +0200670 if (stop_insert_mode
671#ifdef FEAT_INS_EXPAND
672 && !pum_visible()
673#endif
674 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
676 /* ":stopinsert" used or 'insertmode' reset */
677 count = 0;
678 goto doESCkey;
679 }
680
681 /* set curwin->w_curswant for next K_DOWN or K_UP */
682 if (!arrow_used)
683 curwin->w_set_curswant = TRUE;
684
685 /* If there is no typeahead may check for timestamps (e.g., for when a
686 * menu invoked a shell command). */
687 if (stuff_empty())
688 {
689 did_check_timestamps = FALSE;
690 if (need_check_timestamps)
691 check_timestamps(FALSE);
692 }
693
694 /*
695 * When emsg() was called msg_scroll will have been set.
696 */
697 msg_scroll = FALSE;
698
699#ifdef FEAT_GUI
700 /* When 'mousefocus' is set a mouse movement may have taken us to
701 * another window. "need_mouse_correct" may then be set because of an
702 * autocommand. */
703 if (need_mouse_correct)
704 gui_mouse_correct();
705#endif
706
707#ifdef FEAT_FOLDING
708 /* Open fold at the cursor line, according to 'foldopen'. */
709 if (fdo_flags & FDO_INSERT)
710 foldOpenCursor();
711 /* Close folds where the cursor isn't, according to 'foldclose' */
712 if (!char_avail())
713 foldCheckClose();
714#endif
715
Bram Moolenaarf2732452018-06-03 14:47:35 +0200716#ifdef FEAT_JOB_CHANNEL
717 if (bt_prompt(curbuf))
718 {
719 init_prompt(cmdchar_todo);
720 cmdchar_todo = NUL;
721 }
722#endif
723
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 /*
725 * If we inserted a character at the last position of the last line in
726 * the window, scroll the window one line up. This avoids an extra
727 * redraw.
728 * This is detected when the cursor column is smaller after inserting
729 * something.
730 * Don't do this when the topline changed already, it has
731 * already been adjusted (by insertchar() calling open_line())).
732 */
733 if (curbuf->b_mod_set
734 && curwin->w_p_wrap
735 && !did_backspace
736 && curwin->w_topline == old_topline
737#ifdef FEAT_DIFF
738 && curwin->w_topfill == old_topfill
739#endif
740 )
741 {
742 mincol = curwin->w_wcol;
743 validate_cursor_col();
744
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200745 if (
746#ifdef FEAT_VARTABS
747 (int)curwin->w_wcol < mincol - tabstop_at(
748 get_nolist_virtcol(), curbuf->b_p_ts,
749 curbuf->b_p_vts_array)
750#else
751 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 && curwin->w_wrow == W_WINROW(curwin)
754 + curwin->w_height - 1 - p_so
755 && (curwin->w_cursor.lnum != curwin->w_topline
756#ifdef FEAT_DIFF
757 || curwin->w_topfill > 0
758#endif
759 ))
760 {
761#ifdef FEAT_DIFF
762 if (curwin->w_topfill > 0)
763 --curwin->w_topfill;
764 else
765#endif
766#ifdef FEAT_FOLDING
767 if (hasFolding(curwin->w_topline, NULL, &old_topline))
768 set_topline(curwin, old_topline + 1);
769 else
770#endif
771 set_topline(curwin, curwin->w_topline + 1);
772 }
773 }
774
775 /* May need to adjust w_topline to show the cursor. */
776 update_topline();
777
778 did_backspace = FALSE;
779
780 validate_cursor(); /* may set must_redraw */
781
782 /*
783 * Redraw the display when no characters are waiting.
784 * Also shows mode, ruler and positions cursor.
785 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000786 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 if (curwin->w_p_scb)
789 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaar860cae12010-06-05 23:22:07 +0200791 if (curwin->w_p_crb)
792 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 update_curswant();
794 old_topline = curwin->w_topline;
795#ifdef FEAT_DIFF
796 old_topfill = curwin->w_topfill;
797#endif
798
799#ifdef USE_ON_FLY_SCROLL
800 dont_scroll = FALSE; /* allow scrolling here */
801#endif
802
803 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100804 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100806 if (c != K_CURSORHOLD)
807 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200808
809 /* After using CTRL-G U the next cursor key will not break undo. */
810 if (dont_sync_undo == MAYBE)
811 dont_sync_undo = TRUE;
812 else
813 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100814 if (cmdchar == K_PS)
815 /* Got here from normal mode when bracketed paste started. */
816 c = K_PS;
817 else
818 do
819 {
820 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200821
822 if (stop_insert_mode)
823 {
824 // Insert mode ended, possibly from a callback.
825 count = 0;
826 nomove = TRUE;
827 goto doESCkey;
828 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100829 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000831 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
832 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#ifdef FEAT_RIGHTLEFT
835 if (p_hkmap && KeyTyped)
836 c = hkmap(c); /* Hebrew mode mapping */
837#endif
838#ifdef FEAT_FKMAP
839 if (p_fkmap && KeyTyped)
840 c = fkmap(c); /* Farsi mode mapping */
841#endif
842
843#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 /*
845 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000846 * and the cursor is still in the completed word. Only when there is
847 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000848 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000849 if (compl_started
850 && pum_wanted()
851 && curwin->w_cursor.col >= compl_col
852 && (compl_shown_match == NULL
853 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000854 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000855 /* BS: Delete one character from "compl_leader". */
856 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000857 && curwin->w_cursor.col > compl_col
858 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000859 continue;
860
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000861 /* When no match was selected or it was edited. */
862 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000863 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000864 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000865 * "compl_leader". Except when at the original match and
866 * there is nothing to add, CTRL-L works like CTRL-P then. */
867 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100868 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000869 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000870 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000871 {
872 ins_compl_addfrommatch();
873 continue;
874 }
875
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000876 /* A non-white character that fits in with the current
877 * completion: Add to "compl_leader". */
878 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000879 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100880#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100881 /* Trigger InsertCharPre. */
882 char_u *str = do_insert_char_pre(c);
883 char_u *p;
884
885 if (str != NULL)
886 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100887 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100888 ins_compl_addleader(PTR2CHAR(p));
889 vim_free(str);
890 }
891 else
892#endif
893 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000894 continue;
895 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000896
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000897 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000898 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200899 if ((c == Ctrl_Y || (compl_enter_selects
900 && (c == CAR || c == K_KENTER || c == NL)))
901 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000902 {
903 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200904 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000905 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000906 }
907 }
908
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
910 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000911 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000912 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000913 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#endif
915
Bram Moolenaar488c6512005-08-11 20:09:58 +0000916 /* CTRL-\ CTRL-N goes to Normal mode,
917 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
918 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (c == Ctrl_BSL)
920 {
921 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000922 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 ++no_mapping;
924 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000925 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 --no_mapping;
927 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000928 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000930 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 vungetc(c);
932 c = Ctrl_BSL;
933 }
934 else if (c == Ctrl_G && p_im)
935 continue;
936 else
937 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000938 if (c == Ctrl_O)
939 {
940 ins_ctrl_o();
941 ins_at_eol = FALSE; /* cursor keeps its column */
942 nomove = TRUE;
943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 count = 0;
945 goto doESCkey;
946 }
947 }
948
949#ifdef FEAT_DIGRAPHS
950 c = do_digraph(c);
951#endif
952
953#ifdef FEAT_INS_EXPAND
954 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
955 goto docomplete;
956#endif
957 if (c == Ctrl_V || c == Ctrl_Q)
958 {
959 ins_ctrl_v();
960 c = Ctrl_V; /* pretend CTRL-V is last typed character */
961 continue;
962 }
963
964#ifdef FEAT_CINDENT
965 if (cindent_on()
966# ifdef FEAT_INS_EXPAND
967 && ctrl_x_mode == 0
968# endif
969 )
970 {
971 /* A key name preceded by a bang means this key is not to be
972 * inserted. Skip ahead to the re-indenting below.
973 * A key name preceded by a star means that indenting has to be
974 * done before inserting the key. */
975 line_is_white = inindent(0);
976 if (in_cinkeys(c, '!', line_is_white))
977 goto force_cindent;
978 if (can_cindent && in_cinkeys(c, '*', line_is_white)
979 && stop_arrow() == OK)
980 do_c_expr_indent();
981 }
982#endif
983
984#ifdef FEAT_RIGHTLEFT
985 if (curwin->w_p_rl)
986 switch (c)
987 {
988 case K_LEFT: c = K_RIGHT; break;
989 case K_S_LEFT: c = K_S_RIGHT; break;
990 case K_C_LEFT: c = K_C_RIGHT; break;
991 case K_RIGHT: c = K_LEFT; break;
992 case K_S_RIGHT: c = K_S_LEFT; break;
993 case K_C_RIGHT: c = K_C_LEFT; break;
994 }
995#endif
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 /*
998 * If 'keymodel' contains "startsel", may start selection. If it
999 * does, a CTRL-O and c will be stuffed, we need to get these
1000 * characters.
1001 */
1002 if (ins_start_select(c))
1003 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
1005 /*
1006 * The big switch to handle a character in insert mode.
1007 */
1008 switch (c)
1009 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001010 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (echeck_abbr(ESC + ABBR_OFF))
1012 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001013 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#ifdef FEAT_CMDWIN
1017 if (c == Ctrl_C && cmdwin_type != 0)
1018 {
1019 /* Close the cmdline window. */
1020 cmdwin_result = K_IGNORE;
1021 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001022 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 goto doESCkey;
1024 }
1025#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001026#ifdef FEAT_JOB_CHANNEL
1027 if (c == Ctrl_C && bt_prompt(curbuf))
1028 {
1029 if (invoke_prompt_interrupt())
1030 {
1031 if (!bt_prompt(curbuf))
1032 // buffer changed to a non-prompt buffer, get out of
1033 // Insert mode
1034 goto doESCkey;
1035 break;
1036 }
1037 }
1038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040#ifdef UNIX
1041do_intr:
1042#endif
1043 /* when 'insertmode' set, and not halfway a mapping, don't leave
1044 * Insert mode */
1045 if (goto_im())
1046 {
1047 if (got_int)
1048 {
1049 (void)vgetc(); /* flush all buffers */
1050 got_int = FALSE;
1051 }
1052 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001053 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 break;
1055 }
1056doESCkey:
1057 /*
1058 * This is the ONLY return from edit()!
1059 */
1060 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1061 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001062 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 o_lnum = curwin->w_cursor.lnum;
1064
Bram Moolenaar488c6512005-08-11 20:09:58 +00001065 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001066 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00001067 if (cmdchar != 'r' && cmdchar != 'v')
1068 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1069 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001070 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 continue;
1074
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 case Ctrl_Z: /* suspend when 'insertmode' set */
1076 if (!p_im)
1077 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001078 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001079#ifdef CURSOR_SHAPE
1080 ui_cursor_shape(); /* may need to update cursor shape */
1081#endif
1082 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083
1084 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001085#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001086 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 goto docomplete;
1088#endif
1089 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1090 break;
1091 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001092
1093#ifdef FEAT_VIRTUALEDIT
1094 /* don't move the cursor left when 'virtualedit' has "onemore". */
1095 if (ve_flags & VE_ONEMORE)
1096 {
1097 ins_at_eol = FALSE;
1098 nomove = TRUE;
1099 }
1100#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001101 count = 0;
1102 goto doESCkey;
1103
Bram Moolenaar572cb562005-08-05 21:35:02 +00001104 case K_INS: /* toggle insert/replace mode */
1105 case K_KINS:
1106 ins_insert(replaceState);
1107 break;
1108
1109 case K_SELECT: /* end of Select mode mapping - ignore */
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case K_HELP: /* Help key works like <ESC> <Help> */
1113 case K_F1:
1114 case K_XF1:
1115 stuffcharReadbuff(K_HELP);
1116 if (p_im)
1117 need_start_insertmode = TRUE;
1118 goto doESCkey;
1119
1120#ifdef FEAT_NETBEANS_INTG
1121 case K_F21: /* NetBeans command */
1122 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001123 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 --no_mapping;
1125 netbeans_keycommand(i);
1126 break;
1127#endif
1128
1129 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 case NUL:
1131 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 /* For ^@ the trailing ESC will end the insert, unless there is an
1133 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1135 && c != Ctrl_A && !p_im)
1136 goto doESCkey; /* quit insert mode */
1137 inserted_space = FALSE;
1138 break;
1139
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001140 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 ins_reg();
1142 auto_format(FALSE, TRUE);
1143 inserted_space = FALSE;
1144 break;
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 ins_ctrl_g();
1148 break;
1149
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001150 case Ctrl_HAT: /* switch input mode and/or langmap */
1151 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 break;
1153
1154#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (!p_ari)
1157 goto normalchar;
1158 ins_ctrl_();
1159 break;
1160#endif
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1164 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1165 goto docomplete;
1166#endif
1167 /* FALLTHROUGH */
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170# ifdef FEAT_INS_EXPAND
1171 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1172 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001173 if (has_compl_option(FALSE))
1174 goto docomplete;
1175 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
1177# endif
1178 ins_shift(c, lastc);
1179 auto_format(FALSE, TRUE);
1180 inserted_space = FALSE;
1181 break;
1182
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001183 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 case K_KDEL:
1185 ins_del();
1186 auto_format(FALSE, TRUE);
1187 break;
1188
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001189 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 case Ctrl_H:
1191 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1192 auto_format(FALSE, TRUE);
1193 break;
1194
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001195 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001196#ifdef FEAT_JOB_CHANNEL
1197 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1198 {
1199 // In a prompt window CTRL-W is used for window commands.
1200 // Use Shift-CTRL-W to delete a word.
1201 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001202 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001203 nomove = TRUE;
1204 count = 0;
1205 goto doESCkey;
1206 }
1207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1209 auto_format(FALSE, TRUE);
1210 break;
1211
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001212 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001213# ifdef FEAT_COMPL_FUNC
1214 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001215 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001216 goto docomplete;
1217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1219 auto_format(FALSE, TRUE);
1220 inserted_space = FALSE;
1221 break;
1222
1223#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 case K_LEFTMOUSE_NM:
1226 case K_LEFTDRAG:
1227 case K_LEFTRELEASE:
1228 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001229 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 case K_MIDDLEMOUSE:
1231 case K_MIDDLEDRAG:
1232 case K_MIDDLERELEASE:
1233 case K_RIGHTMOUSE:
1234 case K_RIGHTDRAG:
1235 case K_RIGHTRELEASE:
1236 case K_X1MOUSE:
1237 case K_X1DRAG:
1238 case K_X1RELEASE:
1239 case K_X2MOUSE:
1240 case K_X2DRAG:
1241 case K_X2RELEASE:
1242 ins_mouse(c);
1243 break;
1244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001245 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001246 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 break;
1248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001249 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001250 ins_mousescroll(MSCR_UP);
1251 break;
1252
1253 case K_MOUSELEFT: /* Scroll wheel left */
1254 ins_mousescroll(MSCR_LEFT);
1255 break;
1256
1257 case K_MOUSERIGHT: /* Scroll wheel right */
1258 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 break;
1260#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001261 case K_PS:
1262 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1263 if (cmdchar == K_PS)
1264 /* invoked from normal mode, bail out */
1265 goto doESCkey;
1266 break;
1267 case K_PE:
1268 /* Got K_PE without K_PS, ignore. */
1269 break;
1270
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001271#ifdef FEAT_GUI_TABLINE
1272 case K_TABLINE:
1273 case K_TABMENU:
1274 ins_tabline(c);
1275 break;
1276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001278 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 break;
1280
Bram Moolenaar754b5602006-02-09 23:53:20 +00001281 case K_CURSORHOLD: /* Didn't type something for a while. */
1282 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1283 did_cursorhold = TRUE;
1284 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001285
Bram Moolenaar4770d092006-01-12 23:22:24 +00001286#ifdef FEAT_GUI_W32
1287 /* On Win32 ignore <M-F4>, we get it when closing the window was
1288 * cancelled. */
1289 case K_F4:
1290 if (mod_mask != MOD_MASK_ALT)
1291 goto normalchar;
1292 break;
1293#endif
1294
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295#ifdef FEAT_GUI
1296 case K_VER_SCROLLBAR:
1297 ins_scroll();
1298 break;
1299
1300 case K_HOR_SCROLLBAR:
1301 ins_horscroll();
1302 break;
1303#endif
1304
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001305 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 case K_S_HOME:
1308 case K_C_HOME:
1309 ins_home(c);
1310 break;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 case K_S_END:
1315 case K_C_END:
1316 ins_end(c);
1317 break;
1318
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001319 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001320 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1321 ins_s_left();
1322 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001323 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 break;
1325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001326 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 case K_C_LEFT:
1328 ins_s_left();
1329 break;
1330
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001331 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001332 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1333 ins_s_right();
1334 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001335 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 break;
1337
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001338 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 case K_C_RIGHT:
1340 ins_s_right();
1341 break;
1342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001344#ifdef FEAT_INS_EXPAND
1345 if (pum_visible())
1346 goto docomplete;
1347#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001348 if (mod_mask & MOD_MASK_SHIFT)
1349 ins_pageup();
1350 else
1351 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 break;
1353
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001354 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 case K_PAGEUP:
1356 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001357#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001358 if (pum_visible())
1359 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 ins_pageup();
1362 break;
1363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001364 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001365#ifdef FEAT_INS_EXPAND
1366 if (pum_visible())
1367 goto docomplete;
1368#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001369 if (mod_mask & MOD_MASK_SHIFT)
1370 ins_pagedown();
1371 else
1372 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 break;
1374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001375 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 case K_PAGEDOWN:
1377 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001378#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001379 if (pum_visible())
1380 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 ins_pagedown();
1383 break;
1384
1385#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001386 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 ins_drop();
1388 break;
1389#endif
1390
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 c = TAB;
1393 /* FALLTHROUGH */
1394
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001395 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1397 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1398 goto docomplete;
1399#endif
1400 inserted_space = FALSE;
1401 if (ins_tab())
1402 goto normalchar; /* insert TAB as a normal char */
1403 auto_format(FALSE, TRUE);
1404 break;
1405
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001406 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 c = CAR;
1408 /* FALLTHROUGH */
1409 case CAR:
1410 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001411#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 /* In a quickfix window a <CR> jumps to the error under the
1413 * cursor. */
1414 if (bt_quickfix(curbuf) && c == CAR)
1415 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001416 if (curwin->w_llist_ref == NULL) /* quickfix window */
1417 do_cmdline_cmd((char_u *)".cc");
1418 else /* location list window */
1419 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 break;
1421 }
1422#endif
1423#ifdef FEAT_CMDWIN
1424 if (cmdwin_type != 0)
1425 {
1426 /* Execute the command in the cmdline window. */
1427 cmdwin_result = CAR;
1428 goto doESCkey;
1429 }
1430#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001431#ifdef FEAT_JOB_CHANNEL
1432 if (bt_prompt(curbuf))
1433 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001434 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001435 if (!bt_prompt(curbuf))
1436 // buffer changed to a non-prompt buffer, get out of
1437 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001438 goto doESCkey;
1439 break;
1440 }
1441#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001442 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 goto doESCkey; /* out of memory */
1444 auto_format(FALSE, FALSE);
1445 inserted_space = FALSE;
1446 break;
1447
Bram Moolenaar860cae12010-06-05 23:22:07 +02001448#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450# ifdef FEAT_INS_EXPAND
1451 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1452 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001453 if (has_compl_option(TRUE))
1454 goto docomplete;
1455 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457# endif
1458# ifdef FEAT_DIGRAPHS
1459 c = ins_digraph();
1460 if (c == NUL)
1461 break;
1462# endif
1463 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
1466#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001467 case Ctrl_X: /* Enter CTRL-X mode */
1468 ins_ctrl_x();
1469 break;
1470
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001471 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if (ctrl_x_mode != CTRL_X_TAGS)
1473 goto normalchar;
1474 goto docomplete;
1475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001476 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if (ctrl_x_mode != CTRL_X_FILES)
1478 goto normalchar;
1479 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001480
1481 case 's': /* Spelling completion after ^X */
1482 case Ctrl_S:
1483 if (ctrl_x_mode != CTRL_X_SPELL)
1484 goto normalchar;
1485 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486#endif
1487
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001488 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489#ifdef FEAT_INS_EXPAND
1490 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1491#endif
1492 {
1493 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1494 if (p_im)
1495 {
1496 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1497 break;
1498 goto doESCkey;
1499 }
1500 goto normalchar;
1501 }
1502#ifdef FEAT_INS_EXPAND
1503 /* FALLTHROUGH */
1504
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001505 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 case Ctrl_N:
1507 /* if 'complete' is empty then plain ^P is no longer special,
1508 * but it is under other ^X modes */
1509 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001510 && (ctrl_x_mode == CTRL_X_NORMAL
1511 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001512 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 goto normalchar;
1514
1515docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001516 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001517#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001518 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001519#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001520 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001521 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001522#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001523 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001524#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001525 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 break;
1527#endif /* FEAT_INS_EXPAND */
1528
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001529 case Ctrl_Y: /* copy from previous line or scroll down */
1530 case Ctrl_E: /* copy from next line or scroll up */
1531 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 break;
1533
1534 default:
1535#ifdef UNIX
1536 if (c == intr_char) /* special interrupt char */
1537 goto do_intr;
1538#endif
1539
Bram Moolenaare659c952011-05-19 17:25:41 +02001540normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001542 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001544#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001545 if (!p_paste)
1546 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001547 /* Trigger InsertCharPre. */
1548 char_u *str = do_insert_char_pre(c);
1549 char_u *p;
1550
1551 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001552 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001553 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001554 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001555 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001556 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001557 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001558 c = PTR2CHAR(p);
1559 if (c == CAR || c == K_KENTER || c == NL)
1560 ins_eol(c);
1561 else
1562 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001563 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001564 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001565 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001566 vim_free(str);
1567 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001568 }
1569
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001570 /* If the new value is already inserted or an empty string
1571 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001572 if (c == NUL)
1573 break;
1574 }
1575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576#ifdef FEAT_SMARTINDENT
1577 /* Try to perform smart-indenting. */
1578 ins_try_si(c);
1579#endif
1580
1581 if (c == ' ')
1582 {
1583 inserted_space = TRUE;
1584#ifdef FEAT_CINDENT
1585 if (inindent(0))
1586 can_cindent = FALSE;
1587#endif
1588 if (Insstart_blank_vcol == MAXCOL
1589 && curwin->w_cursor.lnum == Insstart.lnum)
1590 Insstart_blank_vcol = get_nolist_virtcol();
1591 }
1592
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001593 /* Insert a normal character and check for abbreviations on a
1594 * special character. Let CTRL-] expand abbreviations without
1595 * inserting it. */
1596 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597#ifdef FEAT_MBYTE
1598 /* Add ABBR_OFF for characters above 0x100, this is
1599 * what check_abbr() expects. */
1600 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1601#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001602 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 insert_special(c, FALSE, FALSE);
1605#ifdef FEAT_RIGHTLEFT
1606 revins_legal++;
1607 revins_chars++;
1608#endif
1609 }
1610
1611 auto_format(FALSE, TRUE);
1612
1613#ifdef FEAT_FOLDING
1614 /* When inserting a character the cursor line must never be in a
1615 * closed fold. */
1616 foldOpenCursor();
1617#endif
1618 break;
1619 } /* end of switch (c) */
1620
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001621 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001622 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001623#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001624 /* but not in CTRL-X mode, a script can't restore the state */
1625 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001626#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001627 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001628 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 /* If the cursor was moved we didn't just insert a space */
1631 if (arrow_used)
1632 inserted_space = FALSE;
1633
1634#ifdef FEAT_CINDENT
1635 if (can_cindent && cindent_on()
1636# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001637 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638# endif
1639 )
1640 {
1641force_cindent:
1642 /*
1643 * Indent now if a key was typed that is in 'cinkeys'.
1644 */
1645 if (in_cinkeys(c, ' ', line_is_white))
1646 {
1647 if (stop_arrow() == OK)
1648 /* re-indent the current line */
1649 do_c_expr_indent();
1650 }
1651 }
1652#endif /* FEAT_CINDENT */
1653
1654 } /* for (;;) */
1655 /* NOTREACHED */
1656}
1657
1658/*
1659 * Redraw for Insert mode.
1660 * This is postponed until getting the next character to make '$' in the 'cpo'
1661 * option work correctly.
1662 * Only redraw when there are no characters available. This speeds up
1663 * inserting sequences of characters (e.g., for CTRL-R).
1664 */
1665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001666ins_redraw(
1667 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001669#ifdef FEAT_CONCEAL
1670 linenr_T conceal_old_cursor_line = 0;
1671 linenr_T conceal_new_cursor_line = 0;
1672 int conceal_update_lines = FALSE;
1673#endif
1674
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675 if (char_avail())
1676 return;
1677
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001678#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001679 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1680 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001681 if (ready && (has_cursormovedI()
1682# if defined(FEAT_CONCEAL)
1683 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001684# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001685 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001686 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001687# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001688 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001689# endif
1690 )
1691 {
1692# ifdef FEAT_SYN_HL
1693 /* Need to update the screen first, to make sure syntax
1694 * highlighting is correct after making a change (e.g., inserting
1695 * a "(". The autocommand may also require a redraw, so it's done
1696 * again below, unfortunately. */
1697 if (syntax_present(curwin) && must_redraw)
1698 update_screen(0);
1699# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001700 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001701 {
1702 /* Make sure curswant is correct, an autocommand may call
1703 * getcurpos(). */
1704 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001705 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001706 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001707# ifdef FEAT_CONCEAL
1708 if (curwin->w_p_cole > 0)
1709 {
1710 conceal_old_cursor_line = last_cursormoved.lnum;
1711 conceal_new_cursor_line = curwin->w_cursor.lnum;
1712 conceal_update_lines = TRUE;
1713 }
1714# endif
1715 last_cursormoved = curwin->w_cursor;
1716 }
1717#endif
1718
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001719 /* Trigger TextChangedI if b_changedtick differs. */
1720 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001721 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001722#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001723 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001724#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001725 )
1726 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001727 aco_save_T aco;
1728
1729 // save and restore curwin and curbuf, in case the autocmd changes them
1730 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001731 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001732 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001733 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001735
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001736#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001737 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1738 * TextChangedI will need to trigger for backwards compatibility, thus use
1739 * different b_last_changedtick* variables. */
1740 if (ready && has_textchangedP()
1741 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1742 && pum_visible())
1743 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001744 aco_save_T aco;
1745
1746 // save and restore curwin and curbuf, in case the autocmd changes them
1747 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001748 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001749 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001750 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1751 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001752#endif
1753
1754 if (must_redraw)
1755 update_screen(0);
1756 else if (clear_cmdline || redraw_cmdline)
1757 showmode(); /* clear cmdline and show mode */
1758# if defined(FEAT_CONCEAL)
1759 if ((conceal_update_lines
1760 && (conceal_old_cursor_line != conceal_new_cursor_line
1761 || conceal_cursor_line(curwin)))
1762 || need_cursor_line_redraw)
1763 {
1764 if (conceal_old_cursor_line != conceal_new_cursor_line)
1765 update_single_line(curwin, conceal_old_cursor_line);
1766 update_single_line(curwin, conceal_new_cursor_line == 0
1767 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1768 curwin->w_valid &= ~VALID_CROW;
1769 }
1770# endif
1771 showruler(FALSE);
1772 setcursor();
1773 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774}
1775
1776/*
1777 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1778 */
1779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001783 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001786 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001791 did_putchar = TRUE;
1792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1794
1795#ifdef FEAT_CMDL_INFO
1796 add_to_showcmd_c(Ctrl_V);
1797#endif
1798
1799 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001800 if (did_putchar)
1801 /* when the line fits in 'columns' the '^' is at the start of the next
1802 * line and will not removed by the redraw */
1803 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#ifdef FEAT_CMDL_INFO
1805 clear_showcmd();
1806#endif
1807 insert_special(c, FALSE, TRUE);
1808#ifdef FEAT_RIGHTLEFT
1809 revins_chars++;
1810 revins_legal++;
1811#endif
1812}
1813
1814/*
1815 * Put a character directly onto the screen. It's not stored in a buffer.
1816 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1817 */
1818static int pc_status;
1819#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1820#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1821#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1822#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824static int pc_attr;
1825static int pc_row;
1826static int pc_col;
1827
1828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001829edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
1831 int attr;
1832
1833 if (ScreenLines != NULL)
1834 {
1835 update_topline(); /* just in case w_topline isn't valid */
1836 validate_cursor();
1837 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001838 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 else
1840 attr = 0;
1841 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001842 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1844 pc_status = PC_STATUS_UNSET;
1845#endif
1846#ifdef FEAT_RIGHTLEFT
1847 if (curwin->w_p_rl)
1848 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001849 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850# ifdef FEAT_MBYTE
1851 if (has_mbyte)
1852 {
1853 int fix_col = mb_fix_col(pc_col, pc_row);
1854
1855 if (fix_col != pc_col)
1856 {
1857 screen_putchar(' ', pc_row, fix_col, attr);
1858 --curwin->w_wcol;
1859 pc_status = PC_STATUS_RIGHT;
1860 }
1861 }
1862# endif
1863 }
1864 else
1865#endif
1866 {
1867 pc_col += curwin->w_wcol;
1868#ifdef FEAT_MBYTE
1869 if (mb_lefthalve(pc_row, pc_col))
1870 pc_status = PC_STATUS_LEFT;
1871#endif
1872 }
1873
1874 /* save the character to be able to put it back */
1875#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1876 if (pc_status == PC_STATUS_UNSET)
1877#endif
1878 {
1879 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1880 pc_status = PC_STATUS_SET;
1881 }
1882 screen_putchar(c, pc_row, pc_col, attr);
1883 }
1884}
1885
Bram Moolenaarf2732452018-06-03 14:47:35 +02001886#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1887/*
1888 * Return the effective prompt for the current buffer.
1889 */
1890 char_u *
1891prompt_text(void)
1892{
1893 if (curbuf->b_prompt_text == NULL)
1894 return (char_u *)"% ";
1895 return curbuf->b_prompt_text;
1896}
1897
1898/*
1899 * Prepare for prompt mode: Make sure the last line has the prompt text.
1900 * Move the cursor to this line.
1901 */
1902 static void
1903init_prompt(int cmdchar_todo)
1904{
1905 char_u *prompt = prompt_text();
1906 char_u *text;
1907
1908 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1909 text = ml_get_curline();
1910 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1911 {
1912 // prompt is missing, insert it or append a line with it
1913 if (*text == NUL)
1914 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1915 else
1916 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1917 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1918 coladvance((colnr_T)MAXCOL);
1919 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1920 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001921
1922 // Insert always starts after the prompt, allow editing text after it.
1923 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1924 || Insstart_orig.col != (int)STRLEN(prompt))
1925 {
1926 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001927 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001928 Insstart_orig = Insstart;
1929 Insstart_textlen = Insstart.col;
1930 Insstart_blank_vcol = MAXCOL;
1931 arrow_used = FALSE;
1932 }
1933
Bram Moolenaarf2732452018-06-03 14:47:35 +02001934 if (cmdchar_todo == 'A')
1935 coladvance((colnr_T)MAXCOL);
1936 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001937 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001938 /* Make sure the cursor is in a valid position. */
1939 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001940}
1941
1942/*
1943 * Return TRUE if the cursor is in the editable position of the prompt line.
1944 */
1945 int
1946prompt_curpos_editable()
1947{
1948 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1949 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1950}
1951#endif
1952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953/*
1954 * Undo the previous edit_putchar().
1955 */
1956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001957edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1960 {
1961#if defined(FEAT_MBYTE)
1962 if (pc_status == PC_STATUS_RIGHT)
1963 ++curwin->w_wcol;
1964 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1965 redrawWinline(curwin->w_cursor.lnum, FALSE);
1966 else
1967#endif
1968 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1969 }
1970}
1971
1972/*
1973 * Called when p_dollar is set: display a '$' at the end of the changed text
1974 * Only works when cursor is in the line that changes.
1975 */
1976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001977display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978{
1979 colnr_T save_col;
1980
1981 if (!redrawing())
1982 return;
1983
1984 cursor_off();
1985 save_col = curwin->w_cursor.col;
1986 curwin->w_cursor.col = col;
1987#ifdef FEAT_MBYTE
1988 if (has_mbyte)
1989 {
1990 char_u *p;
1991
1992 /* If on the last byte of a multi-byte move to the first byte. */
1993 p = ml_get_curline();
1994 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1995 }
1996#endif
1997 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001998 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
2000 edit_putchar('$', FALSE);
2001 dollar_vcol = curwin->w_virtcol;
2002 }
2003 curwin->w_cursor.col = save_col;
2004}
2005
2006/*
2007 * Call this function before moving the cursor from the normal insert position
2008 * in insert mode.
2009 */
2010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002011undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002013 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002015 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 redrawWinline(curwin->w_cursor.lnum, FALSE);
2017 }
2018}
2019
2020/*
2021 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2022 * Keep the cursor on the same character.
2023 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2024 * type == INDENT_DEC decrease indent (for CTRL-D)
2025 * type == INDENT_SET set indent to "amount"
2026 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2027 */
2028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002029change_indent(
2030 int type,
2031 int amount,
2032 int round,
2033 int replaced, /* replaced character, put on replace stack */
2034 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 int vcol;
2037 int last_vcol;
2038 int insstart_less; /* reduction for Insstart.col */
2039 int new_cursor_col;
2040 int i;
2041 char_u *ptr;
2042 int save_p_list;
2043 int start_col;
2044 colnr_T vc;
2045#ifdef FEAT_VREPLACE
2046 colnr_T orig_col = 0; /* init for GCC */
2047 char_u *new_line, *orig_line = NULL; /* init for GCC */
2048
2049 /* VREPLACE mode needs to know what the line was like before changing */
2050 if (State & VREPLACE_FLAG)
2051 {
2052 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2053 orig_col = curwin->w_cursor.col;
2054 }
2055#endif
2056
2057 /* for the following tricks we don't want list mode */
2058 save_p_list = curwin->w_p_list;
2059 curwin->w_p_list = FALSE;
2060 vc = getvcol_nolist(&curwin->w_cursor);
2061 vcol = vc;
2062
2063 /*
2064 * For Replace mode we need to fix the replace stack later, which is only
2065 * possible when the cursor is in the indent. Remember the number of
2066 * characters before the cursor if it's possible.
2067 */
2068 start_col = curwin->w_cursor.col;
2069
2070 /* determine offset from first non-blank */
2071 new_cursor_col = curwin->w_cursor.col;
2072 beginline(BL_WHITE);
2073 new_cursor_col -= curwin->w_cursor.col;
2074
2075 insstart_less = curwin->w_cursor.col;
2076
2077 /*
2078 * If the cursor is in the indent, compute how many screen columns the
2079 * cursor is to the left of the first non-blank.
2080 */
2081 if (new_cursor_col < 0)
2082 vcol = get_indent() - vcol;
2083
2084 if (new_cursor_col > 0) /* can't fix replace stack */
2085 start_col = -1;
2086
2087 /*
2088 * Set the new indent. The cursor will be put on the first non-blank.
2089 */
2090 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002091 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 else
2093 {
2094#ifdef FEAT_VREPLACE
2095 int save_State = State;
2096
2097 /* Avoid being called recursively. */
2098 if (State & VREPLACE_FLAG)
2099 State = INSERT;
2100#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002101 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102#ifdef FEAT_VREPLACE
2103 State = save_State;
2104#endif
2105 }
2106 insstart_less -= curwin->w_cursor.col;
2107
2108 /*
2109 * Try to put cursor on same character.
2110 * If the cursor is at or after the first non-blank in the line,
2111 * compute the cursor column relative to the column of the first
2112 * non-blank character.
2113 * If we are not in insert mode, leave the cursor on the first non-blank.
2114 * If the cursor is before the first non-blank, position it relative
2115 * to the first non-blank, counted in screen columns.
2116 */
2117 if (new_cursor_col >= 0)
2118 {
2119 /*
2120 * When changing the indent while the cursor is touching it, reset
2121 * Insstart_col to 0.
2122 */
2123 if (new_cursor_col == 0)
2124 insstart_less = MAXCOL;
2125 new_cursor_col += curwin->w_cursor.col;
2126 }
2127 else if (!(State & INSERT))
2128 new_cursor_col = curwin->w_cursor.col;
2129 else
2130 {
2131 /*
2132 * Compute the screen column where the cursor should be.
2133 */
2134 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002135 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 /*
2138 * Advance the cursor until we reach the right screen column.
2139 */
2140 vcol = last_vcol = 0;
2141 new_cursor_col = -1;
2142 ptr = ml_get_curline();
2143 while (vcol <= (int)curwin->w_virtcol)
2144 {
2145 last_vcol = vcol;
2146#ifdef FEAT_MBYTE
2147 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002148 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 else
2150#endif
2151 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002152 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 vcol = last_vcol;
2155
2156 /*
2157 * May need to insert spaces to be able to position the cursor on
2158 * the right screen column.
2159 */
2160 if (vcol != (int)curwin->w_virtcol)
2161 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002162 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002164 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (ptr != NULL)
2166 {
2167 new_cursor_col += i;
2168 ptr[i] = NUL;
2169 while (--i >= 0)
2170 ptr[i] = ' ';
2171 ins_str(ptr);
2172 vim_free(ptr);
2173 }
2174 }
2175
2176 /*
2177 * When changing the indent while the cursor is in it, reset
2178 * Insstart_col to 0.
2179 */
2180 insstart_less = MAXCOL;
2181 }
2182
2183 curwin->w_p_list = save_p_list;
2184
2185 if (new_cursor_col <= 0)
2186 curwin->w_cursor.col = 0;
2187 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002188 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 curwin->w_set_curswant = TRUE;
2190 changed_cline_bef_curs();
2191
2192 /*
2193 * May have to adjust the start of the insert.
2194 */
2195 if (State & INSERT)
2196 {
2197 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2198 {
2199 if ((int)Insstart.col <= insstart_less)
2200 Insstart.col = 0;
2201 else
2202 Insstart.col -= insstart_less;
2203 }
2204 if ((int)ai_col <= insstart_less)
2205 ai_col = 0;
2206 else
2207 ai_col -= insstart_less;
2208 }
2209
2210 /*
2211 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2212 * If the number of characters before the cursor decreased, need to pop a
2213 * few characters from the replace stack.
2214 * If the number of characters before the cursor increased, need to push a
2215 * few NULs onto the replace stack.
2216 */
2217 if (REPLACE_NORMAL(State) && start_col >= 0)
2218 {
2219 while (start_col > (int)curwin->w_cursor.col)
2220 {
2221 replace_join(0); /* remove a NUL from the replace stack */
2222 --start_col;
2223 }
2224 while (start_col < (int)curwin->w_cursor.col || replaced)
2225 {
2226 replace_push(NUL);
2227 if (replaced)
2228 {
2229 replace_push(replaced);
2230 replaced = NUL;
2231 }
2232 ++start_col;
2233 }
2234 }
2235
2236#ifdef FEAT_VREPLACE
2237 /*
2238 * For VREPLACE mode, we also have to fix the replace stack. In this case
2239 * it is always possible because we backspace over the whole line and then
2240 * put it back again the way we wanted it.
2241 */
2242 if (State & VREPLACE_FLAG)
2243 {
2244 /* If orig_line didn't allocate, just return. At least we did the job,
2245 * even if you can't backspace. */
2246 if (orig_line == NULL)
2247 return;
2248
2249 /* Save new line */
2250 new_line = vim_strsave(ml_get_curline());
2251 if (new_line == NULL)
2252 return;
2253
2254 /* We only put back the new line up to the cursor */
2255 new_line[curwin->w_cursor.col] = NUL;
2256
2257 /* Put back original line */
2258 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2259 curwin->w_cursor.col = orig_col;
2260
2261 /* Backspace from cursor to start of line */
2262 backspace_until_column(0);
2263
2264 /* Insert new stuff into line again */
2265 ins_bytes(new_line);
2266
2267 vim_free(new_line);
2268 }
2269#endif
2270}
2271
2272/*
2273 * Truncate the space at the end of a line. This is to be used only in an
2274 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2275 * modes.
2276 */
2277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002278truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 int i;
2281
2282 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002283 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 if (State & REPLACE_FLAG)
2286 replace_join(0); /* remove a NUL from the replace stack */
2287 }
2288 line[i + 1] = NUL;
2289}
2290
2291#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2292 || defined(FEAT_COMMENTS) || defined(PROTO)
2293/*
2294 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2295 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002296 * Will attempt not to go before "col" even when there is a composing
2297 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 */
2299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002300backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 while ((int)curwin->w_cursor.col > col)
2303 {
2304 curwin->w_cursor.col--;
2305 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002306 replace_do_bs(col);
2307 else if (!del_char_after_col(col))
2308 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310}
2311#endif
2312
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002313/*
2314 * Like del_char(), but make sure not to go before column "limit_col".
2315 * Only matters when there are composing characters.
2316 * Return TRUE when something was deleted.
2317 */
2318 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002319del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002320{
2321#ifdef FEAT_MBYTE
2322 if (enc_utf8 && limit_col >= 0)
2323 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002324 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002325
2326 /* Make sure the cursor is at the start of a character, but
2327 * skip forward again when going too far back because of a
2328 * composing character. */
2329 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002330 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002331 {
2332 int l = utf_ptr2len(ml_get_cursor());
2333
2334 if (l == 0) /* end of line */
2335 break;
2336 curwin->w_cursor.col += l;
2337 }
2338 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2339 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002340 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002341 }
2342 else
2343#endif
2344 (void)del_char(FALSE);
2345 return TRUE;
2346}
2347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2349/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002350 * CTRL-X pressed in Insert mode.
2351 */
2352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002353ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002354{
2355 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2356 * CTRL-V works like CTRL-N */
2357 if (ctrl_x_mode != CTRL_X_CMDLINE)
2358 {
2359 /* if the next ^X<> won't ADD nothing, then reset
2360 * compl_cont_status */
2361 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002362 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002363 else
2364 compl_cont_status = 0;
2365 /* We're not sure which CTRL-X mode it will be yet */
2366 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2367 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2368 edit_submode_pre = NULL;
2369 showmode();
2370 }
2371}
2372
2373/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002374 * Whether other than default completion has been selected.
2375 */
2376 int
2377ctrl_x_mode_not_default(void)
2378{
2379 return ctrl_x_mode != CTRL_X_NORMAL;
2380}
2381
2382/*
2383 * Whether CTRL-X was typed without a following character.
2384 */
2385 int
2386ctrl_x_mode_not_defined_yet(void)
2387{
2388 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2389}
2390
2391/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002392 * Return TRUE if the 'dict' or 'tsr' option can be used.
2393 */
2394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002395has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002396{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002397 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002398# ifdef FEAT_SPELL
2399 && !curwin->w_p_spell
2400# endif
2401 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002402 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2403 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002404 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002405 edit_submode = NULL;
2406 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2407 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002408 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002409 if (emsg_silent == 0)
2410 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002411 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002412 setcursor();
2413 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002414#ifdef FEAT_EVAL
2415 if (!get_vim_var_nr(VV_TESTING))
2416#endif
2417 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002418 }
2419 return FALSE;
2420 }
2421 return TRUE;
2422}
2423
2424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2426 * This depends on the current mode.
2427 */
2428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002429vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 /* Always allow ^R - let it's results then be checked */
2432 if (c == Ctrl_R)
2433 return TRUE;
2434
Bram Moolenaare3226be2005-12-18 22:10:00 +00002435 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002436 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002437 return TRUE;
2438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 switch (ctrl_x_mode)
2440 {
2441 case 0: /* Not in any CTRL-X mode */
2442 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2443 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002444 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2446 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2447 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002448 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002449 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 case CTRL_X_SCROLL:
2451 return (c == Ctrl_Y || c == Ctrl_E);
2452 case CTRL_X_WHOLE_LINE:
2453 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2454 case CTRL_X_FILES:
2455 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2456 case CTRL_X_DICTIONARY:
2457 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2458 case CTRL_X_THESAURUS:
2459 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2460 case CTRL_X_TAGS:
2461 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2462#ifdef FEAT_FIND_ID
2463 case CTRL_X_PATH_PATTERNS:
2464 return (c == Ctrl_P || c == Ctrl_N);
2465 case CTRL_X_PATH_DEFINES:
2466 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2467#endif
2468 case CTRL_X_CMDLINE:
2469 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2470 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002471#ifdef FEAT_COMPL_FUNC
2472 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002473 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002474 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002475 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002476#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002477 case CTRL_X_SPELL:
2478 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002479 case CTRL_X_EVAL:
2480 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002482 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return FALSE;
2484}
2485
2486/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002487 * Return TRUE when character "c" is part of the item currently being
2488 * completed. Used to decide whether to abandon complete mode when the menu
2489 * is visible.
2490 */
2491 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002492ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002493{
2494 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2495 /* When expanding an identifier only accept identifier chars. */
2496 return vim_isIDc(c);
2497
2498 switch (ctrl_x_mode)
2499 {
2500 case CTRL_X_FILES:
2501 /* When expanding file name only accept file name chars. But not
2502 * path separators, so that "proto/<Tab>" expands files in
2503 * "proto", not "proto/" as a whole */
2504 return vim_isfilec(c) && !vim_ispathsep(c);
2505
2506 case CTRL_X_CMDLINE:
2507 case CTRL_X_OMNI:
2508 /* Command line and Omni completion can work with just about any
2509 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002510 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002511
2512 case CTRL_X_WHOLE_LINE:
2513 /* For while line completion a space can be part of the line. */
2514 return vim_isprintc(c);
2515 }
2516 return vim_iswordc(c);
2517}
2518
2519/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002520 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002522 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 * the rest of the word to be in -- webb
2524 */
2525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002526ins_compl_add_infercase(
2527 char_u *str,
2528 int len,
2529 int icase,
2530 char_u *fname,
2531 int dir,
2532 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002534 char_u *p;
2535 int i, c;
2536 int actual_len; /* Take multi-byte characters */
2537 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002538 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002539 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int has_lower = FALSE;
2541 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002543 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002545 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaara2993e12007-08-12 14:38:46 +00002547 /* Find actual length of completion. */
2548#ifdef FEAT_MBYTE
2549 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002551 p = str;
2552 actual_len = 0;
2553 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002555 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002556 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
2558 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002559 else
2560#endif
2561 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
Bram Moolenaara2993e12007-08-12 14:38:46 +00002563 /* Find actual length of original text. */
2564#ifdef FEAT_MBYTE
2565 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002567 p = compl_orig_text;
2568 actual_compl_length = 0;
2569 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002571 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002572 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002575 else
2576#endif
2577 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002579 /* "actual_len" may be smaller than "actual_compl_length" when using
2580 * thesaurus, only use the minimum when comparing. */
2581 min_len = actual_len < actual_compl_length
2582 ? actual_len : actual_compl_length;
2583
Bram Moolenaara2993e12007-08-12 14:38:46 +00002584 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002585 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002586 if (wca != NULL)
2587 {
2588 p = str;
2589 for (i = 0; i < actual_len; ++i)
2590#ifdef FEAT_MBYTE
2591 if (has_mbyte)
2592 wca[i] = mb_ptr2char_adv(&p);
2593 else
2594#endif
2595 wca[i] = *(p++);
2596
2597 /* Rule 1: Were any chars converted to lower? */
2598 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002599 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002600 {
2601#ifdef FEAT_MBYTE
2602 if (has_mbyte)
2603 c = mb_ptr2char_adv(&p);
2604 else
2605#endif
2606 c = *(p++);
2607 if (MB_ISLOWER(c))
2608 {
2609 has_lower = TRUE;
2610 if (MB_ISUPPER(wca[i]))
2611 {
2612 /* Rule 1 is satisfied. */
2613 for (i = actual_compl_length; i < actual_len; ++i)
2614 wca[i] = MB_TOLOWER(wca[i]);
2615 break;
2616 }
2617 }
2618 }
2619
2620 /*
2621 * Rule 2: No lower case, 2nd consecutive letter converted to
2622 * upper case.
2623 */
2624 if (!has_lower)
2625 {
2626 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002627 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002628 {
2629#ifdef FEAT_MBYTE
2630 if (has_mbyte)
2631 c = mb_ptr2char_adv(&p);
2632 else
2633#endif
2634 c = *(p++);
2635 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2636 {
2637 /* Rule 2 is satisfied. */
2638 for (i = actual_compl_length; i < actual_len; ++i)
2639 wca[i] = MB_TOUPPER(wca[i]);
2640 break;
2641 }
2642 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2643 }
2644 }
2645
2646 /* Copy the original case of the part we typed. */
2647 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002648 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002649 {
2650#ifdef FEAT_MBYTE
2651 if (has_mbyte)
2652 c = mb_ptr2char_adv(&p);
2653 else
2654#endif
2655 c = *(p++);
2656 if (MB_ISLOWER(c))
2657 wca[i] = MB_TOLOWER(wca[i]);
2658 else if (MB_ISUPPER(c))
2659 wca[i] = MB_TOUPPER(wca[i]);
2660 }
2661
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002662 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002663 * Generate encoding specific output from wide character array.
2664 * Multi-byte characters can occupy up to five bytes more than
2665 * ASCII characters, and we also need one byte for NUL, so stay
2666 * six bytes away from the edge of IObuff.
2667 */
2668 p = IObuff;
2669 i = 0;
2670 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2671#ifdef FEAT_MBYTE
2672 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002673 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002674 else
2675#endif
2676 *(p++) = wca[i++];
2677 *p = NUL;
2678
2679 vim_free(wca);
2680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002682 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2683 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002685 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686}
2687
2688/*
2689 * Add a match to the list of matches.
2690 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002691 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002692 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002694 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002695ins_compl_add(
2696 char_u *str,
2697 int len,
2698 int icase,
2699 char_u *fname,
2700 char_u **cptext, /* extra text for popup menu or NULL */
2701 int cdir,
2702 int flags,
2703 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002705 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002706 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 ui_breakcheck();
2709 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002710 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (len < 0)
2712 len = (int)STRLEN(str);
2713
2714 /*
2715 * If the same match is already present, don't add it.
2716 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002717 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002719 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 do
2721 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002722 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002723 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002724 && match->cp_str[len] == NUL)
2725 return NOTDONE;
2726 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002727 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 }
2729
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002730 /* Remove any popup menu before changing the list of matches. */
2731 ins_compl_del_pum();
2732
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 /*
2734 * Allocate a new match structure.
2735 * Copy the values to the new match structure.
2736 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002737 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002739 return FAIL;
2740 match->cp_number = -1;
2741 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002742 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002743 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
2745 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002748 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002749
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002751 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2753 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002754 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002755 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002756 && compl_curr_match->cp_fname != NULL
2757 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002758 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002759 else if (fname != NULL)
2760 {
2761 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002762 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002765 match->cp_fname = NULL;
2766 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002767
2768 if (cptext != NULL)
2769 {
2770 int i;
2771
2772 for (i = 0; i < CPT_COUNT; ++i)
2773 if (cptext[i] != NULL && *cptext[i] != NUL)
2774 match->cp_text[i] = vim_strsave(cptext[i]);
2775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
2777 /*
2778 * Link the new match structure in the list of matches.
2779 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002780 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002781 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 else if (dir == FORWARD)
2783 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002784 match->cp_next = compl_curr_match->cp_next;
2785 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
2787 else /* BACKWARD */
2788 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002789 match->cp_next = compl_curr_match;
2790 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002792 if (match->cp_next)
2793 match->cp_next->cp_prev = match;
2794 if (match->cp_prev)
2795 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002797 compl_first_match = match;
2798 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002800 /*
2801 * Find the longest common string if still doing that.
2802 */
2803 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2804 ins_compl_longest_match(match);
2805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 return OK;
2807}
2808
2809/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002810 * Return TRUE if "str[len]" matches with match->cp_str, considering
2811 * match->cp_icase.
2812 */
2813 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002814ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002815{
2816 if (match->cp_icase)
2817 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2818 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2819}
2820
2821/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002822 * Reduce the longest common string for match "match".
2823 */
2824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002825ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002826{
2827 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002828 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002829 int had_match;
2830
2831 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002832 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002833 /* First match, use it as a whole. */
2834 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 if (compl_leader != NULL)
2836 {
2837 had_match = (curwin->w_cursor.col > compl_col);
2838 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002839 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002840 ins_redraw(FALSE);
2841
2842 /* When the match isn't there (to avoid matching itself) remove it
2843 * again after redrawing. */
2844 if (!had_match)
2845 ins_compl_delete();
2846 compl_used_match = FALSE;
2847 }
2848 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002849 else
2850 {
2851 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002852 p = compl_leader;
2853 s = match->cp_str;
2854 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002855 {
2856#ifdef FEAT_MBYTE
2857 if (has_mbyte)
2858 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002859 c1 = mb_ptr2char(p);
2860 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002861 }
2862 else
2863#endif
2864 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002865 c1 = *p;
2866 c2 = *s;
2867 }
2868 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2869 : (c1 != c2))
2870 break;
2871#ifdef FEAT_MBYTE
2872 if (has_mbyte)
2873 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002874 MB_PTR_ADV(p);
2875 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002876 }
2877 else
2878#endif
2879 {
2880 ++p;
2881 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002882 }
2883 }
2884
2885 if (*p != NUL)
2886 {
2887 /* Leader was shortened, need to change the inserted text. */
2888 *p = NUL;
2889 had_match = (curwin->w_cursor.col > compl_col);
2890 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002891 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002892 ins_redraw(FALSE);
2893
2894 /* When the match isn't there (to avoid matching itself) remove it
2895 * again after redrawing. */
2896 if (!had_match)
2897 ins_compl_delete();
2898 }
2899
2900 compl_used_match = FALSE;
2901 }
2902}
2903
2904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 * Add an array of matches to the list of matches.
2906 * Frees matches[].
2907 */
2908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002909ins_compl_add_matches(
2910 int num_matches,
2911 char_u **matches,
2912 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 int i;
2915 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002916 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
Bram Moolenaar572cb562005-08-05 21:35:02 +00002918 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002919 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002920 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002922 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 FreeWild(num_matches, matches);
2924}
2925
2926/* Make the completion list cyclic.
2927 * Return the number of matches (excluding the original).
2928 */
2929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002930ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002932 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 int count = 0;
2934
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002935 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 {
2937 /*
2938 * Find the end of the list.
2939 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002940 match = compl_first_match;
2941 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002942 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002944 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 ++count;
2946 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002947 match->cp_next = compl_first_match;
2948 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950 return count;
2951}
2952
Bram Moolenaara94bc432006-03-10 21:42:59 +00002953/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002954 * Set variables that store noselect and noinsert behavior from the
2955 * 'completeopt' value.
2956 */
2957 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002958completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002959{
2960 compl_no_insert = FALSE;
2961 compl_no_select = FALSE;
2962 if (strstr((char *)p_cot, "noselect") != NULL)
2963 compl_no_select = TRUE;
2964 if (strstr((char *)p_cot, "noinsert") != NULL)
2965 compl_no_insert = TRUE;
2966}
2967
2968/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002969 * Start completion for the complete() function.
2970 * "startcol" is where the matched text starts (1 is first column).
2971 * "list" is the list of matches.
2972 */
2973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002974set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002975{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002976 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002977 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002978
Bram Moolenaara94bc432006-03-10 21:42:59 +00002979 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002980 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002981 ins_compl_prep(' ');
2982 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002983 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002984
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002985 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002986 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002987 startcol = curwin->w_cursor.col;
2988 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002989 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002990 /* compl_pattern doesn't need to be set */
2991 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2992 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002993 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002994 return;
2995
Bram Moolenaare4214502015-03-05 18:08:43 +01002996 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002997
2998 ins_compl_add_list(list);
2999 compl_matches = ins_compl_make_cyclic();
3000 compl_started = TRUE;
3001 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00003002 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003003
3004 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02003005 if (compl_no_insert || compl_no_select)
3006 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003007 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02003008 if (compl_no_select)
3009 /* Down/Up has no real effect. */
3010 ins_complete(K_UP, FALSE);
3011 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003012 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003013 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02003014 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003015
3016 /* Lazily show the popup menu, unless we got interrupted. */
3017 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003018 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003019 out_flush();
3020}
3021
3022
Bram Moolenaar9372a112005-12-06 19:59:18 +00003023/* "compl_match_array" points the currently displayed list of entries in the
3024 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003025static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003026static int compl_match_arraysize;
3027
3028/*
3029 * Update the screen and when there is any scrolling remove the popup menu.
3030 */
3031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003032ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003033{
3034 int h;
3035
3036 if (compl_match_array != NULL)
3037 {
3038 h = curwin->w_cline_height;
3039 update_screen(0);
3040 if (h != curwin->w_cline_height)
3041 ins_compl_del_pum();
3042 }
3043}
3044
3045/*
3046 * Remove any popup menu.
3047 */
3048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003049ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003050{
3051 if (compl_match_array != NULL)
3052 {
3053 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003054 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003055 }
3056}
3057
3058/*
3059 * Return TRUE if the popup menu should be displayed.
3060 */
3061 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003062pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003063{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003064 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003065 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003066 return FALSE;
3067
3068 /* The display looks bad on a B&W display. */
3069 if (t_colors < 8
3070#ifdef FEAT_GUI
3071 && !gui.in_use
3072#endif
3073 )
3074 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003075 return TRUE;
3076}
3077
3078/*
3079 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003080 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003081 */
3082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003083pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003084{
3085 compl_T *compl;
3086 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003087
3088 /* Don't display the popup menu if there are no matches or there is only
3089 * one (ignoring the original text). */
3090 compl = compl_first_match;
3091 i = 0;
3092 do
3093 {
3094 if (compl == NULL
3095 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3096 break;
3097 compl = compl->cp_next;
3098 } while (compl != compl_first_match);
3099
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003100 if (strstr((char *)p_cot, "menuone") != NULL)
3101 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003102 return (i >= 2);
3103}
3104
3105/*
3106 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003107 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003108 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003110ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003111{
3112 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003113 compl_T *shown_compl = NULL;
3114 int did_find_shown_match = FALSE;
3115 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003116 int i;
3117 int cur = -1;
3118 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003119 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003120
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003121 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003122 return;
3123
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003124#if defined(FEAT_EVAL)
3125 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3126 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3127#endif
3128
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003129 /* Update the screen before drawing the popup menu over it. */
3130 update_screen(0);
3131
3132 if (compl_match_array == NULL)
3133 {
3134 /* Need to build the popup menu list. */
3135 compl_match_arraysize = 0;
3136 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003137 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003138 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003139 do
3140 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003141 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3142 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003143 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003144 ++compl_match_arraysize;
3145 compl = compl->cp_next;
3146 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003147 if (compl_match_arraysize == 0)
3148 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003149 compl_match_array = (pumitem_T *)alloc_clear(
3150 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003151 * compl_match_arraysize));
3152 if (compl_match_array != NULL)
3153 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003154 /* If the current match is the original text don't find the first
3155 * match after it, don't highlight anything. */
3156 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3157 shown_match_ok = TRUE;
3158
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003159 i = 0;
3160 compl = compl_first_match;
3161 do
3162 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003163 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3164 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003165 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003166 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003167 if (!shown_match_ok)
3168 {
3169 if (compl == compl_shown_match || did_find_shown_match)
3170 {
3171 /* This item is the shown match or this is the
3172 * first displayed item after the shown match. */
3173 compl_shown_match = compl;
3174 did_find_shown_match = TRUE;
3175 shown_match_ok = TRUE;
3176 }
3177 else
3178 /* Remember this displayed match for when the
3179 * shown match is just below it. */
3180 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003181 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003182 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003183
3184 if (compl->cp_text[CPT_ABBR] != NULL)
3185 compl_match_array[i].pum_text =
3186 compl->cp_text[CPT_ABBR];
3187 else
3188 compl_match_array[i].pum_text = compl->cp_str;
3189 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3190 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3191 if (compl->cp_text[CPT_MENU] != NULL)
3192 compl_match_array[i++].pum_extra =
3193 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003194 else
3195 compl_match_array[i++].pum_extra = compl->cp_fname;
3196 }
3197
3198 if (compl == compl_shown_match)
3199 {
3200 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003201
3202 /* When the original text is the shown match don't set
3203 * compl_shown_match. */
3204 if (compl->cp_flags & ORIGINAL_TEXT)
3205 shown_match_ok = TRUE;
3206
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003207 if (!shown_match_ok && shown_compl != NULL)
3208 {
3209 /* The shown match isn't displayed, set it to the
3210 * previously displayed match. */
3211 compl_shown_match = shown_compl;
3212 shown_match_ok = TRUE;
3213 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003214 }
3215 compl = compl->cp_next;
3216 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003217
3218 if (!shown_match_ok) /* no displayed match at all */
3219 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003220 }
3221 }
3222 else
3223 {
3224 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003225 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003226 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3227 || compl_match_array[i].pum_text
3228 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003229 {
3230 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003231 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003232 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003233 }
3234
3235 if (compl_match_array != NULL)
3236 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003237 /* In Replace mode when a $ is displayed at the end of the line only
3238 * part of the screen would be updated. We do need to redraw here. */
3239 dollar_vcol = -1;
3240
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003241 /* Compute the screen column of the start of the completed text.
3242 * Use the cursor to get all wrapping and other settings right. */
3243 col = curwin->w_cursor.col;
3244 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003245 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003246 curwin->w_cursor.col = col;
3247 }
3248}
3249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250#define DICT_FIRST (1) /* use just first element in "dict" */
3251#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003252
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003254 * Add any identifiers that match the given pattern in the list of dictionary
3255 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 */
3257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003258ins_compl_dictionaries(
3259 char_u *dict_start,
3260 char_u *pat,
3261 int flags, /* DICT_FIRST and/or DICT_EXACT */
3262 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003264 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 char_u *ptr;
3266 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 char_u **files;
3269 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003271 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar0b238792006-03-02 22:49:12 +00003273 if (*dict == NUL)
3274 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003275#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003276 /* When 'dictionary' is empty and spell checking is enabled use
3277 * "spell". */
3278 if (!thesaurus && curwin->w_p_spell)
3279 dict = (char_u *)"spell";
3280 else
3281#endif
3282 return;
3283 }
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003286 if (buf == NULL)
3287 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003288 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 /* If 'infercase' is set, don't use 'smartcase' here */
3291 save_p_scs = p_scs;
3292 if (curbuf->b_p_inf)
3293 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003294
3295 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3296 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003297 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003298 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003299 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003300 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003301 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003302
3303 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003304 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003305 len = STRLEN(pat_esc) + 10;
3306 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003307 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003308 {
3309 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003310 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003311 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003312 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003313 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3314 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003315 vim_free(ptr);
3316 }
3317 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003318 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003319 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003320 if (regmatch.regprog == NULL)
3321 goto theend;
3322 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003323
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3325 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003326 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 /* copy one dictionary file name into buf */
3329 if (flags == DICT_EXACT)
3330 {
3331 count = 1;
3332 files = &dict;
3333 }
3334 else
3335 {
3336 /* Expand wildcards in the dictionary name, but do not allow
3337 * backticks (for security, the 'dict' option may have been set in
3338 * a modeline). */
3339 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003340# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003341 if (!thesaurus && STRCMP(buf, "spell") == 0)
3342 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003343 else
3344# endif
3345 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 || expand_wildcards(1, &buf, &count, &files,
3347 EW_FILE|EW_SILENT) != OK)
3348 count = 0;
3349 }
3350
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003351# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003352 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003354 /* Complete from active spelling. Skip "\<" in the pattern, we
3355 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003356 if (pat[0] == '\\' && pat[1] == '<')
3357 ptr = pat + 2;
3358 else
3359 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003360 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003362 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003363# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003364 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003365 {
3366 ins_compl_files(count, files, thesaurus, flags,
3367 &regmatch, buf, &dir);
3368 if (flags != DICT_EXACT)
3369 FreeWild(count, files);
3370 }
3371 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 break;
3373 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003374
3375theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003377 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 vim_free(buf);
3379}
3380
Bram Moolenaar0b238792006-03-02 22:49:12 +00003381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003382ins_compl_files(
3383 int count,
3384 char_u **files,
3385 int thesaurus,
3386 int flags,
3387 regmatch_T *regmatch,
3388 char_u *buf,
3389 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003390{
3391 char_u *ptr;
3392 int i;
3393 FILE *fp;
3394 int add_r;
3395
3396 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3397 {
3398 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3399 if (flags != DICT_EXACT)
3400 {
3401 vim_snprintf((char *)IObuff, IOSIZE,
3402 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003403 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003404 }
3405
3406 if (fp != NULL)
3407 {
3408 /*
3409 * Read dictionary file line by line.
3410 * Check each line for a match.
3411 */
3412 while (!got_int && !compl_interrupted
3413 && !vim_fgets(buf, LSIZE, fp))
3414 {
3415 ptr = buf;
3416 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3417 {
3418 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003419 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003420 ptr = find_line_end(ptr);
3421 else
3422 ptr = find_word_end(ptr);
3423 add_r = ins_compl_add_infercase(regmatch->startp[0],
3424 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003425 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003426 if (thesaurus)
3427 {
3428 char_u *wstart;
3429
3430 /*
3431 * Add the other matches on the line
3432 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003433 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003434 while (!got_int)
3435 {
3436 /* Find start of the next word. Skip white
3437 * space and punctuation. */
3438 ptr = find_word_start(ptr);
3439 if (*ptr == NUL || *ptr == NL)
3440 break;
3441 wstart = ptr;
3442
Bram Moolenaara2993e12007-08-12 14:38:46 +00003443 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003444#ifdef FEAT_MBYTE
3445 if (has_mbyte)
3446 /* Japanese words may have characters in
3447 * different classes, only separate words
3448 * with single-byte non-word characters. */
3449 while (*ptr != NUL)
3450 {
3451 int l = (*mb_ptr2len)(ptr);
3452
3453 if (l < 2 && !vim_iswordc(*ptr))
3454 break;
3455 ptr += l;
3456 }
3457 else
3458#endif
3459 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003460
3461 /* Add the word. Skip the regexp match. */
3462 if (wstart != regmatch->startp[0])
3463 add_r = ins_compl_add_infercase(wstart,
3464 (int)(ptr - wstart),
3465 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003466 }
3467 }
3468 if (add_r == OK)
3469 /* if dir was BACKWARD then honor it just once */
3470 *dir = FORWARD;
3471 else if (add_r == FAIL)
3472 break;
3473 /* avoid expensive call to vim_regexec() when at end
3474 * of line */
3475 if (*ptr == '\n' || got_int)
3476 break;
3477 }
3478 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003479 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003480 }
3481 fclose(fp);
3482 }
3483 }
3484}
3485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486/*
3487 * Find the start of the next word.
3488 * Returns a pointer to the first char of the word. Also stops at a NUL.
3489 */
3490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003491find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
3493#ifdef FEAT_MBYTE
3494 if (has_mbyte)
3495 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003496 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 else
3498#endif
3499 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3500 ++ptr;
3501 return ptr;
3502}
3503
3504/*
3505 * Find the end of the word. Assumes it starts inside a word.
3506 * Returns a pointer to just after the word.
3507 */
3508 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003509find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
3511#ifdef FEAT_MBYTE
3512 int start_class;
3513
3514 if (has_mbyte)
3515 {
3516 start_class = mb_get_class(ptr);
3517 if (start_class > 1)
3518 while (*ptr != NUL)
3519 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003520 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (mb_get_class(ptr) != start_class)
3522 break;
3523 }
3524 }
3525 else
3526#endif
3527 while (vim_iswordc(*ptr))
3528 ++ptr;
3529 return ptr;
3530}
3531
3532/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003533 * Find the end of the line, omitting CR and NL at the end.
3534 * Returns a pointer to just after the line.
3535 */
3536 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003537find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003538{
3539 char_u *s;
3540
3541 s = ptr + STRLEN(ptr);
3542 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3543 --s;
3544 return s;
3545}
3546
3547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 * Free the list of completions
3549 */
3550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003551ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003553 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003554 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
Bram Moolenaard23a8232018-02-10 18:45:26 +01003556 VIM_CLEAR(compl_pattern);
3557 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003561
3562 ins_compl_del_pum();
3563 pum_clear();
3564
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003565 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 do
3567 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003568 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003569 compl_curr_match = compl_curr_match->cp_next;
3570 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003572 if (match->cp_flags & FREE_FNAME)
3573 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003574 for (i = 0; i < CPT_COUNT; ++i)
3575 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003577 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3578 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003579 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003580 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
3582
3583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003586 compl_cont_status = 0;
3587 compl_started = FALSE;
3588 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003589 VIM_CLEAR(compl_pattern);
3590 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003592 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003593 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003594 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003595 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596}
3597
3598/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003599 * Return TRUE when Insert completion is active.
3600 */
3601 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003602ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003603{
3604 return compl_started;
3605}
3606
3607/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003608 * Delete one character before the cursor and show the subset of the matches
3609 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003610 * Returns the character to be used, NUL if the work is done and another char
3611 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003612 */
3613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003614ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003615{
3616 char_u *line;
3617 char_u *p;
3618
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003619 line = ml_get_curline();
3620 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003621 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003622
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003623 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003624 * allow the word to be deleted, we won't match everything.
3625 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003626 if ((int)(p - line) - (int)compl_col < 0
3627 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003628 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3629 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3630 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003631 return K_BS;
3632
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003633 /* Deleted more than what was used to find matches or didn't finish
3634 * finding all matches: need to look for matches all over again. */
3635 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003636 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003637 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003638
Bram Moolenaara6557602006-02-04 22:43:20 +00003639 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003640 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003641 if (compl_leader != NULL)
3642 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003643 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003644 if (compl_shown_match != NULL)
3645 /* Make sure current match is not a hidden item. */
3646 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003647 return NUL;
3648 }
3649 return K_BS;
3650}
Bram Moolenaara6557602006-02-04 22:43:20 +00003651
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003652/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003653 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3654 * be called.
3655 */
3656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003658{
3659 /* Return TRUE if we didn't complete finding matches or when the
3660 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3661 return compl_was_interrupted
3662 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3663 && compl_opt_refresh_always);
3664}
3665
3666/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003667 * Called after changing "compl_leader".
3668 * Show the popup menu with a different set of matches.
3669 * May also search for matches again if the previous search was interrupted.
3670 */
3671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003673{
3674 ins_compl_del_pum();
3675 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003676 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003677 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003678
3679 if (compl_started)
3680 ins_compl_set_original_text(compl_leader);
3681 else
3682 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003683#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003684 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003685#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003686 /*
3687 * Matches were cleared, need to search for them now. First display
3688 * the changed text before the cursor. Set "compl_restarting" to
3689 * avoid that the first match is inserted.
3690 */
3691 update_screen(0);
3692#ifdef FEAT_GUI
3693 if (gui.in_use)
3694 {
3695 /* Show the cursor after the match, not after the redrawn text. */
3696 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003697 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003698 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003699#endif
3700 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003701 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003702 compl_cont_status = 0;
3703 compl_restarting = FALSE;
3704 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003705
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003706 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003707
3708 /* Show the popup menu with a different set of matches. */
3709 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003710
3711 /* Don't let Enter select the original text when there is no popup menu. */
3712 if (compl_match_array == NULL)
3713 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003714}
3715
3716/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003717 * Return the length of the completion, from the completion start column to
3718 * the cursor column. Making sure it never goes below zero.
3719 */
3720 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003721ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003722{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003723 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003724
3725 if (off < 0)
3726 return 0;
3727 return off;
3728}
3729
3730/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003731 * Append one character to the match leader. May reduce the number of
3732 * matches.
3733 */
3734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003735ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003736{
3737#ifdef FEAT_MBYTE
3738 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003739#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003740
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003741 if (stop_arrow() == FAIL)
3742 return;
3743#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003744 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3745 {
3746 char_u buf[MB_MAXBYTES + 1];
3747
3748 (*mb_char2bytes)(c, buf);
3749 buf[cc] = NUL;
3750 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003751 if (compl_opt_refresh_always)
3752 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003753 }
3754 else
3755#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003756 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003757 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003758 if (compl_opt_refresh_always)
3759 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003760 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003761
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003762 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003763 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003764 ins_compl_restart();
3765
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003766 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003767 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003768 * break redo. */
3769 if (!compl_opt_refresh_always)
3770 {
3771 vim_free(compl_leader);
3772 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003773 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003774 if (compl_leader != NULL)
3775 ins_compl_new_leader();
3776 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003777}
3778
3779/*
3780 * Setup for finding completions again without leaving CTRL-X mode. Used when
3781 * BS or a key was typed while still searching for matches.
3782 */
3783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003784ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003785{
3786 ins_compl_free();
3787 compl_started = FALSE;
3788 compl_matches = 0;
3789 compl_cont_status = 0;
3790 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003791}
3792
3793/*
3794 * Set the first match, the original text.
3795 */
3796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003797ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003798{
3799 char_u *p;
3800
Bram Moolenaare87edf32018-04-17 22:14:32 +02003801 /* Replace the original text entry.
3802 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3803 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003804 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3805 {
3806 p = vim_strsave(str);
3807 if (p != NULL)
3808 {
3809 vim_free(compl_first_match->cp_str);
3810 compl_first_match->cp_str = p;
3811 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003812 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003813 else if (compl_first_match->cp_prev != NULL
3814 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3815 {
3816 p = vim_strsave(str);
3817 if (p != NULL)
3818 {
3819 vim_free(compl_first_match->cp_prev->cp_str);
3820 compl_first_match->cp_prev->cp_str = p;
3821 }
3822 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003823}
3824
3825/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003826 * Append one character to the match leader. May reduce the number of
3827 * matches.
3828 */
3829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003830ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003831{
3832 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003833 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003834 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003835 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003836
3837 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003838 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003839 {
3840 /* When still at the original match use the first entry that matches
3841 * the leader. */
3842 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3843 {
3844 p = NULL;
3845 for (cp = compl_shown_match->cp_next; cp != NULL
3846 && cp != compl_first_match; cp = cp->cp_next)
3847 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003848 if (compl_leader == NULL
3849 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003850 (int)STRLEN(compl_leader)))
3851 {
3852 p = cp->cp_str;
3853 break;
3854 }
3855 }
3856 if (p == NULL || (int)STRLEN(p) <= len)
3857 return;
3858 }
3859 else
3860 return;
3861 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003862 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003863 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003864 ins_compl_addleader(c);
3865}
3866
3867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003869 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003870 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003873ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
3875 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003877 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
3879 /* Forget any previous 'special' messages if this is actually
3880 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3881 */
3882 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3883 edit_submode_extra = NULL;
3884
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003885 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003886 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3887 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003888 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003890 /* Set "compl_get_longest" when finding the first matches. */
3891 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003892 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003893 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003894 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003895 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003896
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003897 }
3898
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3900 {
3901 /*
3902 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3903 * it will be yet. Now we decide.
3904 */
3905 switch (c)
3906 {
3907 case Ctrl_E:
3908 case Ctrl_Y:
3909 ctrl_x_mode = CTRL_X_SCROLL;
3910 if (!(State & REPLACE_FLAG))
3911 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3912 else
3913 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3914 edit_submode_pre = NULL;
3915 showmode();
3916 break;
3917 case Ctrl_L:
3918 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3919 break;
3920 case Ctrl_F:
3921 ctrl_x_mode = CTRL_X_FILES;
3922 break;
3923 case Ctrl_K:
3924 ctrl_x_mode = CTRL_X_DICTIONARY;
3925 break;
3926 case Ctrl_R:
3927 /* Simply allow ^R to happen without affecting ^X mode */
3928 break;
3929 case Ctrl_T:
3930 ctrl_x_mode = CTRL_X_THESAURUS;
3931 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003932#ifdef FEAT_COMPL_FUNC
3933 case Ctrl_U:
3934 ctrl_x_mode = CTRL_X_FUNCTION;
3935 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003936 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003937 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003938 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003939#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003940 case 's':
3941 case Ctrl_S:
3942 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003943#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003944 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003945 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003946 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003947#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 case Ctrl_RSB:
3950 ctrl_x_mode = CTRL_X_TAGS;
3951 break;
3952#ifdef FEAT_FIND_ID
3953 case Ctrl_I:
3954 case K_S_TAB:
3955 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3956 break;
3957 case Ctrl_D:
3958 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3959 break;
3960#endif
3961 case Ctrl_V:
3962 case Ctrl_Q:
3963 ctrl_x_mode = CTRL_X_CMDLINE;
3964 break;
3965 case Ctrl_P:
3966 case Ctrl_N:
3967 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3968 * just started ^X mode, or there were enough ^X's to cancel
3969 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3970 * do normal expansion when interrupting a different mode (say
3971 * ^X^F^X^P or ^P^X^X^P, see below)
3972 * nothing changes if interrupting mode 0, (eg, the flag
3973 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003974 if (!(compl_cont_status & CONT_INTRPT))
3975 compl_cont_status |= CONT_LOCAL;
3976 else if (compl_cont_mode != 0)
3977 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 /* FALLTHROUGH */
3979 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003980 /* If we have typed at least 2 ^X's... for modes != 0, we set
3981 * compl_cont_status = 0 (eg, as if we had just started ^X
3982 * mode).
3983 * For mode 0, we set "compl_cont_mode" to an impossible
3984 * value, in both cases ^X^X can be used to restart the same
3985 * mode (avoiding ADDING mode).
3986 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3987 * 'complete' and local ^P expansions respectively.
3988 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3989 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (c == Ctrl_X)
3991 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003992 if (compl_cont_mode != 0)
3993 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003995 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003997 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 edit_submode = NULL;
3999 showmode();
4000 break;
4001 }
4002 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004003 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
4005 /* We're already in CTRL-X mode, do we stay in it? */
4006 if (!vim_is_ctrl_x_key(c))
4007 {
4008 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004009 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 else
4011 ctrl_x_mode = CTRL_X_FINISHED;
4012 edit_submode = NULL;
4013 }
4014 showmode();
4015 }
4016
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004017 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
4019 /* Show error message from attempted keyword completion (probably
4020 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004021 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004023 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4024 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 || ctrl_x_mode == CTRL_X_FINISHED)
4026 {
4027 /* Get here when we have finished typing a sequence of ^N and
4028 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004029 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004030 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
4032 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004033 * If any of the original typed text has been changed, eg when
4034 * ignorecase is set, we must add back-spaces to the redo
4035 * buffer. We add as few as necessary to delete just the part
4036 * of the original text that has changed.
4037 * When using the longest match, edited the match or used
4038 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004040 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4041 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004042 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004043 ptr = NULL;
4044 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046
4047#ifdef FEAT_CINDENT
4048 want_cindent = (can_cindent && cindent_on());
4049#endif
4050 /*
4051 * When completing whole lines: fix indent for 'cindent'.
4052 * Otherwise, break line if it's too long.
4053 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
4056#ifdef FEAT_CINDENT
4057 /* re-indent the current line */
4058 if (want_cindent)
4059 {
4060 do_c_expr_indent();
4061 want_cindent = FALSE; /* don't do it again */
4062 }
4063#endif
4064 }
4065 else
4066 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004067 int prev_col = curwin->w_cursor.col;
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004070 if (prev_col > 0)
4071 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004072 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004073 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004075 if (prev_col > 0
4076 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4077 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004080 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004081 * the selection without inserting anything. When
4082 * compl_enter_selects is set the Enter key does the same. */
4083 if ((c == Ctrl_Y || (compl_enter_selects
4084 && (c == CAR || c == K_KENTER || c == NL)))
4085 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004086 retval = TRUE;
4087
Bram Moolenaar47247282016-08-02 22:36:02 +02004088 /* CTRL-E means completion is Ended, go back to the typed text.
4089 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004090 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004091 {
4092 ins_compl_delete();
4093 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004094 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004095 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004096 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004097 retval = TRUE;
4098 }
4099
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004100 auto_format(FALSE, TRUE);
4101
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004103 compl_started = FALSE;
4104 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004105 if (!shortmess(SHM_COMPLETIONMENU))
4106 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004107 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004108 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (edit_submode != NULL)
4110 {
4111 edit_submode = NULL;
4112 showmode();
4113 }
4114
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004115#ifdef FEAT_CMDWIN
4116 if (c == Ctrl_C && cmdwin_type != 0)
4117 /* Avoid the popup menu remains displayed when leaving the
4118 * command line window. */
4119 update_screen(0);
4120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121#ifdef FEAT_CINDENT
4122 /*
4123 * Indent now if a key was typed that is in 'cinkeys'.
4124 */
4125 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4126 do_c_expr_indent();
4127#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004128 /* Trigger the CompleteDone event to give scripts a chance to act
4129 * upon the completion. */
4130 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004133 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4134 /* Trigger the CompleteDone event to give scripts a chance to act
4135 * upon the (possibly failed) completion. */
4136 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 /* reset continue_* if we left expansion-mode, if we stay they'll be
4139 * (re)set properly in ins_complete() */
4140 if (!vim_is_ctrl_x_key(c))
4141 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004142 compl_cont_status = 0;
4143 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004145
4146 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147}
4148
4149/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004150 * Fix the redo buffer for the completion leader replacing some of the typed
4151 * text. This inserts backspaces and appends the changed text.
4152 * "ptr" is the known leader text or NUL.
4153 */
4154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004155ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004156{
4157 int len;
4158 char_u *p;
4159 char_u *ptr = ptr_arg;
4160
4161 if (ptr == NULL)
4162 {
4163 if (compl_leader != NULL)
4164 ptr = compl_leader;
4165 else
4166 return; /* nothing to do */
4167 }
4168 if (compl_orig_text != NULL)
4169 {
4170 p = compl_orig_text;
4171 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4172 ;
4173#ifdef FEAT_MBYTE
4174 if (len > 0)
4175 len -= (*mb_head_off)(p, p + len);
4176#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004177 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004178 AppendCharToRedobuff(K_BS);
4179 }
4180 else
4181 len = 0;
4182 if (ptr != NULL)
4183 AppendToRedobuffLit(ptr + len, -1);
4184}
4185
4186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4188 * (depending on flag) starting from buf and looking for a non-scanned
4189 * buffer (other than curbuf). curbuf is special, if it is called with
4190 * buf=curbuf then it has to be the first call for a given flag/expansion.
4191 *
4192 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4193 */
4194 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004195ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
4199 if (flag == 'w') /* just windows */
4200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 if (buf == curbuf) /* first call for this flag/expansion */
4202 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004203 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 && wp->w_buffer->b_scanned)
4205 ;
4206 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208 else
4209 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4210 * (unlisted buffers)
4211 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004212 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 && ((flag == 'U'
4214 ? buf->b_p_bl
4215 : (!buf->b_p_bl
4216 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004217 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 ;
4219 return buf;
4220}
4221
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004222#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004223/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004224 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004225 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004226 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004228expand_by_function(
4229 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4230 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004231{
Bram Moolenaar82139082011-09-14 16:52:09 +02004232 list_T *matchlist = NULL;
4233 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004234 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004235 char_u *funcname;
4236 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004237 win_T *curwin_save;
4238 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004239 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004240
Bram Moolenaare344bea2005-09-01 20:46:49 +00004241 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4242 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004243 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004244
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004245 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004246 args[0].v_type = VAR_NUMBER;
4247 args[0].vval.v_number = 0;
4248 args[1].v_type = VAR_STRING;
4249 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4250 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004251
Bram Moolenaare344bea2005-09-01 20:46:49 +00004252 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004253 curwin_save = curwin;
4254 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004255
4256 /* Call a function, which returns a list or dict. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004257 if (call_vim_function(funcname, 2, args, &rettv, FALSE) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004258 {
4259 switch (rettv.v_type)
4260 {
4261 case VAR_LIST:
4262 matchlist = rettv.vval.v_list;
4263 break;
4264 case VAR_DICT:
4265 matchdict = rettv.vval.v_dict;
4266 break;
4267 default:
4268 /* TODO: Give error message? */
4269 clear_tv(&rettv);
4270 break;
4271 }
4272 }
4273
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004274 if (curwin_save != curwin || curbuf_save != curbuf)
4275 {
4276 EMSG(_(e_complwin));
4277 goto theend;
4278 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004279 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004280 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004281 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004282 {
4283 EMSG(_(e_compldel));
4284 goto theend;
4285 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004286
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004287 if (matchlist != NULL)
4288 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004289 else if (matchdict != NULL)
4290 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004291
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004292theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004293 if (matchdict != NULL)
4294 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004295 if (matchlist != NULL)
4296 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004297}
4298#endif /* FEAT_COMPL_FUNC */
4299
Bram Moolenaar39f05632006-03-19 22:15:26 +00004300#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004301/*
4302 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004303 */
4304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004305ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004306{
4307 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004308 int dir = compl_direction;
4309
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004310 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004311 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004312 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004313 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4314 /* if dir was BACKWARD then honor it just once */
4315 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004316 else if (did_emsg)
4317 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004318 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004319}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004320
4321/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004322 * Add completions from a dict.
4323 */
4324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004325ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004326{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004327 dictitem_T *di_refresh;
4328 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004329
4330 /* Check for optional "refresh" item. */
4331 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004332 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4333 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004334 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004335 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004336
4337 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4338 compl_opt_refresh_always = TRUE;
4339 }
4340
4341 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004342 di_words = dict_find(dict, (char_u *)"words", 5);
4343 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4344 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004345}
4346
4347/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004348 * Add a match to the list of matches from a typeval_T.
4349 * If the given string is already in the list of completions, then return
4350 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4351 * maybe because alloc() returns NULL, then FAIL is returned.
4352 */
4353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004354ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004355{
4356 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004357 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004358 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004359 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004360 char_u *(cptext[CPT_COUNT]);
4361
4362 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4363 {
4364 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4365 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4366 (char_u *)"abbr", FALSE);
4367 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4368 (char_u *)"menu", FALSE);
4369 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4370 (char_u *)"kind", FALSE);
4371 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4372 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004373 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4374 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004375 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4376 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004377 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004378 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004379 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4380 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004381 }
4382 else
4383 {
4384 word = get_tv_string_chk(tv);
4385 vim_memset(cptext, 0, sizeof(cptext));
4386 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004387 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004388 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004389 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004390}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004391#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004392
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393/*
4394 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004395 * The search starts at position "ini" in curbuf and in the direction
4396 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004397 * When "compl_started" is FALSE start at that position, otherwise continue
4398 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 * This may return before finding all the matches.
4400 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 */
4402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004403ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
4405 static pos_T first_match_pos;
4406 static pos_T last_match_pos;
4407 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004408 static int found_all = FALSE; /* Found all matches of a
4409 certain type. */
4410 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar572cb562005-08-05 21:35:02 +00004412 pos_T *pos;
4413 char_u **matches;
4414 int save_p_scs;
4415 int save_p_ws;
4416 int save_p_ic;
4417 int i;
4418 int num_matches;
4419 int len;
4420 int found_new_match;
4421 int type = ctrl_x_mode;
4422 char_u *ptr;
4423 char_u *dict = NULL;
4424 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004425 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004429 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 ins_buf->b_scanned = 0;
4431 found_all = FALSE;
4432 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004433 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004434 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 last_match_pos = first_match_pos = *ini;
4436 }
4437
Bram Moolenaar4475b622017-05-01 20:46:52 +02004438 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004439 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4441 for (;;)
4442 {
4443 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004444 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004446 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 * or if found_all says this entry is done. For ^X^L only use the
4448 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004449 if ((ctrl_x_mode == CTRL_X_NORMAL
4450 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 found_all = FALSE;
4454 while (*e_cpt == ',' || *e_cpt == ' ')
4455 e_cpt++;
4456 if (*e_cpt == '.' && !curbuf->b_scanned)
4457 {
4458 ins_buf = curbuf;
4459 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004460 /* Move the cursor back one character so that ^N can match the
4461 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004462 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004463 {
4464 /* Move the cursor to after the last character in the
4465 * buffer, so that word at start of buffer is found
4466 * correctly. */
4467 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4468 first_match_pos.col =
4469 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 last_match_pos = first_match_pos;
4472 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004473
4474 /* Remember the first match so that the loop stops when we
4475 * wrap and come back there a second time. */
4476 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4479 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4480 {
4481 /* Scan a buffer, but not the current one. */
4482 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4483 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004484 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 first_match_pos.col = last_match_pos.col = 0;
4486 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4487 last_match_pos.lnum = 0;
4488 type = 0;
4489 }
4490 else /* unloaded buffer, scan like dictionary */
4491 {
4492 found_all = TRUE;
4493 if (ins_buf->b_fname == NULL)
4494 continue;
4495 type = CTRL_X_DICTIONARY;
4496 dict = ins_buf->b_fname;
4497 dict_f = DICT_EXACT;
4498 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004499 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 ins_buf->b_fname == NULL
4501 ? buf_spname(ins_buf)
4502 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004503 ? ins_buf->b_fname
4504 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004505 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507 else if (*e_cpt == NUL)
4508 break;
4509 else
4510 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004511 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 type = -1;
4513 else if (*e_cpt == 'k' || *e_cpt == 's')
4514 {
4515 if (*e_cpt == 'k')
4516 type = CTRL_X_DICTIONARY;
4517 else
4518 type = CTRL_X_THESAURUS;
4519 if (*++e_cpt != ',' && *e_cpt != NUL)
4520 {
4521 dict = e_cpt;
4522 dict_f = DICT_FIRST;
4523 }
4524 }
4525#ifdef FEAT_FIND_ID
4526 else if (*e_cpt == 'i')
4527 type = CTRL_X_PATH_PATTERNS;
4528 else if (*e_cpt == 'd')
4529 type = CTRL_X_PATH_DEFINES;
4530#endif
4531 else if (*e_cpt == ']' || *e_cpt == 't')
4532 {
4533 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004534 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004535 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 }
4537 else
4538 type = -1;
4539
4540 /* in any case e_cpt is advanced to the next entry */
4541 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4542
4543 found_all = TRUE;
4544 if (type == -1)
4545 continue;
4546 }
4547 }
4548
Bram Moolenaar4475b622017-05-01 20:46:52 +02004549 /* If complete() was called then compl_pattern has been reset. The
4550 * following won't work then, bail out. */
4551 if (compl_pattern == NULL)
4552 break;
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 switch (type)
4555 {
4556 case -1:
4557 break;
4558#ifdef FEAT_FIND_ID
4559 case CTRL_X_PATH_PATTERNS:
4560 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004561 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4566 (linenr_T)1, (linenr_T)MAXLNUM);
4567 break;
4568#endif
4569
4570 case CTRL_X_DICTIONARY:
4571 case CTRL_X_THESAURUS:
4572 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004573 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 : (type == CTRL_X_THESAURUS
4575 ? (*curbuf->b_p_tsr == NUL
4576 ? p_tsr
4577 : curbuf->b_p_tsr)
4578 : (*curbuf->b_p_dict == NUL
4579 ? p_dict
4580 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004581 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004582 dict != NULL ? dict_f
4583 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 dict = NULL;
4585 break;
4586
4587 case CTRL_X_TAGS:
4588 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4589 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004592 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 * of matches is found when compl_pattern is empty */
4594 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004595 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4596 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4598 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004599 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 p_ic = save_p_ic;
4602 break;
4603
4604 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4607 {
4608
4609 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004611 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613 break;
4614
4615 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 if (expand_cmdline(&compl_xp, compl_pattern,
4617 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004619 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 break;
4621
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004622#ifdef FEAT_COMPL_FUNC
4623 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004624 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004625 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004626 break;
4627#endif
4628
Bram Moolenaar488c6512005-08-11 20:09:58 +00004629 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004630#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004631 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004632 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004633 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004634 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004635#endif
4636 break;
4637
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 default: /* normal ^P/^N and ^X^L */
4639 /*
4640 * If 'infercase' is set, don't use 'smartcase' here
4641 */
4642 save_p_scs = p_scs;
4643 if (ins_buf->b_p_inf)
4644 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645
Bram Moolenaar361aa502014-01-23 22:45:58 +01004646 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 * end but never from the middle, thus setting nowrapscan in this
4648 * buffers is a good idea, on the other hand, we always set
4649 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4650 save_p_ws = p_ws;
4651 if (ins_buf != curbuf)
4652 p_ws = FALSE;
4653 else if (*e_cpt == '.')
4654 p_ws = TRUE;
4655 for (;;)
4656 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004657 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004659 ++msg_silent; /* Don't want messages for wrapscan. */
4660
Bram Moolenaare4214502015-03-05 18:08:43 +01004661 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4662 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004663 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004664 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004665 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004667 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004669 found_new_match = searchit(NULL, ins_buf, pos,
4670 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004671 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004672 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004673 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004674 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004676 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004677 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 first_match_pos = *pos;
4679 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004680 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004683 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 found_new_match = FAIL;
4685 if (found_new_match == FAIL)
4686 {
4687 if (ins_buf == curbuf)
4688 found_all = TRUE;
4689 break;
4690 }
4691
4692 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004693 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 && ini->lnum == pos->lnum
4695 && ini->col == pos->col)
4696 continue;
4697 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004698 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004700 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
4702 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4703 continue;
4704 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4705 if (!p_paste)
4706 ptr = skipwhite(ptr);
4707 }
4708 len = (int)STRLEN(ptr);
4709 }
4710 else
4711 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004712 char_u *tmp_ptr = ptr;
4713
4714 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004716 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 /* Skip if already inside a word. */
4718 if (vim_iswordp(tmp_ptr))
4719 continue;
4720 /* Find start of next word. */
4721 tmp_ptr = find_word_start(tmp_ptr);
4722 }
4723 /* Find end of this word. */
4724 tmp_ptr = find_word_end(tmp_ptr);
4725 len = (int)(tmp_ptr - ptr);
4726
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004727 if ((compl_cont_status & CONT_ADDING)
4728 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
4730 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4731 {
4732 /* Try next line, if any. the new word will be
4733 * "join" as if the normal command "J" was used.
4734 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004735 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 * works -- Acevedo */
4737 STRNCPY(IObuff, ptr, len);
4738 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4739 tmp_ptr = ptr = skipwhite(ptr);
4740 /* Find start of next word. */
4741 tmp_ptr = find_word_start(tmp_ptr);
4742 /* Find end of next word. */
4743 tmp_ptr = find_word_end(tmp_ptr);
4744 if (tmp_ptr > ptr)
4745 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004746 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004748 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 IObuff[len++] = ' ';
4750 /* IObuf =~ "\k.* ", thus len >= 2 */
4751 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004752 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 || (vim_strchr(p_cpo, CPO_JOINSP)
4754 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004755 && (IObuff[len - 2] == '?'
4756 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 IObuff[len++] = ' ';
4758 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004759 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 if (tmp_ptr - ptr >= IOSIZE - len)
4761 tmp_ptr = ptr + IOSIZE - len - 1;
4762 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4763 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004764 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 IObuff[len] = NUL;
4767 ptr = IObuff;
4768 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004769 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 continue;
4771 }
4772 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004773 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004774 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004775 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
4777 found_new_match = OK;
4778 break;
4779 }
4780 }
4781 p_scs = save_p_scs;
4782 p_ws = save_p_ws;
4783 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004784
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004785 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004786 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004787 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 found_new_match = OK;
4789
4790 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004791 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4792 * match */
4793 if ((ctrl_x_mode != CTRL_X_NORMAL
4794 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004795 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004796 {
4797 if (got_int)
4798 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004799 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004800 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004801 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004802
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004803 if ((ctrl_x_mode != CTRL_X_NORMAL
4804 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004805 || compl_interrupted)
4806 break;
4807 compl_started = TRUE;
4808 }
4809 else
4810 {
4811 /* Mark a buffer scanned when it has been scanned completely */
4812 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4813 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004815 compl_started = FALSE;
4816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004818 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004820 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 && *e_cpt == NUL) /* Got to end of 'complete' */
4822 found_new_match = FAIL;
4823
4824 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004825 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4826 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 i = ins_compl_make_cyclic();
4828
Bram Moolenaar4475b622017-05-01 20:46:52 +02004829 if (compl_old_match != NULL)
4830 {
4831 /* If several matches were added (FORWARD) or the search failed and has
4832 * just been made cyclic then we have to move compl_curr_match to the
4833 * next or previous entry (if any) -- Acevedo */
4834 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4835 : compl_old_match->cp_prev;
4836 if (compl_curr_match == NULL)
4837 compl_curr_match = compl_old_match;
4838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 return i;
4840}
4841
4842/* Delete the old text being completed. */
4843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004844ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004846 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
4848 /*
4849 * In insert mode: Delete the typed part.
4850 * In replace mode: Put the old characters back, if any.
4851 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004852 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4853 if ((int)curwin->w_cursor.col > col)
4854 {
4855 if (stop_arrow() == FAIL)
4856 return;
4857 backspace_until_column(col);
4858 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004859
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004860 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4861 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004863 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004864 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865}
4866
Bram Moolenaar472e8592016-10-15 17:06:47 +02004867/*
4868 * Insert the new text being completed.
4869 * "in_compl_func" is TRUE when called from complete_check().
4870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004872ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004874 dict_T *dict;
4875
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004876 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004877 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4878 compl_used_match = FALSE;
4879 else
4880 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004881
4882 /* Set completed item. */
4883 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004884 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004885 if (dict != NULL)
4886 {
4887 dict_add_nr_str(dict, "word", 0L,
4888 EMPTY_IF_NULL(compl_shown_match->cp_str));
4889 dict_add_nr_str(dict, "abbr", 0L,
4890 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4891 dict_add_nr_str(dict, "menu", 0L,
4892 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4893 dict_add_nr_str(dict, "kind", 0L,
4894 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4895 dict_add_nr_str(dict, "info", 0L,
4896 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004897 dict_add_nr_str(dict, "user_data", 0L,
4898 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004899 }
4900 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004901 if (!in_compl_func)
4902 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903}
4904
4905/*
4906 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004907 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4908 * get more completions. If it is FALSE, then we just do nothing when there
4909 * are no more completions in a given direction. The latter case is used when
4910 * we are still in the middle of finding completions, to allow browsing
4911 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 * Return the total number of matches, or -1 if still unknown -- webb.
4913 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004914 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4915 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 *
4917 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004918 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4919 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 */
4921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004922ins_compl_next(
4923 int allow_get_expansion,
4924 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004925 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004926 int insert_match, /* Insert the newly selected match */
4927 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
4929 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004930 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004931 compl_T *found_compl = NULL;
4932 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004933 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004934 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004936 /* When user complete function return -1 for findstart which is next
4937 * time of 'always', compl_shown_match become NULL. */
4938 if (compl_shown_match == NULL)
4939 return -1;
4940
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004941 if (compl_leader != NULL
4942 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004944 /* Set "compl_shown_match" to the actually shown match, it may differ
4945 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004946 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004947 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004948 && compl_shown_match->cp_next != NULL
4949 && compl_shown_match->cp_next != compl_first_match)
4950 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004951
4952 /* If we didn't find it searching forward, and compl_shows_dir is
4953 * backward, find the last match. */
4954 if (compl_shows_dir == BACKWARD
4955 && !ins_compl_equal(compl_shown_match,
4956 compl_leader, (int)STRLEN(compl_leader))
4957 && (compl_shown_match->cp_next == NULL
4958 || compl_shown_match->cp_next == compl_first_match))
4959 {
4960 while (!ins_compl_equal(compl_shown_match,
4961 compl_leader, (int)STRLEN(compl_leader))
4962 && compl_shown_match->cp_prev != NULL
4963 && compl_shown_match->cp_prev != compl_first_match)
4964 compl_shown_match = compl_shown_match->cp_prev;
4965 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004966 }
4967
4968 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004969 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 /* Delete old text to be replaced */
4971 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004972
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004973 /* When finding the longest common text we stick at the original text,
4974 * don't let CTRL-N or CTRL-P move to the first match. */
4975 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4976
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004977 /* When restarting the search don't insert the first match either. */
4978 if (compl_restarting)
4979 {
4980 advance = FALSE;
4981 compl_restarting = FALSE;
4982 }
4983
Bram Moolenaare3226be2005-12-18 22:10:00 +00004984 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4985 * around. */
4986 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004988 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004990 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004991 found_end = (compl_first_match != NULL
4992 && (compl_shown_match->cp_next == compl_first_match
4993 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004994 }
4995 else if (compl_shows_dir == BACKWARD
4996 && compl_shown_match->cp_prev != NULL)
4997 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004998 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004999 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00005000 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00005003 {
Bram Moolenaara260a972006-06-23 19:36:29 +00005004 if (!allow_get_expansion)
5005 {
5006 if (advance)
5007 {
5008 if (compl_shows_dir == BACKWARD)
5009 compl_pending -= todo + 1;
5010 else
5011 compl_pending += todo + 1;
5012 }
5013 return -1;
5014 }
5015
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005016 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005017 {
5018 if (compl_shows_dir == BACKWARD)
5019 --compl_pending;
5020 else
5021 ++compl_pending;
5022 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005023
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005024 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005025 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005026
5027 /* handle any pending completions */
5028 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005029 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005030 {
5031 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5032 {
5033 compl_shown_match = compl_shown_match->cp_next;
5034 --compl_pending;
5035 }
5036 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5037 {
5038 compl_shown_match = compl_shown_match->cp_prev;
5039 ++compl_pending;
5040 }
5041 else
5042 break;
5043 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005044 found_end = FALSE;
5045 }
5046 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5047 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005048 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005049 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005050 ++todo;
5051 else
5052 /* Remember a matching item. */
5053 found_compl = compl_shown_match;
5054
5055 /* Stop at the end of the list when we found a usable match. */
5056 if (found_end)
5057 {
5058 if (found_compl != NULL)
5059 {
5060 compl_shown_match = found_compl;
5061 break;
5062 }
5063 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005067 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005068 if (compl_no_insert && !started)
5069 {
5070 ins_bytes(compl_orig_text + ins_compl_len());
5071 compl_used_match = FALSE;
5072 }
5073 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005074 {
5075 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005076 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005077 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005078 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005079 }
5080 else
5081 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
5083 if (!allow_get_expansion)
5084 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005085 /* may undisplay the popup menu first */
5086 ins_compl_upd_pum();
5087
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005088 /* redraw to show the user what was inserted */
5089 update_screen(0);
5090
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005091 /* display the updated popup menu */
5092 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005093#ifdef FEAT_GUI
5094 if (gui.in_use)
5095 {
5096 /* Show the cursor after the match, not after the redrawn text. */
5097 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005098 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005099 }
5100#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005101
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 /* Delete old text to be replaced, since we're still searching and
5103 * don't want to match ourselves! */
5104 ins_compl_delete();
5105 }
5106
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005107 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005108 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005109 if (compl_no_insert && !started)
5110 compl_enter_selects = TRUE;
5111 else
5112 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 /*
5115 * Show the file name for the match (if any)
5116 * Truncate the file name to avoid a wait for return.
5117 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005118 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005120 char *lead = _("match in file");
5121 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5122 char_u *s;
5123 char_u *e;
5124
5125 if (space > 0)
5126 {
5127 /* We need the tail that fits. With double-byte encoding going
5128 * back from the end is very slow, thus go from the start and keep
5129 * the text that fits in "space" between "s" and "e". */
5130 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5131 {
5132 space -= ptr2cells(e);
5133 while (space < 0)
5134 {
5135 space += ptr2cells(s);
5136 MB_PTR_ADV(s);
5137 }
5138 }
5139 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5140 s > compl_shown_match->cp_fname ? "<" : "", s);
5141 msg(IObuff);
5142 redraw_cmdline = FALSE; /* don't overwrite! */
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145
5146 return num_matches;
5147}
5148
5149/*
5150 * Call this while finding completions, to check whether the user has hit a key
5151 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005152 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005154 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005155 * "in_compl_func" is TRUE when called from complete_check(), don't set
5156 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 */
5158 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005159ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160{
5161 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005162 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005164 /* Don't check when reading keys from a script, :normal or feedkeys().
5165 * That would break the test scripts. But do check for keys when called
5166 * from complete_check(). */
5167 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 return;
5169
5170 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005171 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 return;
5173 count = 0;
5174
Bram Moolenaara260a972006-06-23 19:36:29 +00005175 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5176 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (c != NUL)
5179 {
5180 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5181 {
5182 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005183 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005184 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005185 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005187 else
5188 {
5189 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005190 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005191 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005192 if (c != K_IGNORE)
5193 {
5194 /* Don't interrupt completion when the character wasn't typed,
5195 * e.g., when doing @q to replay keys. */
5196 if (c != Ctrl_R && KeyTyped)
5197 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005198
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005199 vungetc(c);
5200 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005203 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005204 {
5205 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5206
5207 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005208 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005209 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005210}
5211
5212/*
5213 * Decide the direction of Insert mode complete from the key typed.
5214 * Returns BACKWARD or FORWARD.
5215 */
5216 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005217ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005218{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005219 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005220 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005221 return BACKWARD;
5222 return FORWARD;
5223}
5224
5225/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005226 * Return TRUE for keys that are used for completion only when the popup menu
5227 * is visible.
5228 */
5229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005230ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005231{
5232 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005233 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5234 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005235}
5236
5237/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005238 * Decide the number of completions to move forward.
5239 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5240 */
5241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005242ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005243{
5244 int h;
5245
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005246 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005247 {
5248 h = pum_get_height();
5249 if (h > 3)
5250 h -= 2; /* keep some context */
5251 return h;
5252 }
5253 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254}
5255
5256/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005257 * Return TRUE if completion with "c" should insert the match, FALSE if only
5258 * to change the currently selected completion.
5259 */
5260 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005261ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005262{
5263 switch (c)
5264 {
5265 case K_UP:
5266 case K_DOWN:
5267 case K_PAGEDOWN:
5268 case K_KPAGEDOWN:
5269 case K_S_DOWN:
5270 case K_PAGEUP:
5271 case K_KPAGEUP:
5272 case K_S_UP:
5273 return FALSE;
5274 }
5275 return TRUE;
5276}
5277
5278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 * Do Insert mode completion.
5280 * Called when character "c" was typed, which has a meaning for completion.
5281 * Returns OK if completion was done, FAIL if something failed (out of mem).
5282 */
5283 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005284ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005286 char_u *line;
5287 int startcol = 0; /* column where searched text starts */
5288 colnr_T curs_col; /* cursor column */
5289 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005290 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005291 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005292 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005293 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
Bram Moolenaare3226be2005-12-18 22:10:00 +00005295 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005296 insert_match = ins_compl_use_match(c);
5297
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
5300 /* First time we hit ^N or ^P (in a row, I mean) */
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 did_ai = FALSE;
5303#ifdef FEAT_SMARTINDENT
5304 did_si = FALSE;
5305 can_si = FALSE;
5306 can_si_back = FALSE;
5307#endif
5308 if (stop_arrow() == FAIL)
5309 return FAIL;
5310
5311 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005312 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005313 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005315 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005316 * "compl_startpos" to the cursor as a pattern to add a new word
5317 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005318 * "compl_startpos" is not in the same line as the cursor then fix it
5319 * (the line has been split because it was longer than 'tw'). if SOL
5320 * is set then skip the previous pattern, a word at the beginning of
5321 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005322 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5323 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
5325 /*
5326 * it is a continued search
5327 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005329 if (ctrl_x_mode == CTRL_X_NORMAL
5330 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5331 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 /* line (probably) wrapped, set compl_startpos to the
5336 * first non_blank in the line, if it is not a wordchar
5337 * include it to get a better pattern, but then we don't
5338 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005339 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 compl_startpos.col = compl_col;
5341 compl_startpos.lnum = curwin->w_cursor.lnum;
5342 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 }
5344 else
5345 {
5346 /* S_IPOS was set when we inserted a word that was at the
5347 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 * mode but first we need to redefine compl_startpos */
5349 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 compl_cont_status |= CONT_SOL;
5352 compl_startpos.col = (colnr_T)(skipwhite(
5353 line + compl_length
5354 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005356 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005359 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005360 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 compl_cont_status &= ~CONT_SOL;
5365 compl_length = (IOSIZE - MIN_SPACE);
5366 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5369 if (compl_length < 1)
5370 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005372 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005378 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005383 if (ctrl_x_mode != CTRL_X_NORMAL)
5384 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005385 compl_cont_status = 0;
5386 compl_cont_status |= CONT_N_ADDS;
5387 compl_startpos = curwin->w_cursor;
5388 startcol = (int)curs_col;
5389 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391
5392 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005393 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5397 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005398 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005400 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005402 compl_col += ++startcol;
5403 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 }
5405 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005406 compl_pattern = str_foldcase(line + compl_col,
5407 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 compl_pattern = vim_strnsave(line + compl_col,
5410 compl_length);
5411 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 return FAIL;
5413 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {
5416 char_u *prefix = (char_u *)"\\<";
5417
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005418 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005420 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005423 if (!vim_iswordp(line + compl_col)
5424 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 && (
5426#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005427 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#endif
5431 )))
5432 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005433 STRCPY((char *)compl_pattern, prefix);
5434 (void)quote_meta(compl_pattern + STRLEN(prefix),
5435 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005437 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005439 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442#endif
5443 )
5444 {
5445 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005446 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5447 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005449 compl_col += curs_col;
5450 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
5452 else
5453 {
5454#ifdef FEAT_MBYTE
5455 /* Search the point of change class of multibyte character
5456 * or not a word single byte character backward. */
5457 if (has_mbyte)
5458 {
5459 int base_class;
5460 int head_off;
5461
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005462 startcol -= (*mb_head_off)(line, line + startcol);
5463 base_class = mb_get_class(line + startcol);
5464 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005466 head_off = (*mb_head_off)(line, line + startcol);
5467 if (base_class != mb_get_class(line + startcol
5468 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 }
5473 else
5474#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005475 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005477 compl_col += ++startcol;
5478 compl_length = (int)curs_col - startcol;
5479 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {
5481 /* Only match word with at least two chars -- webb
5482 * there's no need to call quote_meta,
5483 * alloc(7) is enough -- Acevedo
5484 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005485 compl_pattern = alloc(7);
5486 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005488 STRCPY((char *)compl_pattern, "\\<");
5489 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5490 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492 else
5493 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005494 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005495 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005496 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005498 STRCPY((char *)compl_pattern, "\\<");
5499 (void)quote_meta(compl_pattern + 2, line + compl_col,
5500 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502 }
5503 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005504 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005506 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005507 compl_length = (int)curs_col - (int)compl_col;
5508 if (compl_length < 0) /* cursor in indent: empty pattern */
5509 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005511 compl_pattern = str_foldcase(line + compl_col, compl_length,
5512 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005514 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5515 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 return FAIL;
5517 }
5518 else if (ctrl_x_mode == CTRL_X_FILES)
5519 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005520 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005521 if (startcol > 0)
5522 {
5523 char_u *p = line + startcol;
5524
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005525 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005526 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005527 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005528 if (p == line && vim_isfilec(PTR2CHAR(p)))
5529 startcol = 0;
5530 else
5531 startcol = (int)(p - line) + 1;
5532 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005533
Bram Moolenaar0300e462013-09-08 16:03:45 +02005534 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 compl_length = (int)curs_col - startcol;
5536 compl_pattern = addstar(line + compl_col, compl_length,
5537 EXPAND_FILES);
5538 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 return FAIL;
5540 }
5541 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5542 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005543 compl_pattern = vim_strnsave(line, curs_col);
5544 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005546 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005547 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005548 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5549 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005550 /* No completion possible, use an empty pattern to get a
5551 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005552 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005553 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005554 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5555 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005557 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005558 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005559#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005560 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005561 * Call user defined function 'completefunc' with "a:findstart"
5562 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005563 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005564 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005565 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005566 char_u *funcname;
5567 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005568 win_T *curwin_save;
5569 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005570
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005571 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005572 * string */
5573 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5574 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5575 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005576 {
5577 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5578 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005579 /* restore did_ai, so that adding comment leader works */
5580 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005581 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005582 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005583
Bram Moolenaarffa96842018-06-12 22:05:14 +02005584 args[0].v_type = VAR_NUMBER;
5585 args[0].vval.v_number = 1;
5586 args[1].v_type = VAR_STRING;
5587 args[1].vval.v_string = (char_u *)"";
5588 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005589 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005590 curwin_save = curwin;
5591 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005592 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005593 if (curwin_save != curwin || curbuf_save != curbuf)
5594 {
5595 EMSG(_(e_complwin));
5596 return FAIL;
5597 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005598 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005599 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005600 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005601 {
5602 EMSG(_(e_compldel));
5603 return FAIL;
5604 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005605
Bram Moolenaar6110a002012-01-26 18:58:38 +01005606 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005607 * cancel the complete without an error.
5608 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005609 if (col == -2)
5610 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005611 if (col == -3)
5612 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005613 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005614 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005615 if (!shortmess(SHM_COMPLETIONMENU))
5616 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005617 return FAIL;
5618 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005619
Bram Moolenaar82139082011-09-14 16:52:09 +02005620 /*
5621 * Reset extended parameters of completion, when start new
5622 * completion.
5623 */
5624 compl_opt_refresh_always = FALSE;
5625
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005626 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005627 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005628 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005629 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005630 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005631
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005632 /* Setup variables for completion. Need to obtain "line" again,
5633 * it may have become invalid. */
5634 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005635 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005636 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5637 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005638#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005639 return FAIL;
5640 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005641 else if (ctrl_x_mode == CTRL_X_SPELL)
5642 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005643#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005644 if (spell_bad_len > 0)
5645 compl_col = curs_col - spell_bad_len;
5646 else
5647 compl_col = spell_word_start(startcol);
5648 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005649 {
5650 compl_length = 0;
5651 compl_col = curs_col;
5652 }
5653 else
5654 {
5655 spell_expand_check_cap(compl_col);
5656 compl_length = (int)curs_col - compl_col;
5657 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005658 /* Need to obtain "line" again, it may have become invalid. */
5659 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005660 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5661 if (compl_pattern == NULL)
5662#endif
5663 return FAIL;
5664 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005665 else
5666 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005667 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005668 return FAIL;
5669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005671 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
5673 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005674 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
5676 /* Insert a new line, keep indentation but ignore 'comments' */
5677#ifdef FEAT_COMMENTS
5678 char_u *old = curbuf->b_p_com;
5679
5680 curbuf->b_p_com = (char_u *)"";
5681#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005682 compl_startpos.lnum = curwin->w_cursor.lnum;
5683 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 ins_eol('\r');
5685#ifdef FEAT_COMMENTS
5686 curbuf->b_p_com = old;
5687#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005688 compl_length = 0;
5689 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 }
5692 else
5693 {
5694 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005695 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005698 if (compl_cont_status & CONT_LOCAL)
5699 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 else
5701 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5702
Bram Moolenaar62951b12011-09-21 18:23:05 +02005703 /* If any of the original typed text has been changed we need to fix
5704 * the redo buffer. */
5705 ins_compl_fixRedoBufForLeader(NULL);
5706
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005707 /* Always add completion for the original text. */
5708 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005709 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5710 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005711 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005713 VIM_CLEAR(compl_pattern);
5714 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 return FAIL;
5716 }
5717
5718 /* showmode might reset the internal line pointers, so it must
5719 * be called before line = ml_get(), or when this address is no
5720 * longer needed. -- Acevedo.
5721 */
5722 edit_submode_extra = (char_u *)_("-- Searching...");
5723 edit_submode_highl = HLF_COUNT;
5724 showmode();
5725 edit_submode_extra = NULL;
5726 out_flush();
5727 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005728 else if (insert_match && stop_arrow() == FAIL)
5729 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005731 compl_shown_match = compl_curr_match;
5732 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005735 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005737 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005738 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005739 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005741 /* may undisplay the popup menu */
5742 ins_compl_upd_pum();
5743
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005744 if (n > 1) /* all matches have been found */
5745 compl_matches = n;
5746 compl_curr_match = compl_shown_match;
5747 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
Bram Moolenaard68071d2006-05-02 22:08:30 +00005749 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5750 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 if (got_int && !global_busy)
5752 {
5753 (void)vgetc();
5754 got_int = FALSE;
5755 }
5756
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005757 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005758 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005760 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5761 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5763 edit_submode_highl = HLF_E;
5764 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5765 * because we couldn't expand anything at first place, but if we used
5766 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5767 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005768 if ( compl_length > 1
5769 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005770 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5772 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005773 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 }
5775
Bram Moolenaar572cb562005-08-05 21:35:02 +00005776 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005777 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005779 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781 if (edit_submode_extra == NULL)
5782 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005783 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
5785 edit_submode_extra = (char_u *)_("Back at original");
5786 edit_submode_highl = HLF_W;
5787 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005788 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 edit_submode_extra = (char_u *)_("Word from other line");
5791 edit_submode_highl = HLF_COUNT;
5792 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005793 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 edit_submode_extra = (char_u *)_("The only match");
5796 edit_submode_highl = HLF_COUNT;
5797 }
5798 else
5799 {
5800 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005801 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005803 int number = 0;
5804 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005806 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
5808 /* search backwards for the first valid (!= -1) number.
5809 * This should normally succeed already at the first loop
5810 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005811 for (match = compl_curr_match->cp_prev; match != NULL
5812 && match != compl_first_match;
5813 match = match->cp_prev)
5814 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005816 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818 }
5819 if (match != NULL)
5820 /* go up and assign all numbers which are not assigned
5821 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005822 for (match = match->cp_next;
5823 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005824 match = match->cp_next)
5825 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 }
5827 else /* BACKWARD */
5828 {
5829 /* search forwards (upwards) for the first valid (!= -1)
5830 * number. This should normally succeed already at the
5831 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005832 for (match = compl_curr_match->cp_next; match != NULL
5833 && match != compl_first_match;
5834 match = match->cp_next)
5835 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005837 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 break;
5839 }
5840 if (match != NULL)
5841 /* go down and assign all numbers which are not
5842 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005843 for (match = match->cp_prev; match
5844 && match->cp_number == -1;
5845 match = match->cp_prev)
5846 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 }
5848 }
5849
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005850 /* The match should always have a sequence number now, this is
5851 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005852 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005854 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5855 * Translations may need more than twice that. */
5856 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005858 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005859 vim_snprintf((char *)match_ref, sizeof(match_ref),
5860 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005861 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005863 vim_snprintf((char *)match_ref, sizeof(match_ref),
5864 _("match %d"),
5865 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 edit_submode_extra = match_ref;
5867 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005868 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 curs_columns(FALSE);
5870 }
5871 }
5872 }
5873
5874 /* Show a message about what (completion) mode we're in. */
5875 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005876 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005878 if (edit_submode_extra != NULL)
5879 {
5880 if (!p_smd)
5881 msg_attr(edit_submode_extra,
5882 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005883 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005884 }
5885 else
5886 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
Bram Moolenaard68071d2006-05-02 22:08:30 +00005889 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005890 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005891 show_pum(save_w_wrow, save_w_leftcol);
5892
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005893 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005894 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005895
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 return OK;
5897}
5898
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005899 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005900show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005901{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005902 /* RedrawingDisabled may be set when invoked through complete(). */
5903 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005904
Bram Moolenaarcb036422017-03-01 12:29:10 +01005905 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005906
Bram Moolenaarcb036422017-03-01 12:29:10 +01005907 /* If the cursor moved or the display scrolled we need to remove the pum
5908 * first. */
5909 setcursor();
5910 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5911 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005912
Bram Moolenaarcb036422017-03-01 12:29:10 +01005913 ins_compl_show_pum();
5914 setcursor();
5915 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005916}
5917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918/*
5919 * Looks in the first "len" chars. of "src" for search-metachars.
5920 * If dest is not NULL the chars. are copied there quoting (with
5921 * a backslash) the metachars, and dest would be NUL terminated.
5922 * Returns the length (needed) of dest
5923 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005924 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005925quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005927 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005929 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 {
5931 switch (*src)
5932 {
5933 case '.':
5934 case '*':
5935 case '[':
5936 if (ctrl_x_mode == CTRL_X_DICTIONARY
5937 || ctrl_x_mode == CTRL_X_THESAURUS)
5938 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005939 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 case '~':
5941 if (!p_magic) /* quote these only if magic is set */
5942 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005943 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 case '\\':
5945 if (ctrl_x_mode == CTRL_X_DICTIONARY
5946 || ctrl_x_mode == CTRL_X_THESAURUS)
5947 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005948 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 case '^': /* currently it's not needed. */
5950 case '$':
5951 m++;
5952 if (dest != NULL)
5953 *dest++ = '\\';
5954 break;
5955 }
5956 if (dest != NULL)
5957 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005958# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 /* Copy remaining bytes of a multibyte character. */
5960 if (has_mbyte)
5961 {
5962 int i, mb_len;
5963
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005964 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (mb_len > 0 && len >= mb_len)
5966 for (i = 0; i < mb_len; ++i)
5967 {
5968 --len;
5969 ++src;
5970 if (dest != NULL)
5971 *dest++ = *src;
5972 }
5973 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 }
5976 if (dest != NULL)
5977 *dest = NUL;
5978
5979 return m;
5980}
5981#endif /* FEAT_INS_EXPAND */
5982
5983/*
5984 * Next character is interpreted literally.
5985 * A one, two or three digit decimal number is interpreted as its byte value.
5986 * If one or two digits are entered, the next character is given to vungetc().
5987 * For Unicode a character > 255 may be returned.
5988 */
5989 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005990get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 int cc;
5993 int nc;
5994 int i;
5995 int hex = FALSE;
5996 int octal = FALSE;
5997#ifdef FEAT_MBYTE
5998 int unicode = 0;
5999#endif
6000
6001 if (got_int)
6002 return Ctrl_C;
6003
6004#ifdef FEAT_GUI
6005 /*
6006 * In GUI there is no point inserting the internal code for a special key.
6007 * It is more useful to insert the string "<KEY>" instead. This would
6008 * probably be useful in a text window too, but it would not be
6009 * vi-compatible (maybe there should be an option for it?) -- webb
6010 */
6011 if (gui.in_use)
6012 ++allow_keys;
6013#endif
6014#ifdef USE_ON_FLY_SCROLL
6015 dont_scroll = TRUE; /* disallow scrolling here */
6016#endif
6017 ++no_mapping; /* don't map the next key hits */
6018 cc = 0;
6019 i = 0;
6020 for (;;)
6021 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006022 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023#ifdef FEAT_CMDL_INFO
6024 if (!(State & CMDLINE)
6025# ifdef FEAT_MBYTE
6026 && MB_BYTE2LEN_CHECK(nc) == 1
6027# endif
6028 )
6029 add_to_showcmd(nc);
6030#endif
6031 if (nc == 'x' || nc == 'X')
6032 hex = TRUE;
6033 else if (nc == 'o' || nc == 'O')
6034 octal = TRUE;
6035#ifdef FEAT_MBYTE
6036 else if (nc == 'u' || nc == 'U')
6037 unicode = nc;
6038#endif
6039 else
6040 {
6041 if (hex
6042#ifdef FEAT_MBYTE
6043 || unicode != 0
6044#endif
6045 )
6046 {
6047 if (!vim_isxdigit(nc))
6048 break;
6049 cc = cc * 16 + hex2nr(nc);
6050 }
6051 else if (octal)
6052 {
6053 if (nc < '0' || nc > '7')
6054 break;
6055 cc = cc * 8 + nc - '0';
6056 }
6057 else
6058 {
6059 if (!VIM_ISDIGIT(nc))
6060 break;
6061 cc = cc * 10 + nc - '0';
6062 }
6063
6064 ++i;
6065 }
6066
6067 if (cc > 255
6068#ifdef FEAT_MBYTE
6069 && unicode == 0
6070#endif
6071 )
6072 cc = 255; /* limit range to 0-255 */
6073 nc = 0;
6074
6075 if (hex) /* hex: up to two chars */
6076 {
6077 if (i >= 2)
6078 break;
6079 }
6080#ifdef FEAT_MBYTE
6081 else if (unicode) /* Unicode: up to four or eight chars */
6082 {
6083 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6084 break;
6085 }
6086#endif
6087 else if (i >= 3) /* decimal or octal: up to three chars */
6088 break;
6089 }
6090 if (i == 0) /* no number entered */
6091 {
6092 if (nc == K_ZERO) /* NUL is stored as NL */
6093 {
6094 cc = '\n';
6095 nc = 0;
6096 }
6097 else
6098 {
6099 cc = nc;
6100 nc = 0;
6101 }
6102 }
6103
6104 if (cc == 0) /* NUL is stored as NL */
6105 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006106#ifdef FEAT_MBYTE
6107 if (enc_dbcs && (cc & 0xff) == 0)
6108 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6109 second byte will cause trouble! */
6110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112 --no_mapping;
6113#ifdef FEAT_GUI
6114 if (gui.in_use)
6115 --allow_keys;
6116#endif
6117 if (nc)
6118 vungetc(nc);
6119 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6120 return cc;
6121}
6122
6123/*
6124 * Insert character, taking care of special keys and mod_mask
6125 */
6126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006127insert_special(
6128 int c,
6129 int allow_modmask,
6130 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 char_u *p;
6133 int len;
6134
6135 /*
6136 * Special function key, translate into "<Key>". Up to the last '>' is
6137 * inserted with ins_str(), so as not to replace characters in replace
6138 * mode.
6139 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6140 * unless 'allow_modmask' is TRUE.
6141 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006142#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 /* Command-key never produces a normal key */
6144 if (mod_mask & MOD_MASK_CMD)
6145 allow_modmask = TRUE;
6146#endif
6147 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6148 {
6149 p = get_special_key_name(c, mod_mask);
6150 len = (int)STRLEN(p);
6151 c = p[len - 1];
6152 if (len > 2)
6153 {
6154 if (stop_arrow() == FAIL)
6155 return;
6156 p[len - 1] = NUL;
6157 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006158 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 ctrlv = FALSE;
6160 }
6161 }
6162 if (stop_arrow() == OK)
6163 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6164}
6165
6166/*
6167 * Special characters in this context are those that need processing other
6168 * than the simple insertion that can be performed here. This includes ESC
6169 * which terminates the insert, and CR/NL which need special processing to
6170 * open up a new line. This routine tries to optimize insertions performed by
6171 * the "redo", "undo" or "put" commands, so it needs to know when it should
6172 * stop and defer processing to the "normal" mechanism.
6173 * '0' and '^' are special, because they can be followed by CTRL-D.
6174 */
6175#ifdef EBCDIC
6176# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6177#else
6178# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6179#endif
6180
6181#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006182# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006184# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185#endif
6186
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006187/*
6188 * "flags": INSCHAR_FORMAT - force formatting
6189 * INSCHAR_CTRLV - char typed just after CTRL-V
6190 * INSCHAR_NO_FEX - don't use 'formatexpr'
6191 *
6192 * NOTE: passes the flags value straight through to internal_format() which,
6193 * beside INSCHAR_FORMAT (above), is also looking for these:
6194 * INSCHAR_DO_COM - format comments
6195 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006198insertchar(
6199 int c, /* character to insert or NUL */
6200 int flags, /* INSCHAR_FORMAT, etc. */
6201 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 int textwidth;
6204#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006208 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006210 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212
6213 /*
6214 * Try to break the line in two or more pieces when:
6215 * - Always do this if we have been called to do formatting only.
6216 * - Always do this when 'formatoptions' has the 'a' flag and the line
6217 * ends in white space.
6218 * - Otherwise:
6219 * - Don't do this if inserting a blank
6220 * - Don't do this if an existing character is being replaced, unless
6221 * we're in VREPLACE mode.
6222 * - Do this if the cursor is not on the line where insert started
6223 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6224 * before the insert.
6225 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6226 * before 'textwidth'
6227 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006228 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006229 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006230 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 && !((State & REPLACE_FLAG)
6232#ifdef FEAT_VREPLACE
6233 && !(State & VREPLACE_FLAG)
6234#endif
6235 && *ml_get_cursor() != NUL)
6236 && (curwin->w_cursor.lnum != Insstart.lnum
6237 || ((!has_format_option(FO_INS_LONG)
6238 || Insstart_textlen <= (colnr_T)textwidth)
6239 && (!fo_ins_blank
6240 || Insstart_blank_vcol <= (colnr_T)textwidth
6241 ))))))
6242 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243 /* Format with 'formatexpr' when it's set. Use internal formatting
6244 * when 'formatexpr' isn't set or it returns non-zero. */
6245#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006246 int do_internal = TRUE;
6247 colnr_T virtcol = get_nolist_virtcol()
6248 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006249
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006250 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6251 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006252 {
6253 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6254 /* It may be required to save for undo again, e.g. when setline()
6255 * was called. */
6256 ins_need_undo = TRUE;
6257 }
6258 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006260 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006262
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 if (c == NUL) /* only formatting was wanted */
6264 return;
6265
6266#ifdef FEAT_COMMENTS
6267 /* Check whether this character should end a comment. */
6268 if (did_ai && (int)c == end_comment_pending)
6269 {
6270 char_u *line;
6271 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6272 int middle_len, end_len;
6273 int i;
6274
6275 /*
6276 * Need to remove existing (middle) comment leader and insert end
6277 * comment leader. First, check what comment leader we can find.
6278 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006279 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6281 {
6282 /* Skip middle-comment string */
6283 while (*p && p[-1] != ':') /* find end of middle flags */
6284 ++p;
6285 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6286 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006287 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 --middle_len;
6289
6290 /* Find the end-comment string */
6291 while (*p && p[-1] != ':') /* find end of end flags */
6292 ++p;
6293 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6294
6295 /* Skip white space before the cursor */
6296 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006297 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 ;
6299 i++;
6300
6301 /* Skip to before the middle leader */
6302 i -= middle_len;
6303
6304 /* Check some expected things before we go on */
6305 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6306 {
6307 /* Backspace over all the stuff we want to replace */
6308 backspace_until_column(i);
6309
6310 /*
6311 * Insert the end-comment string, except for the last
6312 * character, which will get inserted as normal later.
6313 */
6314 ins_bytes_len(lead_end, end_len - 1);
6315 }
6316 }
6317 }
6318 end_comment_pending = NUL;
6319#endif
6320
6321 did_ai = FALSE;
6322#ifdef FEAT_SMARTINDENT
6323 did_si = FALSE;
6324 can_si = FALSE;
6325 can_si_back = FALSE;
6326#endif
6327
6328 /*
6329 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6330 * This speeds up normal text input considerably.
6331 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6332 * need to re-indent at a ':', or any other character (but not what
6333 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006334 * Don't do this when there an InsertCharPre autocommand is defined,
6335 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006336 * Do the check for InsertCharPre before the call to vpeekc() because the
6337 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 */
6339#ifdef USE_ON_FLY_SCROLL
6340 dont_scroll = FALSE; /* allow scrolling here */
6341#endif
6342
6343 if ( !ISSPECIAL(c)
6344#ifdef FEAT_MBYTE
6345 && (!has_mbyte || (*mb_char2len)(c) == 1)
6346#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006347 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 && vpeekc() != NUL
6349 && !(State & REPLACE_FLAG)
6350#ifdef FEAT_CINDENT
6351 && !cindent_on()
6352#endif
6353#ifdef FEAT_RIGHTLEFT
6354 && !p_ri
6355#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006356 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 {
6358#define INPUT_BUFLEN 100
6359 char_u buf[INPUT_BUFLEN + 1];
6360 int i;
6361 colnr_T virtcol = 0;
6362
6363 buf[0] = c;
6364 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006365 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 virtcol = get_nolist_virtcol();
6367 /*
6368 * Stop the string when:
6369 * - no more chars available
6370 * - finding a special character (command key)
6371 * - buffer is full
6372 * - running into the 'textwidth' boundary
6373 * - need to check for abbreviation: A non-word char after a word-char
6374 */
6375 while ( (c = vpeekc()) != NUL
6376 && !ISSPECIAL(c)
6377#ifdef FEAT_MBYTE
6378 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6379#endif
6380 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006381# ifdef FEAT_FKMAP
6382 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 && (textwidth == 0
6385 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6386 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6387 {
6388#ifdef FEAT_RIGHTLEFT
6389 c = vgetc();
6390 if (p_hkmap && KeyTyped)
6391 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 buf[i++] = c;
6393#else
6394 buf[i++] = vgetc();
6395#endif
6396 }
6397
6398#ifdef FEAT_DIGRAPHS
6399 do_digraph(-1); /* clear digraphs */
6400 do_digraph(buf[i-1]); /* may be the start of a digraph */
6401#endif
6402 buf[i] = NUL;
6403 ins_str(buf);
6404 if (flags & INSCHAR_CTRLV)
6405 {
6406 redo_literal(*buf);
6407 i = 1;
6408 }
6409 else
6410 i = 0;
6411 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006412 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 }
6414 else
6415 {
6416#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006417 int cc;
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6420 {
6421 char_u buf[MB_MAXBYTES + 1];
6422
6423 (*mb_char2bytes)(c, buf);
6424 buf[cc] = NUL;
6425 ins_char_bytes(buf, cc);
6426 AppendCharToRedobuff(c);
6427 }
6428 else
6429#endif
6430 {
6431 ins_char(c);
6432 if (flags & INSCHAR_CTRLV)
6433 redo_literal(c);
6434 else
6435 AppendCharToRedobuff(c);
6436 }
6437 }
6438}
6439
6440/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006441 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006442 *
6443 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6444 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006445 */
6446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006447internal_format(
6448 int textwidth,
6449 int second_indent,
6450 int flags,
6451 int format_only,
6452 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453{
6454 int cc;
6455 int save_char = NUL;
6456 int haveto_redraw = FALSE;
6457 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6458#ifdef FEAT_MBYTE
6459 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6460#endif
6461 int fo_white_par = has_format_option(FO_WHITE_PAR);
6462 int first_line = TRUE;
6463#ifdef FEAT_COMMENTS
6464 colnr_T leader_len;
6465 int no_leader = FALSE;
6466 int do_comments = (flags & INSCHAR_DO_COM);
6467#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006468#ifdef FEAT_LINEBREAK
6469 int has_lbr = curwin->w_p_lbr;
6470
6471 /* make sure win_lbr_chartabsize() counts correctly */
6472 curwin->w_p_lbr = FALSE;
6473#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006474
6475 /*
6476 * When 'ai' is off we don't want a space under the cursor to be
6477 * deleted. Replace it with an 'x' temporarily.
6478 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006479 if (!curbuf->b_p_ai
6480#ifdef FEAT_VREPLACE
6481 && !(State & VREPLACE_FLAG)
6482#endif
6483 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006484 {
6485 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006486 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006487 {
6488 save_char = cc;
6489 pchar_cursor('x');
6490 }
6491 }
6492
6493 /*
6494 * Repeat breaking lines, until the current line is not too long.
6495 */
6496 while (!got_int)
6497 {
6498 int startcol; /* Cursor column at entry */
6499 int wantcol; /* column at textwidth border */
6500 int foundcol; /* column for start of spaces */
6501 int end_foundcol = 0; /* column for start of word */
6502 colnr_T len;
6503 colnr_T virtcol;
6504#ifdef FEAT_VREPLACE
6505 int orig_col = 0;
6506 char_u *saved_text = NULL;
6507#endif
6508 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006509 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006510
Bram Moolenaar97b98102009-11-17 16:41:01 +00006511 virtcol = get_nolist_virtcol()
6512 + char2cells(c != NUL ? c : gchar_cursor());
6513 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514 break;
6515
6516#ifdef FEAT_COMMENTS
6517 if (no_leader)
6518 do_comments = FALSE;
6519 else if (!(flags & INSCHAR_FORMAT)
6520 && has_format_option(FO_WRAP_COMS))
6521 do_comments = TRUE;
6522
6523 /* Don't break until after the comment leader */
6524 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006525 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006526 else
6527 leader_len = 0;
6528
6529 /* If the line doesn't start with a comment leader, then don't
6530 * start one in a following broken line. Avoids that a %word
6531 * moved to the start of the next line causes all following lines
6532 * to start with %. */
6533 if (leader_len == 0)
6534 no_leader = TRUE;
6535#endif
6536 if (!(flags & INSCHAR_FORMAT)
6537#ifdef FEAT_COMMENTS
6538 && leader_len == 0
6539#endif
6540 && !has_format_option(FO_WRAP))
6541
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006543 if ((startcol = curwin->w_cursor.col) == 0)
6544 break;
6545
6546 /* find column of textwidth border */
6547 coladvance((colnr_T)textwidth);
6548 wantcol = curwin->w_cursor.col;
6549
Bram Moolenaar97b98102009-11-17 16:41:01 +00006550 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006551 foundcol = 0;
6552
6553 /*
6554 * Find position to break at.
6555 * Stop at first entered white when 'formatoptions' has 'v'
6556 */
6557 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006558 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006559 || curwin->w_cursor.lnum != Insstart.lnum
6560 || curwin->w_cursor.col >= Insstart.col)
6561 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006562 if (curwin->w_cursor.col == startcol && c != NUL)
6563 cc = c;
6564 else
6565 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006566 if (WHITECHAR(cc))
6567 {
6568 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006569 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006570
6571 /* find start of sequence of blanks */
6572 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6573 {
6574 dec_cursor();
6575 cc = gchar_cursor();
6576 }
6577 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6578 break; /* only spaces in front of text */
6579#ifdef FEAT_COMMENTS
6580 /* Don't break until after the comment leader */
6581 if (curwin->w_cursor.col < leader_len)
6582 break;
6583#endif
6584 if (has_format_option(FO_ONE_LETTER))
6585 {
6586 /* do not break after one-letter words */
6587 if (curwin->w_cursor.col == 0)
6588 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006589#ifdef FEAT_COMMENTS
6590 /* do not break "#a b" when 'tw' is 2 */
6591 if (curwin->w_cursor.col <= leader_len)
6592 break;
6593#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006594 col = curwin->w_cursor.col;
6595 dec_cursor();
6596 cc = gchar_cursor();
6597
6598 if (WHITECHAR(cc))
6599 continue; /* one-letter, continue */
6600 curwin->w_cursor.col = col;
6601 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006602
6603 inc_cursor();
6604
6605 end_foundcol = end_col + 1;
6606 foundcol = curwin->w_cursor.col;
6607 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006608 break;
6609 }
6610#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006611 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006612 {
6613 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006614 if (curwin->w_cursor.col != startcol)
6615 {
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 col = curwin->w_cursor.col;
6622 inc_cursor();
6623 /* Don't change end_foundcol if already set. */
6624 if (foundcol != curwin->w_cursor.col)
6625 {
6626 foundcol = curwin->w_cursor.col;
6627 end_foundcol = foundcol;
6628 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6629 break;
6630 }
6631 curwin->w_cursor.col = col;
6632 }
6633
6634 if (curwin->w_cursor.col == 0)
6635 break;
6636
6637 col = curwin->w_cursor.col;
6638
6639 dec_cursor();
6640 cc = gchar_cursor();
6641
6642 if (WHITECHAR(cc))
6643 continue; /* break with space */
6644#ifdef FEAT_COMMENTS
6645 /* Don't break until after the comment leader */
6646 if (curwin->w_cursor.col < leader_len)
6647 break;
6648#endif
6649
6650 curwin->w_cursor.col = col;
6651
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006652 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006653 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006654 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6655 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006656 }
6657#endif
6658 if (curwin->w_cursor.col == 0)
6659 break;
6660 dec_cursor();
6661 }
6662
6663 if (foundcol == 0) /* no spaces, cannot break line */
6664 {
6665 curwin->w_cursor.col = startcol;
6666 break;
6667 }
6668
6669 /* Going to break the line, remove any "$" now. */
6670 undisplay_dollar();
6671
6672 /*
6673 * Offset between cursor position and line break is used by replace
6674 * stack functions. VREPLACE does not use this, and backspaces
6675 * over the text instead.
6676 */
6677#ifdef FEAT_VREPLACE
6678 if (State & VREPLACE_FLAG)
6679 orig_col = startcol; /* Will start backspacing from here */
6680 else
6681#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006682 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006683
6684 /*
6685 * adjust startcol for spaces that will be deleted and
6686 * characters that will remain on top line
6687 */
6688 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006689 while ((cc = gchar_cursor(), WHITECHAR(cc))
6690 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006691 inc_cursor();
6692 startcol -= curwin->w_cursor.col;
6693 if (startcol < 0)
6694 startcol = 0;
6695
6696#ifdef FEAT_VREPLACE
6697 if (State & VREPLACE_FLAG)
6698 {
6699 /*
6700 * In VREPLACE mode, we will backspace over the text to be
6701 * wrapped, so save a copy now to put on the next line.
6702 */
6703 saved_text = vim_strsave(ml_get_cursor());
6704 curwin->w_cursor.col = orig_col;
6705 if (saved_text == NULL)
6706 break; /* Can't do it, out of memory */
6707 saved_text[startcol] = NUL;
6708
6709 /* Backspace over characters that will move to the next line */
6710 if (!fo_white_par)
6711 backspace_until_column(foundcol);
6712 }
6713 else
6714#endif
6715 {
6716 /* put cursor after pos. to break line */
6717 if (!fo_white_par)
6718 curwin->w_cursor.col = foundcol;
6719 }
6720
6721 /*
6722 * Split the line just before the margin.
6723 * Only insert/delete lines, but don't really redraw the window.
6724 */
6725 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6726 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6727#ifdef FEAT_COMMENTS
6728 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006729 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006730#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006731 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6732 if (!(flags & INSCHAR_COM_LIST))
6733 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006734
6735 replace_offset = 0;
6736 if (first_line)
6737 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006738 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006739 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006740 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006741 * This section is for auto-wrap of numeric lists. When not
6742 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6743 * flag will be set and open_line() will handle it (as seen
6744 * above). The code here (and in get_number_indent()) will
6745 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006746 */
6747 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006748 second_indent =
6749 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006750 if (second_indent >= 0)
6751 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006752#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006753 if (State & VREPLACE_FLAG)
6754 change_indent(INDENT_SET, second_indent,
6755 FALSE, NUL, TRUE);
6756 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006757#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006758#ifdef FEAT_COMMENTS
6759 if (leader_len > 0 && second_indent - leader_len > 0)
6760 {
6761 int i;
6762 int padding = second_indent - leader_len;
6763
6764 /* We started at the first_line of a numbered list
6765 * that has a comment. the open_line() function has
6766 * inserted the proper comment leader and positioned
6767 * the cursor at the end of the split line. Now we
6768 * add the additional whitespace needed after the
6769 * comment leader for the numbered list. */
6770 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006771 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006772 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006773 }
6774 else
6775 {
6776#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006777 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006778#ifdef FEAT_COMMENTS
6779 }
6780#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006781 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006782 }
6783 first_line = FALSE;
6784 }
6785
6786#ifdef FEAT_VREPLACE
6787 if (State & VREPLACE_FLAG)
6788 {
6789 /*
6790 * In VREPLACE mode we have backspaced over the text to be
6791 * moved, now we re-insert it into the new line.
6792 */
6793 ins_bytes(saved_text);
6794 vim_free(saved_text);
6795 }
6796 else
6797#endif
6798 {
6799 /*
6800 * Check if cursor is not past the NUL off the line, cindent
6801 * may have added or removed indent.
6802 */
6803 curwin->w_cursor.col += startcol;
6804 len = (colnr_T)STRLEN(ml_get_curline());
6805 if (curwin->w_cursor.col > len)
6806 curwin->w_cursor.col = len;
6807 }
6808
6809 haveto_redraw = TRUE;
6810#ifdef FEAT_CINDENT
6811 can_cindent = TRUE;
6812#endif
6813 /* moved the cursor, don't autoindent or cindent now */
6814 did_ai = FALSE;
6815#ifdef FEAT_SMARTINDENT
6816 did_si = FALSE;
6817 can_si = FALSE;
6818 can_si_back = FALSE;
6819#endif
6820 line_breakcheck();
6821 }
6822
6823 if (save_char != NUL) /* put back space after cursor */
6824 pchar_cursor(save_char);
6825
Bram Moolenaar0026d472014-09-09 16:32:39 +02006826#ifdef FEAT_LINEBREAK
6827 curwin->w_p_lbr = has_lbr;
6828#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006829 if (!format_only && haveto_redraw)
6830 {
6831 update_topline();
6832 redraw_curbuf_later(VALID);
6833 }
6834}
6835
6836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 * Called after inserting or deleting text: When 'formatoptions' includes the
6838 * 'a' flag format from the current line until the end of the paragraph.
6839 * Keep the cursor at the same position relative to the text.
6840 * The caller must have saved the cursor line for undo, following ones will be
6841 * saved here.
6842 */
6843 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006844auto_format(
6845 int trailblank, /* when TRUE also format with trailing blank */
6846 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 pos_T pos;
6849 colnr_T len;
6850 char_u *old;
6851 char_u *new, *pnew;
6852 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006853 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
6855 if (!has_format_option(FO_AUTO))
6856 return;
6857
6858 pos = curwin->w_cursor;
6859 old = ml_get_curline();
6860
6861 /* may remove added space */
6862 check_auto_format(FALSE);
6863
6864 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6865 * user might insert normal text next. Also skip formatting when "1" is
6866 * in 'formatoptions' and there is a single character before the cursor.
6867 * Otherwise the line would be broken and when typing another non-white
6868 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006869 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (*old != NUL && !trailblank && wasatend)
6871 {
6872 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006873 cc = gchar_cursor();
6874 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6875 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006877 cc = gchar_cursor();
6878 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {
6880 curwin->w_cursor = pos;
6881 return;
6882 }
6883 curwin->w_cursor = pos;
6884 }
6885
6886#ifdef FEAT_COMMENTS
6887 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6888 * comments. */
6889 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006890 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 return;
6892#endif
6893
6894 /*
6895 * May start formatting in a previous line, so that after "x" a word is
6896 * moved to the previous line if it fits there now. Only when this is not
6897 * the start of a paragraph.
6898 */
6899 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6900 {
6901 --curwin->w_cursor.lnum;
6902 if (u_save_cursor() == FAIL)
6903 return;
6904 }
6905
6906 /*
6907 * Do the formatting and restore the cursor position. "saved_cursor" will
6908 * be adjusted for the text formatting.
6909 */
6910 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006911 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 curwin->w_cursor = saved_cursor;
6913 saved_cursor.lnum = 0;
6914
6915 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6916 {
6917 /* "cannot happen" */
6918 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6919 coladvance((colnr_T)MAXCOL);
6920 }
6921 else
6922 check_cursor_col();
6923
6924 /* Insert mode: If the cursor is now after the end of the line while it
6925 * previously wasn't, the line was broken. Because of the rule above we
6926 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6927 * formatted. */
6928 if (!wasatend && has_format_option(FO_WHITE_PAR))
6929 {
6930 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006931 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 if (curwin->w_cursor.col == len)
6933 {
6934 pnew = vim_strnsave(new, len + 2);
6935 pnew[len] = ' ';
6936 pnew[len + 1] = NUL;
6937 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6938 /* remove the space later */
6939 did_add_space = TRUE;
6940 }
6941 else
6942 /* may remove added space */
6943 check_auto_format(FALSE);
6944 }
6945
6946 check_cursor();
6947}
6948
6949/*
6950 * When an extra space was added to continue a paragraph for auto-formatting,
6951 * delete it now. The space must be under the cursor, just after the insert
6952 * position.
6953 */
6954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006955check_auto_format(
6956 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
6958 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006959 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
6961 if (did_add_space)
6962 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006963 cc = gchar_cursor();
6964 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 /* Somehow the space was removed already. */
6966 did_add_space = FALSE;
6967 else
6968 {
6969 if (!end_insert)
6970 {
6971 inc_cursor();
6972 c = gchar_cursor();
6973 dec_cursor();
6974 }
6975 if (c != NUL)
6976 {
6977 /* The space is no longer at the end of the line, delete it. */
6978 del_char(FALSE);
6979 did_add_space = FALSE;
6980 }
6981 }
6982 }
6983}
6984
6985/*
6986 * Find out textwidth to be used for formatting:
6987 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006988 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 * if invalid value, use 0.
6990 * Set default to window width (maximum 79) for "gq" operator.
6991 */
6992 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006993comp_textwidth(
6994 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 int textwidth;
6997
6998 textwidth = curbuf->b_p_tw;
6999 if (textwidth == 0 && curbuf->b_p_wm)
7000 {
7001 /* The width is the window width minus 'wrapmargin' minus all the
7002 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02007003 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004#ifdef FEAT_CMDWIN
7005 if (cmdwin_type != 0)
7006 textwidth -= 1;
7007#endif
7008#ifdef FEAT_FOLDING
7009 textwidth -= curwin->w_p_fdc;
7010#endif
7011#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007012 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 textwidth -= 1;
7014#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02007015 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 textwidth -= 8;
7017 }
7018 if (textwidth < 0)
7019 textwidth = 0;
7020 if (ff && textwidth == 0)
7021 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007022 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 if (textwidth > 79)
7024 textwidth = 79;
7025 }
7026 return textwidth;
7027}
7028
7029/*
7030 * Put a character in the redo buffer, for when just after a CTRL-V.
7031 */
7032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007033redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 char_u buf[10];
7036
7037 /* Only digits need special treatment. Translate them into a string of
7038 * three digits. */
7039 if (VIM_ISDIGIT(c))
7040 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007041 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 AppendToRedobuff(buf);
7043 }
7044 else
7045 AppendCharToRedobuff(c);
7046}
7047
7048/*
7049 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007050 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 */
7052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007053start_arrow(
7054 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007056 start_arrow_common(end_insert_pos, TRUE);
7057}
7058
7059/*
7060 * Like start_arrow() but with end_change argument.
7061 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7062 */
7063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007064start_arrow_with_change(
7065 pos_T *end_insert_pos, /* can be NULL */
7066 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007067{
7068 start_arrow_common(end_insert_pos, end_change);
7069 if (!end_change)
7070 {
7071 AppendCharToRedobuff(Ctrl_G);
7072 AppendCharToRedobuff('U');
7073 }
7074}
7075
7076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007077start_arrow_common(
7078 pos_T *end_insert_pos, /* can be NULL */
7079 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007080{
7081 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {
7083 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007084 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 arrow_used = TRUE; /* this means we stopped the current insert */
7086 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007087#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007088 check_spell_redraw();
7089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090}
7091
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007092#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007093/*
7094 * If we skipped highlighting word at cursor, do it now.
7095 * It may be skipped again, thus reset spell_redraw_lnum first.
7096 */
7097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007098check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007099{
7100 if (spell_redraw_lnum != 0)
7101 {
7102 linenr_T lnum = spell_redraw_lnum;
7103
7104 spell_redraw_lnum = 0;
7105 redrawWinline(lnum, FALSE);
7106 }
7107}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007108
7109/*
7110 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7111 * spelled word, if there is one.
7112 */
7113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007114spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007115{
7116 pos_T tpos = curwin->w_cursor;
7117
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007118 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007119 if (curwin->w_cursor.col != tpos.col)
7120 start_arrow(&tpos);
7121}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007122#endif
7123
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124/*
7125 * stop_arrow() is called before a change is made in insert mode.
7126 * If an arrow key has been used, start a new insertion.
7127 * Returns FAIL if undo is impossible, shouldn't insert then.
7128 */
7129 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007130stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 if (arrow_used)
7133 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007134 Insstart = curwin->w_cursor; /* new insertion starts here */
7135 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7136 /* Don't update the original insert position when moved to the
7137 * right, except when nothing was inserted yet. */
7138 update_Insstart_orig = FALSE;
7139 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (u_save_cursor() == OK)
7142 {
7143 arrow_used = FALSE;
7144 ins_need_undo = FALSE;
7145 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 ai_col = 0;
7148#ifdef FEAT_VREPLACE
7149 if (State & VREPLACE_FLAG)
7150 {
7151 orig_line_count = curbuf->b_ml.ml_line_count;
7152 vr_lines_changed = 1;
7153 }
7154#endif
7155 ResetRedobuff();
7156 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007157 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
7159 else if (ins_need_undo)
7160 {
7161 if (u_save_cursor() == OK)
7162 ins_need_undo = FALSE;
7163 }
7164
7165#ifdef FEAT_FOLDING
7166 /* Always open fold at the cursor line when inserting something. */
7167 foldOpenCursor();
7168#endif
7169
7170 return (arrow_used || ins_need_undo ? FAIL : OK);
7171}
7172
7173/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007174 * Do a few things to stop inserting.
7175 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7176 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 */
7178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007179stop_insert(
7180 pos_T *end_insert_pos,
7181 int esc, /* called by ins_esc() */
7182 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007184 int cc;
7185 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
7187 stop_redo_ins();
7188 replace_flush(); /* abandon replace stack */
7189
7190 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007191 * Save the inserted text for later redo with ^@ and CTRL-A.
7192 * Don't do it when "restart_edit" was set and nothing was inserted,
7193 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007195 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007196 if (did_restart_edit == 0 || (ptr != NULL
7197 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007198 {
7199 vim_free(last_insert);
7200 last_insert = ptr;
7201 last_insert_skip = new_insert_skip;
7202 }
7203 else
7204 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007206 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {
7208 /* Auto-format now. It may seem strange to do this when stopping an
7209 * insertion (or moving the cursor), but it's required when appending
7210 * a line and having it end in a space. But only do it when something
7211 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007212 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007214 pos_T tpos = curwin->w_cursor;
7215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 /* When the cursor is at the end of the line after a space the
7217 * formatting will move it to the following word. Avoid that by
7218 * moving the cursor onto the space. */
7219 cc = 'x';
7220 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7221 {
7222 dec_cursor();
7223 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007224 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007225 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227
7228 auto_format(TRUE, FALSE);
7229
Bram Moolenaar1c465442017-03-12 20:10:05 +01007230 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007231 {
7232 if (gchar_cursor() != NUL)
7233 inc_cursor();
7234#ifdef FEAT_VIRTUALEDIT
7235 /* If the cursor is still at the same character, also keep
7236 * the "coladd". */
7237 if (gchar_cursor() == NUL
7238 && curwin->w_cursor.lnum == tpos.lnum
7239 && curwin->w_cursor.col == tpos.col)
7240 curwin->w_cursor.coladd = tpos.coladd;
7241#endif
7242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 }
7244
7245 /* If a space was inserted for auto-formatting, remove it now. */
7246 check_auto_format(TRUE);
7247
7248 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007249 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007250 * Do this when ESC was used or moving the cursor up/down.
7251 * Check for the old position still being valid, just in case the text
7252 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007253 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007254 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7255 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007257 pos_T tpos = curwin->w_cursor;
7258
7259 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007260 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007261 for (;;)
7262 {
7263 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7264 --curwin->w_cursor.col;
7265 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007266 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007267 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007268 if (del_char(TRUE) == FAIL)
7269 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007270 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007271 if (curwin->w_cursor.lnum != tpos.lnum)
7272 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007273 else
7274 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007275 /* reset tpos, could have been invalidated in the loop above */
7276 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007277 tpos.col++;
7278 if (cc != NUL && gchar_pos(&tpos) == NUL)
7279 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 /* <C-S-Right> may have started Visual mode, adjust the position for
7283 * deleted characters. */
7284 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7285 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007286 int len = (int)STRLEN(ml_get_curline());
7287
7288 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007290 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007291#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 }
7295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 }
7297 }
7298 did_ai = FALSE;
7299#ifdef FEAT_SMARTINDENT
7300 did_si = FALSE;
7301 can_si = FALSE;
7302 can_si_back = FALSE;
7303#endif
7304
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007305 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7306 * now in a different buffer. */
7307 if (end_insert_pos != NULL)
7308 {
7309 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007310 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007311 curbuf->b_op_end = *end_insert_pos;
7312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313}
7314
7315/*
7316 * Set the last inserted text to a single character.
7317 * Used for the replace command.
7318 */
7319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007320set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 char_u *s;
7323
7324 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 if (last_insert != NULL)
7327 {
7328 s = last_insert;
7329 /* Use the CTRL-V only when entering a special char */
7330 if (c < ' ' || c == DEL)
7331 *s++ = Ctrl_V;
7332 s = add_char2buf(c, s);
7333 *s++ = ESC;
7334 *s++ = NUL;
7335 last_insert_skip = 0;
7336 }
7337}
7338
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007339#if defined(EXITFREE) || defined(PROTO)
7340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007341free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007342{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007343 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007344# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007345 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007346# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007347}
7348#endif
7349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350/*
7351 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7352 * and CSI. Handle multi-byte characters.
7353 * Returns a pointer to after the added bytes.
7354 */
7355 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007356add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357{
7358#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007359 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 int i;
7361 int len;
7362
7363 len = (*mb_char2bytes)(c, temp);
7364 for (i = 0; i < len; ++i)
7365 {
7366 c = temp[i];
7367#endif
7368 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7369 if (c == K_SPECIAL)
7370 {
7371 *s++ = K_SPECIAL;
7372 *s++ = KS_SPECIAL;
7373 *s++ = KE_FILLER;
7374 }
7375#ifdef FEAT_GUI
7376 else if (c == CSI)
7377 {
7378 *s++ = CSI;
7379 *s++ = KS_EXTRA;
7380 *s++ = (int)KE_CSI;
7381 }
7382#endif
7383 else
7384 *s++ = c;
7385#ifdef FEAT_MBYTE
7386 }
7387#endif
7388 return s;
7389}
7390
7391/*
7392 * move cursor to start of line
7393 * if flags & BL_WHITE move to first non-white
7394 * if flags & BL_SOL move to first non-white if startofline is set,
7395 * otherwise keep "curswant" column
7396 * if flags & BL_FIX don't leave the cursor on a NUL.
7397 */
7398 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007399beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
7401 if ((flags & BL_SOL) && !p_sol)
7402 coladvance(curwin->w_curswant);
7403 else
7404 {
7405 curwin->w_cursor.col = 0;
7406#ifdef FEAT_VIRTUALEDIT
7407 curwin->w_cursor.coladd = 0;
7408#endif
7409
7410 if (flags & (BL_WHITE | BL_SOL))
7411 {
7412 char_u *ptr;
7413
Bram Moolenaar1c465442017-03-12 20:10:05 +01007414 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7416 ++curwin->w_cursor.col;
7417 }
7418 curwin->w_set_curswant = TRUE;
7419 }
7420}
7421
7422/*
7423 * oneright oneleft cursor_down cursor_up
7424 *
7425 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007426 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 * Return OK when successful, FAIL when we hit a line of file boundary.
7428 */
7429
7430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007431oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432{
7433 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
7436#ifdef FEAT_VIRTUALEDIT
7437 if (virtual_active())
7438 {
7439 pos_T prevpos = curwin->w_cursor;
7440
7441 /* Adjust for multi-wide char (excluding TAB) */
7442 ptr = ml_get_cursor();
7443 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007444# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007446# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 ))
7450 ? ptr2cells(ptr) : 1));
7451 curwin->w_set_curswant = TRUE;
7452 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7453 return (prevpos.col != curwin->w_cursor.col
7454 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7455 }
7456#endif
7457
7458 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007459 if (*ptr == NUL)
7460 return FAIL; /* already at the very end */
7461
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007463 if (has_mbyte)
7464 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 else
7466#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007467 l = 1;
7468
7469 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7470 * contains "onemore". */
7471 if (ptr[l] == NUL
7472#ifdef FEAT_VIRTUALEDIT
7473 && (ve_flags & VE_ONEMORE) == 0
7474#endif
7475 )
7476 return FAIL;
7477 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
7479 curwin->w_set_curswant = TRUE;
7480 return OK;
7481}
7482
7483 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007484oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486#ifdef FEAT_VIRTUALEDIT
7487 if (virtual_active())
7488 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007489# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 int v = getviscol();
7493
7494 if (v == 0)
7495 return FAIL;
7496
7497# ifdef FEAT_LINEBREAK
7498 /* We might get stuck on 'showbreak', skip over it. */
7499 width = 1;
7500 for (;;)
7501 {
7502 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007503 /* getviscol() is slow, skip it when 'showbreak' is empty,
7504 * 'breakindent' is not set and there are no multi-byte
7505 * characters */
7506 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507# ifdef FEAT_MBYTE
7508 && !has_mbyte
7509# endif
7510 ) || getviscol() < v)
7511 break;
7512 ++width;
7513 }
7514# else
7515 coladvance(v - 1);
7516# endif
7517
7518 if (curwin->w_cursor.coladd == 1)
7519 {
7520 char_u *ptr;
7521
7522 /* Adjust for multi-wide char (not a TAB) */
7523 ptr = ml_get_cursor();
7524 if (*ptr != TAB && vim_isprintc(
7525# ifdef FEAT_MBYTE
7526 (*mb_ptr2char)(ptr)
7527# else
7528 *ptr
7529# endif
7530 ) && ptr2cells(ptr) > 1)
7531 curwin->w_cursor.coladd = 0;
7532 }
7533
7534 curwin->w_set_curswant = TRUE;
7535 return OK;
7536 }
7537#endif
7538
7539 if (curwin->w_cursor.col == 0)
7540 return FAIL;
7541
7542 curwin->w_set_curswant = TRUE;
7543 --curwin->w_cursor.col;
7544
7545#ifdef FEAT_MBYTE
7546 /* if the character on the left of the current cursor is a multi-byte
7547 * character, move to its first byte */
7548 if (has_mbyte)
7549 mb_adjust_cursor();
7550#endif
7551 return OK;
7552}
7553
7554 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007555cursor_up(
7556 long n,
7557 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558{
7559 linenr_T lnum;
7560
7561 if (n > 0)
7562 {
7563 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007564 /* This fails if the cursor is already in the first line or the count
7565 * is larger than the line number and '-' is in 'cpoptions' */
7566 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 return FAIL;
7568 if (n >= lnum)
7569 lnum = 1;
7570 else
7571#ifdef FEAT_FOLDING
7572 if (hasAnyFolding(curwin))
7573 {
7574 /*
7575 * Count each sequence of folded lines as one logical line.
7576 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007577 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 (void)hasFolding(lnum, &lnum, NULL);
7579
7580 while (n--)
7581 {
7582 /* move up one line */
7583 --lnum;
7584 if (lnum <= 1)
7585 break;
7586 /* If we entered a fold, move to the beginning, unless in
7587 * Insert mode or when 'foldopen' contains "all": it will open
7588 * in a moment. */
7589 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7590 (void)hasFolding(lnum, &lnum, NULL);
7591 }
7592 if (lnum < 1)
7593 lnum = 1;
7594 }
7595 else
7596#endif
7597 lnum -= n;
7598 curwin->w_cursor.lnum = lnum;
7599 }
7600
7601 /* try to advance to the column we want to be at */
7602 coladvance(curwin->w_curswant);
7603
7604 if (upd_topline)
7605 update_topline(); /* make sure curwin->w_topline is valid */
7606
7607 return OK;
7608}
7609
7610/*
7611 * Cursor down a number of logical lines.
7612 */
7613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007614cursor_down(
7615 long n,
7616 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 linenr_T lnum;
7619
7620 if (n > 0)
7621 {
7622 lnum = curwin->w_cursor.lnum;
7623#ifdef FEAT_FOLDING
7624 /* Move to last line of fold, will fail if it's the end-of-file. */
7625 (void)hasFolding(lnum, NULL, &lnum);
7626#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007627 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007628 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007629 if (lnum >= curbuf->b_ml.ml_line_count
7630 || (lnum + n > curbuf->b_ml.ml_line_count
7631 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 return FAIL;
7633 if (lnum + n >= curbuf->b_ml.ml_line_count)
7634 lnum = curbuf->b_ml.ml_line_count;
7635 else
7636#ifdef FEAT_FOLDING
7637 if (hasAnyFolding(curwin))
7638 {
7639 linenr_T last;
7640
7641 /* count each sequence of folded lines as one logical line */
7642 while (n--)
7643 {
7644 if (hasFolding(lnum, NULL, &last))
7645 lnum = last + 1;
7646 else
7647 ++lnum;
7648 if (lnum >= curbuf->b_ml.ml_line_count)
7649 break;
7650 }
7651 if (lnum > curbuf->b_ml.ml_line_count)
7652 lnum = curbuf->b_ml.ml_line_count;
7653 }
7654 else
7655#endif
7656 lnum += n;
7657 curwin->w_cursor.lnum = lnum;
7658 }
7659
7660 /* try to advance to the column we want to be at */
7661 coladvance(curwin->w_curswant);
7662
7663 if (upd_topline)
7664 update_topline(); /* make sure curwin->w_topline is valid */
7665
7666 return OK;
7667}
7668
7669/*
7670 * Stuff the last inserted text in the read buffer.
7671 * Last_insert actually is a copy of the redo buffer, so we
7672 * first have to remove the command.
7673 */
7674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007675stuff_inserted(
7676 int c, /* Command character to be inserted */
7677 long count, /* Repeat this many times */
7678 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
7680 char_u *esc_ptr;
7681 char_u *ptr;
7682 char_u *last_ptr;
7683 char_u last = NUL;
7684
7685 ptr = get_last_insert();
7686 if (ptr == NULL)
7687 {
7688 EMSG(_(e_noinstext));
7689 return FAIL;
7690 }
7691
7692 /* may want to stuff the command character, to start Insert mode */
7693 if (c != NUL)
7694 stuffcharReadbuff(c);
7695 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7696 *esc_ptr = NUL; /* remove the ESC */
7697
7698 /* when the last char is either "0" or "^" it will be quoted if no ESC
7699 * comes after it OR if it will inserted more than once and "ptr"
7700 * starts with ^D. -- Acevedo
7701 */
7702 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7703 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7704 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7705 {
7706 last = *last_ptr;
7707 *last_ptr = NUL;
7708 }
7709
7710 do
7711 {
7712 stuffReadbuff(ptr);
7713 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7714 if (last)
7715 stuffReadbuff((char_u *)(last == '0'
7716 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7717 : IF_EB("\026^", CTRL_V_STR "^")));
7718 }
7719 while (--count > 0);
7720
7721 if (last)
7722 *last_ptr = last;
7723
7724 if (esc_ptr != NULL)
7725 *esc_ptr = ESC; /* put the ESC back */
7726
7727 /* may want to stuff a trailing ESC, to get out of Insert mode */
7728 if (!no_esc)
7729 stuffcharReadbuff(ESC);
7730
7731 return OK;
7732}
7733
7734 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007735get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 if (last_insert == NULL)
7738 return NULL;
7739 return last_insert + last_insert_skip;
7740}
7741
7742/*
7743 * Get last inserted string, and remove trailing <Esc>.
7744 * Returns pointer to allocated memory (must be freed) or NULL.
7745 */
7746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007747get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
7749 char_u *s;
7750 int len;
7751
7752 if (last_insert == NULL)
7753 return NULL;
7754 s = vim_strsave(last_insert + last_insert_skip);
7755 if (s != NULL)
7756 {
7757 len = (int)STRLEN(s);
7758 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7759 s[len - 1] = NUL;
7760 }
7761 return s;
7762}
7763
7764/*
7765 * Check the word in front of the cursor for an abbreviation.
7766 * Called when the non-id character "c" has been entered.
7767 * When an abbreviation is recognized it is removed from the text and
7768 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7769 */
7770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007771echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 /* Don't check for abbreviation in paste mode, when disabled and just
7774 * after moving around with cursor keys. */
7775 if (p_paste || no_abbr || arrow_used)
7776 return FALSE;
7777
7778 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7779 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7780}
7781
7782/*
7783 * replace-stack functions
7784 *
7785 * When replacing characters, the replaced characters are remembered for each
7786 * new character. This is used to re-insert the old text when backspacing.
7787 *
7788 * There is a NUL headed list of characters for each character that is
7789 * currently in the file after the insertion point. When BS is used, one NUL
7790 * headed list is put back for the deleted character.
7791 *
7792 * For a newline, there are two NUL headed lists. One contains the characters
7793 * that the NL replaced. The extra one stores the characters after the cursor
7794 * that were deleted (always white space).
7795 *
7796 * Replace_offset is normally 0, in which case replace_push will add a new
7797 * character at the end of the stack. If replace_offset is not 0, that many
7798 * characters will be left on the stack above the newly inserted character.
7799 */
7800
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007801static char_u *replace_stack = NULL;
7802static long replace_stack_nr = 0; /* next entry in replace stack */
7803static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
7805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007806replace_push(
7807 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808{
7809 char_u *p;
7810
7811 if (replace_stack_nr < replace_offset) /* nothing to do */
7812 return;
7813 if (replace_stack_len <= replace_stack_nr)
7814 {
7815 replace_stack_len += 50;
7816 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7817 if (p == NULL) /* out of memory */
7818 {
7819 replace_stack_len -= 50;
7820 return;
7821 }
7822 if (replace_stack != NULL)
7823 {
7824 mch_memmove(p, replace_stack,
7825 (size_t)(replace_stack_nr * sizeof(char_u)));
7826 vim_free(replace_stack);
7827 }
7828 replace_stack = p;
7829 }
7830 p = replace_stack + replace_stack_nr - replace_offset;
7831 if (replace_offset)
7832 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7833 *p = c;
7834 ++replace_stack_nr;
7835}
7836
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007837#if defined(FEAT_MBYTE) || defined(PROTO)
7838/*
7839 * Push a character onto the replace stack. Handles a multi-byte character in
7840 * reverse byte order, so that the first byte is popped off first.
7841 * Return the number of bytes done (includes composing characters).
7842 */
7843 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007844replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007845{
7846 int l = (*mb_ptr2len)(p);
7847 int j;
7848
7849 for (j = l - 1; j >= 0; --j)
7850 replace_push(p[j]);
7851 return l;
7852}
7853#endif
7854
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855/*
7856 * Pop one item from the replace stack.
7857 * return -1 if stack empty
7858 * return replaced character or NUL otherwise
7859 */
7860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007861replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 if (replace_stack_nr == 0)
7864 return -1;
7865 return (int)replace_stack[--replace_stack_nr];
7866}
7867
7868/*
7869 * Join the top two items on the replace stack. This removes to "off"'th NUL
7870 * encountered.
7871 */
7872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007873replace_join(
7874 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876 int i;
7877
7878 for (i = replace_stack_nr; --i >= 0; )
7879 if (replace_stack[i] == NUL && off-- <= 0)
7880 {
7881 --replace_stack_nr;
7882 mch_memmove(replace_stack + i, replace_stack + i + 1,
7883 (size_t)(replace_stack_nr - i));
7884 return;
7885 }
7886}
7887
7888/*
7889 * Pop bytes from the replace stack until a NUL is found, and insert them
7890 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7891 */
7892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007893replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 int cc;
7896 int oldState = State;
7897
7898 State = NORMAL; /* don't want REPLACE here */
7899 while ((cc = replace_pop()) > 0)
7900 {
7901#ifdef FEAT_MBYTE
7902 mb_replace_pop_ins(cc);
7903#else
7904 ins_char(cc);
7905#endif
7906 dec_cursor();
7907 }
7908 State = oldState;
7909}
7910
7911#ifdef FEAT_MBYTE
7912/*
7913 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7914 * indicates a multi-byte char, pop the other bytes too.
7915 */
7916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007917mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007920 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 int i;
7922 int c;
7923
7924 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7925 {
7926 buf[0] = cc;
7927 for (i = 1; i < n; ++i)
7928 buf[i] = replace_pop();
7929 ins_bytes_len(buf, n);
7930 }
7931 else
7932 ins_char(cc);
7933
7934 if (enc_utf8)
7935 /* Handle composing chars. */
7936 for (;;)
7937 {
7938 c = replace_pop();
7939 if (c == -1) /* stack empty */
7940 break;
7941 if ((n = MB_BYTE2LEN(c)) == 1)
7942 {
7943 /* Not a multi-byte char, put it back. */
7944 replace_push(c);
7945 break;
7946 }
7947 else
7948 {
7949 buf[0] = c;
7950 for (i = 1; i < n; ++i)
7951 buf[i] = replace_pop();
7952 if (utf_iscomposing(utf_ptr2char(buf)))
7953 ins_bytes_len(buf, n);
7954 else
7955 {
7956 /* Not a composing char, put it back. */
7957 for (i = n - 1; i >= 0; --i)
7958 replace_push(buf[i]);
7959 break;
7960 }
7961 }
7962 }
7963}
7964#endif
7965
7966/*
7967 * make the replace stack empty
7968 * (called when exiting replace mode)
7969 */
7970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007971replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007973 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 replace_stack_len = 0;
7975 replace_stack_nr = 0;
7976}
7977
7978/*
7979 * Handle doing a BS for one character.
7980 * cc < 0: replace stack empty, just move cursor
7981 * cc == 0: character was inserted, delete it
7982 * cc > 0: character was replaced, put cc (first byte of original char) back
7983 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007984 * When "limit_col" is >= 0, don't delete before this column. Matters when
7985 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 */
7987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007988replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 int cc;
7991#ifdef FEAT_VREPLACE
7992 int orig_len = 0;
7993 int ins_len;
7994 int orig_vcols = 0;
7995 colnr_T start_vcol;
7996 char_u *p;
7997 int i;
7998 int vcol;
7999#endif
8000
8001 cc = replace_pop();
8002 if (cc > 0)
8003 {
8004#ifdef FEAT_VREPLACE
8005 if (State & VREPLACE_FLAG)
8006 {
8007 /* Get the number of screen cells used by the character we are
8008 * going to delete. */
8009 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
8010 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
8011 }
8012#endif
8013#ifdef FEAT_MBYTE
8014 if (has_mbyte)
8015 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008016 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017# ifdef FEAT_VREPLACE
8018 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008019 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020# endif
8021 replace_push(cc);
8022 }
8023 else
8024#endif
8025 {
8026 pchar_cursor(cc);
8027#ifdef FEAT_VREPLACE
8028 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008029 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030#endif
8031 }
8032 replace_pop_ins();
8033
8034#ifdef FEAT_VREPLACE
8035 if (State & VREPLACE_FLAG)
8036 {
8037 /* Get the number of screen cells used by the inserted characters */
8038 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008039 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 vcol = start_vcol;
8041 for (i = 0; i < ins_len; ++i)
8042 {
8043 vcol += chartabsize(p + i, vcol);
8044#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008045 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046#endif
8047 }
8048 vcol -= start_vcol;
8049
8050 /* Delete spaces that were inserted after the cursor to keep the
8051 * text aligned. */
8052 curwin->w_cursor.col += ins_len;
8053 while (vcol > orig_vcols && gchar_cursor() == ' ')
8054 {
8055 del_char(FALSE);
8056 ++orig_vcols;
8057 }
8058 curwin->w_cursor.col -= ins_len;
8059 }
8060#endif
8061
8062 /* mark the buffer as changed and prepare for displaying */
8063 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8064 }
8065 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008066 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068
8069#ifdef FEAT_CINDENT
8070/*
8071 * Return TRUE if C-indenting is on.
8072 */
8073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008074cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 return (!p_paste && (curbuf->b_p_cin
8077# ifdef FEAT_EVAL
8078 || *curbuf->b_p_inde != NUL
8079# endif
8080 ));
8081}
8082#endif
8083
8084#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8085/*
8086 * Re-indent the current line, based on the current contents of it and the
8087 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8088 * confused what all the part that handles Control-T is doing that I'm not.
8089 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8090 */
8091
8092 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008093fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008095 int amount = get_the_indent();
8096
8097 if (amount >= 0)
8098 {
8099 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8100 if (linewhite(curwin->w_cursor.lnum))
8101 did_ai = TRUE; /* delete the indent if the line stays empty */
8102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103}
8104
8105 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008106fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 if (p_paste)
8109 return;
8110# ifdef FEAT_LISP
8111 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8112 fixthisline(get_lisp_indent);
8113# endif
8114# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8115 else
8116# endif
8117# ifdef FEAT_CINDENT
8118 if (cindent_on())
8119 do_c_expr_indent();
8120# endif
8121}
8122
8123#endif
8124
8125#ifdef FEAT_CINDENT
8126/*
8127 * return TRUE if 'cinkeys' contains the key "keytyped",
8128 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008129 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8131 *
8132 * "keytyped" can have a few special values:
8133 * KEY_OPEN_FORW
8134 * KEY_OPEN_BACK
8135 * KEY_COMPLETE just finished completion.
8136 *
8137 * If line_is_empty is TRUE accept keys with '0' before them.
8138 */
8139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008140in_cinkeys(
8141 int keytyped,
8142 int when,
8143 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
8145 char_u *look;
8146 int try_match;
8147 int try_match_word;
8148 char_u *p;
8149 char_u *line;
8150 int icase;
8151 int i;
8152
Bram Moolenaar30848942009-12-24 14:46:12 +00008153 if (keytyped == NUL)
8154 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8155 return FALSE;
8156
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157#ifdef FEAT_EVAL
8158 if (*curbuf->b_p_inde != NUL)
8159 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8160 else
8161#endif
8162 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8163 while (*look)
8164 {
8165 /*
8166 * Find out if we want to try a match with this key, depending on
8167 * 'when' and a '*' or '!' before the key.
8168 */
8169 switch (when)
8170 {
8171 case '*': try_match = (*look == '*'); break;
8172 case '!': try_match = (*look == '!'); break;
8173 default: try_match = (*look != '*'); break;
8174 }
8175 if (*look == '*' || *look == '!')
8176 ++look;
8177
8178 /*
8179 * If there is a '0', only accept a match if the line is empty.
8180 * But may still match when typing last char of a word.
8181 */
8182 if (*look == '0')
8183 {
8184 try_match_word = try_match;
8185 if (!line_is_empty)
8186 try_match = FALSE;
8187 ++look;
8188 }
8189 else
8190 try_match_word = FALSE;
8191
8192 /*
8193 * does it look like a control character?
8194 */
8195 if (*look == '^'
8196#ifdef EBCDIC
8197 && (Ctrl_chr(look[1]) != 0)
8198#else
8199 && look[1] >= '?' && look[1] <= '_'
8200#endif
8201 )
8202 {
8203 if (try_match && keytyped == Ctrl_chr(look[1]))
8204 return TRUE;
8205 look += 2;
8206 }
8207 /*
8208 * 'o' means "o" command, open forward.
8209 * 'O' means "O" command, open backward.
8210 */
8211 else if (*look == 'o')
8212 {
8213 if (try_match && keytyped == KEY_OPEN_FORW)
8214 return TRUE;
8215 ++look;
8216 }
8217 else if (*look == 'O')
8218 {
8219 if (try_match && keytyped == KEY_OPEN_BACK)
8220 return TRUE;
8221 ++look;
8222 }
8223
8224 /*
8225 * 'e' means to check for "else" at start of line and just before the
8226 * cursor.
8227 */
8228 else if (*look == 'e')
8229 {
8230 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8231 {
8232 p = ml_get_curline();
8233 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8234 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8235 return TRUE;
8236 }
8237 ++look;
8238 }
8239
8240 /*
8241 * ':' only causes an indent if it is at the end of a label or case
8242 * statement, or when it was before typing the ':' (to fix
8243 * class::method for C++).
8244 */
8245 else if (*look == ':')
8246 {
8247 if (try_match && keytyped == ':')
8248 {
8249 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008250 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008252 /* Need to get the line again after cin_islabel(). */
8253 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 if (curwin->w_cursor.col > 2
8255 && p[curwin->w_cursor.col - 1] == ':'
8256 && p[curwin->w_cursor.col - 2] == ':')
8257 {
8258 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008259 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008260 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 p = ml_get_curline();
8262 p[curwin->w_cursor.col - 1] = ':';
8263 if (i)
8264 return TRUE;
8265 }
8266 }
8267 ++look;
8268 }
8269
8270
8271 /*
8272 * Is it a key in <>, maybe?
8273 */
8274 else if (*look == '<')
8275 {
8276 if (try_match)
8277 {
8278 /*
8279 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8280 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8281 * >, *, : and ! keys if they really really want to.
8282 */
8283 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8284 && keytyped == look[1])
8285 return TRUE;
8286
8287 if (keytyped == get_special_key_code(look + 1))
8288 return TRUE;
8289 }
8290 while (*look && *look != '>')
8291 look++;
8292 while (*look == '>')
8293 look++;
8294 }
8295
8296 /*
8297 * Is it a word: "=word"?
8298 */
8299 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8300 {
8301 ++look;
8302 if (*look == '~')
8303 {
8304 icase = TRUE;
8305 ++look;
8306 }
8307 else
8308 icase = FALSE;
8309 p = vim_strchr(look, ',');
8310 if (p == NULL)
8311 p = look + STRLEN(look);
8312 if ((try_match || try_match_word)
8313 && curwin->w_cursor.col >= (colnr_T)(p - look))
8314 {
8315 int match = FALSE;
8316
8317#ifdef FEAT_INS_EXPAND
8318 if (keytyped == KEY_COMPLETE)
8319 {
8320 char_u *s;
8321
8322 /* Just completed a word, check if it starts with "look".
8323 * search back for the start of a word. */
8324 line = ml_get_curline();
8325# ifdef FEAT_MBYTE
8326 if (has_mbyte)
8327 {
8328 char_u *n;
8329
8330 for (s = line + curwin->w_cursor.col; s > line; s = n)
8331 {
8332 n = mb_prevptr(line, s);
8333 if (!vim_iswordp(n))
8334 break;
8335 }
8336 }
8337 else
8338# endif
8339 for (s = line + curwin->w_cursor.col; s > line; --s)
8340 if (!vim_iswordc(s[-1]))
8341 break;
8342 if (s + (p - look) <= line + curwin->w_cursor.col
8343 && (icase
8344 ? MB_STRNICMP(s, look, p - look)
8345 : STRNCMP(s, look, p - look)) == 0)
8346 match = TRUE;
8347 }
8348 else
8349#endif
8350 /* TODO: multi-byte */
8351 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8352 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8353 {
8354 line = ml_get_cursor();
8355 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8356 || !vim_iswordc(line[-(p - look) - 1]))
8357 && (icase
8358 ? MB_STRNICMP(line - (p - look), look, p - look)
8359 : STRNCMP(line - (p - look), look, p - look))
8360 == 0)
8361 match = TRUE;
8362 }
8363 if (match && try_match_word && !try_match)
8364 {
8365 /* "0=word": Check if there are only blanks before the
8366 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008367 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 (int)(curwin->w_cursor.col - (p - look)))
8369 match = FALSE;
8370 }
8371 if (match)
8372 return TRUE;
8373 }
8374 look = p;
8375 }
8376
8377 /*
8378 * ok, it's a boring generic character.
8379 */
8380 else
8381 {
8382 if (try_match && *look == keytyped)
8383 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008384 if (*look != NUL)
8385 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 }
8387
8388 /*
8389 * Skip over ", ".
8390 */
8391 look = skip_to_option_part(look);
8392 }
8393 return FALSE;
8394}
8395#endif /* FEAT_CINDENT */
8396
8397#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8398/*
8399 * Map Hebrew keyboard when in hkmap mode.
8400 */
8401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008402hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8405 {
8406 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8407 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8408 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8409 static char_u map[26] =
8410 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8411 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8412 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8413 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8414 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8415 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8416 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8417 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8418 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8419
8420 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8421 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8422 /* '-1'='sofit' */
8423 else if (c == 'x')
8424 return 'X';
8425 else if (c == 'q')
8426 return '\''; /* {geresh}={'} */
8427 else if (c == 246)
8428 return ' '; /* \"o --> ' ' for a german keyboard */
8429 else if (c == 228)
8430 return ' '; /* \"a --> ' ' -- / -- */
8431 else if (c == 252)
8432 return ' '; /* \"u --> ' ' -- / -- */
8433#ifdef EBCDIC
8434 else if (islower(c))
8435#else
8436 /* NOTE: islower() does not do the right thing for us on Linux so we
8437 * do this the same was as 5.7 and previous, so it works correctly on
8438 * all systems. Specifically, the e.g. Delete and Arrow keys are
8439 * munged and won't work if e.g. searching for Hebrew text.
8440 */
8441 else if (c >= 'a' && c <= 'z')
8442#endif
8443 return (int)(map[CharOrdLow(c)] + p_aleph);
8444 else
8445 return c;
8446 }
8447 else
8448 {
8449 switch (c)
8450 {
8451 case '`': return ';';
8452 case '/': return '.';
8453 case '\'': return ',';
8454 case 'q': return '/';
8455 case 'w': return '\'';
8456
8457 /* Hebrew letters - set offset from 'a' */
8458 case ',': c = '{'; break;
8459 case '.': c = 'v'; break;
8460 case ';': c = 't'; break;
8461 default: {
8462 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8463
8464#ifdef EBCDIC
8465 /* see note about islower() above */
8466 if (!islower(c))
8467#else
8468 if (c < 'a' || c > 'z')
8469#endif
8470 return c;
8471 c = str[CharOrdLow(c)];
8472 break;
8473 }
8474 }
8475
8476 return (int)(CharOrdLow(c) + p_aleph);
8477 }
8478}
8479#endif
8480
8481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008482ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483{
8484 int need_redraw = FALSE;
8485 int regname;
8486 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008487 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488
8489 /*
8490 * If we are going to wait for a character, show a '"'.
8491 */
8492 pc_status = PC_STATUS_UNSET;
8493 if (redrawing() && !char_avail())
8494 {
8495 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008496 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497
8498 edit_putchar('"', TRUE);
8499#ifdef FEAT_CMDL_INFO
8500 add_to_showcmd_c(Ctrl_R);
8501#endif
8502 }
8503
8504#ifdef USE_ON_FLY_SCROLL
8505 dont_scroll = TRUE; /* disallow scrolling here */
8506#endif
8507
8508 /*
8509 * Don't map the register name. This also prevents the mode message to be
8510 * deleted when ESC is hit.
8511 */
8512 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008513 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8516 {
8517 /* Get a third key for literal register insertion */
8518 literally = regname;
8519#ifdef FEAT_CMDL_INFO
8520 add_to_showcmd_c(literally);
8521#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008522 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 }
8525 --no_mapping;
8526
8527#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008528 /* Don't call u_sync() while typing the expression or giving an error
8529 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 ++no_u_sync;
8531 if (regname == '=')
8532 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008533# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008535# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008536 /* Sync undo when evaluating the expression calls setline() or
8537 * append(), so that it can be undone separately. */
8538 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008539
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008541# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 /* Restore the Input Method. */
8543 if (im_on)
8544 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008547 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8548 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008549 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 else
8553 {
8554#endif
8555 if (literally == Ctrl_O || literally == Ctrl_P)
8556 {
8557 /* Append the command to the redo buffer. */
8558 AppendCharToRedobuff(Ctrl_R);
8559 AppendCharToRedobuff(literally);
8560 AppendCharToRedobuff(regname);
8561
8562 do_put(regname, BACKWARD, 1L,
8563 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8564 }
8565 else if (insert_reg(regname, literally) == FAIL)
8566 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008567 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 need_redraw = TRUE; /* remove the '"' */
8569 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008570 else if (stop_insert_mode)
8571 /* When the '=' register was used and a function was invoked that
8572 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8573 * insert anything, need to remove the '"' */
8574 need_redraw = TRUE;
8575
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576#ifdef FEAT_EVAL
8577 }
8578 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008579 if (u_sync_once == 1)
8580 ins_need_undo = TRUE;
8581 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582#endif
8583#ifdef FEAT_CMDL_INFO
8584 clear_showcmd();
8585#endif
8586
8587 /* If the inserted register is empty, we need to remove the '"' */
8588 if (need_redraw || stuff_empty())
8589 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008590
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008591 /* Disallow starting Visual mode here, would get a weird mode. */
8592 if (!vis_active && VIsual_active)
8593 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594}
8595
8596/*
8597 * CTRL-G commands in Insert mode.
8598 */
8599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
8602 int c;
8603
8604#ifdef FEAT_INS_EXPAND
8605 /* Right after CTRL-X the cursor will be after the ruler. */
8606 setcursor();
8607#endif
8608
8609 /*
8610 * Don't map the second key. This also prevents the mode message to be
8611 * deleted when ESC is hit.
8612 */
8613 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008614 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 --no_mapping;
8616 switch (c)
8617 {
8618 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8619 case K_UP:
8620 case Ctrl_K:
8621 case 'k': ins_up(TRUE);
8622 break;
8623
8624 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8625 case K_DOWN:
8626 case Ctrl_J:
8627 case 'j': ins_down(TRUE);
8628 break;
8629
8630 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008631 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008633
8634 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008635 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008636 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008637 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 break;
8639
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008640 /* CTRL-G U: do not break undo with the next char */
8641 case 'U':
8642 /* Allow one left/right cursor movement with the next char,
8643 * without breaking undo. */
8644 dont_sync_undo = MAYBE;
8645 break;
8646
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008648 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 }
8650}
8651
8652/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008653 * CTRL-^ in Insert mode.
8654 */
8655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008656ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008657{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008658 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008659 {
8660 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8661 if (State & LANGMAP)
8662 {
8663 curbuf->b_p_iminsert = B_IMODE_NONE;
8664 State &= ~LANGMAP;
8665 }
8666 else
8667 {
8668 curbuf->b_p_iminsert = B_IMODE_LMAP;
8669 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008670#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008671 im_set_active(FALSE);
8672#endif
8673 }
8674 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008675#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008676 else
8677 {
8678 /* There are no ":lmap" mappings, toggle IM */
8679 if (im_get_status())
8680 {
8681 curbuf->b_p_iminsert = B_IMODE_NONE;
8682 im_set_active(FALSE);
8683 }
8684 else
8685 {
8686 curbuf->b_p_iminsert = B_IMODE_IM;
8687 State &= ~LANGMAP;
8688 im_set_active(TRUE);
8689 }
8690 }
8691#endif
8692 set_iminsert_global();
8693 showmode();
8694#ifdef FEAT_GUI
8695 /* may show different cursor shape or color */
8696 if (gui.in_use)
8697 gui_update_cursor(TRUE, FALSE);
8698#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008699#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008700 /* Show/unshow value of 'keymap' in status lines. */
8701 status_redraw_curbuf();
8702#endif
8703}
8704
8705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 * Handle ESC in insert mode.
8707 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8708 * insert.
8709 */
8710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008711ins_esc(
8712 long *count,
8713 int cmdchar,
8714 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 int temp;
8717 static int disabled_redraw = FALSE;
8718
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008719#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008720 check_spell_redraw();
8721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722#if defined(FEAT_HANGULIN)
8723# if defined(ESC_CHG_TO_ENG_MODE)
8724 hangul_input_state_set(0);
8725# endif
8726 if (composing_hangul)
8727 {
8728 push_raw_key(composing_hangul_buffer, 2);
8729 composing_hangul = 0;
8730 }
8731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732
8733 temp = curwin->w_cursor.col;
8734 if (disabled_redraw)
8735 {
8736 --RedrawingDisabled;
8737 disabled_redraw = FALSE;
8738 }
8739 if (!arrow_used)
8740 {
8741 /*
8742 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008743 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8744 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 */
8746 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008747 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748
8749 /*
8750 * Repeating insert may take a long time. Check for
8751 * interrupt now and then.
8752 */
8753 if (*count > 0)
8754 {
8755 line_breakcheck();
8756 if (got_int)
8757 *count = 0;
8758 }
8759
8760 if (--*count > 0) /* repeat what was typed */
8761 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008762 /* Vi repeats the insert without replacing characters. */
8763 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8764 State &= ~REPLACE_FLAG;
8765
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 (void)start_redo_ins();
8767 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008768 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 ++RedrawingDisabled;
8770 disabled_redraw = TRUE;
8771 return FALSE; /* repeat the insert */
8772 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008773 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 undisplay_dollar();
8775 }
8776
8777 /* When an autoindent was removed, curswant stays after the
8778 * indent */
8779 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8780 curwin->w_set_curswant = TRUE;
8781
8782 /* Remember the last Insert position in the '^ mark. */
8783 if (!cmdmod.keepjumps)
8784 curbuf->b_last_insert = curwin->w_cursor;
8785
8786 /*
8787 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008788 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008790 if (!nomove
8791 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792#ifdef FEAT_VIRTUALEDIT
8793 || curwin->w_cursor.coladd > 0
8794#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008795 )
8796 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008797 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798#ifdef FEAT_RIGHTLEFT
8799 && !revins_on
8800#endif
8801 )
8802 {
8803#ifdef FEAT_VIRTUALEDIT
8804 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8805 {
8806 oneleft();
8807 if (restart_edit != NUL)
8808 ++curwin->w_cursor.coladd;
8809 }
8810 else
8811#endif
8812 {
8813 --curwin->w_cursor.col;
8814#ifdef FEAT_MBYTE
8815 /* Correct cursor for multi-byte character. */
8816 if (has_mbyte)
8817 mb_adjust_cursor();
8818#endif
8819 }
8820 }
8821
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008822#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 /* Disable IM to allow typing English directly for Normal mode commands.
8824 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8825 * well). */
8826 if (!(State & LANGMAP))
8827 im_save_status(&curbuf->b_p_iminsert);
8828 im_set_active(FALSE);
8829#endif
8830
8831 State = NORMAL;
8832 /* need to position cursor again (e.g. when on a TAB ) */
8833 changed_cline_bef_curs();
8834
8835#ifdef FEAT_MOUSE
8836 setmouse();
8837#endif
8838#ifdef CURSOR_SHAPE
8839 ui_cursor_shape(); /* may show different cursor shape */
8840#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008841 if (!p_ek)
8842 /* Re-enable bracketed paste mode. */
8843 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844
8845 /*
8846 * When recording or for CTRL-O, need to display the new mode.
8847 * Otherwise remove the mode message.
8848 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008849 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 showmode();
8851 else if (p_smd)
8852 MSG("");
8853
8854 return TRUE; /* exit Insert mode */
8855}
8856
8857#ifdef FEAT_RIGHTLEFT
8858/*
8859 * Toggle language: hkmap and revins_on.
8860 * Move to end of reverse inserted text.
8861 */
8862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008863ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865 if (revins_on && revins_chars && revins_scol >= 0)
8866 {
8867 while (gchar_cursor() != NUL && revins_chars--)
8868 ++curwin->w_cursor.col;
8869 }
8870 p_ri = !p_ri;
8871 revins_on = (State == INSERT && p_ri);
8872 if (revins_on)
8873 {
8874 revins_scol = curwin->w_cursor.col;
8875 revins_legal++;
8876 revins_chars = 0;
8877 undisplay_dollar();
8878 }
8879 else
8880 revins_scol = -1;
8881#ifdef FEAT_FKMAP
8882 if (p_altkeymap)
8883 {
8884 /*
8885 * to be consistent also for redo command, using '.'
8886 * set arrow_used to true and stop it - causing to redo
8887 * characters entered in one mode (normal/reverse insert).
8888 */
8889 arrow_used = TRUE;
8890 (void)stop_arrow();
8891 p_fkmap = curwin->w_p_rl ^ p_ri;
8892 if (p_fkmap && p_ri)
8893 State = INSERT;
8894 }
8895 else
8896#endif
8897 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8898 showmode();
8899}
8900#endif
8901
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902/*
8903 * If 'keymodel' contains "startsel", may start selection.
8904 * Returns TRUE when a CTRL-O and other keys stuffed.
8905 */
8906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008907ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 if (km_startsel)
8910 switch (c)
8911 {
8912 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 case K_PAGEUP:
8915 case K_KPAGEUP:
8916 case K_PAGEDOWN:
8917 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008918# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 case K_LEFT:
8920 case K_RIGHT:
8921 case K_UP:
8922 case K_DOWN:
8923 case K_END:
8924 case K_HOME:
8925# endif
8926 if (!(mod_mask & MOD_MASK_SHIFT))
8927 break;
8928 /* FALLTHROUGH */
8929 case K_S_LEFT:
8930 case K_S_RIGHT:
8931 case K_S_UP:
8932 case K_S_DOWN:
8933 case K_S_END:
8934 case K_S_HOME:
8935 /* Start selection right away, the cursor can move with
8936 * CTRL-O when beyond the end of the line. */
8937 start_selection();
8938
8939 /* Execute the key in (insert) Select mode. */
8940 stuffcharReadbuff(Ctrl_O);
8941 if (mod_mask)
8942 {
8943 char_u buf[4];
8944
8945 buf[0] = K_SPECIAL;
8946 buf[1] = KS_MODIFIER;
8947 buf[2] = mod_mask;
8948 buf[3] = NUL;
8949 stuffReadbuff(buf);
8950 }
8951 stuffcharReadbuff(c);
8952 return TRUE;
8953 }
8954 return FALSE;
8955}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
8957/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008958 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008959 */
8960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008961ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008962{
8963#ifdef FEAT_FKMAP
8964 if (p_fkmap && p_ri)
8965 {
8966 beep_flush();
8967 EMSG(farsi_text_3); /* encoded in Farsi */
8968 return;
8969 }
8970#endif
8971
Bram Moolenaar1e015462005-09-25 22:16:38 +00008972# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008973 set_vim_var_string(VV_INSERTMODE,
8974 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008975# ifdef FEAT_VREPLACE
8976 replaceState == VREPLACE ? "v" :
8977# endif
8978 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008979# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008980 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008981 if (State & REPLACE_FLAG)
8982 State = INSERT | (State & LANGMAP);
8983 else
8984 State = replaceState | (State & LANGMAP);
8985 AppendCharToRedobuff(K_INS);
8986 showmode();
8987#ifdef CURSOR_SHAPE
8988 ui_cursor_shape(); /* may show different cursor shape */
8989#endif
8990}
8991
8992/*
8993 * Pressed CTRL-O in Insert mode.
8994 */
8995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008996ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008997{
8998#ifdef FEAT_VREPLACE
8999 if (State & VREPLACE_FLAG)
9000 restart_edit = 'V';
9001 else
9002#endif
9003 if (State & REPLACE_FLAG)
9004 restart_edit = 'R';
9005 else
9006 restart_edit = 'I';
9007#ifdef FEAT_VIRTUALEDIT
9008 if (virtual_active())
9009 ins_at_eol = FALSE; /* cursor always keeps its column */
9010 else
9011#endif
9012 ins_at_eol = (gchar_cursor() == NUL);
9013}
9014
9015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 * If the cursor is on an indent, ^T/^D insert/delete one
9017 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00009018 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 * with vi. But vi only supports ^T and ^D after an
9020 * autoindent, we support it everywhere.
9021 */
9022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009023ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
9025 if (stop_arrow() == FAIL)
9026 return;
9027 AppendCharToRedobuff(c);
9028
9029 /*
9030 * 0^D and ^^D: remove all indent.
9031 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009032 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9033 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 {
9035 --curwin->w_cursor.col;
9036 (void)del_char(FALSE); /* delete the '^' or '0' */
9037 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9038 if (State & REPLACE_FLAG)
9039 replace_pop_ins();
9040 if (lastc == '^')
9041 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009042 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009045 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
9047 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9048 did_ai = FALSE;
9049#ifdef FEAT_SMARTINDENT
9050 did_si = FALSE;
9051 can_si = FALSE;
9052 can_si_back = FALSE;
9053#endif
9054#ifdef FEAT_CINDENT
9055 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9056#endif
9057}
9058
9059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009060ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062 int temp;
9063
9064 if (stop_arrow() == FAIL)
9065 return;
9066 if (gchar_cursor() == NUL) /* delete newline */
9067 {
9068 temp = curwin->w_cursor.col;
9069 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009070 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009071 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009075#ifdef FEAT_VREPLACE
9076 /* Adjust orig_line_count in case more lines have been deleted than
9077 * have been added. That makes sure, that open_line() later
9078 * can access all buffer lines correctly */
9079 if (State & VREPLACE_FLAG &&
9080 orig_line_count > curbuf->b_ml.ml_line_count)
9081 orig_line_count = curbuf->b_ml.ml_line_count;
9082#endif
9083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009085 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9086 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 did_ai = FALSE;
9088#ifdef FEAT_SMARTINDENT
9089 did_si = FALSE;
9090 can_si = FALSE;
9091 can_si_back = FALSE;
9092#endif
9093 AppendCharToRedobuff(K_DEL);
9094}
9095
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009096static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009097
9098/*
9099 * Delete one character for ins_bs().
9100 */
9101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009102ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009103{
9104 dec_cursor();
9105 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9106 if (State & REPLACE_FLAG)
9107 {
9108 /* Don't delete characters before the insert point when in
9109 * Replace mode */
9110 if (curwin->w_cursor.lnum != Insstart.lnum
9111 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009112 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009113 }
9114 else
9115 (void)del_char(FALSE);
9116}
9117
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118/*
9119 * Handle Backspace, delete-word and delete-line in Insert mode.
9120 * Return TRUE when backspace was actually used.
9121 */
9122 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009123ins_bs(
9124 int c,
9125 int mode,
9126 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 linenr_T lnum;
9129 int cc;
9130 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009131 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 colnr_T mincol;
9133 int did_backspace = FALSE;
9134 int in_indent;
9135 int oldState;
9136#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009137 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138#endif
9139
9140 /*
9141 * can't delete anything in an empty file
9142 * can't backup past first character in buffer
9143 * can't backup past starting point unless 'backspace' > 1
9144 * can backup to a previous line if 'backspace' == 0
9145 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009146 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 || (
9148#ifdef FEAT_RIGHTLEFT
9149 !revins_on &&
9150#endif
9151 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9152 || (!can_bs(BS_START)
9153 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009154 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9155 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9157 && curwin->w_cursor.col <= ai_col)
9158 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9159 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009160 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 return FALSE;
9162 }
9163
9164 if (stop_arrow() == FAIL)
9165 return FALSE;
9166 in_indent = inindent(0);
9167#ifdef FEAT_CINDENT
9168 if (in_indent)
9169 can_cindent = FALSE;
9170#endif
9171#ifdef FEAT_COMMENTS
9172 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9173#endif
9174#ifdef FEAT_RIGHTLEFT
9175 if (revins_on) /* put cursor after last inserted char */
9176 inc_cursor();
9177#endif
9178
9179#ifdef FEAT_VIRTUALEDIT
9180 /* Virtualedit:
9181 * BACKSPACE_CHAR eats a virtual space
9182 * BACKSPACE_WORD eats all coladd
9183 * BACKSPACE_LINE eats all coladd and keeps going
9184 */
9185 if (curwin->w_cursor.coladd > 0)
9186 {
9187 if (mode == BACKSPACE_CHAR)
9188 {
9189 --curwin->w_cursor.coladd;
9190 return TRUE;
9191 }
9192 if (mode == BACKSPACE_WORD)
9193 {
9194 curwin->w_cursor.coladd = 0;
9195 return TRUE;
9196 }
9197 curwin->w_cursor.coladd = 0;
9198 }
9199#endif
9200
9201 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009202 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 */
9204 if (curwin->w_cursor.col == 0)
9205 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009206 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009207 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208#ifdef FEAT_RIGHTLEFT
9209 || revins_on
9210#endif
9211 )
9212 {
9213 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9214 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9215 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009216 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009217 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 }
9219 /*
9220 * In replace mode:
9221 * cc < 0: NL was inserted, delete it
9222 * cc >= 0: NL was replaced, put original characters back
9223 */
9224 cc = -1;
9225 if (State & REPLACE_FLAG)
9226 cc = replace_pop(); /* returns -1 if NL was inserted */
9227 /*
9228 * In replace mode, in the line we started replacing, we only move the
9229 * cursor.
9230 */
9231 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9232 {
9233 dec_cursor();
9234 }
9235 else
9236 {
9237#ifdef FEAT_VREPLACE
9238 if (!(State & VREPLACE_FLAG)
9239 || curwin->w_cursor.lnum > orig_line_count)
9240#endif
9241 {
9242 temp = gchar_cursor(); /* remember current char */
9243 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009244
9245 /* When "aw" is in 'formatoptions' we must delete the space at
9246 * the end of the line, otherwise the line will be broken
9247 * again when auto-formatting. */
9248 if (has_format_option(FO_AUTO)
9249 && has_format_option(FO_WHITE_PAR))
9250 {
9251 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9252 TRUE);
9253 int len;
9254
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009255 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009256 if (len > 0 && ptr[len - 1] == ' ')
9257 ptr[len - 1] = NUL;
9258 }
9259
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009260 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 if (temp == NUL && gchar_cursor() != NUL)
9262 inc_cursor();
9263 }
9264#ifdef FEAT_VREPLACE
9265 else
9266 dec_cursor();
9267#endif
9268
9269 /*
9270 * In REPLACE mode we have to put back the text that was replaced
9271 * by the NL. On the replace stack is first a NUL-terminated
9272 * sequence of characters that were deleted and then the
9273 * characters that NL replaced.
9274 */
9275 if (State & REPLACE_FLAG)
9276 {
9277 /*
9278 * Do the next ins_char() in NORMAL state, to
9279 * prevent ins_char() from replacing characters and
9280 * avoiding showmatch().
9281 */
9282 oldState = State;
9283 State = NORMAL;
9284 /*
9285 * restore characters (blanks) deleted after cursor
9286 */
9287 while (cc > 0)
9288 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009289 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290#ifdef FEAT_MBYTE
9291 mb_replace_pop_ins(cc);
9292#else
9293 ins_char(cc);
9294#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009295 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 cc = replace_pop();
9297 }
9298 /* restore the characters that NL replaced */
9299 replace_pop_ins();
9300 State = oldState;
9301 }
9302 }
9303 did_ai = FALSE;
9304 }
9305 else
9306 {
9307 /*
9308 * Delete character(s) before the cursor.
9309 */
9310#ifdef FEAT_RIGHTLEFT
9311 if (revins_on) /* put cursor on last inserted char */
9312 dec_cursor();
9313#endif
9314 mincol = 0;
9315 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009316 if (mode == BACKSPACE_LINE
9317 && (curbuf->b_p_ai
9318#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009319 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009320#endif
9321 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322#ifdef FEAT_RIGHTLEFT
9323 && !revins_on
9324#endif
9325 )
9326 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009327 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009329 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009331 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 }
9333
9334 /*
9335 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9336 */
9337 if ( mode == BACKSPACE_CHAR
9338 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009339 || ((get_sts_value() != 0
9340#ifdef FEAT_VARTABS
9341 || tabstop_count(curbuf->b_p_vsts_array)
9342#endif
9343 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009344 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 && (*(ml_get_cursor() - 1) == TAB
9346 || (*(ml_get_cursor() - 1) == ' '
9347 && (!*inserted_space_p
9348 || arrow_used))))))
9349 {
9350 int ts;
9351 colnr_T vcol;
9352 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009353 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
9355 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 /* Compute the virtual column where we want to be. Since
9357 * 'showbreak' may get in the way, need to get the last column of
9358 * the previous character. */
9359 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009360 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 dec_cursor();
9362 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9363 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009364#ifdef FEAT_VARTABS
9365 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009366 {
9367 ts = (int)get_sw_value(curbuf);
9368 want_vcol = (want_vcol / ts) * ts;
9369 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009370 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009371 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009372 curbuf->b_p_vsts_array);
9373#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009374 if (p_sta && in_indent)
9375 ts = (int)get_sw_value(curbuf);
9376 else
9377 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
9381 /* delete characters until we are at or before want_vcol */
9382 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009383 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009384 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
9386 /* insert extra spaces until we are at want_vcol */
9387 while (vcol < want_vcol)
9388 {
9389 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009390 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9391 && curwin->w_cursor.col < Insstart_orig.col)
9392 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393
9394#ifdef FEAT_VREPLACE
9395 if (State & VREPLACE_FLAG)
9396 ins_char(' ');
9397 else
9398#endif
9399 {
9400 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009401 if ((State & REPLACE_FLAG))
9402 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 }
9404 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9405 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009406
9407 /* If we are now back where we started delete one character. Can
9408 * happen when using 'sts' and 'linebreak'. */
9409 if (vcol >= start_vcol)
9410 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 }
9412
9413 /*
9414 * Delete upto starting point, start of line or previous word.
9415 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009416 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009418#ifdef FEAT_MBYTE
9419 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009421 if (has_mbyte)
9422 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009424 do
9425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009427 if (!revins_on) /* put cursor on char to be deleted */
9428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009430
9431 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009433 /* look multi-byte character class */
9434 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009436 prev_cclass = cclass;
9437 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009440
9441 /* start of word? */
9442 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9443 {
9444 mode = BACKSPACE_WORD_NOT_SPACE;
9445 temp = vim_iswordc(cc);
9446 }
9447 /* end of word? */
9448 else if (mode == BACKSPACE_WORD_NOT_SPACE
9449 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9450#ifdef FEAT_MBYTE
9451 || prev_cclass != cclass
9452#endif
9453 ))
9454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009456 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009458 inc_cursor();
9459#ifdef FEAT_RIGHTLEFT
9460 else if (State & REPLACE_FLAG)
9461 dec_cursor();
9462#endif
9463 break;
9464 }
9465 if (State & REPLACE_FLAG)
9466 replace_do_bs(-1);
9467 else
9468 {
9469#ifdef FEAT_MBYTE
9470 if (enc_utf8 && p_deco)
9471 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9472#endif
9473 (void)del_char(FALSE);
9474#ifdef FEAT_MBYTE
9475 /*
9476 * If there are combining characters and 'delcombine' is set
9477 * move the cursor back. Don't back up before the base
9478 * character.
9479 */
9480 if (enc_utf8 && p_deco && cpc[0] != NUL)
9481 inc_cursor();
9482#endif
9483#ifdef FEAT_RIGHTLEFT
9484 if (revins_chars)
9485 {
9486 revins_chars--;
9487 revins_legal++;
9488 }
9489 if (revins_on && gchar_cursor() == NUL)
9490 break;
9491#endif
9492 }
9493 /* Just a single backspace?: */
9494 if (mode == BACKSPACE_CHAR)
9495 break;
9496 } while (
9497#ifdef FEAT_RIGHTLEFT
9498 revins_on ||
9499#endif
9500 (curwin->w_cursor.col > mincol
9501 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9502 || curwin->w_cursor.col != Insstart_orig.col)));
9503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 did_backspace = TRUE;
9505 }
9506#ifdef FEAT_SMARTINDENT
9507 did_si = FALSE;
9508 can_si = FALSE;
9509 can_si_back = FALSE;
9510#endif
9511 if (curwin->w_cursor.col <= 1)
9512 did_ai = FALSE;
9513 /*
9514 * It's a little strange to put backspaces into the redo
9515 * buffer, but it makes auto-indent a lot easier to deal
9516 * with.
9517 */
9518 AppendCharToRedobuff(c);
9519
9520 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009521 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009522 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009523 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524
9525 /* vi behaviour: the cursor moves backward but the character that
9526 * was there remains visible
9527 * Vim behaviour: the cursor moves backward and the character that
9528 * was there is erased from the screen.
9529 * We can emulate the vi behaviour by pretending there is a dollar
9530 * displayed even when there isn't.
9531 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009532 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 dollar_vcol = curwin->w_virtcol;
9534
Bram Moolenaarce3be472008-01-14 19:12:28 +00009535#ifdef FEAT_FOLDING
9536 /* When deleting a char the cursor line must never be in a closed fold.
9537 * E.g., when 'foldmethod' is indent and deleting the first non-white
9538 * char before a Tab. */
9539 if (did_backspace)
9540 foldOpenCursor();
9541#endif
9542
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 return did_backspace;
9544}
9545
9546#ifdef FEAT_MOUSE
9547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009548ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009551 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552
9553# ifdef FEAT_GUI
9554 /* When GUI is active, also move/paste when 'mouse' is empty */
9555 if (!gui.in_use)
9556# endif
9557 if (!mouse_has(MOUSE_INSERT))
9558 return;
9559
9560 undisplay_dollar();
9561 tpos = curwin->w_cursor;
9562 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9563 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009564 win_T *new_curwin = curwin;
9565
9566 if (curwin != old_curwin && win_valid(old_curwin))
9567 {
9568 /* Mouse took us to another window. We need to go back to the
9569 * previous one to stop insert there properly. */
9570 curwin = old_curwin;
9571 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009572#ifdef FEAT_JOB_CHANNEL
9573 if (bt_prompt(curbuf))
9574 // Restart Insert mode when re-entering the prompt buffer.
9575 curbuf->b_prompt_insert = 'A';
9576#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009577 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009578 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009579 if (curwin != new_curwin && win_valid(new_curwin))
9580 {
9581 curwin = new_curwin;
9582 curbuf = curwin->w_buffer;
9583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584# ifdef FEAT_CINDENT
9585 can_cindent = TRUE;
9586# endif
9587 }
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 /* redraw status lines (in case another window became active) */
9590 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591}
9592
9593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009594ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009597 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009598# ifdef FEAT_INS_EXPAND
9599 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600# endif
9601
9602 tpos = curwin->w_cursor;
9603
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009604 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 {
9606 int row, col;
9607
9608 row = mouse_row;
9609 col = mouse_col;
9610
9611 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009612 wp = mouse_find_win(&row, &col);
9613 if (wp == NULL)
9614 return;
9615 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 curbuf = curwin->w_buffer;
9617 }
9618 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 undisplay_dollar();
9620
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009621# ifdef FEAT_INS_EXPAND
9622 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009623 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009624# endif
9625 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009626 if (dir == MSCR_DOWN || dir == MSCR_UP)
9627 {
9628 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9629 scroll_redraw(dir,
9630 (long)(curwin->w_botline - curwin->w_topline));
9631 else
9632 scroll_redraw(dir, 3L);
9633 }
9634#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009635 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009636 {
9637 int val, step = 6;
9638
9639 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009640 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009641 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9642 if (val < 0)
9643 val = 0;
9644 gui_do_horiz_scroll(val, TRUE);
9645 }
9646#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009647# ifdef FEAT_INS_EXPAND
9648 did_scroll = TRUE;
9649# endif
9650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 curwin->w_redr_status = TRUE;
9653
9654 curwin = old_curwin;
9655 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009657# ifdef FEAT_INS_EXPAND
9658 /* The popup menu may overlay the window, need to redraw it.
9659 * TODO: Would be more efficient to only redraw the windows that are
9660 * overlapped by the popup menu. */
9661 if (pum_visible() && did_scroll)
9662 {
9663 redraw_all_later(NOT_VALID);
9664 ins_compl_show_pum();
9665 }
9666# endif
9667
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009668 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 {
9670 start_arrow(&tpos);
9671# ifdef FEAT_CINDENT
9672 can_cindent = TRUE;
9673# endif
9674 }
9675}
9676#endif
9677
Bram Moolenaarec2da362017-01-21 20:04:22 +01009678/*
9679 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9680 * P_PE literally.
9681 * When "drop" is TRUE then consume the text and drop it.
9682 */
9683 int
9684bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9685{
9686 int c;
9687 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9688 int idx = 0;
9689 char_u *end = find_termcode((char_u *)"PE");
9690 int ret_char = -1;
9691 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009692 int save_paste = p_paste;
9693 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009694
9695 /* If the end code is too long we can't detect it, read everything. */
9696 if (STRLEN(end) >= NUMBUFLEN)
9697 end = NULL;
9698 ++no_mapping;
9699 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009700 p_paste = TRUE;
9701 curbuf->b_p_ai = FALSE;
9702
Bram Moolenaarec2da362017-01-21 20:04:22 +01009703 for (;;)
9704 {
9705 /* When the end is not defined read everything. */
9706 if (end == NULL && vpeekc() == NUL)
9707 break;
9708 c = plain_vgetc();
9709#ifdef FEAT_MBYTE
9710 if (has_mbyte)
9711 idx += (*mb_char2bytes)(c, buf + idx);
9712 else
9713#endif
9714 buf[idx++] = c;
9715 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009716 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009717 {
9718 if (end[idx] == NUL)
9719 break; /* Found the end of paste code. */
9720 continue;
9721 }
9722 if (!drop)
9723 {
9724 switch (mode)
9725 {
9726 case PASTE_CMDLINE:
9727 put_on_cmdline(buf, idx, TRUE);
9728 break;
9729
9730 case PASTE_EX:
9731 if (gap != NULL && ga_grow(gap, idx) == OK)
9732 {
9733 mch_memmove((char *)gap->ga_data + gap->ga_len,
9734 buf, (size_t)idx);
9735 gap->ga_len += idx;
9736 }
9737 break;
9738
9739 case PASTE_INSERT:
9740 if (stop_arrow() == OK)
9741 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009742 c = buf[0];
9743 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9744 ins_eol(c);
9745 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009746 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009747 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009748 AppendToRedobuffLit(buf, idx);
9749 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009750 }
9751 break;
9752
9753 case PASTE_ONE_CHAR:
9754 if (ret_char == -1)
9755 {
9756#ifdef FEAT_MBYTE
9757 if (has_mbyte)
9758 ret_char = (*mb_ptr2char)(buf);
9759 else
9760#endif
9761 ret_char = buf[0];
9762 }
9763 break;
9764 }
9765 }
9766 idx = 0;
9767 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009768
Bram Moolenaarec2da362017-01-21 20:04:22 +01009769 --no_mapping;
9770 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009771 p_paste = save_paste;
9772 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009773
9774 return ret_char;
9775}
9776
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009777#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009779ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009780{
9781 /* We will be leaving the current window, unless closing another tab. */
9782 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9783 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9784 {
9785 undisplay_dollar();
9786 start_arrow(&curwin->w_cursor);
9787# ifdef FEAT_CINDENT
9788 can_cindent = TRUE;
9789# endif
9790 }
9791
9792 if (c == K_TABLINE)
9793 goto_tabpage(current_tab);
9794 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009795 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009796 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009797 redraw_statuslines(); /* will redraw the tabline when needed */
9798 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009799}
9800#endif
9801
9802#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009804ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806 pos_T tpos;
9807
9808 undisplay_dollar();
9809 tpos = curwin->w_cursor;
9810 if (gui_do_scroll())
9811 {
9812 start_arrow(&tpos);
9813# ifdef FEAT_CINDENT
9814 can_cindent = TRUE;
9815# endif
9816 }
9817}
9818
9819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009820ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822 pos_T tpos;
9823
9824 undisplay_dollar();
9825 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009826 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 {
9828 start_arrow(&tpos);
9829# ifdef FEAT_CINDENT
9830 can_cindent = TRUE;
9831# endif
9832 }
9833}
9834#endif
9835
9836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837ins_left(
9838 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
9840 pos_T tpos;
9841
9842#ifdef FEAT_FOLDING
9843 if ((fdo_flags & FDO_HOR) && KeyTyped)
9844 foldOpenCursor();
9845#endif
9846 undisplay_dollar();
9847 tpos = curwin->w_cursor;
9848 if (oneleft() == OK)
9849 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009850#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9851 /* Only call start_arrow() when not busy with preediting, it will
9852 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009853 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009854#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009855 {
9856 start_arrow_with_change(&tpos, end_change);
9857 if (!end_change)
9858 AppendCharToRedobuff(K_LEFT);
9859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860#ifdef FEAT_RIGHTLEFT
9861 /* If exit reversed string, position is fixed */
9862 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9863 revins_legal++;
9864 revins_chars++;
9865#endif
9866 }
9867
9868 /*
9869 * if 'whichwrap' set for cursor in insert mode may go to
9870 * previous line
9871 */
9872 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9873 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009874 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 start_arrow(&tpos);
9876 --(curwin->w_cursor.lnum);
9877 coladvance((colnr_T)MAXCOL);
9878 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9879 }
9880 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009881 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009882 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883}
9884
9885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009886ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887{
9888 pos_T tpos;
9889
9890#ifdef FEAT_FOLDING
9891 if ((fdo_flags & FDO_HOR) && KeyTyped)
9892 foldOpenCursor();
9893#endif
9894 undisplay_dollar();
9895 tpos = curwin->w_cursor;
9896 if (c == K_C_HOME)
9897 curwin->w_cursor.lnum = 1;
9898 curwin->w_cursor.col = 0;
9899#ifdef FEAT_VIRTUALEDIT
9900 curwin->w_cursor.coladd = 0;
9901#endif
9902 curwin->w_curswant = 0;
9903 start_arrow(&tpos);
9904}
9905
9906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009907ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908{
9909 pos_T tpos;
9910
9911#ifdef FEAT_FOLDING
9912 if ((fdo_flags & FDO_HOR) && KeyTyped)
9913 foldOpenCursor();
9914#endif
9915 undisplay_dollar();
9916 tpos = curwin->w_cursor;
9917 if (c == K_C_END)
9918 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9919 coladvance((colnr_T)MAXCOL);
9920 curwin->w_curswant = MAXCOL;
9921
9922 start_arrow(&tpos);
9923}
9924
9925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009926ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927{
9928#ifdef FEAT_FOLDING
9929 if ((fdo_flags & FDO_HOR) && KeyTyped)
9930 foldOpenCursor();
9931#endif
9932 undisplay_dollar();
9933 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9934 {
9935 start_arrow(&curwin->w_cursor);
9936 (void)bck_word(1L, FALSE, FALSE);
9937 curwin->w_set_curswant = TRUE;
9938 }
9939 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009940 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941}
9942
9943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009944ins_right(
9945 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
9947#ifdef FEAT_FOLDING
9948 if ((fdo_flags & FDO_HOR) && KeyTyped)
9949 foldOpenCursor();
9950#endif
9951 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009952 if (gchar_cursor() != NUL
9953#ifdef FEAT_VIRTUALEDIT
9954 || virtual_active()
9955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 )
9957 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009958 start_arrow_with_change(&curwin->w_cursor, end_change);
9959 if (!end_change)
9960 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 curwin->w_set_curswant = TRUE;
9962#ifdef FEAT_VIRTUALEDIT
9963 if (virtual_active())
9964 oneright();
9965 else
9966#endif
9967 {
9968#ifdef FEAT_MBYTE
9969 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009970 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 else
9972#endif
9973 ++curwin->w_cursor.col;
9974 }
9975
9976#ifdef FEAT_RIGHTLEFT
9977 revins_legal++;
9978 if (revins_chars)
9979 revins_chars--;
9980#endif
9981 }
9982 /* if 'whichwrap' set for cursor in insert mode, may move the
9983 * cursor to the next line */
9984 else if (vim_strchr(p_ww, ']') != NULL
9985 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9986 {
9987 start_arrow(&curwin->w_cursor);
9988 curwin->w_set_curswant = TRUE;
9989 ++curwin->w_cursor.lnum;
9990 curwin->w_cursor.col = 0;
9991 }
9992 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009993 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009994 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
9996
9997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009998ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000#ifdef FEAT_FOLDING
10001 if ((fdo_flags & FDO_HOR) && KeyTyped)
10002 foldOpenCursor();
10003#endif
10004 undisplay_dollar();
10005 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
10006 || gchar_cursor() != NUL)
10007 {
10008 start_arrow(&curwin->w_cursor);
10009 (void)fwd_word(1L, FALSE, 0);
10010 curwin->w_set_curswant = TRUE;
10011 }
10012 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010013 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014}
10015
10016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010017ins_up(
10018 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
10020 pos_T tpos;
10021 linenr_T old_topline = curwin->w_topline;
10022#ifdef FEAT_DIFF
10023 int old_topfill = curwin->w_topfill;
10024#endif
10025
10026 undisplay_dollar();
10027 tpos = curwin->w_cursor;
10028 if (cursor_up(1L, TRUE) == OK)
10029 {
10030 if (startcol)
10031 coladvance(getvcol_nolist(&Insstart));
10032 if (old_topline != curwin->w_topline
10033#ifdef FEAT_DIFF
10034 || old_topfill != curwin->w_topfill
10035#endif
10036 )
10037 redraw_later(VALID);
10038 start_arrow(&tpos);
10039#ifdef FEAT_CINDENT
10040 can_cindent = TRUE;
10041#endif
10042 }
10043 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010044 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045}
10046
10047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010048ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
10050 pos_T tpos;
10051
10052 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010053
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010054 if (mod_mask & MOD_MASK_CTRL)
10055 {
10056 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010057 if (first_tabpage->tp_next != NULL)
10058 {
10059 start_arrow(&curwin->w_cursor);
10060 goto_tabpage(-1);
10061 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010062 return;
10063 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010064
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 tpos = curwin->w_cursor;
10066 if (onepage(BACKWARD, 1L) == OK)
10067 {
10068 start_arrow(&tpos);
10069#ifdef FEAT_CINDENT
10070 can_cindent = TRUE;
10071#endif
10072 }
10073 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010074 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075}
10076
10077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010078ins_down(
10079 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080{
10081 pos_T tpos;
10082 linenr_T old_topline = curwin->w_topline;
10083#ifdef FEAT_DIFF
10084 int old_topfill = curwin->w_topfill;
10085#endif
10086
10087 undisplay_dollar();
10088 tpos = curwin->w_cursor;
10089 if (cursor_down(1L, TRUE) == OK)
10090 {
10091 if (startcol)
10092 coladvance(getvcol_nolist(&Insstart));
10093 if (old_topline != curwin->w_topline
10094#ifdef FEAT_DIFF
10095 || old_topfill != curwin->w_topfill
10096#endif
10097 )
10098 redraw_later(VALID);
10099 start_arrow(&tpos);
10100#ifdef FEAT_CINDENT
10101 can_cindent = TRUE;
10102#endif
10103 }
10104 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010105 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106}
10107
10108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010109ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110{
10111 pos_T tpos;
10112
10113 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010114
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010115 if (mod_mask & MOD_MASK_CTRL)
10116 {
10117 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010118 if (first_tabpage->tp_next != NULL)
10119 {
10120 start_arrow(&curwin->w_cursor);
10121 goto_tabpage(0);
10122 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010123 return;
10124 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010125
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 tpos = curwin->w_cursor;
10127 if (onepage(FORWARD, 1L) == OK)
10128 {
10129 start_arrow(&tpos);
10130#ifdef FEAT_CINDENT
10131 can_cindent = TRUE;
10132#endif
10133 }
10134 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010135 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136}
10137
10138#ifdef FEAT_DND
10139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010140ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141{
10142 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10143}
10144#endif
10145
10146/*
10147 * Handle TAB in Insert or Replace mode.
10148 * Return TRUE when the TAB needs to be inserted like a normal character.
10149 */
10150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010151ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152{
10153 int ind;
10154 int i;
10155 int temp;
10156
10157 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10158 Insstart_blank_vcol = get_nolist_virtcol();
10159 if (echeck_abbr(TAB + ABBR_OFF))
10160 return FALSE;
10161
10162 ind = inindent(0);
10163#ifdef FEAT_CINDENT
10164 if (ind)
10165 can_cindent = FALSE;
10166#endif
10167
10168 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010169 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 */
10171 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010172#ifdef FEAT_VARTABS
10173 && !(p_sta && ind
10174 /* These five lines mean 'tabstop' != 'shiftwidth' */
10175 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10176 || (tabstop_count(curbuf->b_p_vts_array) == 1
10177 && tabstop_first(curbuf->b_p_vts_array)
10178 != get_sw_value(curbuf))
10179 || (tabstop_count(curbuf->b_p_vts_array) == 0
10180 && curbuf->b_p_ts != get_sw_value(curbuf))))
10181 && tabstop_count(curbuf->b_p_vsts_array) == 0
10182#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010183 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010184#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010185 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 return TRUE;
10187
10188 if (stop_arrow() == FAIL)
10189 return TRUE;
10190
10191 did_ai = FALSE;
10192#ifdef FEAT_SMARTINDENT
10193 did_si = FALSE;
10194 can_si = FALSE;
10195 can_si_back = FALSE;
10196#endif
10197 AppendToRedobuff((char_u *)"\t");
10198
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010199#ifdef FEAT_VARTABS
10200 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10201 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010202 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010203 temp -= get_nolist_virtcol() % temp;
10204 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010205 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010206 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010207 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010208 curbuf->b_p_vsts_array);
10209 else /* otherwise use 'tabstop' */
10210 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10211 curbuf->b_p_vts_array);
10212#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010214 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010215 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10216 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else /* otherwise use 'tabstop' */
10218 temp = (int)curbuf->b_p_ts;
10219 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221
10222 /*
10223 * Insert the first space with ins_char(). It will delete one char in
10224 * replace mode. Insert the rest with ins_str(); it will not delete any
10225 * chars. For VREPLACE mode, we use ins_char() for all characters.
10226 */
10227 ins_char(' ');
10228 while (--temp > 0)
10229 {
10230#ifdef FEAT_VREPLACE
10231 if (State & VREPLACE_FLAG)
10232 ins_char(' ');
10233 else
10234#endif
10235 {
10236 ins_str((char_u *)" ");
10237 if (State & REPLACE_FLAG) /* no char replaced */
10238 replace_push(NUL);
10239 }
10240 }
10241
10242 /*
10243 * When 'expandtab' not set: Replace spaces by TABs where possible.
10244 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010245#ifdef FEAT_VARTABS
10246 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10247 || get_sts_value() > 0
10248 || (p_sta && ind)))
10249#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010250 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 {
10253 char_u *ptr;
10254#ifdef FEAT_VREPLACE
10255 char_u *saved_line = NULL; /* init for GCC */
10256 pos_T pos;
10257#endif
10258 pos_T fpos;
10259 pos_T *cursor;
10260 colnr_T want_vcol, vcol;
10261 int change_col = -1;
10262 int save_list = curwin->w_p_list;
10263
10264 /*
10265 * Get the current line. For VREPLACE mode, don't make real changes
10266 * yet, just work on a copy of the line.
10267 */
10268#ifdef FEAT_VREPLACE
10269 if (State & VREPLACE_FLAG)
10270 {
10271 pos = curwin->w_cursor;
10272 cursor = &pos;
10273 saved_line = vim_strsave(ml_get_curline());
10274 if (saved_line == NULL)
10275 return FALSE;
10276 ptr = saved_line + pos.col;
10277 }
10278 else
10279#endif
10280 {
10281 ptr = ml_get_cursor();
10282 cursor = &curwin->w_cursor;
10283 }
10284
10285 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10286 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10287 curwin->w_p_list = FALSE;
10288
10289 /* Find first white before the cursor */
10290 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010291 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 {
10293 --fpos.col;
10294 --ptr;
10295 }
10296
10297 /* In Replace mode, don't change characters before the insert point. */
10298 if ((State & REPLACE_FLAG)
10299 && fpos.lnum == Insstart.lnum
10300 && fpos.col < Insstart.col)
10301 {
10302 ptr += Insstart.col - fpos.col;
10303 fpos.col = Insstart.col;
10304 }
10305
10306 /* compute virtual column numbers of first white and cursor */
10307 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10308 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10309
Bram Moolenaar597a4222014-06-25 14:39:50 +020010310 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10311 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010312 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010314 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 if (vcol + i > want_vcol)
10316 break;
10317 if (*ptr != TAB)
10318 {
10319 *ptr = TAB;
10320 if (change_col < 0)
10321 {
10322 change_col = fpos.col; /* Column of first change */
10323 /* May have to adjust Insstart */
10324 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10325 Insstart.col = fpos.col;
10326 }
10327 }
10328 ++fpos.col;
10329 ++ptr;
10330 vcol += i;
10331 }
10332
10333 if (change_col >= 0)
10334 {
10335 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010336 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337
10338 /* Skip over the spaces we need. */
10339 while (vcol < want_vcol && *ptr == ' ')
10340 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010341 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 ++ptr;
10343 ++repl_off;
10344 }
10345 if (vcol > want_vcol)
10346 {
10347 /* Must have a char with 'showbreak' just before it. */
10348 --ptr;
10349 --repl_off;
10350 }
10351 fpos.col += repl_off;
10352
10353 /* Delete following spaces. */
10354 i = cursor->col - fpos.col;
10355 if (i > 0)
10356 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010357 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 /* correct replace stack. */
10359 if ((State & REPLACE_FLAG)
10360#ifdef FEAT_VREPLACE
10361 && !(State & VREPLACE_FLAG)
10362#endif
10363 )
10364 for (temp = i; --temp >= 0; )
10365 replace_join(repl_off);
10366 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010367#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010368 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010369 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010370 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010371 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10372 (char_u *)"\t", 1);
10373 }
10374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 cursor->col -= i;
10376
10377#ifdef FEAT_VREPLACE
10378 /*
10379 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10380 * backspacing over the changed spacing and then inserting the new
10381 * spacing.
10382 */
10383 if (State & VREPLACE_FLAG)
10384 {
10385 /* Backspace from real cursor to change_col */
10386 backspace_until_column(change_col);
10387
10388 /* Insert each char in saved_line from changed_col to
10389 * ptr-cursor */
10390 ins_bytes_len(saved_line + change_col,
10391 cursor->col - change_col);
10392 }
10393#endif
10394 }
10395
10396#ifdef FEAT_VREPLACE
10397 if (State & VREPLACE_FLAG)
10398 vim_free(saved_line);
10399#endif
10400 curwin->w_p_list = save_list;
10401 }
10402
10403 return FALSE;
10404}
10405
10406/*
10407 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010408 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 */
10410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010411ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412{
10413 int i;
10414
10415 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010416 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010418 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 undisplay_dollar();
10420
10421 /*
10422 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10423 * character under the cursor. Only push a NUL on the replace stack,
10424 * nothing to put back when the NL is deleted.
10425 */
10426 if ((State & REPLACE_FLAG)
10427#ifdef FEAT_VREPLACE
10428 && !(State & VREPLACE_FLAG)
10429#endif
10430 )
10431 replace_push(NUL);
10432
10433 /*
10434 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10435 * replacing the next line, so we push all of the characters left on the
10436 * line onto the replace stack. This is not done here though, it is done
10437 * in open_line().
10438 */
10439
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010440#ifdef FEAT_VIRTUALEDIT
10441 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10442 * CTRL-O). */
10443 if (virtual_active() && curwin->w_cursor.coladd > 0)
10444 coladvance(getviscol());
10445#endif
10446
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447#ifdef FEAT_RIGHTLEFT
10448# ifdef FEAT_FKMAP
10449 if (p_altkeymap && p_fkmap)
10450 fkmap(NL);
10451# endif
10452 /* NL in reverse insert will always start in the end of
10453 * current line. */
10454 if (revins_on)
10455 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10456#endif
10457
10458 AppendToRedobuff(NL_STR);
10459 i = open_line(FORWARD,
10460#ifdef FEAT_COMMENTS
10461 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10462#endif
10463 0, old_indent);
10464 old_indent = 0;
10465#ifdef FEAT_CINDENT
10466 can_cindent = TRUE;
10467#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010468#ifdef FEAT_FOLDING
10469 /* When inserting a line the cursor line must never be in a closed fold. */
10470 foldOpenCursor();
10471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010473 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474}
10475
10476#ifdef FEAT_DIGRAPHS
10477/*
10478 * Handle digraph in insert mode.
10479 * Returns character still to be inserted, or NUL when nothing remaining to be
10480 * done.
10481 */
10482 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010483ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484{
10485 int c;
10486 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010487 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488
10489 pc_status = PC_STATUS_UNSET;
10490 if (redrawing() && !char_avail())
10491 {
10492 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010493 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494
10495 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010496 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497#ifdef FEAT_CMDL_INFO
10498 add_to_showcmd_c(Ctrl_K);
10499#endif
10500 }
10501
10502#ifdef USE_ON_FLY_SCROLL
10503 dont_scroll = TRUE; /* disallow scrolling here */
10504#endif
10505
10506 /* don't map the digraph chars. This also prevents the
10507 * mode message to be deleted when ESC is hit */
10508 ++no_mapping;
10509 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010510 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 --no_mapping;
10512 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010513 if (did_putchar)
10514 /* when the line fits in 'columns' the '?' is at the start of the next
10515 * line and will not be removed by the redraw */
10516 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010517
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 if (IS_SPECIAL(c) || mod_mask) /* special key */
10519 {
10520#ifdef FEAT_CMDL_INFO
10521 clear_showcmd();
10522#endif
10523 insert_special(c, TRUE, FALSE);
10524 return NUL;
10525 }
10526 if (c != ESC)
10527 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010528 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 if (redrawing() && !char_avail())
10530 {
10531 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010532 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533
10534 if (char2cells(c) == 1)
10535 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010536 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010538 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 }
10540#ifdef FEAT_CMDL_INFO
10541 add_to_showcmd_c(c);
10542#endif
10543 }
10544 ++no_mapping;
10545 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010546 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 --no_mapping;
10548 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010549 if (did_putchar)
10550 /* when the line fits in 'columns' the '?' is at the start of the
10551 * next line and will not be removed by a redraw */
10552 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 if (cc != ESC)
10554 {
10555 AppendToRedobuff((char_u *)CTRL_V_STR);
10556 c = getdigraph(c, cc, TRUE);
10557#ifdef FEAT_CMDL_INFO
10558 clear_showcmd();
10559#endif
10560 return c;
10561 }
10562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563#ifdef FEAT_CMDL_INFO
10564 clear_showcmd();
10565#endif
10566 return NUL;
10567}
10568#endif
10569
10570/*
10571 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10572 * Returns the char to be inserted, or NUL if none found.
10573 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010574 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010575ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576{
10577 int c;
10578 int temp;
10579 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010580 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581
10582 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10583 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010584 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 return NUL;
10586 }
10587
10588 /* try to advance to the cursor column */
10589 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010590 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 prev_ptr = ptr;
10592 validate_virtcol();
10593 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10594 {
10595 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010596 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 }
10598 if ((colnr_T)temp > curwin->w_virtcol)
10599 ptr = prev_ptr;
10600
10601#ifdef FEAT_MBYTE
10602 c = (*mb_ptr2char)(ptr);
10603#else
10604 c = *ptr;
10605#endif
10606 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010607 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 return c;
10609}
10610
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010611/*
10612 * CTRL-Y or CTRL-E typed in Insert mode.
10613 */
10614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010615ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010616{
10617 int c = tc;
10618
10619#ifdef FEAT_INS_EXPAND
10620 if (ctrl_x_mode == CTRL_X_SCROLL)
10621 {
10622 if (c == Ctrl_Y)
10623 scrolldown_clamp();
10624 else
10625 scrollup_clamp();
10626 redraw_later(VALID);
10627 }
10628 else
10629#endif
10630 {
10631 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10632 if (c != NUL)
10633 {
10634 long tw_save;
10635
10636 /* The character must be taken literally, insert like it
10637 * was typed after a CTRL-V, and pretend 'textwidth'
10638 * wasn't set. Digits, 'o' and 'x' are special after a
10639 * CTRL-V, don't use it for these. */
10640 if (c < 256 && !isalnum(c))
10641 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10642 tw_save = curbuf->b_p_tw;
10643 curbuf->b_p_tw = -1;
10644 insert_special(c, TRUE, FALSE);
10645 curbuf->b_p_tw = tw_save;
10646#ifdef FEAT_RIGHTLEFT
10647 revins_chars++;
10648 revins_legal++;
10649#endif
10650 c = Ctrl_V; /* pretend CTRL-V is last character */
10651 auto_format(FALSE, TRUE);
10652 }
10653 }
10654 return c;
10655}
10656
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657#ifdef FEAT_SMARTINDENT
10658/*
10659 * Try to do some very smart auto-indenting.
10660 * Used when inserting a "normal" character.
10661 */
10662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010663ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664{
10665 pos_T *pos, old_pos;
10666 char_u *ptr;
10667 int i;
10668 int temp;
10669
10670 /*
10671 * do some very smart indenting when entering '{' or '}'
10672 */
10673 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10674 {
10675 /*
10676 * for '}' set indent equal to indent of line containing matching '{'
10677 */
10678 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10679 {
10680 old_pos = curwin->w_cursor;
10681 /*
10682 * If the matching '{' has a ')' immediately before it (ignoring
10683 * white-space), then line up with the start of the line
10684 * containing the matching '(' if there is one. This handles the
10685 * case where an "if (..\n..) {" statement continues over multiple
10686 * lines -- webb
10687 */
10688 ptr = ml_get(pos->lnum);
10689 i = pos->col;
10690 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010691 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 ;
10693 curwin->w_cursor.lnum = pos->lnum;
10694 curwin->w_cursor.col = i;
10695 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10696 curwin->w_cursor = *pos;
10697 i = get_indent();
10698 curwin->w_cursor = old_pos;
10699#ifdef FEAT_VREPLACE
10700 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010701 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 else
10703#endif
10704 (void)set_indent(i, SIN_CHANGED);
10705 }
10706 else if (curwin->w_cursor.col > 0)
10707 {
10708 /*
10709 * when inserting '{' after "O" reduce indent, but not
10710 * more than indent of previous line
10711 */
10712 temp = TRUE;
10713 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10714 {
10715 old_pos = curwin->w_cursor;
10716 i = get_indent();
10717 while (curwin->w_cursor.lnum > 1)
10718 {
10719 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10720
10721 /* ignore empty lines and lines starting with '#'. */
10722 if (*ptr != '#' && *ptr != NUL)
10723 break;
10724 }
10725 if (get_indent() >= i)
10726 temp = FALSE;
10727 curwin->w_cursor = old_pos;
10728 }
10729 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010730 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 }
10732 }
10733
10734 /*
10735 * set indent of '#' always to 0
10736 */
10737 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10738 {
10739 /* remember current indent for next line */
10740 old_indent = get_indent();
10741 (void)set_indent(0, SIN_CHANGED);
10742 }
10743
10744 /* Adjust ai_col, the char at this position can be deleted. */
10745 if (ai_col > curwin->w_cursor.col)
10746 ai_col = curwin->w_cursor.col;
10747}
10748#endif
10749
10750/*
10751 * Get the value that w_virtcol would have when 'list' is off.
10752 * Unless 'cpo' contains the 'L' flag.
10753 */
10754 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010755get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756{
10757 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10758 return getvcol_nolist(&curwin->w_cursor);
10759 validate_virtcol();
10760 return curwin->w_virtcol;
10761}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010762
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010763#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010764/*
10765 * Handle the InsertCharPre autocommand.
10766 * "c" is the character that was typed.
10767 * Return a pointer to allocated memory with the replacement string.
10768 * Return NULL to continue inserting "c".
10769 */
10770 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010771do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010772{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010773 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010774 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010775
10776 /* Return quickly when there is nothing to do. */
10777 if (!has_insertcharpre())
10778 return NULL;
10779
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010780# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010781 if (has_mbyte)
10782 buf[(*mb_char2bytes)(c, buf)] = NUL;
10783 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010784# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010785 {
10786 buf[0] = c;
10787 buf[1] = NUL;
10788 }
10789
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010790 /* Lock the text to avoid weird things from happening. */
10791 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010792 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010793
Bram Moolenaar704984a2012-06-01 14:57:51 +020010794 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010795 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010796 {
10797 /* Get the value of v:char. It may be empty or more than one
10798 * character. Only use it when changed, otherwise continue with the
10799 * original character to avoid breaking autoindent. */
10800 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10801 res = vim_strsave(get_vim_var_str(VV_CHAR));
10802 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010803
10804 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10805 --textlock;
10806
10807 return res;
10808}
10809#endif