blob: 1b79eccac9f7eff8a4c828958edf5747a41abf4d [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 Moolenaar0ab2a882009-05-13 10:51:08 +0000745 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 && curwin->w_wrow == W_WINROW(curwin)
747 + curwin->w_height - 1 - p_so
748 && (curwin->w_cursor.lnum != curwin->w_topline
749#ifdef FEAT_DIFF
750 || curwin->w_topfill > 0
751#endif
752 ))
753 {
754#ifdef FEAT_DIFF
755 if (curwin->w_topfill > 0)
756 --curwin->w_topfill;
757 else
758#endif
759#ifdef FEAT_FOLDING
760 if (hasFolding(curwin->w_topline, NULL, &old_topline))
761 set_topline(curwin, old_topline + 1);
762 else
763#endif
764 set_topline(curwin, curwin->w_topline + 1);
765 }
766 }
767
768 /* May need to adjust w_topline to show the cursor. */
769 update_topline();
770
771 did_backspace = FALSE;
772
773 validate_cursor(); /* may set must_redraw */
774
775 /*
776 * Redraw the display when no characters are waiting.
777 * Also shows mode, ruler and positions cursor.
778 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000779 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (curwin->w_p_scb)
782 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar860cae12010-06-05 23:22:07 +0200784 if (curwin->w_p_crb)
785 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 update_curswant();
787 old_topline = curwin->w_topline;
788#ifdef FEAT_DIFF
789 old_topfill = curwin->w_topfill;
790#endif
791
792#ifdef USE_ON_FLY_SCROLL
793 dont_scroll = FALSE; /* allow scrolling here */
794#endif
795
796 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100797 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100799 if (c != K_CURSORHOLD)
800 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200801
802 /* After using CTRL-G U the next cursor key will not break undo. */
803 if (dont_sync_undo == MAYBE)
804 dont_sync_undo = TRUE;
805 else
806 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100807 if (cmdchar == K_PS)
808 /* Got here from normal mode when bracketed paste started. */
809 c = K_PS;
810 else
811 do
812 {
813 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100814 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000816 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
817 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#ifdef FEAT_RIGHTLEFT
820 if (p_hkmap && KeyTyped)
821 c = hkmap(c); /* Hebrew mode mapping */
822#endif
823#ifdef FEAT_FKMAP
824 if (p_fkmap && KeyTyped)
825 c = fkmap(c); /* Farsi mode mapping */
826#endif
827
828#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000829 /*
830 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000831 * and the cursor is still in the completed word. Only when there is
832 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000833 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000834 if (compl_started
835 && pum_wanted()
836 && curwin->w_cursor.col >= compl_col
837 && (compl_shown_match == NULL
838 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000839 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 /* BS: Delete one character from "compl_leader". */
841 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000842 && curwin->w_cursor.col > compl_col
843 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000844 continue;
845
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000846 /* When no match was selected or it was edited. */
847 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000848 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000849 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000850 * "compl_leader". Except when at the original match and
851 * there is nothing to add, CTRL-L works like CTRL-P then. */
852 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100853 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000854 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000855 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000856 {
857 ins_compl_addfrommatch();
858 continue;
859 }
860
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000861 /* A non-white character that fits in with the current
862 * completion: Add to "compl_leader". */
863 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000864 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100865#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100866 /* Trigger InsertCharPre. */
867 char_u *str = do_insert_char_pre(c);
868 char_u *p;
869
870 if (str != NULL)
871 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100872 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100873 ins_compl_addleader(PTR2CHAR(p));
874 vim_free(str);
875 }
876 else
877#endif
878 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000879 continue;
880 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000881
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000882 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000883 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200884 if ((c == Ctrl_Y || (compl_enter_selects
885 && (c == CAR || c == K_KENTER || c == NL)))
886 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000887 {
888 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200889 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000890 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000891 }
892 }
893
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
895 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000896 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000897 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000898 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899#endif
900
Bram Moolenaar488c6512005-08-11 20:09:58 +0000901 /* CTRL-\ CTRL-N goes to Normal mode,
902 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
903 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (c == Ctrl_BSL)
905 {
906 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000907 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 ++no_mapping;
909 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000910 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 --no_mapping;
912 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000913 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000915 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 vungetc(c);
917 c = Ctrl_BSL;
918 }
919 else if (c == Ctrl_G && p_im)
920 continue;
921 else
922 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000923 if (c == Ctrl_O)
924 {
925 ins_ctrl_o();
926 ins_at_eol = FALSE; /* cursor keeps its column */
927 nomove = TRUE;
928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 count = 0;
930 goto doESCkey;
931 }
932 }
933
934#ifdef FEAT_DIGRAPHS
935 c = do_digraph(c);
936#endif
937
938#ifdef FEAT_INS_EXPAND
939 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
940 goto docomplete;
941#endif
942 if (c == Ctrl_V || c == Ctrl_Q)
943 {
944 ins_ctrl_v();
945 c = Ctrl_V; /* pretend CTRL-V is last typed character */
946 continue;
947 }
948
949#ifdef FEAT_CINDENT
950 if (cindent_on()
951# ifdef FEAT_INS_EXPAND
952 && ctrl_x_mode == 0
953# endif
954 )
955 {
956 /* A key name preceded by a bang means this key is not to be
957 * inserted. Skip ahead to the re-indenting below.
958 * A key name preceded by a star means that indenting has to be
959 * done before inserting the key. */
960 line_is_white = inindent(0);
961 if (in_cinkeys(c, '!', line_is_white))
962 goto force_cindent;
963 if (can_cindent && in_cinkeys(c, '*', line_is_white)
964 && stop_arrow() == OK)
965 do_c_expr_indent();
966 }
967#endif
968
969#ifdef FEAT_RIGHTLEFT
970 if (curwin->w_p_rl)
971 switch (c)
972 {
973 case K_LEFT: c = K_RIGHT; break;
974 case K_S_LEFT: c = K_S_RIGHT; break;
975 case K_C_LEFT: c = K_C_RIGHT; break;
976 case K_RIGHT: c = K_LEFT; break;
977 case K_S_RIGHT: c = K_S_LEFT; break;
978 case K_C_RIGHT: c = K_C_LEFT; break;
979 }
980#endif
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 /*
983 * If 'keymodel' contains "startsel", may start selection. If it
984 * does, a CTRL-O and c will be stuffed, we need to get these
985 * characters.
986 */
987 if (ins_start_select(c))
988 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 /*
991 * The big switch to handle a character in insert mode.
992 */
993 switch (c)
994 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (echeck_abbr(ESC + ABBR_OFF))
997 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200998 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#ifdef FEAT_CMDWIN
1002 if (c == Ctrl_C && cmdwin_type != 0)
1003 {
1004 /* Close the cmdline window. */
1005 cmdwin_result = K_IGNORE;
1006 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001007 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 goto doESCkey;
1009 }
1010#endif
1011
1012#ifdef UNIX
1013do_intr:
1014#endif
1015 /* when 'insertmode' set, and not halfway a mapping, don't leave
1016 * Insert mode */
1017 if (goto_im())
1018 {
1019 if (got_int)
1020 {
1021 (void)vgetc(); /* flush all buffers */
1022 got_int = FALSE;
1023 }
1024 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001025 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 break;
1027 }
1028doESCkey:
1029 /*
1030 * This is the ONLY return from edit()!
1031 */
1032 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1033 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001034 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 o_lnum = curwin->w_cursor.lnum;
1036
Bram Moolenaar488c6512005-08-11 20:09:58 +00001037 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001038 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00001039 if (cmdchar != 'r' && cmdchar != 'v')
1040 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1041 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001042 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 continue;
1046
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001047 case Ctrl_Z: /* suspend when 'insertmode' set */
1048 if (!p_im)
1049 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001050 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001051#ifdef CURSOR_SHAPE
1052 ui_cursor_shape(); /* may need to update cursor shape */
1053#endif
1054 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055
1056 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001057#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001058 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001059 goto docomplete;
1060#endif
1061 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1062 break;
1063 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001064
1065#ifdef FEAT_VIRTUALEDIT
1066 /* don't move the cursor left when 'virtualedit' has "onemore". */
1067 if (ve_flags & VE_ONEMORE)
1068 {
1069 ins_at_eol = FALSE;
1070 nomove = TRUE;
1071 }
1072#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 count = 0;
1074 goto doESCkey;
1075
Bram Moolenaar572cb562005-08-05 21:35:02 +00001076 case K_INS: /* toggle insert/replace mode */
1077 case K_KINS:
1078 ins_insert(replaceState);
1079 break;
1080
1081 case K_SELECT: /* end of Select mode mapping - ignore */
1082 break;
1083
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 case K_HELP: /* Help key works like <ESC> <Help> */
1085 case K_F1:
1086 case K_XF1:
1087 stuffcharReadbuff(K_HELP);
1088 if (p_im)
1089 need_start_insertmode = TRUE;
1090 goto doESCkey;
1091
1092#ifdef FEAT_NETBEANS_INTG
1093 case K_F21: /* NetBeans command */
1094 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001095 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001096 --no_mapping;
1097 netbeans_keycommand(i);
1098 break;
1099#endif
1100
1101 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 case NUL:
1103 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 /* For ^@ the trailing ESC will end the insert, unless there is an
1105 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1107 && c != Ctrl_A && !p_im)
1108 goto doESCkey; /* quit insert mode */
1109 inserted_space = FALSE;
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 ins_reg();
1114 auto_format(FALSE, TRUE);
1115 inserted_space = FALSE;
1116 break;
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 ins_ctrl_g();
1120 break;
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case Ctrl_HAT: /* switch input mode and/or langmap */
1123 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 break;
1125
1126#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001127 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (!p_ari)
1129 goto normalchar;
1130 ins_ctrl_();
1131 break;
1132#endif
1133
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001134 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1136 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1137 goto docomplete;
1138#endif
1139 /* FALLTHROUGH */
1140
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142# ifdef FEAT_INS_EXPAND
1143 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1144 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 if (has_compl_option(FALSE))
1146 goto docomplete;
1147 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149# endif
1150 ins_shift(c, lastc);
1151 auto_format(FALSE, TRUE);
1152 inserted_space = FALSE;
1153 break;
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 case K_KDEL:
1157 ins_del();
1158 auto_format(FALSE, TRUE);
1159 break;
1160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001161 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 case Ctrl_H:
1163 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1164 auto_format(FALSE, TRUE);
1165 break;
1166
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001167 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1169 auto_format(FALSE, TRUE);
1170 break;
1171
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001173# ifdef FEAT_COMPL_FUNC
1174 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001176 goto docomplete;
1177# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1179 auto_format(FALSE, TRUE);
1180 inserted_space = FALSE;
1181 break;
1182
1183#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001184 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 case K_LEFTMOUSE_NM:
1186 case K_LEFTDRAG:
1187 case K_LEFTRELEASE:
1188 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001189 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 case K_MIDDLEMOUSE:
1191 case K_MIDDLEDRAG:
1192 case K_MIDDLERELEASE:
1193 case K_RIGHTMOUSE:
1194 case K_RIGHTDRAG:
1195 case K_RIGHTRELEASE:
1196 case K_X1MOUSE:
1197 case K_X1DRAG:
1198 case K_X1RELEASE:
1199 case K_X2MOUSE:
1200 case K_X2DRAG:
1201 case K_X2RELEASE:
1202 ins_mouse(c);
1203 break;
1204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001206 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 break;
1208
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001210 ins_mousescroll(MSCR_UP);
1211 break;
1212
1213 case K_MOUSELEFT: /* Scroll wheel left */
1214 ins_mousescroll(MSCR_LEFT);
1215 break;
1216
1217 case K_MOUSERIGHT: /* Scroll wheel right */
1218 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 break;
1220#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001221 case K_PS:
1222 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1223 if (cmdchar == K_PS)
1224 /* invoked from normal mode, bail out */
1225 goto doESCkey;
1226 break;
1227 case K_PE:
1228 /* Got K_PE without K_PS, ignore. */
1229 break;
1230
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001231#ifdef FEAT_GUI_TABLINE
1232 case K_TABLINE:
1233 case K_TABMENU:
1234 ins_tabline(c);
1235 break;
1236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 break;
1240
Bram Moolenaar754b5602006-02-09 23:53:20 +00001241 case K_CURSORHOLD: /* Didn't type something for a while. */
1242 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1243 did_cursorhold = TRUE;
1244 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001245
Bram Moolenaar4770d092006-01-12 23:22:24 +00001246#ifdef FEAT_GUI_W32
1247 /* On Win32 ignore <M-F4>, we get it when closing the window was
1248 * cancelled. */
1249 case K_F4:
1250 if (mod_mask != MOD_MASK_ALT)
1251 goto normalchar;
1252 break;
1253#endif
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255#ifdef FEAT_GUI
1256 case K_VER_SCROLLBAR:
1257 ins_scroll();
1258 break;
1259
1260 case K_HOR_SCROLLBAR:
1261 ins_horscroll();
1262 break;
1263#endif
1264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001265 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 case K_S_HOME:
1268 case K_C_HOME:
1269 ins_home(c);
1270 break;
1271
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001272 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 case K_S_END:
1275 case K_C_END:
1276 ins_end(c);
1277 break;
1278
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001279 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001280 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1281 ins_s_left();
1282 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001283 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 break;
1285
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001286 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 case K_C_LEFT:
1288 ins_s_left();
1289 break;
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001292 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1293 ins_s_right();
1294 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001295 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 break;
1297
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001298 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 case K_C_RIGHT:
1300 ins_s_right();
1301 break;
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001304#ifdef FEAT_INS_EXPAND
1305 if (pum_visible())
1306 goto docomplete;
1307#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001308 if (mod_mask & MOD_MASK_SHIFT)
1309 ins_pageup();
1310 else
1311 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 break;
1313
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001314 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 case K_PAGEUP:
1316 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001317#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001318 if (pum_visible())
1319 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 ins_pageup();
1322 break;
1323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001325#ifdef FEAT_INS_EXPAND
1326 if (pum_visible())
1327 goto docomplete;
1328#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001329 if (mod_mask & MOD_MASK_SHIFT)
1330 ins_pagedown();
1331 else
1332 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 break;
1334
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001335 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 case K_PAGEDOWN:
1337 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001338#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001339 if (pum_visible())
1340 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 ins_pagedown();
1343 break;
1344
1345#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001346 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 ins_drop();
1348 break;
1349#endif
1350
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001351 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 c = TAB;
1353 /* FALLTHROUGH */
1354
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001355 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1357 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1358 goto docomplete;
1359#endif
1360 inserted_space = FALSE;
1361 if (ins_tab())
1362 goto normalchar; /* insert TAB as a normal char */
1363 auto_format(FALSE, TRUE);
1364 break;
1365
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001366 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 c = CAR;
1368 /* FALLTHROUGH */
1369 case CAR:
1370 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001371#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 /* In a quickfix window a <CR> jumps to the error under the
1373 * cursor. */
1374 if (bt_quickfix(curbuf) && c == CAR)
1375 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001376 if (curwin->w_llist_ref == NULL) /* quickfix window */
1377 do_cmdline_cmd((char_u *)".cc");
1378 else /* location list window */
1379 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 break;
1381 }
1382#endif
1383#ifdef FEAT_CMDWIN
1384 if (cmdwin_type != 0)
1385 {
1386 /* Execute the command in the cmdline window. */
1387 cmdwin_result = CAR;
1388 goto doESCkey;
1389 }
1390#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001391#ifdef FEAT_JOB_CHANNEL
1392 if (bt_prompt(curbuf))
1393 {
1394 buf_T *buf = curbuf;
1395
1396 invoke_prompt_callback();
1397 if (curbuf != buf)
1398 // buffer changed, get out of Insert mode
1399 goto doESCkey;
1400 break;
1401 }
1402#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001403 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 goto doESCkey; /* out of memory */
1405 auto_format(FALSE, FALSE);
1406 inserted_space = FALSE;
1407 break;
1408
Bram Moolenaar860cae12010-06-05 23:22:07 +02001409#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001410 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411# ifdef FEAT_INS_EXPAND
1412 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1413 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001414 if (has_compl_option(TRUE))
1415 goto docomplete;
1416 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418# endif
1419# ifdef FEAT_DIGRAPHS
1420 c = ins_digraph();
1421 if (c == NUL)
1422 break;
1423# endif
1424 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
1427#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001428 case Ctrl_X: /* Enter CTRL-X mode */
1429 ins_ctrl_x();
1430 break;
1431
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001432 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 if (ctrl_x_mode != CTRL_X_TAGS)
1434 goto normalchar;
1435 goto docomplete;
1436
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001437 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (ctrl_x_mode != CTRL_X_FILES)
1439 goto normalchar;
1440 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001441
1442 case 's': /* Spelling completion after ^X */
1443 case Ctrl_S:
1444 if (ctrl_x_mode != CTRL_X_SPELL)
1445 goto normalchar;
1446 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447#endif
1448
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450#ifdef FEAT_INS_EXPAND
1451 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1452#endif
1453 {
1454 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1455 if (p_im)
1456 {
1457 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1458 break;
1459 goto doESCkey;
1460 }
1461 goto normalchar;
1462 }
1463#ifdef FEAT_INS_EXPAND
1464 /* FALLTHROUGH */
1465
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001466 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 case Ctrl_N:
1468 /* if 'complete' is empty then plain ^P is no longer special,
1469 * but it is under other ^X modes */
1470 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001471 && (ctrl_x_mode == CTRL_X_NORMAL
1472 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001473 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 goto normalchar;
1475
1476docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001477 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001478#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001479 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001480#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001481 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001482 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001483#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001484 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001485#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001486 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 break;
1488#endif /* FEAT_INS_EXPAND */
1489
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001490 case Ctrl_Y: /* copy from previous line or scroll down */
1491 case Ctrl_E: /* copy from next line or scroll up */
1492 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 break;
1494
1495 default:
1496#ifdef UNIX
1497 if (c == intr_char) /* special interrupt char */
1498 goto do_intr;
1499#endif
1500
Bram Moolenaare659c952011-05-19 17:25:41 +02001501normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001503 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001505#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001506 if (!p_paste)
1507 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001508 /* Trigger InsertCharPre. */
1509 char_u *str = do_insert_char_pre(c);
1510 char_u *p;
1511
1512 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001513 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001514 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001515 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001516 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001517 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001518 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001519 c = PTR2CHAR(p);
1520 if (c == CAR || c == K_KENTER || c == NL)
1521 ins_eol(c);
1522 else
1523 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001524 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001525 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001526 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001527 vim_free(str);
1528 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001529 }
1530
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001531 /* If the new value is already inserted or an empty string
1532 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001533 if (c == NUL)
1534 break;
1535 }
1536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#ifdef FEAT_SMARTINDENT
1538 /* Try to perform smart-indenting. */
1539 ins_try_si(c);
1540#endif
1541
1542 if (c == ' ')
1543 {
1544 inserted_space = TRUE;
1545#ifdef FEAT_CINDENT
1546 if (inindent(0))
1547 can_cindent = FALSE;
1548#endif
1549 if (Insstart_blank_vcol == MAXCOL
1550 && curwin->w_cursor.lnum == Insstart.lnum)
1551 Insstart_blank_vcol = get_nolist_virtcol();
1552 }
1553
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001554 /* Insert a normal character and check for abbreviations on a
1555 * special character. Let CTRL-] expand abbreviations without
1556 * inserting it. */
1557 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558#ifdef FEAT_MBYTE
1559 /* Add ABBR_OFF for characters above 0x100, this is
1560 * what check_abbr() expects. */
1561 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1562#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001563 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
1565 insert_special(c, FALSE, FALSE);
1566#ifdef FEAT_RIGHTLEFT
1567 revins_legal++;
1568 revins_chars++;
1569#endif
1570 }
1571
1572 auto_format(FALSE, TRUE);
1573
1574#ifdef FEAT_FOLDING
1575 /* When inserting a character the cursor line must never be in a
1576 * closed fold. */
1577 foldOpenCursor();
1578#endif
1579 break;
1580 } /* end of switch (c) */
1581
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001582 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001583 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001584#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001585 /* but not in CTRL-X mode, a script can't restore the state */
1586 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001587#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001588 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001589 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* If the cursor was moved we didn't just insert a space */
1592 if (arrow_used)
1593 inserted_space = FALSE;
1594
1595#ifdef FEAT_CINDENT
1596 if (can_cindent && cindent_on()
1597# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001598 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599# endif
1600 )
1601 {
1602force_cindent:
1603 /*
1604 * Indent now if a key was typed that is in 'cinkeys'.
1605 */
1606 if (in_cinkeys(c, ' ', line_is_white))
1607 {
1608 if (stop_arrow() == OK)
1609 /* re-indent the current line */
1610 do_c_expr_indent();
1611 }
1612 }
1613#endif /* FEAT_CINDENT */
1614
1615 } /* for (;;) */
1616 /* NOTREACHED */
1617}
1618
1619/*
1620 * Redraw for Insert mode.
1621 * This is postponed until getting the next character to make '$' in the 'cpo'
1622 * option work correctly.
1623 * Only redraw when there are no characters available. This speeds up
1624 * inserting sequences of characters (e.g., for CTRL-R).
1625 */
1626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627ins_redraw(
1628 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001630#ifdef FEAT_CONCEAL
1631 linenr_T conceal_old_cursor_line = 0;
1632 linenr_T conceal_new_cursor_line = 0;
1633 int conceal_update_lines = FALSE;
1634#endif
1635
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001636 if (char_avail())
1637 return;
1638
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001639#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001640 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1641 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001642 if (ready && (has_cursormovedI()
1643# if defined(FEAT_CONCEAL)
1644 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001645# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001646 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001647 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001648# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001649 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001650# endif
1651 )
1652 {
1653# ifdef FEAT_SYN_HL
1654 /* Need to update the screen first, to make sure syntax
1655 * highlighting is correct after making a change (e.g., inserting
1656 * a "(". The autocommand may also require a redraw, so it's done
1657 * again below, unfortunately. */
1658 if (syntax_present(curwin) && must_redraw)
1659 update_screen(0);
1660# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001661 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001662 {
1663 /* Make sure curswant is correct, an autocommand may call
1664 * getcurpos(). */
1665 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001666 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001667 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001668# ifdef FEAT_CONCEAL
1669 if (curwin->w_p_cole > 0)
1670 {
1671 conceal_old_cursor_line = last_cursormoved.lnum;
1672 conceal_new_cursor_line = curwin->w_cursor.lnum;
1673 conceal_update_lines = TRUE;
1674 }
1675# endif
1676 last_cursormoved = curwin->w_cursor;
1677 }
1678#endif
1679
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001680 /* Trigger TextChangedI if b_changedtick differs. */
1681 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001682 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001683#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001684 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001685#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001686 )
1687 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001688 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1689 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001691
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001692#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001693 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1694 * TextChangedI will need to trigger for backwards compatibility, thus use
1695 * different b_last_changedtick* variables. */
1696 if (ready && has_textchangedP()
1697 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1698 && pum_visible())
1699 {
1700 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
1701 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1702 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001703#endif
1704
1705 if (must_redraw)
1706 update_screen(0);
1707 else if (clear_cmdline || redraw_cmdline)
1708 showmode(); /* clear cmdline and show mode */
1709# if defined(FEAT_CONCEAL)
1710 if ((conceal_update_lines
1711 && (conceal_old_cursor_line != conceal_new_cursor_line
1712 || conceal_cursor_line(curwin)))
1713 || need_cursor_line_redraw)
1714 {
1715 if (conceal_old_cursor_line != conceal_new_cursor_line)
1716 update_single_line(curwin, conceal_old_cursor_line);
1717 update_single_line(curwin, conceal_new_cursor_line == 0
1718 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1719 curwin->w_valid &= ~VALID_CROW;
1720 }
1721# endif
1722 showruler(FALSE);
1723 setcursor();
1724 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725}
1726
1727/*
1728 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1729 */
1730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001731ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001734 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
1736 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001737 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
1739 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001742 did_putchar = TRUE;
1743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1745
1746#ifdef FEAT_CMDL_INFO
1747 add_to_showcmd_c(Ctrl_V);
1748#endif
1749
1750 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001751 if (did_putchar)
1752 /* when the line fits in 'columns' the '^' is at the start of the next
1753 * line and will not removed by the redraw */
1754 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755#ifdef FEAT_CMDL_INFO
1756 clear_showcmd();
1757#endif
1758 insert_special(c, FALSE, TRUE);
1759#ifdef FEAT_RIGHTLEFT
1760 revins_chars++;
1761 revins_legal++;
1762#endif
1763}
1764
1765/*
1766 * Put a character directly onto the screen. It's not stored in a buffer.
1767 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1768 */
1769static int pc_status;
1770#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1771#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1772#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1773#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775static int pc_attr;
1776static int pc_row;
1777static int pc_col;
1778
1779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 int attr;
1783
1784 if (ScreenLines != NULL)
1785 {
1786 update_topline(); /* just in case w_topline isn't valid */
1787 validate_cursor();
1788 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001789 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 else
1791 attr = 0;
1792 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001793 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1795 pc_status = PC_STATUS_UNSET;
1796#endif
1797#ifdef FEAT_RIGHTLEFT
1798 if (curwin->w_p_rl)
1799 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001800 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801# ifdef FEAT_MBYTE
1802 if (has_mbyte)
1803 {
1804 int fix_col = mb_fix_col(pc_col, pc_row);
1805
1806 if (fix_col != pc_col)
1807 {
1808 screen_putchar(' ', pc_row, fix_col, attr);
1809 --curwin->w_wcol;
1810 pc_status = PC_STATUS_RIGHT;
1811 }
1812 }
1813# endif
1814 }
1815 else
1816#endif
1817 {
1818 pc_col += curwin->w_wcol;
1819#ifdef FEAT_MBYTE
1820 if (mb_lefthalve(pc_row, pc_col))
1821 pc_status = PC_STATUS_LEFT;
1822#endif
1823 }
1824
1825 /* save the character to be able to put it back */
1826#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1827 if (pc_status == PC_STATUS_UNSET)
1828#endif
1829 {
1830 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1831 pc_status = PC_STATUS_SET;
1832 }
1833 screen_putchar(c, pc_row, pc_col, attr);
1834 }
1835}
1836
Bram Moolenaarf2732452018-06-03 14:47:35 +02001837#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1838/*
1839 * Return the effective prompt for the current buffer.
1840 */
1841 char_u *
1842prompt_text(void)
1843{
1844 if (curbuf->b_prompt_text == NULL)
1845 return (char_u *)"% ";
1846 return curbuf->b_prompt_text;
1847}
1848
1849/*
1850 * Prepare for prompt mode: Make sure the last line has the prompt text.
1851 * Move the cursor to this line.
1852 */
1853 static void
1854init_prompt(int cmdchar_todo)
1855{
1856 char_u *prompt = prompt_text();
1857 char_u *text;
1858
1859 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1860 text = ml_get_curline();
1861 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1862 {
1863 // prompt is missing, insert it or append a line with it
1864 if (*text == NUL)
1865 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1866 else
1867 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1868 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1869 coladvance((colnr_T)MAXCOL);
1870 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1871 }
1872 if (cmdchar_todo == 'A')
1873 coladvance((colnr_T)MAXCOL);
1874 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
1875 curwin->w_cursor.col = STRLEN(prompt);
1876}
1877
1878/*
1879 * Return TRUE if the cursor is in the editable position of the prompt line.
1880 */
1881 int
1882prompt_curpos_editable()
1883{
1884 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1885 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1886}
1887#endif
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889/*
1890 * Undo the previous edit_putchar().
1891 */
1892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001893edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1896 {
1897#if defined(FEAT_MBYTE)
1898 if (pc_status == PC_STATUS_RIGHT)
1899 ++curwin->w_wcol;
1900 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1901 redrawWinline(curwin->w_cursor.lnum, FALSE);
1902 else
1903#endif
1904 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1905 }
1906}
1907
1908/*
1909 * Called when p_dollar is set: display a '$' at the end of the changed text
1910 * Only works when cursor is in the line that changes.
1911 */
1912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001913display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914{
1915 colnr_T save_col;
1916
1917 if (!redrawing())
1918 return;
1919
1920 cursor_off();
1921 save_col = curwin->w_cursor.col;
1922 curwin->w_cursor.col = col;
1923#ifdef FEAT_MBYTE
1924 if (has_mbyte)
1925 {
1926 char_u *p;
1927
1928 /* If on the last byte of a multi-byte move to the first byte. */
1929 p = ml_get_curline();
1930 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1931 }
1932#endif
1933 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001934 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
1936 edit_putchar('$', FALSE);
1937 dollar_vcol = curwin->w_virtcol;
1938 }
1939 curwin->w_cursor.col = save_col;
1940}
1941
1942/*
1943 * Call this function before moving the cursor from the normal insert position
1944 * in insert mode.
1945 */
1946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001947undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001949 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001951 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 redrawWinline(curwin->w_cursor.lnum, FALSE);
1953 }
1954}
1955
1956/*
1957 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1958 * Keep the cursor on the same character.
1959 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1960 * type == INDENT_DEC decrease indent (for CTRL-D)
1961 * type == INDENT_SET set indent to "amount"
1962 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1963 */
1964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001965change_indent(
1966 int type,
1967 int amount,
1968 int round,
1969 int replaced, /* replaced character, put on replace stack */
1970 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972 int vcol;
1973 int last_vcol;
1974 int insstart_less; /* reduction for Insstart.col */
1975 int new_cursor_col;
1976 int i;
1977 char_u *ptr;
1978 int save_p_list;
1979 int start_col;
1980 colnr_T vc;
1981#ifdef FEAT_VREPLACE
1982 colnr_T orig_col = 0; /* init for GCC */
1983 char_u *new_line, *orig_line = NULL; /* init for GCC */
1984
1985 /* VREPLACE mode needs to know what the line was like before changing */
1986 if (State & VREPLACE_FLAG)
1987 {
1988 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1989 orig_col = curwin->w_cursor.col;
1990 }
1991#endif
1992
1993 /* for the following tricks we don't want list mode */
1994 save_p_list = curwin->w_p_list;
1995 curwin->w_p_list = FALSE;
1996 vc = getvcol_nolist(&curwin->w_cursor);
1997 vcol = vc;
1998
1999 /*
2000 * For Replace mode we need to fix the replace stack later, which is only
2001 * possible when the cursor is in the indent. Remember the number of
2002 * characters before the cursor if it's possible.
2003 */
2004 start_col = curwin->w_cursor.col;
2005
2006 /* determine offset from first non-blank */
2007 new_cursor_col = curwin->w_cursor.col;
2008 beginline(BL_WHITE);
2009 new_cursor_col -= curwin->w_cursor.col;
2010
2011 insstart_less = curwin->w_cursor.col;
2012
2013 /*
2014 * If the cursor is in the indent, compute how many screen columns the
2015 * cursor is to the left of the first non-blank.
2016 */
2017 if (new_cursor_col < 0)
2018 vcol = get_indent() - vcol;
2019
2020 if (new_cursor_col > 0) /* can't fix replace stack */
2021 start_col = -1;
2022
2023 /*
2024 * Set the new indent. The cursor will be put on the first non-blank.
2025 */
2026 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002027 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 else
2029 {
2030#ifdef FEAT_VREPLACE
2031 int save_State = State;
2032
2033 /* Avoid being called recursively. */
2034 if (State & VREPLACE_FLAG)
2035 State = INSERT;
2036#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002037 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038#ifdef FEAT_VREPLACE
2039 State = save_State;
2040#endif
2041 }
2042 insstart_less -= curwin->w_cursor.col;
2043
2044 /*
2045 * Try to put cursor on same character.
2046 * If the cursor is at or after the first non-blank in the line,
2047 * compute the cursor column relative to the column of the first
2048 * non-blank character.
2049 * If we are not in insert mode, leave the cursor on the first non-blank.
2050 * If the cursor is before the first non-blank, position it relative
2051 * to the first non-blank, counted in screen columns.
2052 */
2053 if (new_cursor_col >= 0)
2054 {
2055 /*
2056 * When changing the indent while the cursor is touching it, reset
2057 * Insstart_col to 0.
2058 */
2059 if (new_cursor_col == 0)
2060 insstart_less = MAXCOL;
2061 new_cursor_col += curwin->w_cursor.col;
2062 }
2063 else if (!(State & INSERT))
2064 new_cursor_col = curwin->w_cursor.col;
2065 else
2066 {
2067 /*
2068 * Compute the screen column where the cursor should be.
2069 */
2070 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002071 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 /*
2074 * Advance the cursor until we reach the right screen column.
2075 */
2076 vcol = last_vcol = 0;
2077 new_cursor_col = -1;
2078 ptr = ml_get_curline();
2079 while (vcol <= (int)curwin->w_virtcol)
2080 {
2081 last_vcol = vcol;
2082#ifdef FEAT_MBYTE
2083 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002084 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 else
2086#endif
2087 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002088 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 vcol = last_vcol;
2091
2092 /*
2093 * May need to insert spaces to be able to position the cursor on
2094 * the right screen column.
2095 */
2096 if (vcol != (int)curwin->w_virtcol)
2097 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002098 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002100 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 if (ptr != NULL)
2102 {
2103 new_cursor_col += i;
2104 ptr[i] = NUL;
2105 while (--i >= 0)
2106 ptr[i] = ' ';
2107 ins_str(ptr);
2108 vim_free(ptr);
2109 }
2110 }
2111
2112 /*
2113 * When changing the indent while the cursor is in it, reset
2114 * Insstart_col to 0.
2115 */
2116 insstart_less = MAXCOL;
2117 }
2118
2119 curwin->w_p_list = save_p_list;
2120
2121 if (new_cursor_col <= 0)
2122 curwin->w_cursor.col = 0;
2123 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002124 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 curwin->w_set_curswant = TRUE;
2126 changed_cline_bef_curs();
2127
2128 /*
2129 * May have to adjust the start of the insert.
2130 */
2131 if (State & INSERT)
2132 {
2133 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2134 {
2135 if ((int)Insstart.col <= insstart_less)
2136 Insstart.col = 0;
2137 else
2138 Insstart.col -= insstart_less;
2139 }
2140 if ((int)ai_col <= insstart_less)
2141 ai_col = 0;
2142 else
2143 ai_col -= insstart_less;
2144 }
2145
2146 /*
2147 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2148 * If the number of characters before the cursor decreased, need to pop a
2149 * few characters from the replace stack.
2150 * If the number of characters before the cursor increased, need to push a
2151 * few NULs onto the replace stack.
2152 */
2153 if (REPLACE_NORMAL(State) && start_col >= 0)
2154 {
2155 while (start_col > (int)curwin->w_cursor.col)
2156 {
2157 replace_join(0); /* remove a NUL from the replace stack */
2158 --start_col;
2159 }
2160 while (start_col < (int)curwin->w_cursor.col || replaced)
2161 {
2162 replace_push(NUL);
2163 if (replaced)
2164 {
2165 replace_push(replaced);
2166 replaced = NUL;
2167 }
2168 ++start_col;
2169 }
2170 }
2171
2172#ifdef FEAT_VREPLACE
2173 /*
2174 * For VREPLACE mode, we also have to fix the replace stack. In this case
2175 * it is always possible because we backspace over the whole line and then
2176 * put it back again the way we wanted it.
2177 */
2178 if (State & VREPLACE_FLAG)
2179 {
2180 /* If orig_line didn't allocate, just return. At least we did the job,
2181 * even if you can't backspace. */
2182 if (orig_line == NULL)
2183 return;
2184
2185 /* Save new line */
2186 new_line = vim_strsave(ml_get_curline());
2187 if (new_line == NULL)
2188 return;
2189
2190 /* We only put back the new line up to the cursor */
2191 new_line[curwin->w_cursor.col] = NUL;
2192
2193 /* Put back original line */
2194 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2195 curwin->w_cursor.col = orig_col;
2196
2197 /* Backspace from cursor to start of line */
2198 backspace_until_column(0);
2199
2200 /* Insert new stuff into line again */
2201 ins_bytes(new_line);
2202
2203 vim_free(new_line);
2204 }
2205#endif
2206}
2207
2208/*
2209 * Truncate the space at the end of a line. This is to be used only in an
2210 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2211 * modes.
2212 */
2213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 int i;
2217
2218 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002219 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
2221 if (State & REPLACE_FLAG)
2222 replace_join(0); /* remove a NUL from the replace stack */
2223 }
2224 line[i + 1] = NUL;
2225}
2226
2227#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2228 || defined(FEAT_COMMENTS) || defined(PROTO)
2229/*
2230 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2231 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002232 * Will attempt not to go before "col" even when there is a composing
2233 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 */
2235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
2238 while ((int)curwin->w_cursor.col > col)
2239 {
2240 curwin->w_cursor.col--;
2241 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002242 replace_do_bs(col);
2243 else if (!del_char_after_col(col))
2244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
2246}
2247#endif
2248
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002249/*
2250 * Like del_char(), but make sure not to go before column "limit_col".
2251 * Only matters when there are composing characters.
2252 * Return TRUE when something was deleted.
2253 */
2254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002255del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002256{
2257#ifdef FEAT_MBYTE
2258 if (enc_utf8 && limit_col >= 0)
2259 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002260 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002261
2262 /* Make sure the cursor is at the start of a character, but
2263 * skip forward again when going too far back because of a
2264 * composing character. */
2265 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002266 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002267 {
2268 int l = utf_ptr2len(ml_get_cursor());
2269
2270 if (l == 0) /* end of line */
2271 break;
2272 curwin->w_cursor.col += l;
2273 }
2274 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2275 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002276 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002277 }
2278 else
2279#endif
2280 (void)del_char(FALSE);
2281 return TRUE;
2282}
2283
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2285/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002286 * CTRL-X pressed in Insert mode.
2287 */
2288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002289ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002290{
2291 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2292 * CTRL-V works like CTRL-N */
2293 if (ctrl_x_mode != CTRL_X_CMDLINE)
2294 {
2295 /* if the next ^X<> won't ADD nothing, then reset
2296 * compl_cont_status */
2297 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002298 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002299 else
2300 compl_cont_status = 0;
2301 /* We're not sure which CTRL-X mode it will be yet */
2302 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2303 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2304 edit_submode_pre = NULL;
2305 showmode();
2306 }
2307}
2308
2309/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002310 * Whether other than default completion has been selected.
2311 */
2312 int
2313ctrl_x_mode_not_default(void)
2314{
2315 return ctrl_x_mode != CTRL_X_NORMAL;
2316}
2317
2318/*
2319 * Whether CTRL-X was typed without a following character.
2320 */
2321 int
2322ctrl_x_mode_not_defined_yet(void)
2323{
2324 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2325}
2326
2327/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002328 * Return TRUE if the 'dict' or 'tsr' option can be used.
2329 */
2330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002331has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002332{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002333 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002334# ifdef FEAT_SPELL
2335 && !curwin->w_p_spell
2336# endif
2337 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002338 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2339 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002340 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002341 edit_submode = NULL;
2342 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2343 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002344 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002345 if (emsg_silent == 0)
2346 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002347 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002348 setcursor();
2349 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002350#ifdef FEAT_EVAL
2351 if (!get_vim_var_nr(VV_TESTING))
2352#endif
2353 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002354 }
2355 return FALSE;
2356 }
2357 return TRUE;
2358}
2359
2360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2362 * This depends on the current mode.
2363 */
2364 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002365vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367 /* Always allow ^R - let it's results then be checked */
2368 if (c == Ctrl_R)
2369 return TRUE;
2370
Bram Moolenaare3226be2005-12-18 22:10:00 +00002371 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002372 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002373 return TRUE;
2374
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 switch (ctrl_x_mode)
2376 {
2377 case 0: /* Not in any CTRL-X mode */
2378 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2379 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002380 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2382 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2383 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002384 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002385 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 case CTRL_X_SCROLL:
2387 return (c == Ctrl_Y || c == Ctrl_E);
2388 case CTRL_X_WHOLE_LINE:
2389 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2390 case CTRL_X_FILES:
2391 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2392 case CTRL_X_DICTIONARY:
2393 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2394 case CTRL_X_THESAURUS:
2395 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2396 case CTRL_X_TAGS:
2397 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2398#ifdef FEAT_FIND_ID
2399 case CTRL_X_PATH_PATTERNS:
2400 return (c == Ctrl_P || c == Ctrl_N);
2401 case CTRL_X_PATH_DEFINES:
2402 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2403#endif
2404 case CTRL_X_CMDLINE:
2405 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2406 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002407#ifdef FEAT_COMPL_FUNC
2408 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002409 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002410 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002411 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002412#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002413 case CTRL_X_SPELL:
2414 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002415 case CTRL_X_EVAL:
2416 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002418 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 return FALSE;
2420}
2421
2422/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002423 * Return TRUE when character "c" is part of the item currently being
2424 * completed. Used to decide whether to abandon complete mode when the menu
2425 * is visible.
2426 */
2427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002428ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002429{
2430 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2431 /* When expanding an identifier only accept identifier chars. */
2432 return vim_isIDc(c);
2433
2434 switch (ctrl_x_mode)
2435 {
2436 case CTRL_X_FILES:
2437 /* When expanding file name only accept file name chars. But not
2438 * path separators, so that "proto/<Tab>" expands files in
2439 * "proto", not "proto/" as a whole */
2440 return vim_isfilec(c) && !vim_ispathsep(c);
2441
2442 case CTRL_X_CMDLINE:
2443 case CTRL_X_OMNI:
2444 /* Command line and Omni completion can work with just about any
2445 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002446 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002447
2448 case CTRL_X_WHOLE_LINE:
2449 /* For while line completion a space can be part of the line. */
2450 return vim_isprintc(c);
2451 }
2452 return vim_iswordc(c);
2453}
2454
2455/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002456 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002458 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * the rest of the word to be in -- webb
2460 */
2461 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002462ins_compl_add_infercase(
2463 char_u *str,
2464 int len,
2465 int icase,
2466 char_u *fname,
2467 int dir,
2468 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002470 char_u *p;
2471 int i, c;
2472 int actual_len; /* Take multi-byte characters */
2473 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002474 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002475 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 int has_lower = FALSE;
2477 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002479 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002481 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaara2993e12007-08-12 14:38:46 +00002483 /* Find actual length of completion. */
2484#ifdef FEAT_MBYTE
2485 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002487 p = str;
2488 actual_len = 0;
2489 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002491 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002492 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002495 else
2496#endif
2497 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
Bram Moolenaara2993e12007-08-12 14:38:46 +00002499 /* Find actual length of original text. */
2500#ifdef FEAT_MBYTE
2501 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002503 p = compl_orig_text;
2504 actual_compl_length = 0;
2505 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002507 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002508 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002511 else
2512#endif
2513 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002515 /* "actual_len" may be smaller than "actual_compl_length" when using
2516 * thesaurus, only use the minimum when comparing. */
2517 min_len = actual_len < actual_compl_length
2518 ? actual_len : actual_compl_length;
2519
Bram Moolenaara2993e12007-08-12 14:38:46 +00002520 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002521 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002522 if (wca != NULL)
2523 {
2524 p = str;
2525 for (i = 0; i < actual_len; ++i)
2526#ifdef FEAT_MBYTE
2527 if (has_mbyte)
2528 wca[i] = mb_ptr2char_adv(&p);
2529 else
2530#endif
2531 wca[i] = *(p++);
2532
2533 /* Rule 1: Were any chars converted to lower? */
2534 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002535 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002536 {
2537#ifdef FEAT_MBYTE
2538 if (has_mbyte)
2539 c = mb_ptr2char_adv(&p);
2540 else
2541#endif
2542 c = *(p++);
2543 if (MB_ISLOWER(c))
2544 {
2545 has_lower = TRUE;
2546 if (MB_ISUPPER(wca[i]))
2547 {
2548 /* Rule 1 is satisfied. */
2549 for (i = actual_compl_length; i < actual_len; ++i)
2550 wca[i] = MB_TOLOWER(wca[i]);
2551 break;
2552 }
2553 }
2554 }
2555
2556 /*
2557 * Rule 2: No lower case, 2nd consecutive letter converted to
2558 * upper case.
2559 */
2560 if (!has_lower)
2561 {
2562 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002563 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002564 {
2565#ifdef FEAT_MBYTE
2566 if (has_mbyte)
2567 c = mb_ptr2char_adv(&p);
2568 else
2569#endif
2570 c = *(p++);
2571 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2572 {
2573 /* Rule 2 is satisfied. */
2574 for (i = actual_compl_length; i < actual_len; ++i)
2575 wca[i] = MB_TOUPPER(wca[i]);
2576 break;
2577 }
2578 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2579 }
2580 }
2581
2582 /* Copy the original case of the part we typed. */
2583 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002584 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002585 {
2586#ifdef FEAT_MBYTE
2587 if (has_mbyte)
2588 c = mb_ptr2char_adv(&p);
2589 else
2590#endif
2591 c = *(p++);
2592 if (MB_ISLOWER(c))
2593 wca[i] = MB_TOLOWER(wca[i]);
2594 else if (MB_ISUPPER(c))
2595 wca[i] = MB_TOUPPER(wca[i]);
2596 }
2597
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002598 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002599 * Generate encoding specific output from wide character array.
2600 * Multi-byte characters can occupy up to five bytes more than
2601 * ASCII characters, and we also need one byte for NUL, so stay
2602 * six bytes away from the edge of IObuff.
2603 */
2604 p = IObuff;
2605 i = 0;
2606 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2607#ifdef FEAT_MBYTE
2608 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002609 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002610 else
2611#endif
2612 *(p++) = wca[i++];
2613 *p = NUL;
2614
2615 vim_free(wca);
2616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002618 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2619 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002621 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622}
2623
2624/*
2625 * Add a match to the list of matches.
2626 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002627 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002628 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002631ins_compl_add(
2632 char_u *str,
2633 int len,
2634 int icase,
2635 char_u *fname,
2636 char_u **cptext, /* extra text for popup menu or NULL */
2637 int cdir,
2638 int flags,
2639 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002641 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002642 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
2644 ui_breakcheck();
2645 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002646 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (len < 0)
2648 len = (int)STRLEN(str);
2649
2650 /*
2651 * If the same match is already present, don't add it.
2652 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002653 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002655 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 do
2657 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002658 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002659 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002660 && match->cp_str[len] == NUL)
2661 return NOTDONE;
2662 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002663 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002666 /* Remove any popup menu before changing the list of matches. */
2667 ins_compl_del_pum();
2668
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 /*
2670 * Allocate a new match structure.
2671 * Copy the values to the new match structure.
2672 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002673 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002675 return FAIL;
2676 match->cp_number = -1;
2677 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002678 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002679 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
2681 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002682 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002684 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002687 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2689 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002690 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002691 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002692 && compl_curr_match->cp_fname != NULL
2693 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002694 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002695 else if (fname != NULL)
2696 {
2697 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002698 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002701 match->cp_fname = NULL;
2702 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002703
2704 if (cptext != NULL)
2705 {
2706 int i;
2707
2708 for (i = 0; i < CPT_COUNT; ++i)
2709 if (cptext[i] != NULL && *cptext[i] != NUL)
2710 match->cp_text[i] = vim_strsave(cptext[i]);
2711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
2713 /*
2714 * Link the new match structure in the list of matches.
2715 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002716 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002717 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 else if (dir == FORWARD)
2719 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002720 match->cp_next = compl_curr_match->cp_next;
2721 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723 else /* BACKWARD */
2724 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002725 match->cp_next = compl_curr_match;
2726 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002728 if (match->cp_next)
2729 match->cp_next->cp_prev = match;
2730 if (match->cp_prev)
2731 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002733 compl_first_match = match;
2734 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002736 /*
2737 * Find the longest common string if still doing that.
2738 */
2739 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2740 ins_compl_longest_match(match);
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 return OK;
2743}
2744
2745/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002746 * Return TRUE if "str[len]" matches with match->cp_str, considering
2747 * match->cp_icase.
2748 */
2749 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002750ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002751{
2752 if (match->cp_icase)
2753 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2754 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2755}
2756
2757/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002758 * Reduce the longest common string for match "match".
2759 */
2760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002761ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002762{
2763 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002764 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002765 int had_match;
2766
2767 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002768 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002769 /* First match, use it as a whole. */
2770 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002771 if (compl_leader != NULL)
2772 {
2773 had_match = (curwin->w_cursor.col > compl_col);
2774 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002775 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002776 ins_redraw(FALSE);
2777
2778 /* When the match isn't there (to avoid matching itself) remove it
2779 * again after redrawing. */
2780 if (!had_match)
2781 ins_compl_delete();
2782 compl_used_match = FALSE;
2783 }
2784 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002785 else
2786 {
2787 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002788 p = compl_leader;
2789 s = match->cp_str;
2790 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002791 {
2792#ifdef FEAT_MBYTE
2793 if (has_mbyte)
2794 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002795 c1 = mb_ptr2char(p);
2796 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002797 }
2798 else
2799#endif
2800 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002801 c1 = *p;
2802 c2 = *s;
2803 }
2804 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2805 : (c1 != c2))
2806 break;
2807#ifdef FEAT_MBYTE
2808 if (has_mbyte)
2809 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002810 MB_PTR_ADV(p);
2811 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002812 }
2813 else
2814#endif
2815 {
2816 ++p;
2817 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002818 }
2819 }
2820
2821 if (*p != NUL)
2822 {
2823 /* Leader was shortened, need to change the inserted text. */
2824 *p = NUL;
2825 had_match = (curwin->w_cursor.col > compl_col);
2826 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002827 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002828 ins_redraw(FALSE);
2829
2830 /* When the match isn't there (to avoid matching itself) remove it
2831 * again after redrawing. */
2832 if (!had_match)
2833 ins_compl_delete();
2834 }
2835
2836 compl_used_match = FALSE;
2837 }
2838}
2839
2840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 * Add an array of matches to the list of matches.
2842 * Frees matches[].
2843 */
2844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845ins_compl_add_matches(
2846 int num_matches,
2847 char_u **matches,
2848 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 int i;
2851 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002852 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
Bram Moolenaar572cb562005-08-05 21:35:02 +00002854 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002855 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002856 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002858 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 FreeWild(num_matches, matches);
2860}
2861
2862/* Make the completion list cyclic.
2863 * Return the number of matches (excluding the original).
2864 */
2865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002866ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002868 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 int count = 0;
2870
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002871 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
2873 /*
2874 * Find the end of the list.
2875 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002876 match = compl_first_match;
2877 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002878 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002880 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 ++count;
2882 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002883 match->cp_next = compl_first_match;
2884 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
2886 return count;
2887}
2888
Bram Moolenaara94bc432006-03-10 21:42:59 +00002889/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002890 * Set variables that store noselect and noinsert behavior from the
2891 * 'completeopt' value.
2892 */
2893 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002894completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002895{
2896 compl_no_insert = FALSE;
2897 compl_no_select = FALSE;
2898 if (strstr((char *)p_cot, "noselect") != NULL)
2899 compl_no_select = TRUE;
2900 if (strstr((char *)p_cot, "noinsert") != NULL)
2901 compl_no_insert = TRUE;
2902}
2903
2904/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002905 * Start completion for the complete() function.
2906 * "startcol" is where the matched text starts (1 is first column).
2907 * "list" is the list of matches.
2908 */
2909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002910set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002911{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002912 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002913 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002914
Bram Moolenaara94bc432006-03-10 21:42:59 +00002915 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002916 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002917 ins_compl_prep(' ');
2918 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002919 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002920
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002921 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002922 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002923 startcol = curwin->w_cursor.col;
2924 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002925 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002926 /* compl_pattern doesn't need to be set */
2927 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2928 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002929 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002930 return;
2931
Bram Moolenaare4214502015-03-05 18:08:43 +01002932 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002933
2934 ins_compl_add_list(list);
2935 compl_matches = ins_compl_make_cyclic();
2936 compl_started = TRUE;
2937 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002938 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002939
2940 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002941 if (compl_no_insert || compl_no_select)
2942 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002943 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002944 if (compl_no_select)
2945 /* Down/Up has no real effect. */
2946 ins_complete(K_UP, FALSE);
2947 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002948 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002949 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002950 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002951
2952 /* Lazily show the popup menu, unless we got interrupted. */
2953 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002954 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002955 out_flush();
2956}
2957
2958
Bram Moolenaar9372a112005-12-06 19:59:18 +00002959/* "compl_match_array" points the currently displayed list of entries in the
2960 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002961static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002962static int compl_match_arraysize;
2963
2964/*
2965 * Update the screen and when there is any scrolling remove the popup menu.
2966 */
2967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002968ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002969{
2970 int h;
2971
2972 if (compl_match_array != NULL)
2973 {
2974 h = curwin->w_cline_height;
2975 update_screen(0);
2976 if (h != curwin->w_cline_height)
2977 ins_compl_del_pum();
2978 }
2979}
2980
2981/*
2982 * Remove any popup menu.
2983 */
2984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002985ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002986{
2987 if (compl_match_array != NULL)
2988 {
2989 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01002990 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002991 }
2992}
2993
2994/*
2995 * Return TRUE if the popup menu should be displayed.
2996 */
2997 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002998pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002999{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003000 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003001 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003002 return FALSE;
3003
3004 /* The display looks bad on a B&W display. */
3005 if (t_colors < 8
3006#ifdef FEAT_GUI
3007 && !gui.in_use
3008#endif
3009 )
3010 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003011 return TRUE;
3012}
3013
3014/*
3015 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003016 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003017 */
3018 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003019pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003020{
3021 compl_T *compl;
3022 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003023
3024 /* Don't display the popup menu if there are no matches or there is only
3025 * one (ignoring the original text). */
3026 compl = compl_first_match;
3027 i = 0;
3028 do
3029 {
3030 if (compl == NULL
3031 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3032 break;
3033 compl = compl->cp_next;
3034 } while (compl != compl_first_match);
3035
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003036 if (strstr((char *)p_cot, "menuone") != NULL)
3037 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003038 return (i >= 2);
3039}
3040
3041/*
3042 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003043 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003044 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003046ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003047{
3048 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003049 compl_T *shown_compl = NULL;
3050 int did_find_shown_match = FALSE;
3051 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003052 int i;
3053 int cur = -1;
3054 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003055 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003057 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003058 return;
3059
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003060#if defined(FEAT_EVAL)
3061 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3062 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3063#endif
3064
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003065 /* Update the screen before drawing the popup menu over it. */
3066 update_screen(0);
3067
3068 if (compl_match_array == NULL)
3069 {
3070 /* Need to build the popup menu list. */
3071 compl_match_arraysize = 0;
3072 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003073 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003074 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003075 do
3076 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003077 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3078 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003079 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003080 ++compl_match_arraysize;
3081 compl = compl->cp_next;
3082 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003083 if (compl_match_arraysize == 0)
3084 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003085 compl_match_array = (pumitem_T *)alloc_clear(
3086 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003087 * compl_match_arraysize));
3088 if (compl_match_array != NULL)
3089 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003090 /* If the current match is the original text don't find the first
3091 * match after it, don't highlight anything. */
3092 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3093 shown_match_ok = TRUE;
3094
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003095 i = 0;
3096 compl = compl_first_match;
3097 do
3098 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003099 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3100 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003101 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003102 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003103 if (!shown_match_ok)
3104 {
3105 if (compl == compl_shown_match || did_find_shown_match)
3106 {
3107 /* This item is the shown match or this is the
3108 * first displayed item after the shown match. */
3109 compl_shown_match = compl;
3110 did_find_shown_match = TRUE;
3111 shown_match_ok = TRUE;
3112 }
3113 else
3114 /* Remember this displayed match for when the
3115 * shown match is just below it. */
3116 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003117 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003118 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003119
3120 if (compl->cp_text[CPT_ABBR] != NULL)
3121 compl_match_array[i].pum_text =
3122 compl->cp_text[CPT_ABBR];
3123 else
3124 compl_match_array[i].pum_text = compl->cp_str;
3125 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3126 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3127 if (compl->cp_text[CPT_MENU] != NULL)
3128 compl_match_array[i++].pum_extra =
3129 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003130 else
3131 compl_match_array[i++].pum_extra = compl->cp_fname;
3132 }
3133
3134 if (compl == compl_shown_match)
3135 {
3136 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003137
3138 /* When the original text is the shown match don't set
3139 * compl_shown_match. */
3140 if (compl->cp_flags & ORIGINAL_TEXT)
3141 shown_match_ok = TRUE;
3142
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003143 if (!shown_match_ok && shown_compl != NULL)
3144 {
3145 /* The shown match isn't displayed, set it to the
3146 * previously displayed match. */
3147 compl_shown_match = shown_compl;
3148 shown_match_ok = TRUE;
3149 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003150 }
3151 compl = compl->cp_next;
3152 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003153
3154 if (!shown_match_ok) /* no displayed match at all */
3155 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003156 }
3157 }
3158 else
3159 {
3160 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003161 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003162 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3163 || compl_match_array[i].pum_text
3164 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003165 {
3166 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003167 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003168 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003169 }
3170
3171 if (compl_match_array != NULL)
3172 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003173 /* In Replace mode when a $ is displayed at the end of the line only
3174 * part of the screen would be updated. We do need to redraw here. */
3175 dollar_vcol = -1;
3176
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003177 /* Compute the screen column of the start of the completed text.
3178 * Use the cursor to get all wrapping and other settings right. */
3179 col = curwin->w_cursor.col;
3180 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003181 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003182 curwin->w_cursor.col = col;
3183 }
3184}
3185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186#define DICT_FIRST (1) /* use just first element in "dict" */
3187#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003188
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003190 * Add any identifiers that match the given pattern in the list of dictionary
3191 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 */
3193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003194ins_compl_dictionaries(
3195 char_u *dict_start,
3196 char_u *pat,
3197 int flags, /* DICT_FIRST and/or DICT_EXACT */
3198 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003200 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 char_u *ptr;
3202 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 char_u **files;
3205 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003207 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaar0b238792006-03-02 22:49:12 +00003209 if (*dict == NUL)
3210 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003211#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003212 /* When 'dictionary' is empty and spell checking is enabled use
3213 * "spell". */
3214 if (!thesaurus && curwin->w_p_spell)
3215 dict = (char_u *)"spell";
3216 else
3217#endif
3218 return;
3219 }
3220
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003222 if (buf == NULL)
3223 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003224 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003225
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 /* If 'infercase' is set, don't use 'smartcase' here */
3227 save_p_scs = p_scs;
3228 if (curbuf->b_p_inf)
3229 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003230
3231 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3232 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003233 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003234 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003235 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003236 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003237 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003238
3239 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003240 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003241 len = STRLEN(pat_esc) + 10;
3242 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003243 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003244 {
3245 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003246 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003247 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003248 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003249 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3250 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003251 vim_free(ptr);
3252 }
3253 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003254 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003255 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003256 if (regmatch.regprog == NULL)
3257 goto theend;
3258 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3261 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003262 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
3264 /* copy one dictionary file name into buf */
3265 if (flags == DICT_EXACT)
3266 {
3267 count = 1;
3268 files = &dict;
3269 }
3270 else
3271 {
3272 /* Expand wildcards in the dictionary name, but do not allow
3273 * backticks (for security, the 'dict' option may have been set in
3274 * a modeline). */
3275 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003276# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003277 if (!thesaurus && STRCMP(buf, "spell") == 0)
3278 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003279 else
3280# endif
3281 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 || expand_wildcards(1, &buf, &count, &files,
3283 EW_FILE|EW_SILENT) != OK)
3284 count = 0;
3285 }
3286
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003287# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003288 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003290 /* Complete from active spelling. Skip "\<" in the pattern, we
3291 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003292 if (pat[0] == '\\' && pat[1] == '<')
3293 ptr = pat + 2;
3294 else
3295 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003296 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003298 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003299# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003300 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003301 {
3302 ins_compl_files(count, files, thesaurus, flags,
3303 &regmatch, buf, &dir);
3304 if (flags != DICT_EXACT)
3305 FreeWild(count, files);
3306 }
3307 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 break;
3309 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003310
3311theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003313 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 vim_free(buf);
3315}
3316
Bram Moolenaar0b238792006-03-02 22:49:12 +00003317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003318ins_compl_files(
3319 int count,
3320 char_u **files,
3321 int thesaurus,
3322 int flags,
3323 regmatch_T *regmatch,
3324 char_u *buf,
3325 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003326{
3327 char_u *ptr;
3328 int i;
3329 FILE *fp;
3330 int add_r;
3331
3332 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3333 {
3334 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3335 if (flags != DICT_EXACT)
3336 {
3337 vim_snprintf((char *)IObuff, IOSIZE,
3338 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003339 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003340 }
3341
3342 if (fp != NULL)
3343 {
3344 /*
3345 * Read dictionary file line by line.
3346 * Check each line for a match.
3347 */
3348 while (!got_int && !compl_interrupted
3349 && !vim_fgets(buf, LSIZE, fp))
3350 {
3351 ptr = buf;
3352 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3353 {
3354 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003355 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003356 ptr = find_line_end(ptr);
3357 else
3358 ptr = find_word_end(ptr);
3359 add_r = ins_compl_add_infercase(regmatch->startp[0],
3360 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003361 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003362 if (thesaurus)
3363 {
3364 char_u *wstart;
3365
3366 /*
3367 * Add the other matches on the line
3368 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003369 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003370 while (!got_int)
3371 {
3372 /* Find start of the next word. Skip white
3373 * space and punctuation. */
3374 ptr = find_word_start(ptr);
3375 if (*ptr == NUL || *ptr == NL)
3376 break;
3377 wstart = ptr;
3378
Bram Moolenaara2993e12007-08-12 14:38:46 +00003379 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003380#ifdef FEAT_MBYTE
3381 if (has_mbyte)
3382 /* Japanese words may have characters in
3383 * different classes, only separate words
3384 * with single-byte non-word characters. */
3385 while (*ptr != NUL)
3386 {
3387 int l = (*mb_ptr2len)(ptr);
3388
3389 if (l < 2 && !vim_iswordc(*ptr))
3390 break;
3391 ptr += l;
3392 }
3393 else
3394#endif
3395 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003396
3397 /* Add the word. Skip the regexp match. */
3398 if (wstart != regmatch->startp[0])
3399 add_r = ins_compl_add_infercase(wstart,
3400 (int)(ptr - wstart),
3401 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003402 }
3403 }
3404 if (add_r == OK)
3405 /* if dir was BACKWARD then honor it just once */
3406 *dir = FORWARD;
3407 else if (add_r == FAIL)
3408 break;
3409 /* avoid expensive call to vim_regexec() when at end
3410 * of line */
3411 if (*ptr == '\n' || got_int)
3412 break;
3413 }
3414 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003415 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003416 }
3417 fclose(fp);
3418 }
3419 }
3420}
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422/*
3423 * Find the start of the next word.
3424 * Returns a pointer to the first char of the word. Also stops at a NUL.
3425 */
3426 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003427find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429#ifdef FEAT_MBYTE
3430 if (has_mbyte)
3431 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003432 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 else
3434#endif
3435 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3436 ++ptr;
3437 return ptr;
3438}
3439
3440/*
3441 * Find the end of the word. Assumes it starts inside a word.
3442 * Returns a pointer to just after the word.
3443 */
3444 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003445find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447#ifdef FEAT_MBYTE
3448 int start_class;
3449
3450 if (has_mbyte)
3451 {
3452 start_class = mb_get_class(ptr);
3453 if (start_class > 1)
3454 while (*ptr != NUL)
3455 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003456 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (mb_get_class(ptr) != start_class)
3458 break;
3459 }
3460 }
3461 else
3462#endif
3463 while (vim_iswordc(*ptr))
3464 ++ptr;
3465 return ptr;
3466}
3467
3468/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003469 * Find the end of the line, omitting CR and NL at the end.
3470 * Returns a pointer to just after the line.
3471 */
3472 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003473find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003474{
3475 char_u *s;
3476
3477 s = ptr + STRLEN(ptr);
3478 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3479 --s;
3480 return s;
3481}
3482
3483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 * Free the list of completions
3485 */
3486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003487ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003489 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003490 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaard23a8232018-02-10 18:45:26 +01003492 VIM_CLEAR(compl_pattern);
3493 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003495 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003497
3498 ins_compl_del_pum();
3499 pum_clear();
3500
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003501 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 do
3503 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003505 compl_curr_match = compl_curr_match->cp_next;
3506 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003508 if (match->cp_flags & FREE_FNAME)
3509 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003510 for (i = 0; i < CPT_COUNT; ++i)
3511 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003513 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3514 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003515 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003516 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517}
3518
3519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003520ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003522 compl_cont_status = 0;
3523 compl_started = FALSE;
3524 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003525 VIM_CLEAR(compl_pattern);
3526 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003528 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003529 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003530 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003531 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532}
3533
3534/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003535 * Return TRUE when Insert completion is active.
3536 */
3537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003538ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003539{
3540 return compl_started;
3541}
3542
3543/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003544 * Delete one character before the cursor and show the subset of the matches
3545 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003546 * Returns the character to be used, NUL if the work is done and another char
3547 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003548 */
3549 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003550ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003551{
3552 char_u *line;
3553 char_u *p;
3554
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003555 line = ml_get_curline();
3556 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003557 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003558
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003559 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003560 * allow the word to be deleted, we won't match everything.
3561 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003562 if ((int)(p - line) - (int)compl_col < 0
3563 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003564 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3565 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3566 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003567 return K_BS;
3568
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003569 /* Deleted more than what was used to find matches or didn't finish
3570 * finding all matches: need to look for matches all over again. */
3571 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003572 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003573 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003574
Bram Moolenaara6557602006-02-04 22:43:20 +00003575 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003576 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003577 if (compl_leader != NULL)
3578 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003579 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003580 if (compl_shown_match != NULL)
3581 /* Make sure current match is not a hidden item. */
3582 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003583 return NUL;
3584 }
3585 return K_BS;
3586}
Bram Moolenaara6557602006-02-04 22:43:20 +00003587
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003588/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003589 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3590 * be called.
3591 */
3592 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003593ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003594{
3595 /* Return TRUE if we didn't complete finding matches or when the
3596 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3597 return compl_was_interrupted
3598 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3599 && compl_opt_refresh_always);
3600}
3601
3602/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003603 * Called after changing "compl_leader".
3604 * Show the popup menu with a different set of matches.
3605 * May also search for matches again if the previous search was interrupted.
3606 */
3607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003608ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003609{
3610 ins_compl_del_pum();
3611 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003612 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003613 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003614
3615 if (compl_started)
3616 ins_compl_set_original_text(compl_leader);
3617 else
3618 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003619#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003620 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003621#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003622 /*
3623 * Matches were cleared, need to search for them now. First display
3624 * the changed text before the cursor. Set "compl_restarting" to
3625 * avoid that the first match is inserted.
3626 */
3627 update_screen(0);
3628#ifdef FEAT_GUI
3629 if (gui.in_use)
3630 {
3631 /* Show the cursor after the match, not after the redrawn text. */
3632 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003633 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003634 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003635#endif
3636 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003637 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003638 compl_cont_status = 0;
3639 compl_restarting = FALSE;
3640 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003641
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003642 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003643
3644 /* Show the popup menu with a different set of matches. */
3645 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003646
3647 /* Don't let Enter select the original text when there is no popup menu. */
3648 if (compl_match_array == NULL)
3649 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003650}
3651
3652/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003653 * Return the length of the completion, from the completion start column to
3654 * the cursor column. Making sure it never goes below zero.
3655 */
3656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003658{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003659 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003660
3661 if (off < 0)
3662 return 0;
3663 return off;
3664}
3665
3666/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003667 * Append one character to the match leader. May reduce the number of
3668 * matches.
3669 */
3670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003671ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003672{
3673#ifdef FEAT_MBYTE
3674 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003675#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003676
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003677 if (stop_arrow() == FAIL)
3678 return;
3679#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003680 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3681 {
3682 char_u buf[MB_MAXBYTES + 1];
3683
3684 (*mb_char2bytes)(c, buf);
3685 buf[cc] = NUL;
3686 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003687 if (compl_opt_refresh_always)
3688 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003689 }
3690 else
3691#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003692 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003693 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003694 if (compl_opt_refresh_always)
3695 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003696 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003697
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003698 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003699 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003700 ins_compl_restart();
3701
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003702 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003703 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003704 * break redo. */
3705 if (!compl_opt_refresh_always)
3706 {
3707 vim_free(compl_leader);
3708 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003709 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003710 if (compl_leader != NULL)
3711 ins_compl_new_leader();
3712 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003713}
3714
3715/*
3716 * Setup for finding completions again without leaving CTRL-X mode. Used when
3717 * BS or a key was typed while still searching for matches.
3718 */
3719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003721{
3722 ins_compl_free();
3723 compl_started = FALSE;
3724 compl_matches = 0;
3725 compl_cont_status = 0;
3726 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003727}
3728
3729/*
3730 * Set the first match, the original text.
3731 */
3732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003733ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003734{
3735 char_u *p;
3736
Bram Moolenaare87edf32018-04-17 22:14:32 +02003737 /* Replace the original text entry.
3738 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3739 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003740 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3741 {
3742 p = vim_strsave(str);
3743 if (p != NULL)
3744 {
3745 vim_free(compl_first_match->cp_str);
3746 compl_first_match->cp_str = p;
3747 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003748 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003749 else if (compl_first_match->cp_prev != NULL
3750 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3751 {
3752 p = vim_strsave(str);
3753 if (p != NULL)
3754 {
3755 vim_free(compl_first_match->cp_prev->cp_str);
3756 compl_first_match->cp_prev->cp_str = p;
3757 }
3758 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003759}
3760
3761/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003762 * Append one character to the match leader. May reduce the number of
3763 * matches.
3764 */
3765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003767{
3768 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003769 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003770 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003771 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003772
3773 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003774 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003775 {
3776 /* When still at the original match use the first entry that matches
3777 * the leader. */
3778 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3779 {
3780 p = NULL;
3781 for (cp = compl_shown_match->cp_next; cp != NULL
3782 && cp != compl_first_match; cp = cp->cp_next)
3783 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003784 if (compl_leader == NULL
3785 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003786 (int)STRLEN(compl_leader)))
3787 {
3788 p = cp->cp_str;
3789 break;
3790 }
3791 }
3792 if (p == NULL || (int)STRLEN(p) <= len)
3793 return;
3794 }
3795 else
3796 return;
3797 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003798 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003799 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003800 ins_compl_addleader(c);
3801}
3802
3803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003805 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003806 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003808 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003809ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
3811 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003813 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 /* Forget any previous 'special' messages if this is actually
3816 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3817 */
3818 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3819 edit_submode_extra = NULL;
3820
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003821 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003822 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3823 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003824 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003826 /* Set "compl_get_longest" when finding the first matches. */
3827 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003828 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003829 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003830 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003831 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003832
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003833 }
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3836 {
3837 /*
3838 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3839 * it will be yet. Now we decide.
3840 */
3841 switch (c)
3842 {
3843 case Ctrl_E:
3844 case Ctrl_Y:
3845 ctrl_x_mode = CTRL_X_SCROLL;
3846 if (!(State & REPLACE_FLAG))
3847 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3848 else
3849 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3850 edit_submode_pre = NULL;
3851 showmode();
3852 break;
3853 case Ctrl_L:
3854 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3855 break;
3856 case Ctrl_F:
3857 ctrl_x_mode = CTRL_X_FILES;
3858 break;
3859 case Ctrl_K:
3860 ctrl_x_mode = CTRL_X_DICTIONARY;
3861 break;
3862 case Ctrl_R:
3863 /* Simply allow ^R to happen without affecting ^X mode */
3864 break;
3865 case Ctrl_T:
3866 ctrl_x_mode = CTRL_X_THESAURUS;
3867 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003868#ifdef FEAT_COMPL_FUNC
3869 case Ctrl_U:
3870 ctrl_x_mode = CTRL_X_FUNCTION;
3871 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003872 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003873 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003874 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003875#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003876 case 's':
3877 case Ctrl_S:
3878 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003879#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003880 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003881 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003882 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003883#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003884 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 case Ctrl_RSB:
3886 ctrl_x_mode = CTRL_X_TAGS;
3887 break;
3888#ifdef FEAT_FIND_ID
3889 case Ctrl_I:
3890 case K_S_TAB:
3891 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3892 break;
3893 case Ctrl_D:
3894 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3895 break;
3896#endif
3897 case Ctrl_V:
3898 case Ctrl_Q:
3899 ctrl_x_mode = CTRL_X_CMDLINE;
3900 break;
3901 case Ctrl_P:
3902 case Ctrl_N:
3903 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3904 * just started ^X mode, or there were enough ^X's to cancel
3905 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3906 * do normal expansion when interrupting a different mode (say
3907 * ^X^F^X^P or ^P^X^X^P, see below)
3908 * nothing changes if interrupting mode 0, (eg, the flag
3909 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003910 if (!(compl_cont_status & CONT_INTRPT))
3911 compl_cont_status |= CONT_LOCAL;
3912 else if (compl_cont_mode != 0)
3913 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 /* FALLTHROUGH */
3915 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003916 /* If we have typed at least 2 ^X's... for modes != 0, we set
3917 * compl_cont_status = 0 (eg, as if we had just started ^X
3918 * mode).
3919 * For mode 0, we set "compl_cont_mode" to an impossible
3920 * value, in both cases ^X^X can be used to restart the same
3921 * mode (avoiding ADDING mode).
3922 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3923 * 'complete' and local ^P expansions respectively.
3924 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3925 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (c == Ctrl_X)
3927 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003928 if (compl_cont_mode != 0)
3929 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003933 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 edit_submode = NULL;
3935 showmode();
3936 break;
3937 }
3938 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003939 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
3941 /* We're already in CTRL-X mode, do we stay in it? */
3942 if (!vim_is_ctrl_x_key(c))
3943 {
3944 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003945 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 else
3947 ctrl_x_mode = CTRL_X_FINISHED;
3948 edit_submode = NULL;
3949 }
3950 showmode();
3951 }
3952
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003953 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
3955 /* Show error message from attempted keyword completion (probably
3956 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003957 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003959 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3960 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 || ctrl_x_mode == CTRL_X_FINISHED)
3962 {
3963 /* Get here when we have finished typing a sequence of ^N and
3964 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003965 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003966 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
3968 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003969 * If any of the original typed text has been changed, eg when
3970 * ignorecase is set, we must add back-spaces to the redo
3971 * buffer. We add as few as necessary to delete just the part
3972 * of the original text that has changed.
3973 * When using the longest match, edited the match or used
3974 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003976 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3977 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003978 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003979 ptr = NULL;
3980 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982
3983#ifdef FEAT_CINDENT
3984 want_cindent = (can_cindent && cindent_on());
3985#endif
3986 /*
3987 * When completing whole lines: fix indent for 'cindent'.
3988 * Otherwise, break line if it's too long.
3989 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003990 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 {
3992#ifdef FEAT_CINDENT
3993 /* re-indent the current line */
3994 if (want_cindent)
3995 {
3996 do_c_expr_indent();
3997 want_cindent = FALSE; /* don't do it again */
3998 }
3999#endif
4000 }
4001 else
4002 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004003 int prev_col = curwin->w_cursor.col;
4004
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004006 if (prev_col > 0)
4007 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004008 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004009 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004011 if (prev_col > 0
4012 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4013 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004016 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004017 * the selection without inserting anything. When
4018 * compl_enter_selects is set the Enter key does the same. */
4019 if ((c == Ctrl_Y || (compl_enter_selects
4020 && (c == CAR || c == K_KENTER || c == NL)))
4021 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004022 retval = TRUE;
4023
Bram Moolenaar47247282016-08-02 22:36:02 +02004024 /* CTRL-E means completion is Ended, go back to the typed text.
4025 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004026 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004027 {
4028 ins_compl_delete();
4029 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004030 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004031 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004032 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004033 retval = TRUE;
4034 }
4035
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004036 auto_format(FALSE, TRUE);
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039 compl_started = FALSE;
4040 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004041 if (!shortmess(SHM_COMPLETIONMENU))
4042 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004043 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004044 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (edit_submode != NULL)
4046 {
4047 edit_submode = NULL;
4048 showmode();
4049 }
4050
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004051#ifdef FEAT_CMDWIN
4052 if (c == Ctrl_C && cmdwin_type != 0)
4053 /* Avoid the popup menu remains displayed when leaving the
4054 * command line window. */
4055 update_screen(0);
4056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057#ifdef FEAT_CINDENT
4058 /*
4059 * Indent now if a key was typed that is in 'cinkeys'.
4060 */
4061 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4062 do_c_expr_indent();
4063#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004064 /* Trigger the CompleteDone event to give scripts a chance to act
4065 * upon the completion. */
4066 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004069 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4070 /* Trigger the CompleteDone event to give scripts a chance to act
4071 * upon the (possibly failed) completion. */
4072 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
4074 /* reset continue_* if we left expansion-mode, if we stay they'll be
4075 * (re)set properly in ins_complete() */
4076 if (!vim_is_ctrl_x_key(c))
4077 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078 compl_cont_status = 0;
4079 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004081
4082 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083}
4084
4085/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004086 * Fix the redo buffer for the completion leader replacing some of the typed
4087 * text. This inserts backspaces and appends the changed text.
4088 * "ptr" is the known leader text or NUL.
4089 */
4090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004091ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004092{
4093 int len;
4094 char_u *p;
4095 char_u *ptr = ptr_arg;
4096
4097 if (ptr == NULL)
4098 {
4099 if (compl_leader != NULL)
4100 ptr = compl_leader;
4101 else
4102 return; /* nothing to do */
4103 }
4104 if (compl_orig_text != NULL)
4105 {
4106 p = compl_orig_text;
4107 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4108 ;
4109#ifdef FEAT_MBYTE
4110 if (len > 0)
4111 len -= (*mb_head_off)(p, p + len);
4112#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004113 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004114 AppendCharToRedobuff(K_BS);
4115 }
4116 else
4117 len = 0;
4118 if (ptr != NULL)
4119 AppendToRedobuffLit(ptr + len, -1);
4120}
4121
4122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4124 * (depending on flag) starting from buf and looking for a non-scanned
4125 * buffer (other than curbuf). curbuf is special, if it is called with
4126 * buf=curbuf then it has to be the first call for a given flag/expansion.
4127 *
4128 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4129 */
4130 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004131ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134
4135 if (flag == 'w') /* just windows */
4136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (buf == curbuf) /* first call for this flag/expansion */
4138 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004139 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 && wp->w_buffer->b_scanned)
4141 ;
4142 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 else
4145 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4146 * (unlisted buffers)
4147 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004148 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 && ((flag == 'U'
4150 ? buf->b_p_bl
4151 : (!buf->b_p_bl
4152 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004153 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 ;
4155 return buf;
4156}
4157
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004158#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004159/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004160 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004161 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004162 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004164expand_by_function(
4165 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4166 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004167{
Bram Moolenaar82139082011-09-14 16:52:09 +02004168 list_T *matchlist = NULL;
4169 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004170 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004171 char_u *funcname;
4172 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004173 win_T *curwin_save;
4174 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004175 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004176
Bram Moolenaare344bea2005-09-01 20:46:49 +00004177 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4178 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004179 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004180
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004181 /* Call 'completefunc' to obtain the list of matches. */
4182 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004183 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004184
Bram Moolenaare344bea2005-09-01 20:46:49 +00004185 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004186 curwin_save = curwin;
4187 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004188
4189 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004190 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004191 {
4192 switch (rettv.v_type)
4193 {
4194 case VAR_LIST:
4195 matchlist = rettv.vval.v_list;
4196 break;
4197 case VAR_DICT:
4198 matchdict = rettv.vval.v_dict;
4199 break;
4200 default:
4201 /* TODO: Give error message? */
4202 clear_tv(&rettv);
4203 break;
4204 }
4205 }
4206
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004207 if (curwin_save != curwin || curbuf_save != curbuf)
4208 {
4209 EMSG(_(e_complwin));
4210 goto theend;
4211 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004212 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004213 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004214 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004215 {
4216 EMSG(_(e_compldel));
4217 goto theend;
4218 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004219
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004220 if (matchlist != NULL)
4221 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004222 else if (matchdict != NULL)
4223 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004224
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004225theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004226 if (matchdict != NULL)
4227 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004228 if (matchlist != NULL)
4229 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004230}
4231#endif /* FEAT_COMPL_FUNC */
4232
Bram Moolenaar39f05632006-03-19 22:15:26 +00004233#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004234/*
4235 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004236 */
4237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004238ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004239{
4240 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004241 int dir = compl_direction;
4242
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004243 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004244 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004245 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004246 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4247 /* if dir was BACKWARD then honor it just once */
4248 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004249 else if (did_emsg)
4250 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004251 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004252}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004253
4254/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004255 * Add completions from a dict.
4256 */
4257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004258ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004259{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004260 dictitem_T *di_refresh;
4261 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004262
4263 /* Check for optional "refresh" item. */
4264 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004265 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4266 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004267 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004268 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004269
4270 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4271 compl_opt_refresh_always = TRUE;
4272 }
4273
4274 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004275 di_words = dict_find(dict, (char_u *)"words", 5);
4276 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4277 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004278}
4279
4280/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004281 * Add a match to the list of matches from a typeval_T.
4282 * If the given string is already in the list of completions, then return
4283 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4284 * maybe because alloc() returns NULL, then FAIL is returned.
4285 */
4286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004287ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004288{
4289 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004290 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004291 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004292 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004293 char_u *(cptext[CPT_COUNT]);
4294
4295 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4296 {
4297 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4298 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4299 (char_u *)"abbr", FALSE);
4300 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4301 (char_u *)"menu", FALSE);
4302 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4303 (char_u *)"kind", FALSE);
4304 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4305 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004306 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4307 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004308 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4309 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004310 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004311 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004312 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4313 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004314 }
4315 else
4316 {
4317 word = get_tv_string_chk(tv);
4318 vim_memset(cptext, 0, sizeof(cptext));
4319 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004320 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004321 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004322 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004323}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004324#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004326/*
4327 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004328 * The search starts at position "ini" in curbuf and in the direction
4329 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004330 * When "compl_started" is FALSE start at that position, otherwise continue
4331 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332 * This may return before finding all the matches.
4333 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 */
4335 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004336ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337{
4338 static pos_T first_match_pos;
4339 static pos_T last_match_pos;
4340 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341 static int found_all = FALSE; /* Found all matches of a
4342 certain type. */
4343 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaar572cb562005-08-05 21:35:02 +00004345 pos_T *pos;
4346 char_u **matches;
4347 int save_p_scs;
4348 int save_p_ws;
4349 int save_p_ic;
4350 int i;
4351 int num_matches;
4352 int len;
4353 int found_new_match;
4354 int type = ctrl_x_mode;
4355 char_u *ptr;
4356 char_u *dict = NULL;
4357 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004358 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004360 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004362 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 ins_buf->b_scanned = 0;
4364 found_all = FALSE;
4365 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004366 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004367 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 last_match_pos = first_match_pos = *ini;
4369 }
4370
Bram Moolenaar4475b622017-05-01 20:46:52 +02004371 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004372 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4374 for (;;)
4375 {
4376 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004377 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 * or if found_all says this entry is done. For ^X^L only use the
4381 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004382 if ((ctrl_x_mode == CTRL_X_NORMAL
4383 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 found_all = FALSE;
4387 while (*e_cpt == ',' || *e_cpt == ' ')
4388 e_cpt++;
4389 if (*e_cpt == '.' && !curbuf->b_scanned)
4390 {
4391 ins_buf = curbuf;
4392 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004393 /* Move the cursor back one character so that ^N can match the
4394 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004395 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004396 {
4397 /* Move the cursor to after the last character in the
4398 * buffer, so that word at start of buffer is found
4399 * correctly. */
4400 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4401 first_match_pos.col =
4402 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 last_match_pos = first_match_pos;
4405 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004406
4407 /* Remember the first match so that the loop stops when we
4408 * wrap and come back there a second time. */
4409 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4412 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4413 {
4414 /* Scan a buffer, but not the current one. */
4415 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4416 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 first_match_pos.col = last_match_pos.col = 0;
4419 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4420 last_match_pos.lnum = 0;
4421 type = 0;
4422 }
4423 else /* unloaded buffer, scan like dictionary */
4424 {
4425 found_all = TRUE;
4426 if (ins_buf->b_fname == NULL)
4427 continue;
4428 type = CTRL_X_DICTIONARY;
4429 dict = ins_buf->b_fname;
4430 dict_f = DICT_EXACT;
4431 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004432 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 ins_buf->b_fname == NULL
4434 ? buf_spname(ins_buf)
4435 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004436 ? ins_buf->b_fname
4437 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004438 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 else if (*e_cpt == NUL)
4441 break;
4442 else
4443 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004444 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 type = -1;
4446 else if (*e_cpt == 'k' || *e_cpt == 's')
4447 {
4448 if (*e_cpt == 'k')
4449 type = CTRL_X_DICTIONARY;
4450 else
4451 type = CTRL_X_THESAURUS;
4452 if (*++e_cpt != ',' && *e_cpt != NUL)
4453 {
4454 dict = e_cpt;
4455 dict_f = DICT_FIRST;
4456 }
4457 }
4458#ifdef FEAT_FIND_ID
4459 else if (*e_cpt == 'i')
4460 type = CTRL_X_PATH_PATTERNS;
4461 else if (*e_cpt == 'd')
4462 type = CTRL_X_PATH_DEFINES;
4463#endif
4464 else if (*e_cpt == ']' || *e_cpt == 't')
4465 {
4466 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004467 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004468 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 else
4471 type = -1;
4472
4473 /* in any case e_cpt is advanced to the next entry */
4474 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4475
4476 found_all = TRUE;
4477 if (type == -1)
4478 continue;
4479 }
4480 }
4481
Bram Moolenaar4475b622017-05-01 20:46:52 +02004482 /* If complete() was called then compl_pattern has been reset. The
4483 * following won't work then, bail out. */
4484 if (compl_pattern == NULL)
4485 break;
4486
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 switch (type)
4488 {
4489 case -1:
4490 break;
4491#ifdef FEAT_FIND_ID
4492 case CTRL_X_PATH_PATTERNS:
4493 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004494 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004495 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004497 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4499 (linenr_T)1, (linenr_T)MAXLNUM);
4500 break;
4501#endif
4502
4503 case CTRL_X_DICTIONARY:
4504 case CTRL_X_THESAURUS:
4505 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004506 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 : (type == CTRL_X_THESAURUS
4508 ? (*curbuf->b_p_tsr == NUL
4509 ? p_tsr
4510 : curbuf->b_p_tsr)
4511 : (*curbuf->b_p_dict == NUL
4512 ? p_dict
4513 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004514 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004515 dict != NULL ? dict_f
4516 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 dict = NULL;
4518 break;
4519
4520 case CTRL_X_TAGS:
4521 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4522 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004523 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004525 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004526 * of matches is found when compl_pattern is empty */
4527 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004528 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4529 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4531 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004532 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 p_ic = save_p_ic;
4535 break;
4536
4537 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4540 {
4541
4542 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004543 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004544 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
4546 break;
4547
4548 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004549 if (expand_cmdline(&compl_xp, compl_pattern,
4550 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004552 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 break;
4554
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004555#ifdef FEAT_COMPL_FUNC
4556 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004557 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004558 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004559 break;
4560#endif
4561
Bram Moolenaar488c6512005-08-11 20:09:58 +00004562 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004563#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004564 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004565 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004566 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004567 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004568#endif
4569 break;
4570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 default: /* normal ^P/^N and ^X^L */
4572 /*
4573 * If 'infercase' is set, don't use 'smartcase' here
4574 */
4575 save_p_scs = p_scs;
4576 if (ins_buf->b_p_inf)
4577 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004578
Bram Moolenaar361aa502014-01-23 22:45:58 +01004579 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 * end but never from the middle, thus setting nowrapscan in this
4581 * buffers is a good idea, on the other hand, we always set
4582 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4583 save_p_ws = p_ws;
4584 if (ins_buf != curbuf)
4585 p_ws = FALSE;
4586 else if (*e_cpt == '.')
4587 p_ws = TRUE;
4588 for (;;)
4589 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004590 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004592 ++msg_silent; /* Don't want messages for wrapscan. */
4593
Bram Moolenaare4214502015-03-05 18:08:43 +01004594 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4595 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004596 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004597 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004600 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004602 found_new_match = searchit(NULL, ins_buf, pos,
4603 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004604 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004605 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004606 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004607 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004609 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 first_match_pos = *pos;
4612 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004613 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004616 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 found_new_match = FAIL;
4618 if (found_new_match == FAIL)
4619 {
4620 if (ins_buf == curbuf)
4621 found_all = TRUE;
4622 break;
4623 }
4624
4625 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004626 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 && ini->lnum == pos->lnum
4628 && ini->col == pos->col)
4629 continue;
4630 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004631 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004633 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4636 continue;
4637 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4638 if (!p_paste)
4639 ptr = skipwhite(ptr);
4640 }
4641 len = (int)STRLEN(ptr);
4642 }
4643 else
4644 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645 char_u *tmp_ptr = ptr;
4646
4647 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004649 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 /* Skip if already inside a word. */
4651 if (vim_iswordp(tmp_ptr))
4652 continue;
4653 /* Find start of next word. */
4654 tmp_ptr = find_word_start(tmp_ptr);
4655 }
4656 /* Find end of this word. */
4657 tmp_ptr = find_word_end(tmp_ptr);
4658 len = (int)(tmp_ptr - ptr);
4659
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004660 if ((compl_cont_status & CONT_ADDING)
4661 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
4663 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4664 {
4665 /* Try next line, if any. the new word will be
4666 * "join" as if the normal command "J" was used.
4667 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004668 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 * works -- Acevedo */
4670 STRNCPY(IObuff, ptr, len);
4671 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4672 tmp_ptr = ptr = skipwhite(ptr);
4673 /* Find start of next word. */
4674 tmp_ptr = find_word_start(tmp_ptr);
4675 /* Find end of next word. */
4676 tmp_ptr = find_word_end(tmp_ptr);
4677 if (tmp_ptr > ptr)
4678 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004679 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004681 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 IObuff[len++] = ' ';
4683 /* IObuf =~ "\k.* ", thus len >= 2 */
4684 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004685 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 || (vim_strchr(p_cpo, CPO_JOINSP)
4687 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004688 && (IObuff[len - 2] == '?'
4689 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 IObuff[len++] = ' ';
4691 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004692 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (tmp_ptr - ptr >= IOSIZE - len)
4694 tmp_ptr = ptr + IOSIZE - len - 1;
4695 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4696 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004697 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
4699 IObuff[len] = NUL;
4700 ptr = IObuff;
4701 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004702 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 continue;
4704 }
4705 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004706 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004707 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004708 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
4710 found_new_match = OK;
4711 break;
4712 }
4713 }
4714 p_scs = save_p_scs;
4715 p_ws = save_p_ws;
4716 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004717
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004719 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004720 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 found_new_match = OK;
4722
4723 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004724 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4725 * match */
4726 if ((ctrl_x_mode != CTRL_X_NORMAL
4727 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004728 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004729 {
4730 if (got_int)
4731 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004732 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004733 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004734 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004735
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004736 if ((ctrl_x_mode != CTRL_X_NORMAL
4737 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004738 || compl_interrupted)
4739 break;
4740 compl_started = TRUE;
4741 }
4742 else
4743 {
4744 /* Mark a buffer scanned when it has been scanned completely */
4745 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4746 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004748 compl_started = FALSE;
4749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004751 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004753 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 && *e_cpt == NUL) /* Got to end of 'complete' */
4755 found_new_match = FAIL;
4756
4757 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004758 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4759 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 i = ins_compl_make_cyclic();
4761
Bram Moolenaar4475b622017-05-01 20:46:52 +02004762 if (compl_old_match != NULL)
4763 {
4764 /* If several matches were added (FORWARD) or the search failed and has
4765 * just been made cyclic then we have to move compl_curr_match to the
4766 * next or previous entry (if any) -- Acevedo */
4767 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4768 : compl_old_match->cp_prev;
4769 if (compl_curr_match == NULL)
4770 compl_curr_match = compl_old_match;
4771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 return i;
4773}
4774
4775/* Delete the old text being completed. */
4776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004777ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004779 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
4781 /*
4782 * In insert mode: Delete the typed part.
4783 * In replace mode: Put the old characters back, if any.
4784 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004785 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4786 if ((int)curwin->w_cursor.col > col)
4787 {
4788 if (stop_arrow() == FAIL)
4789 return;
4790 backspace_until_column(col);
4791 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004792
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004793 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4794 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004796 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004797 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798}
4799
Bram Moolenaar472e8592016-10-15 17:06:47 +02004800/*
4801 * Insert the new text being completed.
4802 * "in_compl_func" is TRUE when called from complete_check().
4803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004805ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004807 dict_T *dict;
4808
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004809 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004810 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4811 compl_used_match = FALSE;
4812 else
4813 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004814
4815 /* Set completed item. */
4816 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004817 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004818 if (dict != NULL)
4819 {
4820 dict_add_nr_str(dict, "word", 0L,
4821 EMPTY_IF_NULL(compl_shown_match->cp_str));
4822 dict_add_nr_str(dict, "abbr", 0L,
4823 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4824 dict_add_nr_str(dict, "menu", 0L,
4825 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4826 dict_add_nr_str(dict, "kind", 0L,
4827 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4828 dict_add_nr_str(dict, "info", 0L,
4829 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004830 dict_add_nr_str(dict, "user_data", 0L,
4831 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004832 }
4833 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004834 if (!in_compl_func)
4835 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836}
4837
4838/*
4839 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004840 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4841 * get more completions. If it is FALSE, then we just do nothing when there
4842 * are no more completions in a given direction. The latter case is used when
4843 * we are still in the middle of finding completions, to allow browsing
4844 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 * Return the total number of matches, or -1 if still unknown -- webb.
4846 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004847 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4848 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 *
4850 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004851 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4852 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 */
4854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004855ins_compl_next(
4856 int allow_get_expansion,
4857 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004858 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004859 int insert_match, /* Insert the newly selected match */
4860 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
4862 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004863 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004864 compl_T *found_compl = NULL;
4865 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004866 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004867 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004869 /* When user complete function return -1 for findstart which is next
4870 * time of 'always', compl_shown_match become NULL. */
4871 if (compl_shown_match == NULL)
4872 return -1;
4873
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004874 if (compl_leader != NULL
4875 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004877 /* Set "compl_shown_match" to the actually shown match, it may differ
4878 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004879 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004880 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004881 && compl_shown_match->cp_next != NULL
4882 && compl_shown_match->cp_next != compl_first_match)
4883 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004884
4885 /* If we didn't find it searching forward, and compl_shows_dir is
4886 * backward, find the last match. */
4887 if (compl_shows_dir == BACKWARD
4888 && !ins_compl_equal(compl_shown_match,
4889 compl_leader, (int)STRLEN(compl_leader))
4890 && (compl_shown_match->cp_next == NULL
4891 || compl_shown_match->cp_next == compl_first_match))
4892 {
4893 while (!ins_compl_equal(compl_shown_match,
4894 compl_leader, (int)STRLEN(compl_leader))
4895 && compl_shown_match->cp_prev != NULL
4896 && compl_shown_match->cp_prev != compl_first_match)
4897 compl_shown_match = compl_shown_match->cp_prev;
4898 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004899 }
4900
4901 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004902 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 /* Delete old text to be replaced */
4904 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004905
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004906 /* When finding the longest common text we stick at the original text,
4907 * don't let CTRL-N or CTRL-P move to the first match. */
4908 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4909
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004910 /* When restarting the search don't insert the first match either. */
4911 if (compl_restarting)
4912 {
4913 advance = FALSE;
4914 compl_restarting = FALSE;
4915 }
4916
Bram Moolenaare3226be2005-12-18 22:10:00 +00004917 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4918 * around. */
4919 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004921 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004923 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004924 found_end = (compl_first_match != NULL
4925 && (compl_shown_match->cp_next == compl_first_match
4926 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004927 }
4928 else if (compl_shows_dir == BACKWARD
4929 && compl_shown_match->cp_prev != NULL)
4930 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004931 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004932 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004933 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004936 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004937 if (!allow_get_expansion)
4938 {
4939 if (advance)
4940 {
4941 if (compl_shows_dir == BACKWARD)
4942 compl_pending -= todo + 1;
4943 else
4944 compl_pending += todo + 1;
4945 }
4946 return -1;
4947 }
4948
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004949 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004950 {
4951 if (compl_shows_dir == BACKWARD)
4952 --compl_pending;
4953 else
4954 ++compl_pending;
4955 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004956
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004957 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004958 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004959
4960 /* handle any pending completions */
4961 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004962 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004963 {
4964 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4965 {
4966 compl_shown_match = compl_shown_match->cp_next;
4967 --compl_pending;
4968 }
4969 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4970 {
4971 compl_shown_match = compl_shown_match->cp_prev;
4972 ++compl_pending;
4973 }
4974 else
4975 break;
4976 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004977 found_end = FALSE;
4978 }
4979 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4980 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004981 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004982 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004983 ++todo;
4984 else
4985 /* Remember a matching item. */
4986 found_compl = compl_shown_match;
4987
4988 /* Stop at the end of the list when we found a usable match. */
4989 if (found_end)
4990 {
4991 if (found_compl != NULL)
4992 {
4993 compl_shown_match = found_compl;
4994 break;
4995 }
4996 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005000 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005001 if (compl_no_insert && !started)
5002 {
5003 ins_bytes(compl_orig_text + ins_compl_len());
5004 compl_used_match = FALSE;
5005 }
5006 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005007 {
5008 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005009 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005010 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005011 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005012 }
5013 else
5014 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016 if (!allow_get_expansion)
5017 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005018 /* may undisplay the popup menu first */
5019 ins_compl_upd_pum();
5020
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005021 /* redraw to show the user what was inserted */
5022 update_screen(0);
5023
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005024 /* display the updated popup menu */
5025 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005026#ifdef FEAT_GUI
5027 if (gui.in_use)
5028 {
5029 /* Show the cursor after the match, not after the redrawn text. */
5030 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005031 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005032 }
5033#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005034
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 /* Delete old text to be replaced, since we're still searching and
5036 * don't want to match ourselves! */
5037 ins_compl_delete();
5038 }
5039
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005040 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005041 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005042 if (compl_no_insert && !started)
5043 compl_enter_selects = TRUE;
5044 else
5045 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 /*
5048 * Show the file name for the match (if any)
5049 * Truncate the file name to avoid a wait for return.
5050 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005051 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005053 char *lead = _("match in file");
5054 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5055 char_u *s;
5056 char_u *e;
5057
5058 if (space > 0)
5059 {
5060 /* We need the tail that fits. With double-byte encoding going
5061 * back from the end is very slow, thus go from the start and keep
5062 * the text that fits in "space" between "s" and "e". */
5063 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5064 {
5065 space -= ptr2cells(e);
5066 while (space < 0)
5067 {
5068 space += ptr2cells(s);
5069 MB_PTR_ADV(s);
5070 }
5071 }
5072 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5073 s > compl_shown_match->cp_fname ? "<" : "", s);
5074 msg(IObuff);
5075 redraw_cmdline = FALSE; /* don't overwrite! */
5076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078
5079 return num_matches;
5080}
5081
5082/*
5083 * Call this while finding completions, to check whether the user has hit a key
5084 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005085 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005087 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005088 * "in_compl_func" is TRUE when called from complete_check(), don't set
5089 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
5091 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005092ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
5094 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005095 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005097 /* Don't check when reading keys from a script, :normal or feedkeys().
5098 * That would break the test scripts. But do check for keys when called
5099 * from complete_check(). */
5100 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 return;
5102
5103 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005104 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 return;
5106 count = 0;
5107
Bram Moolenaara260a972006-06-23 19:36:29 +00005108 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5109 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (c != NUL)
5112 {
5113 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5114 {
5115 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005116 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005117 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005118 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005120 else
5121 {
5122 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005123 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005124 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005125 if (c != K_IGNORE)
5126 {
5127 /* Don't interrupt completion when the character wasn't typed,
5128 * e.g., when doing @q to replay keys. */
5129 if (c != Ctrl_R && KeyTyped)
5130 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005131
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005132 vungetc(c);
5133 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005136 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005137 {
5138 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5139
5140 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005141 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005142 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005143}
5144
5145/*
5146 * Decide the direction of Insert mode complete from the key typed.
5147 * Returns BACKWARD or FORWARD.
5148 */
5149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005150ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005151{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005152 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005153 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005154 return BACKWARD;
5155 return FORWARD;
5156}
5157
5158/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005159 * Return TRUE for keys that are used for completion only when the popup menu
5160 * is visible.
5161 */
5162 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005163ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005164{
5165 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005166 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5167 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005168}
5169
5170/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005171 * Decide the number of completions to move forward.
5172 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5173 */
5174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005175ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005176{
5177 int h;
5178
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005179 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005180 {
5181 h = pum_get_height();
5182 if (h > 3)
5183 h -= 2; /* keep some context */
5184 return h;
5185 }
5186 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187}
5188
5189/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005190 * Return TRUE if completion with "c" should insert the match, FALSE if only
5191 * to change the currently selected completion.
5192 */
5193 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005194ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005195{
5196 switch (c)
5197 {
5198 case K_UP:
5199 case K_DOWN:
5200 case K_PAGEDOWN:
5201 case K_KPAGEDOWN:
5202 case K_S_DOWN:
5203 case K_PAGEUP:
5204 case K_KPAGEUP:
5205 case K_S_UP:
5206 return FALSE;
5207 }
5208 return TRUE;
5209}
5210
5211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 * Do Insert mode completion.
5213 * Called when character "c" was typed, which has a meaning for completion.
5214 * Returns OK if completion was done, FAIL if something failed (out of mem).
5215 */
5216 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005217ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005219 char_u *line;
5220 int startcol = 0; /* column where searched text starts */
5221 colnr_T curs_col; /* cursor column */
5222 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005223 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005224 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005225 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005226 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227
Bram Moolenaare3226be2005-12-18 22:10:00 +00005228 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005229 insert_match = ins_compl_use_match(c);
5230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 /* First time we hit ^N or ^P (in a row, I mean) */
5234
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 did_ai = FALSE;
5236#ifdef FEAT_SMARTINDENT
5237 did_si = FALSE;
5238 can_si = FALSE;
5239 can_si_back = FALSE;
5240#endif
5241 if (stop_arrow() == FAIL)
5242 return FAIL;
5243
5244 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005245 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005246 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005248 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 * "compl_startpos" to the cursor as a pattern to add a new word
5250 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005251 * "compl_startpos" is not in the same line as the cursor then fix it
5252 * (the line has been split because it was longer than 'tw'). if SOL
5253 * is set then skip the previous pattern, a word at the beginning of
5254 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005255 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5256 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 /*
5259 * it is a continued search
5260 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005261 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005262 if (ctrl_x_mode == CTRL_X_NORMAL
5263 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5264 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005266 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 /* line (probably) wrapped, set compl_startpos to the
5269 * first non_blank in the line, if it is not a wordchar
5270 * include it to get a better pattern, but then we don't
5271 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005272 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005273 compl_startpos.col = compl_col;
5274 compl_startpos.lnum = curwin->w_cursor.lnum;
5275 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
5277 else
5278 {
5279 /* S_IPOS was set when we inserted a word that was at the
5280 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 * mode but first we need to redefine compl_startpos */
5282 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 compl_cont_status |= CONT_SOL;
5285 compl_startpos.col = (colnr_T)(skipwhite(
5286 line + compl_length
5287 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005292 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005293 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005295 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005297 compl_cont_status &= ~CONT_SOL;
5298 compl_length = (IOSIZE - MIN_SPACE);
5299 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5302 if (compl_length < 1)
5303 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005305 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005311 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005316 if (ctrl_x_mode != CTRL_X_NORMAL)
5317 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005318 compl_cont_status = 0;
5319 compl_cont_status |= CONT_N_ADDS;
5320 compl_startpos = curwin->w_cursor;
5321 startcol = (int)curs_col;
5322 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324
5325 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005326 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5330 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005331 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 compl_col += ++startcol;
5336 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 compl_pattern = str_foldcase(line + compl_col,
5340 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 compl_pattern = vim_strnsave(line + compl_col,
5343 compl_length);
5344 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 return FAIL;
5346 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
5349 char_u *prefix = (char_u *)"\\<";
5350
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005351 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005353 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005354 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005356 if (!vim_iswordp(line + compl_col)
5357 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 && (
5359#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363#endif
5364 )))
5365 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005366 STRCPY((char *)compl_pattern, prefix);
5367 (void)quote_meta(compl_pattern + STRLEN(prefix),
5368 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005374 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375#endif
5376 )
5377 {
5378 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005379 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5380 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 compl_col += curs_col;
5383 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
5385 else
5386 {
5387#ifdef FEAT_MBYTE
5388 /* Search the point of change class of multibyte character
5389 * or not a word single byte character backward. */
5390 if (has_mbyte)
5391 {
5392 int base_class;
5393 int head_off;
5394
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 startcol -= (*mb_head_off)(line, line + startcol);
5396 base_class = mb_get_class(line + startcol);
5397 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 head_off = (*mb_head_off)(line, line + startcol);
5400 if (base_class != mb_get_class(line + startcol
5401 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 }
5405 }
5406 else
5407#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005408 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005410 compl_col += ++startcol;
5411 compl_length = (int)curs_col - startcol;
5412 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 /* Only match word with at least two chars -- webb
5415 * there's no need to call quote_meta,
5416 * alloc(7) is enough -- Acevedo
5417 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 compl_pattern = alloc(7);
5419 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 STRCPY((char *)compl_pattern, "\\<");
5422 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5423 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
5425 else
5426 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005427 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005428 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005431 STRCPY((char *)compl_pattern, "\\<");
5432 (void)quote_meta(compl_pattern + 2, line + compl_col,
5433 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435 }
5436 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005437 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005439 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005440 compl_length = (int)curs_col - (int)compl_col;
5441 if (compl_length < 0) /* cursor in indent: empty pattern */
5442 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005444 compl_pattern = str_foldcase(line + compl_col, compl_length,
5445 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005447 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5448 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 return FAIL;
5450 }
5451 else if (ctrl_x_mode == CTRL_X_FILES)
5452 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005453 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005454 if (startcol > 0)
5455 {
5456 char_u *p = line + startcol;
5457
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005458 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005459 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005460 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005461 if (p == line && vim_isfilec(PTR2CHAR(p)))
5462 startcol = 0;
5463 else
5464 startcol = (int)(p - line) + 1;
5465 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005466
Bram Moolenaar0300e462013-09-08 16:03:45 +02005467 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 compl_length = (int)curs_col - startcol;
5469 compl_pattern = addstar(line + compl_col, compl_length,
5470 EXPAND_FILES);
5471 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 return FAIL;
5473 }
5474 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5475 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 compl_pattern = vim_strnsave(line, curs_col);
5477 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005480 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5482 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005483 /* No completion possible, use an empty pattern to get a
5484 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005485 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005486 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005487 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5488 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005490 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005491 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005492#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005493 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005494 * Call user defined function 'completefunc' with "a:findstart"
5495 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005496 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005497 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005498 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005499 char_u *funcname;
5500 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005501 win_T *curwin_save;
5502 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005503
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005504 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005505 * string */
5506 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5507 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5508 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005509 {
5510 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5511 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005512 /* restore did_ai, so that adding comment leader works */
5513 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005514 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005515 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005516
5517 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005518 args[1] = NULL;
5519 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005520 curwin_save = curwin;
5521 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005522 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005523 if (curwin_save != curwin || curbuf_save != curbuf)
5524 {
5525 EMSG(_(e_complwin));
5526 return FAIL;
5527 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005528 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005529 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005530 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005531 {
5532 EMSG(_(e_compldel));
5533 return FAIL;
5534 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005535
Bram Moolenaar6110a002012-01-26 18:58:38 +01005536 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005537 * cancel the complete without an error.
5538 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005539 if (col == -2)
5540 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005541 if (col == -3)
5542 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005543 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005544 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005545 if (!shortmess(SHM_COMPLETIONMENU))
5546 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005547 return FAIL;
5548 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005549
Bram Moolenaar82139082011-09-14 16:52:09 +02005550 /*
5551 * Reset extended parameters of completion, when start new
5552 * completion.
5553 */
5554 compl_opt_refresh_always = FALSE;
5555
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005556 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005557 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005558 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005559 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005560 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005561
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005562 /* Setup variables for completion. Need to obtain "line" again,
5563 * it may have become invalid. */
5564 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005565 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005566 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5567 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005568#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005569 return FAIL;
5570 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005571 else if (ctrl_x_mode == CTRL_X_SPELL)
5572 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005573#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005574 if (spell_bad_len > 0)
5575 compl_col = curs_col - spell_bad_len;
5576 else
5577 compl_col = spell_word_start(startcol);
5578 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005579 {
5580 compl_length = 0;
5581 compl_col = curs_col;
5582 }
5583 else
5584 {
5585 spell_expand_check_cap(compl_col);
5586 compl_length = (int)curs_col - compl_col;
5587 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005588 /* Need to obtain "line" again, it may have become invalid. */
5589 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005590 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5591 if (compl_pattern == NULL)
5592#endif
5593 return FAIL;
5594 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005595 else
5596 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005597 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005598 return FAIL;
5599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005601 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 {
5603 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005604 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
5606 /* Insert a new line, keep indentation but ignore 'comments' */
5607#ifdef FEAT_COMMENTS
5608 char_u *old = curbuf->b_p_com;
5609
5610 curbuf->b_p_com = (char_u *)"";
5611#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005612 compl_startpos.lnum = curwin->w_cursor.lnum;
5613 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 ins_eol('\r');
5615#ifdef FEAT_COMMENTS
5616 curbuf->b_p_com = old;
5617#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005618 compl_length = 0;
5619 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 }
5621 }
5622 else
5623 {
5624 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005625 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005628 if (compl_cont_status & CONT_LOCAL)
5629 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 else
5631 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5632
Bram Moolenaar62951b12011-09-21 18:23:05 +02005633 /* If any of the original typed text has been changed we need to fix
5634 * the redo buffer. */
5635 ins_compl_fixRedoBufForLeader(NULL);
5636
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005637 /* Always add completion for the original text. */
5638 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005639 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5640 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005641 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005643 VIM_CLEAR(compl_pattern);
5644 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 return FAIL;
5646 }
5647
5648 /* showmode might reset the internal line pointers, so it must
5649 * be called before line = ml_get(), or when this address is no
5650 * longer needed. -- Acevedo.
5651 */
5652 edit_submode_extra = (char_u *)_("-- Searching...");
5653 edit_submode_highl = HLF_COUNT;
5654 showmode();
5655 edit_submode_extra = NULL;
5656 out_flush();
5657 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005658 else if (insert_match && stop_arrow() == FAIL)
5659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005661 compl_shown_match = compl_curr_match;
5662 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
5664 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005665 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005667 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005668 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005669 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005671 /* may undisplay the popup menu */
5672 ins_compl_upd_pum();
5673
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005674 if (n > 1) /* all matches have been found */
5675 compl_matches = n;
5676 compl_curr_match = compl_shown_match;
5677 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678
Bram Moolenaard68071d2006-05-02 22:08:30 +00005679 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5680 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 if (got_int && !global_busy)
5682 {
5683 (void)vgetc();
5684 got_int = FALSE;
5685 }
5686
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005687 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005688 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005690 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5691 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5693 edit_submode_highl = HLF_E;
5694 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5695 * because we couldn't expand anything at first place, but if we used
5696 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5697 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005698 if ( compl_length > 1
5699 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005700 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5702 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005703 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 }
5705
Bram Moolenaar572cb562005-08-05 21:35:02 +00005706 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005707 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005709 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
5711 if (edit_submode_extra == NULL)
5712 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005713 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
5715 edit_submode_extra = (char_u *)_("Back at original");
5716 edit_submode_highl = HLF_W;
5717 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005718 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
5720 edit_submode_extra = (char_u *)_("Word from other line");
5721 edit_submode_highl = HLF_COUNT;
5722 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005723 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 {
5725 edit_submode_extra = (char_u *)_("The only match");
5726 edit_submode_highl = HLF_COUNT;
5727 }
5728 else
5729 {
5730 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005731 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005733 int number = 0;
5734 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005736 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {
5738 /* search backwards for the first valid (!= -1) number.
5739 * This should normally succeed already at the first loop
5740 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005741 for (match = compl_curr_match->cp_prev; match != NULL
5742 && match != compl_first_match;
5743 match = match->cp_prev)
5744 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005746 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 break;
5748 }
5749 if (match != NULL)
5750 /* go up and assign all numbers which are not assigned
5751 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005752 for (match = match->cp_next;
5753 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005754 match = match->cp_next)
5755 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757 else /* BACKWARD */
5758 {
5759 /* search forwards (upwards) for the first valid (!= -1)
5760 * number. This should normally succeed already at the
5761 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005762 for (match = compl_curr_match->cp_next; match != NULL
5763 && match != compl_first_match;
5764 match = match->cp_next)
5765 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005767 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 break;
5769 }
5770 if (match != NULL)
5771 /* go down and assign all numbers which are not
5772 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005773 for (match = match->cp_prev; match
5774 && match->cp_number == -1;
5775 match = match->cp_prev)
5776 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778 }
5779
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005780 /* The match should always have a sequence number now, this is
5781 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005782 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005784 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5785 * Translations may need more than twice that. */
5786 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005788 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005789 vim_snprintf((char *)match_ref, sizeof(match_ref),
5790 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005791 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005793 vim_snprintf((char *)match_ref, sizeof(match_ref),
5794 _("match %d"),
5795 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 edit_submode_extra = match_ref;
5797 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005798 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 curs_columns(FALSE);
5800 }
5801 }
5802 }
5803
5804 /* Show a message about what (completion) mode we're in. */
5805 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005806 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005808 if (edit_submode_extra != NULL)
5809 {
5810 if (!p_smd)
5811 msg_attr(edit_submode_extra,
5812 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005813 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005814 }
5815 else
5816 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
Bram Moolenaard68071d2006-05-02 22:08:30 +00005819 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005820 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005821 show_pum(save_w_wrow, save_w_leftcol);
5822
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005823 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005824 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 return OK;
5827}
5828
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005829 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005830show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005831{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005832 /* RedrawingDisabled may be set when invoked through complete(). */
5833 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005834
Bram Moolenaarcb036422017-03-01 12:29:10 +01005835 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005836
Bram Moolenaarcb036422017-03-01 12:29:10 +01005837 /* If the cursor moved or the display scrolled we need to remove the pum
5838 * first. */
5839 setcursor();
5840 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5841 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005842
Bram Moolenaarcb036422017-03-01 12:29:10 +01005843 ins_compl_show_pum();
5844 setcursor();
5845 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005846}
5847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848/*
5849 * Looks in the first "len" chars. of "src" for search-metachars.
5850 * If dest is not NULL the chars. are copied there quoting (with
5851 * a backslash) the metachars, and dest would be NUL terminated.
5852 * Returns the length (needed) of dest
5853 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005854 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005855quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005857 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005859 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
5861 switch (*src)
5862 {
5863 case '.':
5864 case '*':
5865 case '[':
5866 if (ctrl_x_mode == CTRL_X_DICTIONARY
5867 || ctrl_x_mode == CTRL_X_THESAURUS)
5868 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005869 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 case '~':
5871 if (!p_magic) /* quote these only if magic is set */
5872 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005873 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 case '\\':
5875 if (ctrl_x_mode == CTRL_X_DICTIONARY
5876 || ctrl_x_mode == CTRL_X_THESAURUS)
5877 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005878 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 case '^': /* currently it's not needed. */
5880 case '$':
5881 m++;
5882 if (dest != NULL)
5883 *dest++ = '\\';
5884 break;
5885 }
5886 if (dest != NULL)
5887 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005888# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 /* Copy remaining bytes of a multibyte character. */
5890 if (has_mbyte)
5891 {
5892 int i, mb_len;
5893
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005894 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (mb_len > 0 && len >= mb_len)
5896 for (i = 0; i < mb_len; ++i)
5897 {
5898 --len;
5899 ++src;
5900 if (dest != NULL)
5901 *dest++ = *src;
5902 }
5903 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 }
5906 if (dest != NULL)
5907 *dest = NUL;
5908
5909 return m;
5910}
5911#endif /* FEAT_INS_EXPAND */
5912
5913/*
5914 * Next character is interpreted literally.
5915 * A one, two or three digit decimal number is interpreted as its byte value.
5916 * If one or two digits are entered, the next character is given to vungetc().
5917 * For Unicode a character > 255 may be returned.
5918 */
5919 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005920get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
5922 int cc;
5923 int nc;
5924 int i;
5925 int hex = FALSE;
5926 int octal = FALSE;
5927#ifdef FEAT_MBYTE
5928 int unicode = 0;
5929#endif
5930
5931 if (got_int)
5932 return Ctrl_C;
5933
5934#ifdef FEAT_GUI
5935 /*
5936 * In GUI there is no point inserting the internal code for a special key.
5937 * It is more useful to insert the string "<KEY>" instead. This would
5938 * probably be useful in a text window too, but it would not be
5939 * vi-compatible (maybe there should be an option for it?) -- webb
5940 */
5941 if (gui.in_use)
5942 ++allow_keys;
5943#endif
5944#ifdef USE_ON_FLY_SCROLL
5945 dont_scroll = TRUE; /* disallow scrolling here */
5946#endif
5947 ++no_mapping; /* don't map the next key hits */
5948 cc = 0;
5949 i = 0;
5950 for (;;)
5951 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005952 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953#ifdef FEAT_CMDL_INFO
5954 if (!(State & CMDLINE)
5955# ifdef FEAT_MBYTE
5956 && MB_BYTE2LEN_CHECK(nc) == 1
5957# endif
5958 )
5959 add_to_showcmd(nc);
5960#endif
5961 if (nc == 'x' || nc == 'X')
5962 hex = TRUE;
5963 else if (nc == 'o' || nc == 'O')
5964 octal = TRUE;
5965#ifdef FEAT_MBYTE
5966 else if (nc == 'u' || nc == 'U')
5967 unicode = nc;
5968#endif
5969 else
5970 {
5971 if (hex
5972#ifdef FEAT_MBYTE
5973 || unicode != 0
5974#endif
5975 )
5976 {
5977 if (!vim_isxdigit(nc))
5978 break;
5979 cc = cc * 16 + hex2nr(nc);
5980 }
5981 else if (octal)
5982 {
5983 if (nc < '0' || nc > '7')
5984 break;
5985 cc = cc * 8 + nc - '0';
5986 }
5987 else
5988 {
5989 if (!VIM_ISDIGIT(nc))
5990 break;
5991 cc = cc * 10 + nc - '0';
5992 }
5993
5994 ++i;
5995 }
5996
5997 if (cc > 255
5998#ifdef FEAT_MBYTE
5999 && unicode == 0
6000#endif
6001 )
6002 cc = 255; /* limit range to 0-255 */
6003 nc = 0;
6004
6005 if (hex) /* hex: up to two chars */
6006 {
6007 if (i >= 2)
6008 break;
6009 }
6010#ifdef FEAT_MBYTE
6011 else if (unicode) /* Unicode: up to four or eight chars */
6012 {
6013 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6014 break;
6015 }
6016#endif
6017 else if (i >= 3) /* decimal or octal: up to three chars */
6018 break;
6019 }
6020 if (i == 0) /* no number entered */
6021 {
6022 if (nc == K_ZERO) /* NUL is stored as NL */
6023 {
6024 cc = '\n';
6025 nc = 0;
6026 }
6027 else
6028 {
6029 cc = nc;
6030 nc = 0;
6031 }
6032 }
6033
6034 if (cc == 0) /* NUL is stored as NL */
6035 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006036#ifdef FEAT_MBYTE
6037 if (enc_dbcs && (cc & 0xff) == 0)
6038 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6039 second byte will cause trouble! */
6040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041
6042 --no_mapping;
6043#ifdef FEAT_GUI
6044 if (gui.in_use)
6045 --allow_keys;
6046#endif
6047 if (nc)
6048 vungetc(nc);
6049 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6050 return cc;
6051}
6052
6053/*
6054 * Insert character, taking care of special keys and mod_mask
6055 */
6056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006057insert_special(
6058 int c,
6059 int allow_modmask,
6060 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
6062 char_u *p;
6063 int len;
6064
6065 /*
6066 * Special function key, translate into "<Key>". Up to the last '>' is
6067 * inserted with ins_str(), so as not to replace characters in replace
6068 * mode.
6069 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6070 * unless 'allow_modmask' is TRUE.
6071 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006072#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 /* Command-key never produces a normal key */
6074 if (mod_mask & MOD_MASK_CMD)
6075 allow_modmask = TRUE;
6076#endif
6077 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6078 {
6079 p = get_special_key_name(c, mod_mask);
6080 len = (int)STRLEN(p);
6081 c = p[len - 1];
6082 if (len > 2)
6083 {
6084 if (stop_arrow() == FAIL)
6085 return;
6086 p[len - 1] = NUL;
6087 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006088 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 ctrlv = FALSE;
6090 }
6091 }
6092 if (stop_arrow() == OK)
6093 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6094}
6095
6096/*
6097 * Special characters in this context are those that need processing other
6098 * than the simple insertion that can be performed here. This includes ESC
6099 * which terminates the insert, and CR/NL which need special processing to
6100 * open up a new line. This routine tries to optimize insertions performed by
6101 * the "redo", "undo" or "put" commands, so it needs to know when it should
6102 * stop and defer processing to the "normal" mechanism.
6103 * '0' and '^' are special, because they can be followed by CTRL-D.
6104 */
6105#ifdef EBCDIC
6106# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6107#else
6108# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6109#endif
6110
6111#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006112# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006114# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115#endif
6116
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006117/*
6118 * "flags": INSCHAR_FORMAT - force formatting
6119 * INSCHAR_CTRLV - char typed just after CTRL-V
6120 * INSCHAR_NO_FEX - don't use 'formatexpr'
6121 *
6122 * NOTE: passes the flags value straight through to internal_format() which,
6123 * beside INSCHAR_FORMAT (above), is also looking for these:
6124 * INSCHAR_DO_COM - format comments
6125 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006128insertchar(
6129 int c, /* character to insert or NUL */
6130 int flags, /* INSCHAR_FORMAT, etc. */
6131 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 int textwidth;
6134#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006138 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006140 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143 /*
6144 * Try to break the line in two or more pieces when:
6145 * - Always do this if we have been called to do formatting only.
6146 * - Always do this when 'formatoptions' has the 'a' flag and the line
6147 * ends in white space.
6148 * - Otherwise:
6149 * - Don't do this if inserting a blank
6150 * - Don't do this if an existing character is being replaced, unless
6151 * we're in VREPLACE mode.
6152 * - Do this if the cursor is not on the line where insert started
6153 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6154 * before the insert.
6155 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6156 * before 'textwidth'
6157 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006158 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006159 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006160 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 && !((State & REPLACE_FLAG)
6162#ifdef FEAT_VREPLACE
6163 && !(State & VREPLACE_FLAG)
6164#endif
6165 && *ml_get_cursor() != NUL)
6166 && (curwin->w_cursor.lnum != Insstart.lnum
6167 || ((!has_format_option(FO_INS_LONG)
6168 || Insstart_textlen <= (colnr_T)textwidth)
6169 && (!fo_ins_blank
6170 || Insstart_blank_vcol <= (colnr_T)textwidth
6171 ))))))
6172 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006173 /* Format with 'formatexpr' when it's set. Use internal formatting
6174 * when 'formatexpr' isn't set or it returns non-zero. */
6175#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006176 int do_internal = TRUE;
6177 colnr_T virtcol = get_nolist_virtcol()
6178 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006179
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006180 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6181 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006182 {
6183 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6184 /* It may be required to save for undo again, e.g. when setline()
6185 * was called. */
6186 ins_need_undo = TRUE;
6187 }
6188 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006190 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006192
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (c == NUL) /* only formatting was wanted */
6194 return;
6195
6196#ifdef FEAT_COMMENTS
6197 /* Check whether this character should end a comment. */
6198 if (did_ai && (int)c == end_comment_pending)
6199 {
6200 char_u *line;
6201 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6202 int middle_len, end_len;
6203 int i;
6204
6205 /*
6206 * Need to remove existing (middle) comment leader and insert end
6207 * comment leader. First, check what comment leader we can find.
6208 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006209 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6211 {
6212 /* Skip middle-comment string */
6213 while (*p && p[-1] != ':') /* find end of middle flags */
6214 ++p;
6215 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6216 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006217 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 --middle_len;
6219
6220 /* Find the end-comment string */
6221 while (*p && p[-1] != ':') /* find end of end flags */
6222 ++p;
6223 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6224
6225 /* Skip white space before the cursor */
6226 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006227 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 ;
6229 i++;
6230
6231 /* Skip to before the middle leader */
6232 i -= middle_len;
6233
6234 /* Check some expected things before we go on */
6235 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6236 {
6237 /* Backspace over all the stuff we want to replace */
6238 backspace_until_column(i);
6239
6240 /*
6241 * Insert the end-comment string, except for the last
6242 * character, which will get inserted as normal later.
6243 */
6244 ins_bytes_len(lead_end, end_len - 1);
6245 }
6246 }
6247 }
6248 end_comment_pending = NUL;
6249#endif
6250
6251 did_ai = FALSE;
6252#ifdef FEAT_SMARTINDENT
6253 did_si = FALSE;
6254 can_si = FALSE;
6255 can_si_back = FALSE;
6256#endif
6257
6258 /*
6259 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6260 * This speeds up normal text input considerably.
6261 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6262 * need to re-indent at a ':', or any other character (but not what
6263 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006264 * Don't do this when there an InsertCharPre autocommand is defined,
6265 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006266 * Do the check for InsertCharPre before the call to vpeekc() because the
6267 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 */
6269#ifdef USE_ON_FLY_SCROLL
6270 dont_scroll = FALSE; /* allow scrolling here */
6271#endif
6272
6273 if ( !ISSPECIAL(c)
6274#ifdef FEAT_MBYTE
6275 && (!has_mbyte || (*mb_char2len)(c) == 1)
6276#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006277 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 && vpeekc() != NUL
6279 && !(State & REPLACE_FLAG)
6280#ifdef FEAT_CINDENT
6281 && !cindent_on()
6282#endif
6283#ifdef FEAT_RIGHTLEFT
6284 && !p_ri
6285#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006286 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
6288#define INPUT_BUFLEN 100
6289 char_u buf[INPUT_BUFLEN + 1];
6290 int i;
6291 colnr_T virtcol = 0;
6292
6293 buf[0] = c;
6294 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006295 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 virtcol = get_nolist_virtcol();
6297 /*
6298 * Stop the string when:
6299 * - no more chars available
6300 * - finding a special character (command key)
6301 * - buffer is full
6302 * - running into the 'textwidth' boundary
6303 * - need to check for abbreviation: A non-word char after a word-char
6304 */
6305 while ( (c = vpeekc()) != NUL
6306 && !ISSPECIAL(c)
6307#ifdef FEAT_MBYTE
6308 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6309#endif
6310 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006311# ifdef FEAT_FKMAP
6312 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 && (textwidth == 0
6315 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6316 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6317 {
6318#ifdef FEAT_RIGHTLEFT
6319 c = vgetc();
6320 if (p_hkmap && KeyTyped)
6321 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 buf[i++] = c;
6323#else
6324 buf[i++] = vgetc();
6325#endif
6326 }
6327
6328#ifdef FEAT_DIGRAPHS
6329 do_digraph(-1); /* clear digraphs */
6330 do_digraph(buf[i-1]); /* may be the start of a digraph */
6331#endif
6332 buf[i] = NUL;
6333 ins_str(buf);
6334 if (flags & INSCHAR_CTRLV)
6335 {
6336 redo_literal(*buf);
6337 i = 1;
6338 }
6339 else
6340 i = 0;
6341 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006342 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 }
6344 else
6345 {
6346#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347 int cc;
6348
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6350 {
6351 char_u buf[MB_MAXBYTES + 1];
6352
6353 (*mb_char2bytes)(c, buf);
6354 buf[cc] = NUL;
6355 ins_char_bytes(buf, cc);
6356 AppendCharToRedobuff(c);
6357 }
6358 else
6359#endif
6360 {
6361 ins_char(c);
6362 if (flags & INSCHAR_CTRLV)
6363 redo_literal(c);
6364 else
6365 AppendCharToRedobuff(c);
6366 }
6367 }
6368}
6369
6370/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006371 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006372 *
6373 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6374 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006375 */
6376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006377internal_format(
6378 int textwidth,
6379 int second_indent,
6380 int flags,
6381 int format_only,
6382 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383{
6384 int cc;
6385 int save_char = NUL;
6386 int haveto_redraw = FALSE;
6387 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6388#ifdef FEAT_MBYTE
6389 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6390#endif
6391 int fo_white_par = has_format_option(FO_WHITE_PAR);
6392 int first_line = TRUE;
6393#ifdef FEAT_COMMENTS
6394 colnr_T leader_len;
6395 int no_leader = FALSE;
6396 int do_comments = (flags & INSCHAR_DO_COM);
6397#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006398#ifdef FEAT_LINEBREAK
6399 int has_lbr = curwin->w_p_lbr;
6400
6401 /* make sure win_lbr_chartabsize() counts correctly */
6402 curwin->w_p_lbr = FALSE;
6403#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006404
6405 /*
6406 * When 'ai' is off we don't want a space under the cursor to be
6407 * deleted. Replace it with an 'x' temporarily.
6408 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006409 if (!curbuf->b_p_ai
6410#ifdef FEAT_VREPLACE
6411 && !(State & VREPLACE_FLAG)
6412#endif
6413 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006414 {
6415 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006416 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006417 {
6418 save_char = cc;
6419 pchar_cursor('x');
6420 }
6421 }
6422
6423 /*
6424 * Repeat breaking lines, until the current line is not too long.
6425 */
6426 while (!got_int)
6427 {
6428 int startcol; /* Cursor column at entry */
6429 int wantcol; /* column at textwidth border */
6430 int foundcol; /* column for start of spaces */
6431 int end_foundcol = 0; /* column for start of word */
6432 colnr_T len;
6433 colnr_T virtcol;
6434#ifdef FEAT_VREPLACE
6435 int orig_col = 0;
6436 char_u *saved_text = NULL;
6437#endif
6438 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006439 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440
Bram Moolenaar97b98102009-11-17 16:41:01 +00006441 virtcol = get_nolist_virtcol()
6442 + char2cells(c != NUL ? c : gchar_cursor());
6443 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444 break;
6445
6446#ifdef FEAT_COMMENTS
6447 if (no_leader)
6448 do_comments = FALSE;
6449 else if (!(flags & INSCHAR_FORMAT)
6450 && has_format_option(FO_WRAP_COMS))
6451 do_comments = TRUE;
6452
6453 /* Don't break until after the comment leader */
6454 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006455 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006456 else
6457 leader_len = 0;
6458
6459 /* If the line doesn't start with a comment leader, then don't
6460 * start one in a following broken line. Avoids that a %word
6461 * moved to the start of the next line causes all following lines
6462 * to start with %. */
6463 if (leader_len == 0)
6464 no_leader = TRUE;
6465#endif
6466 if (!(flags & INSCHAR_FORMAT)
6467#ifdef FEAT_COMMENTS
6468 && leader_len == 0
6469#endif
6470 && !has_format_option(FO_WRAP))
6471
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006472 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006473 if ((startcol = curwin->w_cursor.col) == 0)
6474 break;
6475
6476 /* find column of textwidth border */
6477 coladvance((colnr_T)textwidth);
6478 wantcol = curwin->w_cursor.col;
6479
Bram Moolenaar97b98102009-11-17 16:41:01 +00006480 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006481 foundcol = 0;
6482
6483 /*
6484 * Find position to break at.
6485 * Stop at first entered white when 'formatoptions' has 'v'
6486 */
6487 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006488 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006489 || curwin->w_cursor.lnum != Insstart.lnum
6490 || curwin->w_cursor.col >= Insstart.col)
6491 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006492 if (curwin->w_cursor.col == startcol && c != NUL)
6493 cc = c;
6494 else
6495 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006496 if (WHITECHAR(cc))
6497 {
6498 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006499 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006500
6501 /* find start of sequence of blanks */
6502 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6503 {
6504 dec_cursor();
6505 cc = gchar_cursor();
6506 }
6507 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6508 break; /* only spaces in front of text */
6509#ifdef FEAT_COMMENTS
6510 /* Don't break until after the comment leader */
6511 if (curwin->w_cursor.col < leader_len)
6512 break;
6513#endif
6514 if (has_format_option(FO_ONE_LETTER))
6515 {
6516 /* do not break after one-letter words */
6517 if (curwin->w_cursor.col == 0)
6518 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006519#ifdef FEAT_COMMENTS
6520 /* do not break "#a b" when 'tw' is 2 */
6521 if (curwin->w_cursor.col <= leader_len)
6522 break;
6523#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006524 col = curwin->w_cursor.col;
6525 dec_cursor();
6526 cc = gchar_cursor();
6527
6528 if (WHITECHAR(cc))
6529 continue; /* one-letter, continue */
6530 curwin->w_cursor.col = col;
6531 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006532
6533 inc_cursor();
6534
6535 end_foundcol = end_col + 1;
6536 foundcol = curwin->w_cursor.col;
6537 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006538 break;
6539 }
6540#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006541 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542 {
6543 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006544 if (curwin->w_cursor.col != startcol)
6545 {
6546#ifdef FEAT_COMMENTS
6547 /* Don't break until after the comment leader */
6548 if (curwin->w_cursor.col < leader_len)
6549 break;
6550#endif
6551 col = curwin->w_cursor.col;
6552 inc_cursor();
6553 /* Don't change end_foundcol if already set. */
6554 if (foundcol != curwin->w_cursor.col)
6555 {
6556 foundcol = curwin->w_cursor.col;
6557 end_foundcol = foundcol;
6558 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6559 break;
6560 }
6561 curwin->w_cursor.col = col;
6562 }
6563
6564 if (curwin->w_cursor.col == 0)
6565 break;
6566
6567 col = curwin->w_cursor.col;
6568
6569 dec_cursor();
6570 cc = gchar_cursor();
6571
6572 if (WHITECHAR(cc))
6573 continue; /* break with space */
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
6580 curwin->w_cursor.col = col;
6581
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006582 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006583 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006584 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6585 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006586 }
6587#endif
6588 if (curwin->w_cursor.col == 0)
6589 break;
6590 dec_cursor();
6591 }
6592
6593 if (foundcol == 0) /* no spaces, cannot break line */
6594 {
6595 curwin->w_cursor.col = startcol;
6596 break;
6597 }
6598
6599 /* Going to break the line, remove any "$" now. */
6600 undisplay_dollar();
6601
6602 /*
6603 * Offset between cursor position and line break is used by replace
6604 * stack functions. VREPLACE does not use this, and backspaces
6605 * over the text instead.
6606 */
6607#ifdef FEAT_VREPLACE
6608 if (State & VREPLACE_FLAG)
6609 orig_col = startcol; /* Will start backspacing from here */
6610 else
6611#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006612 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006613
6614 /*
6615 * adjust startcol for spaces that will be deleted and
6616 * characters that will remain on top line
6617 */
6618 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006619 while ((cc = gchar_cursor(), WHITECHAR(cc))
6620 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006621 inc_cursor();
6622 startcol -= curwin->w_cursor.col;
6623 if (startcol < 0)
6624 startcol = 0;
6625
6626#ifdef FEAT_VREPLACE
6627 if (State & VREPLACE_FLAG)
6628 {
6629 /*
6630 * In VREPLACE mode, we will backspace over the text to be
6631 * wrapped, so save a copy now to put on the next line.
6632 */
6633 saved_text = vim_strsave(ml_get_cursor());
6634 curwin->w_cursor.col = orig_col;
6635 if (saved_text == NULL)
6636 break; /* Can't do it, out of memory */
6637 saved_text[startcol] = NUL;
6638
6639 /* Backspace over characters that will move to the next line */
6640 if (!fo_white_par)
6641 backspace_until_column(foundcol);
6642 }
6643 else
6644#endif
6645 {
6646 /* put cursor after pos. to break line */
6647 if (!fo_white_par)
6648 curwin->w_cursor.col = foundcol;
6649 }
6650
6651 /*
6652 * Split the line just before the margin.
6653 * Only insert/delete lines, but don't really redraw the window.
6654 */
6655 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6656 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6657#ifdef FEAT_COMMENTS
6658 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006659 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006660#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006661 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6662 if (!(flags & INSCHAR_COM_LIST))
6663 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006664
6665 replace_offset = 0;
6666 if (first_line)
6667 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006668 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006669 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006670 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006671 * This section is for auto-wrap of numeric lists. When not
6672 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6673 * flag will be set and open_line() will handle it (as seen
6674 * above). The code here (and in get_number_indent()) will
6675 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006676 */
6677 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006678 second_indent =
6679 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006680 if (second_indent >= 0)
6681 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006682#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006683 if (State & VREPLACE_FLAG)
6684 change_indent(INDENT_SET, second_indent,
6685 FALSE, NUL, TRUE);
6686 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006687#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006688#ifdef FEAT_COMMENTS
6689 if (leader_len > 0 && second_indent - leader_len > 0)
6690 {
6691 int i;
6692 int padding = second_indent - leader_len;
6693
6694 /* We started at the first_line of a numbered list
6695 * that has a comment. the open_line() function has
6696 * inserted the proper comment leader and positioned
6697 * the cursor at the end of the split line. Now we
6698 * add the additional whitespace needed after the
6699 * comment leader for the numbered list. */
6700 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006701 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006702 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006703 }
6704 else
6705 {
6706#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006707 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006708#ifdef FEAT_COMMENTS
6709 }
6710#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006711 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006712 }
6713 first_line = FALSE;
6714 }
6715
6716#ifdef FEAT_VREPLACE
6717 if (State & VREPLACE_FLAG)
6718 {
6719 /*
6720 * In VREPLACE mode we have backspaced over the text to be
6721 * moved, now we re-insert it into the new line.
6722 */
6723 ins_bytes(saved_text);
6724 vim_free(saved_text);
6725 }
6726 else
6727#endif
6728 {
6729 /*
6730 * Check if cursor is not past the NUL off the line, cindent
6731 * may have added or removed indent.
6732 */
6733 curwin->w_cursor.col += startcol;
6734 len = (colnr_T)STRLEN(ml_get_curline());
6735 if (curwin->w_cursor.col > len)
6736 curwin->w_cursor.col = len;
6737 }
6738
6739 haveto_redraw = TRUE;
6740#ifdef FEAT_CINDENT
6741 can_cindent = TRUE;
6742#endif
6743 /* moved the cursor, don't autoindent or cindent now */
6744 did_ai = FALSE;
6745#ifdef FEAT_SMARTINDENT
6746 did_si = FALSE;
6747 can_si = FALSE;
6748 can_si_back = FALSE;
6749#endif
6750 line_breakcheck();
6751 }
6752
6753 if (save_char != NUL) /* put back space after cursor */
6754 pchar_cursor(save_char);
6755
Bram Moolenaar0026d472014-09-09 16:32:39 +02006756#ifdef FEAT_LINEBREAK
6757 curwin->w_p_lbr = has_lbr;
6758#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006759 if (!format_only && haveto_redraw)
6760 {
6761 update_topline();
6762 redraw_curbuf_later(VALID);
6763 }
6764}
6765
6766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 * Called after inserting or deleting text: When 'formatoptions' includes the
6768 * 'a' flag format from the current line until the end of the paragraph.
6769 * Keep the cursor at the same position relative to the text.
6770 * The caller must have saved the cursor line for undo, following ones will be
6771 * saved here.
6772 */
6773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006774auto_format(
6775 int trailblank, /* when TRUE also format with trailing blank */
6776 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 pos_T pos;
6779 colnr_T len;
6780 char_u *old;
6781 char_u *new, *pnew;
6782 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006783 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
6785 if (!has_format_option(FO_AUTO))
6786 return;
6787
6788 pos = curwin->w_cursor;
6789 old = ml_get_curline();
6790
6791 /* may remove added space */
6792 check_auto_format(FALSE);
6793
6794 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6795 * user might insert normal text next. Also skip formatting when "1" is
6796 * in 'formatoptions' and there is a single character before the cursor.
6797 * Otherwise the line would be broken and when typing another non-white
6798 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006799 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (*old != NUL && !trailblank && wasatend)
6801 {
6802 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006803 cc = gchar_cursor();
6804 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6805 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006807 cc = gchar_cursor();
6808 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {
6810 curwin->w_cursor = pos;
6811 return;
6812 }
6813 curwin->w_cursor = pos;
6814 }
6815
6816#ifdef FEAT_COMMENTS
6817 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6818 * comments. */
6819 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006820 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 return;
6822#endif
6823
6824 /*
6825 * May start formatting in a previous line, so that after "x" a word is
6826 * moved to the previous line if it fits there now. Only when this is not
6827 * the start of a paragraph.
6828 */
6829 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6830 {
6831 --curwin->w_cursor.lnum;
6832 if (u_save_cursor() == FAIL)
6833 return;
6834 }
6835
6836 /*
6837 * Do the formatting and restore the cursor position. "saved_cursor" will
6838 * be adjusted for the text formatting.
6839 */
6840 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006841 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 curwin->w_cursor = saved_cursor;
6843 saved_cursor.lnum = 0;
6844
6845 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6846 {
6847 /* "cannot happen" */
6848 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6849 coladvance((colnr_T)MAXCOL);
6850 }
6851 else
6852 check_cursor_col();
6853
6854 /* Insert mode: If the cursor is now after the end of the line while it
6855 * previously wasn't, the line was broken. Because of the rule above we
6856 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6857 * formatted. */
6858 if (!wasatend && has_format_option(FO_WHITE_PAR))
6859 {
6860 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006861 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 if (curwin->w_cursor.col == len)
6863 {
6864 pnew = vim_strnsave(new, len + 2);
6865 pnew[len] = ' ';
6866 pnew[len + 1] = NUL;
6867 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6868 /* remove the space later */
6869 did_add_space = TRUE;
6870 }
6871 else
6872 /* may remove added space */
6873 check_auto_format(FALSE);
6874 }
6875
6876 check_cursor();
6877}
6878
6879/*
6880 * When an extra space was added to continue a paragraph for auto-formatting,
6881 * delete it now. The space must be under the cursor, just after the insert
6882 * position.
6883 */
6884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006885check_auto_format(
6886 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887{
6888 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006889 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890
6891 if (did_add_space)
6892 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006893 cc = gchar_cursor();
6894 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 /* Somehow the space was removed already. */
6896 did_add_space = FALSE;
6897 else
6898 {
6899 if (!end_insert)
6900 {
6901 inc_cursor();
6902 c = gchar_cursor();
6903 dec_cursor();
6904 }
6905 if (c != NUL)
6906 {
6907 /* The space is no longer at the end of the line, delete it. */
6908 del_char(FALSE);
6909 did_add_space = FALSE;
6910 }
6911 }
6912 }
6913}
6914
6915/*
6916 * Find out textwidth to be used for formatting:
6917 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006918 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 * if invalid value, use 0.
6920 * Set default to window width (maximum 79) for "gq" operator.
6921 */
6922 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006923comp_textwidth(
6924 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 int textwidth;
6927
6928 textwidth = curbuf->b_p_tw;
6929 if (textwidth == 0 && curbuf->b_p_wm)
6930 {
6931 /* The width is the window width minus 'wrapmargin' minus all the
6932 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006933 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934#ifdef FEAT_CMDWIN
6935 if (cmdwin_type != 0)
6936 textwidth -= 1;
6937#endif
6938#ifdef FEAT_FOLDING
6939 textwidth -= curwin->w_p_fdc;
6940#endif
6941#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006942 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 textwidth -= 1;
6944#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006945 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 textwidth -= 8;
6947 }
6948 if (textwidth < 0)
6949 textwidth = 0;
6950 if (ff && textwidth == 0)
6951 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006952 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (textwidth > 79)
6954 textwidth = 79;
6955 }
6956 return textwidth;
6957}
6958
6959/*
6960 * Put a character in the redo buffer, for when just after a CTRL-V.
6961 */
6962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006963redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 char_u buf[10];
6966
6967 /* Only digits need special treatment. Translate them into a string of
6968 * three digits. */
6969 if (VIM_ISDIGIT(c))
6970 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006971 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 AppendToRedobuff(buf);
6973 }
6974 else
6975 AppendCharToRedobuff(c);
6976}
6977
6978/*
6979 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006980 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 */
6982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006983start_arrow(
6984 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006986 start_arrow_common(end_insert_pos, TRUE);
6987}
6988
6989/*
6990 * Like start_arrow() but with end_change argument.
6991 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6992 */
6993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006994start_arrow_with_change(
6995 pos_T *end_insert_pos, /* can be NULL */
6996 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006997{
6998 start_arrow_common(end_insert_pos, end_change);
6999 if (!end_change)
7000 {
7001 AppendCharToRedobuff(Ctrl_G);
7002 AppendCharToRedobuff('U');
7003 }
7004}
7005
7006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007007start_arrow_common(
7008 pos_T *end_insert_pos, /* can be NULL */
7009 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007010{
7011 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {
7013 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007014 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 arrow_used = TRUE; /* this means we stopped the current insert */
7016 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007017#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007018 check_spell_redraw();
7019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020}
7021
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007022#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007023/*
7024 * If we skipped highlighting word at cursor, do it now.
7025 * It may be skipped again, thus reset spell_redraw_lnum first.
7026 */
7027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007028check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007029{
7030 if (spell_redraw_lnum != 0)
7031 {
7032 linenr_T lnum = spell_redraw_lnum;
7033
7034 spell_redraw_lnum = 0;
7035 redrawWinline(lnum, FALSE);
7036 }
7037}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007038
7039/*
7040 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7041 * spelled word, if there is one.
7042 */
7043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007044spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007045{
7046 pos_T tpos = curwin->w_cursor;
7047
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007048 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007049 if (curwin->w_cursor.col != tpos.col)
7050 start_arrow(&tpos);
7051}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007052#endif
7053
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054/*
7055 * stop_arrow() is called before a change is made in insert mode.
7056 * If an arrow key has been used, start a new insertion.
7057 * Returns FAIL if undo is impossible, shouldn't insert then.
7058 */
7059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007060stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061{
7062 if (arrow_used)
7063 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007064 Insstart = curwin->w_cursor; /* new insertion starts here */
7065 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7066 /* Don't update the original insert position when moved to the
7067 * right, except when nothing was inserted yet. */
7068 update_Insstart_orig = FALSE;
7069 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7070
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 if (u_save_cursor() == OK)
7072 {
7073 arrow_used = FALSE;
7074 ins_need_undo = FALSE;
7075 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007076
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 ai_col = 0;
7078#ifdef FEAT_VREPLACE
7079 if (State & VREPLACE_FLAG)
7080 {
7081 orig_line_count = curbuf->b_ml.ml_line_count;
7082 vr_lines_changed = 1;
7083 }
7084#endif
7085 ResetRedobuff();
7086 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007087 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 }
7089 else if (ins_need_undo)
7090 {
7091 if (u_save_cursor() == OK)
7092 ins_need_undo = FALSE;
7093 }
7094
7095#ifdef FEAT_FOLDING
7096 /* Always open fold at the cursor line when inserting something. */
7097 foldOpenCursor();
7098#endif
7099
7100 return (arrow_used || ins_need_undo ? FAIL : OK);
7101}
7102
7103/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007104 * Do a few things to stop inserting.
7105 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7106 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 */
7108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109stop_insert(
7110 pos_T *end_insert_pos,
7111 int esc, /* called by ins_esc() */
7112 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007114 int cc;
7115 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
7117 stop_redo_ins();
7118 replace_flush(); /* abandon replace stack */
7119
7120 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007121 * Save the inserted text for later redo with ^@ and CTRL-A.
7122 * Don't do it when "restart_edit" was set and nothing was inserted,
7123 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007125 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007126 if (did_restart_edit == 0 || (ptr != NULL
7127 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007128 {
7129 vim_free(last_insert);
7130 last_insert = ptr;
7131 last_insert_skip = new_insert_skip;
7132 }
7133 else
7134 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007136 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 {
7138 /* Auto-format now. It may seem strange to do this when stopping an
7139 * insertion (or moving the cursor), but it's required when appending
7140 * a line and having it end in a space. But only do it when something
7141 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007142 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007144 pos_T tpos = curwin->w_cursor;
7145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 /* When the cursor is at the end of the line after a space the
7147 * formatting will move it to the following word. Avoid that by
7148 * moving the cursor onto the space. */
7149 cc = 'x';
7150 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7151 {
7152 dec_cursor();
7153 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007154 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007155 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 }
7157
7158 auto_format(TRUE, FALSE);
7159
Bram Moolenaar1c465442017-03-12 20:10:05 +01007160 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007161 {
7162 if (gchar_cursor() != NUL)
7163 inc_cursor();
7164#ifdef FEAT_VIRTUALEDIT
7165 /* If the cursor is still at the same character, also keep
7166 * the "coladd". */
7167 if (gchar_cursor() == NUL
7168 && curwin->w_cursor.lnum == tpos.lnum
7169 && curwin->w_cursor.col == tpos.col)
7170 curwin->w_cursor.coladd = tpos.coladd;
7171#endif
7172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
7174
7175 /* If a space was inserted for auto-formatting, remove it now. */
7176 check_auto_format(TRUE);
7177
7178 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007179 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007180 * Do this when ESC was used or moving the cursor up/down.
7181 * Check for the old position still being valid, just in case the text
7182 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007183 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007184 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7185 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007187 pos_T tpos = curwin->w_cursor;
7188
7189 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007190 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007191 for (;;)
7192 {
7193 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7194 --curwin->w_cursor.col;
7195 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007196 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007197 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007198 if (del_char(TRUE) == FAIL)
7199 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007200 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007201 if (curwin->w_cursor.lnum != tpos.lnum)
7202 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007203 else
7204 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007205 /* reset tpos, could have been invalidated in the loop above */
7206 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007207 tpos.col++;
7208 if (cc != NUL && gchar_pos(&tpos) == NUL)
7209 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 /* <C-S-Right> may have started Visual mode, adjust the position for
7213 * deleted characters. */
7214 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7215 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007216 int len = (int)STRLEN(ml_get_curline());
7217
7218 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007220 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007221#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 }
7225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227 }
7228 did_ai = FALSE;
7229#ifdef FEAT_SMARTINDENT
7230 did_si = FALSE;
7231 can_si = FALSE;
7232 can_si_back = FALSE;
7233#endif
7234
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007235 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7236 * now in a different buffer. */
7237 if (end_insert_pos != NULL)
7238 {
7239 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007240 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007241 curbuf->b_op_end = *end_insert_pos;
7242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243}
7244
7245/*
7246 * Set the last inserted text to a single character.
7247 * Used for the replace command.
7248 */
7249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007250set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 char_u *s;
7253
7254 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 if (last_insert != NULL)
7257 {
7258 s = last_insert;
7259 /* Use the CTRL-V only when entering a special char */
7260 if (c < ' ' || c == DEL)
7261 *s++ = Ctrl_V;
7262 s = add_char2buf(c, s);
7263 *s++ = ESC;
7264 *s++ = NUL;
7265 last_insert_skip = 0;
7266 }
7267}
7268
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007269#if defined(EXITFREE) || defined(PROTO)
7270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007271free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007272{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007273 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007274# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007275 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007276# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007277}
7278#endif
7279
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280/*
7281 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7282 * and CSI. Handle multi-byte characters.
7283 * Returns a pointer to after the added bytes.
7284 */
7285 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007286add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287{
7288#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007289 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 int i;
7291 int len;
7292
7293 len = (*mb_char2bytes)(c, temp);
7294 for (i = 0; i < len; ++i)
7295 {
7296 c = temp[i];
7297#endif
7298 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7299 if (c == K_SPECIAL)
7300 {
7301 *s++ = K_SPECIAL;
7302 *s++ = KS_SPECIAL;
7303 *s++ = KE_FILLER;
7304 }
7305#ifdef FEAT_GUI
7306 else if (c == CSI)
7307 {
7308 *s++ = CSI;
7309 *s++ = KS_EXTRA;
7310 *s++ = (int)KE_CSI;
7311 }
7312#endif
7313 else
7314 *s++ = c;
7315#ifdef FEAT_MBYTE
7316 }
7317#endif
7318 return s;
7319}
7320
7321/*
7322 * move cursor to start of line
7323 * if flags & BL_WHITE move to first non-white
7324 * if flags & BL_SOL move to first non-white if startofline is set,
7325 * otherwise keep "curswant" column
7326 * if flags & BL_FIX don't leave the cursor on a NUL.
7327 */
7328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007329beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
7331 if ((flags & BL_SOL) && !p_sol)
7332 coladvance(curwin->w_curswant);
7333 else
7334 {
7335 curwin->w_cursor.col = 0;
7336#ifdef FEAT_VIRTUALEDIT
7337 curwin->w_cursor.coladd = 0;
7338#endif
7339
7340 if (flags & (BL_WHITE | BL_SOL))
7341 {
7342 char_u *ptr;
7343
Bram Moolenaar1c465442017-03-12 20:10:05 +01007344 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7346 ++curwin->w_cursor.col;
7347 }
7348 curwin->w_set_curswant = TRUE;
7349 }
7350}
7351
7352/*
7353 * oneright oneleft cursor_down cursor_up
7354 *
7355 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007356 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 * Return OK when successful, FAIL when we hit a line of file boundary.
7358 */
7359
7360 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007361oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
7363 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
7366#ifdef FEAT_VIRTUALEDIT
7367 if (virtual_active())
7368 {
7369 pos_T prevpos = curwin->w_cursor;
7370
7371 /* Adjust for multi-wide char (excluding TAB) */
7372 ptr = ml_get_cursor();
7373 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007374# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007376# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 ))
7380 ? ptr2cells(ptr) : 1));
7381 curwin->w_set_curswant = TRUE;
7382 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7383 return (prevpos.col != curwin->w_cursor.col
7384 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7385 }
7386#endif
7387
7388 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007389 if (*ptr == NUL)
7390 return FAIL; /* already at the very end */
7391
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007393 if (has_mbyte)
7394 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 else
7396#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007397 l = 1;
7398
7399 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7400 * contains "onemore". */
7401 if (ptr[l] == NUL
7402#ifdef FEAT_VIRTUALEDIT
7403 && (ve_flags & VE_ONEMORE) == 0
7404#endif
7405 )
7406 return FAIL;
7407 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408
7409 curwin->w_set_curswant = TRUE;
7410 return OK;
7411}
7412
7413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007414oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416#ifdef FEAT_VIRTUALEDIT
7417 if (virtual_active())
7418 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007419# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 int v = getviscol();
7423
7424 if (v == 0)
7425 return FAIL;
7426
7427# ifdef FEAT_LINEBREAK
7428 /* We might get stuck on 'showbreak', skip over it. */
7429 width = 1;
7430 for (;;)
7431 {
7432 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007433 /* getviscol() is slow, skip it when 'showbreak' is empty,
7434 * 'breakindent' is not set and there are no multi-byte
7435 * characters */
7436 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437# ifdef FEAT_MBYTE
7438 && !has_mbyte
7439# endif
7440 ) || getviscol() < v)
7441 break;
7442 ++width;
7443 }
7444# else
7445 coladvance(v - 1);
7446# endif
7447
7448 if (curwin->w_cursor.coladd == 1)
7449 {
7450 char_u *ptr;
7451
7452 /* Adjust for multi-wide char (not a TAB) */
7453 ptr = ml_get_cursor();
7454 if (*ptr != TAB && vim_isprintc(
7455# ifdef FEAT_MBYTE
7456 (*mb_ptr2char)(ptr)
7457# else
7458 *ptr
7459# endif
7460 ) && ptr2cells(ptr) > 1)
7461 curwin->w_cursor.coladd = 0;
7462 }
7463
7464 curwin->w_set_curswant = TRUE;
7465 return OK;
7466 }
7467#endif
7468
7469 if (curwin->w_cursor.col == 0)
7470 return FAIL;
7471
7472 curwin->w_set_curswant = TRUE;
7473 --curwin->w_cursor.col;
7474
7475#ifdef FEAT_MBYTE
7476 /* if the character on the left of the current cursor is a multi-byte
7477 * character, move to its first byte */
7478 if (has_mbyte)
7479 mb_adjust_cursor();
7480#endif
7481 return OK;
7482}
7483
7484 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007485cursor_up(
7486 long n,
7487 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488{
7489 linenr_T lnum;
7490
7491 if (n > 0)
7492 {
7493 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007494 /* This fails if the cursor is already in the first line or the count
7495 * is larger than the line number and '-' is in 'cpoptions' */
7496 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 return FAIL;
7498 if (n >= lnum)
7499 lnum = 1;
7500 else
7501#ifdef FEAT_FOLDING
7502 if (hasAnyFolding(curwin))
7503 {
7504 /*
7505 * Count each sequence of folded lines as one logical line.
7506 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007507 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 (void)hasFolding(lnum, &lnum, NULL);
7509
7510 while (n--)
7511 {
7512 /* move up one line */
7513 --lnum;
7514 if (lnum <= 1)
7515 break;
7516 /* If we entered a fold, move to the beginning, unless in
7517 * Insert mode or when 'foldopen' contains "all": it will open
7518 * in a moment. */
7519 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7520 (void)hasFolding(lnum, &lnum, NULL);
7521 }
7522 if (lnum < 1)
7523 lnum = 1;
7524 }
7525 else
7526#endif
7527 lnum -= n;
7528 curwin->w_cursor.lnum = lnum;
7529 }
7530
7531 /* try to advance to the column we want to be at */
7532 coladvance(curwin->w_curswant);
7533
7534 if (upd_topline)
7535 update_topline(); /* make sure curwin->w_topline is valid */
7536
7537 return OK;
7538}
7539
7540/*
7541 * Cursor down a number of logical lines.
7542 */
7543 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007544cursor_down(
7545 long n,
7546 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547{
7548 linenr_T lnum;
7549
7550 if (n > 0)
7551 {
7552 lnum = curwin->w_cursor.lnum;
7553#ifdef FEAT_FOLDING
7554 /* Move to last line of fold, will fail if it's the end-of-file. */
7555 (void)hasFolding(lnum, NULL, &lnum);
7556#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007557 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007558 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007559 if (lnum >= curbuf->b_ml.ml_line_count
7560 || (lnum + n > curbuf->b_ml.ml_line_count
7561 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 return FAIL;
7563 if (lnum + n >= curbuf->b_ml.ml_line_count)
7564 lnum = curbuf->b_ml.ml_line_count;
7565 else
7566#ifdef FEAT_FOLDING
7567 if (hasAnyFolding(curwin))
7568 {
7569 linenr_T last;
7570
7571 /* count each sequence of folded lines as one logical line */
7572 while (n--)
7573 {
7574 if (hasFolding(lnum, NULL, &last))
7575 lnum = last + 1;
7576 else
7577 ++lnum;
7578 if (lnum >= curbuf->b_ml.ml_line_count)
7579 break;
7580 }
7581 if (lnum > curbuf->b_ml.ml_line_count)
7582 lnum = curbuf->b_ml.ml_line_count;
7583 }
7584 else
7585#endif
7586 lnum += n;
7587 curwin->w_cursor.lnum = lnum;
7588 }
7589
7590 /* try to advance to the column we want to be at */
7591 coladvance(curwin->w_curswant);
7592
7593 if (upd_topline)
7594 update_topline(); /* make sure curwin->w_topline is valid */
7595
7596 return OK;
7597}
7598
7599/*
7600 * Stuff the last inserted text in the read buffer.
7601 * Last_insert actually is a copy of the redo buffer, so we
7602 * first have to remove the command.
7603 */
7604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007605stuff_inserted(
7606 int c, /* Command character to be inserted */
7607 long count, /* Repeat this many times */
7608 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 char_u *esc_ptr;
7611 char_u *ptr;
7612 char_u *last_ptr;
7613 char_u last = NUL;
7614
7615 ptr = get_last_insert();
7616 if (ptr == NULL)
7617 {
7618 EMSG(_(e_noinstext));
7619 return FAIL;
7620 }
7621
7622 /* may want to stuff the command character, to start Insert mode */
7623 if (c != NUL)
7624 stuffcharReadbuff(c);
7625 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7626 *esc_ptr = NUL; /* remove the ESC */
7627
7628 /* when the last char is either "0" or "^" it will be quoted if no ESC
7629 * comes after it OR if it will inserted more than once and "ptr"
7630 * starts with ^D. -- Acevedo
7631 */
7632 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7633 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7634 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7635 {
7636 last = *last_ptr;
7637 *last_ptr = NUL;
7638 }
7639
7640 do
7641 {
7642 stuffReadbuff(ptr);
7643 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7644 if (last)
7645 stuffReadbuff((char_u *)(last == '0'
7646 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7647 : IF_EB("\026^", CTRL_V_STR "^")));
7648 }
7649 while (--count > 0);
7650
7651 if (last)
7652 *last_ptr = last;
7653
7654 if (esc_ptr != NULL)
7655 *esc_ptr = ESC; /* put the ESC back */
7656
7657 /* may want to stuff a trailing ESC, to get out of Insert mode */
7658 if (!no_esc)
7659 stuffcharReadbuff(ESC);
7660
7661 return OK;
7662}
7663
7664 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
7667 if (last_insert == NULL)
7668 return NULL;
7669 return last_insert + last_insert_skip;
7670}
7671
7672/*
7673 * Get last inserted string, and remove trailing <Esc>.
7674 * Returns pointer to allocated memory (must be freed) or NULL.
7675 */
7676 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007677get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 char_u *s;
7680 int len;
7681
7682 if (last_insert == NULL)
7683 return NULL;
7684 s = vim_strsave(last_insert + last_insert_skip);
7685 if (s != NULL)
7686 {
7687 len = (int)STRLEN(s);
7688 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7689 s[len - 1] = NUL;
7690 }
7691 return s;
7692}
7693
7694/*
7695 * Check the word in front of the cursor for an abbreviation.
7696 * Called when the non-id character "c" has been entered.
7697 * When an abbreviation is recognized it is removed from the text and
7698 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7699 */
7700 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007701echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
7703 /* Don't check for abbreviation in paste mode, when disabled and just
7704 * after moving around with cursor keys. */
7705 if (p_paste || no_abbr || arrow_used)
7706 return FALSE;
7707
7708 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7709 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7710}
7711
7712/*
7713 * replace-stack functions
7714 *
7715 * When replacing characters, the replaced characters are remembered for each
7716 * new character. This is used to re-insert the old text when backspacing.
7717 *
7718 * There is a NUL headed list of characters for each character that is
7719 * currently in the file after the insertion point. When BS is used, one NUL
7720 * headed list is put back for the deleted character.
7721 *
7722 * For a newline, there are two NUL headed lists. One contains the characters
7723 * that the NL replaced. The extra one stores the characters after the cursor
7724 * that were deleted (always white space).
7725 *
7726 * Replace_offset is normally 0, in which case replace_push will add a new
7727 * character at the end of the stack. If replace_offset is not 0, that many
7728 * characters will be left on the stack above the newly inserted character.
7729 */
7730
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007731static char_u *replace_stack = NULL;
7732static long replace_stack_nr = 0; /* next entry in replace stack */
7733static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734
7735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736replace_push(
7737 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739 char_u *p;
7740
7741 if (replace_stack_nr < replace_offset) /* nothing to do */
7742 return;
7743 if (replace_stack_len <= replace_stack_nr)
7744 {
7745 replace_stack_len += 50;
7746 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7747 if (p == NULL) /* out of memory */
7748 {
7749 replace_stack_len -= 50;
7750 return;
7751 }
7752 if (replace_stack != NULL)
7753 {
7754 mch_memmove(p, replace_stack,
7755 (size_t)(replace_stack_nr * sizeof(char_u)));
7756 vim_free(replace_stack);
7757 }
7758 replace_stack = p;
7759 }
7760 p = replace_stack + replace_stack_nr - replace_offset;
7761 if (replace_offset)
7762 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7763 *p = c;
7764 ++replace_stack_nr;
7765}
7766
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007767#if defined(FEAT_MBYTE) || defined(PROTO)
7768/*
7769 * Push a character onto the replace stack. Handles a multi-byte character in
7770 * reverse byte order, so that the first byte is popped off first.
7771 * Return the number of bytes done (includes composing characters).
7772 */
7773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007774replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007775{
7776 int l = (*mb_ptr2len)(p);
7777 int j;
7778
7779 for (j = l - 1; j >= 0; --j)
7780 replace_push(p[j]);
7781 return l;
7782}
7783#endif
7784
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785/*
7786 * Pop one item from the replace stack.
7787 * return -1 if stack empty
7788 * return replaced character or NUL otherwise
7789 */
7790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007791replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 if (replace_stack_nr == 0)
7794 return -1;
7795 return (int)replace_stack[--replace_stack_nr];
7796}
7797
7798/*
7799 * Join the top two items on the replace stack. This removes to "off"'th NUL
7800 * encountered.
7801 */
7802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007803replace_join(
7804 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
7806 int i;
7807
7808 for (i = replace_stack_nr; --i >= 0; )
7809 if (replace_stack[i] == NUL && off-- <= 0)
7810 {
7811 --replace_stack_nr;
7812 mch_memmove(replace_stack + i, replace_stack + i + 1,
7813 (size_t)(replace_stack_nr - i));
7814 return;
7815 }
7816}
7817
7818/*
7819 * Pop bytes from the replace stack until a NUL is found, and insert them
7820 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7821 */
7822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007823replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824{
7825 int cc;
7826 int oldState = State;
7827
7828 State = NORMAL; /* don't want REPLACE here */
7829 while ((cc = replace_pop()) > 0)
7830 {
7831#ifdef FEAT_MBYTE
7832 mb_replace_pop_ins(cc);
7833#else
7834 ins_char(cc);
7835#endif
7836 dec_cursor();
7837 }
7838 State = oldState;
7839}
7840
7841#ifdef FEAT_MBYTE
7842/*
7843 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7844 * indicates a multi-byte char, pop the other bytes too.
7845 */
7846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007847mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007850 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 int i;
7852 int c;
7853
7854 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7855 {
7856 buf[0] = cc;
7857 for (i = 1; i < n; ++i)
7858 buf[i] = replace_pop();
7859 ins_bytes_len(buf, n);
7860 }
7861 else
7862 ins_char(cc);
7863
7864 if (enc_utf8)
7865 /* Handle composing chars. */
7866 for (;;)
7867 {
7868 c = replace_pop();
7869 if (c == -1) /* stack empty */
7870 break;
7871 if ((n = MB_BYTE2LEN(c)) == 1)
7872 {
7873 /* Not a multi-byte char, put it back. */
7874 replace_push(c);
7875 break;
7876 }
7877 else
7878 {
7879 buf[0] = c;
7880 for (i = 1; i < n; ++i)
7881 buf[i] = replace_pop();
7882 if (utf_iscomposing(utf_ptr2char(buf)))
7883 ins_bytes_len(buf, n);
7884 else
7885 {
7886 /* Not a composing char, put it back. */
7887 for (i = n - 1; i >= 0; --i)
7888 replace_push(buf[i]);
7889 break;
7890 }
7891 }
7892 }
7893}
7894#endif
7895
7896/*
7897 * make the replace stack empty
7898 * (called when exiting replace mode)
7899 */
7900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007901replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007903 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 replace_stack_len = 0;
7905 replace_stack_nr = 0;
7906}
7907
7908/*
7909 * Handle doing a BS for one character.
7910 * cc < 0: replace stack empty, just move cursor
7911 * cc == 0: character was inserted, delete it
7912 * cc > 0: character was replaced, put cc (first byte of original char) back
7913 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007914 * When "limit_col" is >= 0, don't delete before this column. Matters when
7915 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 */
7917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007918replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919{
7920 int cc;
7921#ifdef FEAT_VREPLACE
7922 int orig_len = 0;
7923 int ins_len;
7924 int orig_vcols = 0;
7925 colnr_T start_vcol;
7926 char_u *p;
7927 int i;
7928 int vcol;
7929#endif
7930
7931 cc = replace_pop();
7932 if (cc > 0)
7933 {
7934#ifdef FEAT_VREPLACE
7935 if (State & VREPLACE_FLAG)
7936 {
7937 /* Get the number of screen cells used by the character we are
7938 * going to delete. */
7939 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7940 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7941 }
7942#endif
7943#ifdef FEAT_MBYTE
7944 if (has_mbyte)
7945 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007946 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947# ifdef FEAT_VREPLACE
7948 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007949 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950# endif
7951 replace_push(cc);
7952 }
7953 else
7954#endif
7955 {
7956 pchar_cursor(cc);
7957#ifdef FEAT_VREPLACE
7958 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007959 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960#endif
7961 }
7962 replace_pop_ins();
7963
7964#ifdef FEAT_VREPLACE
7965 if (State & VREPLACE_FLAG)
7966 {
7967 /* Get the number of screen cells used by the inserted characters */
7968 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007969 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 vcol = start_vcol;
7971 for (i = 0; i < ins_len; ++i)
7972 {
7973 vcol += chartabsize(p + i, vcol);
7974#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007975 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976#endif
7977 }
7978 vcol -= start_vcol;
7979
7980 /* Delete spaces that were inserted after the cursor to keep the
7981 * text aligned. */
7982 curwin->w_cursor.col += ins_len;
7983 while (vcol > orig_vcols && gchar_cursor() == ' ')
7984 {
7985 del_char(FALSE);
7986 ++orig_vcols;
7987 }
7988 curwin->w_cursor.col -= ins_len;
7989 }
7990#endif
7991
7992 /* mark the buffer as changed and prepare for displaying */
7993 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7994 }
7995 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007996 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997}
7998
7999#ifdef FEAT_CINDENT
8000/*
8001 * Return TRUE if C-indenting is on.
8002 */
8003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008004cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 return (!p_paste && (curbuf->b_p_cin
8007# ifdef FEAT_EVAL
8008 || *curbuf->b_p_inde != NUL
8009# endif
8010 ));
8011}
8012#endif
8013
8014#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8015/*
8016 * Re-indent the current line, based on the current contents of it and the
8017 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8018 * confused what all the part that handles Control-T is doing that I'm not.
8019 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8020 */
8021
8022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008023fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008025 int amount = get_the_indent();
8026
8027 if (amount >= 0)
8028 {
8029 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8030 if (linewhite(curwin->w_cursor.lnum))
8031 did_ai = TRUE; /* delete the indent if the line stays empty */
8032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033}
8034
8035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008036fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037{
8038 if (p_paste)
8039 return;
8040# ifdef FEAT_LISP
8041 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8042 fixthisline(get_lisp_indent);
8043# endif
8044# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8045 else
8046# endif
8047# ifdef FEAT_CINDENT
8048 if (cindent_on())
8049 do_c_expr_indent();
8050# endif
8051}
8052
8053#endif
8054
8055#ifdef FEAT_CINDENT
8056/*
8057 * return TRUE if 'cinkeys' contains the key "keytyped",
8058 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008059 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8061 *
8062 * "keytyped" can have a few special values:
8063 * KEY_OPEN_FORW
8064 * KEY_OPEN_BACK
8065 * KEY_COMPLETE just finished completion.
8066 *
8067 * If line_is_empty is TRUE accept keys with '0' before them.
8068 */
8069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008070in_cinkeys(
8071 int keytyped,
8072 int when,
8073 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
8075 char_u *look;
8076 int try_match;
8077 int try_match_word;
8078 char_u *p;
8079 char_u *line;
8080 int icase;
8081 int i;
8082
Bram Moolenaar30848942009-12-24 14:46:12 +00008083 if (keytyped == NUL)
8084 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8085 return FALSE;
8086
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087#ifdef FEAT_EVAL
8088 if (*curbuf->b_p_inde != NUL)
8089 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8090 else
8091#endif
8092 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8093 while (*look)
8094 {
8095 /*
8096 * Find out if we want to try a match with this key, depending on
8097 * 'when' and a '*' or '!' before the key.
8098 */
8099 switch (when)
8100 {
8101 case '*': try_match = (*look == '*'); break;
8102 case '!': try_match = (*look == '!'); break;
8103 default: try_match = (*look != '*'); break;
8104 }
8105 if (*look == '*' || *look == '!')
8106 ++look;
8107
8108 /*
8109 * If there is a '0', only accept a match if the line is empty.
8110 * But may still match when typing last char of a word.
8111 */
8112 if (*look == '0')
8113 {
8114 try_match_word = try_match;
8115 if (!line_is_empty)
8116 try_match = FALSE;
8117 ++look;
8118 }
8119 else
8120 try_match_word = FALSE;
8121
8122 /*
8123 * does it look like a control character?
8124 */
8125 if (*look == '^'
8126#ifdef EBCDIC
8127 && (Ctrl_chr(look[1]) != 0)
8128#else
8129 && look[1] >= '?' && look[1] <= '_'
8130#endif
8131 )
8132 {
8133 if (try_match && keytyped == Ctrl_chr(look[1]))
8134 return TRUE;
8135 look += 2;
8136 }
8137 /*
8138 * 'o' means "o" command, open forward.
8139 * 'O' means "O" command, open backward.
8140 */
8141 else if (*look == 'o')
8142 {
8143 if (try_match && keytyped == KEY_OPEN_FORW)
8144 return TRUE;
8145 ++look;
8146 }
8147 else if (*look == 'O')
8148 {
8149 if (try_match && keytyped == KEY_OPEN_BACK)
8150 return TRUE;
8151 ++look;
8152 }
8153
8154 /*
8155 * 'e' means to check for "else" at start of line and just before the
8156 * cursor.
8157 */
8158 else if (*look == 'e')
8159 {
8160 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8161 {
8162 p = ml_get_curline();
8163 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8164 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8165 return TRUE;
8166 }
8167 ++look;
8168 }
8169
8170 /*
8171 * ':' only causes an indent if it is at the end of a label or case
8172 * statement, or when it was before typing the ':' (to fix
8173 * class::method for C++).
8174 */
8175 else if (*look == ':')
8176 {
8177 if (try_match && keytyped == ':')
8178 {
8179 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008180 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008182 /* Need to get the line again after cin_islabel(). */
8183 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (curwin->w_cursor.col > 2
8185 && p[curwin->w_cursor.col - 1] == ':'
8186 && p[curwin->w_cursor.col - 2] == ':')
8187 {
8188 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008189 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008190 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 p = ml_get_curline();
8192 p[curwin->w_cursor.col - 1] = ':';
8193 if (i)
8194 return TRUE;
8195 }
8196 }
8197 ++look;
8198 }
8199
8200
8201 /*
8202 * Is it a key in <>, maybe?
8203 */
8204 else if (*look == '<')
8205 {
8206 if (try_match)
8207 {
8208 /*
8209 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8210 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8211 * >, *, : and ! keys if they really really want to.
8212 */
8213 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8214 && keytyped == look[1])
8215 return TRUE;
8216
8217 if (keytyped == get_special_key_code(look + 1))
8218 return TRUE;
8219 }
8220 while (*look && *look != '>')
8221 look++;
8222 while (*look == '>')
8223 look++;
8224 }
8225
8226 /*
8227 * Is it a word: "=word"?
8228 */
8229 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8230 {
8231 ++look;
8232 if (*look == '~')
8233 {
8234 icase = TRUE;
8235 ++look;
8236 }
8237 else
8238 icase = FALSE;
8239 p = vim_strchr(look, ',');
8240 if (p == NULL)
8241 p = look + STRLEN(look);
8242 if ((try_match || try_match_word)
8243 && curwin->w_cursor.col >= (colnr_T)(p - look))
8244 {
8245 int match = FALSE;
8246
8247#ifdef FEAT_INS_EXPAND
8248 if (keytyped == KEY_COMPLETE)
8249 {
8250 char_u *s;
8251
8252 /* Just completed a word, check if it starts with "look".
8253 * search back for the start of a word. */
8254 line = ml_get_curline();
8255# ifdef FEAT_MBYTE
8256 if (has_mbyte)
8257 {
8258 char_u *n;
8259
8260 for (s = line + curwin->w_cursor.col; s > line; s = n)
8261 {
8262 n = mb_prevptr(line, s);
8263 if (!vim_iswordp(n))
8264 break;
8265 }
8266 }
8267 else
8268# endif
8269 for (s = line + curwin->w_cursor.col; s > line; --s)
8270 if (!vim_iswordc(s[-1]))
8271 break;
8272 if (s + (p - look) <= line + curwin->w_cursor.col
8273 && (icase
8274 ? MB_STRNICMP(s, look, p - look)
8275 : STRNCMP(s, look, p - look)) == 0)
8276 match = TRUE;
8277 }
8278 else
8279#endif
8280 /* TODO: multi-byte */
8281 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8282 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8283 {
8284 line = ml_get_cursor();
8285 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8286 || !vim_iswordc(line[-(p - look) - 1]))
8287 && (icase
8288 ? MB_STRNICMP(line - (p - look), look, p - look)
8289 : STRNCMP(line - (p - look), look, p - look))
8290 == 0)
8291 match = TRUE;
8292 }
8293 if (match && try_match_word && !try_match)
8294 {
8295 /* "0=word": Check if there are only blanks before the
8296 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008297 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 (int)(curwin->w_cursor.col - (p - look)))
8299 match = FALSE;
8300 }
8301 if (match)
8302 return TRUE;
8303 }
8304 look = p;
8305 }
8306
8307 /*
8308 * ok, it's a boring generic character.
8309 */
8310 else
8311 {
8312 if (try_match && *look == keytyped)
8313 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008314 if (*look != NUL)
8315 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317
8318 /*
8319 * Skip over ", ".
8320 */
8321 look = skip_to_option_part(look);
8322 }
8323 return FALSE;
8324}
8325#endif /* FEAT_CINDENT */
8326
8327#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8328/*
8329 * Map Hebrew keyboard when in hkmap mode.
8330 */
8331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008332hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
8334 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8335 {
8336 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8337 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8338 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8339 static char_u map[26] =
8340 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8341 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8342 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8343 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8344 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8345 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8346 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8347 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8348 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8349
8350 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8351 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8352 /* '-1'='sofit' */
8353 else if (c == 'x')
8354 return 'X';
8355 else if (c == 'q')
8356 return '\''; /* {geresh}={'} */
8357 else if (c == 246)
8358 return ' '; /* \"o --> ' ' for a german keyboard */
8359 else if (c == 228)
8360 return ' '; /* \"a --> ' ' -- / -- */
8361 else if (c == 252)
8362 return ' '; /* \"u --> ' ' -- / -- */
8363#ifdef EBCDIC
8364 else if (islower(c))
8365#else
8366 /* NOTE: islower() does not do the right thing for us on Linux so we
8367 * do this the same was as 5.7 and previous, so it works correctly on
8368 * all systems. Specifically, the e.g. Delete and Arrow keys are
8369 * munged and won't work if e.g. searching for Hebrew text.
8370 */
8371 else if (c >= 'a' && c <= 'z')
8372#endif
8373 return (int)(map[CharOrdLow(c)] + p_aleph);
8374 else
8375 return c;
8376 }
8377 else
8378 {
8379 switch (c)
8380 {
8381 case '`': return ';';
8382 case '/': return '.';
8383 case '\'': return ',';
8384 case 'q': return '/';
8385 case 'w': return '\'';
8386
8387 /* Hebrew letters - set offset from 'a' */
8388 case ',': c = '{'; break;
8389 case '.': c = 'v'; break;
8390 case ';': c = 't'; break;
8391 default: {
8392 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8393
8394#ifdef EBCDIC
8395 /* see note about islower() above */
8396 if (!islower(c))
8397#else
8398 if (c < 'a' || c > 'z')
8399#endif
8400 return c;
8401 c = str[CharOrdLow(c)];
8402 break;
8403 }
8404 }
8405
8406 return (int)(CharOrdLow(c) + p_aleph);
8407 }
8408}
8409#endif
8410
8411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008412ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
8414 int need_redraw = FALSE;
8415 int regname;
8416 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008417 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418
8419 /*
8420 * If we are going to wait for a character, show a '"'.
8421 */
8422 pc_status = PC_STATUS_UNSET;
8423 if (redrawing() && !char_avail())
8424 {
8425 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008426 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427
8428 edit_putchar('"', TRUE);
8429#ifdef FEAT_CMDL_INFO
8430 add_to_showcmd_c(Ctrl_R);
8431#endif
8432 }
8433
8434#ifdef USE_ON_FLY_SCROLL
8435 dont_scroll = TRUE; /* disallow scrolling here */
8436#endif
8437
8438 /*
8439 * Don't map the register name. This also prevents the mode message to be
8440 * deleted when ESC is hit.
8441 */
8442 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008443 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8446 {
8447 /* Get a third key for literal register insertion */
8448 literally = regname;
8449#ifdef FEAT_CMDL_INFO
8450 add_to_showcmd_c(literally);
8451#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008452 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455 --no_mapping;
8456
8457#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008458 /* Don't call u_sync() while typing the expression or giving an error
8459 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 ++no_u_sync;
8461 if (regname == '=')
8462 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008463# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008465# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008466 /* Sync undo when evaluating the expression calls setline() or
8467 * append(), so that it can be undone separately. */
8468 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008471# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 /* Restore the Input Method. */
8473 if (im_on)
8474 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008477 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8478 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008479 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 else
8483 {
8484#endif
8485 if (literally == Ctrl_O || literally == Ctrl_P)
8486 {
8487 /* Append the command to the redo buffer. */
8488 AppendCharToRedobuff(Ctrl_R);
8489 AppendCharToRedobuff(literally);
8490 AppendCharToRedobuff(regname);
8491
8492 do_put(regname, BACKWARD, 1L,
8493 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8494 }
8495 else if (insert_reg(regname, literally) == FAIL)
8496 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008497 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 need_redraw = TRUE; /* remove the '"' */
8499 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008500 else if (stop_insert_mode)
8501 /* When the '=' register was used and a function was invoked that
8502 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8503 * insert anything, need to remove the '"' */
8504 need_redraw = TRUE;
8505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506#ifdef FEAT_EVAL
8507 }
8508 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008509 if (u_sync_once == 1)
8510 ins_need_undo = TRUE;
8511 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512#endif
8513#ifdef FEAT_CMDL_INFO
8514 clear_showcmd();
8515#endif
8516
8517 /* If the inserted register is empty, we need to remove the '"' */
8518 if (need_redraw || stuff_empty())
8519 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008520
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008521 /* Disallow starting Visual mode here, would get a weird mode. */
8522 if (!vis_active && VIsual_active)
8523 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524}
8525
8526/*
8527 * CTRL-G commands in Insert mode.
8528 */
8529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 int c;
8533
8534#ifdef FEAT_INS_EXPAND
8535 /* Right after CTRL-X the cursor will be after the ruler. */
8536 setcursor();
8537#endif
8538
8539 /*
8540 * Don't map the second key. This also prevents the mode message to be
8541 * deleted when ESC is hit.
8542 */
8543 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008544 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 --no_mapping;
8546 switch (c)
8547 {
8548 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8549 case K_UP:
8550 case Ctrl_K:
8551 case 'k': ins_up(TRUE);
8552 break;
8553
8554 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8555 case K_DOWN:
8556 case Ctrl_J:
8557 case 'j': ins_down(TRUE);
8558 break;
8559
8560 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008561 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008563
8564 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008565 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008566 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008567 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 break;
8569
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008570 /* CTRL-G U: do not break undo with the next char */
8571 case 'U':
8572 /* Allow one left/right cursor movement with the next char,
8573 * without breaking undo. */
8574 dont_sync_undo = MAYBE;
8575 break;
8576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008578 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 }
8580}
8581
8582/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008583 * CTRL-^ in Insert mode.
8584 */
8585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008586ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008587{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008588 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008589 {
8590 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8591 if (State & LANGMAP)
8592 {
8593 curbuf->b_p_iminsert = B_IMODE_NONE;
8594 State &= ~LANGMAP;
8595 }
8596 else
8597 {
8598 curbuf->b_p_iminsert = B_IMODE_LMAP;
8599 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008600#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008601 im_set_active(FALSE);
8602#endif
8603 }
8604 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008605#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008606 else
8607 {
8608 /* There are no ":lmap" mappings, toggle IM */
8609 if (im_get_status())
8610 {
8611 curbuf->b_p_iminsert = B_IMODE_NONE;
8612 im_set_active(FALSE);
8613 }
8614 else
8615 {
8616 curbuf->b_p_iminsert = B_IMODE_IM;
8617 State &= ~LANGMAP;
8618 im_set_active(TRUE);
8619 }
8620 }
8621#endif
8622 set_iminsert_global();
8623 showmode();
8624#ifdef FEAT_GUI
8625 /* may show different cursor shape or color */
8626 if (gui.in_use)
8627 gui_update_cursor(TRUE, FALSE);
8628#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008629#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008630 /* Show/unshow value of 'keymap' in status lines. */
8631 status_redraw_curbuf();
8632#endif
8633}
8634
8635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 * Handle ESC in insert mode.
8637 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8638 * insert.
8639 */
8640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008641ins_esc(
8642 long *count,
8643 int cmdchar,
8644 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 int temp;
8647 static int disabled_redraw = FALSE;
8648
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008649#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008650 check_spell_redraw();
8651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652#if defined(FEAT_HANGULIN)
8653# if defined(ESC_CHG_TO_ENG_MODE)
8654 hangul_input_state_set(0);
8655# endif
8656 if (composing_hangul)
8657 {
8658 push_raw_key(composing_hangul_buffer, 2);
8659 composing_hangul = 0;
8660 }
8661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
8663 temp = curwin->w_cursor.col;
8664 if (disabled_redraw)
8665 {
8666 --RedrawingDisabled;
8667 disabled_redraw = FALSE;
8668 }
8669 if (!arrow_used)
8670 {
8671 /*
8672 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008673 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8674 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 */
8676 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008677 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679 /*
8680 * Repeating insert may take a long time. Check for
8681 * interrupt now and then.
8682 */
8683 if (*count > 0)
8684 {
8685 line_breakcheck();
8686 if (got_int)
8687 *count = 0;
8688 }
8689
8690 if (--*count > 0) /* repeat what was typed */
8691 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008692 /* Vi repeats the insert without replacing characters. */
8693 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8694 State &= ~REPLACE_FLAG;
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 (void)start_redo_ins();
8697 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008698 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 ++RedrawingDisabled;
8700 disabled_redraw = TRUE;
8701 return FALSE; /* repeat the insert */
8702 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008703 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 undisplay_dollar();
8705 }
8706
8707 /* When an autoindent was removed, curswant stays after the
8708 * indent */
8709 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8710 curwin->w_set_curswant = TRUE;
8711
8712 /* Remember the last Insert position in the '^ mark. */
8713 if (!cmdmod.keepjumps)
8714 curbuf->b_last_insert = curwin->w_cursor;
8715
8716 /*
8717 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008718 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008720 if (!nomove
8721 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722#ifdef FEAT_VIRTUALEDIT
8723 || curwin->w_cursor.coladd > 0
8724#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008725 )
8726 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008727 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728#ifdef FEAT_RIGHTLEFT
8729 && !revins_on
8730#endif
8731 )
8732 {
8733#ifdef FEAT_VIRTUALEDIT
8734 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8735 {
8736 oneleft();
8737 if (restart_edit != NUL)
8738 ++curwin->w_cursor.coladd;
8739 }
8740 else
8741#endif
8742 {
8743 --curwin->w_cursor.col;
8744#ifdef FEAT_MBYTE
8745 /* Correct cursor for multi-byte character. */
8746 if (has_mbyte)
8747 mb_adjust_cursor();
8748#endif
8749 }
8750 }
8751
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008752#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 /* Disable IM to allow typing English directly for Normal mode commands.
8754 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8755 * well). */
8756 if (!(State & LANGMAP))
8757 im_save_status(&curbuf->b_p_iminsert);
8758 im_set_active(FALSE);
8759#endif
8760
8761 State = NORMAL;
8762 /* need to position cursor again (e.g. when on a TAB ) */
8763 changed_cline_bef_curs();
8764
8765#ifdef FEAT_MOUSE
8766 setmouse();
8767#endif
8768#ifdef CURSOR_SHAPE
8769 ui_cursor_shape(); /* may show different cursor shape */
8770#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008771 if (!p_ek)
8772 /* Re-enable bracketed paste mode. */
8773 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774
8775 /*
8776 * When recording or for CTRL-O, need to display the new mode.
8777 * Otherwise remove the mode message.
8778 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008779 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 showmode();
8781 else if (p_smd)
8782 MSG("");
8783
8784 return TRUE; /* exit Insert mode */
8785}
8786
8787#ifdef FEAT_RIGHTLEFT
8788/*
8789 * Toggle language: hkmap and revins_on.
8790 * Move to end of reverse inserted text.
8791 */
8792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008793ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795 if (revins_on && revins_chars && revins_scol >= 0)
8796 {
8797 while (gchar_cursor() != NUL && revins_chars--)
8798 ++curwin->w_cursor.col;
8799 }
8800 p_ri = !p_ri;
8801 revins_on = (State == INSERT && p_ri);
8802 if (revins_on)
8803 {
8804 revins_scol = curwin->w_cursor.col;
8805 revins_legal++;
8806 revins_chars = 0;
8807 undisplay_dollar();
8808 }
8809 else
8810 revins_scol = -1;
8811#ifdef FEAT_FKMAP
8812 if (p_altkeymap)
8813 {
8814 /*
8815 * to be consistent also for redo command, using '.'
8816 * set arrow_used to true and stop it - causing to redo
8817 * characters entered in one mode (normal/reverse insert).
8818 */
8819 arrow_used = TRUE;
8820 (void)stop_arrow();
8821 p_fkmap = curwin->w_p_rl ^ p_ri;
8822 if (p_fkmap && p_ri)
8823 State = INSERT;
8824 }
8825 else
8826#endif
8827 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8828 showmode();
8829}
8830#endif
8831
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832/*
8833 * If 'keymodel' contains "startsel", may start selection.
8834 * Returns TRUE when a CTRL-O and other keys stuffed.
8835 */
8836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008837ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 if (km_startsel)
8840 switch (c)
8841 {
8842 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 case K_PAGEUP:
8845 case K_KPAGEUP:
8846 case K_PAGEDOWN:
8847 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008848# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 case K_LEFT:
8850 case K_RIGHT:
8851 case K_UP:
8852 case K_DOWN:
8853 case K_END:
8854 case K_HOME:
8855# endif
8856 if (!(mod_mask & MOD_MASK_SHIFT))
8857 break;
8858 /* FALLTHROUGH */
8859 case K_S_LEFT:
8860 case K_S_RIGHT:
8861 case K_S_UP:
8862 case K_S_DOWN:
8863 case K_S_END:
8864 case K_S_HOME:
8865 /* Start selection right away, the cursor can move with
8866 * CTRL-O when beyond the end of the line. */
8867 start_selection();
8868
8869 /* Execute the key in (insert) Select mode. */
8870 stuffcharReadbuff(Ctrl_O);
8871 if (mod_mask)
8872 {
8873 char_u buf[4];
8874
8875 buf[0] = K_SPECIAL;
8876 buf[1] = KS_MODIFIER;
8877 buf[2] = mod_mask;
8878 buf[3] = NUL;
8879 stuffReadbuff(buf);
8880 }
8881 stuffcharReadbuff(c);
8882 return TRUE;
8883 }
8884 return FALSE;
8885}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
8887/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008888 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008889 */
8890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008891ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008892{
8893#ifdef FEAT_FKMAP
8894 if (p_fkmap && p_ri)
8895 {
8896 beep_flush();
8897 EMSG(farsi_text_3); /* encoded in Farsi */
8898 return;
8899 }
8900#endif
8901
Bram Moolenaar1e015462005-09-25 22:16:38 +00008902# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008903 set_vim_var_string(VV_INSERTMODE,
8904 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008905# ifdef FEAT_VREPLACE
8906 replaceState == VREPLACE ? "v" :
8907# endif
8908 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008909# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008910 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008911 if (State & REPLACE_FLAG)
8912 State = INSERT | (State & LANGMAP);
8913 else
8914 State = replaceState | (State & LANGMAP);
8915 AppendCharToRedobuff(K_INS);
8916 showmode();
8917#ifdef CURSOR_SHAPE
8918 ui_cursor_shape(); /* may show different cursor shape */
8919#endif
8920}
8921
8922/*
8923 * Pressed CTRL-O in Insert mode.
8924 */
8925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008926ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008927{
8928#ifdef FEAT_VREPLACE
8929 if (State & VREPLACE_FLAG)
8930 restart_edit = 'V';
8931 else
8932#endif
8933 if (State & REPLACE_FLAG)
8934 restart_edit = 'R';
8935 else
8936 restart_edit = 'I';
8937#ifdef FEAT_VIRTUALEDIT
8938 if (virtual_active())
8939 ins_at_eol = FALSE; /* cursor always keeps its column */
8940 else
8941#endif
8942 ins_at_eol = (gchar_cursor() == NUL);
8943}
8944
8945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 * If the cursor is on an indent, ^T/^D insert/delete one
8947 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008948 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 * with vi. But vi only supports ^T and ^D after an
8950 * autoindent, we support it everywhere.
8951 */
8952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008953ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954{
8955 if (stop_arrow() == FAIL)
8956 return;
8957 AppendCharToRedobuff(c);
8958
8959 /*
8960 * 0^D and ^^D: remove all indent.
8961 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008962 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8963 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 {
8965 --curwin->w_cursor.col;
8966 (void)del_char(FALSE); /* delete the '^' or '0' */
8967 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8968 if (State & REPLACE_FLAG)
8969 replace_pop_ins();
8970 if (lastc == '^')
8971 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008972 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 }
8974 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008975 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
8977 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8978 did_ai = FALSE;
8979#ifdef FEAT_SMARTINDENT
8980 did_si = FALSE;
8981 can_si = FALSE;
8982 can_si_back = FALSE;
8983#endif
8984#ifdef FEAT_CINDENT
8985 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8986#endif
8987}
8988
8989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008990ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
8992 int temp;
8993
8994 if (stop_arrow() == FAIL)
8995 return;
8996 if (gchar_cursor() == NUL) /* delete newline */
8997 {
8998 temp = curwin->w_cursor.col;
8999 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009000 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009001 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009005#ifdef FEAT_VREPLACE
9006 /* Adjust orig_line_count in case more lines have been deleted than
9007 * have been added. That makes sure, that open_line() later
9008 * can access all buffer lines correctly */
9009 if (State & VREPLACE_FLAG &&
9010 orig_line_count > curbuf->b_ml.ml_line_count)
9011 orig_line_count = curbuf->b_ml.ml_line_count;
9012#endif
9013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009015 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9016 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 did_ai = FALSE;
9018#ifdef FEAT_SMARTINDENT
9019 did_si = FALSE;
9020 can_si = FALSE;
9021 can_si_back = FALSE;
9022#endif
9023 AppendCharToRedobuff(K_DEL);
9024}
9025
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009026static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009027
9028/*
9029 * Delete one character for ins_bs().
9030 */
9031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009032ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009033{
9034 dec_cursor();
9035 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9036 if (State & REPLACE_FLAG)
9037 {
9038 /* Don't delete characters before the insert point when in
9039 * Replace mode */
9040 if (curwin->w_cursor.lnum != Insstart.lnum
9041 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009042 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009043 }
9044 else
9045 (void)del_char(FALSE);
9046}
9047
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048/*
9049 * Handle Backspace, delete-word and delete-line in Insert mode.
9050 * Return TRUE when backspace was actually used.
9051 */
9052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009053ins_bs(
9054 int c,
9055 int mode,
9056 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057{
9058 linenr_T lnum;
9059 int cc;
9060 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009061 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 colnr_T mincol;
9063 int did_backspace = FALSE;
9064 int in_indent;
9065 int oldState;
9066#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009067 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068#endif
9069
9070 /*
9071 * can't delete anything in an empty file
9072 * can't backup past first character in buffer
9073 * can't backup past starting point unless 'backspace' > 1
9074 * can backup to a previous line if 'backspace' == 0
9075 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009076 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 || (
9078#ifdef FEAT_RIGHTLEFT
9079 !revins_on &&
9080#endif
9081 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9082 || (!can_bs(BS_START)
9083 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009084 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9085 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9087 && curwin->w_cursor.col <= ai_col)
9088 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9089 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009090 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 return FALSE;
9092 }
9093
9094 if (stop_arrow() == FAIL)
9095 return FALSE;
9096 in_indent = inindent(0);
9097#ifdef FEAT_CINDENT
9098 if (in_indent)
9099 can_cindent = FALSE;
9100#endif
9101#ifdef FEAT_COMMENTS
9102 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9103#endif
9104#ifdef FEAT_RIGHTLEFT
9105 if (revins_on) /* put cursor after last inserted char */
9106 inc_cursor();
9107#endif
9108
9109#ifdef FEAT_VIRTUALEDIT
9110 /* Virtualedit:
9111 * BACKSPACE_CHAR eats a virtual space
9112 * BACKSPACE_WORD eats all coladd
9113 * BACKSPACE_LINE eats all coladd and keeps going
9114 */
9115 if (curwin->w_cursor.coladd > 0)
9116 {
9117 if (mode == BACKSPACE_CHAR)
9118 {
9119 --curwin->w_cursor.coladd;
9120 return TRUE;
9121 }
9122 if (mode == BACKSPACE_WORD)
9123 {
9124 curwin->w_cursor.coladd = 0;
9125 return TRUE;
9126 }
9127 curwin->w_cursor.coladd = 0;
9128 }
9129#endif
9130
9131 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009132 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 */
9134 if (curwin->w_cursor.col == 0)
9135 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009136 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009137 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138#ifdef FEAT_RIGHTLEFT
9139 || revins_on
9140#endif
9141 )
9142 {
9143 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9144 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9145 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009146 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009147 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 }
9149 /*
9150 * In replace mode:
9151 * cc < 0: NL was inserted, delete it
9152 * cc >= 0: NL was replaced, put original characters back
9153 */
9154 cc = -1;
9155 if (State & REPLACE_FLAG)
9156 cc = replace_pop(); /* returns -1 if NL was inserted */
9157 /*
9158 * In replace mode, in the line we started replacing, we only move the
9159 * cursor.
9160 */
9161 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9162 {
9163 dec_cursor();
9164 }
9165 else
9166 {
9167#ifdef FEAT_VREPLACE
9168 if (!(State & VREPLACE_FLAG)
9169 || curwin->w_cursor.lnum > orig_line_count)
9170#endif
9171 {
9172 temp = gchar_cursor(); /* remember current char */
9173 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009174
9175 /* When "aw" is in 'formatoptions' we must delete the space at
9176 * the end of the line, otherwise the line will be broken
9177 * again when auto-formatting. */
9178 if (has_format_option(FO_AUTO)
9179 && has_format_option(FO_WHITE_PAR))
9180 {
9181 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9182 TRUE);
9183 int len;
9184
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009185 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009186 if (len > 0 && ptr[len - 1] == ' ')
9187 ptr[len - 1] = NUL;
9188 }
9189
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009190 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 if (temp == NUL && gchar_cursor() != NUL)
9192 inc_cursor();
9193 }
9194#ifdef FEAT_VREPLACE
9195 else
9196 dec_cursor();
9197#endif
9198
9199 /*
9200 * In REPLACE mode we have to put back the text that was replaced
9201 * by the NL. On the replace stack is first a NUL-terminated
9202 * sequence of characters that were deleted and then the
9203 * characters that NL replaced.
9204 */
9205 if (State & REPLACE_FLAG)
9206 {
9207 /*
9208 * Do the next ins_char() in NORMAL state, to
9209 * prevent ins_char() from replacing characters and
9210 * avoiding showmatch().
9211 */
9212 oldState = State;
9213 State = NORMAL;
9214 /*
9215 * restore characters (blanks) deleted after cursor
9216 */
9217 while (cc > 0)
9218 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009219 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220#ifdef FEAT_MBYTE
9221 mb_replace_pop_ins(cc);
9222#else
9223 ins_char(cc);
9224#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009225 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 cc = replace_pop();
9227 }
9228 /* restore the characters that NL replaced */
9229 replace_pop_ins();
9230 State = oldState;
9231 }
9232 }
9233 did_ai = FALSE;
9234 }
9235 else
9236 {
9237 /*
9238 * Delete character(s) before the cursor.
9239 */
9240#ifdef FEAT_RIGHTLEFT
9241 if (revins_on) /* put cursor on last inserted char */
9242 dec_cursor();
9243#endif
9244 mincol = 0;
9245 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009246 if (mode == BACKSPACE_LINE
9247 && (curbuf->b_p_ai
9248#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009249 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009250#endif
9251 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252#ifdef FEAT_RIGHTLEFT
9253 && !revins_on
9254#endif
9255 )
9256 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009257 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009259 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009261 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 }
9263
9264 /*
9265 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9266 */
9267 if ( mode == BACKSPACE_CHAR
9268 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009269 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009270 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 && (*(ml_get_cursor() - 1) == TAB
9272 || (*(ml_get_cursor() - 1) == ' '
9273 && (!*inserted_space_p
9274 || arrow_used))))))
9275 {
9276 int ts;
9277 colnr_T vcol;
9278 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009279 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280
9281 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009282 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009283 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009285 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 /* Compute the virtual column where we want to be. Since
9287 * 'showbreak' may get in the way, need to get the last column of
9288 * the previous character. */
9289 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009290 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 dec_cursor();
9292 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9293 inc_cursor();
9294 want_vcol = (want_vcol / ts) * ts;
9295
9296 /* delete characters until we are at or before want_vcol */
9297 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009298 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009299 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300
9301 /* insert extra spaces until we are at want_vcol */
9302 while (vcol < want_vcol)
9303 {
9304 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009305 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9306 && curwin->w_cursor.col < Insstart_orig.col)
9307 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308
9309#ifdef FEAT_VREPLACE
9310 if (State & VREPLACE_FLAG)
9311 ins_char(' ');
9312 else
9313#endif
9314 {
9315 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009316 if ((State & REPLACE_FLAG))
9317 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 }
9319 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9320 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009321
9322 /* If we are now back where we started delete one character. Can
9323 * happen when using 'sts' and 'linebreak'. */
9324 if (vcol >= start_vcol)
9325 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 }
9327
9328 /*
9329 * Delete upto starting point, start of line or previous word.
9330 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009333#ifdef FEAT_MBYTE
9334 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009336 if (has_mbyte)
9337 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009339 do
9340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009342 if (!revins_on) /* put cursor on char to be deleted */
9343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009345
9346 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009348 /* look multi-byte character class */
9349 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009351 prev_cclass = cclass;
9352 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009355
9356 /* start of word? */
9357 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9358 {
9359 mode = BACKSPACE_WORD_NOT_SPACE;
9360 temp = vim_iswordc(cc);
9361 }
9362 /* end of word? */
9363 else if (mode == BACKSPACE_WORD_NOT_SPACE
9364 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9365#ifdef FEAT_MBYTE
9366 || prev_cclass != cclass
9367#endif
9368 ))
9369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009371 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009373 inc_cursor();
9374#ifdef FEAT_RIGHTLEFT
9375 else if (State & REPLACE_FLAG)
9376 dec_cursor();
9377#endif
9378 break;
9379 }
9380 if (State & REPLACE_FLAG)
9381 replace_do_bs(-1);
9382 else
9383 {
9384#ifdef FEAT_MBYTE
9385 if (enc_utf8 && p_deco)
9386 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9387#endif
9388 (void)del_char(FALSE);
9389#ifdef FEAT_MBYTE
9390 /*
9391 * If there are combining characters and 'delcombine' is set
9392 * move the cursor back. Don't back up before the base
9393 * character.
9394 */
9395 if (enc_utf8 && p_deco && cpc[0] != NUL)
9396 inc_cursor();
9397#endif
9398#ifdef FEAT_RIGHTLEFT
9399 if (revins_chars)
9400 {
9401 revins_chars--;
9402 revins_legal++;
9403 }
9404 if (revins_on && gchar_cursor() == NUL)
9405 break;
9406#endif
9407 }
9408 /* Just a single backspace?: */
9409 if (mode == BACKSPACE_CHAR)
9410 break;
9411 } while (
9412#ifdef FEAT_RIGHTLEFT
9413 revins_on ||
9414#endif
9415 (curwin->w_cursor.col > mincol
9416 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9417 || curwin->w_cursor.col != Insstart_orig.col)));
9418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 did_backspace = TRUE;
9420 }
9421#ifdef FEAT_SMARTINDENT
9422 did_si = FALSE;
9423 can_si = FALSE;
9424 can_si_back = FALSE;
9425#endif
9426 if (curwin->w_cursor.col <= 1)
9427 did_ai = FALSE;
9428 /*
9429 * It's a little strange to put backspaces into the redo
9430 * buffer, but it makes auto-indent a lot easier to deal
9431 * with.
9432 */
9433 AppendCharToRedobuff(c);
9434
9435 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009436 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9437 && curwin->w_cursor.col < Insstart_orig.col)
9438 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439
9440 /* vi behaviour: the cursor moves backward but the character that
9441 * was there remains visible
9442 * Vim behaviour: the cursor moves backward and the character that
9443 * was there is erased from the screen.
9444 * We can emulate the vi behaviour by pretending there is a dollar
9445 * displayed even when there isn't.
9446 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009447 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 dollar_vcol = curwin->w_virtcol;
9449
Bram Moolenaarce3be472008-01-14 19:12:28 +00009450#ifdef FEAT_FOLDING
9451 /* When deleting a char the cursor line must never be in a closed fold.
9452 * E.g., when 'foldmethod' is indent and deleting the first non-white
9453 * char before a Tab. */
9454 if (did_backspace)
9455 foldOpenCursor();
9456#endif
9457
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 return did_backspace;
9459}
9460
9461#ifdef FEAT_MOUSE
9462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009463ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
9465 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009466 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467
9468# ifdef FEAT_GUI
9469 /* When GUI is active, also move/paste when 'mouse' is empty */
9470 if (!gui.in_use)
9471# endif
9472 if (!mouse_has(MOUSE_INSERT))
9473 return;
9474
9475 undisplay_dollar();
9476 tpos = curwin->w_cursor;
9477 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9478 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009479 win_T *new_curwin = curwin;
9480
9481 if (curwin != old_curwin && win_valid(old_curwin))
9482 {
9483 /* Mouse took us to another window. We need to go back to the
9484 * previous one to stop insert there properly. */
9485 curwin = old_curwin;
9486 curbuf = curwin->w_buffer;
9487 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009488 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009489 if (curwin != new_curwin && win_valid(new_curwin))
9490 {
9491 curwin = new_curwin;
9492 curbuf = curwin->w_buffer;
9493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494# ifdef FEAT_CINDENT
9495 can_cindent = TRUE;
9496# endif
9497 }
9498
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 /* redraw status lines (in case another window became active) */
9500 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501}
9502
9503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009504ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009507 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009508# ifdef FEAT_INS_EXPAND
9509 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510# endif
9511
9512 tpos = curwin->w_cursor;
9513
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009514 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 {
9516 int row, col;
9517
9518 row = mouse_row;
9519 col = mouse_col;
9520
9521 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009522 wp = mouse_find_win(&row, &col);
9523 if (wp == NULL)
9524 return;
9525 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 curbuf = curwin->w_buffer;
9527 }
9528 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 undisplay_dollar();
9530
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009531# ifdef FEAT_INS_EXPAND
9532 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009533 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009534# endif
9535 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009536 if (dir == MSCR_DOWN || dir == MSCR_UP)
9537 {
9538 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9539 scroll_redraw(dir,
9540 (long)(curwin->w_botline - curwin->w_topline));
9541 else
9542 scroll_redraw(dir, 3L);
9543 }
9544#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009545 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009546 {
9547 int val, step = 6;
9548
9549 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009550 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009551 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9552 if (val < 0)
9553 val = 0;
9554 gui_do_horiz_scroll(val, TRUE);
9555 }
9556#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009557# ifdef FEAT_INS_EXPAND
9558 did_scroll = TRUE;
9559# endif
9560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 curwin->w_redr_status = TRUE;
9563
9564 curwin = old_curwin;
9565 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009567# ifdef FEAT_INS_EXPAND
9568 /* The popup menu may overlay the window, need to redraw it.
9569 * TODO: Would be more efficient to only redraw the windows that are
9570 * overlapped by the popup menu. */
9571 if (pum_visible() && did_scroll)
9572 {
9573 redraw_all_later(NOT_VALID);
9574 ins_compl_show_pum();
9575 }
9576# endif
9577
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009578 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 {
9580 start_arrow(&tpos);
9581# ifdef FEAT_CINDENT
9582 can_cindent = TRUE;
9583# endif
9584 }
9585}
9586#endif
9587
Bram Moolenaarec2da362017-01-21 20:04:22 +01009588/*
9589 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9590 * P_PE literally.
9591 * When "drop" is TRUE then consume the text and drop it.
9592 */
9593 int
9594bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9595{
9596 int c;
9597 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9598 int idx = 0;
9599 char_u *end = find_termcode((char_u *)"PE");
9600 int ret_char = -1;
9601 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009602 int save_paste = p_paste;
9603 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009604
9605 /* If the end code is too long we can't detect it, read everything. */
9606 if (STRLEN(end) >= NUMBUFLEN)
9607 end = NULL;
9608 ++no_mapping;
9609 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009610 p_paste = TRUE;
9611 curbuf->b_p_ai = FALSE;
9612
Bram Moolenaarec2da362017-01-21 20:04:22 +01009613 for (;;)
9614 {
9615 /* When the end is not defined read everything. */
9616 if (end == NULL && vpeekc() == NUL)
9617 break;
9618 c = plain_vgetc();
9619#ifdef FEAT_MBYTE
9620 if (has_mbyte)
9621 idx += (*mb_char2bytes)(c, buf + idx);
9622 else
9623#endif
9624 buf[idx++] = c;
9625 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009626 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009627 {
9628 if (end[idx] == NUL)
9629 break; /* Found the end of paste code. */
9630 continue;
9631 }
9632 if (!drop)
9633 {
9634 switch (mode)
9635 {
9636 case PASTE_CMDLINE:
9637 put_on_cmdline(buf, idx, TRUE);
9638 break;
9639
9640 case PASTE_EX:
9641 if (gap != NULL && ga_grow(gap, idx) == OK)
9642 {
9643 mch_memmove((char *)gap->ga_data + gap->ga_len,
9644 buf, (size_t)idx);
9645 gap->ga_len += idx;
9646 }
9647 break;
9648
9649 case PASTE_INSERT:
9650 if (stop_arrow() == OK)
9651 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009652 c = buf[0];
9653 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9654 ins_eol(c);
9655 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009656 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009657 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009658 AppendToRedobuffLit(buf, idx);
9659 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009660 }
9661 break;
9662
9663 case PASTE_ONE_CHAR:
9664 if (ret_char == -1)
9665 {
9666#ifdef FEAT_MBYTE
9667 if (has_mbyte)
9668 ret_char = (*mb_ptr2char)(buf);
9669 else
9670#endif
9671 ret_char = buf[0];
9672 }
9673 break;
9674 }
9675 }
9676 idx = 0;
9677 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009678
Bram Moolenaarec2da362017-01-21 20:04:22 +01009679 --no_mapping;
9680 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009681 p_paste = save_paste;
9682 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009683
9684 return ret_char;
9685}
9686
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009687#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009689ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009690{
9691 /* We will be leaving the current window, unless closing another tab. */
9692 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9693 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9694 {
9695 undisplay_dollar();
9696 start_arrow(&curwin->w_cursor);
9697# ifdef FEAT_CINDENT
9698 can_cindent = TRUE;
9699# endif
9700 }
9701
9702 if (c == K_TABLINE)
9703 goto_tabpage(current_tab);
9704 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009705 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009706 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009707 redraw_statuslines(); /* will redraw the tabline when needed */
9708 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009709}
9710#endif
9711
9712#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009714ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 pos_T tpos;
9717
9718 undisplay_dollar();
9719 tpos = curwin->w_cursor;
9720 if (gui_do_scroll())
9721 {
9722 start_arrow(&tpos);
9723# ifdef FEAT_CINDENT
9724 can_cindent = TRUE;
9725# endif
9726 }
9727}
9728
9729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009730ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731{
9732 pos_T tpos;
9733
9734 undisplay_dollar();
9735 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009736 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 {
9738 start_arrow(&tpos);
9739# ifdef FEAT_CINDENT
9740 can_cindent = TRUE;
9741# endif
9742 }
9743}
9744#endif
9745
9746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009747ins_left(
9748 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750 pos_T tpos;
9751
9752#ifdef FEAT_FOLDING
9753 if ((fdo_flags & FDO_HOR) && KeyTyped)
9754 foldOpenCursor();
9755#endif
9756 undisplay_dollar();
9757 tpos = curwin->w_cursor;
9758 if (oneleft() == OK)
9759 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009760#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9761 /* Only call start_arrow() when not busy with preediting, it will
9762 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009763 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009764#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009765 {
9766 start_arrow_with_change(&tpos, end_change);
9767 if (!end_change)
9768 AppendCharToRedobuff(K_LEFT);
9769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770#ifdef FEAT_RIGHTLEFT
9771 /* If exit reversed string, position is fixed */
9772 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9773 revins_legal++;
9774 revins_chars++;
9775#endif
9776 }
9777
9778 /*
9779 * if 'whichwrap' set for cursor in insert mode may go to
9780 * previous line
9781 */
9782 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9783 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009784 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 start_arrow(&tpos);
9786 --(curwin->w_cursor.lnum);
9787 coladvance((colnr_T)MAXCOL);
9788 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9789 }
9790 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009791 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009792 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793}
9794
9795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009796ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798 pos_T tpos;
9799
9800#ifdef FEAT_FOLDING
9801 if ((fdo_flags & FDO_HOR) && KeyTyped)
9802 foldOpenCursor();
9803#endif
9804 undisplay_dollar();
9805 tpos = curwin->w_cursor;
9806 if (c == K_C_HOME)
9807 curwin->w_cursor.lnum = 1;
9808 curwin->w_cursor.col = 0;
9809#ifdef FEAT_VIRTUALEDIT
9810 curwin->w_cursor.coladd = 0;
9811#endif
9812 curwin->w_curswant = 0;
9813 start_arrow(&tpos);
9814}
9815
9816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009817ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819 pos_T tpos;
9820
9821#ifdef FEAT_FOLDING
9822 if ((fdo_flags & FDO_HOR) && KeyTyped)
9823 foldOpenCursor();
9824#endif
9825 undisplay_dollar();
9826 tpos = curwin->w_cursor;
9827 if (c == K_C_END)
9828 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9829 coladvance((colnr_T)MAXCOL);
9830 curwin->w_curswant = MAXCOL;
9831
9832 start_arrow(&tpos);
9833}
9834
9835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009836ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837{
9838#ifdef FEAT_FOLDING
9839 if ((fdo_flags & FDO_HOR) && KeyTyped)
9840 foldOpenCursor();
9841#endif
9842 undisplay_dollar();
9843 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9844 {
9845 start_arrow(&curwin->w_cursor);
9846 (void)bck_word(1L, FALSE, FALSE);
9847 curwin->w_set_curswant = TRUE;
9848 }
9849 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009850 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851}
9852
9853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009854ins_right(
9855 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
9857#ifdef FEAT_FOLDING
9858 if ((fdo_flags & FDO_HOR) && KeyTyped)
9859 foldOpenCursor();
9860#endif
9861 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009862 if (gchar_cursor() != NUL
9863#ifdef FEAT_VIRTUALEDIT
9864 || virtual_active()
9865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 )
9867 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009868 start_arrow_with_change(&curwin->w_cursor, end_change);
9869 if (!end_change)
9870 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 curwin->w_set_curswant = TRUE;
9872#ifdef FEAT_VIRTUALEDIT
9873 if (virtual_active())
9874 oneright();
9875 else
9876#endif
9877 {
9878#ifdef FEAT_MBYTE
9879 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009880 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 else
9882#endif
9883 ++curwin->w_cursor.col;
9884 }
9885
9886#ifdef FEAT_RIGHTLEFT
9887 revins_legal++;
9888 if (revins_chars)
9889 revins_chars--;
9890#endif
9891 }
9892 /* if 'whichwrap' set for cursor in insert mode, may move the
9893 * cursor to the next line */
9894 else if (vim_strchr(p_ww, ']') != NULL
9895 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9896 {
9897 start_arrow(&curwin->w_cursor);
9898 curwin->w_set_curswant = TRUE;
9899 ++curwin->w_cursor.lnum;
9900 curwin->w_cursor.col = 0;
9901 }
9902 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009903 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009904 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
9907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009908ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909{
9910#ifdef FEAT_FOLDING
9911 if ((fdo_flags & FDO_HOR) && KeyTyped)
9912 foldOpenCursor();
9913#endif
9914 undisplay_dollar();
9915 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9916 || gchar_cursor() != NUL)
9917 {
9918 start_arrow(&curwin->w_cursor);
9919 (void)fwd_word(1L, FALSE, 0);
9920 curwin->w_set_curswant = TRUE;
9921 }
9922 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009923 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924}
9925
9926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009927ins_up(
9928 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929{
9930 pos_T tpos;
9931 linenr_T old_topline = curwin->w_topline;
9932#ifdef FEAT_DIFF
9933 int old_topfill = curwin->w_topfill;
9934#endif
9935
9936 undisplay_dollar();
9937 tpos = curwin->w_cursor;
9938 if (cursor_up(1L, TRUE) == OK)
9939 {
9940 if (startcol)
9941 coladvance(getvcol_nolist(&Insstart));
9942 if (old_topline != curwin->w_topline
9943#ifdef FEAT_DIFF
9944 || old_topfill != curwin->w_topfill
9945#endif
9946 )
9947 redraw_later(VALID);
9948 start_arrow(&tpos);
9949#ifdef FEAT_CINDENT
9950 can_cindent = TRUE;
9951#endif
9952 }
9953 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009954 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955}
9956
9957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009958ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
9960 pos_T tpos;
9961
9962 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009963
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009964 if (mod_mask & MOD_MASK_CTRL)
9965 {
9966 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009967 if (first_tabpage->tp_next != NULL)
9968 {
9969 start_arrow(&curwin->w_cursor);
9970 goto_tabpage(-1);
9971 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009972 return;
9973 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009974
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 tpos = curwin->w_cursor;
9976 if (onepage(BACKWARD, 1L) == OK)
9977 {
9978 start_arrow(&tpos);
9979#ifdef FEAT_CINDENT
9980 can_cindent = TRUE;
9981#endif
9982 }
9983 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009984 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985}
9986
9987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009988ins_down(
9989 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990{
9991 pos_T tpos;
9992 linenr_T old_topline = curwin->w_topline;
9993#ifdef FEAT_DIFF
9994 int old_topfill = curwin->w_topfill;
9995#endif
9996
9997 undisplay_dollar();
9998 tpos = curwin->w_cursor;
9999 if (cursor_down(1L, TRUE) == OK)
10000 {
10001 if (startcol)
10002 coladvance(getvcol_nolist(&Insstart));
10003 if (old_topline != curwin->w_topline
10004#ifdef FEAT_DIFF
10005 || old_topfill != curwin->w_topfill
10006#endif
10007 )
10008 redraw_later(VALID);
10009 start_arrow(&tpos);
10010#ifdef FEAT_CINDENT
10011 can_cindent = TRUE;
10012#endif
10013 }
10014 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010015 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016}
10017
10018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010019ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020{
10021 pos_T tpos;
10022
10023 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010024
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010025 if (mod_mask & MOD_MASK_CTRL)
10026 {
10027 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010028 if (first_tabpage->tp_next != NULL)
10029 {
10030 start_arrow(&curwin->w_cursor);
10031 goto_tabpage(0);
10032 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010033 return;
10034 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010035
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 tpos = curwin->w_cursor;
10037 if (onepage(FORWARD, 1L) == OK)
10038 {
10039 start_arrow(&tpos);
10040#ifdef FEAT_CINDENT
10041 can_cindent = TRUE;
10042#endif
10043 }
10044 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010045 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048#ifdef FEAT_DND
10049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010050ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051{
10052 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10053}
10054#endif
10055
10056/*
10057 * Handle TAB in Insert or Replace mode.
10058 * Return TRUE when the TAB needs to be inserted like a normal character.
10059 */
10060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010061ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
10063 int ind;
10064 int i;
10065 int temp;
10066
10067 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10068 Insstart_blank_vcol = get_nolist_virtcol();
10069 if (echeck_abbr(TAB + ABBR_OFF))
10070 return FALSE;
10071
10072 ind = inindent(0);
10073#ifdef FEAT_CINDENT
10074 if (ind)
10075 can_cindent = FALSE;
10076#endif
10077
10078 /*
10079 * When nothing special, insert TAB like a normal character
10080 */
10081 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010082 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010083 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 return TRUE;
10085
10086 if (stop_arrow() == FAIL)
10087 return TRUE;
10088
10089 did_ai = FALSE;
10090#ifdef FEAT_SMARTINDENT
10091 did_si = FALSE;
10092 can_si = FALSE;
10093 can_si_back = FALSE;
10094#endif
10095 AppendToRedobuff((char_u *)"\t");
10096
10097 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010098 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010099 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10100 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 else /* otherwise use 'tabstop' */
10102 temp = (int)curbuf->b_p_ts;
10103 temp -= get_nolist_virtcol() % temp;
10104
10105 /*
10106 * Insert the first space with ins_char(). It will delete one char in
10107 * replace mode. Insert the rest with ins_str(); it will not delete any
10108 * chars. For VREPLACE mode, we use ins_char() for all characters.
10109 */
10110 ins_char(' ');
10111 while (--temp > 0)
10112 {
10113#ifdef FEAT_VREPLACE
10114 if (State & VREPLACE_FLAG)
10115 ins_char(' ');
10116 else
10117#endif
10118 {
10119 ins_str((char_u *)" ");
10120 if (State & REPLACE_FLAG) /* no char replaced */
10121 replace_push(NUL);
10122 }
10123 }
10124
10125 /*
10126 * When 'expandtab' not set: Replace spaces by TABs where possible.
10127 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010128 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 {
10130 char_u *ptr;
10131#ifdef FEAT_VREPLACE
10132 char_u *saved_line = NULL; /* init for GCC */
10133 pos_T pos;
10134#endif
10135 pos_T fpos;
10136 pos_T *cursor;
10137 colnr_T want_vcol, vcol;
10138 int change_col = -1;
10139 int save_list = curwin->w_p_list;
10140
10141 /*
10142 * Get the current line. For VREPLACE mode, don't make real changes
10143 * yet, just work on a copy of the line.
10144 */
10145#ifdef FEAT_VREPLACE
10146 if (State & VREPLACE_FLAG)
10147 {
10148 pos = curwin->w_cursor;
10149 cursor = &pos;
10150 saved_line = vim_strsave(ml_get_curline());
10151 if (saved_line == NULL)
10152 return FALSE;
10153 ptr = saved_line + pos.col;
10154 }
10155 else
10156#endif
10157 {
10158 ptr = ml_get_cursor();
10159 cursor = &curwin->w_cursor;
10160 }
10161
10162 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10163 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10164 curwin->w_p_list = FALSE;
10165
10166 /* Find first white before the cursor */
10167 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010168 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 {
10170 --fpos.col;
10171 --ptr;
10172 }
10173
10174 /* In Replace mode, don't change characters before the insert point. */
10175 if ((State & REPLACE_FLAG)
10176 && fpos.lnum == Insstart.lnum
10177 && fpos.col < Insstart.col)
10178 {
10179 ptr += Insstart.col - fpos.col;
10180 fpos.col = Insstart.col;
10181 }
10182
10183 /* compute virtual column numbers of first white and cursor */
10184 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10185 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10186
Bram Moolenaar597a4222014-06-25 14:39:50 +020010187 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10188 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010189 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010191 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 if (vcol + i > want_vcol)
10193 break;
10194 if (*ptr != TAB)
10195 {
10196 *ptr = TAB;
10197 if (change_col < 0)
10198 {
10199 change_col = fpos.col; /* Column of first change */
10200 /* May have to adjust Insstart */
10201 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10202 Insstart.col = fpos.col;
10203 }
10204 }
10205 ++fpos.col;
10206 ++ptr;
10207 vcol += i;
10208 }
10209
10210 if (change_col >= 0)
10211 {
10212 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010213 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214
10215 /* Skip over the spaces we need. */
10216 while (vcol < want_vcol && *ptr == ' ')
10217 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010218 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 ++ptr;
10220 ++repl_off;
10221 }
10222 if (vcol > want_vcol)
10223 {
10224 /* Must have a char with 'showbreak' just before it. */
10225 --ptr;
10226 --repl_off;
10227 }
10228 fpos.col += repl_off;
10229
10230 /* Delete following spaces. */
10231 i = cursor->col - fpos.col;
10232 if (i > 0)
10233 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010234 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 /* correct replace stack. */
10236 if ((State & REPLACE_FLAG)
10237#ifdef FEAT_VREPLACE
10238 && !(State & VREPLACE_FLAG)
10239#endif
10240 )
10241 for (temp = i; --temp >= 0; )
10242 replace_join(repl_off);
10243 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010244#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010245 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010246 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010247 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010248 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10249 (char_u *)"\t", 1);
10250 }
10251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 cursor->col -= i;
10253
10254#ifdef FEAT_VREPLACE
10255 /*
10256 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10257 * backspacing over the changed spacing and then inserting the new
10258 * spacing.
10259 */
10260 if (State & VREPLACE_FLAG)
10261 {
10262 /* Backspace from real cursor to change_col */
10263 backspace_until_column(change_col);
10264
10265 /* Insert each char in saved_line from changed_col to
10266 * ptr-cursor */
10267 ins_bytes_len(saved_line + change_col,
10268 cursor->col - change_col);
10269 }
10270#endif
10271 }
10272
10273#ifdef FEAT_VREPLACE
10274 if (State & VREPLACE_FLAG)
10275 vim_free(saved_line);
10276#endif
10277 curwin->w_p_list = save_list;
10278 }
10279
10280 return FALSE;
10281}
10282
10283/*
10284 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010285 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 */
10287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010288ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289{
10290 int i;
10291
10292 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010293 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 undisplay_dollar();
10297
10298 /*
10299 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10300 * character under the cursor. Only push a NUL on the replace stack,
10301 * nothing to put back when the NL is deleted.
10302 */
10303 if ((State & REPLACE_FLAG)
10304#ifdef FEAT_VREPLACE
10305 && !(State & VREPLACE_FLAG)
10306#endif
10307 )
10308 replace_push(NUL);
10309
10310 /*
10311 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10312 * replacing the next line, so we push all of the characters left on the
10313 * line onto the replace stack. This is not done here though, it is done
10314 * in open_line().
10315 */
10316
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010317#ifdef FEAT_VIRTUALEDIT
10318 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10319 * CTRL-O). */
10320 if (virtual_active() && curwin->w_cursor.coladd > 0)
10321 coladvance(getviscol());
10322#endif
10323
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324#ifdef FEAT_RIGHTLEFT
10325# ifdef FEAT_FKMAP
10326 if (p_altkeymap && p_fkmap)
10327 fkmap(NL);
10328# endif
10329 /* NL in reverse insert will always start in the end of
10330 * current line. */
10331 if (revins_on)
10332 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10333#endif
10334
10335 AppendToRedobuff(NL_STR);
10336 i = open_line(FORWARD,
10337#ifdef FEAT_COMMENTS
10338 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10339#endif
10340 0, old_indent);
10341 old_indent = 0;
10342#ifdef FEAT_CINDENT
10343 can_cindent = TRUE;
10344#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010345#ifdef FEAT_FOLDING
10346 /* When inserting a line the cursor line must never be in a closed fold. */
10347 foldOpenCursor();
10348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010350 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351}
10352
10353#ifdef FEAT_DIGRAPHS
10354/*
10355 * Handle digraph in insert mode.
10356 * Returns character still to be inserted, or NUL when nothing remaining to be
10357 * done.
10358 */
10359 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010360ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361{
10362 int c;
10363 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010364 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365
10366 pc_status = PC_STATUS_UNSET;
10367 if (redrawing() && !char_avail())
10368 {
10369 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010370 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371
10372 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010373 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374#ifdef FEAT_CMDL_INFO
10375 add_to_showcmd_c(Ctrl_K);
10376#endif
10377 }
10378
10379#ifdef USE_ON_FLY_SCROLL
10380 dont_scroll = TRUE; /* disallow scrolling here */
10381#endif
10382
10383 /* don't map the digraph chars. This also prevents the
10384 * mode message to be deleted when ESC is hit */
10385 ++no_mapping;
10386 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010387 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 --no_mapping;
10389 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010390 if (did_putchar)
10391 /* when the line fits in 'columns' the '?' is at the start of the next
10392 * line and will not be removed by the redraw */
10393 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010394
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 if (IS_SPECIAL(c) || mod_mask) /* special key */
10396 {
10397#ifdef FEAT_CMDL_INFO
10398 clear_showcmd();
10399#endif
10400 insert_special(c, TRUE, FALSE);
10401 return NUL;
10402 }
10403 if (c != ESC)
10404 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010405 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 if (redrawing() && !char_avail())
10407 {
10408 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010409 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410
10411 if (char2cells(c) == 1)
10412 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010413 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010415 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 }
10417#ifdef FEAT_CMDL_INFO
10418 add_to_showcmd_c(c);
10419#endif
10420 }
10421 ++no_mapping;
10422 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010423 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 --no_mapping;
10425 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010426 if (did_putchar)
10427 /* when the line fits in 'columns' the '?' is at the start of the
10428 * next line and will not be removed by a redraw */
10429 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 if (cc != ESC)
10431 {
10432 AppendToRedobuff((char_u *)CTRL_V_STR);
10433 c = getdigraph(c, cc, TRUE);
10434#ifdef FEAT_CMDL_INFO
10435 clear_showcmd();
10436#endif
10437 return c;
10438 }
10439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440#ifdef FEAT_CMDL_INFO
10441 clear_showcmd();
10442#endif
10443 return NUL;
10444}
10445#endif
10446
10447/*
10448 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10449 * Returns the char to be inserted, or NUL if none found.
10450 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010452ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453{
10454 int c;
10455 int temp;
10456 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010457 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458
10459 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10460 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010461 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 return NUL;
10463 }
10464
10465 /* try to advance to the cursor column */
10466 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010467 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 prev_ptr = ptr;
10469 validate_virtcol();
10470 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10471 {
10472 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010473 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 }
10475 if ((colnr_T)temp > curwin->w_virtcol)
10476 ptr = prev_ptr;
10477
10478#ifdef FEAT_MBYTE
10479 c = (*mb_ptr2char)(ptr);
10480#else
10481 c = *ptr;
10482#endif
10483 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010484 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 return c;
10486}
10487
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010488/*
10489 * CTRL-Y or CTRL-E typed in Insert mode.
10490 */
10491 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010492ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010493{
10494 int c = tc;
10495
10496#ifdef FEAT_INS_EXPAND
10497 if (ctrl_x_mode == CTRL_X_SCROLL)
10498 {
10499 if (c == Ctrl_Y)
10500 scrolldown_clamp();
10501 else
10502 scrollup_clamp();
10503 redraw_later(VALID);
10504 }
10505 else
10506#endif
10507 {
10508 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10509 if (c != NUL)
10510 {
10511 long tw_save;
10512
10513 /* The character must be taken literally, insert like it
10514 * was typed after a CTRL-V, and pretend 'textwidth'
10515 * wasn't set. Digits, 'o' and 'x' are special after a
10516 * CTRL-V, don't use it for these. */
10517 if (c < 256 && !isalnum(c))
10518 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10519 tw_save = curbuf->b_p_tw;
10520 curbuf->b_p_tw = -1;
10521 insert_special(c, TRUE, FALSE);
10522 curbuf->b_p_tw = tw_save;
10523#ifdef FEAT_RIGHTLEFT
10524 revins_chars++;
10525 revins_legal++;
10526#endif
10527 c = Ctrl_V; /* pretend CTRL-V is last character */
10528 auto_format(FALSE, TRUE);
10529 }
10530 }
10531 return c;
10532}
10533
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534#ifdef FEAT_SMARTINDENT
10535/*
10536 * Try to do some very smart auto-indenting.
10537 * Used when inserting a "normal" character.
10538 */
10539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010540ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541{
10542 pos_T *pos, old_pos;
10543 char_u *ptr;
10544 int i;
10545 int temp;
10546
10547 /*
10548 * do some very smart indenting when entering '{' or '}'
10549 */
10550 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10551 {
10552 /*
10553 * for '}' set indent equal to indent of line containing matching '{'
10554 */
10555 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10556 {
10557 old_pos = curwin->w_cursor;
10558 /*
10559 * If the matching '{' has a ')' immediately before it (ignoring
10560 * white-space), then line up with the start of the line
10561 * containing the matching '(' if there is one. This handles the
10562 * case where an "if (..\n..) {" statement continues over multiple
10563 * lines -- webb
10564 */
10565 ptr = ml_get(pos->lnum);
10566 i = pos->col;
10567 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010568 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 ;
10570 curwin->w_cursor.lnum = pos->lnum;
10571 curwin->w_cursor.col = i;
10572 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10573 curwin->w_cursor = *pos;
10574 i = get_indent();
10575 curwin->w_cursor = old_pos;
10576#ifdef FEAT_VREPLACE
10577 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010578 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 else
10580#endif
10581 (void)set_indent(i, SIN_CHANGED);
10582 }
10583 else if (curwin->w_cursor.col > 0)
10584 {
10585 /*
10586 * when inserting '{' after "O" reduce indent, but not
10587 * more than indent of previous line
10588 */
10589 temp = TRUE;
10590 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10591 {
10592 old_pos = curwin->w_cursor;
10593 i = get_indent();
10594 while (curwin->w_cursor.lnum > 1)
10595 {
10596 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10597
10598 /* ignore empty lines and lines starting with '#'. */
10599 if (*ptr != '#' && *ptr != NUL)
10600 break;
10601 }
10602 if (get_indent() >= i)
10603 temp = FALSE;
10604 curwin->w_cursor = old_pos;
10605 }
10606 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010607 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 }
10609 }
10610
10611 /*
10612 * set indent of '#' always to 0
10613 */
10614 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10615 {
10616 /* remember current indent for next line */
10617 old_indent = get_indent();
10618 (void)set_indent(0, SIN_CHANGED);
10619 }
10620
10621 /* Adjust ai_col, the char at this position can be deleted. */
10622 if (ai_col > curwin->w_cursor.col)
10623 ai_col = curwin->w_cursor.col;
10624}
10625#endif
10626
10627/*
10628 * Get the value that w_virtcol would have when 'list' is off.
10629 * Unless 'cpo' contains the 'L' flag.
10630 */
10631 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010632get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
10634 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10635 return getvcol_nolist(&curwin->w_cursor);
10636 validate_virtcol();
10637 return curwin->w_virtcol;
10638}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010639
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010640#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010641/*
10642 * Handle the InsertCharPre autocommand.
10643 * "c" is the character that was typed.
10644 * Return a pointer to allocated memory with the replacement string.
10645 * Return NULL to continue inserting "c".
10646 */
10647 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010648do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010649{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010650 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010651 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010652
10653 /* Return quickly when there is nothing to do. */
10654 if (!has_insertcharpre())
10655 return NULL;
10656
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010657# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010658 if (has_mbyte)
10659 buf[(*mb_char2bytes)(c, buf)] = NUL;
10660 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010661# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010662 {
10663 buf[0] = c;
10664 buf[1] = NUL;
10665 }
10666
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010667 /* Lock the text to avoid weird things from happening. */
10668 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010669 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010670
Bram Moolenaar704984a2012-06-01 14:57:51 +020010671 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010672 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010673 {
10674 /* Get the value of v:char. It may be empty or more than one
10675 * character. Only use it when changed, otherwise continue with the
10676 * original character to avoid breaking autoindent. */
10677 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10678 res = vim_strsave(get_vim_var_str(VV_CHAR));
10679 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010680
10681 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10682 --textlock;
10683
10684 return res;
10685}
10686#endif