blob: 657bd2c864fb7948ab98df017a6ff938f493961a [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 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004887 dict_add_string(dict, "word", compl_shown_match->cp_str);
4888 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4889 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4890 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4891 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4892 dict_add_string(dict, "user_data",
4893 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004894 }
4895 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004896 if (!in_compl_func)
4897 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898}
4899
4900/*
4901 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004902 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4903 * get more completions. If it is FALSE, then we just do nothing when there
4904 * are no more completions in a given direction. The latter case is used when
4905 * we are still in the middle of finding completions, to allow browsing
4906 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 * Return the total number of matches, or -1 if still unknown -- webb.
4908 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004909 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4910 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 *
4912 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004913 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4914 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 */
4916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004917ins_compl_next(
4918 int allow_get_expansion,
4919 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004920 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004921 int insert_match, /* Insert the newly selected match */
4922 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
4924 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004925 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004926 compl_T *found_compl = NULL;
4927 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004928 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004929 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004931 /* When user complete function return -1 for findstart which is next
4932 * time of 'always', compl_shown_match become NULL. */
4933 if (compl_shown_match == NULL)
4934 return -1;
4935
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004936 if (compl_leader != NULL
4937 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004939 /* Set "compl_shown_match" to the actually shown match, it may differ
4940 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004941 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004942 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004943 && compl_shown_match->cp_next != NULL
4944 && compl_shown_match->cp_next != compl_first_match)
4945 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004946
4947 /* If we didn't find it searching forward, and compl_shows_dir is
4948 * backward, find the last match. */
4949 if (compl_shows_dir == BACKWARD
4950 && !ins_compl_equal(compl_shown_match,
4951 compl_leader, (int)STRLEN(compl_leader))
4952 && (compl_shown_match->cp_next == NULL
4953 || compl_shown_match->cp_next == compl_first_match))
4954 {
4955 while (!ins_compl_equal(compl_shown_match,
4956 compl_leader, (int)STRLEN(compl_leader))
4957 && compl_shown_match->cp_prev != NULL
4958 && compl_shown_match->cp_prev != compl_first_match)
4959 compl_shown_match = compl_shown_match->cp_prev;
4960 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004961 }
4962
4963 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004964 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 /* Delete old text to be replaced */
4966 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004967
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004968 /* When finding the longest common text we stick at the original text,
4969 * don't let CTRL-N or CTRL-P move to the first match. */
4970 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4971
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004972 /* When restarting the search don't insert the first match either. */
4973 if (compl_restarting)
4974 {
4975 advance = FALSE;
4976 compl_restarting = FALSE;
4977 }
4978
Bram Moolenaare3226be2005-12-18 22:10:00 +00004979 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4980 * around. */
4981 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004983 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004985 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004986 found_end = (compl_first_match != NULL
4987 && (compl_shown_match->cp_next == compl_first_match
4988 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004989 }
4990 else if (compl_shows_dir == BACKWARD
4991 && compl_shown_match->cp_prev != NULL)
4992 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004993 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004994 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004995 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004998 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004999 if (!allow_get_expansion)
5000 {
5001 if (advance)
5002 {
5003 if (compl_shows_dir == BACKWARD)
5004 compl_pending -= todo + 1;
5005 else
5006 compl_pending += todo + 1;
5007 }
5008 return -1;
5009 }
5010
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005011 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005012 {
5013 if (compl_shows_dir == BACKWARD)
5014 --compl_pending;
5015 else
5016 ++compl_pending;
5017 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005018
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005019 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005020 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005021
5022 /* handle any pending completions */
5023 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005024 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005025 {
5026 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5027 {
5028 compl_shown_match = compl_shown_match->cp_next;
5029 --compl_pending;
5030 }
5031 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5032 {
5033 compl_shown_match = compl_shown_match->cp_prev;
5034 ++compl_pending;
5035 }
5036 else
5037 break;
5038 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005039 found_end = FALSE;
5040 }
5041 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5042 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005043 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005044 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005045 ++todo;
5046 else
5047 /* Remember a matching item. */
5048 found_compl = compl_shown_match;
5049
5050 /* Stop at the end of the list when we found a usable match. */
5051 if (found_end)
5052 {
5053 if (found_compl != NULL)
5054 {
5055 compl_shown_match = found_compl;
5056 break;
5057 }
5058 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005062 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005063 if (compl_no_insert && !started)
5064 {
5065 ins_bytes(compl_orig_text + ins_compl_len());
5066 compl_used_match = FALSE;
5067 }
5068 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005069 {
5070 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005071 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005072 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005073 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005074 }
5075 else
5076 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
5078 if (!allow_get_expansion)
5079 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005080 /* may undisplay the popup menu first */
5081 ins_compl_upd_pum();
5082
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005083 /* redraw to show the user what was inserted */
5084 update_screen(0);
5085
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005086 /* display the updated popup menu */
5087 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005088#ifdef FEAT_GUI
5089 if (gui.in_use)
5090 {
5091 /* Show the cursor after the match, not after the redrawn text. */
5092 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005093 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005094 }
5095#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005096
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 /* Delete old text to be replaced, since we're still searching and
5098 * don't want to match ourselves! */
5099 ins_compl_delete();
5100 }
5101
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005102 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005103 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005104 if (compl_no_insert && !started)
5105 compl_enter_selects = TRUE;
5106 else
5107 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005108
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 /*
5110 * Show the file name for the match (if any)
5111 * Truncate the file name to avoid a wait for return.
5112 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005113 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005115 char *lead = _("match in file");
5116 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5117 char_u *s;
5118 char_u *e;
5119
5120 if (space > 0)
5121 {
5122 /* We need the tail that fits. With double-byte encoding going
5123 * back from the end is very slow, thus go from the start and keep
5124 * the text that fits in "space" between "s" and "e". */
5125 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5126 {
5127 space -= ptr2cells(e);
5128 while (space < 0)
5129 {
5130 space += ptr2cells(s);
5131 MB_PTR_ADV(s);
5132 }
5133 }
5134 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5135 s > compl_shown_match->cp_fname ? "<" : "", s);
5136 msg(IObuff);
5137 redraw_cmdline = FALSE; /* don't overwrite! */
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140
5141 return num_matches;
5142}
5143
5144/*
5145 * Call this while finding completions, to check whether the user has hit a key
5146 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005147 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005149 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005150 * "in_compl_func" is TRUE when called from complete_check(), don't set
5151 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 */
5153 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005154ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155{
5156 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005157 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005159 /* Don't check when reading keys from a script, :normal or feedkeys().
5160 * That would break the test scripts. But do check for keys when called
5161 * from complete_check(). */
5162 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 return;
5164
5165 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005166 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 return;
5168 count = 0;
5169
Bram Moolenaara260a972006-06-23 19:36:29 +00005170 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5171 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (c != NUL)
5174 {
5175 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5176 {
5177 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005178 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005179 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005180 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005182 else
5183 {
5184 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005185 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005186 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005187 if (c != K_IGNORE)
5188 {
5189 /* Don't interrupt completion when the character wasn't typed,
5190 * e.g., when doing @q to replay keys. */
5191 if (c != Ctrl_R && KeyTyped)
5192 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005193
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005194 vungetc(c);
5195 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005198 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005199 {
5200 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5201
5202 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005203 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005204 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005205}
5206
5207/*
5208 * Decide the direction of Insert mode complete from the key typed.
5209 * Returns BACKWARD or FORWARD.
5210 */
5211 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005212ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005213{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005214 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005215 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005216 return BACKWARD;
5217 return FORWARD;
5218}
5219
5220/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005221 * Return TRUE for keys that are used for completion only when the popup menu
5222 * is visible.
5223 */
5224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005225ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005226{
5227 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005228 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5229 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005230}
5231
5232/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005233 * Decide the number of completions to move forward.
5234 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5235 */
5236 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005237ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005238{
5239 int h;
5240
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005241 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005242 {
5243 h = pum_get_height();
5244 if (h > 3)
5245 h -= 2; /* keep some context */
5246 return h;
5247 }
5248 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249}
5250
5251/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005252 * Return TRUE if completion with "c" should insert the match, FALSE if only
5253 * to change the currently selected completion.
5254 */
5255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005256ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005257{
5258 switch (c)
5259 {
5260 case K_UP:
5261 case K_DOWN:
5262 case K_PAGEDOWN:
5263 case K_KPAGEDOWN:
5264 case K_S_DOWN:
5265 case K_PAGEUP:
5266 case K_KPAGEUP:
5267 case K_S_UP:
5268 return FALSE;
5269 }
5270 return TRUE;
5271}
5272
5273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 * Do Insert mode completion.
5275 * Called when character "c" was typed, which has a meaning for completion.
5276 * Returns OK if completion was done, FAIL if something failed (out of mem).
5277 */
5278 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005279ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 char_u *line;
5282 int startcol = 0; /* column where searched text starts */
5283 colnr_T curs_col; /* cursor column */
5284 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005285 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005286 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005287 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005288 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
Bram Moolenaare3226be2005-12-18 22:10:00 +00005290 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005291 insert_match = ins_compl_use_match(c);
5292
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
5295 /* First time we hit ^N or ^P (in a row, I mean) */
5296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 did_ai = FALSE;
5298#ifdef FEAT_SMARTINDENT
5299 did_si = FALSE;
5300 can_si = FALSE;
5301 can_si_back = FALSE;
5302#endif
5303 if (stop_arrow() == FAIL)
5304 return FAIL;
5305
5306 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005307 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005308 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005310 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005311 * "compl_startpos" to the cursor as a pattern to add a new word
5312 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005313 * "compl_startpos" is not in the same line as the cursor then fix it
5314 * (the line has been split because it was longer than 'tw'). if SOL
5315 * is set then skip the previous pattern, a word at the beginning of
5316 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005317 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5318 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
5320 /*
5321 * it is a continued search
5322 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005323 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005324 if (ctrl_x_mode == CTRL_X_NORMAL
5325 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5326 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005330 /* line (probably) wrapped, set compl_startpos to the
5331 * first non_blank in the line, if it is not a wordchar
5332 * include it to get a better pattern, but then we don't
5333 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005334 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 compl_startpos.col = compl_col;
5336 compl_startpos.lnum = curwin->w_cursor.lnum;
5337 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
5339 else
5340 {
5341 /* S_IPOS was set when we inserted a word that was at the
5342 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 * mode but first we need to redefine compl_startpos */
5344 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 compl_cont_status |= CONT_SOL;
5347 compl_startpos.col = (colnr_T)(skipwhite(
5348 line + compl_length
5349 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005354 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005355 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005357 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005359 compl_cont_status &= ~CONT_SOL;
5360 compl_length = (IOSIZE - MIN_SPACE);
5361 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005363 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5364 if (compl_length < 1)
5365 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005367 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
5372 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005377 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005378 if (ctrl_x_mode != CTRL_X_NORMAL)
5379 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 compl_cont_status = 0;
5381 compl_cont_status |= CONT_N_ADDS;
5382 compl_startpos = curwin->w_cursor;
5383 startcol = (int)curs_col;
5384 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
5386
5387 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005388 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005390 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5392 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005393 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005397 compl_col += ++startcol;
5398 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005401 compl_pattern = str_foldcase(line + compl_col,
5402 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005404 compl_pattern = vim_strnsave(line + compl_col,
5405 compl_length);
5406 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 return FAIL;
5408 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 {
5411 char_u *prefix = (char_u *)"\\<";
5412
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005413 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005415 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005416 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 if (!vim_iswordp(line + compl_col)
5419 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 && (
5421#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005422 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005424 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#endif
5426 )))
5427 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005428 STRCPY((char *)compl_pattern, prefix);
5429 (void)quote_meta(compl_pattern + STRLEN(prefix),
5430 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005434 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437#endif
5438 )
5439 {
5440 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5442 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005444 compl_col += curs_col;
5445 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 }
5447 else
5448 {
5449#ifdef FEAT_MBYTE
5450 /* Search the point of change class of multibyte character
5451 * or not a word single byte character backward. */
5452 if (has_mbyte)
5453 {
5454 int base_class;
5455 int head_off;
5456
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005457 startcol -= (*mb_head_off)(line, line + startcol);
5458 base_class = mb_get_class(line + startcol);
5459 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 head_off = (*mb_head_off)(line, line + startcol);
5462 if (base_class != mb_get_class(line + startcol
5463 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005465 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467 }
5468 else
5469#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005472 compl_col += ++startcol;
5473 compl_length = (int)curs_col - startcol;
5474 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
5476 /* Only match word with at least two chars -- webb
5477 * there's no need to call quote_meta,
5478 * alloc(7) is enough -- Acevedo
5479 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005480 compl_pattern = alloc(7);
5481 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005483 STRCPY((char *)compl_pattern, "\\<");
5484 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5485 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 else
5488 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005489 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005490 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005491 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005493 STRCPY((char *)compl_pattern, "\\<");
5494 (void)quote_meta(compl_pattern + 2, line + compl_col,
5495 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497 }
5498 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005499 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005501 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005502 compl_length = (int)curs_col - (int)compl_col;
5503 if (compl_length < 0) /* cursor in indent: empty pattern */
5504 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005506 compl_pattern = str_foldcase(line + compl_col, compl_length,
5507 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005509 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5510 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 return FAIL;
5512 }
5513 else if (ctrl_x_mode == CTRL_X_FILES)
5514 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005515 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005516 if (startcol > 0)
5517 {
5518 char_u *p = line + startcol;
5519
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005520 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005521 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005522 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005523 if (p == line && vim_isfilec(PTR2CHAR(p)))
5524 startcol = 0;
5525 else
5526 startcol = (int)(p - line) + 1;
5527 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005528
Bram Moolenaar0300e462013-09-08 16:03:45 +02005529 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005530 compl_length = (int)curs_col - startcol;
5531 compl_pattern = addstar(line + compl_col, compl_length,
5532 EXPAND_FILES);
5533 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 return FAIL;
5535 }
5536 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5537 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005538 compl_pattern = vim_strnsave(line, curs_col);
5539 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005541 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005542 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005543 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5544 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005545 /* No completion possible, use an empty pattern to get a
5546 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005547 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005548 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005549 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5550 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005552 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005553 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005554#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005555 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005556 * Call user defined function 'completefunc' with "a:findstart"
5557 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005558 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005559 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005560 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005561 char_u *funcname;
5562 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005563 win_T *curwin_save;
5564 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005565
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005566 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005567 * string */
5568 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5569 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5570 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005571 {
5572 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5573 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005574 /* restore did_ai, so that adding comment leader works */
5575 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005576 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005577 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005578
Bram Moolenaarffa96842018-06-12 22:05:14 +02005579 args[0].v_type = VAR_NUMBER;
5580 args[0].vval.v_number = 1;
5581 args[1].v_type = VAR_STRING;
5582 args[1].vval.v_string = (char_u *)"";
5583 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005584 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005585 curwin_save = curwin;
5586 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005587 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005588 if (curwin_save != curwin || curbuf_save != curbuf)
5589 {
5590 EMSG(_(e_complwin));
5591 return FAIL;
5592 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005593 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005594 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005595 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005596 {
5597 EMSG(_(e_compldel));
5598 return FAIL;
5599 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005600
Bram Moolenaar6110a002012-01-26 18:58:38 +01005601 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005602 * cancel the complete without an error.
5603 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005604 if (col == -2)
5605 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005606 if (col == -3)
5607 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005608 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005609 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005610 if (!shortmess(SHM_COMPLETIONMENU))
5611 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005612 return FAIL;
5613 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005614
Bram Moolenaar82139082011-09-14 16:52:09 +02005615 /*
5616 * Reset extended parameters of completion, when start new
5617 * completion.
5618 */
5619 compl_opt_refresh_always = FALSE;
5620
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005621 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005622 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005623 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005624 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005625 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005626
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005627 /* Setup variables for completion. Need to obtain "line" again,
5628 * it may have become invalid. */
5629 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005630 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005631 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5632 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005633#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005634 return FAIL;
5635 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005636 else if (ctrl_x_mode == CTRL_X_SPELL)
5637 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005638#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005639 if (spell_bad_len > 0)
5640 compl_col = curs_col - spell_bad_len;
5641 else
5642 compl_col = spell_word_start(startcol);
5643 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005644 {
5645 compl_length = 0;
5646 compl_col = curs_col;
5647 }
5648 else
5649 {
5650 spell_expand_check_cap(compl_col);
5651 compl_length = (int)curs_col - compl_col;
5652 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005653 /* Need to obtain "line" again, it may have become invalid. */
5654 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005655 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5656 if (compl_pattern == NULL)
5657#endif
5658 return FAIL;
5659 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005660 else
5661 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005662 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005663 return FAIL;
5664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005666 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
5668 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005669 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 /* Insert a new line, keep indentation but ignore 'comments' */
5672#ifdef FEAT_COMMENTS
5673 char_u *old = curbuf->b_p_com;
5674
5675 curbuf->b_p_com = (char_u *)"";
5676#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005677 compl_startpos.lnum = curwin->w_cursor.lnum;
5678 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 ins_eol('\r');
5680#ifdef FEAT_COMMENTS
5681 curbuf->b_p_com = old;
5682#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005683 compl_length = 0;
5684 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 }
5687 else
5688 {
5689 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005690 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005693 if (compl_cont_status & CONT_LOCAL)
5694 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 else
5696 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5697
Bram Moolenaar62951b12011-09-21 18:23:05 +02005698 /* If any of the original typed text has been changed we need to fix
5699 * the redo buffer. */
5700 ins_compl_fixRedoBufForLeader(NULL);
5701
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005702 /* Always add completion for the original text. */
5703 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005704 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5705 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005706 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005708 VIM_CLEAR(compl_pattern);
5709 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 return FAIL;
5711 }
5712
5713 /* showmode might reset the internal line pointers, so it must
5714 * be called before line = ml_get(), or when this address is no
5715 * longer needed. -- Acevedo.
5716 */
5717 edit_submode_extra = (char_u *)_("-- Searching...");
5718 edit_submode_highl = HLF_COUNT;
5719 showmode();
5720 edit_submode_extra = NULL;
5721 out_flush();
5722 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005723 else if (insert_match && stop_arrow() == FAIL)
5724 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005726 compl_shown_match = compl_curr_match;
5727 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005730 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005732 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005733 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005734 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005736 /* may undisplay the popup menu */
5737 ins_compl_upd_pum();
5738
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005739 if (n > 1) /* all matches have been found */
5740 compl_matches = n;
5741 compl_curr_match = compl_shown_match;
5742 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
Bram Moolenaard68071d2006-05-02 22:08:30 +00005744 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5745 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 if (got_int && !global_busy)
5747 {
5748 (void)vgetc();
5749 got_int = FALSE;
5750 }
5751
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005752 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005753 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005755 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5756 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5758 edit_submode_highl = HLF_E;
5759 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5760 * because we couldn't expand anything at first place, but if we used
5761 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5762 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005763 if ( compl_length > 1
5764 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005765 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5767 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005768 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
5770
Bram Moolenaar572cb562005-08-05 21:35:02 +00005771 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005772 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005774 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
5776 if (edit_submode_extra == NULL)
5777 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005778 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
5780 edit_submode_extra = (char_u *)_("Back at original");
5781 edit_submode_highl = HLF_W;
5782 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005783 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
5785 edit_submode_extra = (char_u *)_("Word from other line");
5786 edit_submode_highl = HLF_COUNT;
5787 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005788 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 edit_submode_extra = (char_u *)_("The only match");
5791 edit_submode_highl = HLF_COUNT;
5792 }
5793 else
5794 {
5795 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005796 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005798 int number = 0;
5799 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005801 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 {
5803 /* search backwards for the first valid (!= -1) number.
5804 * This should normally succeed already at the first loop
5805 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005806 for (match = compl_curr_match->cp_prev; match != NULL
5807 && match != compl_first_match;
5808 match = match->cp_prev)
5809 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005811 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 break;
5813 }
5814 if (match != NULL)
5815 /* go up and assign all numbers which are not assigned
5816 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005817 for (match = match->cp_next;
5818 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005819 match = match->cp_next)
5820 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
5822 else /* BACKWARD */
5823 {
5824 /* search forwards (upwards) for the first valid (!= -1)
5825 * number. This should normally succeed already at the
5826 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005827 for (match = compl_curr_match->cp_next; match != NULL
5828 && match != compl_first_match;
5829 match = match->cp_next)
5830 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005832 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 break;
5834 }
5835 if (match != NULL)
5836 /* go down and assign all numbers which are not
5837 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005838 for (match = match->cp_prev; match
5839 && match->cp_number == -1;
5840 match = match->cp_prev)
5841 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
5843 }
5844
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005845 /* The match should always have a sequence number now, this is
5846 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005847 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005849 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5850 * Translations may need more than twice that. */
5851 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005853 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005854 vim_snprintf((char *)match_ref, sizeof(match_ref),
5855 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005856 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005858 vim_snprintf((char *)match_ref, sizeof(match_ref),
5859 _("match %d"),
5860 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 edit_submode_extra = match_ref;
5862 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005863 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 curs_columns(FALSE);
5865 }
5866 }
5867 }
5868
5869 /* Show a message about what (completion) mode we're in. */
5870 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005871 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005873 if (edit_submode_extra != NULL)
5874 {
5875 if (!p_smd)
5876 msg_attr(edit_submode_extra,
5877 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005878 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005879 }
5880 else
5881 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
Bram Moolenaard68071d2006-05-02 22:08:30 +00005884 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005885 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005886 show_pum(save_w_wrow, save_w_leftcol);
5887
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005888 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005889 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 return OK;
5892}
5893
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005894 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005895show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005896{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005897 /* RedrawingDisabled may be set when invoked through complete(). */
5898 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005899
Bram Moolenaarcb036422017-03-01 12:29:10 +01005900 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005901
Bram Moolenaarcb036422017-03-01 12:29:10 +01005902 /* If the cursor moved or the display scrolled we need to remove the pum
5903 * first. */
5904 setcursor();
5905 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5906 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005907
Bram Moolenaarcb036422017-03-01 12:29:10 +01005908 ins_compl_show_pum();
5909 setcursor();
5910 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005911}
5912
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913/*
5914 * Looks in the first "len" chars. of "src" for search-metachars.
5915 * If dest is not NULL the chars. are copied there quoting (with
5916 * a backslash) the metachars, and dest would be NUL terminated.
5917 * Returns the length (needed) of dest
5918 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005919 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005920quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005922 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005924 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
5926 switch (*src)
5927 {
5928 case '.':
5929 case '*':
5930 case '[':
5931 if (ctrl_x_mode == CTRL_X_DICTIONARY
5932 || ctrl_x_mode == CTRL_X_THESAURUS)
5933 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005934 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 case '~':
5936 if (!p_magic) /* quote these only if magic is set */
5937 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005938 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 case '\\':
5940 if (ctrl_x_mode == CTRL_X_DICTIONARY
5941 || ctrl_x_mode == CTRL_X_THESAURUS)
5942 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005943 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 case '^': /* currently it's not needed. */
5945 case '$':
5946 m++;
5947 if (dest != NULL)
5948 *dest++ = '\\';
5949 break;
5950 }
5951 if (dest != NULL)
5952 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005953# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 /* Copy remaining bytes of a multibyte character. */
5955 if (has_mbyte)
5956 {
5957 int i, mb_len;
5958
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005959 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 if (mb_len > 0 && len >= mb_len)
5961 for (i = 0; i < mb_len; ++i)
5962 {
5963 --len;
5964 ++src;
5965 if (dest != NULL)
5966 *dest++ = *src;
5967 }
5968 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 }
5971 if (dest != NULL)
5972 *dest = NUL;
5973
5974 return m;
5975}
5976#endif /* FEAT_INS_EXPAND */
5977
5978/*
5979 * Next character is interpreted literally.
5980 * A one, two or three digit decimal number is interpreted as its byte value.
5981 * If one or two digits are entered, the next character is given to vungetc().
5982 * For Unicode a character > 255 may be returned.
5983 */
5984 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005985get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
5987 int cc;
5988 int nc;
5989 int i;
5990 int hex = FALSE;
5991 int octal = FALSE;
5992#ifdef FEAT_MBYTE
5993 int unicode = 0;
5994#endif
5995
5996 if (got_int)
5997 return Ctrl_C;
5998
5999#ifdef FEAT_GUI
6000 /*
6001 * In GUI there is no point inserting the internal code for a special key.
6002 * It is more useful to insert the string "<KEY>" instead. This would
6003 * probably be useful in a text window too, but it would not be
6004 * vi-compatible (maybe there should be an option for it?) -- webb
6005 */
6006 if (gui.in_use)
6007 ++allow_keys;
6008#endif
6009#ifdef USE_ON_FLY_SCROLL
6010 dont_scroll = TRUE; /* disallow scrolling here */
6011#endif
6012 ++no_mapping; /* don't map the next key hits */
6013 cc = 0;
6014 i = 0;
6015 for (;;)
6016 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006017 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018#ifdef FEAT_CMDL_INFO
6019 if (!(State & CMDLINE)
6020# ifdef FEAT_MBYTE
6021 && MB_BYTE2LEN_CHECK(nc) == 1
6022# endif
6023 )
6024 add_to_showcmd(nc);
6025#endif
6026 if (nc == 'x' || nc == 'X')
6027 hex = TRUE;
6028 else if (nc == 'o' || nc == 'O')
6029 octal = TRUE;
6030#ifdef FEAT_MBYTE
6031 else if (nc == 'u' || nc == 'U')
6032 unicode = nc;
6033#endif
6034 else
6035 {
6036 if (hex
6037#ifdef FEAT_MBYTE
6038 || unicode != 0
6039#endif
6040 )
6041 {
6042 if (!vim_isxdigit(nc))
6043 break;
6044 cc = cc * 16 + hex2nr(nc);
6045 }
6046 else if (octal)
6047 {
6048 if (nc < '0' || nc > '7')
6049 break;
6050 cc = cc * 8 + nc - '0';
6051 }
6052 else
6053 {
6054 if (!VIM_ISDIGIT(nc))
6055 break;
6056 cc = cc * 10 + nc - '0';
6057 }
6058
6059 ++i;
6060 }
6061
6062 if (cc > 255
6063#ifdef FEAT_MBYTE
6064 && unicode == 0
6065#endif
6066 )
6067 cc = 255; /* limit range to 0-255 */
6068 nc = 0;
6069
6070 if (hex) /* hex: up to two chars */
6071 {
6072 if (i >= 2)
6073 break;
6074 }
6075#ifdef FEAT_MBYTE
6076 else if (unicode) /* Unicode: up to four or eight chars */
6077 {
6078 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6079 break;
6080 }
6081#endif
6082 else if (i >= 3) /* decimal or octal: up to three chars */
6083 break;
6084 }
6085 if (i == 0) /* no number entered */
6086 {
6087 if (nc == K_ZERO) /* NUL is stored as NL */
6088 {
6089 cc = '\n';
6090 nc = 0;
6091 }
6092 else
6093 {
6094 cc = nc;
6095 nc = 0;
6096 }
6097 }
6098
6099 if (cc == 0) /* NUL is stored as NL */
6100 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006101#ifdef FEAT_MBYTE
6102 if (enc_dbcs && (cc & 0xff) == 0)
6103 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6104 second byte will cause trouble! */
6105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107 --no_mapping;
6108#ifdef FEAT_GUI
6109 if (gui.in_use)
6110 --allow_keys;
6111#endif
6112 if (nc)
6113 vungetc(nc);
6114 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6115 return cc;
6116}
6117
6118/*
6119 * Insert character, taking care of special keys and mod_mask
6120 */
6121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006122insert_special(
6123 int c,
6124 int allow_modmask,
6125 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
6127 char_u *p;
6128 int len;
6129
6130 /*
6131 * Special function key, translate into "<Key>". Up to the last '>' is
6132 * inserted with ins_str(), so as not to replace characters in replace
6133 * mode.
6134 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6135 * unless 'allow_modmask' is TRUE.
6136 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006137#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 /* Command-key never produces a normal key */
6139 if (mod_mask & MOD_MASK_CMD)
6140 allow_modmask = TRUE;
6141#endif
6142 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6143 {
6144 p = get_special_key_name(c, mod_mask);
6145 len = (int)STRLEN(p);
6146 c = p[len - 1];
6147 if (len > 2)
6148 {
6149 if (stop_arrow() == FAIL)
6150 return;
6151 p[len - 1] = NUL;
6152 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006153 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 ctrlv = FALSE;
6155 }
6156 }
6157 if (stop_arrow() == OK)
6158 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6159}
6160
6161/*
6162 * Special characters in this context are those that need processing other
6163 * than the simple insertion that can be performed here. This includes ESC
6164 * which terminates the insert, and CR/NL which need special processing to
6165 * open up a new line. This routine tries to optimize insertions performed by
6166 * the "redo", "undo" or "put" commands, so it needs to know when it should
6167 * stop and defer processing to the "normal" mechanism.
6168 * '0' and '^' are special, because they can be followed by CTRL-D.
6169 */
6170#ifdef EBCDIC
6171# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6172#else
6173# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6174#endif
6175
6176#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006177# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006179# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180#endif
6181
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006182/*
6183 * "flags": INSCHAR_FORMAT - force formatting
6184 * INSCHAR_CTRLV - char typed just after CTRL-V
6185 * INSCHAR_NO_FEX - don't use 'formatexpr'
6186 *
6187 * NOTE: passes the flags value straight through to internal_format() which,
6188 * beside INSCHAR_FORMAT (above), is also looking for these:
6189 * INSCHAR_DO_COM - format comments
6190 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006193insertchar(
6194 int c, /* character to insert or NUL */
6195 int flags, /* INSCHAR_FORMAT, etc. */
6196 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 int textwidth;
6199#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006203 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006205 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207
6208 /*
6209 * Try to break the line in two or more pieces when:
6210 * - Always do this if we have been called to do formatting only.
6211 * - Always do this when 'formatoptions' has the 'a' flag and the line
6212 * ends in white space.
6213 * - Otherwise:
6214 * - Don't do this if inserting a blank
6215 * - Don't do this if an existing character is being replaced, unless
6216 * we're in VREPLACE mode.
6217 * - Do this if the cursor is not on the line where insert started
6218 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6219 * before the insert.
6220 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6221 * before 'textwidth'
6222 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006223 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006224 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006225 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 && !((State & REPLACE_FLAG)
6227#ifdef FEAT_VREPLACE
6228 && !(State & VREPLACE_FLAG)
6229#endif
6230 && *ml_get_cursor() != NUL)
6231 && (curwin->w_cursor.lnum != Insstart.lnum
6232 || ((!has_format_option(FO_INS_LONG)
6233 || Insstart_textlen <= (colnr_T)textwidth)
6234 && (!fo_ins_blank
6235 || Insstart_blank_vcol <= (colnr_T)textwidth
6236 ))))))
6237 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006238 /* Format with 'formatexpr' when it's set. Use internal formatting
6239 * when 'formatexpr' isn't set or it returns non-zero. */
6240#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006241 int do_internal = TRUE;
6242 colnr_T virtcol = get_nolist_virtcol()
6243 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006244
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006245 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6246 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006247 {
6248 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6249 /* It may be required to save for undo again, e.g. when setline()
6250 * was called. */
6251 ins_need_undo = TRUE;
6252 }
6253 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006255 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (c == NUL) /* only formatting was wanted */
6259 return;
6260
6261#ifdef FEAT_COMMENTS
6262 /* Check whether this character should end a comment. */
6263 if (did_ai && (int)c == end_comment_pending)
6264 {
6265 char_u *line;
6266 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6267 int middle_len, end_len;
6268 int i;
6269
6270 /*
6271 * Need to remove existing (middle) comment leader and insert end
6272 * comment leader. First, check what comment leader we can find.
6273 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006274 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6276 {
6277 /* Skip middle-comment string */
6278 while (*p && p[-1] != ':') /* find end of middle flags */
6279 ++p;
6280 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6281 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006282 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 --middle_len;
6284
6285 /* Find the end-comment string */
6286 while (*p && p[-1] != ':') /* find end of end flags */
6287 ++p;
6288 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6289
6290 /* Skip white space before the cursor */
6291 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006292 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 ;
6294 i++;
6295
6296 /* Skip to before the middle leader */
6297 i -= middle_len;
6298
6299 /* Check some expected things before we go on */
6300 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6301 {
6302 /* Backspace over all the stuff we want to replace */
6303 backspace_until_column(i);
6304
6305 /*
6306 * Insert the end-comment string, except for the last
6307 * character, which will get inserted as normal later.
6308 */
6309 ins_bytes_len(lead_end, end_len - 1);
6310 }
6311 }
6312 }
6313 end_comment_pending = NUL;
6314#endif
6315
6316 did_ai = FALSE;
6317#ifdef FEAT_SMARTINDENT
6318 did_si = FALSE;
6319 can_si = FALSE;
6320 can_si_back = FALSE;
6321#endif
6322
6323 /*
6324 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6325 * This speeds up normal text input considerably.
6326 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6327 * need to re-indent at a ':', or any other character (but not what
6328 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006329 * Don't do this when there an InsertCharPre autocommand is defined,
6330 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006331 * Do the check for InsertCharPre before the call to vpeekc() because the
6332 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 */
6334#ifdef USE_ON_FLY_SCROLL
6335 dont_scroll = FALSE; /* allow scrolling here */
6336#endif
6337
6338 if ( !ISSPECIAL(c)
6339#ifdef FEAT_MBYTE
6340 && (!has_mbyte || (*mb_char2len)(c) == 1)
6341#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006342 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 && vpeekc() != NUL
6344 && !(State & REPLACE_FLAG)
6345#ifdef FEAT_CINDENT
6346 && !cindent_on()
6347#endif
6348#ifdef FEAT_RIGHTLEFT
6349 && !p_ri
6350#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006351 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 {
6353#define INPUT_BUFLEN 100
6354 char_u buf[INPUT_BUFLEN + 1];
6355 int i;
6356 colnr_T virtcol = 0;
6357
6358 buf[0] = c;
6359 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006360 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 virtcol = get_nolist_virtcol();
6362 /*
6363 * Stop the string when:
6364 * - no more chars available
6365 * - finding a special character (command key)
6366 * - buffer is full
6367 * - running into the 'textwidth' boundary
6368 * - need to check for abbreviation: A non-word char after a word-char
6369 */
6370 while ( (c = vpeekc()) != NUL
6371 && !ISSPECIAL(c)
6372#ifdef FEAT_MBYTE
6373 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6374#endif
6375 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006376# ifdef FEAT_FKMAP
6377 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 && (textwidth == 0
6380 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6381 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6382 {
6383#ifdef FEAT_RIGHTLEFT
6384 c = vgetc();
6385 if (p_hkmap && KeyTyped)
6386 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 buf[i++] = c;
6388#else
6389 buf[i++] = vgetc();
6390#endif
6391 }
6392
6393#ifdef FEAT_DIGRAPHS
6394 do_digraph(-1); /* clear digraphs */
6395 do_digraph(buf[i-1]); /* may be the start of a digraph */
6396#endif
6397 buf[i] = NUL;
6398 ins_str(buf);
6399 if (flags & INSCHAR_CTRLV)
6400 {
6401 redo_literal(*buf);
6402 i = 1;
6403 }
6404 else
6405 i = 0;
6406 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006407 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
6409 else
6410 {
6411#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006412 int cc;
6413
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6415 {
6416 char_u buf[MB_MAXBYTES + 1];
6417
6418 (*mb_char2bytes)(c, buf);
6419 buf[cc] = NUL;
6420 ins_char_bytes(buf, cc);
6421 AppendCharToRedobuff(c);
6422 }
6423 else
6424#endif
6425 {
6426 ins_char(c);
6427 if (flags & INSCHAR_CTRLV)
6428 redo_literal(c);
6429 else
6430 AppendCharToRedobuff(c);
6431 }
6432 }
6433}
6434
6435/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006436 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006437 *
6438 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6439 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440 */
6441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006442internal_format(
6443 int textwidth,
6444 int second_indent,
6445 int flags,
6446 int format_only,
6447 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006448{
6449 int cc;
6450 int save_char = NUL;
6451 int haveto_redraw = FALSE;
6452 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6453#ifdef FEAT_MBYTE
6454 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6455#endif
6456 int fo_white_par = has_format_option(FO_WHITE_PAR);
6457 int first_line = TRUE;
6458#ifdef FEAT_COMMENTS
6459 colnr_T leader_len;
6460 int no_leader = FALSE;
6461 int do_comments = (flags & INSCHAR_DO_COM);
6462#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006463#ifdef FEAT_LINEBREAK
6464 int has_lbr = curwin->w_p_lbr;
6465
6466 /* make sure win_lbr_chartabsize() counts correctly */
6467 curwin->w_p_lbr = FALSE;
6468#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006469
6470 /*
6471 * When 'ai' is off we don't want a space under the cursor to be
6472 * deleted. Replace it with an 'x' temporarily.
6473 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006474 if (!curbuf->b_p_ai
6475#ifdef FEAT_VREPLACE
6476 && !(State & VREPLACE_FLAG)
6477#endif
6478 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479 {
6480 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006481 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006482 {
6483 save_char = cc;
6484 pchar_cursor('x');
6485 }
6486 }
6487
6488 /*
6489 * Repeat breaking lines, until the current line is not too long.
6490 */
6491 while (!got_int)
6492 {
6493 int startcol; /* Cursor column at entry */
6494 int wantcol; /* column at textwidth border */
6495 int foundcol; /* column for start of spaces */
6496 int end_foundcol = 0; /* column for start of word */
6497 colnr_T len;
6498 colnr_T virtcol;
6499#ifdef FEAT_VREPLACE
6500 int orig_col = 0;
6501 char_u *saved_text = NULL;
6502#endif
6503 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006504 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006505
Bram Moolenaar97b98102009-11-17 16:41:01 +00006506 virtcol = get_nolist_virtcol()
6507 + char2cells(c != NUL ? c : gchar_cursor());
6508 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006509 break;
6510
6511#ifdef FEAT_COMMENTS
6512 if (no_leader)
6513 do_comments = FALSE;
6514 else if (!(flags & INSCHAR_FORMAT)
6515 && has_format_option(FO_WRAP_COMS))
6516 do_comments = TRUE;
6517
6518 /* Don't break until after the comment leader */
6519 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006520 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006521 else
6522 leader_len = 0;
6523
6524 /* If the line doesn't start with a comment leader, then don't
6525 * start one in a following broken line. Avoids that a %word
6526 * moved to the start of the next line causes all following lines
6527 * to start with %. */
6528 if (leader_len == 0)
6529 no_leader = TRUE;
6530#endif
6531 if (!(flags & INSCHAR_FORMAT)
6532#ifdef FEAT_COMMENTS
6533 && leader_len == 0
6534#endif
6535 && !has_format_option(FO_WRAP))
6536
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006537 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006538 if ((startcol = curwin->w_cursor.col) == 0)
6539 break;
6540
6541 /* find column of textwidth border */
6542 coladvance((colnr_T)textwidth);
6543 wantcol = curwin->w_cursor.col;
6544
Bram Moolenaar97b98102009-11-17 16:41:01 +00006545 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006546 foundcol = 0;
6547
6548 /*
6549 * Find position to break at.
6550 * Stop at first entered white when 'formatoptions' has 'v'
6551 */
6552 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006553 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006554 || curwin->w_cursor.lnum != Insstart.lnum
6555 || curwin->w_cursor.col >= Insstart.col)
6556 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006557 if (curwin->w_cursor.col == startcol && c != NUL)
6558 cc = c;
6559 else
6560 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006561 if (WHITECHAR(cc))
6562 {
6563 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006564 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006565
6566 /* find start of sequence of blanks */
6567 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6568 {
6569 dec_cursor();
6570 cc = gchar_cursor();
6571 }
6572 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6573 break; /* only spaces in front of text */
6574#ifdef FEAT_COMMENTS
6575 /* Don't break until after the comment leader */
6576 if (curwin->w_cursor.col < leader_len)
6577 break;
6578#endif
6579 if (has_format_option(FO_ONE_LETTER))
6580 {
6581 /* do not break after one-letter words */
6582 if (curwin->w_cursor.col == 0)
6583 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006584#ifdef FEAT_COMMENTS
6585 /* do not break "#a b" when 'tw' is 2 */
6586 if (curwin->w_cursor.col <= leader_len)
6587 break;
6588#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006589 col = curwin->w_cursor.col;
6590 dec_cursor();
6591 cc = gchar_cursor();
6592
6593 if (WHITECHAR(cc))
6594 continue; /* one-letter, continue */
6595 curwin->w_cursor.col = col;
6596 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006597
6598 inc_cursor();
6599
6600 end_foundcol = end_col + 1;
6601 foundcol = curwin->w_cursor.col;
6602 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006603 break;
6604 }
6605#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006606 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006607 {
6608 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006609 if (curwin->w_cursor.col != startcol)
6610 {
6611#ifdef FEAT_COMMENTS
6612 /* Don't break until after the comment leader */
6613 if (curwin->w_cursor.col < leader_len)
6614 break;
6615#endif
6616 col = curwin->w_cursor.col;
6617 inc_cursor();
6618 /* Don't change end_foundcol if already set. */
6619 if (foundcol != curwin->w_cursor.col)
6620 {
6621 foundcol = curwin->w_cursor.col;
6622 end_foundcol = foundcol;
6623 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6624 break;
6625 }
6626 curwin->w_cursor.col = col;
6627 }
6628
6629 if (curwin->w_cursor.col == 0)
6630 break;
6631
6632 col = curwin->w_cursor.col;
6633
6634 dec_cursor();
6635 cc = gchar_cursor();
6636
6637 if (WHITECHAR(cc))
6638 continue; /* break with space */
6639#ifdef FEAT_COMMENTS
6640 /* Don't break until after the comment leader */
6641 if (curwin->w_cursor.col < leader_len)
6642 break;
6643#endif
6644
6645 curwin->w_cursor.col = col;
6646
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006647 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006648 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006649 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6650 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006651 }
6652#endif
6653 if (curwin->w_cursor.col == 0)
6654 break;
6655 dec_cursor();
6656 }
6657
6658 if (foundcol == 0) /* no spaces, cannot break line */
6659 {
6660 curwin->w_cursor.col = startcol;
6661 break;
6662 }
6663
6664 /* Going to break the line, remove any "$" now. */
6665 undisplay_dollar();
6666
6667 /*
6668 * Offset between cursor position and line break is used by replace
6669 * stack functions. VREPLACE does not use this, and backspaces
6670 * over the text instead.
6671 */
6672#ifdef FEAT_VREPLACE
6673 if (State & VREPLACE_FLAG)
6674 orig_col = startcol; /* Will start backspacing from here */
6675 else
6676#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006677 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006678
6679 /*
6680 * adjust startcol for spaces that will be deleted and
6681 * characters that will remain on top line
6682 */
6683 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006684 while ((cc = gchar_cursor(), WHITECHAR(cc))
6685 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006686 inc_cursor();
6687 startcol -= curwin->w_cursor.col;
6688 if (startcol < 0)
6689 startcol = 0;
6690
6691#ifdef FEAT_VREPLACE
6692 if (State & VREPLACE_FLAG)
6693 {
6694 /*
6695 * In VREPLACE mode, we will backspace over the text to be
6696 * wrapped, so save a copy now to put on the next line.
6697 */
6698 saved_text = vim_strsave(ml_get_cursor());
6699 curwin->w_cursor.col = orig_col;
6700 if (saved_text == NULL)
6701 break; /* Can't do it, out of memory */
6702 saved_text[startcol] = NUL;
6703
6704 /* Backspace over characters that will move to the next line */
6705 if (!fo_white_par)
6706 backspace_until_column(foundcol);
6707 }
6708 else
6709#endif
6710 {
6711 /* put cursor after pos. to break line */
6712 if (!fo_white_par)
6713 curwin->w_cursor.col = foundcol;
6714 }
6715
6716 /*
6717 * Split the line just before the margin.
6718 * Only insert/delete lines, but don't really redraw the window.
6719 */
6720 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6721 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6722#ifdef FEAT_COMMENTS
6723 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006724 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006725#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006726 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6727 if (!(flags & INSCHAR_COM_LIST))
6728 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006729
6730 replace_offset = 0;
6731 if (first_line)
6732 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006733 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006734 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006735 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006736 * This section is for auto-wrap of numeric lists. When not
6737 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6738 * flag will be set and open_line() will handle it (as seen
6739 * above). The code here (and in get_number_indent()) will
6740 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006741 */
6742 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006743 second_indent =
6744 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006745 if (second_indent >= 0)
6746 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006747#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006748 if (State & VREPLACE_FLAG)
6749 change_indent(INDENT_SET, second_indent,
6750 FALSE, NUL, TRUE);
6751 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006752#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006753#ifdef FEAT_COMMENTS
6754 if (leader_len > 0 && second_indent - leader_len > 0)
6755 {
6756 int i;
6757 int padding = second_indent - leader_len;
6758
6759 /* We started at the first_line of a numbered list
6760 * that has a comment. the open_line() function has
6761 * inserted the proper comment leader and positioned
6762 * the cursor at the end of the split line. Now we
6763 * add the additional whitespace needed after the
6764 * comment leader for the numbered list. */
6765 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006766 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006767 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006768 }
6769 else
6770 {
6771#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006772 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006773#ifdef FEAT_COMMENTS
6774 }
6775#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006776 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006777 }
6778 first_line = FALSE;
6779 }
6780
6781#ifdef FEAT_VREPLACE
6782 if (State & VREPLACE_FLAG)
6783 {
6784 /*
6785 * In VREPLACE mode we have backspaced over the text to be
6786 * moved, now we re-insert it into the new line.
6787 */
6788 ins_bytes(saved_text);
6789 vim_free(saved_text);
6790 }
6791 else
6792#endif
6793 {
6794 /*
6795 * Check if cursor is not past the NUL off the line, cindent
6796 * may have added or removed indent.
6797 */
6798 curwin->w_cursor.col += startcol;
6799 len = (colnr_T)STRLEN(ml_get_curline());
6800 if (curwin->w_cursor.col > len)
6801 curwin->w_cursor.col = len;
6802 }
6803
6804 haveto_redraw = TRUE;
6805#ifdef FEAT_CINDENT
6806 can_cindent = TRUE;
6807#endif
6808 /* moved the cursor, don't autoindent or cindent now */
6809 did_ai = FALSE;
6810#ifdef FEAT_SMARTINDENT
6811 did_si = FALSE;
6812 can_si = FALSE;
6813 can_si_back = FALSE;
6814#endif
6815 line_breakcheck();
6816 }
6817
6818 if (save_char != NUL) /* put back space after cursor */
6819 pchar_cursor(save_char);
6820
Bram Moolenaar0026d472014-09-09 16:32:39 +02006821#ifdef FEAT_LINEBREAK
6822 curwin->w_p_lbr = has_lbr;
6823#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006824 if (!format_only && haveto_redraw)
6825 {
6826 update_topline();
6827 redraw_curbuf_later(VALID);
6828 }
6829}
6830
6831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 * Called after inserting or deleting text: When 'formatoptions' includes the
6833 * 'a' flag format from the current line until the end of the paragraph.
6834 * Keep the cursor at the same position relative to the text.
6835 * The caller must have saved the cursor line for undo, following ones will be
6836 * saved here.
6837 */
6838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839auto_format(
6840 int trailblank, /* when TRUE also format with trailing blank */
6841 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 pos_T pos;
6844 colnr_T len;
6845 char_u *old;
6846 char_u *new, *pnew;
6847 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006848 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
6850 if (!has_format_option(FO_AUTO))
6851 return;
6852
6853 pos = curwin->w_cursor;
6854 old = ml_get_curline();
6855
6856 /* may remove added space */
6857 check_auto_format(FALSE);
6858
6859 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6860 * user might insert normal text next. Also skip formatting when "1" is
6861 * in 'formatoptions' and there is a single character before the cursor.
6862 * Otherwise the line would be broken and when typing another non-white
6863 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006864 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (*old != NUL && !trailblank && wasatend)
6866 {
6867 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006868 cc = gchar_cursor();
6869 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6870 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006872 cc = gchar_cursor();
6873 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {
6875 curwin->w_cursor = pos;
6876 return;
6877 }
6878 curwin->w_cursor = pos;
6879 }
6880
6881#ifdef FEAT_COMMENTS
6882 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6883 * comments. */
6884 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006885 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 return;
6887#endif
6888
6889 /*
6890 * May start formatting in a previous line, so that after "x" a word is
6891 * moved to the previous line if it fits there now. Only when this is not
6892 * the start of a paragraph.
6893 */
6894 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6895 {
6896 --curwin->w_cursor.lnum;
6897 if (u_save_cursor() == FAIL)
6898 return;
6899 }
6900
6901 /*
6902 * Do the formatting and restore the cursor position. "saved_cursor" will
6903 * be adjusted for the text formatting.
6904 */
6905 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006906 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 curwin->w_cursor = saved_cursor;
6908 saved_cursor.lnum = 0;
6909
6910 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6911 {
6912 /* "cannot happen" */
6913 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6914 coladvance((colnr_T)MAXCOL);
6915 }
6916 else
6917 check_cursor_col();
6918
6919 /* Insert mode: If the cursor is now after the end of the line while it
6920 * previously wasn't, the line was broken. Because of the rule above we
6921 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6922 * formatted. */
6923 if (!wasatend && has_format_option(FO_WHITE_PAR))
6924 {
6925 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006926 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 if (curwin->w_cursor.col == len)
6928 {
6929 pnew = vim_strnsave(new, len + 2);
6930 pnew[len] = ' ';
6931 pnew[len + 1] = NUL;
6932 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6933 /* remove the space later */
6934 did_add_space = TRUE;
6935 }
6936 else
6937 /* may remove added space */
6938 check_auto_format(FALSE);
6939 }
6940
6941 check_cursor();
6942}
6943
6944/*
6945 * When an extra space was added to continue a paragraph for auto-formatting,
6946 * delete it now. The space must be under the cursor, just after the insert
6947 * position.
6948 */
6949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006950check_auto_format(
6951 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
6953 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006954 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 if (did_add_space)
6957 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006958 cc = gchar_cursor();
6959 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 /* Somehow the space was removed already. */
6961 did_add_space = FALSE;
6962 else
6963 {
6964 if (!end_insert)
6965 {
6966 inc_cursor();
6967 c = gchar_cursor();
6968 dec_cursor();
6969 }
6970 if (c != NUL)
6971 {
6972 /* The space is no longer at the end of the line, delete it. */
6973 del_char(FALSE);
6974 did_add_space = FALSE;
6975 }
6976 }
6977 }
6978}
6979
6980/*
6981 * Find out textwidth to be used for formatting:
6982 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006983 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 * if invalid value, use 0.
6985 * Set default to window width (maximum 79) for "gq" operator.
6986 */
6987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006988comp_textwidth(
6989 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
6991 int textwidth;
6992
6993 textwidth = curbuf->b_p_tw;
6994 if (textwidth == 0 && curbuf->b_p_wm)
6995 {
6996 /* The width is the window width minus 'wrapmargin' minus all the
6997 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006998 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999#ifdef FEAT_CMDWIN
7000 if (cmdwin_type != 0)
7001 textwidth -= 1;
7002#endif
7003#ifdef FEAT_FOLDING
7004 textwidth -= curwin->w_p_fdc;
7005#endif
7006#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007007 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 textwidth -= 1;
7009#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02007010 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 textwidth -= 8;
7012 }
7013 if (textwidth < 0)
7014 textwidth = 0;
7015 if (ff && textwidth == 0)
7016 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007017 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 if (textwidth > 79)
7019 textwidth = 79;
7020 }
7021 return textwidth;
7022}
7023
7024/*
7025 * Put a character in the redo buffer, for when just after a CTRL-V.
7026 */
7027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007028redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 char_u buf[10];
7031
7032 /* Only digits need special treatment. Translate them into a string of
7033 * three digits. */
7034 if (VIM_ISDIGIT(c))
7035 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007036 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 AppendToRedobuff(buf);
7038 }
7039 else
7040 AppendCharToRedobuff(c);
7041}
7042
7043/*
7044 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007045 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 */
7047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007048start_arrow(
7049 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007051 start_arrow_common(end_insert_pos, TRUE);
7052}
7053
7054/*
7055 * Like start_arrow() but with end_change argument.
7056 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7057 */
7058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007059start_arrow_with_change(
7060 pos_T *end_insert_pos, /* can be NULL */
7061 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007062{
7063 start_arrow_common(end_insert_pos, end_change);
7064 if (!end_change)
7065 {
7066 AppendCharToRedobuff(Ctrl_G);
7067 AppendCharToRedobuff('U');
7068 }
7069}
7070
7071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072start_arrow_common(
7073 pos_T *end_insert_pos, /* can be NULL */
7074 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007075{
7076 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {
7078 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007079 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 arrow_used = TRUE; /* this means we stopped the current insert */
7081 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007082#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007083 check_spell_redraw();
7084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085}
7086
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007087#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007088/*
7089 * If we skipped highlighting word at cursor, do it now.
7090 * It may be skipped again, thus reset spell_redraw_lnum first.
7091 */
7092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007093check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007094{
7095 if (spell_redraw_lnum != 0)
7096 {
7097 linenr_T lnum = spell_redraw_lnum;
7098
7099 spell_redraw_lnum = 0;
7100 redrawWinline(lnum, FALSE);
7101 }
7102}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007103
7104/*
7105 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7106 * spelled word, if there is one.
7107 */
7108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007110{
7111 pos_T tpos = curwin->w_cursor;
7112
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007113 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007114 if (curwin->w_cursor.col != tpos.col)
7115 start_arrow(&tpos);
7116}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007117#endif
7118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119/*
7120 * stop_arrow() is called before a change is made in insert mode.
7121 * If an arrow key has been used, start a new insertion.
7122 * Returns FAIL if undo is impossible, shouldn't insert then.
7123 */
7124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007125stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
7127 if (arrow_used)
7128 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007129 Insstart = curwin->w_cursor; /* new insertion starts here */
7130 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7131 /* Don't update the original insert position when moved to the
7132 * right, except when nothing was inserted yet. */
7133 update_Insstart_orig = FALSE;
7134 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7135
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (u_save_cursor() == OK)
7137 {
7138 arrow_used = FALSE;
7139 ins_need_undo = FALSE;
7140 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007141
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 ai_col = 0;
7143#ifdef FEAT_VREPLACE
7144 if (State & VREPLACE_FLAG)
7145 {
7146 orig_line_count = curbuf->b_ml.ml_line_count;
7147 vr_lines_changed = 1;
7148 }
7149#endif
7150 ResetRedobuff();
7151 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007152 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
7154 else if (ins_need_undo)
7155 {
7156 if (u_save_cursor() == OK)
7157 ins_need_undo = FALSE;
7158 }
7159
7160#ifdef FEAT_FOLDING
7161 /* Always open fold at the cursor line when inserting something. */
7162 foldOpenCursor();
7163#endif
7164
7165 return (arrow_used || ins_need_undo ? FAIL : OK);
7166}
7167
7168/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007169 * Do a few things to stop inserting.
7170 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7171 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 */
7173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007174stop_insert(
7175 pos_T *end_insert_pos,
7176 int esc, /* called by ins_esc() */
7177 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007179 int cc;
7180 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
7182 stop_redo_ins();
7183 replace_flush(); /* abandon replace stack */
7184
7185 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007186 * Save the inserted text for later redo with ^@ and CTRL-A.
7187 * Don't do it when "restart_edit" was set and nothing was inserted,
7188 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007190 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007191 if (did_restart_edit == 0 || (ptr != NULL
7192 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007193 {
7194 vim_free(last_insert);
7195 last_insert = ptr;
7196 last_insert_skip = new_insert_skip;
7197 }
7198 else
7199 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007201 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
7203 /* Auto-format now. It may seem strange to do this when stopping an
7204 * insertion (or moving the cursor), but it's required when appending
7205 * a line and having it end in a space. But only do it when something
7206 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007207 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007209 pos_T tpos = curwin->w_cursor;
7210
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 /* When the cursor is at the end of the line after a space the
7212 * formatting will move it to the following word. Avoid that by
7213 * moving the cursor onto the space. */
7214 cc = 'x';
7215 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7216 {
7217 dec_cursor();
7218 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007219 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007220 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222
7223 auto_format(TRUE, FALSE);
7224
Bram Moolenaar1c465442017-03-12 20:10:05 +01007225 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007226 {
7227 if (gchar_cursor() != NUL)
7228 inc_cursor();
7229#ifdef FEAT_VIRTUALEDIT
7230 /* If the cursor is still at the same character, also keep
7231 * the "coladd". */
7232 if (gchar_cursor() == NUL
7233 && curwin->w_cursor.lnum == tpos.lnum
7234 && curwin->w_cursor.col == tpos.col)
7235 curwin->w_cursor.coladd = tpos.coladd;
7236#endif
7237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239
7240 /* If a space was inserted for auto-formatting, remove it now. */
7241 check_auto_format(TRUE);
7242
7243 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007244 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007245 * Do this when ESC was used or moving the cursor up/down.
7246 * Check for the old position still being valid, just in case the text
7247 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007248 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007249 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7250 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007252 pos_T tpos = curwin->w_cursor;
7253
7254 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007255 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007256 for (;;)
7257 {
7258 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7259 --curwin->w_cursor.col;
7260 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007261 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007262 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007263 if (del_char(TRUE) == FAIL)
7264 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007265 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007266 if (curwin->w_cursor.lnum != tpos.lnum)
7267 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007268 else
7269 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007270 /* reset tpos, could have been invalidated in the loop above */
7271 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007272 tpos.col++;
7273 if (cc != NUL && gchar_pos(&tpos) == NUL)
7274 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 /* <C-S-Right> may have started Visual mode, adjust the position for
7278 * deleted characters. */
7279 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7280 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007281 int len = (int)STRLEN(ml_get_curline());
7282
7283 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007285 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007286#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292 }
7293 did_ai = FALSE;
7294#ifdef FEAT_SMARTINDENT
7295 did_si = FALSE;
7296 can_si = FALSE;
7297 can_si_back = FALSE;
7298#endif
7299
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007300 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7301 * now in a different buffer. */
7302 if (end_insert_pos != NULL)
7303 {
7304 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007305 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007306 curbuf->b_op_end = *end_insert_pos;
7307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308}
7309
7310/*
7311 * Set the last inserted text to a single character.
7312 * Used for the replace command.
7313 */
7314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007315set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
7317 char_u *s;
7318
7319 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 if (last_insert != NULL)
7322 {
7323 s = last_insert;
7324 /* Use the CTRL-V only when entering a special char */
7325 if (c < ' ' || c == DEL)
7326 *s++ = Ctrl_V;
7327 s = add_char2buf(c, s);
7328 *s++ = ESC;
7329 *s++ = NUL;
7330 last_insert_skip = 0;
7331 }
7332}
7333
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007334#if defined(EXITFREE) || defined(PROTO)
7335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007336free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007337{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007338 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007339# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007340 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007341# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007342}
7343#endif
7344
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345/*
7346 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7347 * and CSI. Handle multi-byte characters.
7348 * Returns a pointer to after the added bytes.
7349 */
7350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007351add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352{
7353#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007354 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 int i;
7356 int len;
7357
7358 len = (*mb_char2bytes)(c, temp);
7359 for (i = 0; i < len; ++i)
7360 {
7361 c = temp[i];
7362#endif
7363 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7364 if (c == K_SPECIAL)
7365 {
7366 *s++ = K_SPECIAL;
7367 *s++ = KS_SPECIAL;
7368 *s++ = KE_FILLER;
7369 }
7370#ifdef FEAT_GUI
7371 else if (c == CSI)
7372 {
7373 *s++ = CSI;
7374 *s++ = KS_EXTRA;
7375 *s++ = (int)KE_CSI;
7376 }
7377#endif
7378 else
7379 *s++ = c;
7380#ifdef FEAT_MBYTE
7381 }
7382#endif
7383 return s;
7384}
7385
7386/*
7387 * move cursor to start of line
7388 * if flags & BL_WHITE move to first non-white
7389 * if flags & BL_SOL move to first non-white if startofline is set,
7390 * otherwise keep "curswant" column
7391 * if flags & BL_FIX don't leave the cursor on a NUL.
7392 */
7393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007394beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 if ((flags & BL_SOL) && !p_sol)
7397 coladvance(curwin->w_curswant);
7398 else
7399 {
7400 curwin->w_cursor.col = 0;
7401#ifdef FEAT_VIRTUALEDIT
7402 curwin->w_cursor.coladd = 0;
7403#endif
7404
7405 if (flags & (BL_WHITE | BL_SOL))
7406 {
7407 char_u *ptr;
7408
Bram Moolenaar1c465442017-03-12 20:10:05 +01007409 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7411 ++curwin->w_cursor.col;
7412 }
7413 curwin->w_set_curswant = TRUE;
7414 }
7415}
7416
7417/*
7418 * oneright oneleft cursor_down cursor_up
7419 *
7420 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007421 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 * Return OK when successful, FAIL when we hit a line of file boundary.
7423 */
7424
7425 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007426oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431#ifdef FEAT_VIRTUALEDIT
7432 if (virtual_active())
7433 {
7434 pos_T prevpos = curwin->w_cursor;
7435
7436 /* Adjust for multi-wide char (excluding TAB) */
7437 ptr = ml_get_cursor();
7438 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007439# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007441# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 ))
7445 ? ptr2cells(ptr) : 1));
7446 curwin->w_set_curswant = TRUE;
7447 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7448 return (prevpos.col != curwin->w_cursor.col
7449 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7450 }
7451#endif
7452
7453 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007454 if (*ptr == NUL)
7455 return FAIL; /* already at the very end */
7456
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007458 if (has_mbyte)
7459 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 else
7461#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007462 l = 1;
7463
7464 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7465 * contains "onemore". */
7466 if (ptr[l] == NUL
7467#ifdef FEAT_VIRTUALEDIT
7468 && (ve_flags & VE_ONEMORE) == 0
7469#endif
7470 )
7471 return FAIL;
7472 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
7474 curwin->w_set_curswant = TRUE;
7475 return OK;
7476}
7477
7478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007479oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481#ifdef FEAT_VIRTUALEDIT
7482 if (virtual_active())
7483 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007484# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 int v = getviscol();
7488
7489 if (v == 0)
7490 return FAIL;
7491
7492# ifdef FEAT_LINEBREAK
7493 /* We might get stuck on 'showbreak', skip over it. */
7494 width = 1;
7495 for (;;)
7496 {
7497 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007498 /* getviscol() is slow, skip it when 'showbreak' is empty,
7499 * 'breakindent' is not set and there are no multi-byte
7500 * characters */
7501 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502# ifdef FEAT_MBYTE
7503 && !has_mbyte
7504# endif
7505 ) || getviscol() < v)
7506 break;
7507 ++width;
7508 }
7509# else
7510 coladvance(v - 1);
7511# endif
7512
7513 if (curwin->w_cursor.coladd == 1)
7514 {
7515 char_u *ptr;
7516
7517 /* Adjust for multi-wide char (not a TAB) */
7518 ptr = ml_get_cursor();
7519 if (*ptr != TAB && vim_isprintc(
7520# ifdef FEAT_MBYTE
7521 (*mb_ptr2char)(ptr)
7522# else
7523 *ptr
7524# endif
7525 ) && ptr2cells(ptr) > 1)
7526 curwin->w_cursor.coladd = 0;
7527 }
7528
7529 curwin->w_set_curswant = TRUE;
7530 return OK;
7531 }
7532#endif
7533
7534 if (curwin->w_cursor.col == 0)
7535 return FAIL;
7536
7537 curwin->w_set_curswant = TRUE;
7538 --curwin->w_cursor.col;
7539
7540#ifdef FEAT_MBYTE
7541 /* if the character on the left of the current cursor is a multi-byte
7542 * character, move to its first byte */
7543 if (has_mbyte)
7544 mb_adjust_cursor();
7545#endif
7546 return OK;
7547}
7548
7549 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007550cursor_up(
7551 long n,
7552 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
7554 linenr_T lnum;
7555
7556 if (n > 0)
7557 {
7558 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007559 /* This fails if the cursor is already in the first line or the count
7560 * is larger than the line number and '-' is in 'cpoptions' */
7561 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 return FAIL;
7563 if (n >= lnum)
7564 lnum = 1;
7565 else
7566#ifdef FEAT_FOLDING
7567 if (hasAnyFolding(curwin))
7568 {
7569 /*
7570 * Count each sequence of folded lines as one logical line.
7571 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007572 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 (void)hasFolding(lnum, &lnum, NULL);
7574
7575 while (n--)
7576 {
7577 /* move up one line */
7578 --lnum;
7579 if (lnum <= 1)
7580 break;
7581 /* If we entered a fold, move to the beginning, unless in
7582 * Insert mode or when 'foldopen' contains "all": it will open
7583 * in a moment. */
7584 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7585 (void)hasFolding(lnum, &lnum, NULL);
7586 }
7587 if (lnum < 1)
7588 lnum = 1;
7589 }
7590 else
7591#endif
7592 lnum -= n;
7593 curwin->w_cursor.lnum = lnum;
7594 }
7595
7596 /* try to advance to the column we want to be at */
7597 coladvance(curwin->w_curswant);
7598
7599 if (upd_topline)
7600 update_topline(); /* make sure curwin->w_topline is valid */
7601
7602 return OK;
7603}
7604
7605/*
7606 * Cursor down a number of logical lines.
7607 */
7608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007609cursor_down(
7610 long n,
7611 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613 linenr_T lnum;
7614
7615 if (n > 0)
7616 {
7617 lnum = curwin->w_cursor.lnum;
7618#ifdef FEAT_FOLDING
7619 /* Move to last line of fold, will fail if it's the end-of-file. */
7620 (void)hasFolding(lnum, NULL, &lnum);
7621#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007622 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007623 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007624 if (lnum >= curbuf->b_ml.ml_line_count
7625 || (lnum + n > curbuf->b_ml.ml_line_count
7626 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 return FAIL;
7628 if (lnum + n >= curbuf->b_ml.ml_line_count)
7629 lnum = curbuf->b_ml.ml_line_count;
7630 else
7631#ifdef FEAT_FOLDING
7632 if (hasAnyFolding(curwin))
7633 {
7634 linenr_T last;
7635
7636 /* count each sequence of folded lines as one logical line */
7637 while (n--)
7638 {
7639 if (hasFolding(lnum, NULL, &last))
7640 lnum = last + 1;
7641 else
7642 ++lnum;
7643 if (lnum >= curbuf->b_ml.ml_line_count)
7644 break;
7645 }
7646 if (lnum > curbuf->b_ml.ml_line_count)
7647 lnum = curbuf->b_ml.ml_line_count;
7648 }
7649 else
7650#endif
7651 lnum += n;
7652 curwin->w_cursor.lnum = lnum;
7653 }
7654
7655 /* try to advance to the column we want to be at */
7656 coladvance(curwin->w_curswant);
7657
7658 if (upd_topline)
7659 update_topline(); /* make sure curwin->w_topline is valid */
7660
7661 return OK;
7662}
7663
7664/*
7665 * Stuff the last inserted text in the read buffer.
7666 * Last_insert actually is a copy of the redo buffer, so we
7667 * first have to remove the command.
7668 */
7669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007670stuff_inserted(
7671 int c, /* Command character to be inserted */
7672 long count, /* Repeat this many times */
7673 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675 char_u *esc_ptr;
7676 char_u *ptr;
7677 char_u *last_ptr;
7678 char_u last = NUL;
7679
7680 ptr = get_last_insert();
7681 if (ptr == NULL)
7682 {
7683 EMSG(_(e_noinstext));
7684 return FAIL;
7685 }
7686
7687 /* may want to stuff the command character, to start Insert mode */
7688 if (c != NUL)
7689 stuffcharReadbuff(c);
7690 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7691 *esc_ptr = NUL; /* remove the ESC */
7692
7693 /* when the last char is either "0" or "^" it will be quoted if no ESC
7694 * comes after it OR if it will inserted more than once and "ptr"
7695 * starts with ^D. -- Acevedo
7696 */
7697 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7698 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7699 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7700 {
7701 last = *last_ptr;
7702 *last_ptr = NUL;
7703 }
7704
7705 do
7706 {
7707 stuffReadbuff(ptr);
7708 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7709 if (last)
7710 stuffReadbuff((char_u *)(last == '0'
7711 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7712 : IF_EB("\026^", CTRL_V_STR "^")));
7713 }
7714 while (--count > 0);
7715
7716 if (last)
7717 *last_ptr = last;
7718
7719 if (esc_ptr != NULL)
7720 *esc_ptr = ESC; /* put the ESC back */
7721
7722 /* may want to stuff a trailing ESC, to get out of Insert mode */
7723 if (!no_esc)
7724 stuffcharReadbuff(ESC);
7725
7726 return OK;
7727}
7728
7729 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007730get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
7732 if (last_insert == NULL)
7733 return NULL;
7734 return last_insert + last_insert_skip;
7735}
7736
7737/*
7738 * Get last inserted string, and remove trailing <Esc>.
7739 * Returns pointer to allocated memory (must be freed) or NULL.
7740 */
7741 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007742get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
7744 char_u *s;
7745 int len;
7746
7747 if (last_insert == NULL)
7748 return NULL;
7749 s = vim_strsave(last_insert + last_insert_skip);
7750 if (s != NULL)
7751 {
7752 len = (int)STRLEN(s);
7753 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7754 s[len - 1] = NUL;
7755 }
7756 return s;
7757}
7758
7759/*
7760 * Check the word in front of the cursor for an abbreviation.
7761 * Called when the non-id character "c" has been entered.
7762 * When an abbreviation is recognized it is removed from the text and
7763 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7764 */
7765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007766echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
7768 /* Don't check for abbreviation in paste mode, when disabled and just
7769 * after moving around with cursor keys. */
7770 if (p_paste || no_abbr || arrow_used)
7771 return FALSE;
7772
7773 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7774 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7775}
7776
7777/*
7778 * replace-stack functions
7779 *
7780 * When replacing characters, the replaced characters are remembered for each
7781 * new character. This is used to re-insert the old text when backspacing.
7782 *
7783 * There is a NUL headed list of characters for each character that is
7784 * currently in the file after the insertion point. When BS is used, one NUL
7785 * headed list is put back for the deleted character.
7786 *
7787 * For a newline, there are two NUL headed lists. One contains the characters
7788 * that the NL replaced. The extra one stores the characters after the cursor
7789 * that were deleted (always white space).
7790 *
7791 * Replace_offset is normally 0, in which case replace_push will add a new
7792 * character at the end of the stack. If replace_offset is not 0, that many
7793 * characters will be left on the stack above the newly inserted character.
7794 */
7795
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007796static char_u *replace_stack = NULL;
7797static long replace_stack_nr = 0; /* next entry in replace stack */
7798static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799
7800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007801replace_push(
7802 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 char_u *p;
7805
7806 if (replace_stack_nr < replace_offset) /* nothing to do */
7807 return;
7808 if (replace_stack_len <= replace_stack_nr)
7809 {
7810 replace_stack_len += 50;
7811 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7812 if (p == NULL) /* out of memory */
7813 {
7814 replace_stack_len -= 50;
7815 return;
7816 }
7817 if (replace_stack != NULL)
7818 {
7819 mch_memmove(p, replace_stack,
7820 (size_t)(replace_stack_nr * sizeof(char_u)));
7821 vim_free(replace_stack);
7822 }
7823 replace_stack = p;
7824 }
7825 p = replace_stack + replace_stack_nr - replace_offset;
7826 if (replace_offset)
7827 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7828 *p = c;
7829 ++replace_stack_nr;
7830}
7831
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007832#if defined(FEAT_MBYTE) || defined(PROTO)
7833/*
7834 * Push a character onto the replace stack. Handles a multi-byte character in
7835 * reverse byte order, so that the first byte is popped off first.
7836 * Return the number of bytes done (includes composing characters).
7837 */
7838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007839replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007840{
7841 int l = (*mb_ptr2len)(p);
7842 int j;
7843
7844 for (j = l - 1; j >= 0; --j)
7845 replace_push(p[j]);
7846 return l;
7847}
7848#endif
7849
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850/*
7851 * Pop one item from the replace stack.
7852 * return -1 if stack empty
7853 * return replaced character or NUL otherwise
7854 */
7855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007856replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858 if (replace_stack_nr == 0)
7859 return -1;
7860 return (int)replace_stack[--replace_stack_nr];
7861}
7862
7863/*
7864 * Join the top two items on the replace stack. This removes to "off"'th NUL
7865 * encountered.
7866 */
7867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007868replace_join(
7869 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
7871 int i;
7872
7873 for (i = replace_stack_nr; --i >= 0; )
7874 if (replace_stack[i] == NUL && off-- <= 0)
7875 {
7876 --replace_stack_nr;
7877 mch_memmove(replace_stack + i, replace_stack + i + 1,
7878 (size_t)(replace_stack_nr - i));
7879 return;
7880 }
7881}
7882
7883/*
7884 * Pop bytes from the replace stack until a NUL is found, and insert them
7885 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7886 */
7887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007888replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 int cc;
7891 int oldState = State;
7892
7893 State = NORMAL; /* don't want REPLACE here */
7894 while ((cc = replace_pop()) > 0)
7895 {
7896#ifdef FEAT_MBYTE
7897 mb_replace_pop_ins(cc);
7898#else
7899 ins_char(cc);
7900#endif
7901 dec_cursor();
7902 }
7903 State = oldState;
7904}
7905
7906#ifdef FEAT_MBYTE
7907/*
7908 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7909 * indicates a multi-byte char, pop the other bytes too.
7910 */
7911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007912mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007915 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 int i;
7917 int c;
7918
7919 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7920 {
7921 buf[0] = cc;
7922 for (i = 1; i < n; ++i)
7923 buf[i] = replace_pop();
7924 ins_bytes_len(buf, n);
7925 }
7926 else
7927 ins_char(cc);
7928
7929 if (enc_utf8)
7930 /* Handle composing chars. */
7931 for (;;)
7932 {
7933 c = replace_pop();
7934 if (c == -1) /* stack empty */
7935 break;
7936 if ((n = MB_BYTE2LEN(c)) == 1)
7937 {
7938 /* Not a multi-byte char, put it back. */
7939 replace_push(c);
7940 break;
7941 }
7942 else
7943 {
7944 buf[0] = c;
7945 for (i = 1; i < n; ++i)
7946 buf[i] = replace_pop();
7947 if (utf_iscomposing(utf_ptr2char(buf)))
7948 ins_bytes_len(buf, n);
7949 else
7950 {
7951 /* Not a composing char, put it back. */
7952 for (i = n - 1; i >= 0; --i)
7953 replace_push(buf[i]);
7954 break;
7955 }
7956 }
7957 }
7958}
7959#endif
7960
7961/*
7962 * make the replace stack empty
7963 * (called when exiting replace mode)
7964 */
7965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007966replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007968 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 replace_stack_len = 0;
7970 replace_stack_nr = 0;
7971}
7972
7973/*
7974 * Handle doing a BS for one character.
7975 * cc < 0: replace stack empty, just move cursor
7976 * cc == 0: character was inserted, delete it
7977 * cc > 0: character was replaced, put cc (first byte of original char) back
7978 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007979 * When "limit_col" is >= 0, don't delete before this column. Matters when
7980 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 */
7982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007983replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985 int cc;
7986#ifdef FEAT_VREPLACE
7987 int orig_len = 0;
7988 int ins_len;
7989 int orig_vcols = 0;
7990 colnr_T start_vcol;
7991 char_u *p;
7992 int i;
7993 int vcol;
7994#endif
7995
7996 cc = replace_pop();
7997 if (cc > 0)
7998 {
7999#ifdef FEAT_VREPLACE
8000 if (State & VREPLACE_FLAG)
8001 {
8002 /* Get the number of screen cells used by the character we are
8003 * going to delete. */
8004 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
8005 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
8006 }
8007#endif
8008#ifdef FEAT_MBYTE
8009 if (has_mbyte)
8010 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008011 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012# ifdef FEAT_VREPLACE
8013 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008014 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015# endif
8016 replace_push(cc);
8017 }
8018 else
8019#endif
8020 {
8021 pchar_cursor(cc);
8022#ifdef FEAT_VREPLACE
8023 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008024 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025#endif
8026 }
8027 replace_pop_ins();
8028
8029#ifdef FEAT_VREPLACE
8030 if (State & VREPLACE_FLAG)
8031 {
8032 /* Get the number of screen cells used by the inserted characters */
8033 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008034 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 vcol = start_vcol;
8036 for (i = 0; i < ins_len; ++i)
8037 {
8038 vcol += chartabsize(p + i, vcol);
8039#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008040 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041#endif
8042 }
8043 vcol -= start_vcol;
8044
8045 /* Delete spaces that were inserted after the cursor to keep the
8046 * text aligned. */
8047 curwin->w_cursor.col += ins_len;
8048 while (vcol > orig_vcols && gchar_cursor() == ' ')
8049 {
8050 del_char(FALSE);
8051 ++orig_vcols;
8052 }
8053 curwin->w_cursor.col -= ins_len;
8054 }
8055#endif
8056
8057 /* mark the buffer as changed and prepare for displaying */
8058 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8059 }
8060 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008061 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062}
8063
8064#ifdef FEAT_CINDENT
8065/*
8066 * Return TRUE if C-indenting is on.
8067 */
8068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008069cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070{
8071 return (!p_paste && (curbuf->b_p_cin
8072# ifdef FEAT_EVAL
8073 || *curbuf->b_p_inde != NUL
8074# endif
8075 ));
8076}
8077#endif
8078
8079#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8080/*
8081 * Re-indent the current line, based on the current contents of it and the
8082 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8083 * confused what all the part that handles Control-T is doing that I'm not.
8084 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8085 */
8086
8087 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008088fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008090 int amount = get_the_indent();
8091
8092 if (amount >= 0)
8093 {
8094 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8095 if (linewhite(curwin->w_cursor.lnum))
8096 did_ai = TRUE; /* delete the indent if the line stays empty */
8097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098}
8099
8100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008101fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 if (p_paste)
8104 return;
8105# ifdef FEAT_LISP
8106 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8107 fixthisline(get_lisp_indent);
8108# endif
8109# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8110 else
8111# endif
8112# ifdef FEAT_CINDENT
8113 if (cindent_on())
8114 do_c_expr_indent();
8115# endif
8116}
8117
8118#endif
8119
8120#ifdef FEAT_CINDENT
8121/*
8122 * return TRUE if 'cinkeys' contains the key "keytyped",
8123 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008124 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8126 *
8127 * "keytyped" can have a few special values:
8128 * KEY_OPEN_FORW
8129 * KEY_OPEN_BACK
8130 * KEY_COMPLETE just finished completion.
8131 *
8132 * If line_is_empty is TRUE accept keys with '0' before them.
8133 */
8134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008135in_cinkeys(
8136 int keytyped,
8137 int when,
8138 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140 char_u *look;
8141 int try_match;
8142 int try_match_word;
8143 char_u *p;
8144 char_u *line;
8145 int icase;
8146 int i;
8147
Bram Moolenaar30848942009-12-24 14:46:12 +00008148 if (keytyped == NUL)
8149 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8150 return FALSE;
8151
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152#ifdef FEAT_EVAL
8153 if (*curbuf->b_p_inde != NUL)
8154 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8155 else
8156#endif
8157 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8158 while (*look)
8159 {
8160 /*
8161 * Find out if we want to try a match with this key, depending on
8162 * 'when' and a '*' or '!' before the key.
8163 */
8164 switch (when)
8165 {
8166 case '*': try_match = (*look == '*'); break;
8167 case '!': try_match = (*look == '!'); break;
8168 default: try_match = (*look != '*'); break;
8169 }
8170 if (*look == '*' || *look == '!')
8171 ++look;
8172
8173 /*
8174 * If there is a '0', only accept a match if the line is empty.
8175 * But may still match when typing last char of a word.
8176 */
8177 if (*look == '0')
8178 {
8179 try_match_word = try_match;
8180 if (!line_is_empty)
8181 try_match = FALSE;
8182 ++look;
8183 }
8184 else
8185 try_match_word = FALSE;
8186
8187 /*
8188 * does it look like a control character?
8189 */
8190 if (*look == '^'
8191#ifdef EBCDIC
8192 && (Ctrl_chr(look[1]) != 0)
8193#else
8194 && look[1] >= '?' && look[1] <= '_'
8195#endif
8196 )
8197 {
8198 if (try_match && keytyped == Ctrl_chr(look[1]))
8199 return TRUE;
8200 look += 2;
8201 }
8202 /*
8203 * 'o' means "o" command, open forward.
8204 * 'O' means "O" command, open backward.
8205 */
8206 else if (*look == 'o')
8207 {
8208 if (try_match && keytyped == KEY_OPEN_FORW)
8209 return TRUE;
8210 ++look;
8211 }
8212 else if (*look == 'O')
8213 {
8214 if (try_match && keytyped == KEY_OPEN_BACK)
8215 return TRUE;
8216 ++look;
8217 }
8218
8219 /*
8220 * 'e' means to check for "else" at start of line and just before the
8221 * cursor.
8222 */
8223 else if (*look == 'e')
8224 {
8225 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8226 {
8227 p = ml_get_curline();
8228 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8229 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8230 return TRUE;
8231 }
8232 ++look;
8233 }
8234
8235 /*
8236 * ':' only causes an indent if it is at the end of a label or case
8237 * statement, or when it was before typing the ':' (to fix
8238 * class::method for C++).
8239 */
8240 else if (*look == ':')
8241 {
8242 if (try_match && keytyped == ':')
8243 {
8244 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008245 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008247 /* Need to get the line again after cin_islabel(). */
8248 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (curwin->w_cursor.col > 2
8250 && p[curwin->w_cursor.col - 1] == ':'
8251 && p[curwin->w_cursor.col - 2] == ':')
8252 {
8253 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008254 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008255 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 p = ml_get_curline();
8257 p[curwin->w_cursor.col - 1] = ':';
8258 if (i)
8259 return TRUE;
8260 }
8261 }
8262 ++look;
8263 }
8264
8265
8266 /*
8267 * Is it a key in <>, maybe?
8268 */
8269 else if (*look == '<')
8270 {
8271 if (try_match)
8272 {
8273 /*
8274 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8275 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8276 * >, *, : and ! keys if they really really want to.
8277 */
8278 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8279 && keytyped == look[1])
8280 return TRUE;
8281
8282 if (keytyped == get_special_key_code(look + 1))
8283 return TRUE;
8284 }
8285 while (*look && *look != '>')
8286 look++;
8287 while (*look == '>')
8288 look++;
8289 }
8290
8291 /*
8292 * Is it a word: "=word"?
8293 */
8294 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8295 {
8296 ++look;
8297 if (*look == '~')
8298 {
8299 icase = TRUE;
8300 ++look;
8301 }
8302 else
8303 icase = FALSE;
8304 p = vim_strchr(look, ',');
8305 if (p == NULL)
8306 p = look + STRLEN(look);
8307 if ((try_match || try_match_word)
8308 && curwin->w_cursor.col >= (colnr_T)(p - look))
8309 {
8310 int match = FALSE;
8311
8312#ifdef FEAT_INS_EXPAND
8313 if (keytyped == KEY_COMPLETE)
8314 {
8315 char_u *s;
8316
8317 /* Just completed a word, check if it starts with "look".
8318 * search back for the start of a word. */
8319 line = ml_get_curline();
8320# ifdef FEAT_MBYTE
8321 if (has_mbyte)
8322 {
8323 char_u *n;
8324
8325 for (s = line + curwin->w_cursor.col; s > line; s = n)
8326 {
8327 n = mb_prevptr(line, s);
8328 if (!vim_iswordp(n))
8329 break;
8330 }
8331 }
8332 else
8333# endif
8334 for (s = line + curwin->w_cursor.col; s > line; --s)
8335 if (!vim_iswordc(s[-1]))
8336 break;
8337 if (s + (p - look) <= line + curwin->w_cursor.col
8338 && (icase
8339 ? MB_STRNICMP(s, look, p - look)
8340 : STRNCMP(s, look, p - look)) == 0)
8341 match = TRUE;
8342 }
8343 else
8344#endif
8345 /* TODO: multi-byte */
8346 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8347 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8348 {
8349 line = ml_get_cursor();
8350 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8351 || !vim_iswordc(line[-(p - look) - 1]))
8352 && (icase
8353 ? MB_STRNICMP(line - (p - look), look, p - look)
8354 : STRNCMP(line - (p - look), look, p - look))
8355 == 0)
8356 match = TRUE;
8357 }
8358 if (match && try_match_word && !try_match)
8359 {
8360 /* "0=word": Check if there are only blanks before the
8361 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008362 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 (int)(curwin->w_cursor.col - (p - look)))
8364 match = FALSE;
8365 }
8366 if (match)
8367 return TRUE;
8368 }
8369 look = p;
8370 }
8371
8372 /*
8373 * ok, it's a boring generic character.
8374 */
8375 else
8376 {
8377 if (try_match && *look == keytyped)
8378 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008379 if (*look != NUL)
8380 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 }
8382
8383 /*
8384 * Skip over ", ".
8385 */
8386 look = skip_to_option_part(look);
8387 }
8388 return FALSE;
8389}
8390#endif /* FEAT_CINDENT */
8391
8392#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8393/*
8394 * Map Hebrew keyboard when in hkmap mode.
8395 */
8396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008397hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
8399 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8400 {
8401 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8402 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8403 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8404 static char_u map[26] =
8405 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8406 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8407 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8408 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8409 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8410 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8411 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8412 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8413 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8414
8415 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8416 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8417 /* '-1'='sofit' */
8418 else if (c == 'x')
8419 return 'X';
8420 else if (c == 'q')
8421 return '\''; /* {geresh}={'} */
8422 else if (c == 246)
8423 return ' '; /* \"o --> ' ' for a german keyboard */
8424 else if (c == 228)
8425 return ' '; /* \"a --> ' ' -- / -- */
8426 else if (c == 252)
8427 return ' '; /* \"u --> ' ' -- / -- */
8428#ifdef EBCDIC
8429 else if (islower(c))
8430#else
8431 /* NOTE: islower() does not do the right thing for us on Linux so we
8432 * do this the same was as 5.7 and previous, so it works correctly on
8433 * all systems. Specifically, the e.g. Delete and Arrow keys are
8434 * munged and won't work if e.g. searching for Hebrew text.
8435 */
8436 else if (c >= 'a' && c <= 'z')
8437#endif
8438 return (int)(map[CharOrdLow(c)] + p_aleph);
8439 else
8440 return c;
8441 }
8442 else
8443 {
8444 switch (c)
8445 {
8446 case '`': return ';';
8447 case '/': return '.';
8448 case '\'': return ',';
8449 case 'q': return '/';
8450 case 'w': return '\'';
8451
8452 /* Hebrew letters - set offset from 'a' */
8453 case ',': c = '{'; break;
8454 case '.': c = 'v'; break;
8455 case ';': c = 't'; break;
8456 default: {
8457 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8458
8459#ifdef EBCDIC
8460 /* see note about islower() above */
8461 if (!islower(c))
8462#else
8463 if (c < 'a' || c > 'z')
8464#endif
8465 return c;
8466 c = str[CharOrdLow(c)];
8467 break;
8468 }
8469 }
8470
8471 return (int)(CharOrdLow(c) + p_aleph);
8472 }
8473}
8474#endif
8475
8476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008477ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479 int need_redraw = FALSE;
8480 int regname;
8481 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008482 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483
8484 /*
8485 * If we are going to wait for a character, show a '"'.
8486 */
8487 pc_status = PC_STATUS_UNSET;
8488 if (redrawing() && !char_avail())
8489 {
8490 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008491 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492
8493 edit_putchar('"', TRUE);
8494#ifdef FEAT_CMDL_INFO
8495 add_to_showcmd_c(Ctrl_R);
8496#endif
8497 }
8498
8499#ifdef USE_ON_FLY_SCROLL
8500 dont_scroll = TRUE; /* disallow scrolling here */
8501#endif
8502
8503 /*
8504 * Don't map the register name. This also prevents the mode message to be
8505 * deleted when ESC is hit.
8506 */
8507 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008508 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8511 {
8512 /* Get a third key for literal register insertion */
8513 literally = regname;
8514#ifdef FEAT_CMDL_INFO
8515 add_to_showcmd_c(literally);
8516#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008517 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 }
8520 --no_mapping;
8521
8522#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008523 /* Don't call u_sync() while typing the expression or giving an error
8524 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 ++no_u_sync;
8526 if (regname == '=')
8527 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008528# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008530# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008531 /* Sync undo when evaluating the expression calls setline() or
8532 * append(), so that it can be undone separately. */
8533 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008534
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008536# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 /* Restore the Input Method. */
8538 if (im_on)
8539 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008542 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8543 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008544 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 else
8548 {
8549#endif
8550 if (literally == Ctrl_O || literally == Ctrl_P)
8551 {
8552 /* Append the command to the redo buffer. */
8553 AppendCharToRedobuff(Ctrl_R);
8554 AppendCharToRedobuff(literally);
8555 AppendCharToRedobuff(regname);
8556
8557 do_put(regname, BACKWARD, 1L,
8558 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8559 }
8560 else if (insert_reg(regname, literally) == FAIL)
8561 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008562 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 need_redraw = TRUE; /* remove the '"' */
8564 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008565 else if (stop_insert_mode)
8566 /* When the '=' register was used and a function was invoked that
8567 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8568 * insert anything, need to remove the '"' */
8569 need_redraw = TRUE;
8570
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571#ifdef FEAT_EVAL
8572 }
8573 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008574 if (u_sync_once == 1)
8575 ins_need_undo = TRUE;
8576 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577#endif
8578#ifdef FEAT_CMDL_INFO
8579 clear_showcmd();
8580#endif
8581
8582 /* If the inserted register is empty, we need to remove the '"' */
8583 if (need_redraw || stuff_empty())
8584 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008585
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008586 /* Disallow starting Visual mode here, would get a weird mode. */
8587 if (!vis_active && VIsual_active)
8588 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
8591/*
8592 * CTRL-G commands in Insert mode.
8593 */
8594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008595ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 int c;
8598
8599#ifdef FEAT_INS_EXPAND
8600 /* Right after CTRL-X the cursor will be after the ruler. */
8601 setcursor();
8602#endif
8603
8604 /*
8605 * Don't map the second key. This also prevents the mode message to be
8606 * deleted when ESC is hit.
8607 */
8608 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008609 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 --no_mapping;
8611 switch (c)
8612 {
8613 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8614 case K_UP:
8615 case Ctrl_K:
8616 case 'k': ins_up(TRUE);
8617 break;
8618
8619 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8620 case K_DOWN:
8621 case Ctrl_J:
8622 case 'j': ins_down(TRUE);
8623 break;
8624
8625 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008626 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008628
8629 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008630 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008631 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008632 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 break;
8634
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008635 /* CTRL-G U: do not break undo with the next char */
8636 case 'U':
8637 /* Allow one left/right cursor movement with the next char,
8638 * without breaking undo. */
8639 dont_sync_undo = MAYBE;
8640 break;
8641
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008643 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 }
8645}
8646
8647/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008648 * CTRL-^ in Insert mode.
8649 */
8650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008651ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008652{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008653 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008654 {
8655 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8656 if (State & LANGMAP)
8657 {
8658 curbuf->b_p_iminsert = B_IMODE_NONE;
8659 State &= ~LANGMAP;
8660 }
8661 else
8662 {
8663 curbuf->b_p_iminsert = B_IMODE_LMAP;
8664 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008665#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008666 im_set_active(FALSE);
8667#endif
8668 }
8669 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008670#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008671 else
8672 {
8673 /* There are no ":lmap" mappings, toggle IM */
8674 if (im_get_status())
8675 {
8676 curbuf->b_p_iminsert = B_IMODE_NONE;
8677 im_set_active(FALSE);
8678 }
8679 else
8680 {
8681 curbuf->b_p_iminsert = B_IMODE_IM;
8682 State &= ~LANGMAP;
8683 im_set_active(TRUE);
8684 }
8685 }
8686#endif
8687 set_iminsert_global();
8688 showmode();
8689#ifdef FEAT_GUI
8690 /* may show different cursor shape or color */
8691 if (gui.in_use)
8692 gui_update_cursor(TRUE, FALSE);
8693#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008694#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008695 /* Show/unshow value of 'keymap' in status lines. */
8696 status_redraw_curbuf();
8697#endif
8698}
8699
8700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 * Handle ESC in insert mode.
8702 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8703 * insert.
8704 */
8705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008706ins_esc(
8707 long *count,
8708 int cmdchar,
8709 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 int temp;
8712 static int disabled_redraw = FALSE;
8713
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008714#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008715 check_spell_redraw();
8716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717#if defined(FEAT_HANGULIN)
8718# if defined(ESC_CHG_TO_ENG_MODE)
8719 hangul_input_state_set(0);
8720# endif
8721 if (composing_hangul)
8722 {
8723 push_raw_key(composing_hangul_buffer, 2);
8724 composing_hangul = 0;
8725 }
8726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 temp = curwin->w_cursor.col;
8729 if (disabled_redraw)
8730 {
8731 --RedrawingDisabled;
8732 disabled_redraw = FALSE;
8733 }
8734 if (!arrow_used)
8735 {
8736 /*
8737 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008738 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8739 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 */
8741 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008742 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
8744 /*
8745 * Repeating insert may take a long time. Check for
8746 * interrupt now and then.
8747 */
8748 if (*count > 0)
8749 {
8750 line_breakcheck();
8751 if (got_int)
8752 *count = 0;
8753 }
8754
8755 if (--*count > 0) /* repeat what was typed */
8756 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008757 /* Vi repeats the insert without replacing characters. */
8758 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8759 State &= ~REPLACE_FLAG;
8760
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 (void)start_redo_ins();
8762 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008763 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 ++RedrawingDisabled;
8765 disabled_redraw = TRUE;
8766 return FALSE; /* repeat the insert */
8767 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008768 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 undisplay_dollar();
8770 }
8771
8772 /* When an autoindent was removed, curswant stays after the
8773 * indent */
8774 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8775 curwin->w_set_curswant = TRUE;
8776
8777 /* Remember the last Insert position in the '^ mark. */
8778 if (!cmdmod.keepjumps)
8779 curbuf->b_last_insert = curwin->w_cursor;
8780
8781 /*
8782 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008783 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008785 if (!nomove
8786 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787#ifdef FEAT_VIRTUALEDIT
8788 || curwin->w_cursor.coladd > 0
8789#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008790 )
8791 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008792 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793#ifdef FEAT_RIGHTLEFT
8794 && !revins_on
8795#endif
8796 )
8797 {
8798#ifdef FEAT_VIRTUALEDIT
8799 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8800 {
8801 oneleft();
8802 if (restart_edit != NUL)
8803 ++curwin->w_cursor.coladd;
8804 }
8805 else
8806#endif
8807 {
8808 --curwin->w_cursor.col;
8809#ifdef FEAT_MBYTE
8810 /* Correct cursor for multi-byte character. */
8811 if (has_mbyte)
8812 mb_adjust_cursor();
8813#endif
8814 }
8815 }
8816
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008817#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 /* Disable IM to allow typing English directly for Normal mode commands.
8819 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8820 * well). */
8821 if (!(State & LANGMAP))
8822 im_save_status(&curbuf->b_p_iminsert);
8823 im_set_active(FALSE);
8824#endif
8825
8826 State = NORMAL;
8827 /* need to position cursor again (e.g. when on a TAB ) */
8828 changed_cline_bef_curs();
8829
8830#ifdef FEAT_MOUSE
8831 setmouse();
8832#endif
8833#ifdef CURSOR_SHAPE
8834 ui_cursor_shape(); /* may show different cursor shape */
8835#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008836 if (!p_ek)
8837 /* Re-enable bracketed paste mode. */
8838 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
8840 /*
8841 * When recording or for CTRL-O, need to display the new mode.
8842 * Otherwise remove the mode message.
8843 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008844 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 showmode();
8846 else if (p_smd)
8847 MSG("");
8848
8849 return TRUE; /* exit Insert mode */
8850}
8851
8852#ifdef FEAT_RIGHTLEFT
8853/*
8854 * Toggle language: hkmap and revins_on.
8855 * Move to end of reverse inserted text.
8856 */
8857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008858ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 if (revins_on && revins_chars && revins_scol >= 0)
8861 {
8862 while (gchar_cursor() != NUL && revins_chars--)
8863 ++curwin->w_cursor.col;
8864 }
8865 p_ri = !p_ri;
8866 revins_on = (State == INSERT && p_ri);
8867 if (revins_on)
8868 {
8869 revins_scol = curwin->w_cursor.col;
8870 revins_legal++;
8871 revins_chars = 0;
8872 undisplay_dollar();
8873 }
8874 else
8875 revins_scol = -1;
8876#ifdef FEAT_FKMAP
8877 if (p_altkeymap)
8878 {
8879 /*
8880 * to be consistent also for redo command, using '.'
8881 * set arrow_used to true and stop it - causing to redo
8882 * characters entered in one mode (normal/reverse insert).
8883 */
8884 arrow_used = TRUE;
8885 (void)stop_arrow();
8886 p_fkmap = curwin->w_p_rl ^ p_ri;
8887 if (p_fkmap && p_ri)
8888 State = INSERT;
8889 }
8890 else
8891#endif
8892 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8893 showmode();
8894}
8895#endif
8896
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897/*
8898 * If 'keymodel' contains "startsel", may start selection.
8899 * Returns TRUE when a CTRL-O and other keys stuffed.
8900 */
8901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008902ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904 if (km_startsel)
8905 switch (c)
8906 {
8907 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 case K_PAGEUP:
8910 case K_KPAGEUP:
8911 case K_PAGEDOWN:
8912 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008913# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 case K_LEFT:
8915 case K_RIGHT:
8916 case K_UP:
8917 case K_DOWN:
8918 case K_END:
8919 case K_HOME:
8920# endif
8921 if (!(mod_mask & MOD_MASK_SHIFT))
8922 break;
8923 /* FALLTHROUGH */
8924 case K_S_LEFT:
8925 case K_S_RIGHT:
8926 case K_S_UP:
8927 case K_S_DOWN:
8928 case K_S_END:
8929 case K_S_HOME:
8930 /* Start selection right away, the cursor can move with
8931 * CTRL-O when beyond the end of the line. */
8932 start_selection();
8933
8934 /* Execute the key in (insert) Select mode. */
8935 stuffcharReadbuff(Ctrl_O);
8936 if (mod_mask)
8937 {
8938 char_u buf[4];
8939
8940 buf[0] = K_SPECIAL;
8941 buf[1] = KS_MODIFIER;
8942 buf[2] = mod_mask;
8943 buf[3] = NUL;
8944 stuffReadbuff(buf);
8945 }
8946 stuffcharReadbuff(c);
8947 return TRUE;
8948 }
8949 return FALSE;
8950}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
8952/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008953 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008954 */
8955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008956ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008957{
8958#ifdef FEAT_FKMAP
8959 if (p_fkmap && p_ri)
8960 {
8961 beep_flush();
8962 EMSG(farsi_text_3); /* encoded in Farsi */
8963 return;
8964 }
8965#endif
8966
Bram Moolenaar1e015462005-09-25 22:16:38 +00008967# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008968 set_vim_var_string(VV_INSERTMODE,
8969 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008970# ifdef FEAT_VREPLACE
8971 replaceState == VREPLACE ? "v" :
8972# endif
8973 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008974# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008975 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008976 if (State & REPLACE_FLAG)
8977 State = INSERT | (State & LANGMAP);
8978 else
8979 State = replaceState | (State & LANGMAP);
8980 AppendCharToRedobuff(K_INS);
8981 showmode();
8982#ifdef CURSOR_SHAPE
8983 ui_cursor_shape(); /* may show different cursor shape */
8984#endif
8985}
8986
8987/*
8988 * Pressed CTRL-O in Insert mode.
8989 */
8990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008991ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008992{
8993#ifdef FEAT_VREPLACE
8994 if (State & VREPLACE_FLAG)
8995 restart_edit = 'V';
8996 else
8997#endif
8998 if (State & REPLACE_FLAG)
8999 restart_edit = 'R';
9000 else
9001 restart_edit = 'I';
9002#ifdef FEAT_VIRTUALEDIT
9003 if (virtual_active())
9004 ins_at_eol = FALSE; /* cursor always keeps its column */
9005 else
9006#endif
9007 ins_at_eol = (gchar_cursor() == NUL);
9008}
9009
9010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 * If the cursor is on an indent, ^T/^D insert/delete one
9012 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00009013 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 * with vi. But vi only supports ^T and ^D after an
9015 * autoindent, we support it everywhere.
9016 */
9017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009018ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019{
9020 if (stop_arrow() == FAIL)
9021 return;
9022 AppendCharToRedobuff(c);
9023
9024 /*
9025 * 0^D and ^^D: remove all indent.
9026 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009027 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9028 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 {
9030 --curwin->w_cursor.col;
9031 (void)del_char(FALSE); /* delete the '^' or '0' */
9032 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9033 if (State & REPLACE_FLAG)
9034 replace_pop_ins();
9035 if (lastc == '^')
9036 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009037 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 }
9039 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009040 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041
9042 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9043 did_ai = FALSE;
9044#ifdef FEAT_SMARTINDENT
9045 did_si = FALSE;
9046 can_si = FALSE;
9047 can_si_back = FALSE;
9048#endif
9049#ifdef FEAT_CINDENT
9050 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9051#endif
9052}
9053
9054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009055ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057 int temp;
9058
9059 if (stop_arrow() == FAIL)
9060 return;
9061 if (gchar_cursor() == NUL) /* delete newline */
9062 {
9063 temp = curwin->w_cursor.col;
9064 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009065 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009066 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009068 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009070#ifdef FEAT_VREPLACE
9071 /* Adjust orig_line_count in case more lines have been deleted than
9072 * have been added. That makes sure, that open_line() later
9073 * can access all buffer lines correctly */
9074 if (State & VREPLACE_FLAG &&
9075 orig_line_count > curbuf->b_ml.ml_line_count)
9076 orig_line_count = curbuf->b_ml.ml_line_count;
9077#endif
9078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009080 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9081 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 did_ai = FALSE;
9083#ifdef FEAT_SMARTINDENT
9084 did_si = FALSE;
9085 can_si = FALSE;
9086 can_si_back = FALSE;
9087#endif
9088 AppendCharToRedobuff(K_DEL);
9089}
9090
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009091static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009092
9093/*
9094 * Delete one character for ins_bs().
9095 */
9096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009097ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009098{
9099 dec_cursor();
9100 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9101 if (State & REPLACE_FLAG)
9102 {
9103 /* Don't delete characters before the insert point when in
9104 * Replace mode */
9105 if (curwin->w_cursor.lnum != Insstart.lnum
9106 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009107 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009108 }
9109 else
9110 (void)del_char(FALSE);
9111}
9112
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113/*
9114 * Handle Backspace, delete-word and delete-line in Insert mode.
9115 * Return TRUE when backspace was actually used.
9116 */
9117 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009118ins_bs(
9119 int c,
9120 int mode,
9121 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122{
9123 linenr_T lnum;
9124 int cc;
9125 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009126 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 colnr_T mincol;
9128 int did_backspace = FALSE;
9129 int in_indent;
9130 int oldState;
9131#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009132 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133#endif
9134
9135 /*
9136 * can't delete anything in an empty file
9137 * can't backup past first character in buffer
9138 * can't backup past starting point unless 'backspace' > 1
9139 * can backup to a previous line if 'backspace' == 0
9140 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009141 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 || (
9143#ifdef FEAT_RIGHTLEFT
9144 !revins_on &&
9145#endif
9146 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9147 || (!can_bs(BS_START)
9148 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009149 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9150 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9152 && curwin->w_cursor.col <= ai_col)
9153 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9154 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009155 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 return FALSE;
9157 }
9158
9159 if (stop_arrow() == FAIL)
9160 return FALSE;
9161 in_indent = inindent(0);
9162#ifdef FEAT_CINDENT
9163 if (in_indent)
9164 can_cindent = FALSE;
9165#endif
9166#ifdef FEAT_COMMENTS
9167 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9168#endif
9169#ifdef FEAT_RIGHTLEFT
9170 if (revins_on) /* put cursor after last inserted char */
9171 inc_cursor();
9172#endif
9173
9174#ifdef FEAT_VIRTUALEDIT
9175 /* Virtualedit:
9176 * BACKSPACE_CHAR eats a virtual space
9177 * BACKSPACE_WORD eats all coladd
9178 * BACKSPACE_LINE eats all coladd and keeps going
9179 */
9180 if (curwin->w_cursor.coladd > 0)
9181 {
9182 if (mode == BACKSPACE_CHAR)
9183 {
9184 --curwin->w_cursor.coladd;
9185 return TRUE;
9186 }
9187 if (mode == BACKSPACE_WORD)
9188 {
9189 curwin->w_cursor.coladd = 0;
9190 return TRUE;
9191 }
9192 curwin->w_cursor.coladd = 0;
9193 }
9194#endif
9195
9196 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009197 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 */
9199 if (curwin->w_cursor.col == 0)
9200 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009201 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009202 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203#ifdef FEAT_RIGHTLEFT
9204 || revins_on
9205#endif
9206 )
9207 {
9208 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9209 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9210 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009211 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009212 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 }
9214 /*
9215 * In replace mode:
9216 * cc < 0: NL was inserted, delete it
9217 * cc >= 0: NL was replaced, put original characters back
9218 */
9219 cc = -1;
9220 if (State & REPLACE_FLAG)
9221 cc = replace_pop(); /* returns -1 if NL was inserted */
9222 /*
9223 * In replace mode, in the line we started replacing, we only move the
9224 * cursor.
9225 */
9226 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9227 {
9228 dec_cursor();
9229 }
9230 else
9231 {
9232#ifdef FEAT_VREPLACE
9233 if (!(State & VREPLACE_FLAG)
9234 || curwin->w_cursor.lnum > orig_line_count)
9235#endif
9236 {
9237 temp = gchar_cursor(); /* remember current char */
9238 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009239
9240 /* When "aw" is in 'formatoptions' we must delete the space at
9241 * the end of the line, otherwise the line will be broken
9242 * again when auto-formatting. */
9243 if (has_format_option(FO_AUTO)
9244 && has_format_option(FO_WHITE_PAR))
9245 {
9246 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9247 TRUE);
9248 int len;
9249
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009250 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009251 if (len > 0 && ptr[len - 1] == ' ')
9252 ptr[len - 1] = NUL;
9253 }
9254
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009255 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 if (temp == NUL && gchar_cursor() != NUL)
9257 inc_cursor();
9258 }
9259#ifdef FEAT_VREPLACE
9260 else
9261 dec_cursor();
9262#endif
9263
9264 /*
9265 * In REPLACE mode we have to put back the text that was replaced
9266 * by the NL. On the replace stack is first a NUL-terminated
9267 * sequence of characters that were deleted and then the
9268 * characters that NL replaced.
9269 */
9270 if (State & REPLACE_FLAG)
9271 {
9272 /*
9273 * Do the next ins_char() in NORMAL state, to
9274 * prevent ins_char() from replacing characters and
9275 * avoiding showmatch().
9276 */
9277 oldState = State;
9278 State = NORMAL;
9279 /*
9280 * restore characters (blanks) deleted after cursor
9281 */
9282 while (cc > 0)
9283 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009284 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285#ifdef FEAT_MBYTE
9286 mb_replace_pop_ins(cc);
9287#else
9288 ins_char(cc);
9289#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009290 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 cc = replace_pop();
9292 }
9293 /* restore the characters that NL replaced */
9294 replace_pop_ins();
9295 State = oldState;
9296 }
9297 }
9298 did_ai = FALSE;
9299 }
9300 else
9301 {
9302 /*
9303 * Delete character(s) before the cursor.
9304 */
9305#ifdef FEAT_RIGHTLEFT
9306 if (revins_on) /* put cursor on last inserted char */
9307 dec_cursor();
9308#endif
9309 mincol = 0;
9310 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009311 if (mode == BACKSPACE_LINE
9312 && (curbuf->b_p_ai
9313#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009314 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009315#endif
9316 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317#ifdef FEAT_RIGHTLEFT
9318 && !revins_on
9319#endif
9320 )
9321 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009322 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009324 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009326 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 }
9328
9329 /*
9330 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9331 */
9332 if ( mode == BACKSPACE_CHAR
9333 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009334 || ((get_sts_value() != 0
9335#ifdef FEAT_VARTABS
9336 || tabstop_count(curbuf->b_p_vsts_array)
9337#endif
9338 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009339 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 && (*(ml_get_cursor() - 1) == TAB
9341 || (*(ml_get_cursor() - 1) == ' '
9342 && (!*inserted_space_p
9343 || arrow_used))))))
9344 {
9345 int ts;
9346 colnr_T vcol;
9347 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009348 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349
9350 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 /* Compute the virtual column where we want to be. Since
9352 * 'showbreak' may get in the way, need to get the last column of
9353 * the previous character. */
9354 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009355 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 dec_cursor();
9357 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9358 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009359#ifdef FEAT_VARTABS
9360 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009361 {
9362 ts = (int)get_sw_value(curbuf);
9363 want_vcol = (want_vcol / ts) * ts;
9364 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009365 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009366 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009367 curbuf->b_p_vsts_array);
9368#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009369 if (p_sta && in_indent)
9370 ts = (int)get_sw_value(curbuf);
9371 else
9372 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375
9376 /* delete characters until we are at or before want_vcol */
9377 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009378 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009379 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
9381 /* insert extra spaces until we are at want_vcol */
9382 while (vcol < want_vcol)
9383 {
9384 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009385 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9386 && curwin->w_cursor.col < Insstart_orig.col)
9387 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388
9389#ifdef FEAT_VREPLACE
9390 if (State & VREPLACE_FLAG)
9391 ins_char(' ');
9392 else
9393#endif
9394 {
9395 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009396 if ((State & REPLACE_FLAG))
9397 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 }
9399 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9400 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009401
9402 /* If we are now back where we started delete one character. Can
9403 * happen when using 'sts' and 'linebreak'. */
9404 if (vcol >= start_vcol)
9405 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 }
9407
9408 /*
9409 * Delete upto starting point, start of line or previous word.
9410 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009411 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009413#ifdef FEAT_MBYTE
9414 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009416 if (has_mbyte)
9417 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009419 do
9420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009422 if (!revins_on) /* put cursor on char to be deleted */
9423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009425
9426 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009428 /* look multi-byte character class */
9429 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009431 prev_cclass = cclass;
9432 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009435
9436 /* start of word? */
9437 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9438 {
9439 mode = BACKSPACE_WORD_NOT_SPACE;
9440 temp = vim_iswordc(cc);
9441 }
9442 /* end of word? */
9443 else if (mode == BACKSPACE_WORD_NOT_SPACE
9444 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9445#ifdef FEAT_MBYTE
9446 || prev_cclass != cclass
9447#endif
9448 ))
9449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009451 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009453 inc_cursor();
9454#ifdef FEAT_RIGHTLEFT
9455 else if (State & REPLACE_FLAG)
9456 dec_cursor();
9457#endif
9458 break;
9459 }
9460 if (State & REPLACE_FLAG)
9461 replace_do_bs(-1);
9462 else
9463 {
9464#ifdef FEAT_MBYTE
9465 if (enc_utf8 && p_deco)
9466 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9467#endif
9468 (void)del_char(FALSE);
9469#ifdef FEAT_MBYTE
9470 /*
9471 * If there are combining characters and 'delcombine' is set
9472 * move the cursor back. Don't back up before the base
9473 * character.
9474 */
9475 if (enc_utf8 && p_deco && cpc[0] != NUL)
9476 inc_cursor();
9477#endif
9478#ifdef FEAT_RIGHTLEFT
9479 if (revins_chars)
9480 {
9481 revins_chars--;
9482 revins_legal++;
9483 }
9484 if (revins_on && gchar_cursor() == NUL)
9485 break;
9486#endif
9487 }
9488 /* Just a single backspace?: */
9489 if (mode == BACKSPACE_CHAR)
9490 break;
9491 } while (
9492#ifdef FEAT_RIGHTLEFT
9493 revins_on ||
9494#endif
9495 (curwin->w_cursor.col > mincol
9496 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9497 || curwin->w_cursor.col != Insstart_orig.col)));
9498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 did_backspace = TRUE;
9500 }
9501#ifdef FEAT_SMARTINDENT
9502 did_si = FALSE;
9503 can_si = FALSE;
9504 can_si_back = FALSE;
9505#endif
9506 if (curwin->w_cursor.col <= 1)
9507 did_ai = FALSE;
9508 /*
9509 * It's a little strange to put backspaces into the redo
9510 * buffer, but it makes auto-indent a lot easier to deal
9511 * with.
9512 */
9513 AppendCharToRedobuff(c);
9514
9515 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009516 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009517 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009518 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519
9520 /* vi behaviour: the cursor moves backward but the character that
9521 * was there remains visible
9522 * Vim behaviour: the cursor moves backward and the character that
9523 * was there is erased from the screen.
9524 * We can emulate the vi behaviour by pretending there is a dollar
9525 * displayed even when there isn't.
9526 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009527 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 dollar_vcol = curwin->w_virtcol;
9529
Bram Moolenaarce3be472008-01-14 19:12:28 +00009530#ifdef FEAT_FOLDING
9531 /* When deleting a char the cursor line must never be in a closed fold.
9532 * E.g., when 'foldmethod' is indent and deleting the first non-white
9533 * char before a Tab. */
9534 if (did_backspace)
9535 foldOpenCursor();
9536#endif
9537
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 return did_backspace;
9539}
9540
9541#ifdef FEAT_MOUSE
9542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009543ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
9545 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009546 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547
9548# ifdef FEAT_GUI
9549 /* When GUI is active, also move/paste when 'mouse' is empty */
9550 if (!gui.in_use)
9551# endif
9552 if (!mouse_has(MOUSE_INSERT))
9553 return;
9554
9555 undisplay_dollar();
9556 tpos = curwin->w_cursor;
9557 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9558 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009559 win_T *new_curwin = curwin;
9560
9561 if (curwin != old_curwin && win_valid(old_curwin))
9562 {
9563 /* Mouse took us to another window. We need to go back to the
9564 * previous one to stop insert there properly. */
9565 curwin = old_curwin;
9566 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009567#ifdef FEAT_JOB_CHANNEL
9568 if (bt_prompt(curbuf))
9569 // Restart Insert mode when re-entering the prompt buffer.
9570 curbuf->b_prompt_insert = 'A';
9571#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009572 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009573 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009574 if (curwin != new_curwin && win_valid(new_curwin))
9575 {
9576 curwin = new_curwin;
9577 curbuf = curwin->w_buffer;
9578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579# ifdef FEAT_CINDENT
9580 can_cindent = TRUE;
9581# endif
9582 }
9583
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 /* redraw status lines (in case another window became active) */
9585 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586}
9587
9588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009589ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
9591 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009592 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009593# ifdef FEAT_INS_EXPAND
9594 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595# endif
9596
9597 tpos = curwin->w_cursor;
9598
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009599 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
9601 int row, col;
9602
9603 row = mouse_row;
9604 col = mouse_col;
9605
9606 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009607 wp = mouse_find_win(&row, &col);
9608 if (wp == NULL)
9609 return;
9610 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 curbuf = curwin->w_buffer;
9612 }
9613 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 undisplay_dollar();
9615
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009616# ifdef FEAT_INS_EXPAND
9617 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009618 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009619# endif
9620 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009621 if (dir == MSCR_DOWN || dir == MSCR_UP)
9622 {
9623 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9624 scroll_redraw(dir,
9625 (long)(curwin->w_botline - curwin->w_topline));
9626 else
9627 scroll_redraw(dir, 3L);
9628 }
9629#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009630 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009631 {
9632 int val, step = 6;
9633
9634 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009635 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009636 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9637 if (val < 0)
9638 val = 0;
9639 gui_do_horiz_scroll(val, TRUE);
9640 }
9641#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009642# ifdef FEAT_INS_EXPAND
9643 did_scroll = TRUE;
9644# endif
9645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 curwin->w_redr_status = TRUE;
9648
9649 curwin = old_curwin;
9650 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009652# ifdef FEAT_INS_EXPAND
9653 /* The popup menu may overlay the window, need to redraw it.
9654 * TODO: Would be more efficient to only redraw the windows that are
9655 * overlapped by the popup menu. */
9656 if (pum_visible() && did_scroll)
9657 {
9658 redraw_all_later(NOT_VALID);
9659 ins_compl_show_pum();
9660 }
9661# endif
9662
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009663 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 {
9665 start_arrow(&tpos);
9666# ifdef FEAT_CINDENT
9667 can_cindent = TRUE;
9668# endif
9669 }
9670}
9671#endif
9672
Bram Moolenaarec2da362017-01-21 20:04:22 +01009673/*
9674 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9675 * P_PE literally.
9676 * When "drop" is TRUE then consume the text and drop it.
9677 */
9678 int
9679bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9680{
9681 int c;
9682 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9683 int idx = 0;
9684 char_u *end = find_termcode((char_u *)"PE");
9685 int ret_char = -1;
9686 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009687 int save_paste = p_paste;
9688 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009689
9690 /* If the end code is too long we can't detect it, read everything. */
9691 if (STRLEN(end) >= NUMBUFLEN)
9692 end = NULL;
9693 ++no_mapping;
9694 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009695 p_paste = TRUE;
9696 curbuf->b_p_ai = FALSE;
9697
Bram Moolenaarec2da362017-01-21 20:04:22 +01009698 for (;;)
9699 {
9700 /* When the end is not defined read everything. */
9701 if (end == NULL && vpeekc() == NUL)
9702 break;
9703 c = plain_vgetc();
9704#ifdef FEAT_MBYTE
9705 if (has_mbyte)
9706 idx += (*mb_char2bytes)(c, buf + idx);
9707 else
9708#endif
9709 buf[idx++] = c;
9710 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009711 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009712 {
9713 if (end[idx] == NUL)
9714 break; /* Found the end of paste code. */
9715 continue;
9716 }
9717 if (!drop)
9718 {
9719 switch (mode)
9720 {
9721 case PASTE_CMDLINE:
9722 put_on_cmdline(buf, idx, TRUE);
9723 break;
9724
9725 case PASTE_EX:
9726 if (gap != NULL && ga_grow(gap, idx) == OK)
9727 {
9728 mch_memmove((char *)gap->ga_data + gap->ga_len,
9729 buf, (size_t)idx);
9730 gap->ga_len += idx;
9731 }
9732 break;
9733
9734 case PASTE_INSERT:
9735 if (stop_arrow() == OK)
9736 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009737 c = buf[0];
9738 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9739 ins_eol(c);
9740 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009741 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009742 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009743 AppendToRedobuffLit(buf, idx);
9744 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009745 }
9746 break;
9747
9748 case PASTE_ONE_CHAR:
9749 if (ret_char == -1)
9750 {
9751#ifdef FEAT_MBYTE
9752 if (has_mbyte)
9753 ret_char = (*mb_ptr2char)(buf);
9754 else
9755#endif
9756 ret_char = buf[0];
9757 }
9758 break;
9759 }
9760 }
9761 idx = 0;
9762 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009763
Bram Moolenaarec2da362017-01-21 20:04:22 +01009764 --no_mapping;
9765 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009766 p_paste = save_paste;
9767 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009768
9769 return ret_char;
9770}
9771
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009772#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009774ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009775{
9776 /* We will be leaving the current window, unless closing another tab. */
9777 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9778 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9779 {
9780 undisplay_dollar();
9781 start_arrow(&curwin->w_cursor);
9782# ifdef FEAT_CINDENT
9783 can_cindent = TRUE;
9784# endif
9785 }
9786
9787 if (c == K_TABLINE)
9788 goto_tabpage(current_tab);
9789 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009790 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009791 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009792 redraw_statuslines(); /* will redraw the tabline when needed */
9793 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009794}
9795#endif
9796
9797#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009799ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
9801 pos_T tpos;
9802
9803 undisplay_dollar();
9804 tpos = curwin->w_cursor;
9805 if (gui_do_scroll())
9806 {
9807 start_arrow(&tpos);
9808# ifdef FEAT_CINDENT
9809 can_cindent = TRUE;
9810# endif
9811 }
9812}
9813
9814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
9817 pos_T tpos;
9818
9819 undisplay_dollar();
9820 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009821 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 {
9823 start_arrow(&tpos);
9824# ifdef FEAT_CINDENT
9825 can_cindent = TRUE;
9826# endif
9827 }
9828}
9829#endif
9830
9831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009832ins_left(
9833 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834{
9835 pos_T tpos;
9836
9837#ifdef FEAT_FOLDING
9838 if ((fdo_flags & FDO_HOR) && KeyTyped)
9839 foldOpenCursor();
9840#endif
9841 undisplay_dollar();
9842 tpos = curwin->w_cursor;
9843 if (oneleft() == OK)
9844 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009845#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9846 /* Only call start_arrow() when not busy with preediting, it will
9847 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009848 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009849#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009850 {
9851 start_arrow_with_change(&tpos, end_change);
9852 if (!end_change)
9853 AppendCharToRedobuff(K_LEFT);
9854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855#ifdef FEAT_RIGHTLEFT
9856 /* If exit reversed string, position is fixed */
9857 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9858 revins_legal++;
9859 revins_chars++;
9860#endif
9861 }
9862
9863 /*
9864 * if 'whichwrap' set for cursor in insert mode may go to
9865 * previous line
9866 */
9867 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9868 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009869 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 start_arrow(&tpos);
9871 --(curwin->w_cursor.lnum);
9872 coladvance((colnr_T)MAXCOL);
9873 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9874 }
9875 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009876 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009877 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878}
9879
9880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009881ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882{
9883 pos_T tpos;
9884
9885#ifdef FEAT_FOLDING
9886 if ((fdo_flags & FDO_HOR) && KeyTyped)
9887 foldOpenCursor();
9888#endif
9889 undisplay_dollar();
9890 tpos = curwin->w_cursor;
9891 if (c == K_C_HOME)
9892 curwin->w_cursor.lnum = 1;
9893 curwin->w_cursor.col = 0;
9894#ifdef FEAT_VIRTUALEDIT
9895 curwin->w_cursor.coladd = 0;
9896#endif
9897 curwin->w_curswant = 0;
9898 start_arrow(&tpos);
9899}
9900
9901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009902ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903{
9904 pos_T tpos;
9905
9906#ifdef FEAT_FOLDING
9907 if ((fdo_flags & FDO_HOR) && KeyTyped)
9908 foldOpenCursor();
9909#endif
9910 undisplay_dollar();
9911 tpos = curwin->w_cursor;
9912 if (c == K_C_END)
9913 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9914 coladvance((colnr_T)MAXCOL);
9915 curwin->w_curswant = MAXCOL;
9916
9917 start_arrow(&tpos);
9918}
9919
9920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009921ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923#ifdef FEAT_FOLDING
9924 if ((fdo_flags & FDO_HOR) && KeyTyped)
9925 foldOpenCursor();
9926#endif
9927 undisplay_dollar();
9928 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9929 {
9930 start_arrow(&curwin->w_cursor);
9931 (void)bck_word(1L, FALSE, FALSE);
9932 curwin->w_set_curswant = TRUE;
9933 }
9934 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009935 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936}
9937
9938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009939ins_right(
9940 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941{
9942#ifdef FEAT_FOLDING
9943 if ((fdo_flags & FDO_HOR) && KeyTyped)
9944 foldOpenCursor();
9945#endif
9946 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009947 if (gchar_cursor() != NUL
9948#ifdef FEAT_VIRTUALEDIT
9949 || virtual_active()
9950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 )
9952 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009953 start_arrow_with_change(&curwin->w_cursor, end_change);
9954 if (!end_change)
9955 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 curwin->w_set_curswant = TRUE;
9957#ifdef FEAT_VIRTUALEDIT
9958 if (virtual_active())
9959 oneright();
9960 else
9961#endif
9962 {
9963#ifdef FEAT_MBYTE
9964 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009965 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 else
9967#endif
9968 ++curwin->w_cursor.col;
9969 }
9970
9971#ifdef FEAT_RIGHTLEFT
9972 revins_legal++;
9973 if (revins_chars)
9974 revins_chars--;
9975#endif
9976 }
9977 /* if 'whichwrap' set for cursor in insert mode, may move the
9978 * cursor to the next line */
9979 else if (vim_strchr(p_ww, ']') != NULL
9980 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9981 {
9982 start_arrow(&curwin->w_cursor);
9983 curwin->w_set_curswant = TRUE;
9984 ++curwin->w_cursor.lnum;
9985 curwin->w_cursor.col = 0;
9986 }
9987 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009988 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009989 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990}
9991
9992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009993ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994{
9995#ifdef FEAT_FOLDING
9996 if ((fdo_flags & FDO_HOR) && KeyTyped)
9997 foldOpenCursor();
9998#endif
9999 undisplay_dollar();
10000 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
10001 || gchar_cursor() != NUL)
10002 {
10003 start_arrow(&curwin->w_cursor);
10004 (void)fwd_word(1L, FALSE, 0);
10005 curwin->w_set_curswant = TRUE;
10006 }
10007 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010008 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009}
10010
10011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010012ins_up(
10013 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014{
10015 pos_T tpos;
10016 linenr_T old_topline = curwin->w_topline;
10017#ifdef FEAT_DIFF
10018 int old_topfill = curwin->w_topfill;
10019#endif
10020
10021 undisplay_dollar();
10022 tpos = curwin->w_cursor;
10023 if (cursor_up(1L, TRUE) == OK)
10024 {
10025 if (startcol)
10026 coladvance(getvcol_nolist(&Insstart));
10027 if (old_topline != curwin->w_topline
10028#ifdef FEAT_DIFF
10029 || old_topfill != curwin->w_topfill
10030#endif
10031 )
10032 redraw_later(VALID);
10033 start_arrow(&tpos);
10034#ifdef FEAT_CINDENT
10035 can_cindent = TRUE;
10036#endif
10037 }
10038 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010039 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040}
10041
10042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010043ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044{
10045 pos_T tpos;
10046
10047 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010048
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010049 if (mod_mask & MOD_MASK_CTRL)
10050 {
10051 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010052 if (first_tabpage->tp_next != NULL)
10053 {
10054 start_arrow(&curwin->w_cursor);
10055 goto_tabpage(-1);
10056 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010057 return;
10058 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010059
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 tpos = curwin->w_cursor;
10061 if (onepage(BACKWARD, 1L) == OK)
10062 {
10063 start_arrow(&tpos);
10064#ifdef FEAT_CINDENT
10065 can_cindent = TRUE;
10066#endif
10067 }
10068 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010069 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070}
10071
10072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010073ins_down(
10074 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075{
10076 pos_T tpos;
10077 linenr_T old_topline = curwin->w_topline;
10078#ifdef FEAT_DIFF
10079 int old_topfill = curwin->w_topfill;
10080#endif
10081
10082 undisplay_dollar();
10083 tpos = curwin->w_cursor;
10084 if (cursor_down(1L, TRUE) == OK)
10085 {
10086 if (startcol)
10087 coladvance(getvcol_nolist(&Insstart));
10088 if (old_topline != curwin->w_topline
10089#ifdef FEAT_DIFF
10090 || old_topfill != curwin->w_topfill
10091#endif
10092 )
10093 redraw_later(VALID);
10094 start_arrow(&tpos);
10095#ifdef FEAT_CINDENT
10096 can_cindent = TRUE;
10097#endif
10098 }
10099 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010100 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101}
10102
10103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010104ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
10106 pos_T tpos;
10107
10108 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010109
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010110 if (mod_mask & MOD_MASK_CTRL)
10111 {
10112 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010113 if (first_tabpage->tp_next != NULL)
10114 {
10115 start_arrow(&curwin->w_cursor);
10116 goto_tabpage(0);
10117 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010118 return;
10119 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010120
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 tpos = curwin->w_cursor;
10122 if (onepage(FORWARD, 1L) == OK)
10123 {
10124 start_arrow(&tpos);
10125#ifdef FEAT_CINDENT
10126 can_cindent = TRUE;
10127#endif
10128 }
10129 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010130 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131}
10132
10133#ifdef FEAT_DND
10134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010135ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136{
10137 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10138}
10139#endif
10140
10141/*
10142 * Handle TAB in Insert or Replace mode.
10143 * Return TRUE when the TAB needs to be inserted like a normal character.
10144 */
10145 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010146ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147{
10148 int ind;
10149 int i;
10150 int temp;
10151
10152 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10153 Insstart_blank_vcol = get_nolist_virtcol();
10154 if (echeck_abbr(TAB + ABBR_OFF))
10155 return FALSE;
10156
10157 ind = inindent(0);
10158#ifdef FEAT_CINDENT
10159 if (ind)
10160 can_cindent = FALSE;
10161#endif
10162
10163 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010164 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 */
10166 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010167#ifdef FEAT_VARTABS
10168 && !(p_sta && ind
10169 /* These five lines mean 'tabstop' != 'shiftwidth' */
10170 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10171 || (tabstop_count(curbuf->b_p_vts_array) == 1
10172 && tabstop_first(curbuf->b_p_vts_array)
10173 != get_sw_value(curbuf))
10174 || (tabstop_count(curbuf->b_p_vts_array) == 0
10175 && curbuf->b_p_ts != get_sw_value(curbuf))))
10176 && tabstop_count(curbuf->b_p_vsts_array) == 0
10177#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010178 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010179#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010180 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 return TRUE;
10182
10183 if (stop_arrow() == FAIL)
10184 return TRUE;
10185
10186 did_ai = FALSE;
10187#ifdef FEAT_SMARTINDENT
10188 did_si = FALSE;
10189 can_si = FALSE;
10190 can_si_back = FALSE;
10191#endif
10192 AppendToRedobuff((char_u *)"\t");
10193
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010194#ifdef FEAT_VARTABS
10195 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10196 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010197 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010198 temp -= get_nolist_virtcol() % temp;
10199 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010200 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010201 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010202 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010203 curbuf->b_p_vsts_array);
10204 else /* otherwise use 'tabstop' */
10205 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10206 curbuf->b_p_vts_array);
10207#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010209 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010210 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10211 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 else /* otherwise use 'tabstop' */
10213 temp = (int)curbuf->b_p_ts;
10214 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216
10217 /*
10218 * Insert the first space with ins_char(). It will delete one char in
10219 * replace mode. Insert the rest with ins_str(); it will not delete any
10220 * chars. For VREPLACE mode, we use ins_char() for all characters.
10221 */
10222 ins_char(' ');
10223 while (--temp > 0)
10224 {
10225#ifdef FEAT_VREPLACE
10226 if (State & VREPLACE_FLAG)
10227 ins_char(' ');
10228 else
10229#endif
10230 {
10231 ins_str((char_u *)" ");
10232 if (State & REPLACE_FLAG) /* no char replaced */
10233 replace_push(NUL);
10234 }
10235 }
10236
10237 /*
10238 * When 'expandtab' not set: Replace spaces by TABs where possible.
10239 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010240#ifdef FEAT_VARTABS
10241 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10242 || get_sts_value() > 0
10243 || (p_sta && ind)))
10244#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010245 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 {
10248 char_u *ptr;
10249#ifdef FEAT_VREPLACE
10250 char_u *saved_line = NULL; /* init for GCC */
10251 pos_T pos;
10252#endif
10253 pos_T fpos;
10254 pos_T *cursor;
10255 colnr_T want_vcol, vcol;
10256 int change_col = -1;
10257 int save_list = curwin->w_p_list;
10258
10259 /*
10260 * Get the current line. For VREPLACE mode, don't make real changes
10261 * yet, just work on a copy of the line.
10262 */
10263#ifdef FEAT_VREPLACE
10264 if (State & VREPLACE_FLAG)
10265 {
10266 pos = curwin->w_cursor;
10267 cursor = &pos;
10268 saved_line = vim_strsave(ml_get_curline());
10269 if (saved_line == NULL)
10270 return FALSE;
10271 ptr = saved_line + pos.col;
10272 }
10273 else
10274#endif
10275 {
10276 ptr = ml_get_cursor();
10277 cursor = &curwin->w_cursor;
10278 }
10279
10280 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10281 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10282 curwin->w_p_list = FALSE;
10283
10284 /* Find first white before the cursor */
10285 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010286 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 {
10288 --fpos.col;
10289 --ptr;
10290 }
10291
10292 /* In Replace mode, don't change characters before the insert point. */
10293 if ((State & REPLACE_FLAG)
10294 && fpos.lnum == Insstart.lnum
10295 && fpos.col < Insstart.col)
10296 {
10297 ptr += Insstart.col - fpos.col;
10298 fpos.col = Insstart.col;
10299 }
10300
10301 /* compute virtual column numbers of first white and cursor */
10302 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10303 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10304
Bram Moolenaar597a4222014-06-25 14:39:50 +020010305 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10306 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010307 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010309 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 if (vcol + i > want_vcol)
10311 break;
10312 if (*ptr != TAB)
10313 {
10314 *ptr = TAB;
10315 if (change_col < 0)
10316 {
10317 change_col = fpos.col; /* Column of first change */
10318 /* May have to adjust Insstart */
10319 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10320 Insstart.col = fpos.col;
10321 }
10322 }
10323 ++fpos.col;
10324 ++ptr;
10325 vcol += i;
10326 }
10327
10328 if (change_col >= 0)
10329 {
10330 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010331 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332
10333 /* Skip over the spaces we need. */
10334 while (vcol < want_vcol && *ptr == ' ')
10335 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010336 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 ++ptr;
10338 ++repl_off;
10339 }
10340 if (vcol > want_vcol)
10341 {
10342 /* Must have a char with 'showbreak' just before it. */
10343 --ptr;
10344 --repl_off;
10345 }
10346 fpos.col += repl_off;
10347
10348 /* Delete following spaces. */
10349 i = cursor->col - fpos.col;
10350 if (i > 0)
10351 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010352 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 /* correct replace stack. */
10354 if ((State & REPLACE_FLAG)
10355#ifdef FEAT_VREPLACE
10356 && !(State & VREPLACE_FLAG)
10357#endif
10358 )
10359 for (temp = i; --temp >= 0; )
10360 replace_join(repl_off);
10361 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010362#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010363 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010364 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010365 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010366 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10367 (char_u *)"\t", 1);
10368 }
10369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 cursor->col -= i;
10371
10372#ifdef FEAT_VREPLACE
10373 /*
10374 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10375 * backspacing over the changed spacing and then inserting the new
10376 * spacing.
10377 */
10378 if (State & VREPLACE_FLAG)
10379 {
10380 /* Backspace from real cursor to change_col */
10381 backspace_until_column(change_col);
10382
10383 /* Insert each char in saved_line from changed_col to
10384 * ptr-cursor */
10385 ins_bytes_len(saved_line + change_col,
10386 cursor->col - change_col);
10387 }
10388#endif
10389 }
10390
10391#ifdef FEAT_VREPLACE
10392 if (State & VREPLACE_FLAG)
10393 vim_free(saved_line);
10394#endif
10395 curwin->w_p_list = save_list;
10396 }
10397
10398 return FALSE;
10399}
10400
10401/*
10402 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010403 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 */
10405 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010406ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407{
10408 int i;
10409
10410 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010411 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 undisplay_dollar();
10415
10416 /*
10417 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10418 * character under the cursor. Only push a NUL on the replace stack,
10419 * nothing to put back when the NL is deleted.
10420 */
10421 if ((State & REPLACE_FLAG)
10422#ifdef FEAT_VREPLACE
10423 && !(State & VREPLACE_FLAG)
10424#endif
10425 )
10426 replace_push(NUL);
10427
10428 /*
10429 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10430 * replacing the next line, so we push all of the characters left on the
10431 * line onto the replace stack. This is not done here though, it is done
10432 * in open_line().
10433 */
10434
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010435#ifdef FEAT_VIRTUALEDIT
10436 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10437 * CTRL-O). */
10438 if (virtual_active() && curwin->w_cursor.coladd > 0)
10439 coladvance(getviscol());
10440#endif
10441
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442#ifdef FEAT_RIGHTLEFT
10443# ifdef FEAT_FKMAP
10444 if (p_altkeymap && p_fkmap)
10445 fkmap(NL);
10446# endif
10447 /* NL in reverse insert will always start in the end of
10448 * current line. */
10449 if (revins_on)
10450 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10451#endif
10452
10453 AppendToRedobuff(NL_STR);
10454 i = open_line(FORWARD,
10455#ifdef FEAT_COMMENTS
10456 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10457#endif
10458 0, old_indent);
10459 old_indent = 0;
10460#ifdef FEAT_CINDENT
10461 can_cindent = TRUE;
10462#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010463#ifdef FEAT_FOLDING
10464 /* When inserting a line the cursor line must never be in a closed fold. */
10465 foldOpenCursor();
10466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010468 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469}
10470
10471#ifdef FEAT_DIGRAPHS
10472/*
10473 * Handle digraph in insert mode.
10474 * Returns character still to be inserted, or NUL when nothing remaining to be
10475 * done.
10476 */
10477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010478ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479{
10480 int c;
10481 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010482 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
10484 pc_status = PC_STATUS_UNSET;
10485 if (redrawing() && !char_avail())
10486 {
10487 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010488 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489
10490 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010491 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492#ifdef FEAT_CMDL_INFO
10493 add_to_showcmd_c(Ctrl_K);
10494#endif
10495 }
10496
10497#ifdef USE_ON_FLY_SCROLL
10498 dont_scroll = TRUE; /* disallow scrolling here */
10499#endif
10500
10501 /* don't map the digraph chars. This also prevents the
10502 * mode message to be deleted when ESC is hit */
10503 ++no_mapping;
10504 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010505 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 --no_mapping;
10507 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010508 if (did_putchar)
10509 /* when the line fits in 'columns' the '?' is at the start of the next
10510 * line and will not be removed by the redraw */
10511 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010512
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 if (IS_SPECIAL(c) || mod_mask) /* special key */
10514 {
10515#ifdef FEAT_CMDL_INFO
10516 clear_showcmd();
10517#endif
10518 insert_special(c, TRUE, FALSE);
10519 return NUL;
10520 }
10521 if (c != ESC)
10522 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010523 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 if (redrawing() && !char_avail())
10525 {
10526 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010527 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528
10529 if (char2cells(c) == 1)
10530 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010531 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010533 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 }
10535#ifdef FEAT_CMDL_INFO
10536 add_to_showcmd_c(c);
10537#endif
10538 }
10539 ++no_mapping;
10540 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010541 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 --no_mapping;
10543 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010544 if (did_putchar)
10545 /* when the line fits in 'columns' the '?' is at the start of the
10546 * next line and will not be removed by a redraw */
10547 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 if (cc != ESC)
10549 {
10550 AppendToRedobuff((char_u *)CTRL_V_STR);
10551 c = getdigraph(c, cc, TRUE);
10552#ifdef FEAT_CMDL_INFO
10553 clear_showcmd();
10554#endif
10555 return c;
10556 }
10557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558#ifdef FEAT_CMDL_INFO
10559 clear_showcmd();
10560#endif
10561 return NUL;
10562}
10563#endif
10564
10565/*
10566 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10567 * Returns the char to be inserted, or NUL if none found.
10568 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010569 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010570ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571{
10572 int c;
10573 int temp;
10574 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010575 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576
10577 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10578 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010579 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 return NUL;
10581 }
10582
10583 /* try to advance to the cursor column */
10584 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010585 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 prev_ptr = ptr;
10587 validate_virtcol();
10588 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10589 {
10590 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010591 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 }
10593 if ((colnr_T)temp > curwin->w_virtcol)
10594 ptr = prev_ptr;
10595
10596#ifdef FEAT_MBYTE
10597 c = (*mb_ptr2char)(ptr);
10598#else
10599 c = *ptr;
10600#endif
10601 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010602 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 return c;
10604}
10605
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010606/*
10607 * CTRL-Y or CTRL-E typed in Insert mode.
10608 */
10609 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010610ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010611{
10612 int c = tc;
10613
10614#ifdef FEAT_INS_EXPAND
10615 if (ctrl_x_mode == CTRL_X_SCROLL)
10616 {
10617 if (c == Ctrl_Y)
10618 scrolldown_clamp();
10619 else
10620 scrollup_clamp();
10621 redraw_later(VALID);
10622 }
10623 else
10624#endif
10625 {
10626 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10627 if (c != NUL)
10628 {
10629 long tw_save;
10630
10631 /* The character must be taken literally, insert like it
10632 * was typed after a CTRL-V, and pretend 'textwidth'
10633 * wasn't set. Digits, 'o' and 'x' are special after a
10634 * CTRL-V, don't use it for these. */
10635 if (c < 256 && !isalnum(c))
10636 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10637 tw_save = curbuf->b_p_tw;
10638 curbuf->b_p_tw = -1;
10639 insert_special(c, TRUE, FALSE);
10640 curbuf->b_p_tw = tw_save;
10641#ifdef FEAT_RIGHTLEFT
10642 revins_chars++;
10643 revins_legal++;
10644#endif
10645 c = Ctrl_V; /* pretend CTRL-V is last character */
10646 auto_format(FALSE, TRUE);
10647 }
10648 }
10649 return c;
10650}
10651
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652#ifdef FEAT_SMARTINDENT
10653/*
10654 * Try to do some very smart auto-indenting.
10655 * Used when inserting a "normal" character.
10656 */
10657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010658ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659{
10660 pos_T *pos, old_pos;
10661 char_u *ptr;
10662 int i;
10663 int temp;
10664
10665 /*
10666 * do some very smart indenting when entering '{' or '}'
10667 */
10668 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10669 {
10670 /*
10671 * for '}' set indent equal to indent of line containing matching '{'
10672 */
10673 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10674 {
10675 old_pos = curwin->w_cursor;
10676 /*
10677 * If the matching '{' has a ')' immediately before it (ignoring
10678 * white-space), then line up with the start of the line
10679 * containing the matching '(' if there is one. This handles the
10680 * case where an "if (..\n..) {" statement continues over multiple
10681 * lines -- webb
10682 */
10683 ptr = ml_get(pos->lnum);
10684 i = pos->col;
10685 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010686 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 ;
10688 curwin->w_cursor.lnum = pos->lnum;
10689 curwin->w_cursor.col = i;
10690 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10691 curwin->w_cursor = *pos;
10692 i = get_indent();
10693 curwin->w_cursor = old_pos;
10694#ifdef FEAT_VREPLACE
10695 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010696 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 else
10698#endif
10699 (void)set_indent(i, SIN_CHANGED);
10700 }
10701 else if (curwin->w_cursor.col > 0)
10702 {
10703 /*
10704 * when inserting '{' after "O" reduce indent, but not
10705 * more than indent of previous line
10706 */
10707 temp = TRUE;
10708 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10709 {
10710 old_pos = curwin->w_cursor;
10711 i = get_indent();
10712 while (curwin->w_cursor.lnum > 1)
10713 {
10714 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10715
10716 /* ignore empty lines and lines starting with '#'. */
10717 if (*ptr != '#' && *ptr != NUL)
10718 break;
10719 }
10720 if (get_indent() >= i)
10721 temp = FALSE;
10722 curwin->w_cursor = old_pos;
10723 }
10724 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010725 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 }
10727 }
10728
10729 /*
10730 * set indent of '#' always to 0
10731 */
10732 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10733 {
10734 /* remember current indent for next line */
10735 old_indent = get_indent();
10736 (void)set_indent(0, SIN_CHANGED);
10737 }
10738
10739 /* Adjust ai_col, the char at this position can be deleted. */
10740 if (ai_col > curwin->w_cursor.col)
10741 ai_col = curwin->w_cursor.col;
10742}
10743#endif
10744
10745/*
10746 * Get the value that w_virtcol would have when 'list' is off.
10747 * Unless 'cpo' contains the 'L' flag.
10748 */
10749 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010750get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751{
10752 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10753 return getvcol_nolist(&curwin->w_cursor);
10754 validate_virtcol();
10755 return curwin->w_virtcol;
10756}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010757
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010758#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010759/*
10760 * Handle the InsertCharPre autocommand.
10761 * "c" is the character that was typed.
10762 * Return a pointer to allocated memory with the replacement string.
10763 * Return NULL to continue inserting "c".
10764 */
10765 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010766do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010767{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010768 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010769 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010770
10771 /* Return quickly when there is nothing to do. */
10772 if (!has_insertcharpre())
10773 return NULL;
10774
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010775# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010776 if (has_mbyte)
10777 buf[(*mb_char2bytes)(c, buf)] = NUL;
10778 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010779# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010780 {
10781 buf[0] = c;
10782 buf[1] = NUL;
10783 }
10784
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010785 /* Lock the text to avoid weird things from happening. */
10786 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010787 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010788
Bram Moolenaar704984a2012-06-01 14:57:51 +020010789 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010790 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010791 {
10792 /* Get the value of v:char. It may be empty or more than one
10793 * character. Only use it when changed, otherwise continue with the
10794 * original character to avoid breaking autoindent. */
10795 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10796 res = vim_strsave(get_vim_var_str(VV_CHAR));
10797 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010798
10799 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10800 --textlock;
10801
10802 return res;
10803}
10804#endif