blob: 76cbfce54984e7d184479e49b768d8f3b8e42b96 [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 Moolenaar6d41c782018-06-06 09:11:12 +0200814
815 if (stop_insert_mode)
816 {
817 // Insert mode ended, possibly from a callback.
818 count = 0;
819 nomove = TRUE;
820 goto doESCkey;
821 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100822 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000824 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
825 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827#ifdef FEAT_RIGHTLEFT
828 if (p_hkmap && KeyTyped)
829 c = hkmap(c); /* Hebrew mode mapping */
830#endif
831#ifdef FEAT_FKMAP
832 if (p_fkmap && KeyTyped)
833 c = fkmap(c); /* Farsi mode mapping */
834#endif
835
836#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000837 /*
838 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000839 * and the cursor is still in the completed word. Only when there is
840 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000841 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000842 if (compl_started
843 && pum_wanted()
844 && curwin->w_cursor.col >= compl_col
845 && (compl_shown_match == NULL
846 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000847 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000848 /* BS: Delete one character from "compl_leader". */
849 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000850 && curwin->w_cursor.col > compl_col
851 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000852 continue;
853
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000854 /* When no match was selected or it was edited. */
855 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000856 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000857 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000858 * "compl_leader". Except when at the original match and
859 * there is nothing to add, CTRL-L works like CTRL-P then. */
860 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100861 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000862 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000863 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000864 {
865 ins_compl_addfrommatch();
866 continue;
867 }
868
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000869 /* A non-white character that fits in with the current
870 * completion: Add to "compl_leader". */
871 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000872 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100873#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100874 /* Trigger InsertCharPre. */
875 char_u *str = do_insert_char_pre(c);
876 char_u *p;
877
878 if (str != NULL)
879 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100880 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100881 ins_compl_addleader(PTR2CHAR(p));
882 vim_free(str);
883 }
884 else
885#endif
886 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000887 continue;
888 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000889
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000890 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000891 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200892 if ((c == Ctrl_Y || (compl_enter_selects
893 && (c == CAR || c == K_KENTER || c == NL)))
894 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000895 {
896 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200897 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000898 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000899 }
900 }
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
903 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000904 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000905 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000906 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908
Bram Moolenaar488c6512005-08-11 20:09:58 +0000909 /* CTRL-\ CTRL-N goes to Normal mode,
910 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
911 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (c == Ctrl_BSL)
913 {
914 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000915 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 ++no_mapping;
917 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000918 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 --no_mapping;
920 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000921 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000923 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 vungetc(c);
925 c = Ctrl_BSL;
926 }
927 else if (c == Ctrl_G && p_im)
928 continue;
929 else
930 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000931 if (c == Ctrl_O)
932 {
933 ins_ctrl_o();
934 ins_at_eol = FALSE; /* cursor keeps its column */
935 nomove = TRUE;
936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 count = 0;
938 goto doESCkey;
939 }
940 }
941
942#ifdef FEAT_DIGRAPHS
943 c = do_digraph(c);
944#endif
945
946#ifdef FEAT_INS_EXPAND
947 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
948 goto docomplete;
949#endif
950 if (c == Ctrl_V || c == Ctrl_Q)
951 {
952 ins_ctrl_v();
953 c = Ctrl_V; /* pretend CTRL-V is last typed character */
954 continue;
955 }
956
957#ifdef FEAT_CINDENT
958 if (cindent_on()
959# ifdef FEAT_INS_EXPAND
960 && ctrl_x_mode == 0
961# endif
962 )
963 {
964 /* A key name preceded by a bang means this key is not to be
965 * inserted. Skip ahead to the re-indenting below.
966 * A key name preceded by a star means that indenting has to be
967 * done before inserting the key. */
968 line_is_white = inindent(0);
969 if (in_cinkeys(c, '!', line_is_white))
970 goto force_cindent;
971 if (can_cindent && in_cinkeys(c, '*', line_is_white)
972 && stop_arrow() == OK)
973 do_c_expr_indent();
974 }
975#endif
976
977#ifdef FEAT_RIGHTLEFT
978 if (curwin->w_p_rl)
979 switch (c)
980 {
981 case K_LEFT: c = K_RIGHT; break;
982 case K_S_LEFT: c = K_S_RIGHT; break;
983 case K_C_LEFT: c = K_C_RIGHT; break;
984 case K_RIGHT: c = K_LEFT; break;
985 case K_S_RIGHT: c = K_S_LEFT; break;
986 case K_C_RIGHT: c = K_C_LEFT; break;
987 }
988#endif
989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 /*
991 * If 'keymodel' contains "startsel", may start selection. If it
992 * does, a CTRL-O and c will be stuffed, we need to get these
993 * characters.
994 */
995 if (ins_start_select(c))
996 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998 /*
999 * The big switch to handle a character in insert mode.
1000 */
1001 switch (c)
1002 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001003 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (echeck_abbr(ESC + ABBR_OFF))
1005 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001006 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009#ifdef FEAT_CMDWIN
1010 if (c == Ctrl_C && cmdwin_type != 0)
1011 {
1012 /* Close the cmdline window. */
1013 cmdwin_result = K_IGNORE;
1014 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001015 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 goto doESCkey;
1017 }
1018#endif
1019
1020#ifdef UNIX
1021do_intr:
1022#endif
1023 /* when 'insertmode' set, and not halfway a mapping, don't leave
1024 * Insert mode */
1025 if (goto_im())
1026 {
1027 if (got_int)
1028 {
1029 (void)vgetc(); /* flush all buffers */
1030 got_int = FALSE;
1031 }
1032 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001033 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 break;
1035 }
1036doESCkey:
1037 /*
1038 * This is the ONLY return from edit()!
1039 */
1040 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1041 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001042 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 o_lnum = curwin->w_cursor.lnum;
1044
Bram Moolenaar488c6512005-08-11 20:09:58 +00001045 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001046 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00001047 if (cmdchar != 'r' && cmdchar != 'v')
1048 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1049 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001050 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 continue;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case Ctrl_Z: /* suspend when 'insertmode' set */
1056 if (!p_im)
1057 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001058 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001059#ifdef CURSOR_SHAPE
1060 ui_cursor_shape(); /* may need to update cursor shape */
1061#endif
1062 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063
1064 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001065#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001066 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 goto docomplete;
1068#endif
1069 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1070 break;
1071 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001072
1073#ifdef FEAT_VIRTUALEDIT
1074 /* don't move the cursor left when 'virtualedit' has "onemore". */
1075 if (ve_flags & VE_ONEMORE)
1076 {
1077 ins_at_eol = FALSE;
1078 nomove = TRUE;
1079 }
1080#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001081 count = 0;
1082 goto doESCkey;
1083
Bram Moolenaar572cb562005-08-05 21:35:02 +00001084 case K_INS: /* toggle insert/replace mode */
1085 case K_KINS:
1086 ins_insert(replaceState);
1087 break;
1088
1089 case K_SELECT: /* end of Select mode mapping - ignore */
1090 break;
1091
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 case K_HELP: /* Help key works like <ESC> <Help> */
1093 case K_F1:
1094 case K_XF1:
1095 stuffcharReadbuff(K_HELP);
1096 if (p_im)
1097 need_start_insertmode = TRUE;
1098 goto doESCkey;
1099
1100#ifdef FEAT_NETBEANS_INTG
1101 case K_F21: /* NetBeans command */
1102 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001103 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 --no_mapping;
1105 netbeans_keycommand(i);
1106 break;
1107#endif
1108
1109 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 case NUL:
1111 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 /* For ^@ the trailing ESC will end the insert, unless there is an
1113 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1115 && c != Ctrl_A && !p_im)
1116 goto doESCkey; /* quit insert mode */
1117 inserted_space = FALSE;
1118 break;
1119
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 ins_reg();
1122 auto_format(FALSE, TRUE);
1123 inserted_space = FALSE;
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ins_ctrl_g();
1128 break;
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case Ctrl_HAT: /* switch input mode and/or langmap */
1131 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 break;
1133
1134#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 if (!p_ari)
1137 goto normalchar;
1138 ins_ctrl_();
1139 break;
1140#endif
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1144 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1145 goto docomplete;
1146#endif
1147 /* FALLTHROUGH */
1148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150# ifdef FEAT_INS_EXPAND
1151 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1152 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153 if (has_compl_option(FALSE))
1154 goto docomplete;
1155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157# endif
1158 ins_shift(c, lastc);
1159 auto_format(FALSE, TRUE);
1160 inserted_space = FALSE;
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 case K_KDEL:
1165 ins_del();
1166 auto_format(FALSE, TRUE);
1167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 case Ctrl_H:
1171 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1172 auto_format(FALSE, TRUE);
1173 break;
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001176#ifdef FEAT_JOB_CHANNEL
1177 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1178 {
1179 // In a prompt window CTRL-W is used for window commands.
1180 // Use Shift-CTRL-W to delete a word.
1181 stuffcharReadbuff(Ctrl_W);
1182 restart_edit = 'i';
1183 nomove = TRUE;
1184 count = 0;
1185 goto doESCkey;
1186 }
1187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1189 auto_format(FALSE, TRUE);
1190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001193# ifdef FEAT_COMPL_FUNC
1194 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001195 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001196 goto docomplete;
1197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1199 auto_format(FALSE, TRUE);
1200 inserted_space = FALSE;
1201 break;
1202
1203#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 case K_LEFTMOUSE_NM:
1206 case K_LEFTDRAG:
1207 case K_LEFTRELEASE:
1208 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001209 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 case K_MIDDLEMOUSE:
1211 case K_MIDDLEDRAG:
1212 case K_MIDDLERELEASE:
1213 case K_RIGHTMOUSE:
1214 case K_RIGHTDRAG:
1215 case K_RIGHTRELEASE:
1216 case K_X1MOUSE:
1217 case K_X1DRAG:
1218 case K_X1RELEASE:
1219 case K_X2MOUSE:
1220 case K_X2DRAG:
1221 case K_X2RELEASE:
1222 ins_mouse(c);
1223 break;
1224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001226 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 break;
1228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001230 ins_mousescroll(MSCR_UP);
1231 break;
1232
1233 case K_MOUSELEFT: /* Scroll wheel left */
1234 ins_mousescroll(MSCR_LEFT);
1235 break;
1236
1237 case K_MOUSERIGHT: /* Scroll wheel right */
1238 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 break;
1240#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001241 case K_PS:
1242 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1243 if (cmdchar == K_PS)
1244 /* invoked from normal mode, bail out */
1245 goto doESCkey;
1246 break;
1247 case K_PE:
1248 /* Got K_PE without K_PS, ignore. */
1249 break;
1250
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001251#ifdef FEAT_GUI_TABLINE
1252 case K_TABLINE:
1253 case K_TABMENU:
1254 ins_tabline(c);
1255 break;
1256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 break;
1260
Bram Moolenaar754b5602006-02-09 23:53:20 +00001261 case K_CURSORHOLD: /* Didn't type something for a while. */
1262 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1263 did_cursorhold = TRUE;
1264 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001265
Bram Moolenaar4770d092006-01-12 23:22:24 +00001266#ifdef FEAT_GUI_W32
1267 /* On Win32 ignore <M-F4>, we get it when closing the window was
1268 * cancelled. */
1269 case K_F4:
1270 if (mod_mask != MOD_MASK_ALT)
1271 goto normalchar;
1272 break;
1273#endif
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#ifdef FEAT_GUI
1276 case K_VER_SCROLLBAR:
1277 ins_scroll();
1278 break;
1279
1280 case K_HOR_SCROLLBAR:
1281 ins_horscroll();
1282 break;
1283#endif
1284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 case K_S_HOME:
1288 case K_C_HOME:
1289 ins_home(c);
1290 break;
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 case K_S_END:
1295 case K_C_END:
1296 ins_end(c);
1297 break;
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001300 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1301 ins_s_left();
1302 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001303 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 break;
1305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001306 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 case K_C_LEFT:
1308 ins_s_left();
1309 break;
1310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001311 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001312 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1313 ins_s_right();
1314 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001315 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 break;
1317
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001318 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 case K_C_RIGHT:
1320 ins_s_right();
1321 break;
1322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001323 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001324#ifdef FEAT_INS_EXPAND
1325 if (pum_visible())
1326 goto docomplete;
1327#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001328 if (mod_mask & MOD_MASK_SHIFT)
1329 ins_pageup();
1330 else
1331 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 case K_PAGEUP:
1336 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001337#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001338 if (pum_visible())
1339 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ins_pageup();
1342 break;
1343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001345#ifdef FEAT_INS_EXPAND
1346 if (pum_visible())
1347 goto docomplete;
1348#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001349 if (mod_mask & MOD_MASK_SHIFT)
1350 ins_pagedown();
1351 else
1352 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 break;
1354
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001355 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 case K_PAGEDOWN:
1357 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001358#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001359 if (pum_visible())
1360 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 ins_pagedown();
1363 break;
1364
1365#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001366 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ins_drop();
1368 break;
1369#endif
1370
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001371 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 c = TAB;
1373 /* FALLTHROUGH */
1374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001375 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1377 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1378 goto docomplete;
1379#endif
1380 inserted_space = FALSE;
1381 if (ins_tab())
1382 goto normalchar; /* insert TAB as a normal char */
1383 auto_format(FALSE, TRUE);
1384 break;
1385
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001386 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 c = CAR;
1388 /* FALLTHROUGH */
1389 case CAR:
1390 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001391#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 /* In a quickfix window a <CR> jumps to the error under the
1393 * cursor. */
1394 if (bt_quickfix(curbuf) && c == CAR)
1395 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001396 if (curwin->w_llist_ref == NULL) /* quickfix window */
1397 do_cmdline_cmd((char_u *)".cc");
1398 else /* location list window */
1399 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 break;
1401 }
1402#endif
1403#ifdef FEAT_CMDWIN
1404 if (cmdwin_type != 0)
1405 {
1406 /* Execute the command in the cmdline window. */
1407 cmdwin_result = CAR;
1408 goto doESCkey;
1409 }
1410#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001411#ifdef FEAT_JOB_CHANNEL
1412 if (bt_prompt(curbuf))
1413 {
1414 buf_T *buf = curbuf;
1415
1416 invoke_prompt_callback();
1417 if (curbuf != buf)
1418 // buffer changed, get out of Insert mode
1419 goto doESCkey;
1420 break;
1421 }
1422#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001423 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 goto doESCkey; /* out of memory */
1425 auto_format(FALSE, FALSE);
1426 inserted_space = FALSE;
1427 break;
1428
Bram Moolenaar860cae12010-06-05 23:22:07 +02001429#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001430 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431# ifdef FEAT_INS_EXPAND
1432 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1433 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001434 if (has_compl_option(TRUE))
1435 goto docomplete;
1436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
1438# endif
1439# ifdef FEAT_DIGRAPHS
1440 c = ins_digraph();
1441 if (c == NUL)
1442 break;
1443# endif
1444 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
1447#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001448 case Ctrl_X: /* Enter CTRL-X mode */
1449 ins_ctrl_x();
1450 break;
1451
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001452 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (ctrl_x_mode != CTRL_X_TAGS)
1454 goto normalchar;
1455 goto docomplete;
1456
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001457 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (ctrl_x_mode != CTRL_X_FILES)
1459 goto normalchar;
1460 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001461
1462 case 's': /* Spelling completion after ^X */
1463 case Ctrl_S:
1464 if (ctrl_x_mode != CTRL_X_SPELL)
1465 goto normalchar;
1466 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467#endif
1468
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001469 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#ifdef FEAT_INS_EXPAND
1471 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1472#endif
1473 {
1474 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1475 if (p_im)
1476 {
1477 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1478 break;
1479 goto doESCkey;
1480 }
1481 goto normalchar;
1482 }
1483#ifdef FEAT_INS_EXPAND
1484 /* FALLTHROUGH */
1485
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001486 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 case Ctrl_N:
1488 /* if 'complete' is empty then plain ^P is no longer special,
1489 * but it is under other ^X modes */
1490 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001491 && (ctrl_x_mode == CTRL_X_NORMAL
1492 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001493 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 goto normalchar;
1495
1496docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001497 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001498#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001499 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001500#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001501 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001502 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001503#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001504 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001505#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001506 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 break;
1508#endif /* FEAT_INS_EXPAND */
1509
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001510 case Ctrl_Y: /* copy from previous line or scroll down */
1511 case Ctrl_E: /* copy from next line or scroll up */
1512 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 break;
1514
1515 default:
1516#ifdef UNIX
1517 if (c == intr_char) /* special interrupt char */
1518 goto do_intr;
1519#endif
1520
Bram Moolenaare659c952011-05-19 17:25:41 +02001521normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001523 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001525#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001526 if (!p_paste)
1527 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001528 /* Trigger InsertCharPre. */
1529 char_u *str = do_insert_char_pre(c);
1530 char_u *p;
1531
1532 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001533 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001534 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001535 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001536 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001537 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001538 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001539 c = PTR2CHAR(p);
1540 if (c == CAR || c == K_KENTER || c == NL)
1541 ins_eol(c);
1542 else
1543 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001544 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001545 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001546 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001547 vim_free(str);
1548 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001549 }
1550
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001551 /* If the new value is already inserted or an empty string
1552 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001553 if (c == NUL)
1554 break;
1555 }
1556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557#ifdef FEAT_SMARTINDENT
1558 /* Try to perform smart-indenting. */
1559 ins_try_si(c);
1560#endif
1561
1562 if (c == ' ')
1563 {
1564 inserted_space = TRUE;
1565#ifdef FEAT_CINDENT
1566 if (inindent(0))
1567 can_cindent = FALSE;
1568#endif
1569 if (Insstart_blank_vcol == MAXCOL
1570 && curwin->w_cursor.lnum == Insstart.lnum)
1571 Insstart_blank_vcol = get_nolist_virtcol();
1572 }
1573
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001574 /* Insert a normal character and check for abbreviations on a
1575 * special character. Let CTRL-] expand abbreviations without
1576 * inserting it. */
1577 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578#ifdef FEAT_MBYTE
1579 /* Add ABBR_OFF for characters above 0x100, this is
1580 * what check_abbr() expects. */
1581 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1582#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001583 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 insert_special(c, FALSE, FALSE);
1586#ifdef FEAT_RIGHTLEFT
1587 revins_legal++;
1588 revins_chars++;
1589#endif
1590 }
1591
1592 auto_format(FALSE, TRUE);
1593
1594#ifdef FEAT_FOLDING
1595 /* When inserting a character the cursor line must never be in a
1596 * closed fold. */
1597 foldOpenCursor();
1598#endif
1599 break;
1600 } /* end of switch (c) */
1601
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001602 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001603 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001604#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001605 /* but not in CTRL-X mode, a script can't restore the state */
1606 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001607#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001608 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001609 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 /* If the cursor was moved we didn't just insert a space */
1612 if (arrow_used)
1613 inserted_space = FALSE;
1614
1615#ifdef FEAT_CINDENT
1616 if (can_cindent && cindent_on()
1617# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001618 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619# endif
1620 )
1621 {
1622force_cindent:
1623 /*
1624 * Indent now if a key was typed that is in 'cinkeys'.
1625 */
1626 if (in_cinkeys(c, ' ', line_is_white))
1627 {
1628 if (stop_arrow() == OK)
1629 /* re-indent the current line */
1630 do_c_expr_indent();
1631 }
1632 }
1633#endif /* FEAT_CINDENT */
1634
1635 } /* for (;;) */
1636 /* NOTREACHED */
1637}
1638
1639/*
1640 * Redraw for Insert mode.
1641 * This is postponed until getting the next character to make '$' in the 'cpo'
1642 * option work correctly.
1643 * Only redraw when there are no characters available. This speeds up
1644 * inserting sequences of characters (e.g., for CTRL-R).
1645 */
1646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001647ins_redraw(
1648 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001650#ifdef FEAT_CONCEAL
1651 linenr_T conceal_old_cursor_line = 0;
1652 linenr_T conceal_new_cursor_line = 0;
1653 int conceal_update_lines = FALSE;
1654#endif
1655
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001656 if (char_avail())
1657 return;
1658
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001659#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001660 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1661 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001662 if (ready && (has_cursormovedI()
1663# if defined(FEAT_CONCEAL)
1664 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001665# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001666 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001667 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001668# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001669 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001670# endif
1671 )
1672 {
1673# ifdef FEAT_SYN_HL
1674 /* Need to update the screen first, to make sure syntax
1675 * highlighting is correct after making a change (e.g., inserting
1676 * a "(". The autocommand may also require a redraw, so it's done
1677 * again below, unfortunately. */
1678 if (syntax_present(curwin) && must_redraw)
1679 update_screen(0);
1680# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001681 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001682 {
1683 /* Make sure curswant is correct, an autocommand may call
1684 * getcurpos(). */
1685 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001686 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001687 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001688# ifdef FEAT_CONCEAL
1689 if (curwin->w_p_cole > 0)
1690 {
1691 conceal_old_cursor_line = last_cursormoved.lnum;
1692 conceal_new_cursor_line = curwin->w_cursor.lnum;
1693 conceal_update_lines = TRUE;
1694 }
1695# endif
1696 last_cursormoved = curwin->w_cursor;
1697 }
1698#endif
1699
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001700 /* Trigger TextChangedI if b_changedtick differs. */
1701 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001702 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001703#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001704 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001705#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001706 )
1707 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001708 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1709 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001711
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001712#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001713 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1714 * TextChangedI will need to trigger for backwards compatibility, thus use
1715 * different b_last_changedtick* variables. */
1716 if (ready && has_textchangedP()
1717 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1718 && pum_visible())
1719 {
1720 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
1721 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1722 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001723#endif
1724
1725 if (must_redraw)
1726 update_screen(0);
1727 else if (clear_cmdline || redraw_cmdline)
1728 showmode(); /* clear cmdline and show mode */
1729# if defined(FEAT_CONCEAL)
1730 if ((conceal_update_lines
1731 && (conceal_old_cursor_line != conceal_new_cursor_line
1732 || conceal_cursor_line(curwin)))
1733 || need_cursor_line_redraw)
1734 {
1735 if (conceal_old_cursor_line != conceal_new_cursor_line)
1736 update_single_line(curwin, conceal_old_cursor_line);
1737 update_single_line(curwin, conceal_new_cursor_line == 0
1738 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1739 curwin->w_valid &= ~VALID_CROW;
1740 }
1741# endif
1742 showruler(FALSE);
1743 setcursor();
1744 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
1747/*
1748 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1749 */
1750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752{
1753 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001754 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001757 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
1759 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001762 did_putchar = TRUE;
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1765
1766#ifdef FEAT_CMDL_INFO
1767 add_to_showcmd_c(Ctrl_V);
1768#endif
1769
1770 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001771 if (did_putchar)
1772 /* when the line fits in 'columns' the '^' is at the start of the next
1773 * line and will not removed by the redraw */
1774 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#ifdef FEAT_CMDL_INFO
1776 clear_showcmd();
1777#endif
1778 insert_special(c, FALSE, TRUE);
1779#ifdef FEAT_RIGHTLEFT
1780 revins_chars++;
1781 revins_legal++;
1782#endif
1783}
1784
1785/*
1786 * Put a character directly onto the screen. It's not stored in a buffer.
1787 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1788 */
1789static int pc_status;
1790#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1791#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1792#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1793#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795static int pc_attr;
1796static int pc_row;
1797static int pc_col;
1798
1799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 int attr;
1803
1804 if (ScreenLines != NULL)
1805 {
1806 update_topline(); /* just in case w_topline isn't valid */
1807 validate_cursor();
1808 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001809 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 else
1811 attr = 0;
1812 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001813 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1815 pc_status = PC_STATUS_UNSET;
1816#endif
1817#ifdef FEAT_RIGHTLEFT
1818 if (curwin->w_p_rl)
1819 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001820 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821# ifdef FEAT_MBYTE
1822 if (has_mbyte)
1823 {
1824 int fix_col = mb_fix_col(pc_col, pc_row);
1825
1826 if (fix_col != pc_col)
1827 {
1828 screen_putchar(' ', pc_row, fix_col, attr);
1829 --curwin->w_wcol;
1830 pc_status = PC_STATUS_RIGHT;
1831 }
1832 }
1833# endif
1834 }
1835 else
1836#endif
1837 {
1838 pc_col += curwin->w_wcol;
1839#ifdef FEAT_MBYTE
1840 if (mb_lefthalve(pc_row, pc_col))
1841 pc_status = PC_STATUS_LEFT;
1842#endif
1843 }
1844
1845 /* save the character to be able to put it back */
1846#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1847 if (pc_status == PC_STATUS_UNSET)
1848#endif
1849 {
1850 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1851 pc_status = PC_STATUS_SET;
1852 }
1853 screen_putchar(c, pc_row, pc_col, attr);
1854 }
1855}
1856
Bram Moolenaarf2732452018-06-03 14:47:35 +02001857#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1858/*
1859 * Return the effective prompt for the current buffer.
1860 */
1861 char_u *
1862prompt_text(void)
1863{
1864 if (curbuf->b_prompt_text == NULL)
1865 return (char_u *)"% ";
1866 return curbuf->b_prompt_text;
1867}
1868
1869/*
1870 * Prepare for prompt mode: Make sure the last line has the prompt text.
1871 * Move the cursor to this line.
1872 */
1873 static void
1874init_prompt(int cmdchar_todo)
1875{
1876 char_u *prompt = prompt_text();
1877 char_u *text;
1878
1879 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1880 text = ml_get_curline();
1881 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1882 {
1883 // prompt is missing, insert it or append a line with it
1884 if (*text == NUL)
1885 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1886 else
1887 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1888 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1889 coladvance((colnr_T)MAXCOL);
1890 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1891 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001892
1893 // Insert always starts after the prompt, allow editing text after it.
1894 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1895 || Insstart_orig.col != (int)STRLEN(prompt))
1896 {
1897 Insstart.lnum = curwin->w_cursor.lnum;
1898 Insstart.col = STRLEN(prompt);
1899 Insstart_orig = Insstart;
1900 Insstart_textlen = Insstart.col;
1901 Insstart_blank_vcol = MAXCOL;
1902 arrow_used = FALSE;
1903 }
1904
Bram Moolenaarf2732452018-06-03 14:47:35 +02001905 if (cmdchar_todo == 'A')
1906 coladvance((colnr_T)MAXCOL);
1907 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
1908 curwin->w_cursor.col = STRLEN(prompt);
1909}
1910
1911/*
1912 * Return TRUE if the cursor is in the editable position of the prompt line.
1913 */
1914 int
1915prompt_curpos_editable()
1916{
1917 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1918 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1919}
1920#endif
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922/*
1923 * Undo the previous edit_putchar().
1924 */
1925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001926edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927{
1928 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1929 {
1930#if defined(FEAT_MBYTE)
1931 if (pc_status == PC_STATUS_RIGHT)
1932 ++curwin->w_wcol;
1933 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1934 redrawWinline(curwin->w_cursor.lnum, FALSE);
1935 else
1936#endif
1937 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1938 }
1939}
1940
1941/*
1942 * Called when p_dollar is set: display a '$' at the end of the changed text
1943 * Only works when cursor is in the line that changes.
1944 */
1945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001946display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
1948 colnr_T save_col;
1949
1950 if (!redrawing())
1951 return;
1952
1953 cursor_off();
1954 save_col = curwin->w_cursor.col;
1955 curwin->w_cursor.col = col;
1956#ifdef FEAT_MBYTE
1957 if (has_mbyte)
1958 {
1959 char_u *p;
1960
1961 /* If on the last byte of a multi-byte move to the first byte. */
1962 p = ml_get_curline();
1963 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1964 }
1965#endif
1966 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001967 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 edit_putchar('$', FALSE);
1970 dollar_vcol = curwin->w_virtcol;
1971 }
1972 curwin->w_cursor.col = save_col;
1973}
1974
1975/*
1976 * Call this function before moving the cursor from the normal insert position
1977 * in insert mode.
1978 */
1979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001980undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001982 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001984 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 redrawWinline(curwin->w_cursor.lnum, FALSE);
1986 }
1987}
1988
1989/*
1990 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1991 * Keep the cursor on the same character.
1992 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1993 * type == INDENT_DEC decrease indent (for CTRL-D)
1994 * type == INDENT_SET set indent to "amount"
1995 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1996 */
1997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001998change_indent(
1999 int type,
2000 int amount,
2001 int round,
2002 int replaced, /* replaced character, put on replace stack */
2003 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 int vcol;
2006 int last_vcol;
2007 int insstart_less; /* reduction for Insstart.col */
2008 int new_cursor_col;
2009 int i;
2010 char_u *ptr;
2011 int save_p_list;
2012 int start_col;
2013 colnr_T vc;
2014#ifdef FEAT_VREPLACE
2015 colnr_T orig_col = 0; /* init for GCC */
2016 char_u *new_line, *orig_line = NULL; /* init for GCC */
2017
2018 /* VREPLACE mode needs to know what the line was like before changing */
2019 if (State & VREPLACE_FLAG)
2020 {
2021 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2022 orig_col = curwin->w_cursor.col;
2023 }
2024#endif
2025
2026 /* for the following tricks we don't want list mode */
2027 save_p_list = curwin->w_p_list;
2028 curwin->w_p_list = FALSE;
2029 vc = getvcol_nolist(&curwin->w_cursor);
2030 vcol = vc;
2031
2032 /*
2033 * For Replace mode we need to fix the replace stack later, which is only
2034 * possible when the cursor is in the indent. Remember the number of
2035 * characters before the cursor if it's possible.
2036 */
2037 start_col = curwin->w_cursor.col;
2038
2039 /* determine offset from first non-blank */
2040 new_cursor_col = curwin->w_cursor.col;
2041 beginline(BL_WHITE);
2042 new_cursor_col -= curwin->w_cursor.col;
2043
2044 insstart_less = curwin->w_cursor.col;
2045
2046 /*
2047 * If the cursor is in the indent, compute how many screen columns the
2048 * cursor is to the left of the first non-blank.
2049 */
2050 if (new_cursor_col < 0)
2051 vcol = get_indent() - vcol;
2052
2053 if (new_cursor_col > 0) /* can't fix replace stack */
2054 start_col = -1;
2055
2056 /*
2057 * Set the new indent. The cursor will be put on the first non-blank.
2058 */
2059 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002060 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 else
2062 {
2063#ifdef FEAT_VREPLACE
2064 int save_State = State;
2065
2066 /* Avoid being called recursively. */
2067 if (State & VREPLACE_FLAG)
2068 State = INSERT;
2069#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002070 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#ifdef FEAT_VREPLACE
2072 State = save_State;
2073#endif
2074 }
2075 insstart_less -= curwin->w_cursor.col;
2076
2077 /*
2078 * Try to put cursor on same character.
2079 * If the cursor is at or after the first non-blank in the line,
2080 * compute the cursor column relative to the column of the first
2081 * non-blank character.
2082 * If we are not in insert mode, leave the cursor on the first non-blank.
2083 * If the cursor is before the first non-blank, position it relative
2084 * to the first non-blank, counted in screen columns.
2085 */
2086 if (new_cursor_col >= 0)
2087 {
2088 /*
2089 * When changing the indent while the cursor is touching it, reset
2090 * Insstart_col to 0.
2091 */
2092 if (new_cursor_col == 0)
2093 insstart_less = MAXCOL;
2094 new_cursor_col += curwin->w_cursor.col;
2095 }
2096 else if (!(State & INSERT))
2097 new_cursor_col = curwin->w_cursor.col;
2098 else
2099 {
2100 /*
2101 * Compute the screen column where the cursor should be.
2102 */
2103 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002104 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 /*
2107 * Advance the cursor until we reach the right screen column.
2108 */
2109 vcol = last_vcol = 0;
2110 new_cursor_col = -1;
2111 ptr = ml_get_curline();
2112 while (vcol <= (int)curwin->w_virtcol)
2113 {
2114 last_vcol = vcol;
2115#ifdef FEAT_MBYTE
2116 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002117 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 else
2119#endif
2120 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002121 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123 vcol = last_vcol;
2124
2125 /*
2126 * May need to insert spaces to be able to position the cursor on
2127 * the right screen column.
2128 */
2129 if (vcol != (int)curwin->w_virtcol)
2130 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002131 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002133 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (ptr != NULL)
2135 {
2136 new_cursor_col += i;
2137 ptr[i] = NUL;
2138 while (--i >= 0)
2139 ptr[i] = ' ';
2140 ins_str(ptr);
2141 vim_free(ptr);
2142 }
2143 }
2144
2145 /*
2146 * When changing the indent while the cursor is in it, reset
2147 * Insstart_col to 0.
2148 */
2149 insstart_less = MAXCOL;
2150 }
2151
2152 curwin->w_p_list = save_p_list;
2153
2154 if (new_cursor_col <= 0)
2155 curwin->w_cursor.col = 0;
2156 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002157 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 curwin->w_set_curswant = TRUE;
2159 changed_cline_bef_curs();
2160
2161 /*
2162 * May have to adjust the start of the insert.
2163 */
2164 if (State & INSERT)
2165 {
2166 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2167 {
2168 if ((int)Insstart.col <= insstart_less)
2169 Insstart.col = 0;
2170 else
2171 Insstart.col -= insstart_less;
2172 }
2173 if ((int)ai_col <= insstart_less)
2174 ai_col = 0;
2175 else
2176 ai_col -= insstart_less;
2177 }
2178
2179 /*
2180 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2181 * If the number of characters before the cursor decreased, need to pop a
2182 * few characters from the replace stack.
2183 * If the number of characters before the cursor increased, need to push a
2184 * few NULs onto the replace stack.
2185 */
2186 if (REPLACE_NORMAL(State) && start_col >= 0)
2187 {
2188 while (start_col > (int)curwin->w_cursor.col)
2189 {
2190 replace_join(0); /* remove a NUL from the replace stack */
2191 --start_col;
2192 }
2193 while (start_col < (int)curwin->w_cursor.col || replaced)
2194 {
2195 replace_push(NUL);
2196 if (replaced)
2197 {
2198 replace_push(replaced);
2199 replaced = NUL;
2200 }
2201 ++start_col;
2202 }
2203 }
2204
2205#ifdef FEAT_VREPLACE
2206 /*
2207 * For VREPLACE mode, we also have to fix the replace stack. In this case
2208 * it is always possible because we backspace over the whole line and then
2209 * put it back again the way we wanted it.
2210 */
2211 if (State & VREPLACE_FLAG)
2212 {
2213 /* If orig_line didn't allocate, just return. At least we did the job,
2214 * even if you can't backspace. */
2215 if (orig_line == NULL)
2216 return;
2217
2218 /* Save new line */
2219 new_line = vim_strsave(ml_get_curline());
2220 if (new_line == NULL)
2221 return;
2222
2223 /* We only put back the new line up to the cursor */
2224 new_line[curwin->w_cursor.col] = NUL;
2225
2226 /* Put back original line */
2227 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2228 curwin->w_cursor.col = orig_col;
2229
2230 /* Backspace from cursor to start of line */
2231 backspace_until_column(0);
2232
2233 /* Insert new stuff into line again */
2234 ins_bytes(new_line);
2235
2236 vim_free(new_line);
2237 }
2238#endif
2239}
2240
2241/*
2242 * Truncate the space at the end of a line. This is to be used only in an
2243 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2244 * modes.
2245 */
2246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002247truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
2249 int i;
2250
2251 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002252 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 if (State & REPLACE_FLAG)
2255 replace_join(0); /* remove a NUL from the replace stack */
2256 }
2257 line[i + 1] = NUL;
2258}
2259
2260#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2261 || defined(FEAT_COMMENTS) || defined(PROTO)
2262/*
2263 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2264 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002265 * Will attempt not to go before "col" even when there is a composing
2266 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
2268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002269backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
2271 while ((int)curwin->w_cursor.col > col)
2272 {
2273 curwin->w_cursor.col--;
2274 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002275 replace_do_bs(col);
2276 else if (!del_char_after_col(col))
2277 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279}
2280#endif
2281
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002282/*
2283 * Like del_char(), but make sure not to go before column "limit_col".
2284 * Only matters when there are composing characters.
2285 * Return TRUE when something was deleted.
2286 */
2287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002288del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002289{
2290#ifdef FEAT_MBYTE
2291 if (enc_utf8 && limit_col >= 0)
2292 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002293 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002294
2295 /* Make sure the cursor is at the start of a character, but
2296 * skip forward again when going too far back because of a
2297 * composing character. */
2298 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002299 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002300 {
2301 int l = utf_ptr2len(ml_get_cursor());
2302
2303 if (l == 0) /* end of line */
2304 break;
2305 curwin->w_cursor.col += l;
2306 }
2307 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2308 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002309 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002310 }
2311 else
2312#endif
2313 (void)del_char(FALSE);
2314 return TRUE;
2315}
2316
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2318/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002319 * CTRL-X pressed in Insert mode.
2320 */
2321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002322ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002323{
2324 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2325 * CTRL-V works like CTRL-N */
2326 if (ctrl_x_mode != CTRL_X_CMDLINE)
2327 {
2328 /* if the next ^X<> won't ADD nothing, then reset
2329 * compl_cont_status */
2330 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002331 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002332 else
2333 compl_cont_status = 0;
2334 /* We're not sure which CTRL-X mode it will be yet */
2335 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2336 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2337 edit_submode_pre = NULL;
2338 showmode();
2339 }
2340}
2341
2342/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002343 * Whether other than default completion has been selected.
2344 */
2345 int
2346ctrl_x_mode_not_default(void)
2347{
2348 return ctrl_x_mode != CTRL_X_NORMAL;
2349}
2350
2351/*
2352 * Whether CTRL-X was typed without a following character.
2353 */
2354 int
2355ctrl_x_mode_not_defined_yet(void)
2356{
2357 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2358}
2359
2360/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002361 * Return TRUE if the 'dict' or 'tsr' option can be used.
2362 */
2363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002364has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002365{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002366 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002367# ifdef FEAT_SPELL
2368 && !curwin->w_p_spell
2369# endif
2370 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002371 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2372 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002373 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002374 edit_submode = NULL;
2375 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2376 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002377 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002378 if (emsg_silent == 0)
2379 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002380 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002381 setcursor();
2382 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002383#ifdef FEAT_EVAL
2384 if (!get_vim_var_nr(VV_TESTING))
2385#endif
2386 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002387 }
2388 return FALSE;
2389 }
2390 return TRUE;
2391}
2392
2393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2395 * This depends on the current mode.
2396 */
2397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002398vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400 /* Always allow ^R - let it's results then be checked */
2401 if (c == Ctrl_R)
2402 return TRUE;
2403
Bram Moolenaare3226be2005-12-18 22:10:00 +00002404 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002405 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002406 return TRUE;
2407
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 switch (ctrl_x_mode)
2409 {
2410 case 0: /* Not in any CTRL-X mode */
2411 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2412 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002413 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2415 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2416 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002417 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002418 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CTRL_X_SCROLL:
2420 return (c == Ctrl_Y || c == Ctrl_E);
2421 case CTRL_X_WHOLE_LINE:
2422 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2423 case CTRL_X_FILES:
2424 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2425 case CTRL_X_DICTIONARY:
2426 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2427 case CTRL_X_THESAURUS:
2428 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2429 case CTRL_X_TAGS:
2430 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2431#ifdef FEAT_FIND_ID
2432 case CTRL_X_PATH_PATTERNS:
2433 return (c == Ctrl_P || c == Ctrl_N);
2434 case CTRL_X_PATH_DEFINES:
2435 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2436#endif
2437 case CTRL_X_CMDLINE:
2438 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2439 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002440#ifdef FEAT_COMPL_FUNC
2441 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002442 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002443 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002444 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002445#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002446 case CTRL_X_SPELL:
2447 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002448 case CTRL_X_EVAL:
2449 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002451 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 return FALSE;
2453}
2454
2455/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002456 * Return TRUE when character "c" is part of the item currently being
2457 * completed. Used to decide whether to abandon complete mode when the menu
2458 * is visible.
2459 */
2460 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002461ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002462{
2463 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2464 /* When expanding an identifier only accept identifier chars. */
2465 return vim_isIDc(c);
2466
2467 switch (ctrl_x_mode)
2468 {
2469 case CTRL_X_FILES:
2470 /* When expanding file name only accept file name chars. But not
2471 * path separators, so that "proto/<Tab>" expands files in
2472 * "proto", not "proto/" as a whole */
2473 return vim_isfilec(c) && !vim_ispathsep(c);
2474
2475 case CTRL_X_CMDLINE:
2476 case CTRL_X_OMNI:
2477 /* Command line and Omni completion can work with just about any
2478 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002479 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002480
2481 case CTRL_X_WHOLE_LINE:
2482 /* For while line completion a space can be part of the line. */
2483 return vim_isprintc(c);
2484 }
2485 return vim_iswordc(c);
2486}
2487
2488/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002489 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002491 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 * the rest of the word to be in -- webb
2493 */
2494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002495ins_compl_add_infercase(
2496 char_u *str,
2497 int len,
2498 int icase,
2499 char_u *fname,
2500 int dir,
2501 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002503 char_u *p;
2504 int i, c;
2505 int actual_len; /* Take multi-byte characters */
2506 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002507 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002508 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 int has_lower = FALSE;
2510 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002512 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002514 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
Bram Moolenaara2993e12007-08-12 14:38:46 +00002516 /* Find actual length of completion. */
2517#ifdef FEAT_MBYTE
2518 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002520 p = str;
2521 actual_len = 0;
2522 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002524 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002525 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
2527 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002528 else
2529#endif
2530 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
Bram Moolenaara2993e12007-08-12 14:38:46 +00002532 /* Find actual length of original text. */
2533#ifdef FEAT_MBYTE
2534 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002536 p = compl_orig_text;
2537 actual_compl_length = 0;
2538 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002540 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002541 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002544 else
2545#endif
2546 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002548 /* "actual_len" may be smaller than "actual_compl_length" when using
2549 * thesaurus, only use the minimum when comparing. */
2550 min_len = actual_len < actual_compl_length
2551 ? actual_len : actual_compl_length;
2552
Bram Moolenaara2993e12007-08-12 14:38:46 +00002553 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002554 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002555 if (wca != NULL)
2556 {
2557 p = str;
2558 for (i = 0; i < actual_len; ++i)
2559#ifdef FEAT_MBYTE
2560 if (has_mbyte)
2561 wca[i] = mb_ptr2char_adv(&p);
2562 else
2563#endif
2564 wca[i] = *(p++);
2565
2566 /* Rule 1: Were any chars converted to lower? */
2567 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002568 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002569 {
2570#ifdef FEAT_MBYTE
2571 if (has_mbyte)
2572 c = mb_ptr2char_adv(&p);
2573 else
2574#endif
2575 c = *(p++);
2576 if (MB_ISLOWER(c))
2577 {
2578 has_lower = TRUE;
2579 if (MB_ISUPPER(wca[i]))
2580 {
2581 /* Rule 1 is satisfied. */
2582 for (i = actual_compl_length; i < actual_len; ++i)
2583 wca[i] = MB_TOLOWER(wca[i]);
2584 break;
2585 }
2586 }
2587 }
2588
2589 /*
2590 * Rule 2: No lower case, 2nd consecutive letter converted to
2591 * upper case.
2592 */
2593 if (!has_lower)
2594 {
2595 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002596 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002597 {
2598#ifdef FEAT_MBYTE
2599 if (has_mbyte)
2600 c = mb_ptr2char_adv(&p);
2601 else
2602#endif
2603 c = *(p++);
2604 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2605 {
2606 /* Rule 2 is satisfied. */
2607 for (i = actual_compl_length; i < actual_len; ++i)
2608 wca[i] = MB_TOUPPER(wca[i]);
2609 break;
2610 }
2611 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2612 }
2613 }
2614
2615 /* Copy the original case of the part we typed. */
2616 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002617 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002618 {
2619#ifdef FEAT_MBYTE
2620 if (has_mbyte)
2621 c = mb_ptr2char_adv(&p);
2622 else
2623#endif
2624 c = *(p++);
2625 if (MB_ISLOWER(c))
2626 wca[i] = MB_TOLOWER(wca[i]);
2627 else if (MB_ISUPPER(c))
2628 wca[i] = MB_TOUPPER(wca[i]);
2629 }
2630
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002631 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002632 * Generate encoding specific output from wide character array.
2633 * Multi-byte characters can occupy up to five bytes more than
2634 * ASCII characters, and we also need one byte for NUL, so stay
2635 * six bytes away from the edge of IObuff.
2636 */
2637 p = IObuff;
2638 i = 0;
2639 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2640#ifdef FEAT_MBYTE
2641 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002642 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002643 else
2644#endif
2645 *(p++) = wca[i++];
2646 *p = NUL;
2647
2648 vim_free(wca);
2649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002651 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2652 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002654 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655}
2656
2657/*
2658 * Add a match to the list of matches.
2659 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002660 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002661 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002663 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002664ins_compl_add(
2665 char_u *str,
2666 int len,
2667 int icase,
2668 char_u *fname,
2669 char_u **cptext, /* extra text for popup menu or NULL */
2670 int cdir,
2671 int flags,
2672 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002674 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002675 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677 ui_breakcheck();
2678 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002679 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (len < 0)
2681 len = (int)STRLEN(str);
2682
2683 /*
2684 * If the same match is already present, don't add it.
2685 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002686 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002688 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 do
2690 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002691 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002692 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002693 && match->cp_str[len] == NUL)
2694 return NOTDONE;
2695 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002696 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002699 /* Remove any popup menu before changing the list of matches. */
2700 ins_compl_del_pum();
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 /*
2703 * Allocate a new match structure.
2704 * Copy the values to the new match structure.
2705 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002706 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002708 return FAIL;
2709 match->cp_number = -1;
2710 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002711 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002712 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
2714 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002715 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002717 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002718
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002720 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2722 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002723 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002724 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002725 && compl_curr_match->cp_fname != NULL
2726 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002727 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002728 else if (fname != NULL)
2729 {
2730 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002731 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002734 match->cp_fname = NULL;
2735 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002736
2737 if (cptext != NULL)
2738 {
2739 int i;
2740
2741 for (i = 0; i < CPT_COUNT; ++i)
2742 if (cptext[i] != NULL && *cptext[i] != NUL)
2743 match->cp_text[i] = vim_strsave(cptext[i]);
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 /*
2747 * Link the new match structure in the list of matches.
2748 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002749 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002750 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 else if (dir == FORWARD)
2752 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002753 match->cp_next = compl_curr_match->cp_next;
2754 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756 else /* BACKWARD */
2757 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002758 match->cp_next = compl_curr_match;
2759 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002761 if (match->cp_next)
2762 match->cp_next->cp_prev = match;
2763 if (match->cp_prev)
2764 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002766 compl_first_match = match;
2767 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002769 /*
2770 * Find the longest common string if still doing that.
2771 */
2772 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2773 ins_compl_longest_match(match);
2774
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 return OK;
2776}
2777
2778/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002779 * Return TRUE if "str[len]" matches with match->cp_str, considering
2780 * match->cp_icase.
2781 */
2782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002783ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002784{
2785 if (match->cp_icase)
2786 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2787 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2788}
2789
2790/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002791 * Reduce the longest common string for match "match".
2792 */
2793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002795{
2796 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002797 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002798 int had_match;
2799
2800 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002801 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002802 /* First match, use it as a whole. */
2803 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002804 if (compl_leader != NULL)
2805 {
2806 had_match = (curwin->w_cursor.col > compl_col);
2807 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002808 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002809 ins_redraw(FALSE);
2810
2811 /* When the match isn't there (to avoid matching itself) remove it
2812 * again after redrawing. */
2813 if (!had_match)
2814 ins_compl_delete();
2815 compl_used_match = FALSE;
2816 }
2817 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002818 else
2819 {
2820 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002821 p = compl_leader;
2822 s = match->cp_str;
2823 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002824 {
2825#ifdef FEAT_MBYTE
2826 if (has_mbyte)
2827 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002828 c1 = mb_ptr2char(p);
2829 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002830 }
2831 else
2832#endif
2833 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002834 c1 = *p;
2835 c2 = *s;
2836 }
2837 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2838 : (c1 != c2))
2839 break;
2840#ifdef FEAT_MBYTE
2841 if (has_mbyte)
2842 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002843 MB_PTR_ADV(p);
2844 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002845 }
2846 else
2847#endif
2848 {
2849 ++p;
2850 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002851 }
2852 }
2853
2854 if (*p != NUL)
2855 {
2856 /* Leader was shortened, need to change the inserted text. */
2857 *p = NUL;
2858 had_match = (curwin->w_cursor.col > compl_col);
2859 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002860 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002861 ins_redraw(FALSE);
2862
2863 /* When the match isn't there (to avoid matching itself) remove it
2864 * again after redrawing. */
2865 if (!had_match)
2866 ins_compl_delete();
2867 }
2868
2869 compl_used_match = FALSE;
2870 }
2871}
2872
2873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 * Add an array of matches to the list of matches.
2875 * Frees matches[].
2876 */
2877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002878ins_compl_add_matches(
2879 int num_matches,
2880 char_u **matches,
2881 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 int i;
2884 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002885 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
Bram Moolenaar572cb562005-08-05 21:35:02 +00002887 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002888 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002889 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002891 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 FreeWild(num_matches, matches);
2893}
2894
2895/* Make the completion list cyclic.
2896 * Return the number of matches (excluding the original).
2897 */
2898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002899ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002901 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 int count = 0;
2903
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002904 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
2906 /*
2907 * Find the end of the list.
2908 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002909 match = compl_first_match;
2910 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002911 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002913 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 ++count;
2915 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002916 match->cp_next = compl_first_match;
2917 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
2919 return count;
2920}
2921
Bram Moolenaara94bc432006-03-10 21:42:59 +00002922/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002923 * Set variables that store noselect and noinsert behavior from the
2924 * 'completeopt' value.
2925 */
2926 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002927completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002928{
2929 compl_no_insert = FALSE;
2930 compl_no_select = FALSE;
2931 if (strstr((char *)p_cot, "noselect") != NULL)
2932 compl_no_select = TRUE;
2933 if (strstr((char *)p_cot, "noinsert") != NULL)
2934 compl_no_insert = TRUE;
2935}
2936
2937/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002938 * Start completion for the complete() function.
2939 * "startcol" is where the matched text starts (1 is first column).
2940 * "list" is the list of matches.
2941 */
2942 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002943set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002944{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002945 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002946 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002947
Bram Moolenaara94bc432006-03-10 21:42:59 +00002948 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002949 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002950 ins_compl_prep(' ');
2951 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002952 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002953
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002954 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002955 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002956 startcol = curwin->w_cursor.col;
2957 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002958 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002959 /* compl_pattern doesn't need to be set */
2960 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2961 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002962 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002963 return;
2964
Bram Moolenaare4214502015-03-05 18:08:43 +01002965 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002966
2967 ins_compl_add_list(list);
2968 compl_matches = ins_compl_make_cyclic();
2969 compl_started = TRUE;
2970 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002971 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002972
2973 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002974 if (compl_no_insert || compl_no_select)
2975 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002976 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002977 if (compl_no_select)
2978 /* Down/Up has no real effect. */
2979 ins_complete(K_UP, FALSE);
2980 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002981 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002982 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002983 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002984
2985 /* Lazily show the popup menu, unless we got interrupted. */
2986 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002987 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002988 out_flush();
2989}
2990
2991
Bram Moolenaar9372a112005-12-06 19:59:18 +00002992/* "compl_match_array" points the currently displayed list of entries in the
2993 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002994static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002995static int compl_match_arraysize;
2996
2997/*
2998 * Update the screen and when there is any scrolling remove the popup menu.
2999 */
3000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003001ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003002{
3003 int h;
3004
3005 if (compl_match_array != NULL)
3006 {
3007 h = curwin->w_cline_height;
3008 update_screen(0);
3009 if (h != curwin->w_cline_height)
3010 ins_compl_del_pum();
3011 }
3012}
3013
3014/*
3015 * Remove any popup menu.
3016 */
3017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003018ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003019{
3020 if (compl_match_array != NULL)
3021 {
3022 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003023 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003024 }
3025}
3026
3027/*
3028 * Return TRUE if the popup menu should be displayed.
3029 */
3030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003031pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003032{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003033 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003034 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003035 return FALSE;
3036
3037 /* The display looks bad on a B&W display. */
3038 if (t_colors < 8
3039#ifdef FEAT_GUI
3040 && !gui.in_use
3041#endif
3042 )
3043 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003044 return TRUE;
3045}
3046
3047/*
3048 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003049 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003050 */
3051 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003052pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003053{
3054 compl_T *compl;
3055 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056
3057 /* Don't display the popup menu if there are no matches or there is only
3058 * one (ignoring the original text). */
3059 compl = compl_first_match;
3060 i = 0;
3061 do
3062 {
3063 if (compl == NULL
3064 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3065 break;
3066 compl = compl->cp_next;
3067 } while (compl != compl_first_match);
3068
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003069 if (strstr((char *)p_cot, "menuone") != NULL)
3070 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003071 return (i >= 2);
3072}
3073
3074/*
3075 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003076 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003077 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003080{
3081 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003082 compl_T *shown_compl = NULL;
3083 int did_find_shown_match = FALSE;
3084 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003085 int i;
3086 int cur = -1;
3087 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003088 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003089
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003090 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003091 return;
3092
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003093#if defined(FEAT_EVAL)
3094 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3095 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3096#endif
3097
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003098 /* Update the screen before drawing the popup menu over it. */
3099 update_screen(0);
3100
3101 if (compl_match_array == NULL)
3102 {
3103 /* Need to build the popup menu list. */
3104 compl_match_arraysize = 0;
3105 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003106 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003107 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003108 do
3109 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003110 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3111 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003112 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003113 ++compl_match_arraysize;
3114 compl = compl->cp_next;
3115 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003116 if (compl_match_arraysize == 0)
3117 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003118 compl_match_array = (pumitem_T *)alloc_clear(
3119 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003120 * compl_match_arraysize));
3121 if (compl_match_array != NULL)
3122 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003123 /* If the current match is the original text don't find the first
3124 * match after it, don't highlight anything. */
3125 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3126 shown_match_ok = TRUE;
3127
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003128 i = 0;
3129 compl = compl_first_match;
3130 do
3131 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003132 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3133 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003134 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003135 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003136 if (!shown_match_ok)
3137 {
3138 if (compl == compl_shown_match || did_find_shown_match)
3139 {
3140 /* This item is the shown match or this is the
3141 * first displayed item after the shown match. */
3142 compl_shown_match = compl;
3143 did_find_shown_match = TRUE;
3144 shown_match_ok = TRUE;
3145 }
3146 else
3147 /* Remember this displayed match for when the
3148 * shown match is just below it. */
3149 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003150 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003151 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003152
3153 if (compl->cp_text[CPT_ABBR] != NULL)
3154 compl_match_array[i].pum_text =
3155 compl->cp_text[CPT_ABBR];
3156 else
3157 compl_match_array[i].pum_text = compl->cp_str;
3158 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3159 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3160 if (compl->cp_text[CPT_MENU] != NULL)
3161 compl_match_array[i++].pum_extra =
3162 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003163 else
3164 compl_match_array[i++].pum_extra = compl->cp_fname;
3165 }
3166
3167 if (compl == compl_shown_match)
3168 {
3169 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003170
3171 /* When the original text is the shown match don't set
3172 * compl_shown_match. */
3173 if (compl->cp_flags & ORIGINAL_TEXT)
3174 shown_match_ok = TRUE;
3175
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003176 if (!shown_match_ok && shown_compl != NULL)
3177 {
3178 /* The shown match isn't displayed, set it to the
3179 * previously displayed match. */
3180 compl_shown_match = shown_compl;
3181 shown_match_ok = TRUE;
3182 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003183 }
3184 compl = compl->cp_next;
3185 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003186
3187 if (!shown_match_ok) /* no displayed match at all */
3188 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003189 }
3190 }
3191 else
3192 {
3193 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003194 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003195 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3196 || compl_match_array[i].pum_text
3197 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003198 {
3199 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003200 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003201 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003202 }
3203
3204 if (compl_match_array != NULL)
3205 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003206 /* In Replace mode when a $ is displayed at the end of the line only
3207 * part of the screen would be updated. We do need to redraw here. */
3208 dollar_vcol = -1;
3209
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003210 /* Compute the screen column of the start of the completed text.
3211 * Use the cursor to get all wrapping and other settings right. */
3212 col = curwin->w_cursor.col;
3213 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003214 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003215 curwin->w_cursor.col = col;
3216 }
3217}
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#define DICT_FIRST (1) /* use just first element in "dict" */
3220#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003223 * Add any identifiers that match the given pattern in the list of dictionary
3224 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 */
3226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227ins_compl_dictionaries(
3228 char_u *dict_start,
3229 char_u *pat,
3230 int flags, /* DICT_FIRST and/or DICT_EXACT */
3231 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003233 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 char_u *ptr;
3235 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 char_u **files;
3238 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003240 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
Bram Moolenaar0b238792006-03-02 22:49:12 +00003242 if (*dict == NUL)
3243 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003244#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003245 /* When 'dictionary' is empty and spell checking is enabled use
3246 * "spell". */
3247 if (!thesaurus && curwin->w_p_spell)
3248 dict = (char_u *)"spell";
3249 else
3250#endif
3251 return;
3252 }
3253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003255 if (buf == NULL)
3256 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003257 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 /* If 'infercase' is set, don't use 'smartcase' here */
3260 save_p_scs = p_scs;
3261 if (curbuf->b_p_inf)
3262 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263
3264 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3265 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003266 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003267 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003268 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003269 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003270 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003271
3272 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003273 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003274 len = STRLEN(pat_esc) + 10;
3275 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003276 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003277 {
3278 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003279 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003280 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003281 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003282 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3283 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003284 vim_free(ptr);
3285 }
3286 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003287 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003288 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003289 if (regmatch.regprog == NULL)
3290 goto theend;
3291 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3294 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003295 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 /* copy one dictionary file name into buf */
3298 if (flags == DICT_EXACT)
3299 {
3300 count = 1;
3301 files = &dict;
3302 }
3303 else
3304 {
3305 /* Expand wildcards in the dictionary name, but do not allow
3306 * backticks (for security, the 'dict' option may have been set in
3307 * a modeline). */
3308 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003309# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003310 if (!thesaurus && STRCMP(buf, "spell") == 0)
3311 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003312 else
3313# endif
3314 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 || expand_wildcards(1, &buf, &count, &files,
3316 EW_FILE|EW_SILENT) != OK)
3317 count = 0;
3318 }
3319
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003320# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003321 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003323 /* Complete from active spelling. Skip "\<" in the pattern, we
3324 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003325 if (pat[0] == '\\' && pat[1] == '<')
3326 ptr = pat + 2;
3327 else
3328 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003329 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003331 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003332# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003333 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003334 {
3335 ins_compl_files(count, files, thesaurus, flags,
3336 &regmatch, buf, &dir);
3337 if (flags != DICT_EXACT)
3338 FreeWild(count, files);
3339 }
3340 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 break;
3342 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003343
3344theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003346 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 vim_free(buf);
3348}
3349
Bram Moolenaar0b238792006-03-02 22:49:12 +00003350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003351ins_compl_files(
3352 int count,
3353 char_u **files,
3354 int thesaurus,
3355 int flags,
3356 regmatch_T *regmatch,
3357 char_u *buf,
3358 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003359{
3360 char_u *ptr;
3361 int i;
3362 FILE *fp;
3363 int add_r;
3364
3365 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3366 {
3367 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3368 if (flags != DICT_EXACT)
3369 {
3370 vim_snprintf((char *)IObuff, IOSIZE,
3371 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003372 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003373 }
3374
3375 if (fp != NULL)
3376 {
3377 /*
3378 * Read dictionary file line by line.
3379 * Check each line for a match.
3380 */
3381 while (!got_int && !compl_interrupted
3382 && !vim_fgets(buf, LSIZE, fp))
3383 {
3384 ptr = buf;
3385 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3386 {
3387 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003388 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003389 ptr = find_line_end(ptr);
3390 else
3391 ptr = find_word_end(ptr);
3392 add_r = ins_compl_add_infercase(regmatch->startp[0],
3393 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003394 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003395 if (thesaurus)
3396 {
3397 char_u *wstart;
3398
3399 /*
3400 * Add the other matches on the line
3401 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003402 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003403 while (!got_int)
3404 {
3405 /* Find start of the next word. Skip white
3406 * space and punctuation. */
3407 ptr = find_word_start(ptr);
3408 if (*ptr == NUL || *ptr == NL)
3409 break;
3410 wstart = ptr;
3411
Bram Moolenaara2993e12007-08-12 14:38:46 +00003412 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003413#ifdef FEAT_MBYTE
3414 if (has_mbyte)
3415 /* Japanese words may have characters in
3416 * different classes, only separate words
3417 * with single-byte non-word characters. */
3418 while (*ptr != NUL)
3419 {
3420 int l = (*mb_ptr2len)(ptr);
3421
3422 if (l < 2 && !vim_iswordc(*ptr))
3423 break;
3424 ptr += l;
3425 }
3426 else
3427#endif
3428 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003429
3430 /* Add the word. Skip the regexp match. */
3431 if (wstart != regmatch->startp[0])
3432 add_r = ins_compl_add_infercase(wstart,
3433 (int)(ptr - wstart),
3434 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003435 }
3436 }
3437 if (add_r == OK)
3438 /* if dir was BACKWARD then honor it just once */
3439 *dir = FORWARD;
3440 else if (add_r == FAIL)
3441 break;
3442 /* avoid expensive call to vim_regexec() when at end
3443 * of line */
3444 if (*ptr == '\n' || got_int)
3445 break;
3446 }
3447 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003448 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003449 }
3450 fclose(fp);
3451 }
3452 }
3453}
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455/*
3456 * Find the start of the next word.
3457 * Returns a pointer to the first char of the word. Also stops at a NUL.
3458 */
3459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003460find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462#ifdef FEAT_MBYTE
3463 if (has_mbyte)
3464 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003465 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 else
3467#endif
3468 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3469 ++ptr;
3470 return ptr;
3471}
3472
3473/*
3474 * Find the end of the word. Assumes it starts inside a word.
3475 * Returns a pointer to just after the word.
3476 */
3477 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003478find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
3480#ifdef FEAT_MBYTE
3481 int start_class;
3482
3483 if (has_mbyte)
3484 {
3485 start_class = mb_get_class(ptr);
3486 if (start_class > 1)
3487 while (*ptr != NUL)
3488 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003489 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (mb_get_class(ptr) != start_class)
3491 break;
3492 }
3493 }
3494 else
3495#endif
3496 while (vim_iswordc(*ptr))
3497 ++ptr;
3498 return ptr;
3499}
3500
3501/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003502 * Find the end of the line, omitting CR and NL at the end.
3503 * Returns a pointer to just after the line.
3504 */
3505 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003506find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003507{
3508 char_u *s;
3509
3510 s = ptr + STRLEN(ptr);
3511 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3512 --s;
3513 return s;
3514}
3515
3516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 * Free the list of completions
3518 */
3519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003520ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003522 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003523 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
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
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003528 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003530
3531 ins_compl_del_pum();
3532 pum_clear();
3533
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003534 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 do
3536 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003537 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003538 compl_curr_match = compl_curr_match->cp_next;
3539 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003541 if (match->cp_flags & FREE_FNAME)
3542 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003543 for (i = 0; i < CPT_COUNT; ++i)
3544 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003546 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3547 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003548 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003549 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550}
3551
3552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003555 compl_cont_status = 0;
3556 compl_started = FALSE;
3557 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003558 VIM_CLEAR(compl_pattern);
3559 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003561 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003562 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003563 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003564 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565}
3566
3567/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003568 * Return TRUE when Insert completion is active.
3569 */
3570 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003571ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003572{
3573 return compl_started;
3574}
3575
3576/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003577 * Delete one character before the cursor and show the subset of the matches
3578 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003579 * Returns the character to be used, NUL if the work is done and another char
3580 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003581 */
3582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003584{
3585 char_u *line;
3586 char_u *p;
3587
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003588 line = ml_get_curline();
3589 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003590 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003591
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003592 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003593 * allow the word to be deleted, we won't match everything.
3594 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003595 if ((int)(p - line) - (int)compl_col < 0
3596 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003597 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3598 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3599 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003600 return K_BS;
3601
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003602 /* Deleted more than what was used to find matches or didn't finish
3603 * finding all matches: need to look for matches all over again. */
3604 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003605 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003606 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003607
Bram Moolenaara6557602006-02-04 22:43:20 +00003608 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003609 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003610 if (compl_leader != NULL)
3611 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003612 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003613 if (compl_shown_match != NULL)
3614 /* Make sure current match is not a hidden item. */
3615 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003616 return NUL;
3617 }
3618 return K_BS;
3619}
Bram Moolenaara6557602006-02-04 22:43:20 +00003620
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003621/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003622 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3623 * be called.
3624 */
3625 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003627{
3628 /* Return TRUE if we didn't complete finding matches or when the
3629 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3630 return compl_was_interrupted
3631 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3632 && compl_opt_refresh_always);
3633}
3634
3635/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003636 * Called after changing "compl_leader".
3637 * Show the popup menu with a different set of matches.
3638 * May also search for matches again if the previous search was interrupted.
3639 */
3640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003642{
3643 ins_compl_del_pum();
3644 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003645 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003646 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003647
3648 if (compl_started)
3649 ins_compl_set_original_text(compl_leader);
3650 else
3651 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003652#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003653 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003654#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003655 /*
3656 * Matches were cleared, need to search for them now. First display
3657 * the changed text before the cursor. Set "compl_restarting" to
3658 * avoid that the first match is inserted.
3659 */
3660 update_screen(0);
3661#ifdef FEAT_GUI
3662 if (gui.in_use)
3663 {
3664 /* Show the cursor after the match, not after the redrawn text. */
3665 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003666 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003667 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003668#endif
3669 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003670 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003671 compl_cont_status = 0;
3672 compl_restarting = FALSE;
3673 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003674
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003675 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003676
3677 /* Show the popup menu with a different set of matches. */
3678 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003679
3680 /* Don't let Enter select the original text when there is no popup menu. */
3681 if (compl_match_array == NULL)
3682 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003683}
3684
3685/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003686 * Return the length of the completion, from the completion start column to
3687 * the cursor column. Making sure it never goes below zero.
3688 */
3689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003690ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003691{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003692 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003693
3694 if (off < 0)
3695 return 0;
3696 return off;
3697}
3698
3699/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003700 * Append one character to the match leader. May reduce the number of
3701 * matches.
3702 */
3703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003704ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003705{
3706#ifdef FEAT_MBYTE
3707 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003708#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003709
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003710 if (stop_arrow() == FAIL)
3711 return;
3712#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003713 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3714 {
3715 char_u buf[MB_MAXBYTES + 1];
3716
3717 (*mb_char2bytes)(c, buf);
3718 buf[cc] = NUL;
3719 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003720 if (compl_opt_refresh_always)
3721 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003722 }
3723 else
3724#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003725 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003726 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003727 if (compl_opt_refresh_always)
3728 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003729 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003730
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003731 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003732 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003733 ins_compl_restart();
3734
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003735 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003736 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003737 * break redo. */
3738 if (!compl_opt_refresh_always)
3739 {
3740 vim_free(compl_leader);
3741 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003742 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003743 if (compl_leader != NULL)
3744 ins_compl_new_leader();
3745 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003746}
3747
3748/*
3749 * Setup for finding completions again without leaving CTRL-X mode. Used when
3750 * BS or a key was typed while still searching for matches.
3751 */
3752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003753ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003754{
3755 ins_compl_free();
3756 compl_started = FALSE;
3757 compl_matches = 0;
3758 compl_cont_status = 0;
3759 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003760}
3761
3762/*
3763 * Set the first match, the original text.
3764 */
3765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003767{
3768 char_u *p;
3769
Bram Moolenaare87edf32018-04-17 22:14:32 +02003770 /* Replace the original text entry.
3771 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3772 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003773 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3774 {
3775 p = vim_strsave(str);
3776 if (p != NULL)
3777 {
3778 vim_free(compl_first_match->cp_str);
3779 compl_first_match->cp_str = p;
3780 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003781 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003782 else if (compl_first_match->cp_prev != NULL
3783 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3784 {
3785 p = vim_strsave(str);
3786 if (p != NULL)
3787 {
3788 vim_free(compl_first_match->cp_prev->cp_str);
3789 compl_first_match->cp_prev->cp_str = p;
3790 }
3791 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003792}
3793
3794/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003795 * Append one character to the match leader. May reduce the number of
3796 * matches.
3797 */
3798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003799ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003800{
3801 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003802 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003803 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003804 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003805
3806 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003807 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003808 {
3809 /* When still at the original match use the first entry that matches
3810 * the leader. */
3811 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3812 {
3813 p = NULL;
3814 for (cp = compl_shown_match->cp_next; cp != NULL
3815 && cp != compl_first_match; cp = cp->cp_next)
3816 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003817 if (compl_leader == NULL
3818 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003819 (int)STRLEN(compl_leader)))
3820 {
3821 p = cp->cp_str;
3822 break;
3823 }
3824 }
3825 if (p == NULL || (int)STRLEN(p) <= len)
3826 return;
3827 }
3828 else
3829 return;
3830 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003831 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003832 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003833 ins_compl_addleader(c);
3834}
3835
3836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003838 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003839 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003841 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003842ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
3844 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003846 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 /* Forget any previous 'special' messages if this is actually
3849 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3850 */
3851 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3852 edit_submode_extra = NULL;
3853
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003854 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003855 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3856 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003857 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003859 /* Set "compl_get_longest" when finding the first matches. */
3860 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003861 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003862 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003863 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003864 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003865
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003866 }
3867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3869 {
3870 /*
3871 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3872 * it will be yet. Now we decide.
3873 */
3874 switch (c)
3875 {
3876 case Ctrl_E:
3877 case Ctrl_Y:
3878 ctrl_x_mode = CTRL_X_SCROLL;
3879 if (!(State & REPLACE_FLAG))
3880 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3881 else
3882 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3883 edit_submode_pre = NULL;
3884 showmode();
3885 break;
3886 case Ctrl_L:
3887 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3888 break;
3889 case Ctrl_F:
3890 ctrl_x_mode = CTRL_X_FILES;
3891 break;
3892 case Ctrl_K:
3893 ctrl_x_mode = CTRL_X_DICTIONARY;
3894 break;
3895 case Ctrl_R:
3896 /* Simply allow ^R to happen without affecting ^X mode */
3897 break;
3898 case Ctrl_T:
3899 ctrl_x_mode = CTRL_X_THESAURUS;
3900 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003901#ifdef FEAT_COMPL_FUNC
3902 case Ctrl_U:
3903 ctrl_x_mode = CTRL_X_FUNCTION;
3904 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003905 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003906 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003907 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003908#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003909 case 's':
3910 case Ctrl_S:
3911 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003912#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003913 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003914 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003915 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003916#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003917 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 case Ctrl_RSB:
3919 ctrl_x_mode = CTRL_X_TAGS;
3920 break;
3921#ifdef FEAT_FIND_ID
3922 case Ctrl_I:
3923 case K_S_TAB:
3924 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3925 break;
3926 case Ctrl_D:
3927 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3928 break;
3929#endif
3930 case Ctrl_V:
3931 case Ctrl_Q:
3932 ctrl_x_mode = CTRL_X_CMDLINE;
3933 break;
3934 case Ctrl_P:
3935 case Ctrl_N:
3936 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3937 * just started ^X mode, or there were enough ^X's to cancel
3938 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3939 * do normal expansion when interrupting a different mode (say
3940 * ^X^F^X^P or ^P^X^X^P, see below)
3941 * nothing changes if interrupting mode 0, (eg, the flag
3942 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003943 if (!(compl_cont_status & CONT_INTRPT))
3944 compl_cont_status |= CONT_LOCAL;
3945 else if (compl_cont_mode != 0)
3946 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 /* FALLTHROUGH */
3948 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003949 /* If we have typed at least 2 ^X's... for modes != 0, we set
3950 * compl_cont_status = 0 (eg, as if we had just started ^X
3951 * mode).
3952 * For mode 0, we set "compl_cont_mode" to an impossible
3953 * value, in both cases ^X^X can be used to restart the same
3954 * mode (avoiding ADDING mode).
3955 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3956 * 'complete' and local ^P expansions respectively.
3957 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3958 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if (c == Ctrl_X)
3960 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003961 if (compl_cont_mode != 0)
3962 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003964 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003966 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 edit_submode = NULL;
3968 showmode();
3969 break;
3970 }
3971 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003972 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
3974 /* We're already in CTRL-X mode, do we stay in it? */
3975 if (!vim_is_ctrl_x_key(c))
3976 {
3977 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003978 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 else
3980 ctrl_x_mode = CTRL_X_FINISHED;
3981 edit_submode = NULL;
3982 }
3983 showmode();
3984 }
3985
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003986 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 {
3988 /* Show error message from attempted keyword completion (probably
3989 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003990 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003992 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3993 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 || ctrl_x_mode == CTRL_X_FINISHED)
3995 {
3996 /* Get here when we have finished typing a sequence of ^N and
3997 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003998 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003999 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
4001 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004002 * If any of the original typed text has been changed, eg when
4003 * ignorecase is set, we must add back-spaces to the redo
4004 * buffer. We add as few as necessary to delete just the part
4005 * of the original text that has changed.
4006 * When using the longest match, edited the match or used
4007 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004009 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4010 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004011 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004012 ptr = NULL;
4013 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015
4016#ifdef FEAT_CINDENT
4017 want_cindent = (can_cindent && cindent_on());
4018#endif
4019 /*
4020 * When completing whole lines: fix indent for 'cindent'.
4021 * Otherwise, break line if it's too long.
4022 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004023 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
4025#ifdef FEAT_CINDENT
4026 /* re-indent the current line */
4027 if (want_cindent)
4028 {
4029 do_c_expr_indent();
4030 want_cindent = FALSE; /* don't do it again */
4031 }
4032#endif
4033 }
4034 else
4035 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004036 int prev_col = curwin->w_cursor.col;
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004039 if (prev_col > 0)
4040 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004041 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004042 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004044 if (prev_col > 0
4045 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4046 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
4048
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004049 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004050 * the selection without inserting anything. When
4051 * compl_enter_selects is set the Enter key does the same. */
4052 if ((c == Ctrl_Y || (compl_enter_selects
4053 && (c == CAR || c == K_KENTER || c == NL)))
4054 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004055 retval = TRUE;
4056
Bram Moolenaar47247282016-08-02 22:36:02 +02004057 /* CTRL-E means completion is Ended, go back to the typed text.
4058 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004059 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004060 {
4061 ins_compl_delete();
4062 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004063 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004064 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004065 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004066 retval = TRUE;
4067 }
4068
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004069 auto_format(FALSE, TRUE);
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072 compl_started = FALSE;
4073 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004074 if (!shortmess(SHM_COMPLETIONMENU))
4075 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004076 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004077 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (edit_submode != NULL)
4079 {
4080 edit_submode = NULL;
4081 showmode();
4082 }
4083
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004084#ifdef FEAT_CMDWIN
4085 if (c == Ctrl_C && cmdwin_type != 0)
4086 /* Avoid the popup menu remains displayed when leaving the
4087 * command line window. */
4088 update_screen(0);
4089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090#ifdef FEAT_CINDENT
4091 /*
4092 * Indent now if a key was typed that is in 'cinkeys'.
4093 */
4094 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4095 do_c_expr_indent();
4096#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004097 /* Trigger the CompleteDone event to give scripts a chance to act
4098 * upon the completion. */
4099 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004102 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4103 /* Trigger the CompleteDone event to give scripts a chance to act
4104 * upon the (possibly failed) completion. */
4105 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107 /* reset continue_* if we left expansion-mode, if we stay they'll be
4108 * (re)set properly in ins_complete() */
4109 if (!vim_is_ctrl_x_key(c))
4110 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004111 compl_cont_status = 0;
4112 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004114
4115 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116}
4117
4118/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004119 * Fix the redo buffer for the completion leader replacing some of the typed
4120 * text. This inserts backspaces and appends the changed text.
4121 * "ptr" is the known leader text or NUL.
4122 */
4123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004124ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004125{
4126 int len;
4127 char_u *p;
4128 char_u *ptr = ptr_arg;
4129
4130 if (ptr == NULL)
4131 {
4132 if (compl_leader != NULL)
4133 ptr = compl_leader;
4134 else
4135 return; /* nothing to do */
4136 }
4137 if (compl_orig_text != NULL)
4138 {
4139 p = compl_orig_text;
4140 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4141 ;
4142#ifdef FEAT_MBYTE
4143 if (len > 0)
4144 len -= (*mb_head_off)(p, p + len);
4145#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004146 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004147 AppendCharToRedobuff(K_BS);
4148 }
4149 else
4150 len = 0;
4151 if (ptr != NULL)
4152 AppendToRedobuffLit(ptr + len, -1);
4153}
4154
4155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4157 * (depending on flag) starting from buf and looking for a non-scanned
4158 * buffer (other than curbuf). curbuf is special, if it is called with
4159 * buf=curbuf then it has to be the first call for a given flag/expansion.
4160 *
4161 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4162 */
4163 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004164ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
4168 if (flag == 'w') /* just windows */
4169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 if (buf == curbuf) /* first call for this flag/expansion */
4171 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004172 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 && wp->w_buffer->b_scanned)
4174 ;
4175 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177 else
4178 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4179 * (unlisted buffers)
4180 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004181 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 && ((flag == 'U'
4183 ? buf->b_p_bl
4184 : (!buf->b_p_bl
4185 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004186 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 ;
4188 return buf;
4189}
4190
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004191#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004192/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004193 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004194 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004195 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004197expand_by_function(
4198 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4199 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004200{
Bram Moolenaar82139082011-09-14 16:52:09 +02004201 list_T *matchlist = NULL;
4202 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004203 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004204 char_u *funcname;
4205 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004206 win_T *curwin_save;
4207 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004208 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004209
Bram Moolenaare344bea2005-09-01 20:46:49 +00004210 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4211 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004212 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004213
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004214 /* Call 'completefunc' to obtain the list of matches. */
4215 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004216 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004217
Bram Moolenaare344bea2005-09-01 20:46:49 +00004218 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004219 curwin_save = curwin;
4220 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004221
4222 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004223 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004224 {
4225 switch (rettv.v_type)
4226 {
4227 case VAR_LIST:
4228 matchlist = rettv.vval.v_list;
4229 break;
4230 case VAR_DICT:
4231 matchdict = rettv.vval.v_dict;
4232 break;
4233 default:
4234 /* TODO: Give error message? */
4235 clear_tv(&rettv);
4236 break;
4237 }
4238 }
4239
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004240 if (curwin_save != curwin || curbuf_save != curbuf)
4241 {
4242 EMSG(_(e_complwin));
4243 goto theend;
4244 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004245 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004246 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004247 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004248 {
4249 EMSG(_(e_compldel));
4250 goto theend;
4251 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004252
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004253 if (matchlist != NULL)
4254 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004255 else if (matchdict != NULL)
4256 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004257
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004258theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004259 if (matchdict != NULL)
4260 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004261 if (matchlist != NULL)
4262 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004263}
4264#endif /* FEAT_COMPL_FUNC */
4265
Bram Moolenaar39f05632006-03-19 22:15:26 +00004266#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004267/*
4268 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004269 */
4270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004271ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004272{
4273 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004274 int dir = compl_direction;
4275
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004276 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004277 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004278 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004279 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4280 /* if dir was BACKWARD then honor it just once */
4281 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004282 else if (did_emsg)
4283 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004284 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004285}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004286
4287/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004288 * Add completions from a dict.
4289 */
4290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004291ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004292{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004293 dictitem_T *di_refresh;
4294 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004295
4296 /* Check for optional "refresh" item. */
4297 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004298 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4299 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004300 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004301 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004302
4303 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4304 compl_opt_refresh_always = TRUE;
4305 }
4306
4307 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004308 di_words = dict_find(dict, (char_u *)"words", 5);
4309 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4310 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004311}
4312
4313/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004314 * Add a match to the list of matches from a typeval_T.
4315 * If the given string is already in the list of completions, then return
4316 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4317 * maybe because alloc() returns NULL, then FAIL is returned.
4318 */
4319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004320ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004321{
4322 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004323 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004324 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004325 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004326 char_u *(cptext[CPT_COUNT]);
4327
4328 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4329 {
4330 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4331 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4332 (char_u *)"abbr", FALSE);
4333 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4334 (char_u *)"menu", FALSE);
4335 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4336 (char_u *)"kind", FALSE);
4337 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4338 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004339 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4340 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004341 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4342 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004343 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004344 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004345 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4346 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004347 }
4348 else
4349 {
4350 word = get_tv_string_chk(tv);
4351 vim_memset(cptext, 0, sizeof(cptext));
4352 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004353 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004354 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004355 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004356}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004357#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004358
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004359/*
4360 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004361 * The search starts at position "ini" in curbuf and in the direction
4362 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004363 * When "compl_started" is FALSE start at that position, otherwise continue
4364 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365 * This may return before finding all the matches.
4366 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 */
4368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004369ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
4371 static pos_T first_match_pos;
4372 static pos_T last_match_pos;
4373 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374 static int found_all = FALSE; /* Found all matches of a
4375 certain type. */
4376 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
Bram Moolenaar572cb562005-08-05 21:35:02 +00004378 pos_T *pos;
4379 char_u **matches;
4380 int save_p_scs;
4381 int save_p_ws;
4382 int save_p_ic;
4383 int i;
4384 int num_matches;
4385 int len;
4386 int found_new_match;
4387 int type = ctrl_x_mode;
4388 char_u *ptr;
4389 char_u *dict = NULL;
4390 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004391 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004395 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 ins_buf->b_scanned = 0;
4397 found_all = FALSE;
4398 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004400 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 last_match_pos = first_match_pos = *ini;
4402 }
4403
Bram Moolenaar4475b622017-05-01 20:46:52 +02004404 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004405 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4407 for (;;)
4408 {
4409 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004410 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 * or if found_all says this entry is done. For ^X^L only use the
4414 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004415 if ((ctrl_x_mode == CTRL_X_NORMAL
4416 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
4419 found_all = FALSE;
4420 while (*e_cpt == ',' || *e_cpt == ' ')
4421 e_cpt++;
4422 if (*e_cpt == '.' && !curbuf->b_scanned)
4423 {
4424 ins_buf = curbuf;
4425 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004426 /* Move the cursor back one character so that ^N can match the
4427 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004428 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004429 {
4430 /* Move the cursor to after the last character in the
4431 * buffer, so that word at start of buffer is found
4432 * correctly. */
4433 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4434 first_match_pos.col =
4435 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 last_match_pos = first_match_pos;
4438 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004439
4440 /* Remember the first match so that the loop stops when we
4441 * wrap and come back there a second time. */
4442 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4445 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4446 {
4447 /* Scan a buffer, but not the current one. */
4448 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4449 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004450 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 first_match_pos.col = last_match_pos.col = 0;
4452 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4453 last_match_pos.lnum = 0;
4454 type = 0;
4455 }
4456 else /* unloaded buffer, scan like dictionary */
4457 {
4458 found_all = TRUE;
4459 if (ins_buf->b_fname == NULL)
4460 continue;
4461 type = CTRL_X_DICTIONARY;
4462 dict = ins_buf->b_fname;
4463 dict_f = DICT_EXACT;
4464 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004465 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 ins_buf->b_fname == NULL
4467 ? buf_spname(ins_buf)
4468 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004469 ? ins_buf->b_fname
4470 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004471 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 else if (*e_cpt == NUL)
4474 break;
4475 else
4476 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004477 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 type = -1;
4479 else if (*e_cpt == 'k' || *e_cpt == 's')
4480 {
4481 if (*e_cpt == 'k')
4482 type = CTRL_X_DICTIONARY;
4483 else
4484 type = CTRL_X_THESAURUS;
4485 if (*++e_cpt != ',' && *e_cpt != NUL)
4486 {
4487 dict = e_cpt;
4488 dict_f = DICT_FIRST;
4489 }
4490 }
4491#ifdef FEAT_FIND_ID
4492 else if (*e_cpt == 'i')
4493 type = CTRL_X_PATH_PATTERNS;
4494 else if (*e_cpt == 'd')
4495 type = CTRL_X_PATH_DEFINES;
4496#endif
4497 else if (*e_cpt == ']' || *e_cpt == 't')
4498 {
4499 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004500 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004501 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503 else
4504 type = -1;
4505
4506 /* in any case e_cpt is advanced to the next entry */
4507 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4508
4509 found_all = TRUE;
4510 if (type == -1)
4511 continue;
4512 }
4513 }
4514
Bram Moolenaar4475b622017-05-01 20:46:52 +02004515 /* If complete() was called then compl_pattern has been reset. The
4516 * following won't work then, bail out. */
4517 if (compl_pattern == NULL)
4518 break;
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 switch (type)
4521 {
4522 case -1:
4523 break;
4524#ifdef FEAT_FIND_ID
4525 case CTRL_X_PATH_PATTERNS:
4526 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004527 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004530 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4532 (linenr_T)1, (linenr_T)MAXLNUM);
4533 break;
4534#endif
4535
4536 case CTRL_X_DICTIONARY:
4537 case CTRL_X_THESAURUS:
4538 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004539 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 : (type == CTRL_X_THESAURUS
4541 ? (*curbuf->b_p_tsr == NUL
4542 ? p_tsr
4543 : curbuf->b_p_tsr)
4544 : (*curbuf->b_p_dict == NUL
4545 ? p_dict
4546 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004547 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004548 dict != NULL ? dict_f
4549 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 dict = NULL;
4551 break;
4552
4553 case CTRL_X_TAGS:
4554 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4555 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004558 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 * of matches is found when compl_pattern is empty */
4560 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004561 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4562 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4564 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004565 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 p_ic = save_p_ic;
4568 break;
4569
4570 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4573 {
4574
4575 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004576 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004577 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
4579 break;
4580
4581 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004582 if (expand_cmdline(&compl_xp, compl_pattern,
4583 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004585 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 break;
4587
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004588#ifdef FEAT_COMPL_FUNC
4589 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004590 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004591 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004592 break;
4593#endif
4594
Bram Moolenaar488c6512005-08-11 20:09:58 +00004595 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004596#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004597 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004598 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004599 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004600 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004601#endif
4602 break;
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 default: /* normal ^P/^N and ^X^L */
4605 /*
4606 * If 'infercase' is set, don't use 'smartcase' here
4607 */
4608 save_p_scs = p_scs;
4609 if (ins_buf->b_p_inf)
4610 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004611
Bram Moolenaar361aa502014-01-23 22:45:58 +01004612 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 * end but never from the middle, thus setting nowrapscan in this
4614 * buffers is a good idea, on the other hand, we always set
4615 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4616 save_p_ws = p_ws;
4617 if (ins_buf != curbuf)
4618 p_ws = FALSE;
4619 else if (*e_cpt == '.')
4620 p_ws = TRUE;
4621 for (;;)
4622 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004623 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004625 ++msg_silent; /* Don't want messages for wrapscan. */
4626
Bram Moolenaare4214502015-03-05 18:08:43 +01004627 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4628 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004629 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004630 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004631 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004633 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004635 found_new_match = searchit(NULL, ins_buf, pos,
4636 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004638 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004639 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004640 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004642 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004643 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 first_match_pos = *pos;
4645 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004646 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004649 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 found_new_match = FAIL;
4651 if (found_new_match == FAIL)
4652 {
4653 if (ins_buf == curbuf)
4654 found_all = TRUE;
4655 break;
4656 }
4657
4658 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004659 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 && ini->lnum == pos->lnum
4661 && ini->col == pos->col)
4662 continue;
4663 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004664 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004666 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
4668 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4669 continue;
4670 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4671 if (!p_paste)
4672 ptr = skipwhite(ptr);
4673 }
4674 len = (int)STRLEN(ptr);
4675 }
4676 else
4677 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004678 char_u *tmp_ptr = ptr;
4679
4680 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004682 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 /* Skip if already inside a word. */
4684 if (vim_iswordp(tmp_ptr))
4685 continue;
4686 /* Find start of next word. */
4687 tmp_ptr = find_word_start(tmp_ptr);
4688 }
4689 /* Find end of this word. */
4690 tmp_ptr = find_word_end(tmp_ptr);
4691 len = (int)(tmp_ptr - ptr);
4692
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004693 if ((compl_cont_status & CONT_ADDING)
4694 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
4696 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4697 {
4698 /* Try next line, if any. the new word will be
4699 * "join" as if the normal command "J" was used.
4700 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004701 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 * works -- Acevedo */
4703 STRNCPY(IObuff, ptr, len);
4704 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4705 tmp_ptr = ptr = skipwhite(ptr);
4706 /* Find start of next word. */
4707 tmp_ptr = find_word_start(tmp_ptr);
4708 /* Find end of next word. */
4709 tmp_ptr = find_word_end(tmp_ptr);
4710 if (tmp_ptr > ptr)
4711 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004712 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004714 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 IObuff[len++] = ' ';
4716 /* IObuf =~ "\k.* ", thus len >= 2 */
4717 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004718 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 || (vim_strchr(p_cpo, CPO_JOINSP)
4720 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004721 && (IObuff[len - 2] == '?'
4722 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 IObuff[len++] = ' ';
4724 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004725 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (tmp_ptr - ptr >= IOSIZE - len)
4727 tmp_ptr = ptr + IOSIZE - len - 1;
4728 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4729 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004730 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732 IObuff[len] = NUL;
4733 ptr = IObuff;
4734 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004735 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 continue;
4737 }
4738 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004739 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004740 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004741 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
4743 found_new_match = OK;
4744 break;
4745 }
4746 }
4747 p_scs = save_p_scs;
4748 p_ws = save_p_ws;
4749 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004750
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004751 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004752 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004753 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 found_new_match = OK;
4755
4756 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004757 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4758 * match */
4759 if ((ctrl_x_mode != CTRL_X_NORMAL
4760 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004761 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004762 {
4763 if (got_int)
4764 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004765 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004766 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004767 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004768
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004769 if ((ctrl_x_mode != CTRL_X_NORMAL
4770 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004771 || compl_interrupted)
4772 break;
4773 compl_started = TRUE;
4774 }
4775 else
4776 {
4777 /* Mark a buffer scanned when it has been scanned completely */
4778 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4779 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004781 compl_started = FALSE;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004784 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004786 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 && *e_cpt == NUL) /* Got to end of 'complete' */
4788 found_new_match = FAIL;
4789
4790 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004791 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4792 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 i = ins_compl_make_cyclic();
4794
Bram Moolenaar4475b622017-05-01 20:46:52 +02004795 if (compl_old_match != NULL)
4796 {
4797 /* If several matches were added (FORWARD) or the search failed and has
4798 * just been made cyclic then we have to move compl_curr_match to the
4799 * next or previous entry (if any) -- Acevedo */
4800 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4801 : compl_old_match->cp_prev;
4802 if (compl_curr_match == NULL)
4803 compl_curr_match = compl_old_match;
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return i;
4806}
4807
4808/* Delete the old text being completed. */
4809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004810ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004812 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 /*
4815 * In insert mode: Delete the typed part.
4816 * In replace mode: Put the old characters back, if any.
4817 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004818 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4819 if ((int)curwin->w_cursor.col > col)
4820 {
4821 if (stop_arrow() == FAIL)
4822 return;
4823 backspace_until_column(col);
4824 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004825
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004826 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4827 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004829 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004830 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831}
4832
Bram Moolenaar472e8592016-10-15 17:06:47 +02004833/*
4834 * Insert the new text being completed.
4835 * "in_compl_func" is TRUE when called from complete_check().
4836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004838ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004840 dict_T *dict;
4841
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004842 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004843 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4844 compl_used_match = FALSE;
4845 else
4846 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004847
4848 /* Set completed item. */
4849 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004850 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004851 if (dict != NULL)
4852 {
4853 dict_add_nr_str(dict, "word", 0L,
4854 EMPTY_IF_NULL(compl_shown_match->cp_str));
4855 dict_add_nr_str(dict, "abbr", 0L,
4856 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4857 dict_add_nr_str(dict, "menu", 0L,
4858 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4859 dict_add_nr_str(dict, "kind", 0L,
4860 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4861 dict_add_nr_str(dict, "info", 0L,
4862 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004863 dict_add_nr_str(dict, "user_data", 0L,
4864 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004865 }
4866 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004867 if (!in_compl_func)
4868 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869}
4870
4871/*
4872 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004873 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4874 * get more completions. If it is FALSE, then we just do nothing when there
4875 * are no more completions in a given direction. The latter case is used when
4876 * we are still in the middle of finding completions, to allow browsing
4877 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 * Return the total number of matches, or -1 if still unknown -- webb.
4879 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004880 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4881 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 *
4883 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004884 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4885 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 */
4887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004888ins_compl_next(
4889 int allow_get_expansion,
4890 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004891 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004892 int insert_match, /* Insert the newly selected match */
4893 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
4895 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004896 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004897 compl_T *found_compl = NULL;
4898 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004899 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004900 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004902 /* When user complete function return -1 for findstart which is next
4903 * time of 'always', compl_shown_match become NULL. */
4904 if (compl_shown_match == NULL)
4905 return -1;
4906
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004907 if (compl_leader != NULL
4908 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004910 /* Set "compl_shown_match" to the actually shown match, it may differ
4911 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004912 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004913 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004914 && compl_shown_match->cp_next != NULL
4915 && compl_shown_match->cp_next != compl_first_match)
4916 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004917
4918 /* If we didn't find it searching forward, and compl_shows_dir is
4919 * backward, find the last match. */
4920 if (compl_shows_dir == BACKWARD
4921 && !ins_compl_equal(compl_shown_match,
4922 compl_leader, (int)STRLEN(compl_leader))
4923 && (compl_shown_match->cp_next == NULL
4924 || compl_shown_match->cp_next == compl_first_match))
4925 {
4926 while (!ins_compl_equal(compl_shown_match,
4927 compl_leader, (int)STRLEN(compl_leader))
4928 && compl_shown_match->cp_prev != NULL
4929 && compl_shown_match->cp_prev != compl_first_match)
4930 compl_shown_match = compl_shown_match->cp_prev;
4931 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004932 }
4933
4934 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004935 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 /* Delete old text to be replaced */
4937 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004938
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004939 /* When finding the longest common text we stick at the original text,
4940 * don't let CTRL-N or CTRL-P move to the first match. */
4941 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4942
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004943 /* When restarting the search don't insert the first match either. */
4944 if (compl_restarting)
4945 {
4946 advance = FALSE;
4947 compl_restarting = FALSE;
4948 }
4949
Bram Moolenaare3226be2005-12-18 22:10:00 +00004950 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4951 * around. */
4952 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004954 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004956 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004957 found_end = (compl_first_match != NULL
4958 && (compl_shown_match->cp_next == compl_first_match
4959 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004960 }
4961 else if (compl_shows_dir == BACKWARD
4962 && compl_shown_match->cp_prev != NULL)
4963 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004964 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004965 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004966 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004969 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004970 if (!allow_get_expansion)
4971 {
4972 if (advance)
4973 {
4974 if (compl_shows_dir == BACKWARD)
4975 compl_pending -= todo + 1;
4976 else
4977 compl_pending += todo + 1;
4978 }
4979 return -1;
4980 }
4981
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004982 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004983 {
4984 if (compl_shows_dir == BACKWARD)
4985 --compl_pending;
4986 else
4987 ++compl_pending;
4988 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004989
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004990 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004991 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004992
4993 /* handle any pending completions */
4994 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004995 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004996 {
4997 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4998 {
4999 compl_shown_match = compl_shown_match->cp_next;
5000 --compl_pending;
5001 }
5002 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5003 {
5004 compl_shown_match = compl_shown_match->cp_prev;
5005 ++compl_pending;
5006 }
5007 else
5008 break;
5009 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005010 found_end = FALSE;
5011 }
5012 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5013 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005014 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005015 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005016 ++todo;
5017 else
5018 /* Remember a matching item. */
5019 found_compl = compl_shown_match;
5020
5021 /* Stop at the end of the list when we found a usable match. */
5022 if (found_end)
5023 {
5024 if (found_compl != NULL)
5025 {
5026 compl_shown_match = found_compl;
5027 break;
5028 }
5029 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005033 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005034 if (compl_no_insert && !started)
5035 {
5036 ins_bytes(compl_orig_text + ins_compl_len());
5037 compl_used_match = FALSE;
5038 }
5039 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005040 {
5041 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005042 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005043 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005044 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005045 }
5046 else
5047 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 if (!allow_get_expansion)
5050 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005051 /* may undisplay the popup menu first */
5052 ins_compl_upd_pum();
5053
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005054 /* redraw to show the user what was inserted */
5055 update_screen(0);
5056
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005057 /* display the updated popup menu */
5058 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005059#ifdef FEAT_GUI
5060 if (gui.in_use)
5061 {
5062 /* Show the cursor after the match, not after the redrawn text. */
5063 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005064 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005065 }
5066#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 /* Delete old text to be replaced, since we're still searching and
5069 * don't want to match ourselves! */
5070 ins_compl_delete();
5071 }
5072
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005073 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005074 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005075 if (compl_no_insert && !started)
5076 compl_enter_selects = TRUE;
5077 else
5078 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005079
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 /*
5081 * Show the file name for the match (if any)
5082 * Truncate the file name to avoid a wait for return.
5083 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005084 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005086 char *lead = _("match in file");
5087 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5088 char_u *s;
5089 char_u *e;
5090
5091 if (space > 0)
5092 {
5093 /* We need the tail that fits. With double-byte encoding going
5094 * back from the end is very slow, thus go from the start and keep
5095 * the text that fits in "space" between "s" and "e". */
5096 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5097 {
5098 space -= ptr2cells(e);
5099 while (space < 0)
5100 {
5101 space += ptr2cells(s);
5102 MB_PTR_ADV(s);
5103 }
5104 }
5105 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5106 s > compl_shown_match->cp_fname ? "<" : "", s);
5107 msg(IObuff);
5108 redraw_cmdline = FALSE; /* don't overwrite! */
5109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111
5112 return num_matches;
5113}
5114
5115/*
5116 * Call this while finding completions, to check whether the user has hit a key
5117 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005118 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005120 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005121 * "in_compl_func" is TRUE when called from complete_check(), don't set
5122 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 */
5124 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005125ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
5127 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005128 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005130 /* Don't check when reading keys from a script, :normal or feedkeys().
5131 * That would break the test scripts. But do check for keys when called
5132 * from complete_check(). */
5133 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 return;
5135
5136 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005137 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 return;
5139 count = 0;
5140
Bram Moolenaara260a972006-06-23 19:36:29 +00005141 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5142 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 if (c != NUL)
5145 {
5146 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5147 {
5148 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005149 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005150 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005151 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005153 else
5154 {
5155 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005156 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005157 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005158 if (c != K_IGNORE)
5159 {
5160 /* Don't interrupt completion when the character wasn't typed,
5161 * e.g., when doing @q to replay keys. */
5162 if (c != Ctrl_R && KeyTyped)
5163 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005164
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005165 vungetc(c);
5166 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005169 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005170 {
5171 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5172
5173 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005174 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005175 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005176}
5177
5178/*
5179 * Decide the direction of Insert mode complete from the key typed.
5180 * Returns BACKWARD or FORWARD.
5181 */
5182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005183ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005184{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005185 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005186 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005187 return BACKWARD;
5188 return FORWARD;
5189}
5190
5191/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005192 * Return TRUE for keys that are used for completion only when the popup menu
5193 * is visible.
5194 */
5195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005196ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005197{
5198 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005199 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5200 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005201}
5202
5203/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005204 * Decide the number of completions to move forward.
5205 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5206 */
5207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005208ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005209{
5210 int h;
5211
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005212 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005213 {
5214 h = pum_get_height();
5215 if (h > 3)
5216 h -= 2; /* keep some context */
5217 return h;
5218 }
5219 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220}
5221
5222/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005223 * Return TRUE if completion with "c" should insert the match, FALSE if only
5224 * to change the currently selected completion.
5225 */
5226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005227ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005228{
5229 switch (c)
5230 {
5231 case K_UP:
5232 case K_DOWN:
5233 case K_PAGEDOWN:
5234 case K_KPAGEDOWN:
5235 case K_S_DOWN:
5236 case K_PAGEUP:
5237 case K_KPAGEUP:
5238 case K_S_UP:
5239 return FALSE;
5240 }
5241 return TRUE;
5242}
5243
5244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 * Do Insert mode completion.
5246 * Called when character "c" was typed, which has a meaning for completion.
5247 * Returns OK if completion was done, FAIL if something failed (out of mem).
5248 */
5249 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005250ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 char_u *line;
5253 int startcol = 0; /* column where searched text starts */
5254 colnr_T curs_col; /* cursor column */
5255 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005256 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005257 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005258 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005259 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
Bram Moolenaare3226be2005-12-18 22:10:00 +00005261 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005262 insert_match = ins_compl_use_match(c);
5263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
5266 /* First time we hit ^N or ^P (in a row, I mean) */
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 did_ai = FALSE;
5269#ifdef FEAT_SMARTINDENT
5270 did_si = FALSE;
5271 can_si = FALSE;
5272 can_si_back = FALSE;
5273#endif
5274 if (stop_arrow() == FAIL)
5275 return FAIL;
5276
5277 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005279 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005281 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 * "compl_startpos" to the cursor as a pattern to add a new word
5283 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005284 * "compl_startpos" is not in the same line as the cursor then fix it
5285 * (the line has been split because it was longer than 'tw'). if SOL
5286 * is set then skip the previous pattern, a word at the beginning of
5287 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005288 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5289 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
5291 /*
5292 * it is a continued search
5293 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005294 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005295 if (ctrl_x_mode == CTRL_X_NORMAL
5296 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5297 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 /* line (probably) wrapped, set compl_startpos to the
5302 * first non_blank in the line, if it is not a wordchar
5303 * include it to get a better pattern, but then we don't
5304 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005305 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 compl_startpos.col = compl_col;
5307 compl_startpos.lnum = curwin->w_cursor.lnum;
5308 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310 else
5311 {
5312 /* S_IPOS was set when we inserted a word that was at the
5313 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005314 * mode but first we need to redefine compl_startpos */
5315 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005317 compl_cont_status |= CONT_SOL;
5318 compl_startpos.col = (colnr_T)(skipwhite(
5319 line + compl_length
5320 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005322 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005324 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005325 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005326 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005330 compl_cont_status &= ~CONT_SOL;
5331 compl_length = (IOSIZE - MIN_SPACE);
5332 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005334 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5335 if (compl_length < 1)
5336 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005338 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
5343 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005349 if (ctrl_x_mode != CTRL_X_NORMAL)
5350 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 compl_cont_status = 0;
5352 compl_cont_status |= CONT_N_ADDS;
5353 compl_startpos = curwin->w_cursor;
5354 startcol = (int)curs_col;
5355 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357
5358 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005359 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005361 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5363 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005366 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 compl_col += ++startcol;
5369 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 compl_pattern = str_foldcase(line + compl_col,
5373 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 compl_pattern = vim_strnsave(line + compl_col,
5376 compl_length);
5377 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return FAIL;
5379 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 char_u *prefix = (char_u *)"\\<";
5383
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005384 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005385 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005386 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 if (!vim_iswordp(line + compl_col)
5390 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 && (
5392#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005393 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#endif
5397 )))
5398 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 STRCPY((char *)compl_pattern, prefix);
5400 (void)quote_meta(compl_pattern + STRLEN(prefix),
5401 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408#endif
5409 )
5410 {
5411 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5413 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005415 compl_col += curs_col;
5416 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 else
5419 {
5420#ifdef FEAT_MBYTE
5421 /* Search the point of change class of multibyte character
5422 * or not a word single byte character backward. */
5423 if (has_mbyte)
5424 {
5425 int base_class;
5426 int head_off;
5427
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005428 startcol -= (*mb_head_off)(line, line + startcol);
5429 base_class = mb_get_class(line + startcol);
5430 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 head_off = (*mb_head_off)(line, line + startcol);
5433 if (base_class != mb_get_class(line + startcol
5434 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438 }
5439 else
5440#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005443 compl_col += ++startcol;
5444 compl_length = (int)curs_col - startcol;
5445 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
5447 /* Only match word with at least two chars -- webb
5448 * there's no need to call quote_meta,
5449 * alloc(7) is enough -- Acevedo
5450 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005451 compl_pattern = alloc(7);
5452 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005454 STRCPY((char *)compl_pattern, "\\<");
5455 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5456 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458 else
5459 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005460 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005461 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005462 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005464 STRCPY((char *)compl_pattern, "\\<");
5465 (void)quote_meta(compl_pattern + 2, line + compl_col,
5466 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 }
5469 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005470 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005472 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005473 compl_length = (int)curs_col - (int)compl_col;
5474 if (compl_length < 0) /* cursor in indent: empty pattern */
5475 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005477 compl_pattern = str_foldcase(line + compl_col, compl_length,
5478 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005480 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5481 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 return FAIL;
5483 }
5484 else if (ctrl_x_mode == CTRL_X_FILES)
5485 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005486 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005487 if (startcol > 0)
5488 {
5489 char_u *p = line + startcol;
5490
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005491 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005492 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005493 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005494 if (p == line && vim_isfilec(PTR2CHAR(p)))
5495 startcol = 0;
5496 else
5497 startcol = (int)(p - line) + 1;
5498 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005499
Bram Moolenaar0300e462013-09-08 16:03:45 +02005500 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005501 compl_length = (int)curs_col - startcol;
5502 compl_pattern = addstar(line + compl_col, compl_length,
5503 EXPAND_FILES);
5504 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 return FAIL;
5506 }
5507 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5508 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005509 compl_pattern = vim_strnsave(line, curs_col);
5510 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005512 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005513 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005514 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5515 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005516 /* No completion possible, use an empty pattern to get a
5517 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005518 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005519 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005520 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5521 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005523 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005524 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005525#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005526 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005527 * Call user defined function 'completefunc' with "a:findstart"
5528 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005529 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005530 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005531 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005532 char_u *funcname;
5533 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005534 win_T *curwin_save;
5535 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005536
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005537 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005538 * string */
5539 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5540 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5541 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005542 {
5543 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5544 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005545 /* restore did_ai, so that adding comment leader works */
5546 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005547 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005548 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005549
5550 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005551 args[1] = NULL;
5552 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005553 curwin_save = curwin;
5554 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005555 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005556 if (curwin_save != curwin || curbuf_save != curbuf)
5557 {
5558 EMSG(_(e_complwin));
5559 return FAIL;
5560 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005561 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005562 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005563 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005564 {
5565 EMSG(_(e_compldel));
5566 return FAIL;
5567 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005568
Bram Moolenaar6110a002012-01-26 18:58:38 +01005569 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005570 * cancel the complete without an error.
5571 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005572 if (col == -2)
5573 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005574 if (col == -3)
5575 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005576 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005577 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005578 if (!shortmess(SHM_COMPLETIONMENU))
5579 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005580 return FAIL;
5581 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005582
Bram Moolenaar82139082011-09-14 16:52:09 +02005583 /*
5584 * Reset extended parameters of completion, when start new
5585 * completion.
5586 */
5587 compl_opt_refresh_always = FALSE;
5588
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005589 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005590 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005591 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005592 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005593 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005594
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005595 /* Setup variables for completion. Need to obtain "line" again,
5596 * it may have become invalid. */
5597 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005598 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005599 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5600 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005601#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005602 return FAIL;
5603 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005604 else if (ctrl_x_mode == CTRL_X_SPELL)
5605 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005606#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005607 if (spell_bad_len > 0)
5608 compl_col = curs_col - spell_bad_len;
5609 else
5610 compl_col = spell_word_start(startcol);
5611 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005612 {
5613 compl_length = 0;
5614 compl_col = curs_col;
5615 }
5616 else
5617 {
5618 spell_expand_check_cap(compl_col);
5619 compl_length = (int)curs_col - compl_col;
5620 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005621 /* Need to obtain "line" again, it may have become invalid. */
5622 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005623 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5624 if (compl_pattern == NULL)
5625#endif
5626 return FAIL;
5627 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005628 else
5629 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005630 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005631 return FAIL;
5632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005634 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005637 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
5639 /* Insert a new line, keep indentation but ignore 'comments' */
5640#ifdef FEAT_COMMENTS
5641 char_u *old = curbuf->b_p_com;
5642
5643 curbuf->b_p_com = (char_u *)"";
5644#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005645 compl_startpos.lnum = curwin->w_cursor.lnum;
5646 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 ins_eol('\r');
5648#ifdef FEAT_COMMENTS
5649 curbuf->b_p_com = old;
5650#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005651 compl_length = 0;
5652 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
5654 }
5655 else
5656 {
5657 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005658 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005661 if (compl_cont_status & CONT_LOCAL)
5662 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 else
5664 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5665
Bram Moolenaar62951b12011-09-21 18:23:05 +02005666 /* If any of the original typed text has been changed we need to fix
5667 * the redo buffer. */
5668 ins_compl_fixRedoBufForLeader(NULL);
5669
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005670 /* Always add completion for the original text. */
5671 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005672 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5673 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005674 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005676 VIM_CLEAR(compl_pattern);
5677 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 return FAIL;
5679 }
5680
5681 /* showmode might reset the internal line pointers, so it must
5682 * be called before line = ml_get(), or when this address is no
5683 * longer needed. -- Acevedo.
5684 */
5685 edit_submode_extra = (char_u *)_("-- Searching...");
5686 edit_submode_highl = HLF_COUNT;
5687 showmode();
5688 edit_submode_extra = NULL;
5689 out_flush();
5690 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005691 else if (insert_match && stop_arrow() == FAIL)
5692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005694 compl_shown_match = compl_curr_match;
5695 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005698 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005700 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005701 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005702 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005704 /* may undisplay the popup menu */
5705 ins_compl_upd_pum();
5706
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005707 if (n > 1) /* all matches have been found */
5708 compl_matches = n;
5709 compl_curr_match = compl_shown_match;
5710 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
Bram Moolenaard68071d2006-05-02 22:08:30 +00005712 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5713 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 if (got_int && !global_busy)
5715 {
5716 (void)vgetc();
5717 got_int = FALSE;
5718 }
5719
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005720 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005721 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005723 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5724 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5726 edit_submode_highl = HLF_E;
5727 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5728 * because we couldn't expand anything at first place, but if we used
5729 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5730 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005731 if ( compl_length > 1
5732 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005733 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5735 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005736 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738
Bram Moolenaar572cb562005-08-05 21:35:02 +00005739 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005740 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005742 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
5744 if (edit_submode_extra == NULL)
5745 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005746 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 edit_submode_extra = (char_u *)_("Back at original");
5749 edit_submode_highl = HLF_W;
5750 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005751 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 edit_submode_extra = (char_u *)_("Word from other line");
5754 edit_submode_highl = HLF_COUNT;
5755 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005756 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 edit_submode_extra = (char_u *)_("The only match");
5759 edit_submode_highl = HLF_COUNT;
5760 }
5761 else
5762 {
5763 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005764 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005766 int number = 0;
5767 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005769 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 /* search backwards for the first valid (!= -1) number.
5772 * This should normally succeed already at the first loop
5773 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005774 for (match = compl_curr_match->cp_prev; match != NULL
5775 && match != compl_first_match;
5776 match = match->cp_prev)
5777 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005779 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 break;
5781 }
5782 if (match != NULL)
5783 /* go up and assign all numbers which are not assigned
5784 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005785 for (match = match->cp_next;
5786 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005787 match = match->cp_next)
5788 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790 else /* BACKWARD */
5791 {
5792 /* search forwards (upwards) for the first valid (!= -1)
5793 * number. This should normally succeed already at the
5794 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005795 for (match = compl_curr_match->cp_next; match != NULL
5796 && match != compl_first_match;
5797 match = match->cp_next)
5798 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005800 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 break;
5802 }
5803 if (match != NULL)
5804 /* go down and assign all numbers which are not
5805 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005806 for (match = match->cp_prev; match
5807 && match->cp_number == -1;
5808 match = match->cp_prev)
5809 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
5811 }
5812
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005813 /* The match should always have a sequence number now, this is
5814 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005815 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005817 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5818 * Translations may need more than twice that. */
5819 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005821 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005822 vim_snprintf((char *)match_ref, sizeof(match_ref),
5823 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005824 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005826 vim_snprintf((char *)match_ref, sizeof(match_ref),
5827 _("match %d"),
5828 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 edit_submode_extra = match_ref;
5830 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005831 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 curs_columns(FALSE);
5833 }
5834 }
5835 }
5836
5837 /* Show a message about what (completion) mode we're in. */
5838 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005839 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005841 if (edit_submode_extra != NULL)
5842 {
5843 if (!p_smd)
5844 msg_attr(edit_submode_extra,
5845 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005846 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005847 }
5848 else
5849 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
Bram Moolenaard68071d2006-05-02 22:08:30 +00005852 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005853 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005854 show_pum(save_w_wrow, save_w_leftcol);
5855
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005856 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005857 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005858
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 return OK;
5860}
5861
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005862 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005863show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005864{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005865 /* RedrawingDisabled may be set when invoked through complete(). */
5866 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005867
Bram Moolenaarcb036422017-03-01 12:29:10 +01005868 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005869
Bram Moolenaarcb036422017-03-01 12:29:10 +01005870 /* If the cursor moved or the display scrolled we need to remove the pum
5871 * first. */
5872 setcursor();
5873 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5874 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005875
Bram Moolenaarcb036422017-03-01 12:29:10 +01005876 ins_compl_show_pum();
5877 setcursor();
5878 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005879}
5880
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881/*
5882 * Looks in the first "len" chars. of "src" for search-metachars.
5883 * If dest is not NULL the chars. are copied there quoting (with
5884 * a backslash) the metachars, and dest would be NUL terminated.
5885 * Returns the length (needed) of dest
5886 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005887 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005888quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005890 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005892 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 {
5894 switch (*src)
5895 {
5896 case '.':
5897 case '*':
5898 case '[':
5899 if (ctrl_x_mode == CTRL_X_DICTIONARY
5900 || ctrl_x_mode == CTRL_X_THESAURUS)
5901 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005902 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 case '~':
5904 if (!p_magic) /* quote these only if magic is set */
5905 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005906 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 case '\\':
5908 if (ctrl_x_mode == CTRL_X_DICTIONARY
5909 || ctrl_x_mode == CTRL_X_THESAURUS)
5910 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005911 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 case '^': /* currently it's not needed. */
5913 case '$':
5914 m++;
5915 if (dest != NULL)
5916 *dest++ = '\\';
5917 break;
5918 }
5919 if (dest != NULL)
5920 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005921# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 /* Copy remaining bytes of a multibyte character. */
5923 if (has_mbyte)
5924 {
5925 int i, mb_len;
5926
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005927 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 if (mb_len > 0 && len >= mb_len)
5929 for (i = 0; i < mb_len; ++i)
5930 {
5931 --len;
5932 ++src;
5933 if (dest != NULL)
5934 *dest++ = *src;
5935 }
5936 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 }
5939 if (dest != NULL)
5940 *dest = NUL;
5941
5942 return m;
5943}
5944#endif /* FEAT_INS_EXPAND */
5945
5946/*
5947 * Next character is interpreted literally.
5948 * A one, two or three digit decimal number is interpreted as its byte value.
5949 * If one or two digits are entered, the next character is given to vungetc().
5950 * For Unicode a character > 255 may be returned.
5951 */
5952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005953get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
5955 int cc;
5956 int nc;
5957 int i;
5958 int hex = FALSE;
5959 int octal = FALSE;
5960#ifdef FEAT_MBYTE
5961 int unicode = 0;
5962#endif
5963
5964 if (got_int)
5965 return Ctrl_C;
5966
5967#ifdef FEAT_GUI
5968 /*
5969 * In GUI there is no point inserting the internal code for a special key.
5970 * It is more useful to insert the string "<KEY>" instead. This would
5971 * probably be useful in a text window too, but it would not be
5972 * vi-compatible (maybe there should be an option for it?) -- webb
5973 */
5974 if (gui.in_use)
5975 ++allow_keys;
5976#endif
5977#ifdef USE_ON_FLY_SCROLL
5978 dont_scroll = TRUE; /* disallow scrolling here */
5979#endif
5980 ++no_mapping; /* don't map the next key hits */
5981 cc = 0;
5982 i = 0;
5983 for (;;)
5984 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005985 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986#ifdef FEAT_CMDL_INFO
5987 if (!(State & CMDLINE)
5988# ifdef FEAT_MBYTE
5989 && MB_BYTE2LEN_CHECK(nc) == 1
5990# endif
5991 )
5992 add_to_showcmd(nc);
5993#endif
5994 if (nc == 'x' || nc == 'X')
5995 hex = TRUE;
5996 else if (nc == 'o' || nc == 'O')
5997 octal = TRUE;
5998#ifdef FEAT_MBYTE
5999 else if (nc == 'u' || nc == 'U')
6000 unicode = nc;
6001#endif
6002 else
6003 {
6004 if (hex
6005#ifdef FEAT_MBYTE
6006 || unicode != 0
6007#endif
6008 )
6009 {
6010 if (!vim_isxdigit(nc))
6011 break;
6012 cc = cc * 16 + hex2nr(nc);
6013 }
6014 else if (octal)
6015 {
6016 if (nc < '0' || nc > '7')
6017 break;
6018 cc = cc * 8 + nc - '0';
6019 }
6020 else
6021 {
6022 if (!VIM_ISDIGIT(nc))
6023 break;
6024 cc = cc * 10 + nc - '0';
6025 }
6026
6027 ++i;
6028 }
6029
6030 if (cc > 255
6031#ifdef FEAT_MBYTE
6032 && unicode == 0
6033#endif
6034 )
6035 cc = 255; /* limit range to 0-255 */
6036 nc = 0;
6037
6038 if (hex) /* hex: up to two chars */
6039 {
6040 if (i >= 2)
6041 break;
6042 }
6043#ifdef FEAT_MBYTE
6044 else if (unicode) /* Unicode: up to four or eight chars */
6045 {
6046 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6047 break;
6048 }
6049#endif
6050 else if (i >= 3) /* decimal or octal: up to three chars */
6051 break;
6052 }
6053 if (i == 0) /* no number entered */
6054 {
6055 if (nc == K_ZERO) /* NUL is stored as NL */
6056 {
6057 cc = '\n';
6058 nc = 0;
6059 }
6060 else
6061 {
6062 cc = nc;
6063 nc = 0;
6064 }
6065 }
6066
6067 if (cc == 0) /* NUL is stored as NL */
6068 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006069#ifdef FEAT_MBYTE
6070 if (enc_dbcs && (cc & 0xff) == 0)
6071 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6072 second byte will cause trouble! */
6073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
6075 --no_mapping;
6076#ifdef FEAT_GUI
6077 if (gui.in_use)
6078 --allow_keys;
6079#endif
6080 if (nc)
6081 vungetc(nc);
6082 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6083 return cc;
6084}
6085
6086/*
6087 * Insert character, taking care of special keys and mod_mask
6088 */
6089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006090insert_special(
6091 int c,
6092 int allow_modmask,
6093 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 char_u *p;
6096 int len;
6097
6098 /*
6099 * Special function key, translate into "<Key>". Up to the last '>' is
6100 * inserted with ins_str(), so as not to replace characters in replace
6101 * mode.
6102 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6103 * unless 'allow_modmask' is TRUE.
6104 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006105#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 /* Command-key never produces a normal key */
6107 if (mod_mask & MOD_MASK_CMD)
6108 allow_modmask = TRUE;
6109#endif
6110 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6111 {
6112 p = get_special_key_name(c, mod_mask);
6113 len = (int)STRLEN(p);
6114 c = p[len - 1];
6115 if (len > 2)
6116 {
6117 if (stop_arrow() == FAIL)
6118 return;
6119 p[len - 1] = NUL;
6120 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006121 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 ctrlv = FALSE;
6123 }
6124 }
6125 if (stop_arrow() == OK)
6126 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6127}
6128
6129/*
6130 * Special characters in this context are those that need processing other
6131 * than the simple insertion that can be performed here. This includes ESC
6132 * which terminates the insert, and CR/NL which need special processing to
6133 * open up a new line. This routine tries to optimize insertions performed by
6134 * the "redo", "undo" or "put" commands, so it needs to know when it should
6135 * stop and defer processing to the "normal" mechanism.
6136 * '0' and '^' are special, because they can be followed by CTRL-D.
6137 */
6138#ifdef EBCDIC
6139# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6140#else
6141# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6142#endif
6143
6144#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006145# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006147# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148#endif
6149
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006150/*
6151 * "flags": INSCHAR_FORMAT - force formatting
6152 * INSCHAR_CTRLV - char typed just after CTRL-V
6153 * INSCHAR_NO_FEX - don't use 'formatexpr'
6154 *
6155 * NOTE: passes the flags value straight through to internal_format() which,
6156 * beside INSCHAR_FORMAT (above), is also looking for these:
6157 * INSCHAR_DO_COM - format comments
6158 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006161insertchar(
6162 int c, /* character to insert or NUL */
6163 int flags, /* INSCHAR_FORMAT, etc. */
6164 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 int textwidth;
6167#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006171 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006173 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
6176 /*
6177 * Try to break the line in two or more pieces when:
6178 * - Always do this if we have been called to do formatting only.
6179 * - Always do this when 'formatoptions' has the 'a' flag and the line
6180 * ends in white space.
6181 * - Otherwise:
6182 * - Don't do this if inserting a blank
6183 * - Don't do this if an existing character is being replaced, unless
6184 * we're in VREPLACE mode.
6185 * - Do this if the cursor is not on the line where insert started
6186 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6187 * before the insert.
6188 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6189 * before 'textwidth'
6190 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006191 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006192 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006193 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 && !((State & REPLACE_FLAG)
6195#ifdef FEAT_VREPLACE
6196 && !(State & VREPLACE_FLAG)
6197#endif
6198 && *ml_get_cursor() != NUL)
6199 && (curwin->w_cursor.lnum != Insstart.lnum
6200 || ((!has_format_option(FO_INS_LONG)
6201 || Insstart_textlen <= (colnr_T)textwidth)
6202 && (!fo_ins_blank
6203 || Insstart_blank_vcol <= (colnr_T)textwidth
6204 ))))))
6205 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206 /* Format with 'formatexpr' when it's set. Use internal formatting
6207 * when 'formatexpr' isn't set or it returns non-zero. */
6208#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006209 int do_internal = TRUE;
6210 colnr_T virtcol = get_nolist_virtcol()
6211 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006212
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006213 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6214 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006215 {
6216 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6217 /* It may be required to save for undo again, e.g. when setline()
6218 * was called. */
6219 ins_need_undo = TRUE;
6220 }
6221 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006223 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 if (c == NUL) /* only formatting was wanted */
6227 return;
6228
6229#ifdef FEAT_COMMENTS
6230 /* Check whether this character should end a comment. */
6231 if (did_ai && (int)c == end_comment_pending)
6232 {
6233 char_u *line;
6234 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6235 int middle_len, end_len;
6236 int i;
6237
6238 /*
6239 * Need to remove existing (middle) comment leader and insert end
6240 * comment leader. First, check what comment leader we can find.
6241 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006242 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6244 {
6245 /* Skip middle-comment string */
6246 while (*p && p[-1] != ':') /* find end of middle flags */
6247 ++p;
6248 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6249 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006250 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 --middle_len;
6252
6253 /* Find the end-comment string */
6254 while (*p && p[-1] != ':') /* find end of end flags */
6255 ++p;
6256 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6257
6258 /* Skip white space before the cursor */
6259 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006260 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 ;
6262 i++;
6263
6264 /* Skip to before the middle leader */
6265 i -= middle_len;
6266
6267 /* Check some expected things before we go on */
6268 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6269 {
6270 /* Backspace over all the stuff we want to replace */
6271 backspace_until_column(i);
6272
6273 /*
6274 * Insert the end-comment string, except for the last
6275 * character, which will get inserted as normal later.
6276 */
6277 ins_bytes_len(lead_end, end_len - 1);
6278 }
6279 }
6280 }
6281 end_comment_pending = NUL;
6282#endif
6283
6284 did_ai = FALSE;
6285#ifdef FEAT_SMARTINDENT
6286 did_si = FALSE;
6287 can_si = FALSE;
6288 can_si_back = FALSE;
6289#endif
6290
6291 /*
6292 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6293 * This speeds up normal text input considerably.
6294 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6295 * need to re-indent at a ':', or any other character (but not what
6296 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006297 * Don't do this when there an InsertCharPre autocommand is defined,
6298 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006299 * Do the check for InsertCharPre before the call to vpeekc() because the
6300 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 */
6302#ifdef USE_ON_FLY_SCROLL
6303 dont_scroll = FALSE; /* allow scrolling here */
6304#endif
6305
6306 if ( !ISSPECIAL(c)
6307#ifdef FEAT_MBYTE
6308 && (!has_mbyte || (*mb_char2len)(c) == 1)
6309#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006310 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 && vpeekc() != NUL
6312 && !(State & REPLACE_FLAG)
6313#ifdef FEAT_CINDENT
6314 && !cindent_on()
6315#endif
6316#ifdef FEAT_RIGHTLEFT
6317 && !p_ri
6318#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006319 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 {
6321#define INPUT_BUFLEN 100
6322 char_u buf[INPUT_BUFLEN + 1];
6323 int i;
6324 colnr_T virtcol = 0;
6325
6326 buf[0] = c;
6327 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006328 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 virtcol = get_nolist_virtcol();
6330 /*
6331 * Stop the string when:
6332 * - no more chars available
6333 * - finding a special character (command key)
6334 * - buffer is full
6335 * - running into the 'textwidth' boundary
6336 * - need to check for abbreviation: A non-word char after a word-char
6337 */
6338 while ( (c = vpeekc()) != NUL
6339 && !ISSPECIAL(c)
6340#ifdef FEAT_MBYTE
6341 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6342#endif
6343 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006344# ifdef FEAT_FKMAP
6345 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 && (textwidth == 0
6348 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6349 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6350 {
6351#ifdef FEAT_RIGHTLEFT
6352 c = vgetc();
6353 if (p_hkmap && KeyTyped)
6354 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 buf[i++] = c;
6356#else
6357 buf[i++] = vgetc();
6358#endif
6359 }
6360
6361#ifdef FEAT_DIGRAPHS
6362 do_digraph(-1); /* clear digraphs */
6363 do_digraph(buf[i-1]); /* may be the start of a digraph */
6364#endif
6365 buf[i] = NUL;
6366 ins_str(buf);
6367 if (flags & INSCHAR_CTRLV)
6368 {
6369 redo_literal(*buf);
6370 i = 1;
6371 }
6372 else
6373 i = 0;
6374 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006375 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 }
6377 else
6378 {
6379#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006380 int cc;
6381
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6383 {
6384 char_u buf[MB_MAXBYTES + 1];
6385
6386 (*mb_char2bytes)(c, buf);
6387 buf[cc] = NUL;
6388 ins_char_bytes(buf, cc);
6389 AppendCharToRedobuff(c);
6390 }
6391 else
6392#endif
6393 {
6394 ins_char(c);
6395 if (flags & INSCHAR_CTRLV)
6396 redo_literal(c);
6397 else
6398 AppendCharToRedobuff(c);
6399 }
6400 }
6401}
6402
6403/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006404 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006405 *
6406 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6407 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006408 */
6409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006410internal_format(
6411 int textwidth,
6412 int second_indent,
6413 int flags,
6414 int format_only,
6415 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006416{
6417 int cc;
6418 int save_char = NUL;
6419 int haveto_redraw = FALSE;
6420 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6421#ifdef FEAT_MBYTE
6422 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6423#endif
6424 int fo_white_par = has_format_option(FO_WHITE_PAR);
6425 int first_line = TRUE;
6426#ifdef FEAT_COMMENTS
6427 colnr_T leader_len;
6428 int no_leader = FALSE;
6429 int do_comments = (flags & INSCHAR_DO_COM);
6430#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006431#ifdef FEAT_LINEBREAK
6432 int has_lbr = curwin->w_p_lbr;
6433
6434 /* make sure win_lbr_chartabsize() counts correctly */
6435 curwin->w_p_lbr = FALSE;
6436#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437
6438 /*
6439 * When 'ai' is off we don't want a space under the cursor to be
6440 * deleted. Replace it with an 'x' temporarily.
6441 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006442 if (!curbuf->b_p_ai
6443#ifdef FEAT_VREPLACE
6444 && !(State & VREPLACE_FLAG)
6445#endif
6446 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006447 {
6448 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006449 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006450 {
6451 save_char = cc;
6452 pchar_cursor('x');
6453 }
6454 }
6455
6456 /*
6457 * Repeat breaking lines, until the current line is not too long.
6458 */
6459 while (!got_int)
6460 {
6461 int startcol; /* Cursor column at entry */
6462 int wantcol; /* column at textwidth border */
6463 int foundcol; /* column for start of spaces */
6464 int end_foundcol = 0; /* column for start of word */
6465 colnr_T len;
6466 colnr_T virtcol;
6467#ifdef FEAT_VREPLACE
6468 int orig_col = 0;
6469 char_u *saved_text = NULL;
6470#endif
6471 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006472 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006473
Bram Moolenaar97b98102009-11-17 16:41:01 +00006474 virtcol = get_nolist_virtcol()
6475 + char2cells(c != NUL ? c : gchar_cursor());
6476 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477 break;
6478
6479#ifdef FEAT_COMMENTS
6480 if (no_leader)
6481 do_comments = FALSE;
6482 else if (!(flags & INSCHAR_FORMAT)
6483 && has_format_option(FO_WRAP_COMS))
6484 do_comments = TRUE;
6485
6486 /* Don't break until after the comment leader */
6487 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006488 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006489 else
6490 leader_len = 0;
6491
6492 /* If the line doesn't start with a comment leader, then don't
6493 * start one in a following broken line. Avoids that a %word
6494 * moved to the start of the next line causes all following lines
6495 * to start with %. */
6496 if (leader_len == 0)
6497 no_leader = TRUE;
6498#endif
6499 if (!(flags & INSCHAR_FORMAT)
6500#ifdef FEAT_COMMENTS
6501 && leader_len == 0
6502#endif
6503 && !has_format_option(FO_WRAP))
6504
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006505 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006506 if ((startcol = curwin->w_cursor.col) == 0)
6507 break;
6508
6509 /* find column of textwidth border */
6510 coladvance((colnr_T)textwidth);
6511 wantcol = curwin->w_cursor.col;
6512
Bram Moolenaar97b98102009-11-17 16:41:01 +00006513 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514 foundcol = 0;
6515
6516 /*
6517 * Find position to break at.
6518 * Stop at first entered white when 'formatoptions' has 'v'
6519 */
6520 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006521 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006522 || curwin->w_cursor.lnum != Insstart.lnum
6523 || curwin->w_cursor.col >= Insstart.col)
6524 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006525 if (curwin->w_cursor.col == startcol && c != NUL)
6526 cc = c;
6527 else
6528 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006529 if (WHITECHAR(cc))
6530 {
6531 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006532 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006533
6534 /* find start of sequence of blanks */
6535 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6536 {
6537 dec_cursor();
6538 cc = gchar_cursor();
6539 }
6540 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6541 break; /* only spaces in front of text */
6542#ifdef FEAT_COMMENTS
6543 /* Don't break until after the comment leader */
6544 if (curwin->w_cursor.col < leader_len)
6545 break;
6546#endif
6547 if (has_format_option(FO_ONE_LETTER))
6548 {
6549 /* do not break after one-letter words */
6550 if (curwin->w_cursor.col == 0)
6551 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006552#ifdef FEAT_COMMENTS
6553 /* do not break "#a b" when 'tw' is 2 */
6554 if (curwin->w_cursor.col <= leader_len)
6555 break;
6556#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006557 col = curwin->w_cursor.col;
6558 dec_cursor();
6559 cc = gchar_cursor();
6560
6561 if (WHITECHAR(cc))
6562 continue; /* one-letter, continue */
6563 curwin->w_cursor.col = col;
6564 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006565
6566 inc_cursor();
6567
6568 end_foundcol = end_col + 1;
6569 foundcol = curwin->w_cursor.col;
6570 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006571 break;
6572 }
6573#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006574 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006575 {
6576 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006577 if (curwin->w_cursor.col != startcol)
6578 {
6579#ifdef FEAT_COMMENTS
6580 /* Don't break until after the comment leader */
6581 if (curwin->w_cursor.col < leader_len)
6582 break;
6583#endif
6584 col = curwin->w_cursor.col;
6585 inc_cursor();
6586 /* Don't change end_foundcol if already set. */
6587 if (foundcol != curwin->w_cursor.col)
6588 {
6589 foundcol = curwin->w_cursor.col;
6590 end_foundcol = foundcol;
6591 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6592 break;
6593 }
6594 curwin->w_cursor.col = col;
6595 }
6596
6597 if (curwin->w_cursor.col == 0)
6598 break;
6599
6600 col = curwin->w_cursor.col;
6601
6602 dec_cursor();
6603 cc = gchar_cursor();
6604
6605 if (WHITECHAR(cc))
6606 continue; /* break with space */
6607#ifdef FEAT_COMMENTS
6608 /* Don't break until after the comment leader */
6609 if (curwin->w_cursor.col < leader_len)
6610 break;
6611#endif
6612
6613 curwin->w_cursor.col = col;
6614
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006615 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006616 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006617 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6618 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006619 }
6620#endif
6621 if (curwin->w_cursor.col == 0)
6622 break;
6623 dec_cursor();
6624 }
6625
6626 if (foundcol == 0) /* no spaces, cannot break line */
6627 {
6628 curwin->w_cursor.col = startcol;
6629 break;
6630 }
6631
6632 /* Going to break the line, remove any "$" now. */
6633 undisplay_dollar();
6634
6635 /*
6636 * Offset between cursor position and line break is used by replace
6637 * stack functions. VREPLACE does not use this, and backspaces
6638 * over the text instead.
6639 */
6640#ifdef FEAT_VREPLACE
6641 if (State & VREPLACE_FLAG)
6642 orig_col = startcol; /* Will start backspacing from here */
6643 else
6644#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006645 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006646
6647 /*
6648 * adjust startcol for spaces that will be deleted and
6649 * characters that will remain on top line
6650 */
6651 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006652 while ((cc = gchar_cursor(), WHITECHAR(cc))
6653 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006654 inc_cursor();
6655 startcol -= curwin->w_cursor.col;
6656 if (startcol < 0)
6657 startcol = 0;
6658
6659#ifdef FEAT_VREPLACE
6660 if (State & VREPLACE_FLAG)
6661 {
6662 /*
6663 * In VREPLACE mode, we will backspace over the text to be
6664 * wrapped, so save a copy now to put on the next line.
6665 */
6666 saved_text = vim_strsave(ml_get_cursor());
6667 curwin->w_cursor.col = orig_col;
6668 if (saved_text == NULL)
6669 break; /* Can't do it, out of memory */
6670 saved_text[startcol] = NUL;
6671
6672 /* Backspace over characters that will move to the next line */
6673 if (!fo_white_par)
6674 backspace_until_column(foundcol);
6675 }
6676 else
6677#endif
6678 {
6679 /* put cursor after pos. to break line */
6680 if (!fo_white_par)
6681 curwin->w_cursor.col = foundcol;
6682 }
6683
6684 /*
6685 * Split the line just before the margin.
6686 * Only insert/delete lines, but don't really redraw the window.
6687 */
6688 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6689 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6690#ifdef FEAT_COMMENTS
6691 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006692 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006693#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006694 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6695 if (!(flags & INSCHAR_COM_LIST))
6696 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006697
6698 replace_offset = 0;
6699 if (first_line)
6700 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006701 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006703 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006704 * This section is for auto-wrap of numeric lists. When not
6705 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6706 * flag will be set and open_line() will handle it (as seen
6707 * above). The code here (and in get_number_indent()) will
6708 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006709 */
6710 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006711 second_indent =
6712 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006713 if (second_indent >= 0)
6714 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006715#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006716 if (State & VREPLACE_FLAG)
6717 change_indent(INDENT_SET, second_indent,
6718 FALSE, NUL, TRUE);
6719 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006720#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006721#ifdef FEAT_COMMENTS
6722 if (leader_len > 0 && second_indent - leader_len > 0)
6723 {
6724 int i;
6725 int padding = second_indent - leader_len;
6726
6727 /* We started at the first_line of a numbered list
6728 * that has a comment. the open_line() function has
6729 * inserted the proper comment leader and positioned
6730 * the cursor at the end of the split line. Now we
6731 * add the additional whitespace needed after the
6732 * comment leader for the numbered list. */
6733 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006734 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006735 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006736 }
6737 else
6738 {
6739#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006740 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006741#ifdef FEAT_COMMENTS
6742 }
6743#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006744 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006745 }
6746 first_line = FALSE;
6747 }
6748
6749#ifdef FEAT_VREPLACE
6750 if (State & VREPLACE_FLAG)
6751 {
6752 /*
6753 * In VREPLACE mode we have backspaced over the text to be
6754 * moved, now we re-insert it into the new line.
6755 */
6756 ins_bytes(saved_text);
6757 vim_free(saved_text);
6758 }
6759 else
6760#endif
6761 {
6762 /*
6763 * Check if cursor is not past the NUL off the line, cindent
6764 * may have added or removed indent.
6765 */
6766 curwin->w_cursor.col += startcol;
6767 len = (colnr_T)STRLEN(ml_get_curline());
6768 if (curwin->w_cursor.col > len)
6769 curwin->w_cursor.col = len;
6770 }
6771
6772 haveto_redraw = TRUE;
6773#ifdef FEAT_CINDENT
6774 can_cindent = TRUE;
6775#endif
6776 /* moved the cursor, don't autoindent or cindent now */
6777 did_ai = FALSE;
6778#ifdef FEAT_SMARTINDENT
6779 did_si = FALSE;
6780 can_si = FALSE;
6781 can_si_back = FALSE;
6782#endif
6783 line_breakcheck();
6784 }
6785
6786 if (save_char != NUL) /* put back space after cursor */
6787 pchar_cursor(save_char);
6788
Bram Moolenaar0026d472014-09-09 16:32:39 +02006789#ifdef FEAT_LINEBREAK
6790 curwin->w_p_lbr = has_lbr;
6791#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006792 if (!format_only && haveto_redraw)
6793 {
6794 update_topline();
6795 redraw_curbuf_later(VALID);
6796 }
6797}
6798
6799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 * Called after inserting or deleting text: When 'formatoptions' includes the
6801 * 'a' flag format from the current line until the end of the paragraph.
6802 * Keep the cursor at the same position relative to the text.
6803 * The caller must have saved the cursor line for undo, following ones will be
6804 * saved here.
6805 */
6806 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006807auto_format(
6808 int trailblank, /* when TRUE also format with trailing blank */
6809 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810{
6811 pos_T pos;
6812 colnr_T len;
6813 char_u *old;
6814 char_u *new, *pnew;
6815 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006816 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818 if (!has_format_option(FO_AUTO))
6819 return;
6820
6821 pos = curwin->w_cursor;
6822 old = ml_get_curline();
6823
6824 /* may remove added space */
6825 check_auto_format(FALSE);
6826
6827 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6828 * user might insert normal text next. Also skip formatting when "1" is
6829 * in 'formatoptions' and there is a single character before the cursor.
6830 * Otherwise the line would be broken and when typing another non-white
6831 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006832 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 if (*old != NUL && !trailblank && wasatend)
6834 {
6835 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006836 cc = gchar_cursor();
6837 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6838 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006840 cc = gchar_cursor();
6841 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {
6843 curwin->w_cursor = pos;
6844 return;
6845 }
6846 curwin->w_cursor = pos;
6847 }
6848
6849#ifdef FEAT_COMMENTS
6850 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6851 * comments. */
6852 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006853 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 return;
6855#endif
6856
6857 /*
6858 * May start formatting in a previous line, so that after "x" a word is
6859 * moved to the previous line if it fits there now. Only when this is not
6860 * the start of a paragraph.
6861 */
6862 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6863 {
6864 --curwin->w_cursor.lnum;
6865 if (u_save_cursor() == FAIL)
6866 return;
6867 }
6868
6869 /*
6870 * Do the formatting and restore the cursor position. "saved_cursor" will
6871 * be adjusted for the text formatting.
6872 */
6873 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006874 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 curwin->w_cursor = saved_cursor;
6876 saved_cursor.lnum = 0;
6877
6878 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6879 {
6880 /* "cannot happen" */
6881 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6882 coladvance((colnr_T)MAXCOL);
6883 }
6884 else
6885 check_cursor_col();
6886
6887 /* Insert mode: If the cursor is now after the end of the line while it
6888 * previously wasn't, the line was broken. Because of the rule above we
6889 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6890 * formatted. */
6891 if (!wasatend && has_format_option(FO_WHITE_PAR))
6892 {
6893 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006894 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (curwin->w_cursor.col == len)
6896 {
6897 pnew = vim_strnsave(new, len + 2);
6898 pnew[len] = ' ';
6899 pnew[len + 1] = NUL;
6900 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6901 /* remove the space later */
6902 did_add_space = TRUE;
6903 }
6904 else
6905 /* may remove added space */
6906 check_auto_format(FALSE);
6907 }
6908
6909 check_cursor();
6910}
6911
6912/*
6913 * When an extra space was added to continue a paragraph for auto-formatting,
6914 * delete it now. The space must be under the cursor, just after the insert
6915 * position.
6916 */
6917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006918check_auto_format(
6919 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006922 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
6924 if (did_add_space)
6925 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006926 cc = gchar_cursor();
6927 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 /* Somehow the space was removed already. */
6929 did_add_space = FALSE;
6930 else
6931 {
6932 if (!end_insert)
6933 {
6934 inc_cursor();
6935 c = gchar_cursor();
6936 dec_cursor();
6937 }
6938 if (c != NUL)
6939 {
6940 /* The space is no longer at the end of the line, delete it. */
6941 del_char(FALSE);
6942 did_add_space = FALSE;
6943 }
6944 }
6945 }
6946}
6947
6948/*
6949 * Find out textwidth to be used for formatting:
6950 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006951 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 * if invalid value, use 0.
6953 * Set default to window width (maximum 79) for "gq" operator.
6954 */
6955 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006956comp_textwidth(
6957 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958{
6959 int textwidth;
6960
6961 textwidth = curbuf->b_p_tw;
6962 if (textwidth == 0 && curbuf->b_p_wm)
6963 {
6964 /* The width is the window width minus 'wrapmargin' minus all the
6965 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006966 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967#ifdef FEAT_CMDWIN
6968 if (cmdwin_type != 0)
6969 textwidth -= 1;
6970#endif
6971#ifdef FEAT_FOLDING
6972 textwidth -= curwin->w_p_fdc;
6973#endif
6974#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006975 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 textwidth -= 1;
6977#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006978 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 textwidth -= 8;
6980 }
6981 if (textwidth < 0)
6982 textwidth = 0;
6983 if (ff && textwidth == 0)
6984 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006985 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (textwidth > 79)
6987 textwidth = 79;
6988 }
6989 return textwidth;
6990}
6991
6992/*
6993 * Put a character in the redo buffer, for when just after a CTRL-V.
6994 */
6995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006996redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 char_u buf[10];
6999
7000 /* Only digits need special treatment. Translate them into a string of
7001 * three digits. */
7002 if (VIM_ISDIGIT(c))
7003 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007004 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 AppendToRedobuff(buf);
7006 }
7007 else
7008 AppendCharToRedobuff(c);
7009}
7010
7011/*
7012 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007013 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 */
7015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007016start_arrow(
7017 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007019 start_arrow_common(end_insert_pos, TRUE);
7020}
7021
7022/*
7023 * Like start_arrow() but with end_change argument.
7024 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7025 */
7026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007027start_arrow_with_change(
7028 pos_T *end_insert_pos, /* can be NULL */
7029 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007030{
7031 start_arrow_common(end_insert_pos, end_change);
7032 if (!end_change)
7033 {
7034 AppendCharToRedobuff(Ctrl_G);
7035 AppendCharToRedobuff('U');
7036 }
7037}
7038
7039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007040start_arrow_common(
7041 pos_T *end_insert_pos, /* can be NULL */
7042 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007043{
7044 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {
7046 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007047 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 arrow_used = TRUE; /* this means we stopped the current insert */
7049 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007050#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007051 check_spell_redraw();
7052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053}
7054
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007055#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007056/*
7057 * If we skipped highlighting word at cursor, do it now.
7058 * It may be skipped again, thus reset spell_redraw_lnum first.
7059 */
7060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007061check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007062{
7063 if (spell_redraw_lnum != 0)
7064 {
7065 linenr_T lnum = spell_redraw_lnum;
7066
7067 spell_redraw_lnum = 0;
7068 redrawWinline(lnum, FALSE);
7069 }
7070}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007071
7072/*
7073 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7074 * spelled word, if there is one.
7075 */
7076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007077spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007078{
7079 pos_T tpos = curwin->w_cursor;
7080
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007081 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007082 if (curwin->w_cursor.col != tpos.col)
7083 start_arrow(&tpos);
7084}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007085#endif
7086
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087/*
7088 * stop_arrow() is called before a change is made in insert mode.
7089 * If an arrow key has been used, start a new insertion.
7090 * Returns FAIL if undo is impossible, shouldn't insert then.
7091 */
7092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007093stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
7095 if (arrow_used)
7096 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007097 Insstart = curwin->w_cursor; /* new insertion starts here */
7098 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7099 /* Don't update the original insert position when moved to the
7100 * right, except when nothing was inserted yet. */
7101 update_Insstart_orig = FALSE;
7102 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7103
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 if (u_save_cursor() == OK)
7105 {
7106 arrow_used = FALSE;
7107 ins_need_undo = FALSE;
7108 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007109
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 ai_col = 0;
7111#ifdef FEAT_VREPLACE
7112 if (State & VREPLACE_FLAG)
7113 {
7114 orig_line_count = curbuf->b_ml.ml_line_count;
7115 vr_lines_changed = 1;
7116 }
7117#endif
7118 ResetRedobuff();
7119 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007120 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 }
7122 else if (ins_need_undo)
7123 {
7124 if (u_save_cursor() == OK)
7125 ins_need_undo = FALSE;
7126 }
7127
7128#ifdef FEAT_FOLDING
7129 /* Always open fold at the cursor line when inserting something. */
7130 foldOpenCursor();
7131#endif
7132
7133 return (arrow_used || ins_need_undo ? FAIL : OK);
7134}
7135
7136/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007137 * Do a few things to stop inserting.
7138 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7139 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 */
7141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007142stop_insert(
7143 pos_T *end_insert_pos,
7144 int esc, /* called by ins_esc() */
7145 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007147 int cc;
7148 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
7150 stop_redo_ins();
7151 replace_flush(); /* abandon replace stack */
7152
7153 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007154 * Save the inserted text for later redo with ^@ and CTRL-A.
7155 * Don't do it when "restart_edit" was set and nothing was inserted,
7156 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007158 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007159 if (did_restart_edit == 0 || (ptr != NULL
7160 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007161 {
7162 vim_free(last_insert);
7163 last_insert = ptr;
7164 last_insert_skip = new_insert_skip;
7165 }
7166 else
7167 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007169 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {
7171 /* Auto-format now. It may seem strange to do this when stopping an
7172 * insertion (or moving the cursor), but it's required when appending
7173 * a line and having it end in a space. But only do it when something
7174 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007175 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007177 pos_T tpos = curwin->w_cursor;
7178
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 /* When the cursor is at the end of the line after a space the
7180 * formatting will move it to the following word. Avoid that by
7181 * moving the cursor onto the space. */
7182 cc = 'x';
7183 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7184 {
7185 dec_cursor();
7186 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007187 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007188 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 }
7190
7191 auto_format(TRUE, FALSE);
7192
Bram Moolenaar1c465442017-03-12 20:10:05 +01007193 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007194 {
7195 if (gchar_cursor() != NUL)
7196 inc_cursor();
7197#ifdef FEAT_VIRTUALEDIT
7198 /* If the cursor is still at the same character, also keep
7199 * the "coladd". */
7200 if (gchar_cursor() == NUL
7201 && curwin->w_cursor.lnum == tpos.lnum
7202 && curwin->w_cursor.col == tpos.col)
7203 curwin->w_cursor.coladd = tpos.coladd;
7204#endif
7205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
7207
7208 /* If a space was inserted for auto-formatting, remove it now. */
7209 check_auto_format(TRUE);
7210
7211 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007212 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007213 * Do this when ESC was used or moving the cursor up/down.
7214 * Check for the old position still being valid, just in case the text
7215 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007216 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007217 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7218 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007220 pos_T tpos = curwin->w_cursor;
7221
7222 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007223 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007224 for (;;)
7225 {
7226 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7227 --curwin->w_cursor.col;
7228 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007229 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007230 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007231 if (del_char(TRUE) == FAIL)
7232 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007233 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007234 if (curwin->w_cursor.lnum != tpos.lnum)
7235 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007236 else
7237 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007238 /* reset tpos, could have been invalidated in the loop above */
7239 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007240 tpos.col++;
7241 if (cc != NUL && gchar_pos(&tpos) == NUL)
7242 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 /* <C-S-Right> may have started Visual mode, adjust the position for
7246 * deleted characters. */
7247 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7248 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007249 int len = (int)STRLEN(ml_get_curline());
7250
7251 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007253 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007254#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
7258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260 }
7261 did_ai = FALSE;
7262#ifdef FEAT_SMARTINDENT
7263 did_si = FALSE;
7264 can_si = FALSE;
7265 can_si_back = FALSE;
7266#endif
7267
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007268 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7269 * now in a different buffer. */
7270 if (end_insert_pos != NULL)
7271 {
7272 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007273 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007274 curbuf->b_op_end = *end_insert_pos;
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276}
7277
7278/*
7279 * Set the last inserted text to a single character.
7280 * Used for the replace command.
7281 */
7282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007283set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
7285 char_u *s;
7286
7287 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 if (last_insert != NULL)
7290 {
7291 s = last_insert;
7292 /* Use the CTRL-V only when entering a special char */
7293 if (c < ' ' || c == DEL)
7294 *s++ = Ctrl_V;
7295 s = add_char2buf(c, s);
7296 *s++ = ESC;
7297 *s++ = NUL;
7298 last_insert_skip = 0;
7299 }
7300}
7301
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007302#if defined(EXITFREE) || defined(PROTO)
7303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007304free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007305{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007306 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007307# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007308 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007309# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007310}
7311#endif
7312
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313/*
7314 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7315 * and CSI. Handle multi-byte characters.
7316 * Returns a pointer to after the added bytes.
7317 */
7318 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007319add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007322 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 int i;
7324 int len;
7325
7326 len = (*mb_char2bytes)(c, temp);
7327 for (i = 0; i < len; ++i)
7328 {
7329 c = temp[i];
7330#endif
7331 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7332 if (c == K_SPECIAL)
7333 {
7334 *s++ = K_SPECIAL;
7335 *s++ = KS_SPECIAL;
7336 *s++ = KE_FILLER;
7337 }
7338#ifdef FEAT_GUI
7339 else if (c == CSI)
7340 {
7341 *s++ = CSI;
7342 *s++ = KS_EXTRA;
7343 *s++ = (int)KE_CSI;
7344 }
7345#endif
7346 else
7347 *s++ = c;
7348#ifdef FEAT_MBYTE
7349 }
7350#endif
7351 return s;
7352}
7353
7354/*
7355 * move cursor to start of line
7356 * if flags & BL_WHITE move to first non-white
7357 * if flags & BL_SOL move to first non-white if startofline is set,
7358 * otherwise keep "curswant" column
7359 * if flags & BL_FIX don't leave the cursor on a NUL.
7360 */
7361 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007362beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
7364 if ((flags & BL_SOL) && !p_sol)
7365 coladvance(curwin->w_curswant);
7366 else
7367 {
7368 curwin->w_cursor.col = 0;
7369#ifdef FEAT_VIRTUALEDIT
7370 curwin->w_cursor.coladd = 0;
7371#endif
7372
7373 if (flags & (BL_WHITE | BL_SOL))
7374 {
7375 char_u *ptr;
7376
Bram Moolenaar1c465442017-03-12 20:10:05 +01007377 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7379 ++curwin->w_cursor.col;
7380 }
7381 curwin->w_set_curswant = TRUE;
7382 }
7383}
7384
7385/*
7386 * oneright oneleft cursor_down cursor_up
7387 *
7388 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007389 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 * Return OK when successful, FAIL when we hit a line of file boundary.
7391 */
7392
7393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007394oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
7399#ifdef FEAT_VIRTUALEDIT
7400 if (virtual_active())
7401 {
7402 pos_T prevpos = curwin->w_cursor;
7403
7404 /* Adjust for multi-wide char (excluding TAB) */
7405 ptr = ml_get_cursor();
7406 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007407# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007409# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 ))
7413 ? ptr2cells(ptr) : 1));
7414 curwin->w_set_curswant = TRUE;
7415 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7416 return (prevpos.col != curwin->w_cursor.col
7417 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7418 }
7419#endif
7420
7421 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007422 if (*ptr == NUL)
7423 return FAIL; /* already at the very end */
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007426 if (has_mbyte)
7427 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 else
7429#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007430 l = 1;
7431
7432 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7433 * contains "onemore". */
7434 if (ptr[l] == NUL
7435#ifdef FEAT_VIRTUALEDIT
7436 && (ve_flags & VE_ONEMORE) == 0
7437#endif
7438 )
7439 return FAIL;
7440 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441
7442 curwin->w_set_curswant = TRUE;
7443 return OK;
7444}
7445
7446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007447oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449#ifdef FEAT_VIRTUALEDIT
7450 if (virtual_active())
7451 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007452# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 int v = getviscol();
7456
7457 if (v == 0)
7458 return FAIL;
7459
7460# ifdef FEAT_LINEBREAK
7461 /* We might get stuck on 'showbreak', skip over it. */
7462 width = 1;
7463 for (;;)
7464 {
7465 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007466 /* getviscol() is slow, skip it when 'showbreak' is empty,
7467 * 'breakindent' is not set and there are no multi-byte
7468 * characters */
7469 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470# ifdef FEAT_MBYTE
7471 && !has_mbyte
7472# endif
7473 ) || getviscol() < v)
7474 break;
7475 ++width;
7476 }
7477# else
7478 coladvance(v - 1);
7479# endif
7480
7481 if (curwin->w_cursor.coladd == 1)
7482 {
7483 char_u *ptr;
7484
7485 /* Adjust for multi-wide char (not a TAB) */
7486 ptr = ml_get_cursor();
7487 if (*ptr != TAB && vim_isprintc(
7488# ifdef FEAT_MBYTE
7489 (*mb_ptr2char)(ptr)
7490# else
7491 *ptr
7492# endif
7493 ) && ptr2cells(ptr) > 1)
7494 curwin->w_cursor.coladd = 0;
7495 }
7496
7497 curwin->w_set_curswant = TRUE;
7498 return OK;
7499 }
7500#endif
7501
7502 if (curwin->w_cursor.col == 0)
7503 return FAIL;
7504
7505 curwin->w_set_curswant = TRUE;
7506 --curwin->w_cursor.col;
7507
7508#ifdef FEAT_MBYTE
7509 /* if the character on the left of the current cursor is a multi-byte
7510 * character, move to its first byte */
7511 if (has_mbyte)
7512 mb_adjust_cursor();
7513#endif
7514 return OK;
7515}
7516
7517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007518cursor_up(
7519 long n,
7520 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
7522 linenr_T lnum;
7523
7524 if (n > 0)
7525 {
7526 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007527 /* This fails if the cursor is already in the first line or the count
7528 * is larger than the line number and '-' is in 'cpoptions' */
7529 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 return FAIL;
7531 if (n >= lnum)
7532 lnum = 1;
7533 else
7534#ifdef FEAT_FOLDING
7535 if (hasAnyFolding(curwin))
7536 {
7537 /*
7538 * Count each sequence of folded lines as one logical line.
7539 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007540 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 (void)hasFolding(lnum, &lnum, NULL);
7542
7543 while (n--)
7544 {
7545 /* move up one line */
7546 --lnum;
7547 if (lnum <= 1)
7548 break;
7549 /* If we entered a fold, move to the beginning, unless in
7550 * Insert mode or when 'foldopen' contains "all": it will open
7551 * in a moment. */
7552 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7553 (void)hasFolding(lnum, &lnum, NULL);
7554 }
7555 if (lnum < 1)
7556 lnum = 1;
7557 }
7558 else
7559#endif
7560 lnum -= n;
7561 curwin->w_cursor.lnum = lnum;
7562 }
7563
7564 /* try to advance to the column we want to be at */
7565 coladvance(curwin->w_curswant);
7566
7567 if (upd_topline)
7568 update_topline(); /* make sure curwin->w_topline is valid */
7569
7570 return OK;
7571}
7572
7573/*
7574 * Cursor down a number of logical lines.
7575 */
7576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007577cursor_down(
7578 long n,
7579 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
7581 linenr_T lnum;
7582
7583 if (n > 0)
7584 {
7585 lnum = curwin->w_cursor.lnum;
7586#ifdef FEAT_FOLDING
7587 /* Move to last line of fold, will fail if it's the end-of-file. */
7588 (void)hasFolding(lnum, NULL, &lnum);
7589#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007590 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007591 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007592 if (lnum >= curbuf->b_ml.ml_line_count
7593 || (lnum + n > curbuf->b_ml.ml_line_count
7594 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 return FAIL;
7596 if (lnum + n >= curbuf->b_ml.ml_line_count)
7597 lnum = curbuf->b_ml.ml_line_count;
7598 else
7599#ifdef FEAT_FOLDING
7600 if (hasAnyFolding(curwin))
7601 {
7602 linenr_T last;
7603
7604 /* count each sequence of folded lines as one logical line */
7605 while (n--)
7606 {
7607 if (hasFolding(lnum, NULL, &last))
7608 lnum = last + 1;
7609 else
7610 ++lnum;
7611 if (lnum >= curbuf->b_ml.ml_line_count)
7612 break;
7613 }
7614 if (lnum > curbuf->b_ml.ml_line_count)
7615 lnum = curbuf->b_ml.ml_line_count;
7616 }
7617 else
7618#endif
7619 lnum += n;
7620 curwin->w_cursor.lnum = lnum;
7621 }
7622
7623 /* try to advance to the column we want to be at */
7624 coladvance(curwin->w_curswant);
7625
7626 if (upd_topline)
7627 update_topline(); /* make sure curwin->w_topline is valid */
7628
7629 return OK;
7630}
7631
7632/*
7633 * Stuff the last inserted text in the read buffer.
7634 * Last_insert actually is a copy of the redo buffer, so we
7635 * first have to remove the command.
7636 */
7637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007638stuff_inserted(
7639 int c, /* Command character to be inserted */
7640 long count, /* Repeat this many times */
7641 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
7643 char_u *esc_ptr;
7644 char_u *ptr;
7645 char_u *last_ptr;
7646 char_u last = NUL;
7647
7648 ptr = get_last_insert();
7649 if (ptr == NULL)
7650 {
7651 EMSG(_(e_noinstext));
7652 return FAIL;
7653 }
7654
7655 /* may want to stuff the command character, to start Insert mode */
7656 if (c != NUL)
7657 stuffcharReadbuff(c);
7658 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7659 *esc_ptr = NUL; /* remove the ESC */
7660
7661 /* when the last char is either "0" or "^" it will be quoted if no ESC
7662 * comes after it OR if it will inserted more than once and "ptr"
7663 * starts with ^D. -- Acevedo
7664 */
7665 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7666 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7667 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7668 {
7669 last = *last_ptr;
7670 *last_ptr = NUL;
7671 }
7672
7673 do
7674 {
7675 stuffReadbuff(ptr);
7676 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7677 if (last)
7678 stuffReadbuff((char_u *)(last == '0'
7679 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7680 : IF_EB("\026^", CTRL_V_STR "^")));
7681 }
7682 while (--count > 0);
7683
7684 if (last)
7685 *last_ptr = last;
7686
7687 if (esc_ptr != NULL)
7688 *esc_ptr = ESC; /* put the ESC back */
7689
7690 /* may want to stuff a trailing ESC, to get out of Insert mode */
7691 if (!no_esc)
7692 stuffcharReadbuff(ESC);
7693
7694 return OK;
7695}
7696
7697 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007698get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 if (last_insert == NULL)
7701 return NULL;
7702 return last_insert + last_insert_skip;
7703}
7704
7705/*
7706 * Get last inserted string, and remove trailing <Esc>.
7707 * Returns pointer to allocated memory (must be freed) or NULL.
7708 */
7709 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007710get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
7712 char_u *s;
7713 int len;
7714
7715 if (last_insert == NULL)
7716 return NULL;
7717 s = vim_strsave(last_insert + last_insert_skip);
7718 if (s != NULL)
7719 {
7720 len = (int)STRLEN(s);
7721 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7722 s[len - 1] = NUL;
7723 }
7724 return s;
7725}
7726
7727/*
7728 * Check the word in front of the cursor for an abbreviation.
7729 * Called when the non-id character "c" has been entered.
7730 * When an abbreviation is recognized it is removed from the text and
7731 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7732 */
7733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007734echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
7736 /* Don't check for abbreviation in paste mode, when disabled and just
7737 * after moving around with cursor keys. */
7738 if (p_paste || no_abbr || arrow_used)
7739 return FALSE;
7740
7741 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7742 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7743}
7744
7745/*
7746 * replace-stack functions
7747 *
7748 * When replacing characters, the replaced characters are remembered for each
7749 * new character. This is used to re-insert the old text when backspacing.
7750 *
7751 * There is a NUL headed list of characters for each character that is
7752 * currently in the file after the insertion point. When BS is used, one NUL
7753 * headed list is put back for the deleted character.
7754 *
7755 * For a newline, there are two NUL headed lists. One contains the characters
7756 * that the NL replaced. The extra one stores the characters after the cursor
7757 * that were deleted (always white space).
7758 *
7759 * Replace_offset is normally 0, in which case replace_push will add a new
7760 * character at the end of the stack. If replace_offset is not 0, that many
7761 * characters will be left on the stack above the newly inserted character.
7762 */
7763
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007764static char_u *replace_stack = NULL;
7765static long replace_stack_nr = 0; /* next entry in replace stack */
7766static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
7768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007769replace_push(
7770 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
7772 char_u *p;
7773
7774 if (replace_stack_nr < replace_offset) /* nothing to do */
7775 return;
7776 if (replace_stack_len <= replace_stack_nr)
7777 {
7778 replace_stack_len += 50;
7779 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7780 if (p == NULL) /* out of memory */
7781 {
7782 replace_stack_len -= 50;
7783 return;
7784 }
7785 if (replace_stack != NULL)
7786 {
7787 mch_memmove(p, replace_stack,
7788 (size_t)(replace_stack_nr * sizeof(char_u)));
7789 vim_free(replace_stack);
7790 }
7791 replace_stack = p;
7792 }
7793 p = replace_stack + replace_stack_nr - replace_offset;
7794 if (replace_offset)
7795 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7796 *p = c;
7797 ++replace_stack_nr;
7798}
7799
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007800#if defined(FEAT_MBYTE) || defined(PROTO)
7801/*
7802 * Push a character onto the replace stack. Handles a multi-byte character in
7803 * reverse byte order, so that the first byte is popped off first.
7804 * Return the number of bytes done (includes composing characters).
7805 */
7806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007807replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007808{
7809 int l = (*mb_ptr2len)(p);
7810 int j;
7811
7812 for (j = l - 1; j >= 0; --j)
7813 replace_push(p[j]);
7814 return l;
7815}
7816#endif
7817
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818/*
7819 * Pop one item from the replace stack.
7820 * return -1 if stack empty
7821 * return replaced character or NUL otherwise
7822 */
7823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007824replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 if (replace_stack_nr == 0)
7827 return -1;
7828 return (int)replace_stack[--replace_stack_nr];
7829}
7830
7831/*
7832 * Join the top two items on the replace stack. This removes to "off"'th NUL
7833 * encountered.
7834 */
7835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007836replace_join(
7837 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 int i;
7840
7841 for (i = replace_stack_nr; --i >= 0; )
7842 if (replace_stack[i] == NUL && off-- <= 0)
7843 {
7844 --replace_stack_nr;
7845 mch_memmove(replace_stack + i, replace_stack + i + 1,
7846 (size_t)(replace_stack_nr - i));
7847 return;
7848 }
7849}
7850
7851/*
7852 * Pop bytes from the replace stack until a NUL is found, and insert them
7853 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7854 */
7855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007856replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858 int cc;
7859 int oldState = State;
7860
7861 State = NORMAL; /* don't want REPLACE here */
7862 while ((cc = replace_pop()) > 0)
7863 {
7864#ifdef FEAT_MBYTE
7865 mb_replace_pop_ins(cc);
7866#else
7867 ins_char(cc);
7868#endif
7869 dec_cursor();
7870 }
7871 State = oldState;
7872}
7873
7874#ifdef FEAT_MBYTE
7875/*
7876 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7877 * indicates a multi-byte char, pop the other bytes too.
7878 */
7879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007880mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007883 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 int i;
7885 int c;
7886
7887 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7888 {
7889 buf[0] = cc;
7890 for (i = 1; i < n; ++i)
7891 buf[i] = replace_pop();
7892 ins_bytes_len(buf, n);
7893 }
7894 else
7895 ins_char(cc);
7896
7897 if (enc_utf8)
7898 /* Handle composing chars. */
7899 for (;;)
7900 {
7901 c = replace_pop();
7902 if (c == -1) /* stack empty */
7903 break;
7904 if ((n = MB_BYTE2LEN(c)) == 1)
7905 {
7906 /* Not a multi-byte char, put it back. */
7907 replace_push(c);
7908 break;
7909 }
7910 else
7911 {
7912 buf[0] = c;
7913 for (i = 1; i < n; ++i)
7914 buf[i] = replace_pop();
7915 if (utf_iscomposing(utf_ptr2char(buf)))
7916 ins_bytes_len(buf, n);
7917 else
7918 {
7919 /* Not a composing char, put it back. */
7920 for (i = n - 1; i >= 0; --i)
7921 replace_push(buf[i]);
7922 break;
7923 }
7924 }
7925 }
7926}
7927#endif
7928
7929/*
7930 * make the replace stack empty
7931 * (called when exiting replace mode)
7932 */
7933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007934replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007936 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 replace_stack_len = 0;
7938 replace_stack_nr = 0;
7939}
7940
7941/*
7942 * Handle doing a BS for one character.
7943 * cc < 0: replace stack empty, just move cursor
7944 * cc == 0: character was inserted, delete it
7945 * cc > 0: character was replaced, put cc (first byte of original char) back
7946 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007947 * When "limit_col" is >= 0, don't delete before this column. Matters when
7948 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 */
7950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007951replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
7953 int cc;
7954#ifdef FEAT_VREPLACE
7955 int orig_len = 0;
7956 int ins_len;
7957 int orig_vcols = 0;
7958 colnr_T start_vcol;
7959 char_u *p;
7960 int i;
7961 int vcol;
7962#endif
7963
7964 cc = replace_pop();
7965 if (cc > 0)
7966 {
7967#ifdef FEAT_VREPLACE
7968 if (State & VREPLACE_FLAG)
7969 {
7970 /* Get the number of screen cells used by the character we are
7971 * going to delete. */
7972 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7973 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7974 }
7975#endif
7976#ifdef FEAT_MBYTE
7977 if (has_mbyte)
7978 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007979 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980# ifdef FEAT_VREPLACE
7981 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007982 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983# endif
7984 replace_push(cc);
7985 }
7986 else
7987#endif
7988 {
7989 pchar_cursor(cc);
7990#ifdef FEAT_VREPLACE
7991 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007992 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993#endif
7994 }
7995 replace_pop_ins();
7996
7997#ifdef FEAT_VREPLACE
7998 if (State & VREPLACE_FLAG)
7999 {
8000 /* Get the number of screen cells used by the inserted characters */
8001 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008002 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 vcol = start_vcol;
8004 for (i = 0; i < ins_len; ++i)
8005 {
8006 vcol += chartabsize(p + i, vcol);
8007#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008008 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009#endif
8010 }
8011 vcol -= start_vcol;
8012
8013 /* Delete spaces that were inserted after the cursor to keep the
8014 * text aligned. */
8015 curwin->w_cursor.col += ins_len;
8016 while (vcol > orig_vcols && gchar_cursor() == ' ')
8017 {
8018 del_char(FALSE);
8019 ++orig_vcols;
8020 }
8021 curwin->w_cursor.col -= ins_len;
8022 }
8023#endif
8024
8025 /* mark the buffer as changed and prepare for displaying */
8026 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8027 }
8028 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008029 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030}
8031
8032#ifdef FEAT_CINDENT
8033/*
8034 * Return TRUE if C-indenting is on.
8035 */
8036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008037cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038{
8039 return (!p_paste && (curbuf->b_p_cin
8040# ifdef FEAT_EVAL
8041 || *curbuf->b_p_inde != NUL
8042# endif
8043 ));
8044}
8045#endif
8046
8047#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8048/*
8049 * Re-indent the current line, based on the current contents of it and the
8050 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8051 * confused what all the part that handles Control-T is doing that I'm not.
8052 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8053 */
8054
8055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008056fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008058 int amount = get_the_indent();
8059
8060 if (amount >= 0)
8061 {
8062 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8063 if (linewhite(curwin->w_cursor.lnum))
8064 did_ai = TRUE; /* delete the indent if the line stays empty */
8065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066}
8067
8068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008069fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070{
8071 if (p_paste)
8072 return;
8073# ifdef FEAT_LISP
8074 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8075 fixthisline(get_lisp_indent);
8076# endif
8077# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8078 else
8079# endif
8080# ifdef FEAT_CINDENT
8081 if (cindent_on())
8082 do_c_expr_indent();
8083# endif
8084}
8085
8086#endif
8087
8088#ifdef FEAT_CINDENT
8089/*
8090 * return TRUE if 'cinkeys' contains the key "keytyped",
8091 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008092 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8094 *
8095 * "keytyped" can have a few special values:
8096 * KEY_OPEN_FORW
8097 * KEY_OPEN_BACK
8098 * KEY_COMPLETE just finished completion.
8099 *
8100 * If line_is_empty is TRUE accept keys with '0' before them.
8101 */
8102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008103in_cinkeys(
8104 int keytyped,
8105 int when,
8106 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 char_u *look;
8109 int try_match;
8110 int try_match_word;
8111 char_u *p;
8112 char_u *line;
8113 int icase;
8114 int i;
8115
Bram Moolenaar30848942009-12-24 14:46:12 +00008116 if (keytyped == NUL)
8117 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8118 return FALSE;
8119
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120#ifdef FEAT_EVAL
8121 if (*curbuf->b_p_inde != NUL)
8122 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8123 else
8124#endif
8125 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8126 while (*look)
8127 {
8128 /*
8129 * Find out if we want to try a match with this key, depending on
8130 * 'when' and a '*' or '!' before the key.
8131 */
8132 switch (when)
8133 {
8134 case '*': try_match = (*look == '*'); break;
8135 case '!': try_match = (*look == '!'); break;
8136 default: try_match = (*look != '*'); break;
8137 }
8138 if (*look == '*' || *look == '!')
8139 ++look;
8140
8141 /*
8142 * If there is a '0', only accept a match if the line is empty.
8143 * But may still match when typing last char of a word.
8144 */
8145 if (*look == '0')
8146 {
8147 try_match_word = try_match;
8148 if (!line_is_empty)
8149 try_match = FALSE;
8150 ++look;
8151 }
8152 else
8153 try_match_word = FALSE;
8154
8155 /*
8156 * does it look like a control character?
8157 */
8158 if (*look == '^'
8159#ifdef EBCDIC
8160 && (Ctrl_chr(look[1]) != 0)
8161#else
8162 && look[1] >= '?' && look[1] <= '_'
8163#endif
8164 )
8165 {
8166 if (try_match && keytyped == Ctrl_chr(look[1]))
8167 return TRUE;
8168 look += 2;
8169 }
8170 /*
8171 * 'o' means "o" command, open forward.
8172 * 'O' means "O" command, open backward.
8173 */
8174 else if (*look == 'o')
8175 {
8176 if (try_match && keytyped == KEY_OPEN_FORW)
8177 return TRUE;
8178 ++look;
8179 }
8180 else if (*look == 'O')
8181 {
8182 if (try_match && keytyped == KEY_OPEN_BACK)
8183 return TRUE;
8184 ++look;
8185 }
8186
8187 /*
8188 * 'e' means to check for "else" at start of line and just before the
8189 * cursor.
8190 */
8191 else if (*look == 'e')
8192 {
8193 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8194 {
8195 p = ml_get_curline();
8196 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8197 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8198 return TRUE;
8199 }
8200 ++look;
8201 }
8202
8203 /*
8204 * ':' only causes an indent if it is at the end of a label or case
8205 * statement, or when it was before typing the ':' (to fix
8206 * class::method for C++).
8207 */
8208 else if (*look == ':')
8209 {
8210 if (try_match && keytyped == ':')
8211 {
8212 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008213 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008215 /* Need to get the line again after cin_islabel(). */
8216 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 if (curwin->w_cursor.col > 2
8218 && p[curwin->w_cursor.col - 1] == ':'
8219 && p[curwin->w_cursor.col - 2] == ':')
8220 {
8221 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008222 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008223 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 p = ml_get_curline();
8225 p[curwin->w_cursor.col - 1] = ':';
8226 if (i)
8227 return TRUE;
8228 }
8229 }
8230 ++look;
8231 }
8232
8233
8234 /*
8235 * Is it a key in <>, maybe?
8236 */
8237 else if (*look == '<')
8238 {
8239 if (try_match)
8240 {
8241 /*
8242 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8243 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8244 * >, *, : and ! keys if they really really want to.
8245 */
8246 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8247 && keytyped == look[1])
8248 return TRUE;
8249
8250 if (keytyped == get_special_key_code(look + 1))
8251 return TRUE;
8252 }
8253 while (*look && *look != '>')
8254 look++;
8255 while (*look == '>')
8256 look++;
8257 }
8258
8259 /*
8260 * Is it a word: "=word"?
8261 */
8262 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8263 {
8264 ++look;
8265 if (*look == '~')
8266 {
8267 icase = TRUE;
8268 ++look;
8269 }
8270 else
8271 icase = FALSE;
8272 p = vim_strchr(look, ',');
8273 if (p == NULL)
8274 p = look + STRLEN(look);
8275 if ((try_match || try_match_word)
8276 && curwin->w_cursor.col >= (colnr_T)(p - look))
8277 {
8278 int match = FALSE;
8279
8280#ifdef FEAT_INS_EXPAND
8281 if (keytyped == KEY_COMPLETE)
8282 {
8283 char_u *s;
8284
8285 /* Just completed a word, check if it starts with "look".
8286 * search back for the start of a word. */
8287 line = ml_get_curline();
8288# ifdef FEAT_MBYTE
8289 if (has_mbyte)
8290 {
8291 char_u *n;
8292
8293 for (s = line + curwin->w_cursor.col; s > line; s = n)
8294 {
8295 n = mb_prevptr(line, s);
8296 if (!vim_iswordp(n))
8297 break;
8298 }
8299 }
8300 else
8301# endif
8302 for (s = line + curwin->w_cursor.col; s > line; --s)
8303 if (!vim_iswordc(s[-1]))
8304 break;
8305 if (s + (p - look) <= line + curwin->w_cursor.col
8306 && (icase
8307 ? MB_STRNICMP(s, look, p - look)
8308 : STRNCMP(s, look, p - look)) == 0)
8309 match = TRUE;
8310 }
8311 else
8312#endif
8313 /* TODO: multi-byte */
8314 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8315 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8316 {
8317 line = ml_get_cursor();
8318 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8319 || !vim_iswordc(line[-(p - look) - 1]))
8320 && (icase
8321 ? MB_STRNICMP(line - (p - look), look, p - look)
8322 : STRNCMP(line - (p - look), look, p - look))
8323 == 0)
8324 match = TRUE;
8325 }
8326 if (match && try_match_word && !try_match)
8327 {
8328 /* "0=word": Check if there are only blanks before the
8329 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008330 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 (int)(curwin->w_cursor.col - (p - look)))
8332 match = FALSE;
8333 }
8334 if (match)
8335 return TRUE;
8336 }
8337 look = p;
8338 }
8339
8340 /*
8341 * ok, it's a boring generic character.
8342 */
8343 else
8344 {
8345 if (try_match && *look == keytyped)
8346 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008347 if (*look != NUL)
8348 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 }
8350
8351 /*
8352 * Skip over ", ".
8353 */
8354 look = skip_to_option_part(look);
8355 }
8356 return FALSE;
8357}
8358#endif /* FEAT_CINDENT */
8359
8360#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8361/*
8362 * Map Hebrew keyboard when in hkmap mode.
8363 */
8364 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008365hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
8367 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8368 {
8369 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8370 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8371 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8372 static char_u map[26] =
8373 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8374 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8375 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8376 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8377 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8378 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8379 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8380 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8381 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8382
8383 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8384 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8385 /* '-1'='sofit' */
8386 else if (c == 'x')
8387 return 'X';
8388 else if (c == 'q')
8389 return '\''; /* {geresh}={'} */
8390 else if (c == 246)
8391 return ' '; /* \"o --> ' ' for a german keyboard */
8392 else if (c == 228)
8393 return ' '; /* \"a --> ' ' -- / -- */
8394 else if (c == 252)
8395 return ' '; /* \"u --> ' ' -- / -- */
8396#ifdef EBCDIC
8397 else if (islower(c))
8398#else
8399 /* NOTE: islower() does not do the right thing for us on Linux so we
8400 * do this the same was as 5.7 and previous, so it works correctly on
8401 * all systems. Specifically, the e.g. Delete and Arrow keys are
8402 * munged and won't work if e.g. searching for Hebrew text.
8403 */
8404 else if (c >= 'a' && c <= 'z')
8405#endif
8406 return (int)(map[CharOrdLow(c)] + p_aleph);
8407 else
8408 return c;
8409 }
8410 else
8411 {
8412 switch (c)
8413 {
8414 case '`': return ';';
8415 case '/': return '.';
8416 case '\'': return ',';
8417 case 'q': return '/';
8418 case 'w': return '\'';
8419
8420 /* Hebrew letters - set offset from 'a' */
8421 case ',': c = '{'; break;
8422 case '.': c = 'v'; break;
8423 case ';': c = 't'; break;
8424 default: {
8425 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8426
8427#ifdef EBCDIC
8428 /* see note about islower() above */
8429 if (!islower(c))
8430#else
8431 if (c < 'a' || c > 'z')
8432#endif
8433 return c;
8434 c = str[CharOrdLow(c)];
8435 break;
8436 }
8437 }
8438
8439 return (int)(CharOrdLow(c) + p_aleph);
8440 }
8441}
8442#endif
8443
8444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008445ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446{
8447 int need_redraw = FALSE;
8448 int regname;
8449 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008450 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451
8452 /*
8453 * If we are going to wait for a character, show a '"'.
8454 */
8455 pc_status = PC_STATUS_UNSET;
8456 if (redrawing() && !char_avail())
8457 {
8458 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008459 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460
8461 edit_putchar('"', TRUE);
8462#ifdef FEAT_CMDL_INFO
8463 add_to_showcmd_c(Ctrl_R);
8464#endif
8465 }
8466
8467#ifdef USE_ON_FLY_SCROLL
8468 dont_scroll = TRUE; /* disallow scrolling here */
8469#endif
8470
8471 /*
8472 * Don't map the register name. This also prevents the mode message to be
8473 * deleted when ESC is hit.
8474 */
8475 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008476 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8479 {
8480 /* Get a third key for literal register insertion */
8481 literally = regname;
8482#ifdef FEAT_CMDL_INFO
8483 add_to_showcmd_c(literally);
8484#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008485 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488 --no_mapping;
8489
8490#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008491 /* Don't call u_sync() while typing the expression or giving an error
8492 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 ++no_u_sync;
8494 if (regname == '=')
8495 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008496# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008498# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008499 /* Sync undo when evaluating the expression calls setline() or
8500 * append(), so that it can be undone separately. */
8501 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008502
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008504# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 /* Restore the Input Method. */
8506 if (im_on)
8507 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008510 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8511 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008512 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 else
8516 {
8517#endif
8518 if (literally == Ctrl_O || literally == Ctrl_P)
8519 {
8520 /* Append the command to the redo buffer. */
8521 AppendCharToRedobuff(Ctrl_R);
8522 AppendCharToRedobuff(literally);
8523 AppendCharToRedobuff(regname);
8524
8525 do_put(regname, BACKWARD, 1L,
8526 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8527 }
8528 else if (insert_reg(regname, literally) == FAIL)
8529 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008530 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 need_redraw = TRUE; /* remove the '"' */
8532 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008533 else if (stop_insert_mode)
8534 /* When the '=' register was used and a function was invoked that
8535 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8536 * insert anything, need to remove the '"' */
8537 need_redraw = TRUE;
8538
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539#ifdef FEAT_EVAL
8540 }
8541 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008542 if (u_sync_once == 1)
8543 ins_need_undo = TRUE;
8544 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545#endif
8546#ifdef FEAT_CMDL_INFO
8547 clear_showcmd();
8548#endif
8549
8550 /* If the inserted register is empty, we need to remove the '"' */
8551 if (need_redraw || stuff_empty())
8552 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008553
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008554 /* Disallow starting Visual mode here, would get a weird mode. */
8555 if (!vis_active && VIsual_active)
8556 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557}
8558
8559/*
8560 * CTRL-G commands in Insert mode.
8561 */
8562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008563ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 int c;
8566
8567#ifdef FEAT_INS_EXPAND
8568 /* Right after CTRL-X the cursor will be after the ruler. */
8569 setcursor();
8570#endif
8571
8572 /*
8573 * Don't map the second key. This also prevents the mode message to be
8574 * deleted when ESC is hit.
8575 */
8576 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008577 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 --no_mapping;
8579 switch (c)
8580 {
8581 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8582 case K_UP:
8583 case Ctrl_K:
8584 case 'k': ins_up(TRUE);
8585 break;
8586
8587 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8588 case K_DOWN:
8589 case Ctrl_J:
8590 case 'j': ins_down(TRUE);
8591 break;
8592
8593 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008594 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008596
8597 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008598 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008599 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008600 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 break;
8602
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008603 /* CTRL-G U: do not break undo with the next char */
8604 case 'U':
8605 /* Allow one left/right cursor movement with the next char,
8606 * without breaking undo. */
8607 dont_sync_undo = MAYBE;
8608 break;
8609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008611 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613}
8614
8615/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008616 * CTRL-^ in Insert mode.
8617 */
8618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008619ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008620{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008621 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008622 {
8623 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8624 if (State & LANGMAP)
8625 {
8626 curbuf->b_p_iminsert = B_IMODE_NONE;
8627 State &= ~LANGMAP;
8628 }
8629 else
8630 {
8631 curbuf->b_p_iminsert = B_IMODE_LMAP;
8632 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008633#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008634 im_set_active(FALSE);
8635#endif
8636 }
8637 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008638#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008639 else
8640 {
8641 /* There are no ":lmap" mappings, toggle IM */
8642 if (im_get_status())
8643 {
8644 curbuf->b_p_iminsert = B_IMODE_NONE;
8645 im_set_active(FALSE);
8646 }
8647 else
8648 {
8649 curbuf->b_p_iminsert = B_IMODE_IM;
8650 State &= ~LANGMAP;
8651 im_set_active(TRUE);
8652 }
8653 }
8654#endif
8655 set_iminsert_global();
8656 showmode();
8657#ifdef FEAT_GUI
8658 /* may show different cursor shape or color */
8659 if (gui.in_use)
8660 gui_update_cursor(TRUE, FALSE);
8661#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008662#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008663 /* Show/unshow value of 'keymap' in status lines. */
8664 status_redraw_curbuf();
8665#endif
8666}
8667
8668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 * Handle ESC in insert mode.
8670 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8671 * insert.
8672 */
8673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008674ins_esc(
8675 long *count,
8676 int cmdchar,
8677 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
8679 int temp;
8680 static int disabled_redraw = FALSE;
8681
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008682#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008683 check_spell_redraw();
8684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685#if defined(FEAT_HANGULIN)
8686# if defined(ESC_CHG_TO_ENG_MODE)
8687 hangul_input_state_set(0);
8688# endif
8689 if (composing_hangul)
8690 {
8691 push_raw_key(composing_hangul_buffer, 2);
8692 composing_hangul = 0;
8693 }
8694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695
8696 temp = curwin->w_cursor.col;
8697 if (disabled_redraw)
8698 {
8699 --RedrawingDisabled;
8700 disabled_redraw = FALSE;
8701 }
8702 if (!arrow_used)
8703 {
8704 /*
8705 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008706 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8707 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 */
8709 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008710 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711
8712 /*
8713 * Repeating insert may take a long time. Check for
8714 * interrupt now and then.
8715 */
8716 if (*count > 0)
8717 {
8718 line_breakcheck();
8719 if (got_int)
8720 *count = 0;
8721 }
8722
8723 if (--*count > 0) /* repeat what was typed */
8724 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008725 /* Vi repeats the insert without replacing characters. */
8726 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8727 State &= ~REPLACE_FLAG;
8728
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 (void)start_redo_ins();
8730 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008731 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 ++RedrawingDisabled;
8733 disabled_redraw = TRUE;
8734 return FALSE; /* repeat the insert */
8735 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008736 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 undisplay_dollar();
8738 }
8739
8740 /* When an autoindent was removed, curswant stays after the
8741 * indent */
8742 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8743 curwin->w_set_curswant = TRUE;
8744
8745 /* Remember the last Insert position in the '^ mark. */
8746 if (!cmdmod.keepjumps)
8747 curbuf->b_last_insert = curwin->w_cursor;
8748
8749 /*
8750 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008751 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008753 if (!nomove
8754 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755#ifdef FEAT_VIRTUALEDIT
8756 || curwin->w_cursor.coladd > 0
8757#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008758 )
8759 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008760 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761#ifdef FEAT_RIGHTLEFT
8762 && !revins_on
8763#endif
8764 )
8765 {
8766#ifdef FEAT_VIRTUALEDIT
8767 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8768 {
8769 oneleft();
8770 if (restart_edit != NUL)
8771 ++curwin->w_cursor.coladd;
8772 }
8773 else
8774#endif
8775 {
8776 --curwin->w_cursor.col;
8777#ifdef FEAT_MBYTE
8778 /* Correct cursor for multi-byte character. */
8779 if (has_mbyte)
8780 mb_adjust_cursor();
8781#endif
8782 }
8783 }
8784
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008785#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 /* Disable IM to allow typing English directly for Normal mode commands.
8787 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8788 * well). */
8789 if (!(State & LANGMAP))
8790 im_save_status(&curbuf->b_p_iminsert);
8791 im_set_active(FALSE);
8792#endif
8793
8794 State = NORMAL;
8795 /* need to position cursor again (e.g. when on a TAB ) */
8796 changed_cline_bef_curs();
8797
8798#ifdef FEAT_MOUSE
8799 setmouse();
8800#endif
8801#ifdef CURSOR_SHAPE
8802 ui_cursor_shape(); /* may show different cursor shape */
8803#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008804 if (!p_ek)
8805 /* Re-enable bracketed paste mode. */
8806 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
8808 /*
8809 * When recording or for CTRL-O, need to display the new mode.
8810 * Otherwise remove the mode message.
8811 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008812 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 showmode();
8814 else if (p_smd)
8815 MSG("");
8816
8817 return TRUE; /* exit Insert mode */
8818}
8819
8820#ifdef FEAT_RIGHTLEFT
8821/*
8822 * Toggle language: hkmap and revins_on.
8823 * Move to end of reverse inserted text.
8824 */
8825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008826ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
8828 if (revins_on && revins_chars && revins_scol >= 0)
8829 {
8830 while (gchar_cursor() != NUL && revins_chars--)
8831 ++curwin->w_cursor.col;
8832 }
8833 p_ri = !p_ri;
8834 revins_on = (State == INSERT && p_ri);
8835 if (revins_on)
8836 {
8837 revins_scol = curwin->w_cursor.col;
8838 revins_legal++;
8839 revins_chars = 0;
8840 undisplay_dollar();
8841 }
8842 else
8843 revins_scol = -1;
8844#ifdef FEAT_FKMAP
8845 if (p_altkeymap)
8846 {
8847 /*
8848 * to be consistent also for redo command, using '.'
8849 * set arrow_used to true and stop it - causing to redo
8850 * characters entered in one mode (normal/reverse insert).
8851 */
8852 arrow_used = TRUE;
8853 (void)stop_arrow();
8854 p_fkmap = curwin->w_p_rl ^ p_ri;
8855 if (p_fkmap && p_ri)
8856 State = INSERT;
8857 }
8858 else
8859#endif
8860 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8861 showmode();
8862}
8863#endif
8864
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865/*
8866 * If 'keymodel' contains "startsel", may start selection.
8867 * Returns TRUE when a CTRL-O and other keys stuffed.
8868 */
8869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008870ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872 if (km_startsel)
8873 switch (c)
8874 {
8875 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 case K_PAGEUP:
8878 case K_KPAGEUP:
8879 case K_PAGEDOWN:
8880 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008881# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 case K_LEFT:
8883 case K_RIGHT:
8884 case K_UP:
8885 case K_DOWN:
8886 case K_END:
8887 case K_HOME:
8888# endif
8889 if (!(mod_mask & MOD_MASK_SHIFT))
8890 break;
8891 /* FALLTHROUGH */
8892 case K_S_LEFT:
8893 case K_S_RIGHT:
8894 case K_S_UP:
8895 case K_S_DOWN:
8896 case K_S_END:
8897 case K_S_HOME:
8898 /* Start selection right away, the cursor can move with
8899 * CTRL-O when beyond the end of the line. */
8900 start_selection();
8901
8902 /* Execute the key in (insert) Select mode. */
8903 stuffcharReadbuff(Ctrl_O);
8904 if (mod_mask)
8905 {
8906 char_u buf[4];
8907
8908 buf[0] = K_SPECIAL;
8909 buf[1] = KS_MODIFIER;
8910 buf[2] = mod_mask;
8911 buf[3] = NUL;
8912 stuffReadbuff(buf);
8913 }
8914 stuffcharReadbuff(c);
8915 return TRUE;
8916 }
8917 return FALSE;
8918}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919
8920/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008921 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008922 */
8923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008924ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008925{
8926#ifdef FEAT_FKMAP
8927 if (p_fkmap && p_ri)
8928 {
8929 beep_flush();
8930 EMSG(farsi_text_3); /* encoded in Farsi */
8931 return;
8932 }
8933#endif
8934
Bram Moolenaar1e015462005-09-25 22:16:38 +00008935# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008936 set_vim_var_string(VV_INSERTMODE,
8937 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008938# ifdef FEAT_VREPLACE
8939 replaceState == VREPLACE ? "v" :
8940# endif
8941 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008942# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008943 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008944 if (State & REPLACE_FLAG)
8945 State = INSERT | (State & LANGMAP);
8946 else
8947 State = replaceState | (State & LANGMAP);
8948 AppendCharToRedobuff(K_INS);
8949 showmode();
8950#ifdef CURSOR_SHAPE
8951 ui_cursor_shape(); /* may show different cursor shape */
8952#endif
8953}
8954
8955/*
8956 * Pressed CTRL-O in Insert mode.
8957 */
8958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008959ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008960{
8961#ifdef FEAT_VREPLACE
8962 if (State & VREPLACE_FLAG)
8963 restart_edit = 'V';
8964 else
8965#endif
8966 if (State & REPLACE_FLAG)
8967 restart_edit = 'R';
8968 else
8969 restart_edit = 'I';
8970#ifdef FEAT_VIRTUALEDIT
8971 if (virtual_active())
8972 ins_at_eol = FALSE; /* cursor always keeps its column */
8973 else
8974#endif
8975 ins_at_eol = (gchar_cursor() == NUL);
8976}
8977
8978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 * If the cursor is on an indent, ^T/^D insert/delete one
8980 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008981 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 * with vi. But vi only supports ^T and ^D after an
8983 * autoindent, we support it everywhere.
8984 */
8985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008986ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988 if (stop_arrow() == FAIL)
8989 return;
8990 AppendCharToRedobuff(c);
8991
8992 /*
8993 * 0^D and ^^D: remove all indent.
8994 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008995 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8996 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 {
8998 --curwin->w_cursor.col;
8999 (void)del_char(FALSE); /* delete the '^' or '0' */
9000 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9001 if (State & REPLACE_FLAG)
9002 replace_pop_ins();
9003 if (lastc == '^')
9004 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009005 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 }
9007 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009008 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009
9010 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9011 did_ai = FALSE;
9012#ifdef FEAT_SMARTINDENT
9013 did_si = FALSE;
9014 can_si = FALSE;
9015 can_si_back = FALSE;
9016#endif
9017#ifdef FEAT_CINDENT
9018 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9019#endif
9020}
9021
9022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009023ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
9025 int temp;
9026
9027 if (stop_arrow() == FAIL)
9028 return;
9029 if (gchar_cursor() == NUL) /* delete newline */
9030 {
9031 temp = curwin->w_cursor.col;
9032 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009033 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009034 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009038#ifdef FEAT_VREPLACE
9039 /* Adjust orig_line_count in case more lines have been deleted than
9040 * have been added. That makes sure, that open_line() later
9041 * can access all buffer lines correctly */
9042 if (State & VREPLACE_FLAG &&
9043 orig_line_count > curbuf->b_ml.ml_line_count)
9044 orig_line_count = curbuf->b_ml.ml_line_count;
9045#endif
9046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009048 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9049 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 did_ai = FALSE;
9051#ifdef FEAT_SMARTINDENT
9052 did_si = FALSE;
9053 can_si = FALSE;
9054 can_si_back = FALSE;
9055#endif
9056 AppendCharToRedobuff(K_DEL);
9057}
9058
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009059static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009060
9061/*
9062 * Delete one character for ins_bs().
9063 */
9064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009065ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009066{
9067 dec_cursor();
9068 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9069 if (State & REPLACE_FLAG)
9070 {
9071 /* Don't delete characters before the insert point when in
9072 * Replace mode */
9073 if (curwin->w_cursor.lnum != Insstart.lnum
9074 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009075 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009076 }
9077 else
9078 (void)del_char(FALSE);
9079}
9080
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081/*
9082 * Handle Backspace, delete-word and delete-line in Insert mode.
9083 * Return TRUE when backspace was actually used.
9084 */
9085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009086ins_bs(
9087 int c,
9088 int mode,
9089 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090{
9091 linenr_T lnum;
9092 int cc;
9093 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009094 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 colnr_T mincol;
9096 int did_backspace = FALSE;
9097 int in_indent;
9098 int oldState;
9099#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009100 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101#endif
9102
9103 /*
9104 * can't delete anything in an empty file
9105 * can't backup past first character in buffer
9106 * can't backup past starting point unless 'backspace' > 1
9107 * can backup to a previous line if 'backspace' == 0
9108 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009109 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 || (
9111#ifdef FEAT_RIGHTLEFT
9112 !revins_on &&
9113#endif
9114 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9115 || (!can_bs(BS_START)
9116 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009117 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9118 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9120 && curwin->w_cursor.col <= ai_col)
9121 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9122 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009123 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 return FALSE;
9125 }
9126
9127 if (stop_arrow() == FAIL)
9128 return FALSE;
9129 in_indent = inindent(0);
9130#ifdef FEAT_CINDENT
9131 if (in_indent)
9132 can_cindent = FALSE;
9133#endif
9134#ifdef FEAT_COMMENTS
9135 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9136#endif
9137#ifdef FEAT_RIGHTLEFT
9138 if (revins_on) /* put cursor after last inserted char */
9139 inc_cursor();
9140#endif
9141
9142#ifdef FEAT_VIRTUALEDIT
9143 /* Virtualedit:
9144 * BACKSPACE_CHAR eats a virtual space
9145 * BACKSPACE_WORD eats all coladd
9146 * BACKSPACE_LINE eats all coladd and keeps going
9147 */
9148 if (curwin->w_cursor.coladd > 0)
9149 {
9150 if (mode == BACKSPACE_CHAR)
9151 {
9152 --curwin->w_cursor.coladd;
9153 return TRUE;
9154 }
9155 if (mode == BACKSPACE_WORD)
9156 {
9157 curwin->w_cursor.coladd = 0;
9158 return TRUE;
9159 }
9160 curwin->w_cursor.coladd = 0;
9161 }
9162#endif
9163
9164 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009165 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 */
9167 if (curwin->w_cursor.col == 0)
9168 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009169 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009170 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171#ifdef FEAT_RIGHTLEFT
9172 || revins_on
9173#endif
9174 )
9175 {
9176 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9177 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9178 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009179 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009180 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 }
9182 /*
9183 * In replace mode:
9184 * cc < 0: NL was inserted, delete it
9185 * cc >= 0: NL was replaced, put original characters back
9186 */
9187 cc = -1;
9188 if (State & REPLACE_FLAG)
9189 cc = replace_pop(); /* returns -1 if NL was inserted */
9190 /*
9191 * In replace mode, in the line we started replacing, we only move the
9192 * cursor.
9193 */
9194 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9195 {
9196 dec_cursor();
9197 }
9198 else
9199 {
9200#ifdef FEAT_VREPLACE
9201 if (!(State & VREPLACE_FLAG)
9202 || curwin->w_cursor.lnum > orig_line_count)
9203#endif
9204 {
9205 temp = gchar_cursor(); /* remember current char */
9206 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009207
9208 /* When "aw" is in 'formatoptions' we must delete the space at
9209 * the end of the line, otherwise the line will be broken
9210 * again when auto-formatting. */
9211 if (has_format_option(FO_AUTO)
9212 && has_format_option(FO_WHITE_PAR))
9213 {
9214 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9215 TRUE);
9216 int len;
9217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009218 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009219 if (len > 0 && ptr[len - 1] == ' ')
9220 ptr[len - 1] = NUL;
9221 }
9222
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009223 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 if (temp == NUL && gchar_cursor() != NUL)
9225 inc_cursor();
9226 }
9227#ifdef FEAT_VREPLACE
9228 else
9229 dec_cursor();
9230#endif
9231
9232 /*
9233 * In REPLACE mode we have to put back the text that was replaced
9234 * by the NL. On the replace stack is first a NUL-terminated
9235 * sequence of characters that were deleted and then the
9236 * characters that NL replaced.
9237 */
9238 if (State & REPLACE_FLAG)
9239 {
9240 /*
9241 * Do the next ins_char() in NORMAL state, to
9242 * prevent ins_char() from replacing characters and
9243 * avoiding showmatch().
9244 */
9245 oldState = State;
9246 State = NORMAL;
9247 /*
9248 * restore characters (blanks) deleted after cursor
9249 */
9250 while (cc > 0)
9251 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009252 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253#ifdef FEAT_MBYTE
9254 mb_replace_pop_ins(cc);
9255#else
9256 ins_char(cc);
9257#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009258 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 cc = replace_pop();
9260 }
9261 /* restore the characters that NL replaced */
9262 replace_pop_ins();
9263 State = oldState;
9264 }
9265 }
9266 did_ai = FALSE;
9267 }
9268 else
9269 {
9270 /*
9271 * Delete character(s) before the cursor.
9272 */
9273#ifdef FEAT_RIGHTLEFT
9274 if (revins_on) /* put cursor on last inserted char */
9275 dec_cursor();
9276#endif
9277 mincol = 0;
9278 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009279 if (mode == BACKSPACE_LINE
9280 && (curbuf->b_p_ai
9281#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009282 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009283#endif
9284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285#ifdef FEAT_RIGHTLEFT
9286 && !revins_on
9287#endif
9288 )
9289 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009290 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009292 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009294 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 }
9296
9297 /*
9298 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9299 */
9300 if ( mode == BACKSPACE_CHAR
9301 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009302 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009303 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 && (*(ml_get_cursor() - 1) == TAB
9305 || (*(ml_get_cursor() - 1) == ' '
9306 && (!*inserted_space_p
9307 || arrow_used))))))
9308 {
9309 int ts;
9310 colnr_T vcol;
9311 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009312 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313
9314 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009315 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009316 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009318 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 /* Compute the virtual column where we want to be. Since
9320 * 'showbreak' may get in the way, need to get the last column of
9321 * the previous character. */
9322 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009323 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 dec_cursor();
9325 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9326 inc_cursor();
9327 want_vcol = (want_vcol / ts) * ts;
9328
9329 /* delete characters until we are at or before want_vcol */
9330 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009331 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009332 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
9334 /* insert extra spaces until we are at want_vcol */
9335 while (vcol < want_vcol)
9336 {
9337 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009338 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9339 && curwin->w_cursor.col < Insstart_orig.col)
9340 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
9342#ifdef FEAT_VREPLACE
9343 if (State & VREPLACE_FLAG)
9344 ins_char(' ');
9345 else
9346#endif
9347 {
9348 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009349 if ((State & REPLACE_FLAG))
9350 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 }
9352 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9353 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009354
9355 /* If we are now back where we started delete one character. Can
9356 * happen when using 'sts' and 'linebreak'. */
9357 if (vcol >= start_vcol)
9358 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 }
9360
9361 /*
9362 * Delete upto starting point, start of line or previous word.
9363 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009364 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009366#ifdef FEAT_MBYTE
9367 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009369 if (has_mbyte)
9370 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009372 do
9373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009375 if (!revins_on) /* put cursor on char to be deleted */
9376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009378
9379 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009381 /* look multi-byte character class */
9382 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009384 prev_cclass = cclass;
9385 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009388
9389 /* start of word? */
9390 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9391 {
9392 mode = BACKSPACE_WORD_NOT_SPACE;
9393 temp = vim_iswordc(cc);
9394 }
9395 /* end of word? */
9396 else if (mode == BACKSPACE_WORD_NOT_SPACE
9397 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9398#ifdef FEAT_MBYTE
9399 || prev_cclass != cclass
9400#endif
9401 ))
9402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009404 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009406 inc_cursor();
9407#ifdef FEAT_RIGHTLEFT
9408 else if (State & REPLACE_FLAG)
9409 dec_cursor();
9410#endif
9411 break;
9412 }
9413 if (State & REPLACE_FLAG)
9414 replace_do_bs(-1);
9415 else
9416 {
9417#ifdef FEAT_MBYTE
9418 if (enc_utf8 && p_deco)
9419 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9420#endif
9421 (void)del_char(FALSE);
9422#ifdef FEAT_MBYTE
9423 /*
9424 * If there are combining characters and 'delcombine' is set
9425 * move the cursor back. Don't back up before the base
9426 * character.
9427 */
9428 if (enc_utf8 && p_deco && cpc[0] != NUL)
9429 inc_cursor();
9430#endif
9431#ifdef FEAT_RIGHTLEFT
9432 if (revins_chars)
9433 {
9434 revins_chars--;
9435 revins_legal++;
9436 }
9437 if (revins_on && gchar_cursor() == NUL)
9438 break;
9439#endif
9440 }
9441 /* Just a single backspace?: */
9442 if (mode == BACKSPACE_CHAR)
9443 break;
9444 } while (
9445#ifdef FEAT_RIGHTLEFT
9446 revins_on ||
9447#endif
9448 (curwin->w_cursor.col > mincol
9449 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9450 || curwin->w_cursor.col != Insstart_orig.col)));
9451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 did_backspace = TRUE;
9453 }
9454#ifdef FEAT_SMARTINDENT
9455 did_si = FALSE;
9456 can_si = FALSE;
9457 can_si_back = FALSE;
9458#endif
9459 if (curwin->w_cursor.col <= 1)
9460 did_ai = FALSE;
9461 /*
9462 * It's a little strange to put backspaces into the redo
9463 * buffer, but it makes auto-indent a lot easier to deal
9464 * with.
9465 */
9466 AppendCharToRedobuff(c);
9467
9468 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009469 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9470 && curwin->w_cursor.col < Insstart_orig.col)
9471 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472
9473 /* vi behaviour: the cursor moves backward but the character that
9474 * was there remains visible
9475 * Vim behaviour: the cursor moves backward and the character that
9476 * was there is erased from the screen.
9477 * We can emulate the vi behaviour by pretending there is a dollar
9478 * displayed even when there isn't.
9479 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009480 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 dollar_vcol = curwin->w_virtcol;
9482
Bram Moolenaarce3be472008-01-14 19:12:28 +00009483#ifdef FEAT_FOLDING
9484 /* When deleting a char the cursor line must never be in a closed fold.
9485 * E.g., when 'foldmethod' is indent and deleting the first non-white
9486 * char before a Tab. */
9487 if (did_backspace)
9488 foldOpenCursor();
9489#endif
9490
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 return did_backspace;
9492}
9493
9494#ifdef FEAT_MOUSE
9495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009496ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497{
9498 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009499 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500
9501# ifdef FEAT_GUI
9502 /* When GUI is active, also move/paste when 'mouse' is empty */
9503 if (!gui.in_use)
9504# endif
9505 if (!mouse_has(MOUSE_INSERT))
9506 return;
9507
9508 undisplay_dollar();
9509 tpos = curwin->w_cursor;
9510 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9511 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009512 win_T *new_curwin = curwin;
9513
9514 if (curwin != old_curwin && win_valid(old_curwin))
9515 {
9516 /* Mouse took us to another window. We need to go back to the
9517 * previous one to stop insert there properly. */
9518 curwin = old_curwin;
9519 curbuf = curwin->w_buffer;
9520 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009521 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009522 if (curwin != new_curwin && win_valid(new_curwin))
9523 {
9524 curwin = new_curwin;
9525 curbuf = curwin->w_buffer;
9526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527# ifdef FEAT_CINDENT
9528 can_cindent = TRUE;
9529# endif
9530 }
9531
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 /* redraw status lines (in case another window became active) */
9533 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534}
9535
9536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009537ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009540 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009541# ifdef FEAT_INS_EXPAND
9542 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543# endif
9544
9545 tpos = curwin->w_cursor;
9546
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009547 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 {
9549 int row, col;
9550
9551 row = mouse_row;
9552 col = mouse_col;
9553
9554 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009555 wp = mouse_find_win(&row, &col);
9556 if (wp == NULL)
9557 return;
9558 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 curbuf = curwin->w_buffer;
9560 }
9561 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 undisplay_dollar();
9563
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009564# ifdef FEAT_INS_EXPAND
9565 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009566 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009567# endif
9568 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009569 if (dir == MSCR_DOWN || dir == MSCR_UP)
9570 {
9571 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9572 scroll_redraw(dir,
9573 (long)(curwin->w_botline - curwin->w_topline));
9574 else
9575 scroll_redraw(dir, 3L);
9576 }
9577#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009578 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009579 {
9580 int val, step = 6;
9581
9582 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009583 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009584 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9585 if (val < 0)
9586 val = 0;
9587 gui_do_horiz_scroll(val, TRUE);
9588 }
9589#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009590# ifdef FEAT_INS_EXPAND
9591 did_scroll = TRUE;
9592# endif
9593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 curwin->w_redr_status = TRUE;
9596
9597 curwin = old_curwin;
9598 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009600# ifdef FEAT_INS_EXPAND
9601 /* The popup menu may overlay the window, need to redraw it.
9602 * TODO: Would be more efficient to only redraw the windows that are
9603 * overlapped by the popup menu. */
9604 if (pum_visible() && did_scroll)
9605 {
9606 redraw_all_later(NOT_VALID);
9607 ins_compl_show_pum();
9608 }
9609# endif
9610
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009611 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 {
9613 start_arrow(&tpos);
9614# ifdef FEAT_CINDENT
9615 can_cindent = TRUE;
9616# endif
9617 }
9618}
9619#endif
9620
Bram Moolenaarec2da362017-01-21 20:04:22 +01009621/*
9622 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9623 * P_PE literally.
9624 * When "drop" is TRUE then consume the text and drop it.
9625 */
9626 int
9627bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9628{
9629 int c;
9630 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9631 int idx = 0;
9632 char_u *end = find_termcode((char_u *)"PE");
9633 int ret_char = -1;
9634 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009635 int save_paste = p_paste;
9636 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009637
9638 /* If the end code is too long we can't detect it, read everything. */
9639 if (STRLEN(end) >= NUMBUFLEN)
9640 end = NULL;
9641 ++no_mapping;
9642 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009643 p_paste = TRUE;
9644 curbuf->b_p_ai = FALSE;
9645
Bram Moolenaarec2da362017-01-21 20:04:22 +01009646 for (;;)
9647 {
9648 /* When the end is not defined read everything. */
9649 if (end == NULL && vpeekc() == NUL)
9650 break;
9651 c = plain_vgetc();
9652#ifdef FEAT_MBYTE
9653 if (has_mbyte)
9654 idx += (*mb_char2bytes)(c, buf + idx);
9655 else
9656#endif
9657 buf[idx++] = c;
9658 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009659 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009660 {
9661 if (end[idx] == NUL)
9662 break; /* Found the end of paste code. */
9663 continue;
9664 }
9665 if (!drop)
9666 {
9667 switch (mode)
9668 {
9669 case PASTE_CMDLINE:
9670 put_on_cmdline(buf, idx, TRUE);
9671 break;
9672
9673 case PASTE_EX:
9674 if (gap != NULL && ga_grow(gap, idx) == OK)
9675 {
9676 mch_memmove((char *)gap->ga_data + gap->ga_len,
9677 buf, (size_t)idx);
9678 gap->ga_len += idx;
9679 }
9680 break;
9681
9682 case PASTE_INSERT:
9683 if (stop_arrow() == OK)
9684 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009685 c = buf[0];
9686 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9687 ins_eol(c);
9688 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009689 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009690 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009691 AppendToRedobuffLit(buf, idx);
9692 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009693 }
9694 break;
9695
9696 case PASTE_ONE_CHAR:
9697 if (ret_char == -1)
9698 {
9699#ifdef FEAT_MBYTE
9700 if (has_mbyte)
9701 ret_char = (*mb_ptr2char)(buf);
9702 else
9703#endif
9704 ret_char = buf[0];
9705 }
9706 break;
9707 }
9708 }
9709 idx = 0;
9710 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009711
Bram Moolenaarec2da362017-01-21 20:04:22 +01009712 --no_mapping;
9713 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009714 p_paste = save_paste;
9715 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009716
9717 return ret_char;
9718}
9719
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009720#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009722ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009723{
9724 /* We will be leaving the current window, unless closing another tab. */
9725 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9726 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9727 {
9728 undisplay_dollar();
9729 start_arrow(&curwin->w_cursor);
9730# ifdef FEAT_CINDENT
9731 can_cindent = TRUE;
9732# endif
9733 }
9734
9735 if (c == K_TABLINE)
9736 goto_tabpage(current_tab);
9737 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009738 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009739 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009740 redraw_statuslines(); /* will redraw the tabline when needed */
9741 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009742}
9743#endif
9744
9745#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009747ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
9749 pos_T tpos;
9750
9751 undisplay_dollar();
9752 tpos = curwin->w_cursor;
9753 if (gui_do_scroll())
9754 {
9755 start_arrow(&tpos);
9756# ifdef FEAT_CINDENT
9757 can_cindent = TRUE;
9758# endif
9759 }
9760}
9761
9762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009763ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764{
9765 pos_T tpos;
9766
9767 undisplay_dollar();
9768 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009769 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 {
9771 start_arrow(&tpos);
9772# ifdef FEAT_CINDENT
9773 can_cindent = TRUE;
9774# endif
9775 }
9776}
9777#endif
9778
9779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009780ins_left(
9781 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
9783 pos_T tpos;
9784
9785#ifdef FEAT_FOLDING
9786 if ((fdo_flags & FDO_HOR) && KeyTyped)
9787 foldOpenCursor();
9788#endif
9789 undisplay_dollar();
9790 tpos = curwin->w_cursor;
9791 if (oneleft() == OK)
9792 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009793#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9794 /* Only call start_arrow() when not busy with preediting, it will
9795 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009796 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009797#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009798 {
9799 start_arrow_with_change(&tpos, end_change);
9800 if (!end_change)
9801 AppendCharToRedobuff(K_LEFT);
9802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803#ifdef FEAT_RIGHTLEFT
9804 /* If exit reversed string, position is fixed */
9805 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9806 revins_legal++;
9807 revins_chars++;
9808#endif
9809 }
9810
9811 /*
9812 * if 'whichwrap' set for cursor in insert mode may go to
9813 * previous line
9814 */
9815 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9816 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009817 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 start_arrow(&tpos);
9819 --(curwin->w_cursor.lnum);
9820 coladvance((colnr_T)MAXCOL);
9821 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9822 }
9823 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009824 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009825 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
9828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009829ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
9831 pos_T tpos;
9832
9833#ifdef FEAT_FOLDING
9834 if ((fdo_flags & FDO_HOR) && KeyTyped)
9835 foldOpenCursor();
9836#endif
9837 undisplay_dollar();
9838 tpos = curwin->w_cursor;
9839 if (c == K_C_HOME)
9840 curwin->w_cursor.lnum = 1;
9841 curwin->w_cursor.col = 0;
9842#ifdef FEAT_VIRTUALEDIT
9843 curwin->w_cursor.coladd = 0;
9844#endif
9845 curwin->w_curswant = 0;
9846 start_arrow(&tpos);
9847}
9848
9849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009850ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851{
9852 pos_T tpos;
9853
9854#ifdef FEAT_FOLDING
9855 if ((fdo_flags & FDO_HOR) && KeyTyped)
9856 foldOpenCursor();
9857#endif
9858 undisplay_dollar();
9859 tpos = curwin->w_cursor;
9860 if (c == K_C_END)
9861 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9862 coladvance((colnr_T)MAXCOL);
9863 curwin->w_curswant = MAXCOL;
9864
9865 start_arrow(&tpos);
9866}
9867
9868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009869ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870{
9871#ifdef FEAT_FOLDING
9872 if ((fdo_flags & FDO_HOR) && KeyTyped)
9873 foldOpenCursor();
9874#endif
9875 undisplay_dollar();
9876 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9877 {
9878 start_arrow(&curwin->w_cursor);
9879 (void)bck_word(1L, FALSE, FALSE);
9880 curwin->w_set_curswant = TRUE;
9881 }
9882 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009883 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884}
9885
9886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009887ins_right(
9888 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
9890#ifdef FEAT_FOLDING
9891 if ((fdo_flags & FDO_HOR) && KeyTyped)
9892 foldOpenCursor();
9893#endif
9894 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009895 if (gchar_cursor() != NUL
9896#ifdef FEAT_VIRTUALEDIT
9897 || virtual_active()
9898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 )
9900 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009901 start_arrow_with_change(&curwin->w_cursor, end_change);
9902 if (!end_change)
9903 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 curwin->w_set_curswant = TRUE;
9905#ifdef FEAT_VIRTUALEDIT
9906 if (virtual_active())
9907 oneright();
9908 else
9909#endif
9910 {
9911#ifdef FEAT_MBYTE
9912 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009913 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 else
9915#endif
9916 ++curwin->w_cursor.col;
9917 }
9918
9919#ifdef FEAT_RIGHTLEFT
9920 revins_legal++;
9921 if (revins_chars)
9922 revins_chars--;
9923#endif
9924 }
9925 /* if 'whichwrap' set for cursor in insert mode, may move the
9926 * cursor to the next line */
9927 else if (vim_strchr(p_ww, ']') != NULL
9928 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9929 {
9930 start_arrow(&curwin->w_cursor);
9931 curwin->w_set_curswant = TRUE;
9932 ++curwin->w_cursor.lnum;
9933 curwin->w_cursor.col = 0;
9934 }
9935 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009936 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009937 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938}
9939
9940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009941ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942{
9943#ifdef FEAT_FOLDING
9944 if ((fdo_flags & FDO_HOR) && KeyTyped)
9945 foldOpenCursor();
9946#endif
9947 undisplay_dollar();
9948 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9949 || gchar_cursor() != NUL)
9950 {
9951 start_arrow(&curwin->w_cursor);
9952 (void)fwd_word(1L, FALSE, 0);
9953 curwin->w_set_curswant = TRUE;
9954 }
9955 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009956 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957}
9958
9959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009960ins_up(
9961 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962{
9963 pos_T tpos;
9964 linenr_T old_topline = curwin->w_topline;
9965#ifdef FEAT_DIFF
9966 int old_topfill = curwin->w_topfill;
9967#endif
9968
9969 undisplay_dollar();
9970 tpos = curwin->w_cursor;
9971 if (cursor_up(1L, TRUE) == OK)
9972 {
9973 if (startcol)
9974 coladvance(getvcol_nolist(&Insstart));
9975 if (old_topline != curwin->w_topline
9976#ifdef FEAT_DIFF
9977 || old_topfill != curwin->w_topfill
9978#endif
9979 )
9980 redraw_later(VALID);
9981 start_arrow(&tpos);
9982#ifdef FEAT_CINDENT
9983 can_cindent = TRUE;
9984#endif
9985 }
9986 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009987 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988}
9989
9990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009991ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992{
9993 pos_T tpos;
9994
9995 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009996
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009997 if (mod_mask & MOD_MASK_CTRL)
9998 {
9999 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010000 if (first_tabpage->tp_next != NULL)
10001 {
10002 start_arrow(&curwin->w_cursor);
10003 goto_tabpage(-1);
10004 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010005 return;
10006 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010007
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 tpos = curwin->w_cursor;
10009 if (onepage(BACKWARD, 1L) == OK)
10010 {
10011 start_arrow(&tpos);
10012#ifdef FEAT_CINDENT
10013 can_cindent = TRUE;
10014#endif
10015 }
10016 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010017 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018}
10019
10020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010021ins_down(
10022 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023{
10024 pos_T tpos;
10025 linenr_T old_topline = curwin->w_topline;
10026#ifdef FEAT_DIFF
10027 int old_topfill = curwin->w_topfill;
10028#endif
10029
10030 undisplay_dollar();
10031 tpos = curwin->w_cursor;
10032 if (cursor_down(1L, TRUE) == OK)
10033 {
10034 if (startcol)
10035 coladvance(getvcol_nolist(&Insstart));
10036 if (old_topline != curwin->w_topline
10037#ifdef FEAT_DIFF
10038 || old_topfill != curwin->w_topfill
10039#endif
10040 )
10041 redraw_later(VALID);
10042 start_arrow(&tpos);
10043#ifdef FEAT_CINDENT
10044 can_cindent = TRUE;
10045#endif
10046 }
10047 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010048 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049}
10050
10051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010052ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 pos_T tpos;
10055
10056 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010057
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010058 if (mod_mask & MOD_MASK_CTRL)
10059 {
10060 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010061 if (first_tabpage->tp_next != NULL)
10062 {
10063 start_arrow(&curwin->w_cursor);
10064 goto_tabpage(0);
10065 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010066 return;
10067 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010068
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 tpos = curwin->w_cursor;
10070 if (onepage(FORWARD, 1L) == OK)
10071 {
10072 start_arrow(&tpos);
10073#ifdef FEAT_CINDENT
10074 can_cindent = TRUE;
10075#endif
10076 }
10077 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010078 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079}
10080
10081#ifdef FEAT_DND
10082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010083ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084{
10085 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10086}
10087#endif
10088
10089/*
10090 * Handle TAB in Insert or Replace mode.
10091 * Return TRUE when the TAB needs to be inserted like a normal character.
10092 */
10093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010094ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095{
10096 int ind;
10097 int i;
10098 int temp;
10099
10100 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10101 Insstart_blank_vcol = get_nolist_virtcol();
10102 if (echeck_abbr(TAB + ABBR_OFF))
10103 return FALSE;
10104
10105 ind = inindent(0);
10106#ifdef FEAT_CINDENT
10107 if (ind)
10108 can_cindent = FALSE;
10109#endif
10110
10111 /*
10112 * When nothing special, insert TAB like a normal character
10113 */
10114 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010115 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010116 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 return TRUE;
10118
10119 if (stop_arrow() == FAIL)
10120 return TRUE;
10121
10122 did_ai = FALSE;
10123#ifdef FEAT_SMARTINDENT
10124 did_si = FALSE;
10125 can_si = FALSE;
10126 can_si_back = FALSE;
10127#endif
10128 AppendToRedobuff((char_u *)"\t");
10129
10130 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010131 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010132 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10133 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 else /* otherwise use 'tabstop' */
10135 temp = (int)curbuf->b_p_ts;
10136 temp -= get_nolist_virtcol() % temp;
10137
10138 /*
10139 * Insert the first space with ins_char(). It will delete one char in
10140 * replace mode. Insert the rest with ins_str(); it will not delete any
10141 * chars. For VREPLACE mode, we use ins_char() for all characters.
10142 */
10143 ins_char(' ');
10144 while (--temp > 0)
10145 {
10146#ifdef FEAT_VREPLACE
10147 if (State & VREPLACE_FLAG)
10148 ins_char(' ');
10149 else
10150#endif
10151 {
10152 ins_str((char_u *)" ");
10153 if (State & REPLACE_FLAG) /* no char replaced */
10154 replace_push(NUL);
10155 }
10156 }
10157
10158 /*
10159 * When 'expandtab' not set: Replace spaces by TABs where possible.
10160 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010161 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 {
10163 char_u *ptr;
10164#ifdef FEAT_VREPLACE
10165 char_u *saved_line = NULL; /* init for GCC */
10166 pos_T pos;
10167#endif
10168 pos_T fpos;
10169 pos_T *cursor;
10170 colnr_T want_vcol, vcol;
10171 int change_col = -1;
10172 int save_list = curwin->w_p_list;
10173
10174 /*
10175 * Get the current line. For VREPLACE mode, don't make real changes
10176 * yet, just work on a copy of the line.
10177 */
10178#ifdef FEAT_VREPLACE
10179 if (State & VREPLACE_FLAG)
10180 {
10181 pos = curwin->w_cursor;
10182 cursor = &pos;
10183 saved_line = vim_strsave(ml_get_curline());
10184 if (saved_line == NULL)
10185 return FALSE;
10186 ptr = saved_line + pos.col;
10187 }
10188 else
10189#endif
10190 {
10191 ptr = ml_get_cursor();
10192 cursor = &curwin->w_cursor;
10193 }
10194
10195 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10196 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10197 curwin->w_p_list = FALSE;
10198
10199 /* Find first white before the cursor */
10200 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010201 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 {
10203 --fpos.col;
10204 --ptr;
10205 }
10206
10207 /* In Replace mode, don't change characters before the insert point. */
10208 if ((State & REPLACE_FLAG)
10209 && fpos.lnum == Insstart.lnum
10210 && fpos.col < Insstart.col)
10211 {
10212 ptr += Insstart.col - fpos.col;
10213 fpos.col = Insstart.col;
10214 }
10215
10216 /* compute virtual column numbers of first white and cursor */
10217 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10218 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10219
Bram Moolenaar597a4222014-06-25 14:39:50 +020010220 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10221 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010222 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010224 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 if (vcol + i > want_vcol)
10226 break;
10227 if (*ptr != TAB)
10228 {
10229 *ptr = TAB;
10230 if (change_col < 0)
10231 {
10232 change_col = fpos.col; /* Column of first change */
10233 /* May have to adjust Insstart */
10234 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10235 Insstart.col = fpos.col;
10236 }
10237 }
10238 ++fpos.col;
10239 ++ptr;
10240 vcol += i;
10241 }
10242
10243 if (change_col >= 0)
10244 {
10245 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010246 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247
10248 /* Skip over the spaces we need. */
10249 while (vcol < want_vcol && *ptr == ' ')
10250 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010251 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 ++ptr;
10253 ++repl_off;
10254 }
10255 if (vcol > want_vcol)
10256 {
10257 /* Must have a char with 'showbreak' just before it. */
10258 --ptr;
10259 --repl_off;
10260 }
10261 fpos.col += repl_off;
10262
10263 /* Delete following spaces. */
10264 i = cursor->col - fpos.col;
10265 if (i > 0)
10266 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010267 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 /* correct replace stack. */
10269 if ((State & REPLACE_FLAG)
10270#ifdef FEAT_VREPLACE
10271 && !(State & VREPLACE_FLAG)
10272#endif
10273 )
10274 for (temp = i; --temp >= 0; )
10275 replace_join(repl_off);
10276 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010277#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010278 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010279 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010280 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010281 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10282 (char_u *)"\t", 1);
10283 }
10284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 cursor->col -= i;
10286
10287#ifdef FEAT_VREPLACE
10288 /*
10289 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10290 * backspacing over the changed spacing and then inserting the new
10291 * spacing.
10292 */
10293 if (State & VREPLACE_FLAG)
10294 {
10295 /* Backspace from real cursor to change_col */
10296 backspace_until_column(change_col);
10297
10298 /* Insert each char in saved_line from changed_col to
10299 * ptr-cursor */
10300 ins_bytes_len(saved_line + change_col,
10301 cursor->col - change_col);
10302 }
10303#endif
10304 }
10305
10306#ifdef FEAT_VREPLACE
10307 if (State & VREPLACE_FLAG)
10308 vim_free(saved_line);
10309#endif
10310 curwin->w_p_list = save_list;
10311 }
10312
10313 return FALSE;
10314}
10315
10316/*
10317 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010318 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 */
10320 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010321ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322{
10323 int i;
10324
10325 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010326 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010328 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 undisplay_dollar();
10330
10331 /*
10332 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10333 * character under the cursor. Only push a NUL on the replace stack,
10334 * nothing to put back when the NL is deleted.
10335 */
10336 if ((State & REPLACE_FLAG)
10337#ifdef FEAT_VREPLACE
10338 && !(State & VREPLACE_FLAG)
10339#endif
10340 )
10341 replace_push(NUL);
10342
10343 /*
10344 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10345 * replacing the next line, so we push all of the characters left on the
10346 * line onto the replace stack. This is not done here though, it is done
10347 * in open_line().
10348 */
10349
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010350#ifdef FEAT_VIRTUALEDIT
10351 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10352 * CTRL-O). */
10353 if (virtual_active() && curwin->w_cursor.coladd > 0)
10354 coladvance(getviscol());
10355#endif
10356
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357#ifdef FEAT_RIGHTLEFT
10358# ifdef FEAT_FKMAP
10359 if (p_altkeymap && p_fkmap)
10360 fkmap(NL);
10361# endif
10362 /* NL in reverse insert will always start in the end of
10363 * current line. */
10364 if (revins_on)
10365 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10366#endif
10367
10368 AppendToRedobuff(NL_STR);
10369 i = open_line(FORWARD,
10370#ifdef FEAT_COMMENTS
10371 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10372#endif
10373 0, old_indent);
10374 old_indent = 0;
10375#ifdef FEAT_CINDENT
10376 can_cindent = TRUE;
10377#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010378#ifdef FEAT_FOLDING
10379 /* When inserting a line the cursor line must never be in a closed fold. */
10380 foldOpenCursor();
10381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010383 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384}
10385
10386#ifdef FEAT_DIGRAPHS
10387/*
10388 * Handle digraph in insert mode.
10389 * Returns character still to be inserted, or NUL when nothing remaining to be
10390 * done.
10391 */
10392 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010393ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394{
10395 int c;
10396 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010397 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398
10399 pc_status = PC_STATUS_UNSET;
10400 if (redrawing() && !char_avail())
10401 {
10402 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010403 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404
10405 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010406 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407#ifdef FEAT_CMDL_INFO
10408 add_to_showcmd_c(Ctrl_K);
10409#endif
10410 }
10411
10412#ifdef USE_ON_FLY_SCROLL
10413 dont_scroll = TRUE; /* disallow scrolling here */
10414#endif
10415
10416 /* don't map the digraph chars. This also prevents the
10417 * mode message to be deleted when ESC is hit */
10418 ++no_mapping;
10419 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010420 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 --no_mapping;
10422 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010423 if (did_putchar)
10424 /* when the line fits in 'columns' the '?' is at the start of the next
10425 * line and will not be removed by the redraw */
10426 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010427
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 if (IS_SPECIAL(c) || mod_mask) /* special key */
10429 {
10430#ifdef FEAT_CMDL_INFO
10431 clear_showcmd();
10432#endif
10433 insert_special(c, TRUE, FALSE);
10434 return NUL;
10435 }
10436 if (c != ESC)
10437 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010438 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 if (redrawing() && !char_avail())
10440 {
10441 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010442 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443
10444 if (char2cells(c) == 1)
10445 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010446 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010448 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 }
10450#ifdef FEAT_CMDL_INFO
10451 add_to_showcmd_c(c);
10452#endif
10453 }
10454 ++no_mapping;
10455 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010456 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 --no_mapping;
10458 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010459 if (did_putchar)
10460 /* when the line fits in 'columns' the '?' is at the start of the
10461 * next line and will not be removed by a redraw */
10462 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 if (cc != ESC)
10464 {
10465 AppendToRedobuff((char_u *)CTRL_V_STR);
10466 c = getdigraph(c, cc, TRUE);
10467#ifdef FEAT_CMDL_INFO
10468 clear_showcmd();
10469#endif
10470 return c;
10471 }
10472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473#ifdef FEAT_CMDL_INFO
10474 clear_showcmd();
10475#endif
10476 return NUL;
10477}
10478#endif
10479
10480/*
10481 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10482 * Returns the char to be inserted, or NUL if none found.
10483 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010484 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010485ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486{
10487 int c;
10488 int temp;
10489 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010490 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491
10492 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10493 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010494 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 return NUL;
10496 }
10497
10498 /* try to advance to the cursor column */
10499 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010500 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 prev_ptr = ptr;
10502 validate_virtcol();
10503 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10504 {
10505 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010506 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 }
10508 if ((colnr_T)temp > curwin->w_virtcol)
10509 ptr = prev_ptr;
10510
10511#ifdef FEAT_MBYTE
10512 c = (*mb_ptr2char)(ptr);
10513#else
10514 c = *ptr;
10515#endif
10516 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010517 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 return c;
10519}
10520
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010521/*
10522 * CTRL-Y or CTRL-E typed in Insert mode.
10523 */
10524 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010525ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010526{
10527 int c = tc;
10528
10529#ifdef FEAT_INS_EXPAND
10530 if (ctrl_x_mode == CTRL_X_SCROLL)
10531 {
10532 if (c == Ctrl_Y)
10533 scrolldown_clamp();
10534 else
10535 scrollup_clamp();
10536 redraw_later(VALID);
10537 }
10538 else
10539#endif
10540 {
10541 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10542 if (c != NUL)
10543 {
10544 long tw_save;
10545
10546 /* The character must be taken literally, insert like it
10547 * was typed after a CTRL-V, and pretend 'textwidth'
10548 * wasn't set. Digits, 'o' and 'x' are special after a
10549 * CTRL-V, don't use it for these. */
10550 if (c < 256 && !isalnum(c))
10551 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10552 tw_save = curbuf->b_p_tw;
10553 curbuf->b_p_tw = -1;
10554 insert_special(c, TRUE, FALSE);
10555 curbuf->b_p_tw = tw_save;
10556#ifdef FEAT_RIGHTLEFT
10557 revins_chars++;
10558 revins_legal++;
10559#endif
10560 c = Ctrl_V; /* pretend CTRL-V is last character */
10561 auto_format(FALSE, TRUE);
10562 }
10563 }
10564 return c;
10565}
10566
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567#ifdef FEAT_SMARTINDENT
10568/*
10569 * Try to do some very smart auto-indenting.
10570 * Used when inserting a "normal" character.
10571 */
10572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010573ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574{
10575 pos_T *pos, old_pos;
10576 char_u *ptr;
10577 int i;
10578 int temp;
10579
10580 /*
10581 * do some very smart indenting when entering '{' or '}'
10582 */
10583 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10584 {
10585 /*
10586 * for '}' set indent equal to indent of line containing matching '{'
10587 */
10588 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10589 {
10590 old_pos = curwin->w_cursor;
10591 /*
10592 * If the matching '{' has a ')' immediately before it (ignoring
10593 * white-space), then line up with the start of the line
10594 * containing the matching '(' if there is one. This handles the
10595 * case where an "if (..\n..) {" statement continues over multiple
10596 * lines -- webb
10597 */
10598 ptr = ml_get(pos->lnum);
10599 i = pos->col;
10600 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010601 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 ;
10603 curwin->w_cursor.lnum = pos->lnum;
10604 curwin->w_cursor.col = i;
10605 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10606 curwin->w_cursor = *pos;
10607 i = get_indent();
10608 curwin->w_cursor = old_pos;
10609#ifdef FEAT_VREPLACE
10610 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010611 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 else
10613#endif
10614 (void)set_indent(i, SIN_CHANGED);
10615 }
10616 else if (curwin->w_cursor.col > 0)
10617 {
10618 /*
10619 * when inserting '{' after "O" reduce indent, but not
10620 * more than indent of previous line
10621 */
10622 temp = TRUE;
10623 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10624 {
10625 old_pos = curwin->w_cursor;
10626 i = get_indent();
10627 while (curwin->w_cursor.lnum > 1)
10628 {
10629 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10630
10631 /* ignore empty lines and lines starting with '#'. */
10632 if (*ptr != '#' && *ptr != NUL)
10633 break;
10634 }
10635 if (get_indent() >= i)
10636 temp = FALSE;
10637 curwin->w_cursor = old_pos;
10638 }
10639 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010640 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 }
10642 }
10643
10644 /*
10645 * set indent of '#' always to 0
10646 */
10647 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10648 {
10649 /* remember current indent for next line */
10650 old_indent = get_indent();
10651 (void)set_indent(0, SIN_CHANGED);
10652 }
10653
10654 /* Adjust ai_col, the char at this position can be deleted. */
10655 if (ai_col > curwin->w_cursor.col)
10656 ai_col = curwin->w_cursor.col;
10657}
10658#endif
10659
10660/*
10661 * Get the value that w_virtcol would have when 'list' is off.
10662 * Unless 'cpo' contains the 'L' flag.
10663 */
10664 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010665get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666{
10667 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10668 return getvcol_nolist(&curwin->w_cursor);
10669 validate_virtcol();
10670 return curwin->w_virtcol;
10671}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010672
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010673#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010674/*
10675 * Handle the InsertCharPre autocommand.
10676 * "c" is the character that was typed.
10677 * Return a pointer to allocated memory with the replacement string.
10678 * Return NULL to continue inserting "c".
10679 */
10680 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010681do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010682{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010683 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010684 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010685
10686 /* Return quickly when there is nothing to do. */
10687 if (!has_insertcharpre())
10688 return NULL;
10689
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010690# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010691 if (has_mbyte)
10692 buf[(*mb_char2bytes)(c, buf)] = NUL;
10693 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010694# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010695 {
10696 buf[0] = c;
10697 buf[1] = NUL;
10698 }
10699
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010700 /* Lock the text to avoid weird things from happening. */
10701 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010702 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010703
Bram Moolenaar704984a2012-06-01 14:57:51 +020010704 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010705 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010706 {
10707 /* Get the value of v:char. It may be empty or more than one
10708 * character. Only use it when changed, otherwise continue with the
10709 * original character to avoid breaking autoindent. */
10710 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10711 res = vim_strsave(get_vim_var_str(VV_CHAR));
10712 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010713
10714 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10715 --textlock;
10716
10717 return res;
10718}
10719#endif