blob: 92c9d6ad447e202597c22c542972c81cebd0b145 [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 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001414 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001415 if (!bt_prompt(curbuf))
1416 // buffer changed to a non-prompt buffer, get out of
1417 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001418 goto doESCkey;
1419 break;
1420 }
1421#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001422 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 goto doESCkey; /* out of memory */
1424 auto_format(FALSE, FALSE);
1425 inserted_space = FALSE;
1426 break;
1427
Bram Moolenaar860cae12010-06-05 23:22:07 +02001428#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001429 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430# ifdef FEAT_INS_EXPAND
1431 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1432 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001433 if (has_compl_option(TRUE))
1434 goto docomplete;
1435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437# endif
1438# ifdef FEAT_DIGRAPHS
1439 c = ins_digraph();
1440 if (c == NUL)
1441 break;
1442# endif
1443 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001447 case Ctrl_X: /* Enter CTRL-X mode */
1448 ins_ctrl_x();
1449 break;
1450
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001451 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (ctrl_x_mode != CTRL_X_TAGS)
1453 goto normalchar;
1454 goto docomplete;
1455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001456 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (ctrl_x_mode != CTRL_X_FILES)
1458 goto normalchar;
1459 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001460
1461 case 's': /* Spelling completion after ^X */
1462 case Ctrl_S:
1463 if (ctrl_x_mode != CTRL_X_SPELL)
1464 goto normalchar;
1465 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
1467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001468 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#ifdef FEAT_INS_EXPAND
1470 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1471#endif
1472 {
1473 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1474 if (p_im)
1475 {
1476 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1477 break;
1478 goto doESCkey;
1479 }
1480 goto normalchar;
1481 }
1482#ifdef FEAT_INS_EXPAND
1483 /* FALLTHROUGH */
1484
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001485 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 case Ctrl_N:
1487 /* if 'complete' is empty then plain ^P is no longer special,
1488 * but it is under other ^X modes */
1489 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001490 && (ctrl_x_mode == CTRL_X_NORMAL
1491 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001492 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 goto normalchar;
1494
1495docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001496 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001497#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001498 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001499#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001500 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001501 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001502#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001503 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001504#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001505 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 break;
1507#endif /* FEAT_INS_EXPAND */
1508
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001509 case Ctrl_Y: /* copy from previous line or scroll down */
1510 case Ctrl_E: /* copy from next line or scroll up */
1511 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 break;
1513
1514 default:
1515#ifdef UNIX
1516 if (c == intr_char) /* special interrupt char */
1517 goto do_intr;
1518#endif
1519
Bram Moolenaare659c952011-05-19 17:25:41 +02001520normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001522 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001524#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001525 if (!p_paste)
1526 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001527 /* Trigger InsertCharPre. */
1528 char_u *str = do_insert_char_pre(c);
1529 char_u *p;
1530
1531 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001532 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001533 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001534 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001535 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001536 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001537 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001538 c = PTR2CHAR(p);
1539 if (c == CAR || c == K_KENTER || c == NL)
1540 ins_eol(c);
1541 else
1542 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001543 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001544 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001545 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001546 vim_free(str);
1547 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001548 }
1549
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001550 /* If the new value is already inserted or an empty string
1551 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001552 if (c == NUL)
1553 break;
1554 }
1555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#ifdef FEAT_SMARTINDENT
1557 /* Try to perform smart-indenting. */
1558 ins_try_si(c);
1559#endif
1560
1561 if (c == ' ')
1562 {
1563 inserted_space = TRUE;
1564#ifdef FEAT_CINDENT
1565 if (inindent(0))
1566 can_cindent = FALSE;
1567#endif
1568 if (Insstart_blank_vcol == MAXCOL
1569 && curwin->w_cursor.lnum == Insstart.lnum)
1570 Insstart_blank_vcol = get_nolist_virtcol();
1571 }
1572
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001573 /* Insert a normal character and check for abbreviations on a
1574 * special character. Let CTRL-] expand abbreviations without
1575 * inserting it. */
1576 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577#ifdef FEAT_MBYTE
1578 /* Add ABBR_OFF for characters above 0x100, this is
1579 * what check_abbr() expects. */
1580 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1581#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001582 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 insert_special(c, FALSE, FALSE);
1585#ifdef FEAT_RIGHTLEFT
1586 revins_legal++;
1587 revins_chars++;
1588#endif
1589 }
1590
1591 auto_format(FALSE, TRUE);
1592
1593#ifdef FEAT_FOLDING
1594 /* When inserting a character the cursor line must never be in a
1595 * closed fold. */
1596 foldOpenCursor();
1597#endif
1598 break;
1599 } /* end of switch (c) */
1600
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001601 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001602 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001603#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001604 /* but not in CTRL-X mode, a script can't restore the state */
1605 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001606#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001607 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001608 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 /* If the cursor was moved we didn't just insert a space */
1611 if (arrow_used)
1612 inserted_space = FALSE;
1613
1614#ifdef FEAT_CINDENT
1615 if (can_cindent && cindent_on()
1616# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001617 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618# endif
1619 )
1620 {
1621force_cindent:
1622 /*
1623 * Indent now if a key was typed that is in 'cinkeys'.
1624 */
1625 if (in_cinkeys(c, ' ', line_is_white))
1626 {
1627 if (stop_arrow() == OK)
1628 /* re-indent the current line */
1629 do_c_expr_indent();
1630 }
1631 }
1632#endif /* FEAT_CINDENT */
1633
1634 } /* for (;;) */
1635 /* NOTREACHED */
1636}
1637
1638/*
1639 * Redraw for Insert mode.
1640 * This is postponed until getting the next character to make '$' in the 'cpo'
1641 * option work correctly.
1642 * Only redraw when there are no characters available. This speeds up
1643 * inserting sequences of characters (e.g., for CTRL-R).
1644 */
1645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646ins_redraw(
1647 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001649#ifdef FEAT_CONCEAL
1650 linenr_T conceal_old_cursor_line = 0;
1651 linenr_T conceal_new_cursor_line = 0;
1652 int conceal_update_lines = FALSE;
1653#endif
1654
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001655 if (char_avail())
1656 return;
1657
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001658#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001659 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1660 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001661 if (ready && (has_cursormovedI()
1662# if defined(FEAT_CONCEAL)
1663 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001664# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001667# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001669# endif
1670 )
1671 {
1672# ifdef FEAT_SYN_HL
1673 /* Need to update the screen first, to make sure syntax
1674 * highlighting is correct after making a change (e.g., inserting
1675 * a "(". The autocommand may also require a redraw, so it's done
1676 * again below, unfortunately. */
1677 if (syntax_present(curwin) && must_redraw)
1678 update_screen(0);
1679# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001680 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001681 {
1682 /* Make sure curswant is correct, an autocommand may call
1683 * getcurpos(). */
1684 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001685 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001686 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001687# ifdef FEAT_CONCEAL
1688 if (curwin->w_p_cole > 0)
1689 {
1690 conceal_old_cursor_line = last_cursormoved.lnum;
1691 conceal_new_cursor_line = curwin->w_cursor.lnum;
1692 conceal_update_lines = TRUE;
1693 }
1694# endif
1695 last_cursormoved = curwin->w_cursor;
1696 }
1697#endif
1698
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001699 /* Trigger TextChangedI if b_changedtick differs. */
1700 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001701 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001702#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001703 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001704#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001705 )
1706 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001707 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1708 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001710
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001711#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001712 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1713 * TextChangedI will need to trigger for backwards compatibility, thus use
1714 * different b_last_changedtick* variables. */
1715 if (ready && has_textchangedP()
1716 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1717 && pum_visible())
1718 {
1719 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
1720 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1721 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001722#endif
1723
1724 if (must_redraw)
1725 update_screen(0);
1726 else if (clear_cmdline || redraw_cmdline)
1727 showmode(); /* clear cmdline and show mode */
1728# if defined(FEAT_CONCEAL)
1729 if ((conceal_update_lines
1730 && (conceal_old_cursor_line != conceal_new_cursor_line
1731 || conceal_cursor_line(curwin)))
1732 || need_cursor_line_redraw)
1733 {
1734 if (conceal_old_cursor_line != conceal_new_cursor_line)
1735 update_single_line(curwin, conceal_old_cursor_line);
1736 update_single_line(curwin, conceal_new_cursor_line == 0
1737 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1738 curwin->w_valid &= ~VALID_CROW;
1739 }
1740# endif
1741 showruler(FALSE);
1742 setcursor();
1743 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744}
1745
1746/*
1747 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1748 */
1749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001753 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001756 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001761 did_putchar = TRUE;
1762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1764
1765#ifdef FEAT_CMDL_INFO
1766 add_to_showcmd_c(Ctrl_V);
1767#endif
1768
1769 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001770 if (did_putchar)
1771 /* when the line fits in 'columns' the '^' is at the start of the next
1772 * line and will not removed by the redraw */
1773 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#ifdef FEAT_CMDL_INFO
1775 clear_showcmd();
1776#endif
1777 insert_special(c, FALSE, TRUE);
1778#ifdef FEAT_RIGHTLEFT
1779 revins_chars++;
1780 revins_legal++;
1781#endif
1782}
1783
1784/*
1785 * Put a character directly onto the screen. It's not stored in a buffer.
1786 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1787 */
1788static int pc_status;
1789#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1790#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1791#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1792#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794static int pc_attr;
1795static int pc_row;
1796static int pc_col;
1797
1798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 int attr;
1802
1803 if (ScreenLines != NULL)
1804 {
1805 update_topline(); /* just in case w_topline isn't valid */
1806 validate_cursor();
1807 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001808 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 else
1810 attr = 0;
1811 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001812 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1814 pc_status = PC_STATUS_UNSET;
1815#endif
1816#ifdef FEAT_RIGHTLEFT
1817 if (curwin->w_p_rl)
1818 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001819 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820# ifdef FEAT_MBYTE
1821 if (has_mbyte)
1822 {
1823 int fix_col = mb_fix_col(pc_col, pc_row);
1824
1825 if (fix_col != pc_col)
1826 {
1827 screen_putchar(' ', pc_row, fix_col, attr);
1828 --curwin->w_wcol;
1829 pc_status = PC_STATUS_RIGHT;
1830 }
1831 }
1832# endif
1833 }
1834 else
1835#endif
1836 {
1837 pc_col += curwin->w_wcol;
1838#ifdef FEAT_MBYTE
1839 if (mb_lefthalve(pc_row, pc_col))
1840 pc_status = PC_STATUS_LEFT;
1841#endif
1842 }
1843
1844 /* save the character to be able to put it back */
1845#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1846 if (pc_status == PC_STATUS_UNSET)
1847#endif
1848 {
1849 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1850 pc_status = PC_STATUS_SET;
1851 }
1852 screen_putchar(c, pc_row, pc_col, attr);
1853 }
1854}
1855
Bram Moolenaarf2732452018-06-03 14:47:35 +02001856#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1857/*
1858 * Return the effective prompt for the current buffer.
1859 */
1860 char_u *
1861prompt_text(void)
1862{
1863 if (curbuf->b_prompt_text == NULL)
1864 return (char_u *)"% ";
1865 return curbuf->b_prompt_text;
1866}
1867
1868/*
1869 * Prepare for prompt mode: Make sure the last line has the prompt text.
1870 * Move the cursor to this line.
1871 */
1872 static void
1873init_prompt(int cmdchar_todo)
1874{
1875 char_u *prompt = prompt_text();
1876 char_u *text;
1877
1878 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1879 text = ml_get_curline();
1880 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1881 {
1882 // prompt is missing, insert it or append a line with it
1883 if (*text == NUL)
1884 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1885 else
1886 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1887 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1888 coladvance((colnr_T)MAXCOL);
1889 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1890 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001891
1892 // Insert always starts after the prompt, allow editing text after it.
1893 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1894 || Insstart_orig.col != (int)STRLEN(prompt))
1895 {
1896 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001897 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001898 Insstart_orig = Insstart;
1899 Insstart_textlen = Insstart.col;
1900 Insstart_blank_vcol = MAXCOL;
1901 arrow_used = FALSE;
1902 }
1903
Bram Moolenaarf2732452018-06-03 14:47:35 +02001904 if (cmdchar_todo == 'A')
1905 coladvance((colnr_T)MAXCOL);
1906 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001907 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001908 /* Make sure the cursor is in a valid position. */
1909 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001910}
1911
1912/*
1913 * Return TRUE if the cursor is in the editable position of the prompt line.
1914 */
1915 int
1916prompt_curpos_editable()
1917{
1918 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1919 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1920}
1921#endif
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923/*
1924 * Undo the previous edit_putchar().
1925 */
1926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001927edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1930 {
1931#if defined(FEAT_MBYTE)
1932 if (pc_status == PC_STATUS_RIGHT)
1933 ++curwin->w_wcol;
1934 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1935 redrawWinline(curwin->w_cursor.lnum, FALSE);
1936 else
1937#endif
1938 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1939 }
1940}
1941
1942/*
1943 * Called when p_dollar is set: display a '$' at the end of the changed text
1944 * Only works when cursor is in the line that changes.
1945 */
1946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001947display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 colnr_T save_col;
1950
1951 if (!redrawing())
1952 return;
1953
1954 cursor_off();
1955 save_col = curwin->w_cursor.col;
1956 curwin->w_cursor.col = col;
1957#ifdef FEAT_MBYTE
1958 if (has_mbyte)
1959 {
1960 char_u *p;
1961
1962 /* If on the last byte of a multi-byte move to the first byte. */
1963 p = ml_get_curline();
1964 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1965 }
1966#endif
1967 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001968 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
1970 edit_putchar('$', FALSE);
1971 dollar_vcol = curwin->w_virtcol;
1972 }
1973 curwin->w_cursor.col = save_col;
1974}
1975
1976/*
1977 * Call this function before moving the cursor from the normal insert position
1978 * in insert mode.
1979 */
1980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001981undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001983 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001985 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 redrawWinline(curwin->w_cursor.lnum, FALSE);
1987 }
1988}
1989
1990/*
1991 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1992 * Keep the cursor on the same character.
1993 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1994 * type == INDENT_DEC decrease indent (for CTRL-D)
1995 * type == INDENT_SET set indent to "amount"
1996 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1997 */
1998 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001999change_indent(
2000 int type,
2001 int amount,
2002 int round,
2003 int replaced, /* replaced character, put on replace stack */
2004 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 int vcol;
2007 int last_vcol;
2008 int insstart_less; /* reduction for Insstart.col */
2009 int new_cursor_col;
2010 int i;
2011 char_u *ptr;
2012 int save_p_list;
2013 int start_col;
2014 colnr_T vc;
2015#ifdef FEAT_VREPLACE
2016 colnr_T orig_col = 0; /* init for GCC */
2017 char_u *new_line, *orig_line = NULL; /* init for GCC */
2018
2019 /* VREPLACE mode needs to know what the line was like before changing */
2020 if (State & VREPLACE_FLAG)
2021 {
2022 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2023 orig_col = curwin->w_cursor.col;
2024 }
2025#endif
2026
2027 /* for the following tricks we don't want list mode */
2028 save_p_list = curwin->w_p_list;
2029 curwin->w_p_list = FALSE;
2030 vc = getvcol_nolist(&curwin->w_cursor);
2031 vcol = vc;
2032
2033 /*
2034 * For Replace mode we need to fix the replace stack later, which is only
2035 * possible when the cursor is in the indent. Remember the number of
2036 * characters before the cursor if it's possible.
2037 */
2038 start_col = curwin->w_cursor.col;
2039
2040 /* determine offset from first non-blank */
2041 new_cursor_col = curwin->w_cursor.col;
2042 beginline(BL_WHITE);
2043 new_cursor_col -= curwin->w_cursor.col;
2044
2045 insstart_less = curwin->w_cursor.col;
2046
2047 /*
2048 * If the cursor is in the indent, compute how many screen columns the
2049 * cursor is to the left of the first non-blank.
2050 */
2051 if (new_cursor_col < 0)
2052 vcol = get_indent() - vcol;
2053
2054 if (new_cursor_col > 0) /* can't fix replace stack */
2055 start_col = -1;
2056
2057 /*
2058 * Set the new indent. The cursor will be put on the first non-blank.
2059 */
2060 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002061 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 else
2063 {
2064#ifdef FEAT_VREPLACE
2065 int save_State = State;
2066
2067 /* Avoid being called recursively. */
2068 if (State & VREPLACE_FLAG)
2069 State = INSERT;
2070#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002071 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072#ifdef FEAT_VREPLACE
2073 State = save_State;
2074#endif
2075 }
2076 insstart_less -= curwin->w_cursor.col;
2077
2078 /*
2079 * Try to put cursor on same character.
2080 * If the cursor is at or after the first non-blank in the line,
2081 * compute the cursor column relative to the column of the first
2082 * non-blank character.
2083 * If we are not in insert mode, leave the cursor on the first non-blank.
2084 * If the cursor is before the first non-blank, position it relative
2085 * to the first non-blank, counted in screen columns.
2086 */
2087 if (new_cursor_col >= 0)
2088 {
2089 /*
2090 * When changing the indent while the cursor is touching it, reset
2091 * Insstart_col to 0.
2092 */
2093 if (new_cursor_col == 0)
2094 insstart_less = MAXCOL;
2095 new_cursor_col += curwin->w_cursor.col;
2096 }
2097 else if (!(State & INSERT))
2098 new_cursor_col = curwin->w_cursor.col;
2099 else
2100 {
2101 /*
2102 * Compute the screen column where the cursor should be.
2103 */
2104 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002105 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
2107 /*
2108 * Advance the cursor until we reach the right screen column.
2109 */
2110 vcol = last_vcol = 0;
2111 new_cursor_col = -1;
2112 ptr = ml_get_curline();
2113 while (vcol <= (int)curwin->w_virtcol)
2114 {
2115 last_vcol = vcol;
2116#ifdef FEAT_MBYTE
2117 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002118 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 else
2120#endif
2121 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002122 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124 vcol = last_vcol;
2125
2126 /*
2127 * May need to insert spaces to be able to position the cursor on
2128 * the right screen column.
2129 */
2130 if (vcol != (int)curwin->w_virtcol)
2131 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002132 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002134 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (ptr != NULL)
2136 {
2137 new_cursor_col += i;
2138 ptr[i] = NUL;
2139 while (--i >= 0)
2140 ptr[i] = ' ';
2141 ins_str(ptr);
2142 vim_free(ptr);
2143 }
2144 }
2145
2146 /*
2147 * When changing the indent while the cursor is in it, reset
2148 * Insstart_col to 0.
2149 */
2150 insstart_less = MAXCOL;
2151 }
2152
2153 curwin->w_p_list = save_p_list;
2154
2155 if (new_cursor_col <= 0)
2156 curwin->w_cursor.col = 0;
2157 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002158 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 curwin->w_set_curswant = TRUE;
2160 changed_cline_bef_curs();
2161
2162 /*
2163 * May have to adjust the start of the insert.
2164 */
2165 if (State & INSERT)
2166 {
2167 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2168 {
2169 if ((int)Insstart.col <= insstart_less)
2170 Insstart.col = 0;
2171 else
2172 Insstart.col -= insstart_less;
2173 }
2174 if ((int)ai_col <= insstart_less)
2175 ai_col = 0;
2176 else
2177 ai_col -= insstart_less;
2178 }
2179
2180 /*
2181 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2182 * If the number of characters before the cursor decreased, need to pop a
2183 * few characters from the replace stack.
2184 * If the number of characters before the cursor increased, need to push a
2185 * few NULs onto the replace stack.
2186 */
2187 if (REPLACE_NORMAL(State) && start_col >= 0)
2188 {
2189 while (start_col > (int)curwin->w_cursor.col)
2190 {
2191 replace_join(0); /* remove a NUL from the replace stack */
2192 --start_col;
2193 }
2194 while (start_col < (int)curwin->w_cursor.col || replaced)
2195 {
2196 replace_push(NUL);
2197 if (replaced)
2198 {
2199 replace_push(replaced);
2200 replaced = NUL;
2201 }
2202 ++start_col;
2203 }
2204 }
2205
2206#ifdef FEAT_VREPLACE
2207 /*
2208 * For VREPLACE mode, we also have to fix the replace stack. In this case
2209 * it is always possible because we backspace over the whole line and then
2210 * put it back again the way we wanted it.
2211 */
2212 if (State & VREPLACE_FLAG)
2213 {
2214 /* If orig_line didn't allocate, just return. At least we did the job,
2215 * even if you can't backspace. */
2216 if (orig_line == NULL)
2217 return;
2218
2219 /* Save new line */
2220 new_line = vim_strsave(ml_get_curline());
2221 if (new_line == NULL)
2222 return;
2223
2224 /* We only put back the new line up to the cursor */
2225 new_line[curwin->w_cursor.col] = NUL;
2226
2227 /* Put back original line */
2228 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2229 curwin->w_cursor.col = orig_col;
2230
2231 /* Backspace from cursor to start of line */
2232 backspace_until_column(0);
2233
2234 /* Insert new stuff into line again */
2235 ins_bytes(new_line);
2236
2237 vim_free(new_line);
2238 }
2239#endif
2240}
2241
2242/*
2243 * Truncate the space at the end of a line. This is to be used only in an
2244 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2245 * modes.
2246 */
2247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
2250 int i;
2251
2252 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002253 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
2255 if (State & REPLACE_FLAG)
2256 replace_join(0); /* remove a NUL from the replace stack */
2257 }
2258 line[i + 1] = NUL;
2259}
2260
2261#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2262 || defined(FEAT_COMMENTS) || defined(PROTO)
2263/*
2264 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2265 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002266 * Will attempt not to go before "col" even when there is a composing
2267 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 */
2269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002270backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 while ((int)curwin->w_cursor.col > col)
2273 {
2274 curwin->w_cursor.col--;
2275 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002276 replace_do_bs(col);
2277 else if (!del_char_after_col(col))
2278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280}
2281#endif
2282
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002283/*
2284 * Like del_char(), but make sure not to go before column "limit_col".
2285 * Only matters when there are composing characters.
2286 * Return TRUE when something was deleted.
2287 */
2288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002289del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002290{
2291#ifdef FEAT_MBYTE
2292 if (enc_utf8 && limit_col >= 0)
2293 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002294 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002295
2296 /* Make sure the cursor is at the start of a character, but
2297 * skip forward again when going too far back because of a
2298 * composing character. */
2299 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002300 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002301 {
2302 int l = utf_ptr2len(ml_get_cursor());
2303
2304 if (l == 0) /* end of line */
2305 break;
2306 curwin->w_cursor.col += l;
2307 }
2308 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2309 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002310 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002311 }
2312 else
2313#endif
2314 (void)del_char(FALSE);
2315 return TRUE;
2316}
2317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2319/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002320 * CTRL-X pressed in Insert mode.
2321 */
2322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002323ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002324{
2325 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2326 * CTRL-V works like CTRL-N */
2327 if (ctrl_x_mode != CTRL_X_CMDLINE)
2328 {
2329 /* if the next ^X<> won't ADD nothing, then reset
2330 * compl_cont_status */
2331 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002332 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002333 else
2334 compl_cont_status = 0;
2335 /* We're not sure which CTRL-X mode it will be yet */
2336 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2337 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2338 edit_submode_pre = NULL;
2339 showmode();
2340 }
2341}
2342
2343/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002344 * Whether other than default completion has been selected.
2345 */
2346 int
2347ctrl_x_mode_not_default(void)
2348{
2349 return ctrl_x_mode != CTRL_X_NORMAL;
2350}
2351
2352/*
2353 * Whether CTRL-X was typed without a following character.
2354 */
2355 int
2356ctrl_x_mode_not_defined_yet(void)
2357{
2358 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2359}
2360
2361/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002362 * Return TRUE if the 'dict' or 'tsr' option can be used.
2363 */
2364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002365has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002366{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002367 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002368# ifdef FEAT_SPELL
2369 && !curwin->w_p_spell
2370# endif
2371 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002372 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2373 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002374 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002375 edit_submode = NULL;
2376 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2377 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002378 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002379 if (emsg_silent == 0)
2380 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002381 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002382 setcursor();
2383 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002384#ifdef FEAT_EVAL
2385 if (!get_vim_var_nr(VV_TESTING))
2386#endif
2387 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002388 }
2389 return FALSE;
2390 }
2391 return TRUE;
2392}
2393
2394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2396 * This depends on the current mode.
2397 */
2398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002399vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
2401 /* Always allow ^R - let it's results then be checked */
2402 if (c == Ctrl_R)
2403 return TRUE;
2404
Bram Moolenaare3226be2005-12-18 22:10:00 +00002405 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002406 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002407 return TRUE;
2408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 switch (ctrl_x_mode)
2410 {
2411 case 0: /* Not in any CTRL-X mode */
2412 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2413 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002414 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2416 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2417 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002418 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002419 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 case CTRL_X_SCROLL:
2421 return (c == Ctrl_Y || c == Ctrl_E);
2422 case CTRL_X_WHOLE_LINE:
2423 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2424 case CTRL_X_FILES:
2425 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2426 case CTRL_X_DICTIONARY:
2427 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2428 case CTRL_X_THESAURUS:
2429 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2430 case CTRL_X_TAGS:
2431 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2432#ifdef FEAT_FIND_ID
2433 case CTRL_X_PATH_PATTERNS:
2434 return (c == Ctrl_P || c == Ctrl_N);
2435 case CTRL_X_PATH_DEFINES:
2436 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2437#endif
2438 case CTRL_X_CMDLINE:
2439 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2440 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002441#ifdef FEAT_COMPL_FUNC
2442 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002443 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002444 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002445 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002446#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002447 case CTRL_X_SPELL:
2448 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002449 case CTRL_X_EVAL:
2450 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002452 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 return FALSE;
2454}
2455
2456/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002457 * Return TRUE when character "c" is part of the item currently being
2458 * completed. Used to decide whether to abandon complete mode when the menu
2459 * is visible.
2460 */
2461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002462ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002463{
2464 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2465 /* When expanding an identifier only accept identifier chars. */
2466 return vim_isIDc(c);
2467
2468 switch (ctrl_x_mode)
2469 {
2470 case CTRL_X_FILES:
2471 /* When expanding file name only accept file name chars. But not
2472 * path separators, so that "proto/<Tab>" expands files in
2473 * "proto", not "proto/" as a whole */
2474 return vim_isfilec(c) && !vim_ispathsep(c);
2475
2476 case CTRL_X_CMDLINE:
2477 case CTRL_X_OMNI:
2478 /* Command line and Omni completion can work with just about any
2479 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002480 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002481
2482 case CTRL_X_WHOLE_LINE:
2483 /* For while line completion a space can be part of the line. */
2484 return vim_isprintc(c);
2485 }
2486 return vim_iswordc(c);
2487}
2488
2489/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002490 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002492 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 * the rest of the word to be in -- webb
2494 */
2495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002496ins_compl_add_infercase(
2497 char_u *str,
2498 int len,
2499 int icase,
2500 char_u *fname,
2501 int dir,
2502 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002504 char_u *p;
2505 int i, c;
2506 int actual_len; /* Take multi-byte characters */
2507 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002508 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002509 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 int has_lower = FALSE;
2511 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002513 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002515 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaara2993e12007-08-12 14:38:46 +00002517 /* Find actual length of completion. */
2518#ifdef FEAT_MBYTE
2519 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002521 p = str;
2522 actual_len = 0;
2523 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002525 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002526 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002529 else
2530#endif
2531 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaara2993e12007-08-12 14:38:46 +00002533 /* Find actual length of original text. */
2534#ifdef FEAT_MBYTE
2535 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002537 p = compl_orig_text;
2538 actual_compl_length = 0;
2539 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002541 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002542 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002545 else
2546#endif
2547 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002549 /* "actual_len" may be smaller than "actual_compl_length" when using
2550 * thesaurus, only use the minimum when comparing. */
2551 min_len = actual_len < actual_compl_length
2552 ? actual_len : actual_compl_length;
2553
Bram Moolenaara2993e12007-08-12 14:38:46 +00002554 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002555 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002556 if (wca != NULL)
2557 {
2558 p = str;
2559 for (i = 0; i < actual_len; ++i)
2560#ifdef FEAT_MBYTE
2561 if (has_mbyte)
2562 wca[i] = mb_ptr2char_adv(&p);
2563 else
2564#endif
2565 wca[i] = *(p++);
2566
2567 /* Rule 1: Were any chars converted to lower? */
2568 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002569 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002570 {
2571#ifdef FEAT_MBYTE
2572 if (has_mbyte)
2573 c = mb_ptr2char_adv(&p);
2574 else
2575#endif
2576 c = *(p++);
2577 if (MB_ISLOWER(c))
2578 {
2579 has_lower = TRUE;
2580 if (MB_ISUPPER(wca[i]))
2581 {
2582 /* Rule 1 is satisfied. */
2583 for (i = actual_compl_length; i < actual_len; ++i)
2584 wca[i] = MB_TOLOWER(wca[i]);
2585 break;
2586 }
2587 }
2588 }
2589
2590 /*
2591 * Rule 2: No lower case, 2nd consecutive letter converted to
2592 * upper case.
2593 */
2594 if (!has_lower)
2595 {
2596 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002597 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002598 {
2599#ifdef FEAT_MBYTE
2600 if (has_mbyte)
2601 c = mb_ptr2char_adv(&p);
2602 else
2603#endif
2604 c = *(p++);
2605 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2606 {
2607 /* Rule 2 is satisfied. */
2608 for (i = actual_compl_length; i < actual_len; ++i)
2609 wca[i] = MB_TOUPPER(wca[i]);
2610 break;
2611 }
2612 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2613 }
2614 }
2615
2616 /* Copy the original case of the part we typed. */
2617 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002618 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002619 {
2620#ifdef FEAT_MBYTE
2621 if (has_mbyte)
2622 c = mb_ptr2char_adv(&p);
2623 else
2624#endif
2625 c = *(p++);
2626 if (MB_ISLOWER(c))
2627 wca[i] = MB_TOLOWER(wca[i]);
2628 else if (MB_ISUPPER(c))
2629 wca[i] = MB_TOUPPER(wca[i]);
2630 }
2631
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002632 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002633 * Generate encoding specific output from wide character array.
2634 * Multi-byte characters can occupy up to five bytes more than
2635 * ASCII characters, and we also need one byte for NUL, so stay
2636 * six bytes away from the edge of IObuff.
2637 */
2638 p = IObuff;
2639 i = 0;
2640 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2641#ifdef FEAT_MBYTE
2642 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002643 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002644 else
2645#endif
2646 *(p++) = wca[i++];
2647 *p = NUL;
2648
2649 vim_free(wca);
2650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002652 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2653 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002655 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656}
2657
2658/*
2659 * Add a match to the list of matches.
2660 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002661 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002662 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002664 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002665ins_compl_add(
2666 char_u *str,
2667 int len,
2668 int icase,
2669 char_u *fname,
2670 char_u **cptext, /* extra text for popup menu or NULL */
2671 int cdir,
2672 int flags,
2673 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002675 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002676 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
2678 ui_breakcheck();
2679 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (len < 0)
2682 len = (int)STRLEN(str);
2683
2684 /*
2685 * If the same match is already present, don't add it.
2686 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002687 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002689 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 do
2691 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002692 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002693 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002694 && match->cp_str[len] == NUL)
2695 return NOTDONE;
2696 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002697 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002700 /* Remove any popup menu before changing the list of matches. */
2701 ins_compl_del_pum();
2702
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 /*
2704 * Allocate a new match structure.
2705 * Copy the values to the new match structure.
2706 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002707 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002709 return FAIL;
2710 match->cp_number = -1;
2711 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002712 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002713 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
2715 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002716 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002718 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002719
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002721 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2723 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002724 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002725 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002726 && compl_curr_match->cp_fname != NULL
2727 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002728 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002729 else if (fname != NULL)
2730 {
2731 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002732 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002735 match->cp_fname = NULL;
2736 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002737
2738 if (cptext != NULL)
2739 {
2740 int i;
2741
2742 for (i = 0; i < CPT_COUNT; ++i)
2743 if (cptext[i] != NULL && *cptext[i] != NUL)
2744 match->cp_text[i] = vim_strsave(cptext[i]);
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 /*
2748 * Link the new match structure in the list of matches.
2749 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002750 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002751 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else if (dir == FORWARD)
2753 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002754 match->cp_next = compl_curr_match->cp_next;
2755 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 else /* BACKWARD */
2758 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002759 match->cp_next = compl_curr_match;
2760 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002762 if (match->cp_next)
2763 match->cp_next->cp_prev = match;
2764 if (match->cp_prev)
2765 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002767 compl_first_match = match;
2768 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002770 /*
2771 * Find the longest common string if still doing that.
2772 */
2773 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2774 ins_compl_longest_match(match);
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 return OK;
2777}
2778
2779/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002780 * Return TRUE if "str[len]" matches with match->cp_str, considering
2781 * match->cp_icase.
2782 */
2783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002784ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002785{
2786 if (match->cp_icase)
2787 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2788 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2789}
2790
2791/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002792 * Reduce the longest common string for match "match".
2793 */
2794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002795ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002796{
2797 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002798 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002799 int had_match;
2800
2801 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002802 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002803 /* First match, use it as a whole. */
2804 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002805 if (compl_leader != NULL)
2806 {
2807 had_match = (curwin->w_cursor.col > compl_col);
2808 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002809 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002810 ins_redraw(FALSE);
2811
2812 /* When the match isn't there (to avoid matching itself) remove it
2813 * again after redrawing. */
2814 if (!had_match)
2815 ins_compl_delete();
2816 compl_used_match = FALSE;
2817 }
2818 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002819 else
2820 {
2821 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002822 p = compl_leader;
2823 s = match->cp_str;
2824 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002825 {
2826#ifdef FEAT_MBYTE
2827 if (has_mbyte)
2828 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002829 c1 = mb_ptr2char(p);
2830 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002831 }
2832 else
2833#endif
2834 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002835 c1 = *p;
2836 c2 = *s;
2837 }
2838 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2839 : (c1 != c2))
2840 break;
2841#ifdef FEAT_MBYTE
2842 if (has_mbyte)
2843 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002844 MB_PTR_ADV(p);
2845 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002846 }
2847 else
2848#endif
2849 {
2850 ++p;
2851 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002852 }
2853 }
2854
2855 if (*p != NUL)
2856 {
2857 /* Leader was shortened, need to change the inserted text. */
2858 *p = NUL;
2859 had_match = (curwin->w_cursor.col > compl_col);
2860 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002861 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002862 ins_redraw(FALSE);
2863
2864 /* When the match isn't there (to avoid matching itself) remove it
2865 * again after redrawing. */
2866 if (!had_match)
2867 ins_compl_delete();
2868 }
2869
2870 compl_used_match = FALSE;
2871 }
2872}
2873
2874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 * Add an array of matches to the list of matches.
2876 * Frees matches[].
2877 */
2878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002879ins_compl_add_matches(
2880 int num_matches,
2881 char_u **matches,
2882 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
2884 int i;
2885 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002886 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
Bram Moolenaar572cb562005-08-05 21:35:02 +00002888 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002889 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002890 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002892 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 FreeWild(num_matches, matches);
2894}
2895
2896/* Make the completion list cyclic.
2897 * Return the number of matches (excluding the original).
2898 */
2899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002900ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002902 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 int count = 0;
2904
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002905 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
2907 /*
2908 * Find the end of the list.
2909 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002910 match = compl_first_match;
2911 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002912 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002914 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 ++count;
2916 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002917 match->cp_next = compl_first_match;
2918 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920 return count;
2921}
2922
Bram Moolenaara94bc432006-03-10 21:42:59 +00002923/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002924 * Set variables that store noselect and noinsert behavior from the
2925 * 'completeopt' value.
2926 */
2927 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002928completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002929{
2930 compl_no_insert = FALSE;
2931 compl_no_select = FALSE;
2932 if (strstr((char *)p_cot, "noselect") != NULL)
2933 compl_no_select = TRUE;
2934 if (strstr((char *)p_cot, "noinsert") != NULL)
2935 compl_no_insert = TRUE;
2936}
2937
2938/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002939 * Start completion for the complete() function.
2940 * "startcol" is where the matched text starts (1 is first column).
2941 * "list" is the list of matches.
2942 */
2943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002945{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002946 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002947 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002948
Bram Moolenaara94bc432006-03-10 21:42:59 +00002949 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002950 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002951 ins_compl_prep(' ');
2952 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002953 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002954
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002955 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002956 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002957 startcol = curwin->w_cursor.col;
2958 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002959 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002960 /* compl_pattern doesn't need to be set */
2961 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2962 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002963 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002964 return;
2965
Bram Moolenaare4214502015-03-05 18:08:43 +01002966 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002967
2968 ins_compl_add_list(list);
2969 compl_matches = ins_compl_make_cyclic();
2970 compl_started = TRUE;
2971 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002972 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002973
2974 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002975 if (compl_no_insert || compl_no_select)
2976 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002977 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002978 if (compl_no_select)
2979 /* Down/Up has no real effect. */
2980 ins_complete(K_UP, FALSE);
2981 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002982 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002983 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002984 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002985
2986 /* Lazily show the popup menu, unless we got interrupted. */
2987 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002988 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002989 out_flush();
2990}
2991
2992
Bram Moolenaar9372a112005-12-06 19:59:18 +00002993/* "compl_match_array" points the currently displayed list of entries in the
2994 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002995static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002996static int compl_match_arraysize;
2997
2998/*
2999 * Update the screen and when there is any scrolling remove the popup menu.
3000 */
3001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003002ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003003{
3004 int h;
3005
3006 if (compl_match_array != NULL)
3007 {
3008 h = curwin->w_cline_height;
3009 update_screen(0);
3010 if (h != curwin->w_cline_height)
3011 ins_compl_del_pum();
3012 }
3013}
3014
3015/*
3016 * Remove any popup menu.
3017 */
3018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003019ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003020{
3021 if (compl_match_array != NULL)
3022 {
3023 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003024 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003025 }
3026}
3027
3028/*
3029 * Return TRUE if the popup menu should be displayed.
3030 */
3031 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003032pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003033{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003034 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003035 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003036 return FALSE;
3037
3038 /* The display looks bad on a B&W display. */
3039 if (t_colors < 8
3040#ifdef FEAT_GUI
3041 && !gui.in_use
3042#endif
3043 )
3044 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003045 return TRUE;
3046}
3047
3048/*
3049 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003050 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003051 */
3052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003053pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003054{
3055 compl_T *compl;
3056 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003057
3058 /* Don't display the popup menu if there are no matches or there is only
3059 * one (ignoring the original text). */
3060 compl = compl_first_match;
3061 i = 0;
3062 do
3063 {
3064 if (compl == NULL
3065 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3066 break;
3067 compl = compl->cp_next;
3068 } while (compl != compl_first_match);
3069
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003070 if (strstr((char *)p_cot, "menuone") != NULL)
3071 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003072 return (i >= 2);
3073}
3074
3075/*
3076 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003077 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003078 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003081{
3082 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003083 compl_T *shown_compl = NULL;
3084 int did_find_shown_match = FALSE;
3085 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003086 int i;
3087 int cur = -1;
3088 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003089 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003090
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003091 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003092 return;
3093
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003094#if defined(FEAT_EVAL)
3095 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3096 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3097#endif
3098
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003099 /* Update the screen before drawing the popup menu over it. */
3100 update_screen(0);
3101
3102 if (compl_match_array == NULL)
3103 {
3104 /* Need to build the popup menu list. */
3105 compl_match_arraysize = 0;
3106 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003107 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003108 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003109 do
3110 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003111 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3112 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003113 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003114 ++compl_match_arraysize;
3115 compl = compl->cp_next;
3116 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003117 if (compl_match_arraysize == 0)
3118 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003119 compl_match_array = (pumitem_T *)alloc_clear(
3120 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003121 * compl_match_arraysize));
3122 if (compl_match_array != NULL)
3123 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003124 /* If the current match is the original text don't find the first
3125 * match after it, don't highlight anything. */
3126 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3127 shown_match_ok = TRUE;
3128
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003129 i = 0;
3130 compl = compl_first_match;
3131 do
3132 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003133 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3134 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003135 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003136 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003137 if (!shown_match_ok)
3138 {
3139 if (compl == compl_shown_match || did_find_shown_match)
3140 {
3141 /* This item is the shown match or this is the
3142 * first displayed item after the shown match. */
3143 compl_shown_match = compl;
3144 did_find_shown_match = TRUE;
3145 shown_match_ok = TRUE;
3146 }
3147 else
3148 /* Remember this displayed match for when the
3149 * shown match is just below it. */
3150 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003151 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003152 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003153
3154 if (compl->cp_text[CPT_ABBR] != NULL)
3155 compl_match_array[i].pum_text =
3156 compl->cp_text[CPT_ABBR];
3157 else
3158 compl_match_array[i].pum_text = compl->cp_str;
3159 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3160 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3161 if (compl->cp_text[CPT_MENU] != NULL)
3162 compl_match_array[i++].pum_extra =
3163 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003164 else
3165 compl_match_array[i++].pum_extra = compl->cp_fname;
3166 }
3167
3168 if (compl == compl_shown_match)
3169 {
3170 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003171
3172 /* When the original text is the shown match don't set
3173 * compl_shown_match. */
3174 if (compl->cp_flags & ORIGINAL_TEXT)
3175 shown_match_ok = TRUE;
3176
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003177 if (!shown_match_ok && shown_compl != NULL)
3178 {
3179 /* The shown match isn't displayed, set it to the
3180 * previously displayed match. */
3181 compl_shown_match = shown_compl;
3182 shown_match_ok = TRUE;
3183 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003184 }
3185 compl = compl->cp_next;
3186 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003187
3188 if (!shown_match_ok) /* no displayed match at all */
3189 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003190 }
3191 }
3192 else
3193 {
3194 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003195 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003196 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3197 || compl_match_array[i].pum_text
3198 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003199 {
3200 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003201 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003202 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003203 }
3204
3205 if (compl_match_array != NULL)
3206 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003207 /* In Replace mode when a $ is displayed at the end of the line only
3208 * part of the screen would be updated. We do need to redraw here. */
3209 dollar_vcol = -1;
3210
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003211 /* Compute the screen column of the start of the completed text.
3212 * Use the cursor to get all wrapping and other settings right. */
3213 col = curwin->w_cursor.col;
3214 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003215 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003216 curwin->w_cursor.col = col;
3217 }
3218}
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#define DICT_FIRST (1) /* use just first element in "dict" */
3221#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003224 * Add any identifiers that match the given pattern in the list of dictionary
3225 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 */
3227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003228ins_compl_dictionaries(
3229 char_u *dict_start,
3230 char_u *pat,
3231 int flags, /* DICT_FIRST and/or DICT_EXACT */
3232 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003234 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 char_u *ptr;
3236 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 char_u **files;
3239 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003241 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
Bram Moolenaar0b238792006-03-02 22:49:12 +00003243 if (*dict == NUL)
3244 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003245#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003246 /* When 'dictionary' is empty and spell checking is enabled use
3247 * "spell". */
3248 if (!thesaurus && curwin->w_p_spell)
3249 dict = (char_u *)"spell";
3250 else
3251#endif
3252 return;
3253 }
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003256 if (buf == NULL)
3257 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003258 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 /* If 'infercase' is set, don't use 'smartcase' here */
3261 save_p_scs = p_scs;
3262 if (curbuf->b_p_inf)
3263 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003264
3265 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3266 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003267 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003268 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003269 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003270 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003271 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003272
3273 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003274 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003275 len = STRLEN(pat_esc) + 10;
3276 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003277 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003278 {
3279 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003280 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003281 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003282 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003283 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3284 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003285 vim_free(ptr);
3286 }
3287 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003288 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003289 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003290 if (regmatch.regprog == NULL)
3291 goto theend;
3292 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3295 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003296 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 /* copy one dictionary file name into buf */
3299 if (flags == DICT_EXACT)
3300 {
3301 count = 1;
3302 files = &dict;
3303 }
3304 else
3305 {
3306 /* Expand wildcards in the dictionary name, but do not allow
3307 * backticks (for security, the 'dict' option may have been set in
3308 * a modeline). */
3309 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003310# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003311 if (!thesaurus && STRCMP(buf, "spell") == 0)
3312 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003313 else
3314# endif
3315 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 || expand_wildcards(1, &buf, &count, &files,
3317 EW_FILE|EW_SILENT) != OK)
3318 count = 0;
3319 }
3320
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003321# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003322 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003324 /* Complete from active spelling. Skip "\<" in the pattern, we
3325 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003326 if (pat[0] == '\\' && pat[1] == '<')
3327 ptr = pat + 2;
3328 else
3329 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003330 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003332 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003333# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003334 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003335 {
3336 ins_compl_files(count, files, thesaurus, flags,
3337 &regmatch, buf, &dir);
3338 if (flags != DICT_EXACT)
3339 FreeWild(count, files);
3340 }
3341 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 break;
3343 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003344
3345theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003347 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 vim_free(buf);
3349}
3350
Bram Moolenaar0b238792006-03-02 22:49:12 +00003351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003352ins_compl_files(
3353 int count,
3354 char_u **files,
3355 int thesaurus,
3356 int flags,
3357 regmatch_T *regmatch,
3358 char_u *buf,
3359 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003360{
3361 char_u *ptr;
3362 int i;
3363 FILE *fp;
3364 int add_r;
3365
3366 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3367 {
3368 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3369 if (flags != DICT_EXACT)
3370 {
3371 vim_snprintf((char *)IObuff, IOSIZE,
3372 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003373 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003374 }
3375
3376 if (fp != NULL)
3377 {
3378 /*
3379 * Read dictionary file line by line.
3380 * Check each line for a match.
3381 */
3382 while (!got_int && !compl_interrupted
3383 && !vim_fgets(buf, LSIZE, fp))
3384 {
3385 ptr = buf;
3386 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3387 {
3388 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003389 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003390 ptr = find_line_end(ptr);
3391 else
3392 ptr = find_word_end(ptr);
3393 add_r = ins_compl_add_infercase(regmatch->startp[0],
3394 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003395 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003396 if (thesaurus)
3397 {
3398 char_u *wstart;
3399
3400 /*
3401 * Add the other matches on the line
3402 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003403 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003404 while (!got_int)
3405 {
3406 /* Find start of the next word. Skip white
3407 * space and punctuation. */
3408 ptr = find_word_start(ptr);
3409 if (*ptr == NUL || *ptr == NL)
3410 break;
3411 wstart = ptr;
3412
Bram Moolenaara2993e12007-08-12 14:38:46 +00003413 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003414#ifdef FEAT_MBYTE
3415 if (has_mbyte)
3416 /* Japanese words may have characters in
3417 * different classes, only separate words
3418 * with single-byte non-word characters. */
3419 while (*ptr != NUL)
3420 {
3421 int l = (*mb_ptr2len)(ptr);
3422
3423 if (l < 2 && !vim_iswordc(*ptr))
3424 break;
3425 ptr += l;
3426 }
3427 else
3428#endif
3429 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003430
3431 /* Add the word. Skip the regexp match. */
3432 if (wstart != regmatch->startp[0])
3433 add_r = ins_compl_add_infercase(wstart,
3434 (int)(ptr - wstart),
3435 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003436 }
3437 }
3438 if (add_r == OK)
3439 /* if dir was BACKWARD then honor it just once */
3440 *dir = FORWARD;
3441 else if (add_r == FAIL)
3442 break;
3443 /* avoid expensive call to vim_regexec() when at end
3444 * of line */
3445 if (*ptr == '\n' || got_int)
3446 break;
3447 }
3448 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003449 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003450 }
3451 fclose(fp);
3452 }
3453 }
3454}
3455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456/*
3457 * Find the start of the next word.
3458 * Returns a pointer to the first char of the word. Also stops at a NUL.
3459 */
3460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003461find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
3463#ifdef FEAT_MBYTE
3464 if (has_mbyte)
3465 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003466 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 else
3468#endif
3469 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3470 ++ptr;
3471 return ptr;
3472}
3473
3474/*
3475 * Find the end of the word. Assumes it starts inside a word.
3476 * Returns a pointer to just after the word.
3477 */
3478 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003479find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481#ifdef FEAT_MBYTE
3482 int start_class;
3483
3484 if (has_mbyte)
3485 {
3486 start_class = mb_get_class(ptr);
3487 if (start_class > 1)
3488 while (*ptr != NUL)
3489 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003490 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (mb_get_class(ptr) != start_class)
3492 break;
3493 }
3494 }
3495 else
3496#endif
3497 while (vim_iswordc(*ptr))
3498 ++ptr;
3499 return ptr;
3500}
3501
3502/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003503 * Find the end of the line, omitting CR and NL at the end.
3504 * Returns a pointer to just after the line.
3505 */
3506 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003507find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003508{
3509 char_u *s;
3510
3511 s = ptr + STRLEN(ptr);
3512 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3513 --s;
3514 return s;
3515}
3516
3517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 * Free the list of completions
3519 */
3520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003521ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003523 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003524 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaard23a8232018-02-10 18:45:26 +01003526 VIM_CLEAR(compl_pattern);
3527 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003529 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003531
3532 ins_compl_del_pum();
3533 pum_clear();
3534
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003535 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 do
3537 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003538 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003539 compl_curr_match = compl_curr_match->cp_next;
3540 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003542 if (match->cp_flags & FREE_FNAME)
3543 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003544 for (i = 0; i < CPT_COUNT; ++i)
3545 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003547 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3548 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003549 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003550 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551}
3552
3553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003554ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003556 compl_cont_status = 0;
3557 compl_started = FALSE;
3558 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003559 VIM_CLEAR(compl_pattern);
3560 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003562 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003563 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003564 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003565 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003569 * Return TRUE when Insert completion is active.
3570 */
3571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003572ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003573{
3574 return compl_started;
3575}
3576
3577/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003578 * Delete one character before the cursor and show the subset of the matches
3579 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003580 * Returns the character to be used, NUL if the work is done and another char
3581 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003582 */
3583 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003585{
3586 char_u *line;
3587 char_u *p;
3588
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003589 line = ml_get_curline();
3590 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003591 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003592
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003593 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003594 * allow the word to be deleted, we won't match everything.
3595 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003596 if ((int)(p - line) - (int)compl_col < 0
3597 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003598 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3599 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3600 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003601 return K_BS;
3602
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003603 /* Deleted more than what was used to find matches or didn't finish
3604 * finding all matches: need to look for matches all over again. */
3605 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003606 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003607 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003608
Bram Moolenaara6557602006-02-04 22:43:20 +00003609 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003610 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003611 if (compl_leader != NULL)
3612 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003613 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003614 if (compl_shown_match != NULL)
3615 /* Make sure current match is not a hidden item. */
3616 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003617 return NUL;
3618 }
3619 return K_BS;
3620}
Bram Moolenaara6557602006-02-04 22:43:20 +00003621
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003622/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003623 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3624 * be called.
3625 */
3626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003627ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003628{
3629 /* Return TRUE if we didn't complete finding matches or when the
3630 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3631 return compl_was_interrupted
3632 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3633 && compl_opt_refresh_always);
3634}
3635
3636/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003637 * Called after changing "compl_leader".
3638 * Show the popup menu with a different set of matches.
3639 * May also search for matches again if the previous search was interrupted.
3640 */
3641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003643{
3644 ins_compl_del_pum();
3645 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003646 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003647 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003648
3649 if (compl_started)
3650 ins_compl_set_original_text(compl_leader);
3651 else
3652 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003653#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003654 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003655#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003656 /*
3657 * Matches were cleared, need to search for them now. First display
3658 * the changed text before the cursor. Set "compl_restarting" to
3659 * avoid that the first match is inserted.
3660 */
3661 update_screen(0);
3662#ifdef FEAT_GUI
3663 if (gui.in_use)
3664 {
3665 /* Show the cursor after the match, not after the redrawn text. */
3666 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003667 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003668 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003669#endif
3670 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003671 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003672 compl_cont_status = 0;
3673 compl_restarting = FALSE;
3674 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003675
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003676 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003677
3678 /* Show the popup menu with a different set of matches. */
3679 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003680
3681 /* Don't let Enter select the original text when there is no popup menu. */
3682 if (compl_match_array == NULL)
3683 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003684}
3685
3686/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003687 * Return the length of the completion, from the completion start column to
3688 * the cursor column. Making sure it never goes below zero.
3689 */
3690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003691ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003692{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003693 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003694
3695 if (off < 0)
3696 return 0;
3697 return off;
3698}
3699
3700/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003701 * Append one character to the match leader. May reduce the number of
3702 * matches.
3703 */
3704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003706{
3707#ifdef FEAT_MBYTE
3708 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003709#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003710
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003711 if (stop_arrow() == FAIL)
3712 return;
3713#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003714 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3715 {
3716 char_u buf[MB_MAXBYTES + 1];
3717
3718 (*mb_char2bytes)(c, buf);
3719 buf[cc] = NUL;
3720 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003721 if (compl_opt_refresh_always)
3722 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003723 }
3724 else
3725#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003726 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003727 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003728 if (compl_opt_refresh_always)
3729 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003730 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003731
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003732 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003733 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003734 ins_compl_restart();
3735
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003736 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003737 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003738 * break redo. */
3739 if (!compl_opt_refresh_always)
3740 {
3741 vim_free(compl_leader);
3742 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003743 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003744 if (compl_leader != NULL)
3745 ins_compl_new_leader();
3746 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003747}
3748
3749/*
3750 * Setup for finding completions again without leaving CTRL-X mode. Used when
3751 * BS or a key was typed while still searching for matches.
3752 */
3753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003754ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003755{
3756 ins_compl_free();
3757 compl_started = FALSE;
3758 compl_matches = 0;
3759 compl_cont_status = 0;
3760 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003761}
3762
3763/*
3764 * Set the first match, the original text.
3765 */
3766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003767ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003768{
3769 char_u *p;
3770
Bram Moolenaare87edf32018-04-17 22:14:32 +02003771 /* Replace the original text entry.
3772 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3773 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003774 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3775 {
3776 p = vim_strsave(str);
3777 if (p != NULL)
3778 {
3779 vim_free(compl_first_match->cp_str);
3780 compl_first_match->cp_str = p;
3781 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003782 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003783 else if (compl_first_match->cp_prev != NULL
3784 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3785 {
3786 p = vim_strsave(str);
3787 if (p != NULL)
3788 {
3789 vim_free(compl_first_match->cp_prev->cp_str);
3790 compl_first_match->cp_prev->cp_str = p;
3791 }
3792 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003793}
3794
3795/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003796 * Append one character to the match leader. May reduce the number of
3797 * matches.
3798 */
3799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003800ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003801{
3802 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003803 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003804 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003805 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003806
3807 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003808 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003809 {
3810 /* When still at the original match use the first entry that matches
3811 * the leader. */
3812 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3813 {
3814 p = NULL;
3815 for (cp = compl_shown_match->cp_next; cp != NULL
3816 && cp != compl_first_match; cp = cp->cp_next)
3817 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003818 if (compl_leader == NULL
3819 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003820 (int)STRLEN(compl_leader)))
3821 {
3822 p = cp->cp_str;
3823 break;
3824 }
3825 }
3826 if (p == NULL || (int)STRLEN(p) <= len)
3827 return;
3828 }
3829 else
3830 return;
3831 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003832 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003833 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003834 ins_compl_addleader(c);
3835}
3836
3837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003839 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003840 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003843ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
3845 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003847 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 /* Forget any previous 'special' messages if this is actually
3850 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3851 */
3852 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3853 edit_submode_extra = NULL;
3854
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003855 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003856 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3857 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003858 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003860 /* Set "compl_get_longest" when finding the first matches. */
3861 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003862 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003863 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003864 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003865 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003866
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003867 }
3868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3870 {
3871 /*
3872 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3873 * it will be yet. Now we decide.
3874 */
3875 switch (c)
3876 {
3877 case Ctrl_E:
3878 case Ctrl_Y:
3879 ctrl_x_mode = CTRL_X_SCROLL;
3880 if (!(State & REPLACE_FLAG))
3881 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3882 else
3883 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3884 edit_submode_pre = NULL;
3885 showmode();
3886 break;
3887 case Ctrl_L:
3888 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3889 break;
3890 case Ctrl_F:
3891 ctrl_x_mode = CTRL_X_FILES;
3892 break;
3893 case Ctrl_K:
3894 ctrl_x_mode = CTRL_X_DICTIONARY;
3895 break;
3896 case Ctrl_R:
3897 /* Simply allow ^R to happen without affecting ^X mode */
3898 break;
3899 case Ctrl_T:
3900 ctrl_x_mode = CTRL_X_THESAURUS;
3901 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003902#ifdef FEAT_COMPL_FUNC
3903 case Ctrl_U:
3904 ctrl_x_mode = CTRL_X_FUNCTION;
3905 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003907 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003909#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003910 case 's':
3911 case Ctrl_S:
3912 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003913#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003914 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003915 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003916 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003917#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 case Ctrl_RSB:
3920 ctrl_x_mode = CTRL_X_TAGS;
3921 break;
3922#ifdef FEAT_FIND_ID
3923 case Ctrl_I:
3924 case K_S_TAB:
3925 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3926 break;
3927 case Ctrl_D:
3928 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3929 break;
3930#endif
3931 case Ctrl_V:
3932 case Ctrl_Q:
3933 ctrl_x_mode = CTRL_X_CMDLINE;
3934 break;
3935 case Ctrl_P:
3936 case Ctrl_N:
3937 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3938 * just started ^X mode, or there were enough ^X's to cancel
3939 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3940 * do normal expansion when interrupting a different mode (say
3941 * ^X^F^X^P or ^P^X^X^P, see below)
3942 * nothing changes if interrupting mode 0, (eg, the flag
3943 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003944 if (!(compl_cont_status & CONT_INTRPT))
3945 compl_cont_status |= CONT_LOCAL;
3946 else if (compl_cont_mode != 0)
3947 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 /* FALLTHROUGH */
3949 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003950 /* If we have typed at least 2 ^X's... for modes != 0, we set
3951 * compl_cont_status = 0 (eg, as if we had just started ^X
3952 * mode).
3953 * For mode 0, we set "compl_cont_mode" to an impossible
3954 * value, in both cases ^X^X can be used to restart the same
3955 * mode (avoiding ADDING mode).
3956 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3957 * 'complete' and local ^P expansions respectively.
3958 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3959 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (c == Ctrl_X)
3961 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 if (compl_cont_mode != 0)
3963 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003965 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003967 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 edit_submode = NULL;
3969 showmode();
3970 break;
3971 }
3972 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003973 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
3975 /* We're already in CTRL-X mode, do we stay in it? */
3976 if (!vim_is_ctrl_x_key(c))
3977 {
3978 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003979 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 else
3981 ctrl_x_mode = CTRL_X_FINISHED;
3982 edit_submode = NULL;
3983 }
3984 showmode();
3985 }
3986
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003987 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
3989 /* Show error message from attempted keyword completion (probably
3990 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003991 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003993 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3994 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 || ctrl_x_mode == CTRL_X_FINISHED)
3996 {
3997 /* Get here when we have finished typing a sequence of ^N and
3998 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003999 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004000 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
4002 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004003 * If any of the original typed text has been changed, eg when
4004 * ignorecase is set, we must add back-spaces to the redo
4005 * buffer. We add as few as necessary to delete just the part
4006 * of the original text that has changed.
4007 * When using the longest match, edited the match or used
4008 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004010 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4011 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004012 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004013 ptr = NULL;
4014 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016
4017#ifdef FEAT_CINDENT
4018 want_cindent = (can_cindent && cindent_on());
4019#endif
4020 /*
4021 * When completing whole lines: fix indent for 'cindent'.
4022 * Otherwise, break line if it's too long.
4023 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026#ifdef FEAT_CINDENT
4027 /* re-indent the current line */
4028 if (want_cindent)
4029 {
4030 do_c_expr_indent();
4031 want_cindent = FALSE; /* don't do it again */
4032 }
4033#endif
4034 }
4035 else
4036 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004037 int prev_col = curwin->w_cursor.col;
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004040 if (prev_col > 0)
4041 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004042 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004043 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004045 if (prev_col > 0
4046 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4047 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004050 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004051 * the selection without inserting anything. When
4052 * compl_enter_selects is set the Enter key does the same. */
4053 if ((c == Ctrl_Y || (compl_enter_selects
4054 && (c == CAR || c == K_KENTER || c == NL)))
4055 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004056 retval = TRUE;
4057
Bram Moolenaar47247282016-08-02 22:36:02 +02004058 /* CTRL-E means completion is Ended, go back to the typed text.
4059 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004060 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004061 {
4062 ins_compl_delete();
4063 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004064 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004065 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004066 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004067 retval = TRUE;
4068 }
4069
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004070 auto_format(FALSE, TRUE);
4071
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004073 compl_started = FALSE;
4074 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004075 if (!shortmess(SHM_COMPLETIONMENU))
4076 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004077 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004078 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 if (edit_submode != NULL)
4080 {
4081 edit_submode = NULL;
4082 showmode();
4083 }
4084
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004085#ifdef FEAT_CMDWIN
4086 if (c == Ctrl_C && cmdwin_type != 0)
4087 /* Avoid the popup menu remains displayed when leaving the
4088 * command line window. */
4089 update_screen(0);
4090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091#ifdef FEAT_CINDENT
4092 /*
4093 * Indent now if a key was typed that is in 'cinkeys'.
4094 */
4095 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4096 do_c_expr_indent();
4097#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004098 /* Trigger the CompleteDone event to give scripts a chance to act
4099 * upon the completion. */
4100 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004103 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4104 /* Trigger the CompleteDone event to give scripts a chance to act
4105 * upon the (possibly failed) completion. */
4106 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 /* reset continue_* if we left expansion-mode, if we stay they'll be
4109 * (re)set properly in ins_complete() */
4110 if (!vim_is_ctrl_x_key(c))
4111 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004112 compl_cont_status = 0;
4113 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004115
4116 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117}
4118
4119/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004120 * Fix the redo buffer for the completion leader replacing some of the typed
4121 * text. This inserts backspaces and appends the changed text.
4122 * "ptr" is the known leader text or NUL.
4123 */
4124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004125ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004126{
4127 int len;
4128 char_u *p;
4129 char_u *ptr = ptr_arg;
4130
4131 if (ptr == NULL)
4132 {
4133 if (compl_leader != NULL)
4134 ptr = compl_leader;
4135 else
4136 return; /* nothing to do */
4137 }
4138 if (compl_orig_text != NULL)
4139 {
4140 p = compl_orig_text;
4141 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4142 ;
4143#ifdef FEAT_MBYTE
4144 if (len > 0)
4145 len -= (*mb_head_off)(p, p + len);
4146#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004147 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004148 AppendCharToRedobuff(K_BS);
4149 }
4150 else
4151 len = 0;
4152 if (ptr != NULL)
4153 AppendToRedobuffLit(ptr + len, -1);
4154}
4155
4156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4158 * (depending on flag) starting from buf and looking for a non-scanned
4159 * buffer (other than curbuf). curbuf is special, if it is called with
4160 * buf=curbuf then it has to be the first call for a given flag/expansion.
4161 *
4162 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4163 */
4164 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004165ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 if (flag == 'w') /* just windows */
4170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if (buf == curbuf) /* first call for this flag/expansion */
4172 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004173 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 && wp->w_buffer->b_scanned)
4175 ;
4176 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178 else
4179 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4180 * (unlisted buffers)
4181 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004182 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 && ((flag == 'U'
4184 ? buf->b_p_bl
4185 : (!buf->b_p_bl
4186 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004187 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 ;
4189 return buf;
4190}
4191
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004192#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004193/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004194 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004195 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004196 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004198expand_by_function(
4199 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4200 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004201{
Bram Moolenaar82139082011-09-14 16:52:09 +02004202 list_T *matchlist = NULL;
4203 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004204 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004205 char_u *funcname;
4206 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004207 win_T *curwin_save;
4208 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004209 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004210
Bram Moolenaare344bea2005-09-01 20:46:49 +00004211 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4212 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004213 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004214
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004215 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004216 args[0].v_type = VAR_NUMBER;
4217 args[0].vval.v_number = 0;
4218 args[1].v_type = VAR_STRING;
4219 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4220 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004221
Bram Moolenaare344bea2005-09-01 20:46:49 +00004222 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004223 curwin_save = curwin;
4224 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004225
4226 /* Call a function, which returns a list or dict. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004227 if (call_vim_function(funcname, 2, args, &rettv, FALSE) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004228 {
4229 switch (rettv.v_type)
4230 {
4231 case VAR_LIST:
4232 matchlist = rettv.vval.v_list;
4233 break;
4234 case VAR_DICT:
4235 matchdict = rettv.vval.v_dict;
4236 break;
4237 default:
4238 /* TODO: Give error message? */
4239 clear_tv(&rettv);
4240 break;
4241 }
4242 }
4243
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004244 if (curwin_save != curwin || curbuf_save != curbuf)
4245 {
4246 EMSG(_(e_complwin));
4247 goto theend;
4248 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004249 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004250 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004251 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004252 {
4253 EMSG(_(e_compldel));
4254 goto theend;
4255 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004256
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004257 if (matchlist != NULL)
4258 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004259 else if (matchdict != NULL)
4260 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004261
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004262theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004263 if (matchdict != NULL)
4264 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004265 if (matchlist != NULL)
4266 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004267}
4268#endif /* FEAT_COMPL_FUNC */
4269
Bram Moolenaar39f05632006-03-19 22:15:26 +00004270#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004271/*
4272 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004273 */
4274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004275ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004276{
4277 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004278 int dir = compl_direction;
4279
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004280 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004281 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004282 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004283 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4284 /* if dir was BACKWARD then honor it just once */
4285 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004286 else if (did_emsg)
4287 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004288 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004289}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004290
4291/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004292 * Add completions from a dict.
4293 */
4294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004295ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004296{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004297 dictitem_T *di_refresh;
4298 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004299
4300 /* Check for optional "refresh" item. */
4301 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004302 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4303 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004304 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004305 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004306
4307 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4308 compl_opt_refresh_always = TRUE;
4309 }
4310
4311 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004312 di_words = dict_find(dict, (char_u *)"words", 5);
4313 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4314 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004315}
4316
4317/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004318 * Add a match to the list of matches from a typeval_T.
4319 * If the given string is already in the list of completions, then return
4320 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4321 * maybe because alloc() returns NULL, then FAIL is returned.
4322 */
4323 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004324ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004325{
4326 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004327 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004328 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004329 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004330 char_u *(cptext[CPT_COUNT]);
4331
4332 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4333 {
4334 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4335 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4336 (char_u *)"abbr", FALSE);
4337 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4338 (char_u *)"menu", FALSE);
4339 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4340 (char_u *)"kind", FALSE);
4341 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4342 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004343 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4344 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004345 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4346 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004347 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004348 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004349 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4350 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004351 }
4352 else
4353 {
4354 word = get_tv_string_chk(tv);
4355 vim_memset(cptext, 0, sizeof(cptext));
4356 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004357 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004358 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004359 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004360}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004361#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004362
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363/*
4364 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004365 * The search starts at position "ini" in curbuf and in the direction
4366 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004367 * When "compl_started" is FALSE start at that position, otherwise continue
4368 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369 * This may return before finding all the matches.
4370 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 */
4372 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004373ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374{
4375 static pos_T first_match_pos;
4376 static pos_T last_match_pos;
4377 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004378 static int found_all = FALSE; /* Found all matches of a
4379 certain type. */
4380 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
Bram Moolenaar572cb562005-08-05 21:35:02 +00004382 pos_T *pos;
4383 char_u **matches;
4384 int save_p_scs;
4385 int save_p_ws;
4386 int save_p_ic;
4387 int i;
4388 int num_matches;
4389 int len;
4390 int found_new_match;
4391 int type = ctrl_x_mode;
4392 char_u *ptr;
4393 char_u *dict = NULL;
4394 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004395 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004397 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004399 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 ins_buf->b_scanned = 0;
4401 found_all = FALSE;
4402 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004404 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 last_match_pos = first_match_pos = *ini;
4406 }
4407
Bram Moolenaar4475b622017-05-01 20:46:52 +02004408 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004409 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4411 for (;;)
4412 {
4413 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004414 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004416 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 * or if found_all says this entry is done. For ^X^L only use the
4418 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004419 if ((ctrl_x_mode == CTRL_X_NORMAL
4420 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
4423 found_all = FALSE;
4424 while (*e_cpt == ',' || *e_cpt == ' ')
4425 e_cpt++;
4426 if (*e_cpt == '.' && !curbuf->b_scanned)
4427 {
4428 ins_buf = curbuf;
4429 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004430 /* Move the cursor back one character so that ^N can match the
4431 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004432 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004433 {
4434 /* Move the cursor to after the last character in the
4435 * buffer, so that word at start of buffer is found
4436 * correctly. */
4437 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4438 first_match_pos.col =
4439 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 last_match_pos = first_match_pos;
4442 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004443
4444 /* Remember the first match so that the loop stops when we
4445 * wrap and come back there a second time. */
4446 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4449 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4450 {
4451 /* Scan a buffer, but not the current one. */
4452 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4453 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 first_match_pos.col = last_match_pos.col = 0;
4456 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4457 last_match_pos.lnum = 0;
4458 type = 0;
4459 }
4460 else /* unloaded buffer, scan like dictionary */
4461 {
4462 found_all = TRUE;
4463 if (ins_buf->b_fname == NULL)
4464 continue;
4465 type = CTRL_X_DICTIONARY;
4466 dict = ins_buf->b_fname;
4467 dict_f = DICT_EXACT;
4468 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004469 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 ins_buf->b_fname == NULL
4471 ? buf_spname(ins_buf)
4472 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004473 ? ins_buf->b_fname
4474 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004475 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 else if (*e_cpt == NUL)
4478 break;
4479 else
4480 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004481 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 type = -1;
4483 else if (*e_cpt == 'k' || *e_cpt == 's')
4484 {
4485 if (*e_cpt == 'k')
4486 type = CTRL_X_DICTIONARY;
4487 else
4488 type = CTRL_X_THESAURUS;
4489 if (*++e_cpt != ',' && *e_cpt != NUL)
4490 {
4491 dict = e_cpt;
4492 dict_f = DICT_FIRST;
4493 }
4494 }
4495#ifdef FEAT_FIND_ID
4496 else if (*e_cpt == 'i')
4497 type = CTRL_X_PATH_PATTERNS;
4498 else if (*e_cpt == 'd')
4499 type = CTRL_X_PATH_DEFINES;
4500#endif
4501 else if (*e_cpt == ']' || *e_cpt == 't')
4502 {
4503 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004504 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004505 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507 else
4508 type = -1;
4509
4510 /* in any case e_cpt is advanced to the next entry */
4511 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4512
4513 found_all = TRUE;
4514 if (type == -1)
4515 continue;
4516 }
4517 }
4518
Bram Moolenaar4475b622017-05-01 20:46:52 +02004519 /* If complete() was called then compl_pattern has been reset. The
4520 * following won't work then, bail out. */
4521 if (compl_pattern == NULL)
4522 break;
4523
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 switch (type)
4525 {
4526 case -1:
4527 break;
4528#ifdef FEAT_FIND_ID
4529 case CTRL_X_PATH_PATTERNS:
4530 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004531 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004534 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4536 (linenr_T)1, (linenr_T)MAXLNUM);
4537 break;
4538#endif
4539
4540 case CTRL_X_DICTIONARY:
4541 case CTRL_X_THESAURUS:
4542 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004543 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 : (type == CTRL_X_THESAURUS
4545 ? (*curbuf->b_p_tsr == NUL
4546 ? p_tsr
4547 : curbuf->b_p_tsr)
4548 : (*curbuf->b_p_dict == NUL
4549 ? p_dict
4550 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004551 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004552 dict != NULL ? dict_f
4553 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 dict = NULL;
4555 break;
4556
4557 case CTRL_X_TAGS:
4558 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4559 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004560 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004562 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 * of matches is found when compl_pattern is empty */
4564 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004565 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4566 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4568 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004569 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
4571 p_ic = save_p_ic;
4572 break;
4573
4574 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004575 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4577 {
4578
4579 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004581 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 break;
4584
4585 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004586 if (expand_cmdline(&compl_xp, compl_pattern,
4587 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004589 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 break;
4591
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004592#ifdef FEAT_COMPL_FUNC
4593 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004594 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004595 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004596 break;
4597#endif
4598
Bram Moolenaar488c6512005-08-11 20:09:58 +00004599 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004600#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004601 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004602 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004603 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004604 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004605#endif
4606 break;
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 default: /* normal ^P/^N and ^X^L */
4609 /*
4610 * If 'infercase' is set, don't use 'smartcase' here
4611 */
4612 save_p_scs = p_scs;
4613 if (ins_buf->b_p_inf)
4614 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004615
Bram Moolenaar361aa502014-01-23 22:45:58 +01004616 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 * end but never from the middle, thus setting nowrapscan in this
4618 * buffers is a good idea, on the other hand, we always set
4619 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4620 save_p_ws = p_ws;
4621 if (ins_buf != curbuf)
4622 p_ws = FALSE;
4623 else if (*e_cpt == '.')
4624 p_ws = TRUE;
4625 for (;;)
4626 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004627 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004629 ++msg_silent; /* Don't want messages for wrapscan. */
4630
Bram Moolenaare4214502015-03-05 18:08:43 +01004631 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4632 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004633 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004634 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004637 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004639 found_new_match = searchit(NULL, ins_buf, pos,
4640 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004641 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004642 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004643 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004644 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004646 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 first_match_pos = *pos;
4649 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004650 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004653 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 found_new_match = FAIL;
4655 if (found_new_match == FAIL)
4656 {
4657 if (ins_buf == curbuf)
4658 found_all = TRUE;
4659 break;
4660 }
4661
4662 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004663 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 && ini->lnum == pos->lnum
4665 && ini->col == pos->col)
4666 continue;
4667 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004668 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004670 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
4672 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4673 continue;
4674 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4675 if (!p_paste)
4676 ptr = skipwhite(ptr);
4677 }
4678 len = (int)STRLEN(ptr);
4679 }
4680 else
4681 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004682 char_u *tmp_ptr = ptr;
4683
4684 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004686 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 /* Skip if already inside a word. */
4688 if (vim_iswordp(tmp_ptr))
4689 continue;
4690 /* Find start of next word. */
4691 tmp_ptr = find_word_start(tmp_ptr);
4692 }
4693 /* Find end of this word. */
4694 tmp_ptr = find_word_end(tmp_ptr);
4695 len = (int)(tmp_ptr - ptr);
4696
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004697 if ((compl_cont_status & CONT_ADDING)
4698 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
4700 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4701 {
4702 /* Try next line, if any. the new word will be
4703 * "join" as if the normal command "J" was used.
4704 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004705 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 * works -- Acevedo */
4707 STRNCPY(IObuff, ptr, len);
4708 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4709 tmp_ptr = ptr = skipwhite(ptr);
4710 /* Find start of next word. */
4711 tmp_ptr = find_word_start(tmp_ptr);
4712 /* Find end of next word. */
4713 tmp_ptr = find_word_end(tmp_ptr);
4714 if (tmp_ptr > ptr)
4715 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004716 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004718 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 IObuff[len++] = ' ';
4720 /* IObuf =~ "\k.* ", thus len >= 2 */
4721 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004722 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 || (vim_strchr(p_cpo, CPO_JOINSP)
4724 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004725 && (IObuff[len - 2] == '?'
4726 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 IObuff[len++] = ' ';
4728 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004729 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (tmp_ptr - ptr >= IOSIZE - len)
4731 tmp_ptr = ptr + IOSIZE - len - 1;
4732 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4733 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004734 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736 IObuff[len] = NUL;
4737 ptr = IObuff;
4738 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004739 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 continue;
4741 }
4742 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004743 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004744 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004745 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
4747 found_new_match = OK;
4748 break;
4749 }
4750 }
4751 p_scs = save_p_scs;
4752 p_ws = save_p_ws;
4753 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004754
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004755 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004756 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004757 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 found_new_match = OK;
4759
4760 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004761 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4762 * match */
4763 if ((ctrl_x_mode != CTRL_X_NORMAL
4764 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004765 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004766 {
4767 if (got_int)
4768 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004769 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004770 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004771 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004772
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004773 if ((ctrl_x_mode != CTRL_X_NORMAL
4774 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004775 || compl_interrupted)
4776 break;
4777 compl_started = TRUE;
4778 }
4779 else
4780 {
4781 /* Mark a buffer scanned when it has been scanned completely */
4782 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4783 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004785 compl_started = FALSE;
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004788 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004790 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 && *e_cpt == NUL) /* Got to end of 'complete' */
4792 found_new_match = FAIL;
4793
4794 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004795 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4796 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 i = ins_compl_make_cyclic();
4798
Bram Moolenaar4475b622017-05-01 20:46:52 +02004799 if (compl_old_match != NULL)
4800 {
4801 /* If several matches were added (FORWARD) or the search failed and has
4802 * just been made cyclic then we have to move compl_curr_match to the
4803 * next or previous entry (if any) -- Acevedo */
4804 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4805 : compl_old_match->cp_prev;
4806 if (compl_curr_match == NULL)
4807 compl_curr_match = compl_old_match;
4808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return i;
4810}
4811
4812/* Delete the old text being completed. */
4813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004814ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004816 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
4818 /*
4819 * In insert mode: Delete the typed part.
4820 * In replace mode: Put the old characters back, if any.
4821 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004822 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4823 if ((int)curwin->w_cursor.col > col)
4824 {
4825 if (stop_arrow() == FAIL)
4826 return;
4827 backspace_until_column(col);
4828 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004829
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004830 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4831 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004833 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004834 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835}
4836
Bram Moolenaar472e8592016-10-15 17:06:47 +02004837/*
4838 * Insert the new text being completed.
4839 * "in_compl_func" is TRUE when called from complete_check().
4840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004842ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004844 dict_T *dict;
4845
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004846 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004847 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4848 compl_used_match = FALSE;
4849 else
4850 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004851
4852 /* Set completed item. */
4853 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004854 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004855 if (dict != NULL)
4856 {
4857 dict_add_nr_str(dict, "word", 0L,
4858 EMPTY_IF_NULL(compl_shown_match->cp_str));
4859 dict_add_nr_str(dict, "abbr", 0L,
4860 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4861 dict_add_nr_str(dict, "menu", 0L,
4862 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4863 dict_add_nr_str(dict, "kind", 0L,
4864 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4865 dict_add_nr_str(dict, "info", 0L,
4866 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004867 dict_add_nr_str(dict, "user_data", 0L,
4868 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004869 }
4870 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004871 if (!in_compl_func)
4872 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873}
4874
4875/*
4876 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004877 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4878 * get more completions. If it is FALSE, then we just do nothing when there
4879 * are no more completions in a given direction. The latter case is used when
4880 * we are still in the middle of finding completions, to allow browsing
4881 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 * Return the total number of matches, or -1 if still unknown -- webb.
4883 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004884 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4885 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 *
4887 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004888 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4889 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 */
4891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004892ins_compl_next(
4893 int allow_get_expansion,
4894 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004895 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004896 int insert_match, /* Insert the newly selected match */
4897 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
4899 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004900 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004901 compl_T *found_compl = NULL;
4902 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004903 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004904 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004906 /* When user complete function return -1 for findstart which is next
4907 * time of 'always', compl_shown_match become NULL. */
4908 if (compl_shown_match == NULL)
4909 return -1;
4910
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004911 if (compl_leader != NULL
4912 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004914 /* Set "compl_shown_match" to the actually shown match, it may differ
4915 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004916 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004917 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004918 && compl_shown_match->cp_next != NULL
4919 && compl_shown_match->cp_next != compl_first_match)
4920 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004921
4922 /* If we didn't find it searching forward, and compl_shows_dir is
4923 * backward, find the last match. */
4924 if (compl_shows_dir == BACKWARD
4925 && !ins_compl_equal(compl_shown_match,
4926 compl_leader, (int)STRLEN(compl_leader))
4927 && (compl_shown_match->cp_next == NULL
4928 || compl_shown_match->cp_next == compl_first_match))
4929 {
4930 while (!ins_compl_equal(compl_shown_match,
4931 compl_leader, (int)STRLEN(compl_leader))
4932 && compl_shown_match->cp_prev != NULL
4933 && compl_shown_match->cp_prev != compl_first_match)
4934 compl_shown_match = compl_shown_match->cp_prev;
4935 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004936 }
4937
4938 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004939 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 /* Delete old text to be replaced */
4941 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004942
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004943 /* When finding the longest common text we stick at the original text,
4944 * don't let CTRL-N or CTRL-P move to the first match. */
4945 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4946
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004947 /* When restarting the search don't insert the first match either. */
4948 if (compl_restarting)
4949 {
4950 advance = FALSE;
4951 compl_restarting = FALSE;
4952 }
4953
Bram Moolenaare3226be2005-12-18 22:10:00 +00004954 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4955 * around. */
4956 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004958 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004960 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004961 found_end = (compl_first_match != NULL
4962 && (compl_shown_match->cp_next == compl_first_match
4963 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004964 }
4965 else if (compl_shows_dir == BACKWARD
4966 && compl_shown_match->cp_prev != NULL)
4967 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004968 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004969 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004970 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 }
4972 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004973 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004974 if (!allow_get_expansion)
4975 {
4976 if (advance)
4977 {
4978 if (compl_shows_dir == BACKWARD)
4979 compl_pending -= todo + 1;
4980 else
4981 compl_pending += todo + 1;
4982 }
4983 return -1;
4984 }
4985
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004986 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004987 {
4988 if (compl_shows_dir == BACKWARD)
4989 --compl_pending;
4990 else
4991 ++compl_pending;
4992 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004993
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004994 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004995 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004996
4997 /* handle any pending completions */
4998 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004999 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005000 {
5001 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5002 {
5003 compl_shown_match = compl_shown_match->cp_next;
5004 --compl_pending;
5005 }
5006 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5007 {
5008 compl_shown_match = compl_shown_match->cp_prev;
5009 ++compl_pending;
5010 }
5011 else
5012 break;
5013 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005014 found_end = FALSE;
5015 }
5016 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5017 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005018 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005019 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005020 ++todo;
5021 else
5022 /* Remember a matching item. */
5023 found_compl = compl_shown_match;
5024
5025 /* Stop at the end of the list when we found a usable match. */
5026 if (found_end)
5027 {
5028 if (found_compl != NULL)
5029 {
5030 compl_shown_match = found_compl;
5031 break;
5032 }
5033 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
5036
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005037 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005038 if (compl_no_insert && !started)
5039 {
5040 ins_bytes(compl_orig_text + ins_compl_len());
5041 compl_used_match = FALSE;
5042 }
5043 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005044 {
5045 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005046 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005047 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005048 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005049 }
5050 else
5051 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
5053 if (!allow_get_expansion)
5054 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005055 /* may undisplay the popup menu first */
5056 ins_compl_upd_pum();
5057
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005058 /* redraw to show the user what was inserted */
5059 update_screen(0);
5060
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005061 /* display the updated popup menu */
5062 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005063#ifdef FEAT_GUI
5064 if (gui.in_use)
5065 {
5066 /* Show the cursor after the match, not after the redrawn text. */
5067 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005068 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005069 }
5070#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 /* Delete old text to be replaced, since we're still searching and
5073 * don't want to match ourselves! */
5074 ins_compl_delete();
5075 }
5076
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005077 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005078 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005079 if (compl_no_insert && !started)
5080 compl_enter_selects = TRUE;
5081 else
5082 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005083
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 /*
5085 * Show the file name for the match (if any)
5086 * Truncate the file name to avoid a wait for return.
5087 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005088 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005090 char *lead = _("match in file");
5091 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5092 char_u *s;
5093 char_u *e;
5094
5095 if (space > 0)
5096 {
5097 /* We need the tail that fits. With double-byte encoding going
5098 * back from the end is very slow, thus go from the start and keep
5099 * the text that fits in "space" between "s" and "e". */
5100 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5101 {
5102 space -= ptr2cells(e);
5103 while (space < 0)
5104 {
5105 space += ptr2cells(s);
5106 MB_PTR_ADV(s);
5107 }
5108 }
5109 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5110 s > compl_shown_match->cp_fname ? "<" : "", s);
5111 msg(IObuff);
5112 redraw_cmdline = FALSE; /* don't overwrite! */
5113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115
5116 return num_matches;
5117}
5118
5119/*
5120 * Call this while finding completions, to check whether the user has hit a key
5121 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005122 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005124 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005125 * "in_compl_func" is TRUE when called from complete_check(), don't set
5126 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 */
5128 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005129ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
5131 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005132 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005134 /* Don't check when reading keys from a script, :normal or feedkeys().
5135 * That would break the test scripts. But do check for keys when called
5136 * from complete_check(). */
5137 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 return;
5139
5140 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005141 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 return;
5143 count = 0;
5144
Bram Moolenaara260a972006-06-23 19:36:29 +00005145 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5146 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (c != NUL)
5149 {
5150 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5151 {
5152 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005153 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005154 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005155 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005157 else
5158 {
5159 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005160 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005161 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005162 if (c != K_IGNORE)
5163 {
5164 /* Don't interrupt completion when the character wasn't typed,
5165 * e.g., when doing @q to replay keys. */
5166 if (c != Ctrl_R && KeyTyped)
5167 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005168
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005169 vungetc(c);
5170 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005173 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005174 {
5175 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5176
5177 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005178 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005179 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005180}
5181
5182/*
5183 * Decide the direction of Insert mode complete from the key typed.
5184 * Returns BACKWARD or FORWARD.
5185 */
5186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005187ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005188{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005189 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005190 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005191 return BACKWARD;
5192 return FORWARD;
5193}
5194
5195/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005196 * Return TRUE for keys that are used for completion only when the popup menu
5197 * is visible.
5198 */
5199 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005200ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005201{
5202 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005203 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5204 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005205}
5206
5207/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005208 * Decide the number of completions to move forward.
5209 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5210 */
5211 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005212ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005213{
5214 int h;
5215
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005216 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005217 {
5218 h = pum_get_height();
5219 if (h > 3)
5220 h -= 2; /* keep some context */
5221 return h;
5222 }
5223 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224}
5225
5226/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005227 * Return TRUE if completion with "c" should insert the match, FALSE if only
5228 * to change the currently selected completion.
5229 */
5230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005231ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005232{
5233 switch (c)
5234 {
5235 case K_UP:
5236 case K_DOWN:
5237 case K_PAGEDOWN:
5238 case K_KPAGEDOWN:
5239 case K_S_DOWN:
5240 case K_PAGEUP:
5241 case K_KPAGEUP:
5242 case K_S_UP:
5243 return FALSE;
5244 }
5245 return TRUE;
5246}
5247
5248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 * Do Insert mode completion.
5250 * Called when character "c" was typed, which has a meaning for completion.
5251 * Returns OK if completion was done, FAIL if something failed (out of mem).
5252 */
5253 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005254ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005256 char_u *line;
5257 int startcol = 0; /* column where searched text starts */
5258 colnr_T curs_col; /* cursor column */
5259 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005260 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005261 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005262 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005263 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
Bram Moolenaare3226be2005-12-18 22:10:00 +00005265 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005266 insert_match = ins_compl_use_match(c);
5267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
5270 /* First time we hit ^N or ^P (in a row, I mean) */
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 did_ai = FALSE;
5273#ifdef FEAT_SMARTINDENT
5274 did_si = FALSE;
5275 can_si = FALSE;
5276 can_si_back = FALSE;
5277#endif
5278 if (stop_arrow() == FAIL)
5279 return FAIL;
5280
5281 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005283 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005285 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005286 * "compl_startpos" to the cursor as a pattern to add a new word
5287 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005288 * "compl_startpos" is not in the same line as the cursor then fix it
5289 * (the line has been split because it was longer than 'tw'). if SOL
5290 * is set then skip the previous pattern, a word at the beginning of
5291 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005292 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5293 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
5295 /*
5296 * it is a continued search
5297 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005298 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005299 if (ctrl_x_mode == CTRL_X_NORMAL
5300 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5301 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005303 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 /* line (probably) wrapped, set compl_startpos to the
5306 * first non_blank in the line, if it is not a wordchar
5307 * include it to get a better pattern, but then we don't
5308 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005309 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 compl_startpos.col = compl_col;
5311 compl_startpos.lnum = curwin->w_cursor.lnum;
5312 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
5314 else
5315 {
5316 /* S_IPOS was set when we inserted a word that was at the
5317 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005318 * mode but first we need to redefine compl_startpos */
5319 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005321 compl_cont_status |= CONT_SOL;
5322 compl_startpos.col = (colnr_T)(skipwhite(
5323 line + compl_length
5324 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005329 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005330 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005332 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005334 compl_cont_status &= ~CONT_SOL;
5335 compl_length = (IOSIZE - MIN_SPACE);
5336 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5339 if (compl_length < 1)
5340 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005342 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005345 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
5347 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005353 if (ctrl_x_mode != CTRL_X_NORMAL)
5354 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 compl_cont_status = 0;
5356 compl_cont_status |= CONT_N_ADDS;
5357 compl_startpos = curwin->w_cursor;
5358 startcol = (int)curs_col;
5359 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361
5362 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005363 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5367 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 compl_col += ++startcol;
5373 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005376 compl_pattern = str_foldcase(line + compl_col,
5377 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005379 compl_pattern = vim_strnsave(line + compl_col,
5380 compl_length);
5381 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 return FAIL;
5383 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
5386 char_u *prefix = (char_u *)"\\<";
5387
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005388 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005390 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005391 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005393 if (!vim_iswordp(line + compl_col)
5394 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 && (
5396#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005397 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400#endif
5401 )))
5402 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 STRCPY((char *)compl_pattern, prefix);
5404 (void)quote_meta(compl_pattern + STRLEN(prefix),
5405 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005411 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#endif
5413 )
5414 {
5415 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005416 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5417 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 compl_col += curs_col;
5420 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
5422 else
5423 {
5424#ifdef FEAT_MBYTE
5425 /* Search the point of change class of multibyte character
5426 * or not a word single byte character backward. */
5427 if (has_mbyte)
5428 {
5429 int base_class;
5430 int head_off;
5431
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 startcol -= (*mb_head_off)(line, line + startcol);
5433 base_class = mb_get_class(line + startcol);
5434 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 head_off = (*mb_head_off)(line, line + startcol);
5437 if (base_class != mb_get_class(line + startcol
5438 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005440 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
5442 }
5443 else
5444#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005445 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005447 compl_col += ++startcol;
5448 compl_length = (int)curs_col - startcol;
5449 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
5451 /* Only match word with at least two chars -- webb
5452 * there's no need to call quote_meta,
5453 * alloc(7) is enough -- Acevedo
5454 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 compl_pattern = alloc(7);
5456 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005458 STRCPY((char *)compl_pattern, "\\<");
5459 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5460 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
5462 else
5463 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005464 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005465 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005466 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 STRCPY((char *)compl_pattern, "\\<");
5469 (void)quote_meta(compl_pattern + 2, line + compl_col,
5470 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 }
5473 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005474 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005476 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005477 compl_length = (int)curs_col - (int)compl_col;
5478 if (compl_length < 0) /* cursor in indent: empty pattern */
5479 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 compl_pattern = str_foldcase(line + compl_col, compl_length,
5482 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005484 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5485 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 return FAIL;
5487 }
5488 else if (ctrl_x_mode == CTRL_X_FILES)
5489 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005490 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005491 if (startcol > 0)
5492 {
5493 char_u *p = line + startcol;
5494
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005495 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005496 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005497 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005498 if (p == line && vim_isfilec(PTR2CHAR(p)))
5499 startcol = 0;
5500 else
5501 startcol = (int)(p - line) + 1;
5502 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005503
Bram Moolenaar0300e462013-09-08 16:03:45 +02005504 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005505 compl_length = (int)curs_col - startcol;
5506 compl_pattern = addstar(line + compl_col, compl_length,
5507 EXPAND_FILES);
5508 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 return FAIL;
5510 }
5511 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5512 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005513 compl_pattern = vim_strnsave(line, curs_col);
5514 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005516 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005517 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005518 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5519 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005520 /* No completion possible, use an empty pattern to get a
5521 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005522 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005523 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005524 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5525 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005527 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005528 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005529#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005530 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005531 * Call user defined function 'completefunc' with "a:findstart"
5532 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005533 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005534 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005535 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005536 char_u *funcname;
5537 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005538 win_T *curwin_save;
5539 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005540
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005541 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005542 * string */
5543 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5544 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5545 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005546 {
5547 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5548 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005549 /* restore did_ai, so that adding comment leader works */
5550 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005551 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005552 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005553
Bram Moolenaarffa96842018-06-12 22:05:14 +02005554 args[0].v_type = VAR_NUMBER;
5555 args[0].vval.v_number = 1;
5556 args[1].v_type = VAR_STRING;
5557 args[1].vval.v_string = (char_u *)"";
5558 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005559 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005560 curwin_save = curwin;
5561 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005562 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005563 if (curwin_save != curwin || curbuf_save != curbuf)
5564 {
5565 EMSG(_(e_complwin));
5566 return FAIL;
5567 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005568 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005569 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005570 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005571 {
5572 EMSG(_(e_compldel));
5573 return FAIL;
5574 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005575
Bram Moolenaar6110a002012-01-26 18:58:38 +01005576 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005577 * cancel the complete without an error.
5578 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005579 if (col == -2)
5580 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005581 if (col == -3)
5582 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005583 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005584 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005585 if (!shortmess(SHM_COMPLETIONMENU))
5586 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005587 return FAIL;
5588 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005589
Bram Moolenaar82139082011-09-14 16:52:09 +02005590 /*
5591 * Reset extended parameters of completion, when start new
5592 * completion.
5593 */
5594 compl_opt_refresh_always = FALSE;
5595
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005596 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005597 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005598 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005599 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005600 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005601
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005602 /* Setup variables for completion. Need to obtain "line" again,
5603 * it may have become invalid. */
5604 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005605 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005606 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5607 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005608#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005609 return FAIL;
5610 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005611 else if (ctrl_x_mode == CTRL_X_SPELL)
5612 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005613#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005614 if (spell_bad_len > 0)
5615 compl_col = curs_col - spell_bad_len;
5616 else
5617 compl_col = spell_word_start(startcol);
5618 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005619 {
5620 compl_length = 0;
5621 compl_col = curs_col;
5622 }
5623 else
5624 {
5625 spell_expand_check_cap(compl_col);
5626 compl_length = (int)curs_col - compl_col;
5627 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005628 /* Need to obtain "line" again, it may have become invalid. */
5629 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005630 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5631 if (compl_pattern == NULL)
5632#endif
5633 return FAIL;
5634 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005635 else
5636 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005637 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005638 return FAIL;
5639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005641 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005644 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
5646 /* Insert a new line, keep indentation but ignore 'comments' */
5647#ifdef FEAT_COMMENTS
5648 char_u *old = curbuf->b_p_com;
5649
5650 curbuf->b_p_com = (char_u *)"";
5651#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005652 compl_startpos.lnum = curwin->w_cursor.lnum;
5653 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 ins_eol('\r');
5655#ifdef FEAT_COMMENTS
5656 curbuf->b_p_com = old;
5657#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005658 compl_length = 0;
5659 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661 }
5662 else
5663 {
5664 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005665 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
5667
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005668 if (compl_cont_status & CONT_LOCAL)
5669 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 else
5671 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5672
Bram Moolenaar62951b12011-09-21 18:23:05 +02005673 /* If any of the original typed text has been changed we need to fix
5674 * the redo buffer. */
5675 ins_compl_fixRedoBufForLeader(NULL);
5676
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005677 /* Always add completion for the original text. */
5678 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005679 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5680 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005681 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005683 VIM_CLEAR(compl_pattern);
5684 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 return FAIL;
5686 }
5687
5688 /* showmode might reset the internal line pointers, so it must
5689 * be called before line = ml_get(), or when this address is no
5690 * longer needed. -- Acevedo.
5691 */
5692 edit_submode_extra = (char_u *)_("-- Searching...");
5693 edit_submode_highl = HLF_COUNT;
5694 showmode();
5695 edit_submode_extra = NULL;
5696 out_flush();
5697 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005698 else if (insert_match && stop_arrow() == FAIL)
5699 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005701 compl_shown_match = compl_curr_match;
5702 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
5704 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005705 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005707 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005708 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005709 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005711 /* may undisplay the popup menu */
5712 ins_compl_upd_pum();
5713
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005714 if (n > 1) /* all matches have been found */
5715 compl_matches = n;
5716 compl_curr_match = compl_shown_match;
5717 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
Bram Moolenaard68071d2006-05-02 22:08:30 +00005719 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5720 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 if (got_int && !global_busy)
5722 {
5723 (void)vgetc();
5724 got_int = FALSE;
5725 }
5726
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005727 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005728 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005730 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5731 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5733 edit_submode_highl = HLF_E;
5734 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5735 * because we couldn't expand anything at first place, but if we used
5736 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5737 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005738 if ( compl_length > 1
5739 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005740 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5742 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005743 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
5745
Bram Moolenaar572cb562005-08-05 21:35:02 +00005746 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005747 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005749 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
5751 if (edit_submode_extra == NULL)
5752 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005753 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755 edit_submode_extra = (char_u *)_("Back at original");
5756 edit_submode_highl = HLF_W;
5757 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005758 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {
5760 edit_submode_extra = (char_u *)_("Word from other line");
5761 edit_submode_highl = HLF_COUNT;
5762 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005763 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
5765 edit_submode_extra = (char_u *)_("The only match");
5766 edit_submode_highl = HLF_COUNT;
5767 }
5768 else
5769 {
5770 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005771 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005773 int number = 0;
5774 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005776 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 {
5778 /* search backwards for the first valid (!= -1) number.
5779 * This should normally succeed already at the first loop
5780 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005781 for (match = compl_curr_match->cp_prev; match != NULL
5782 && match != compl_first_match;
5783 match = match->cp_prev)
5784 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005786 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 break;
5788 }
5789 if (match != NULL)
5790 /* go up and assign all numbers which are not assigned
5791 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005792 for (match = match->cp_next;
5793 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005794 match = match->cp_next)
5795 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
5797 else /* BACKWARD */
5798 {
5799 /* search forwards (upwards) for the first valid (!= -1)
5800 * number. This should normally succeed already at the
5801 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005802 for (match = compl_curr_match->cp_next; match != NULL
5803 && match != compl_first_match;
5804 match = match->cp_next)
5805 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005807 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 break;
5809 }
5810 if (match != NULL)
5811 /* go down and assign all numbers which are not
5812 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005813 for (match = match->cp_prev; match
5814 && match->cp_number == -1;
5815 match = match->cp_prev)
5816 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
5818 }
5819
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005820 /* The match should always have a sequence number now, this is
5821 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005822 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005824 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5825 * Translations may need more than twice that. */
5826 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005828 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005829 vim_snprintf((char *)match_ref, sizeof(match_ref),
5830 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005831 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005833 vim_snprintf((char *)match_ref, sizeof(match_ref),
5834 _("match %d"),
5835 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 edit_submode_extra = match_ref;
5837 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005838 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 curs_columns(FALSE);
5840 }
5841 }
5842 }
5843
5844 /* Show a message about what (completion) mode we're in. */
5845 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005846 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005848 if (edit_submode_extra != NULL)
5849 {
5850 if (!p_smd)
5851 msg_attr(edit_submode_extra,
5852 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005853 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005854 }
5855 else
5856 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
Bram Moolenaard68071d2006-05-02 22:08:30 +00005859 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005860 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005861 show_pum(save_w_wrow, save_w_leftcol);
5862
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005863 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005864 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005865
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 return OK;
5867}
5868
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005869 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005870show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005871{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005872 /* RedrawingDisabled may be set when invoked through complete(). */
5873 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005874
Bram Moolenaarcb036422017-03-01 12:29:10 +01005875 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005876
Bram Moolenaarcb036422017-03-01 12:29:10 +01005877 /* If the cursor moved or the display scrolled we need to remove the pum
5878 * first. */
5879 setcursor();
5880 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5881 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005882
Bram Moolenaarcb036422017-03-01 12:29:10 +01005883 ins_compl_show_pum();
5884 setcursor();
5885 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005886}
5887
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888/*
5889 * Looks in the first "len" chars. of "src" for search-metachars.
5890 * If dest is not NULL the chars. are copied there quoting (with
5891 * a backslash) the metachars, and dest would be NUL terminated.
5892 * Returns the length (needed) of dest
5893 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005894 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005895quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005897 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005899 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 {
5901 switch (*src)
5902 {
5903 case '.':
5904 case '*':
5905 case '[':
5906 if (ctrl_x_mode == CTRL_X_DICTIONARY
5907 || ctrl_x_mode == CTRL_X_THESAURUS)
5908 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005909 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 case '~':
5911 if (!p_magic) /* quote these only if magic is set */
5912 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005913 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 case '\\':
5915 if (ctrl_x_mode == CTRL_X_DICTIONARY
5916 || ctrl_x_mode == CTRL_X_THESAURUS)
5917 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005918 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 case '^': /* currently it's not needed. */
5920 case '$':
5921 m++;
5922 if (dest != NULL)
5923 *dest++ = '\\';
5924 break;
5925 }
5926 if (dest != NULL)
5927 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005928# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 /* Copy remaining bytes of a multibyte character. */
5930 if (has_mbyte)
5931 {
5932 int i, mb_len;
5933
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005934 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 if (mb_len > 0 && len >= mb_len)
5936 for (i = 0; i < mb_len; ++i)
5937 {
5938 --len;
5939 ++src;
5940 if (dest != NULL)
5941 *dest++ = *src;
5942 }
5943 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 }
5946 if (dest != NULL)
5947 *dest = NUL;
5948
5949 return m;
5950}
5951#endif /* FEAT_INS_EXPAND */
5952
5953/*
5954 * Next character is interpreted literally.
5955 * A one, two or three digit decimal number is interpreted as its byte value.
5956 * If one or two digits are entered, the next character is given to vungetc().
5957 * For Unicode a character > 255 may be returned.
5958 */
5959 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005960get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 int cc;
5963 int nc;
5964 int i;
5965 int hex = FALSE;
5966 int octal = FALSE;
5967#ifdef FEAT_MBYTE
5968 int unicode = 0;
5969#endif
5970
5971 if (got_int)
5972 return Ctrl_C;
5973
5974#ifdef FEAT_GUI
5975 /*
5976 * In GUI there is no point inserting the internal code for a special key.
5977 * It is more useful to insert the string "<KEY>" instead. This would
5978 * probably be useful in a text window too, but it would not be
5979 * vi-compatible (maybe there should be an option for it?) -- webb
5980 */
5981 if (gui.in_use)
5982 ++allow_keys;
5983#endif
5984#ifdef USE_ON_FLY_SCROLL
5985 dont_scroll = TRUE; /* disallow scrolling here */
5986#endif
5987 ++no_mapping; /* don't map the next key hits */
5988 cc = 0;
5989 i = 0;
5990 for (;;)
5991 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005992 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993#ifdef FEAT_CMDL_INFO
5994 if (!(State & CMDLINE)
5995# ifdef FEAT_MBYTE
5996 && MB_BYTE2LEN_CHECK(nc) == 1
5997# endif
5998 )
5999 add_to_showcmd(nc);
6000#endif
6001 if (nc == 'x' || nc == 'X')
6002 hex = TRUE;
6003 else if (nc == 'o' || nc == 'O')
6004 octal = TRUE;
6005#ifdef FEAT_MBYTE
6006 else if (nc == 'u' || nc == 'U')
6007 unicode = nc;
6008#endif
6009 else
6010 {
6011 if (hex
6012#ifdef FEAT_MBYTE
6013 || unicode != 0
6014#endif
6015 )
6016 {
6017 if (!vim_isxdigit(nc))
6018 break;
6019 cc = cc * 16 + hex2nr(nc);
6020 }
6021 else if (octal)
6022 {
6023 if (nc < '0' || nc > '7')
6024 break;
6025 cc = cc * 8 + nc - '0';
6026 }
6027 else
6028 {
6029 if (!VIM_ISDIGIT(nc))
6030 break;
6031 cc = cc * 10 + nc - '0';
6032 }
6033
6034 ++i;
6035 }
6036
6037 if (cc > 255
6038#ifdef FEAT_MBYTE
6039 && unicode == 0
6040#endif
6041 )
6042 cc = 255; /* limit range to 0-255 */
6043 nc = 0;
6044
6045 if (hex) /* hex: up to two chars */
6046 {
6047 if (i >= 2)
6048 break;
6049 }
6050#ifdef FEAT_MBYTE
6051 else if (unicode) /* Unicode: up to four or eight chars */
6052 {
6053 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6054 break;
6055 }
6056#endif
6057 else if (i >= 3) /* decimal or octal: up to three chars */
6058 break;
6059 }
6060 if (i == 0) /* no number entered */
6061 {
6062 if (nc == K_ZERO) /* NUL is stored as NL */
6063 {
6064 cc = '\n';
6065 nc = 0;
6066 }
6067 else
6068 {
6069 cc = nc;
6070 nc = 0;
6071 }
6072 }
6073
6074 if (cc == 0) /* NUL is stored as NL */
6075 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006076#ifdef FEAT_MBYTE
6077 if (enc_dbcs && (cc & 0xff) == 0)
6078 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6079 second byte will cause trouble! */
6080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
6082 --no_mapping;
6083#ifdef FEAT_GUI
6084 if (gui.in_use)
6085 --allow_keys;
6086#endif
6087 if (nc)
6088 vungetc(nc);
6089 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6090 return cc;
6091}
6092
6093/*
6094 * Insert character, taking care of special keys and mod_mask
6095 */
6096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006097insert_special(
6098 int c,
6099 int allow_modmask,
6100 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
6102 char_u *p;
6103 int len;
6104
6105 /*
6106 * Special function key, translate into "<Key>". Up to the last '>' is
6107 * inserted with ins_str(), so as not to replace characters in replace
6108 * mode.
6109 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6110 * unless 'allow_modmask' is TRUE.
6111 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006112#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 /* Command-key never produces a normal key */
6114 if (mod_mask & MOD_MASK_CMD)
6115 allow_modmask = TRUE;
6116#endif
6117 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6118 {
6119 p = get_special_key_name(c, mod_mask);
6120 len = (int)STRLEN(p);
6121 c = p[len - 1];
6122 if (len > 2)
6123 {
6124 if (stop_arrow() == FAIL)
6125 return;
6126 p[len - 1] = NUL;
6127 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006128 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 ctrlv = FALSE;
6130 }
6131 }
6132 if (stop_arrow() == OK)
6133 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6134}
6135
6136/*
6137 * Special characters in this context are those that need processing other
6138 * than the simple insertion that can be performed here. This includes ESC
6139 * which terminates the insert, and CR/NL which need special processing to
6140 * open up a new line. This routine tries to optimize insertions performed by
6141 * the "redo", "undo" or "put" commands, so it needs to know when it should
6142 * stop and defer processing to the "normal" mechanism.
6143 * '0' and '^' are special, because they can be followed by CTRL-D.
6144 */
6145#ifdef EBCDIC
6146# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6147#else
6148# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6149#endif
6150
6151#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006152# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006154# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155#endif
6156
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006157/*
6158 * "flags": INSCHAR_FORMAT - force formatting
6159 * INSCHAR_CTRLV - char typed just after CTRL-V
6160 * INSCHAR_NO_FEX - don't use 'formatexpr'
6161 *
6162 * NOTE: passes the flags value straight through to internal_format() which,
6163 * beside INSCHAR_FORMAT (above), is also looking for these:
6164 * INSCHAR_DO_COM - format comments
6165 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006168insertchar(
6169 int c, /* character to insert or NUL */
6170 int flags, /* INSCHAR_FORMAT, etc. */
6171 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 int textwidth;
6174#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006178 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006180 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183 /*
6184 * Try to break the line in two or more pieces when:
6185 * - Always do this if we have been called to do formatting only.
6186 * - Always do this when 'formatoptions' has the 'a' flag and the line
6187 * ends in white space.
6188 * - Otherwise:
6189 * - Don't do this if inserting a blank
6190 * - Don't do this if an existing character is being replaced, unless
6191 * we're in VREPLACE mode.
6192 * - Do this if the cursor is not on the line where insert started
6193 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6194 * before the insert.
6195 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6196 * before 'textwidth'
6197 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006199 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006200 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 && !((State & REPLACE_FLAG)
6202#ifdef FEAT_VREPLACE
6203 && !(State & VREPLACE_FLAG)
6204#endif
6205 && *ml_get_cursor() != NUL)
6206 && (curwin->w_cursor.lnum != Insstart.lnum
6207 || ((!has_format_option(FO_INS_LONG)
6208 || Insstart_textlen <= (colnr_T)textwidth)
6209 && (!fo_ins_blank
6210 || Insstart_blank_vcol <= (colnr_T)textwidth
6211 ))))))
6212 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213 /* Format with 'formatexpr' when it's set. Use internal formatting
6214 * when 'formatexpr' isn't set or it returns non-zero. */
6215#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006216 int do_internal = TRUE;
6217 colnr_T virtcol = get_nolist_virtcol()
6218 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006219
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006220 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6221 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006222 {
6223 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6224 /* It may be required to save for undo again, e.g. when setline()
6225 * was called. */
6226 ins_need_undo = TRUE;
6227 }
6228 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006230 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (c == NUL) /* only formatting was wanted */
6234 return;
6235
6236#ifdef FEAT_COMMENTS
6237 /* Check whether this character should end a comment. */
6238 if (did_ai && (int)c == end_comment_pending)
6239 {
6240 char_u *line;
6241 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6242 int middle_len, end_len;
6243 int i;
6244
6245 /*
6246 * Need to remove existing (middle) comment leader and insert end
6247 * comment leader. First, check what comment leader we can find.
6248 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006249 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6251 {
6252 /* Skip middle-comment string */
6253 while (*p && p[-1] != ':') /* find end of middle flags */
6254 ++p;
6255 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6256 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006257 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 --middle_len;
6259
6260 /* Find the end-comment string */
6261 while (*p && p[-1] != ':') /* find end of end flags */
6262 ++p;
6263 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6264
6265 /* Skip white space before the cursor */
6266 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006267 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 ;
6269 i++;
6270
6271 /* Skip to before the middle leader */
6272 i -= middle_len;
6273
6274 /* Check some expected things before we go on */
6275 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6276 {
6277 /* Backspace over all the stuff we want to replace */
6278 backspace_until_column(i);
6279
6280 /*
6281 * Insert the end-comment string, except for the last
6282 * character, which will get inserted as normal later.
6283 */
6284 ins_bytes_len(lead_end, end_len - 1);
6285 }
6286 }
6287 }
6288 end_comment_pending = NUL;
6289#endif
6290
6291 did_ai = FALSE;
6292#ifdef FEAT_SMARTINDENT
6293 did_si = FALSE;
6294 can_si = FALSE;
6295 can_si_back = FALSE;
6296#endif
6297
6298 /*
6299 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6300 * This speeds up normal text input considerably.
6301 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6302 * need to re-indent at a ':', or any other character (but not what
6303 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006304 * Don't do this when there an InsertCharPre autocommand is defined,
6305 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006306 * Do the check for InsertCharPre before the call to vpeekc() because the
6307 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 */
6309#ifdef USE_ON_FLY_SCROLL
6310 dont_scroll = FALSE; /* allow scrolling here */
6311#endif
6312
6313 if ( !ISSPECIAL(c)
6314#ifdef FEAT_MBYTE
6315 && (!has_mbyte || (*mb_char2len)(c) == 1)
6316#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006317 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 && vpeekc() != NUL
6319 && !(State & REPLACE_FLAG)
6320#ifdef FEAT_CINDENT
6321 && !cindent_on()
6322#endif
6323#ifdef FEAT_RIGHTLEFT
6324 && !p_ri
6325#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006326 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 {
6328#define INPUT_BUFLEN 100
6329 char_u buf[INPUT_BUFLEN + 1];
6330 int i;
6331 colnr_T virtcol = 0;
6332
6333 buf[0] = c;
6334 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006335 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 virtcol = get_nolist_virtcol();
6337 /*
6338 * Stop the string when:
6339 * - no more chars available
6340 * - finding a special character (command key)
6341 * - buffer is full
6342 * - running into the 'textwidth' boundary
6343 * - need to check for abbreviation: A non-word char after a word-char
6344 */
6345 while ( (c = vpeekc()) != NUL
6346 && !ISSPECIAL(c)
6347#ifdef FEAT_MBYTE
6348 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6349#endif
6350 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006351# ifdef FEAT_FKMAP
6352 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 && (textwidth == 0
6355 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6356 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6357 {
6358#ifdef FEAT_RIGHTLEFT
6359 c = vgetc();
6360 if (p_hkmap && KeyTyped)
6361 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 buf[i++] = c;
6363#else
6364 buf[i++] = vgetc();
6365#endif
6366 }
6367
6368#ifdef FEAT_DIGRAPHS
6369 do_digraph(-1); /* clear digraphs */
6370 do_digraph(buf[i-1]); /* may be the start of a digraph */
6371#endif
6372 buf[i] = NUL;
6373 ins_str(buf);
6374 if (flags & INSCHAR_CTRLV)
6375 {
6376 redo_literal(*buf);
6377 i = 1;
6378 }
6379 else
6380 i = 0;
6381 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006382 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 }
6384 else
6385 {
6386#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006387 int cc;
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6390 {
6391 char_u buf[MB_MAXBYTES + 1];
6392
6393 (*mb_char2bytes)(c, buf);
6394 buf[cc] = NUL;
6395 ins_char_bytes(buf, cc);
6396 AppendCharToRedobuff(c);
6397 }
6398 else
6399#endif
6400 {
6401 ins_char(c);
6402 if (flags & INSCHAR_CTRLV)
6403 redo_literal(c);
6404 else
6405 AppendCharToRedobuff(c);
6406 }
6407 }
6408}
6409
6410/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006411 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006412 *
6413 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6414 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006415 */
6416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006417internal_format(
6418 int textwidth,
6419 int second_indent,
6420 int flags,
6421 int format_only,
6422 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006423{
6424 int cc;
6425 int save_char = NUL;
6426 int haveto_redraw = FALSE;
6427 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6428#ifdef FEAT_MBYTE
6429 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6430#endif
6431 int fo_white_par = has_format_option(FO_WHITE_PAR);
6432 int first_line = TRUE;
6433#ifdef FEAT_COMMENTS
6434 colnr_T leader_len;
6435 int no_leader = FALSE;
6436 int do_comments = (flags & INSCHAR_DO_COM);
6437#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006438#ifdef FEAT_LINEBREAK
6439 int has_lbr = curwin->w_p_lbr;
6440
6441 /* make sure win_lbr_chartabsize() counts correctly */
6442 curwin->w_p_lbr = FALSE;
6443#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444
6445 /*
6446 * When 'ai' is off we don't want a space under the cursor to be
6447 * deleted. Replace it with an 'x' temporarily.
6448 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006449 if (!curbuf->b_p_ai
6450#ifdef FEAT_VREPLACE
6451 && !(State & VREPLACE_FLAG)
6452#endif
6453 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006454 {
6455 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006456 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006457 {
6458 save_char = cc;
6459 pchar_cursor('x');
6460 }
6461 }
6462
6463 /*
6464 * Repeat breaking lines, until the current line is not too long.
6465 */
6466 while (!got_int)
6467 {
6468 int startcol; /* Cursor column at entry */
6469 int wantcol; /* column at textwidth border */
6470 int foundcol; /* column for start of spaces */
6471 int end_foundcol = 0; /* column for start of word */
6472 colnr_T len;
6473 colnr_T virtcol;
6474#ifdef FEAT_VREPLACE
6475 int orig_col = 0;
6476 char_u *saved_text = NULL;
6477#endif
6478 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006479 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006480
Bram Moolenaar97b98102009-11-17 16:41:01 +00006481 virtcol = get_nolist_virtcol()
6482 + char2cells(c != NUL ? c : gchar_cursor());
6483 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006484 break;
6485
6486#ifdef FEAT_COMMENTS
6487 if (no_leader)
6488 do_comments = FALSE;
6489 else if (!(flags & INSCHAR_FORMAT)
6490 && has_format_option(FO_WRAP_COMS))
6491 do_comments = TRUE;
6492
6493 /* Don't break until after the comment leader */
6494 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006495 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006496 else
6497 leader_len = 0;
6498
6499 /* If the line doesn't start with a comment leader, then don't
6500 * start one in a following broken line. Avoids that a %word
6501 * moved to the start of the next line causes all following lines
6502 * to start with %. */
6503 if (leader_len == 0)
6504 no_leader = TRUE;
6505#endif
6506 if (!(flags & INSCHAR_FORMAT)
6507#ifdef FEAT_COMMENTS
6508 && leader_len == 0
6509#endif
6510 && !has_format_option(FO_WRAP))
6511
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006512 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006513 if ((startcol = curwin->w_cursor.col) == 0)
6514 break;
6515
6516 /* find column of textwidth border */
6517 coladvance((colnr_T)textwidth);
6518 wantcol = curwin->w_cursor.col;
6519
Bram Moolenaar97b98102009-11-17 16:41:01 +00006520 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006521 foundcol = 0;
6522
6523 /*
6524 * Find position to break at.
6525 * Stop at first entered white when 'formatoptions' has 'v'
6526 */
6527 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006528 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006529 || curwin->w_cursor.lnum != Insstart.lnum
6530 || curwin->w_cursor.col >= Insstart.col)
6531 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006532 if (curwin->w_cursor.col == startcol && c != NUL)
6533 cc = c;
6534 else
6535 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006536 if (WHITECHAR(cc))
6537 {
6538 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006539 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006540
6541 /* find start of sequence of blanks */
6542 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6543 {
6544 dec_cursor();
6545 cc = gchar_cursor();
6546 }
6547 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6548 break; /* only spaces in front of text */
6549#ifdef FEAT_COMMENTS
6550 /* Don't break until after the comment leader */
6551 if (curwin->w_cursor.col < leader_len)
6552 break;
6553#endif
6554 if (has_format_option(FO_ONE_LETTER))
6555 {
6556 /* do not break after one-letter words */
6557 if (curwin->w_cursor.col == 0)
6558 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006559#ifdef FEAT_COMMENTS
6560 /* do not break "#a b" when 'tw' is 2 */
6561 if (curwin->w_cursor.col <= leader_len)
6562 break;
6563#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006564 col = curwin->w_cursor.col;
6565 dec_cursor();
6566 cc = gchar_cursor();
6567
6568 if (WHITECHAR(cc))
6569 continue; /* one-letter, continue */
6570 curwin->w_cursor.col = col;
6571 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006572
6573 inc_cursor();
6574
6575 end_foundcol = end_col + 1;
6576 foundcol = curwin->w_cursor.col;
6577 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006578 break;
6579 }
6580#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006581 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006582 {
6583 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006584 if (curwin->w_cursor.col != startcol)
6585 {
6586#ifdef FEAT_COMMENTS
6587 /* Don't break until after the comment leader */
6588 if (curwin->w_cursor.col < leader_len)
6589 break;
6590#endif
6591 col = curwin->w_cursor.col;
6592 inc_cursor();
6593 /* Don't change end_foundcol if already set. */
6594 if (foundcol != curwin->w_cursor.col)
6595 {
6596 foundcol = curwin->w_cursor.col;
6597 end_foundcol = foundcol;
6598 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6599 break;
6600 }
6601 curwin->w_cursor.col = col;
6602 }
6603
6604 if (curwin->w_cursor.col == 0)
6605 break;
6606
6607 col = curwin->w_cursor.col;
6608
6609 dec_cursor();
6610 cc = gchar_cursor();
6611
6612 if (WHITECHAR(cc))
6613 continue; /* break with space */
6614#ifdef FEAT_COMMENTS
6615 /* Don't break until after the comment leader */
6616 if (curwin->w_cursor.col < leader_len)
6617 break;
6618#endif
6619
6620 curwin->w_cursor.col = col;
6621
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006622 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006623 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006624 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6625 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006626 }
6627#endif
6628 if (curwin->w_cursor.col == 0)
6629 break;
6630 dec_cursor();
6631 }
6632
6633 if (foundcol == 0) /* no spaces, cannot break line */
6634 {
6635 curwin->w_cursor.col = startcol;
6636 break;
6637 }
6638
6639 /* Going to break the line, remove any "$" now. */
6640 undisplay_dollar();
6641
6642 /*
6643 * Offset between cursor position and line break is used by replace
6644 * stack functions. VREPLACE does not use this, and backspaces
6645 * over the text instead.
6646 */
6647#ifdef FEAT_VREPLACE
6648 if (State & VREPLACE_FLAG)
6649 orig_col = startcol; /* Will start backspacing from here */
6650 else
6651#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006652 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006653
6654 /*
6655 * adjust startcol for spaces that will be deleted and
6656 * characters that will remain on top line
6657 */
6658 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006659 while ((cc = gchar_cursor(), WHITECHAR(cc))
6660 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006661 inc_cursor();
6662 startcol -= curwin->w_cursor.col;
6663 if (startcol < 0)
6664 startcol = 0;
6665
6666#ifdef FEAT_VREPLACE
6667 if (State & VREPLACE_FLAG)
6668 {
6669 /*
6670 * In VREPLACE mode, we will backspace over the text to be
6671 * wrapped, so save a copy now to put on the next line.
6672 */
6673 saved_text = vim_strsave(ml_get_cursor());
6674 curwin->w_cursor.col = orig_col;
6675 if (saved_text == NULL)
6676 break; /* Can't do it, out of memory */
6677 saved_text[startcol] = NUL;
6678
6679 /* Backspace over characters that will move to the next line */
6680 if (!fo_white_par)
6681 backspace_until_column(foundcol);
6682 }
6683 else
6684#endif
6685 {
6686 /* put cursor after pos. to break line */
6687 if (!fo_white_par)
6688 curwin->w_cursor.col = foundcol;
6689 }
6690
6691 /*
6692 * Split the line just before the margin.
6693 * Only insert/delete lines, but don't really redraw the window.
6694 */
6695 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6696 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6697#ifdef FEAT_COMMENTS
6698 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006699 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006700#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006701 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6702 if (!(flags & INSCHAR_COM_LIST))
6703 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006704
6705 replace_offset = 0;
6706 if (first_line)
6707 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006708 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006709 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006710 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006711 * This section is for auto-wrap of numeric lists. When not
6712 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6713 * flag will be set and open_line() will handle it (as seen
6714 * above). The code here (and in get_number_indent()) will
6715 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006716 */
6717 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006718 second_indent =
6719 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006720 if (second_indent >= 0)
6721 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006722#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006723 if (State & VREPLACE_FLAG)
6724 change_indent(INDENT_SET, second_indent,
6725 FALSE, NUL, TRUE);
6726 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006727#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006728#ifdef FEAT_COMMENTS
6729 if (leader_len > 0 && second_indent - leader_len > 0)
6730 {
6731 int i;
6732 int padding = second_indent - leader_len;
6733
6734 /* We started at the first_line of a numbered list
6735 * that has a comment. the open_line() function has
6736 * inserted the proper comment leader and positioned
6737 * the cursor at the end of the split line. Now we
6738 * add the additional whitespace needed after the
6739 * comment leader for the numbered list. */
6740 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006741 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006742 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006743 }
6744 else
6745 {
6746#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006747 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006748#ifdef FEAT_COMMENTS
6749 }
6750#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006751 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006752 }
6753 first_line = FALSE;
6754 }
6755
6756#ifdef FEAT_VREPLACE
6757 if (State & VREPLACE_FLAG)
6758 {
6759 /*
6760 * In VREPLACE mode we have backspaced over the text to be
6761 * moved, now we re-insert it into the new line.
6762 */
6763 ins_bytes(saved_text);
6764 vim_free(saved_text);
6765 }
6766 else
6767#endif
6768 {
6769 /*
6770 * Check if cursor is not past the NUL off the line, cindent
6771 * may have added or removed indent.
6772 */
6773 curwin->w_cursor.col += startcol;
6774 len = (colnr_T)STRLEN(ml_get_curline());
6775 if (curwin->w_cursor.col > len)
6776 curwin->w_cursor.col = len;
6777 }
6778
6779 haveto_redraw = TRUE;
6780#ifdef FEAT_CINDENT
6781 can_cindent = TRUE;
6782#endif
6783 /* moved the cursor, don't autoindent or cindent now */
6784 did_ai = FALSE;
6785#ifdef FEAT_SMARTINDENT
6786 did_si = FALSE;
6787 can_si = FALSE;
6788 can_si_back = FALSE;
6789#endif
6790 line_breakcheck();
6791 }
6792
6793 if (save_char != NUL) /* put back space after cursor */
6794 pchar_cursor(save_char);
6795
Bram Moolenaar0026d472014-09-09 16:32:39 +02006796#ifdef FEAT_LINEBREAK
6797 curwin->w_p_lbr = has_lbr;
6798#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006799 if (!format_only && haveto_redraw)
6800 {
6801 update_topline();
6802 redraw_curbuf_later(VALID);
6803 }
6804}
6805
6806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 * Called after inserting or deleting text: When 'formatoptions' includes the
6808 * 'a' flag format from the current line until the end of the paragraph.
6809 * Keep the cursor at the same position relative to the text.
6810 * The caller must have saved the cursor line for undo, following ones will be
6811 * saved here.
6812 */
6813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006814auto_format(
6815 int trailblank, /* when TRUE also format with trailing blank */
6816 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817{
6818 pos_T pos;
6819 colnr_T len;
6820 char_u *old;
6821 char_u *new, *pnew;
6822 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006823 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824
6825 if (!has_format_option(FO_AUTO))
6826 return;
6827
6828 pos = curwin->w_cursor;
6829 old = ml_get_curline();
6830
6831 /* may remove added space */
6832 check_auto_format(FALSE);
6833
6834 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6835 * user might insert normal text next. Also skip formatting when "1" is
6836 * in 'formatoptions' and there is a single character before the cursor.
6837 * Otherwise the line would be broken and when typing another non-white
6838 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006839 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (*old != NUL && !trailblank && wasatend)
6841 {
6842 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006843 cc = gchar_cursor();
6844 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6845 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006847 cc = gchar_cursor();
6848 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {
6850 curwin->w_cursor = pos;
6851 return;
6852 }
6853 curwin->w_cursor = pos;
6854 }
6855
6856#ifdef FEAT_COMMENTS
6857 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6858 * comments. */
6859 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006860 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 return;
6862#endif
6863
6864 /*
6865 * May start formatting in a previous line, so that after "x" a word is
6866 * moved to the previous line if it fits there now. Only when this is not
6867 * the start of a paragraph.
6868 */
6869 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6870 {
6871 --curwin->w_cursor.lnum;
6872 if (u_save_cursor() == FAIL)
6873 return;
6874 }
6875
6876 /*
6877 * Do the formatting and restore the cursor position. "saved_cursor" will
6878 * be adjusted for the text formatting.
6879 */
6880 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006881 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 curwin->w_cursor = saved_cursor;
6883 saved_cursor.lnum = 0;
6884
6885 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6886 {
6887 /* "cannot happen" */
6888 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6889 coladvance((colnr_T)MAXCOL);
6890 }
6891 else
6892 check_cursor_col();
6893
6894 /* Insert mode: If the cursor is now after the end of the line while it
6895 * previously wasn't, the line was broken. Because of the rule above we
6896 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6897 * formatted. */
6898 if (!wasatend && has_format_option(FO_WHITE_PAR))
6899 {
6900 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006901 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (curwin->w_cursor.col == len)
6903 {
6904 pnew = vim_strnsave(new, len + 2);
6905 pnew[len] = ' ';
6906 pnew[len + 1] = NUL;
6907 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6908 /* remove the space later */
6909 did_add_space = TRUE;
6910 }
6911 else
6912 /* may remove added space */
6913 check_auto_format(FALSE);
6914 }
6915
6916 check_cursor();
6917}
6918
6919/*
6920 * When an extra space was added to continue a paragraph for auto-formatting,
6921 * delete it now. The space must be under the cursor, just after the insert
6922 * position.
6923 */
6924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006925check_auto_format(
6926 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927{
6928 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006929 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
6931 if (did_add_space)
6932 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006933 cc = gchar_cursor();
6934 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 /* Somehow the space was removed already. */
6936 did_add_space = FALSE;
6937 else
6938 {
6939 if (!end_insert)
6940 {
6941 inc_cursor();
6942 c = gchar_cursor();
6943 dec_cursor();
6944 }
6945 if (c != NUL)
6946 {
6947 /* The space is no longer at the end of the line, delete it. */
6948 del_char(FALSE);
6949 did_add_space = FALSE;
6950 }
6951 }
6952 }
6953}
6954
6955/*
6956 * Find out textwidth to be used for formatting:
6957 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006958 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 * if invalid value, use 0.
6960 * Set default to window width (maximum 79) for "gq" operator.
6961 */
6962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006963comp_textwidth(
6964 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965{
6966 int textwidth;
6967
6968 textwidth = curbuf->b_p_tw;
6969 if (textwidth == 0 && curbuf->b_p_wm)
6970 {
6971 /* The width is the window width minus 'wrapmargin' minus all the
6972 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006973 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974#ifdef FEAT_CMDWIN
6975 if (cmdwin_type != 0)
6976 textwidth -= 1;
6977#endif
6978#ifdef FEAT_FOLDING
6979 textwidth -= curwin->w_p_fdc;
6980#endif
6981#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006982 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 textwidth -= 1;
6984#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006985 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 textwidth -= 8;
6987 }
6988 if (textwidth < 0)
6989 textwidth = 0;
6990 if (ff && textwidth == 0)
6991 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006992 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (textwidth > 79)
6994 textwidth = 79;
6995 }
6996 return textwidth;
6997}
6998
6999/*
7000 * Put a character in the redo buffer, for when just after a CTRL-V.
7001 */
7002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007003redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005 char_u buf[10];
7006
7007 /* Only digits need special treatment. Translate them into a string of
7008 * three digits. */
7009 if (VIM_ISDIGIT(c))
7010 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007011 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 AppendToRedobuff(buf);
7013 }
7014 else
7015 AppendCharToRedobuff(c);
7016}
7017
7018/*
7019 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007020 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 */
7022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023start_arrow(
7024 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007026 start_arrow_common(end_insert_pos, TRUE);
7027}
7028
7029/*
7030 * Like start_arrow() but with end_change argument.
7031 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7032 */
7033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034start_arrow_with_change(
7035 pos_T *end_insert_pos, /* can be NULL */
7036 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007037{
7038 start_arrow_common(end_insert_pos, end_change);
7039 if (!end_change)
7040 {
7041 AppendCharToRedobuff(Ctrl_G);
7042 AppendCharToRedobuff('U');
7043 }
7044}
7045
7046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007047start_arrow_common(
7048 pos_T *end_insert_pos, /* can be NULL */
7049 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007050{
7051 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {
7053 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007054 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 arrow_used = TRUE; /* this means we stopped the current insert */
7056 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007057#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007058 check_spell_redraw();
7059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060}
7061
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007062#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007063/*
7064 * If we skipped highlighting word at cursor, do it now.
7065 * It may be skipped again, thus reset spell_redraw_lnum first.
7066 */
7067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007068check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007069{
7070 if (spell_redraw_lnum != 0)
7071 {
7072 linenr_T lnum = spell_redraw_lnum;
7073
7074 spell_redraw_lnum = 0;
7075 redrawWinline(lnum, FALSE);
7076 }
7077}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007078
7079/*
7080 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7081 * spelled word, if there is one.
7082 */
7083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007084spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007085{
7086 pos_T tpos = curwin->w_cursor;
7087
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007088 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007089 if (curwin->w_cursor.col != tpos.col)
7090 start_arrow(&tpos);
7091}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007092#endif
7093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094/*
7095 * stop_arrow() is called before a change is made in insert mode.
7096 * If an arrow key has been used, start a new insertion.
7097 * Returns FAIL if undo is impossible, shouldn't insert then.
7098 */
7099 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007100stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
7102 if (arrow_used)
7103 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007104 Insstart = curwin->w_cursor; /* new insertion starts here */
7105 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7106 /* Don't update the original insert position when moved to the
7107 * right, except when nothing was inserted yet. */
7108 update_Insstart_orig = FALSE;
7109 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 if (u_save_cursor() == OK)
7112 {
7113 arrow_used = FALSE;
7114 ins_need_undo = FALSE;
7115 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 ai_col = 0;
7118#ifdef FEAT_VREPLACE
7119 if (State & VREPLACE_FLAG)
7120 {
7121 orig_line_count = curbuf->b_ml.ml_line_count;
7122 vr_lines_changed = 1;
7123 }
7124#endif
7125 ResetRedobuff();
7126 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007127 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 }
7129 else if (ins_need_undo)
7130 {
7131 if (u_save_cursor() == OK)
7132 ins_need_undo = FALSE;
7133 }
7134
7135#ifdef FEAT_FOLDING
7136 /* Always open fold at the cursor line when inserting something. */
7137 foldOpenCursor();
7138#endif
7139
7140 return (arrow_used || ins_need_undo ? FAIL : OK);
7141}
7142
7143/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007144 * Do a few things to stop inserting.
7145 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7146 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 */
7148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007149stop_insert(
7150 pos_T *end_insert_pos,
7151 int esc, /* called by ins_esc() */
7152 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007154 int cc;
7155 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 stop_redo_ins();
7158 replace_flush(); /* abandon replace stack */
7159
7160 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007161 * Save the inserted text for later redo with ^@ and CTRL-A.
7162 * Don't do it when "restart_edit" was set and nothing was inserted,
7163 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007165 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007166 if (did_restart_edit == 0 || (ptr != NULL
7167 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007168 {
7169 vim_free(last_insert);
7170 last_insert = ptr;
7171 last_insert_skip = new_insert_skip;
7172 }
7173 else
7174 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007176 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
7178 /* Auto-format now. It may seem strange to do this when stopping an
7179 * insertion (or moving the cursor), but it's required when appending
7180 * a line and having it end in a space. But only do it when something
7181 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007182 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007184 pos_T tpos = curwin->w_cursor;
7185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 /* When the cursor is at the end of the line after a space the
7187 * formatting will move it to the following word. Avoid that by
7188 * moving the cursor onto the space. */
7189 cc = 'x';
7190 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7191 {
7192 dec_cursor();
7193 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007194 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007195 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
7197
7198 auto_format(TRUE, FALSE);
7199
Bram Moolenaar1c465442017-03-12 20:10:05 +01007200 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007201 {
7202 if (gchar_cursor() != NUL)
7203 inc_cursor();
7204#ifdef FEAT_VIRTUALEDIT
7205 /* If the cursor is still at the same character, also keep
7206 * the "coladd". */
7207 if (gchar_cursor() == NUL
7208 && curwin->w_cursor.lnum == tpos.lnum
7209 && curwin->w_cursor.col == tpos.col)
7210 curwin->w_cursor.coladd = tpos.coladd;
7211#endif
7212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 }
7214
7215 /* If a space was inserted for auto-formatting, remove it now. */
7216 check_auto_format(TRUE);
7217
7218 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007219 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007220 * Do this when ESC was used or moving the cursor up/down.
7221 * Check for the old position still being valid, just in case the text
7222 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007223 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007224 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7225 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007227 pos_T tpos = curwin->w_cursor;
7228
7229 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007230 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007231 for (;;)
7232 {
7233 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7234 --curwin->w_cursor.col;
7235 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007236 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007237 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007238 if (del_char(TRUE) == FAIL)
7239 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007240 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007241 if (curwin->w_cursor.lnum != tpos.lnum)
7242 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007243 else
7244 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007245 /* reset tpos, could have been invalidated in the loop above */
7246 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007247 tpos.col++;
7248 if (cc != NUL && gchar_pos(&tpos) == NUL)
7249 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 /* <C-S-Right> may have started Visual mode, adjust the position for
7253 * deleted characters. */
7254 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7255 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007256 int len = (int)STRLEN(ml_get_curline());
7257
7258 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007260 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007261#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
7265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267 }
7268 did_ai = FALSE;
7269#ifdef FEAT_SMARTINDENT
7270 did_si = FALSE;
7271 can_si = FALSE;
7272 can_si_back = FALSE;
7273#endif
7274
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007275 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7276 * now in a different buffer. */
7277 if (end_insert_pos != NULL)
7278 {
7279 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007280 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007281 curbuf->b_op_end = *end_insert_pos;
7282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283}
7284
7285/*
7286 * Set the last inserted text to a single character.
7287 * Used for the replace command.
7288 */
7289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
7292 char_u *s;
7293
7294 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 if (last_insert != NULL)
7297 {
7298 s = last_insert;
7299 /* Use the CTRL-V only when entering a special char */
7300 if (c < ' ' || c == DEL)
7301 *s++ = Ctrl_V;
7302 s = add_char2buf(c, s);
7303 *s++ = ESC;
7304 *s++ = NUL;
7305 last_insert_skip = 0;
7306 }
7307}
7308
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007309#if defined(EXITFREE) || defined(PROTO)
7310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007311free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007312{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007313 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007314# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007315 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007316# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007317}
7318#endif
7319
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320/*
7321 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7322 * and CSI. Handle multi-byte characters.
7323 * Returns a pointer to after the added bytes.
7324 */
7325 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007326add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
7328#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007329 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 int i;
7331 int len;
7332
7333 len = (*mb_char2bytes)(c, temp);
7334 for (i = 0; i < len; ++i)
7335 {
7336 c = temp[i];
7337#endif
7338 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7339 if (c == K_SPECIAL)
7340 {
7341 *s++ = K_SPECIAL;
7342 *s++ = KS_SPECIAL;
7343 *s++ = KE_FILLER;
7344 }
7345#ifdef FEAT_GUI
7346 else if (c == CSI)
7347 {
7348 *s++ = CSI;
7349 *s++ = KS_EXTRA;
7350 *s++ = (int)KE_CSI;
7351 }
7352#endif
7353 else
7354 *s++ = c;
7355#ifdef FEAT_MBYTE
7356 }
7357#endif
7358 return s;
7359}
7360
7361/*
7362 * move cursor to start of line
7363 * if flags & BL_WHITE move to first non-white
7364 * if flags & BL_SOL move to first non-white if startofline is set,
7365 * otherwise keep "curswant" column
7366 * if flags & BL_FIX don't leave the cursor on a NUL.
7367 */
7368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007369beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370{
7371 if ((flags & BL_SOL) && !p_sol)
7372 coladvance(curwin->w_curswant);
7373 else
7374 {
7375 curwin->w_cursor.col = 0;
7376#ifdef FEAT_VIRTUALEDIT
7377 curwin->w_cursor.coladd = 0;
7378#endif
7379
7380 if (flags & (BL_WHITE | BL_SOL))
7381 {
7382 char_u *ptr;
7383
Bram Moolenaar1c465442017-03-12 20:10:05 +01007384 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7386 ++curwin->w_cursor.col;
7387 }
7388 curwin->w_set_curswant = TRUE;
7389 }
7390}
7391
7392/*
7393 * oneright oneleft cursor_down cursor_up
7394 *
7395 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007396 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 * Return OK when successful, FAIL when we hit a line of file boundary.
7398 */
7399
7400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007401oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
7406#ifdef FEAT_VIRTUALEDIT
7407 if (virtual_active())
7408 {
7409 pos_T prevpos = curwin->w_cursor;
7410
7411 /* Adjust for multi-wide char (excluding TAB) */
7412 ptr = ml_get_cursor();
7413 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007414# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007416# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 ))
7420 ? ptr2cells(ptr) : 1));
7421 curwin->w_set_curswant = TRUE;
7422 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7423 return (prevpos.col != curwin->w_cursor.col
7424 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7425 }
7426#endif
7427
7428 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007429 if (*ptr == NUL)
7430 return FAIL; /* already at the very end */
7431
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007433 if (has_mbyte)
7434 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 else
7436#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007437 l = 1;
7438
7439 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7440 * contains "onemore". */
7441 if (ptr[l] == NUL
7442#ifdef FEAT_VIRTUALEDIT
7443 && (ve_flags & VE_ONEMORE) == 0
7444#endif
7445 )
7446 return FAIL;
7447 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
7449 curwin->w_set_curswant = TRUE;
7450 return OK;
7451}
7452
7453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007454oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455{
7456#ifdef FEAT_VIRTUALEDIT
7457 if (virtual_active())
7458 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007459# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 int v = getviscol();
7463
7464 if (v == 0)
7465 return FAIL;
7466
7467# ifdef FEAT_LINEBREAK
7468 /* We might get stuck on 'showbreak', skip over it. */
7469 width = 1;
7470 for (;;)
7471 {
7472 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007473 /* getviscol() is slow, skip it when 'showbreak' is empty,
7474 * 'breakindent' is not set and there are no multi-byte
7475 * characters */
7476 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477# ifdef FEAT_MBYTE
7478 && !has_mbyte
7479# endif
7480 ) || getviscol() < v)
7481 break;
7482 ++width;
7483 }
7484# else
7485 coladvance(v - 1);
7486# endif
7487
7488 if (curwin->w_cursor.coladd == 1)
7489 {
7490 char_u *ptr;
7491
7492 /* Adjust for multi-wide char (not a TAB) */
7493 ptr = ml_get_cursor();
7494 if (*ptr != TAB && vim_isprintc(
7495# ifdef FEAT_MBYTE
7496 (*mb_ptr2char)(ptr)
7497# else
7498 *ptr
7499# endif
7500 ) && ptr2cells(ptr) > 1)
7501 curwin->w_cursor.coladd = 0;
7502 }
7503
7504 curwin->w_set_curswant = TRUE;
7505 return OK;
7506 }
7507#endif
7508
7509 if (curwin->w_cursor.col == 0)
7510 return FAIL;
7511
7512 curwin->w_set_curswant = TRUE;
7513 --curwin->w_cursor.col;
7514
7515#ifdef FEAT_MBYTE
7516 /* if the character on the left of the current cursor is a multi-byte
7517 * character, move to its first byte */
7518 if (has_mbyte)
7519 mb_adjust_cursor();
7520#endif
7521 return OK;
7522}
7523
7524 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007525cursor_up(
7526 long n,
7527 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
7529 linenr_T lnum;
7530
7531 if (n > 0)
7532 {
7533 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007534 /* This fails if the cursor is already in the first line or the count
7535 * is larger than the line number and '-' is in 'cpoptions' */
7536 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 return FAIL;
7538 if (n >= lnum)
7539 lnum = 1;
7540 else
7541#ifdef FEAT_FOLDING
7542 if (hasAnyFolding(curwin))
7543 {
7544 /*
7545 * Count each sequence of folded lines as one logical line.
7546 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007547 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 (void)hasFolding(lnum, &lnum, NULL);
7549
7550 while (n--)
7551 {
7552 /* move up one line */
7553 --lnum;
7554 if (lnum <= 1)
7555 break;
7556 /* If we entered a fold, move to the beginning, unless in
7557 * Insert mode or when 'foldopen' contains "all": it will open
7558 * in a moment. */
7559 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7560 (void)hasFolding(lnum, &lnum, NULL);
7561 }
7562 if (lnum < 1)
7563 lnum = 1;
7564 }
7565 else
7566#endif
7567 lnum -= n;
7568 curwin->w_cursor.lnum = lnum;
7569 }
7570
7571 /* try to advance to the column we want to be at */
7572 coladvance(curwin->w_curswant);
7573
7574 if (upd_topline)
7575 update_topline(); /* make sure curwin->w_topline is valid */
7576
7577 return OK;
7578}
7579
7580/*
7581 * Cursor down a number of logical lines.
7582 */
7583 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584cursor_down(
7585 long n,
7586 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587{
7588 linenr_T lnum;
7589
7590 if (n > 0)
7591 {
7592 lnum = curwin->w_cursor.lnum;
7593#ifdef FEAT_FOLDING
7594 /* Move to last line of fold, will fail if it's the end-of-file. */
7595 (void)hasFolding(lnum, NULL, &lnum);
7596#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007597 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007598 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007599 if (lnum >= curbuf->b_ml.ml_line_count
7600 || (lnum + n > curbuf->b_ml.ml_line_count
7601 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 return FAIL;
7603 if (lnum + n >= curbuf->b_ml.ml_line_count)
7604 lnum = curbuf->b_ml.ml_line_count;
7605 else
7606#ifdef FEAT_FOLDING
7607 if (hasAnyFolding(curwin))
7608 {
7609 linenr_T last;
7610
7611 /* count each sequence of folded lines as one logical line */
7612 while (n--)
7613 {
7614 if (hasFolding(lnum, NULL, &last))
7615 lnum = last + 1;
7616 else
7617 ++lnum;
7618 if (lnum >= curbuf->b_ml.ml_line_count)
7619 break;
7620 }
7621 if (lnum > curbuf->b_ml.ml_line_count)
7622 lnum = curbuf->b_ml.ml_line_count;
7623 }
7624 else
7625#endif
7626 lnum += n;
7627 curwin->w_cursor.lnum = lnum;
7628 }
7629
7630 /* try to advance to the column we want to be at */
7631 coladvance(curwin->w_curswant);
7632
7633 if (upd_topline)
7634 update_topline(); /* make sure curwin->w_topline is valid */
7635
7636 return OK;
7637}
7638
7639/*
7640 * Stuff the last inserted text in the read buffer.
7641 * Last_insert actually is a copy of the redo buffer, so we
7642 * first have to remove the command.
7643 */
7644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007645stuff_inserted(
7646 int c, /* Command character to be inserted */
7647 long count, /* Repeat this many times */
7648 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
7650 char_u *esc_ptr;
7651 char_u *ptr;
7652 char_u *last_ptr;
7653 char_u last = NUL;
7654
7655 ptr = get_last_insert();
7656 if (ptr == NULL)
7657 {
7658 EMSG(_(e_noinstext));
7659 return FAIL;
7660 }
7661
7662 /* may want to stuff the command character, to start Insert mode */
7663 if (c != NUL)
7664 stuffcharReadbuff(c);
7665 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7666 *esc_ptr = NUL; /* remove the ESC */
7667
7668 /* when the last char is either "0" or "^" it will be quoted if no ESC
7669 * comes after it OR if it will inserted more than once and "ptr"
7670 * starts with ^D. -- Acevedo
7671 */
7672 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7673 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7674 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7675 {
7676 last = *last_ptr;
7677 *last_ptr = NUL;
7678 }
7679
7680 do
7681 {
7682 stuffReadbuff(ptr);
7683 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7684 if (last)
7685 stuffReadbuff((char_u *)(last == '0'
7686 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7687 : IF_EB("\026^", CTRL_V_STR "^")));
7688 }
7689 while (--count > 0);
7690
7691 if (last)
7692 *last_ptr = last;
7693
7694 if (esc_ptr != NULL)
7695 *esc_ptr = ESC; /* put the ESC back */
7696
7697 /* may want to stuff a trailing ESC, to get out of Insert mode */
7698 if (!no_esc)
7699 stuffcharReadbuff(ESC);
7700
7701 return OK;
7702}
7703
7704 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007705get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 if (last_insert == NULL)
7708 return NULL;
7709 return last_insert + last_insert_skip;
7710}
7711
7712/*
7713 * Get last inserted string, and remove trailing <Esc>.
7714 * Returns pointer to allocated memory (must be freed) or NULL.
7715 */
7716 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007717get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
7719 char_u *s;
7720 int len;
7721
7722 if (last_insert == NULL)
7723 return NULL;
7724 s = vim_strsave(last_insert + last_insert_skip);
7725 if (s != NULL)
7726 {
7727 len = (int)STRLEN(s);
7728 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7729 s[len - 1] = NUL;
7730 }
7731 return s;
7732}
7733
7734/*
7735 * Check the word in front of the cursor for an abbreviation.
7736 * Called when the non-id character "c" has been entered.
7737 * When an abbreviation is recognized it is removed from the text and
7738 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7739 */
7740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007741echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
7743 /* Don't check for abbreviation in paste mode, when disabled and just
7744 * after moving around with cursor keys. */
7745 if (p_paste || no_abbr || arrow_used)
7746 return FALSE;
7747
7748 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7749 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7750}
7751
7752/*
7753 * replace-stack functions
7754 *
7755 * When replacing characters, the replaced characters are remembered for each
7756 * new character. This is used to re-insert the old text when backspacing.
7757 *
7758 * There is a NUL headed list of characters for each character that is
7759 * currently in the file after the insertion point. When BS is used, one NUL
7760 * headed list is put back for the deleted character.
7761 *
7762 * For a newline, there are two NUL headed lists. One contains the characters
7763 * that the NL replaced. The extra one stores the characters after the cursor
7764 * that were deleted (always white space).
7765 *
7766 * Replace_offset is normally 0, in which case replace_push will add a new
7767 * character at the end of the stack. If replace_offset is not 0, that many
7768 * characters will be left on the stack above the newly inserted character.
7769 */
7770
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007771static char_u *replace_stack = NULL;
7772static long replace_stack_nr = 0; /* next entry in replace stack */
7773static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007776replace_push(
7777 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 char_u *p;
7780
7781 if (replace_stack_nr < replace_offset) /* nothing to do */
7782 return;
7783 if (replace_stack_len <= replace_stack_nr)
7784 {
7785 replace_stack_len += 50;
7786 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7787 if (p == NULL) /* out of memory */
7788 {
7789 replace_stack_len -= 50;
7790 return;
7791 }
7792 if (replace_stack != NULL)
7793 {
7794 mch_memmove(p, replace_stack,
7795 (size_t)(replace_stack_nr * sizeof(char_u)));
7796 vim_free(replace_stack);
7797 }
7798 replace_stack = p;
7799 }
7800 p = replace_stack + replace_stack_nr - replace_offset;
7801 if (replace_offset)
7802 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7803 *p = c;
7804 ++replace_stack_nr;
7805}
7806
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007807#if defined(FEAT_MBYTE) || defined(PROTO)
7808/*
7809 * Push a character onto the replace stack. Handles a multi-byte character in
7810 * reverse byte order, so that the first byte is popped off first.
7811 * Return the number of bytes done (includes composing characters).
7812 */
7813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007814replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007815{
7816 int l = (*mb_ptr2len)(p);
7817 int j;
7818
7819 for (j = l - 1; j >= 0; --j)
7820 replace_push(p[j]);
7821 return l;
7822}
7823#endif
7824
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825/*
7826 * Pop one item from the replace stack.
7827 * return -1 if stack empty
7828 * return replaced character or NUL otherwise
7829 */
7830 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007831replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
7833 if (replace_stack_nr == 0)
7834 return -1;
7835 return (int)replace_stack[--replace_stack_nr];
7836}
7837
7838/*
7839 * Join the top two items on the replace stack. This removes to "off"'th NUL
7840 * encountered.
7841 */
7842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007843replace_join(
7844 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 int i;
7847
7848 for (i = replace_stack_nr; --i >= 0; )
7849 if (replace_stack[i] == NUL && off-- <= 0)
7850 {
7851 --replace_stack_nr;
7852 mch_memmove(replace_stack + i, replace_stack + i + 1,
7853 (size_t)(replace_stack_nr - i));
7854 return;
7855 }
7856}
7857
7858/*
7859 * Pop bytes from the replace stack until a NUL is found, and insert them
7860 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7861 */
7862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007863replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 int cc;
7866 int oldState = State;
7867
7868 State = NORMAL; /* don't want REPLACE here */
7869 while ((cc = replace_pop()) > 0)
7870 {
7871#ifdef FEAT_MBYTE
7872 mb_replace_pop_ins(cc);
7873#else
7874 ins_char(cc);
7875#endif
7876 dec_cursor();
7877 }
7878 State = oldState;
7879}
7880
7881#ifdef FEAT_MBYTE
7882/*
7883 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7884 * indicates a multi-byte char, pop the other bytes too.
7885 */
7886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007887mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
7889 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007890 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 int i;
7892 int c;
7893
7894 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7895 {
7896 buf[0] = cc;
7897 for (i = 1; i < n; ++i)
7898 buf[i] = replace_pop();
7899 ins_bytes_len(buf, n);
7900 }
7901 else
7902 ins_char(cc);
7903
7904 if (enc_utf8)
7905 /* Handle composing chars. */
7906 for (;;)
7907 {
7908 c = replace_pop();
7909 if (c == -1) /* stack empty */
7910 break;
7911 if ((n = MB_BYTE2LEN(c)) == 1)
7912 {
7913 /* Not a multi-byte char, put it back. */
7914 replace_push(c);
7915 break;
7916 }
7917 else
7918 {
7919 buf[0] = c;
7920 for (i = 1; i < n; ++i)
7921 buf[i] = replace_pop();
7922 if (utf_iscomposing(utf_ptr2char(buf)))
7923 ins_bytes_len(buf, n);
7924 else
7925 {
7926 /* Not a composing char, put it back. */
7927 for (i = n - 1; i >= 0; --i)
7928 replace_push(buf[i]);
7929 break;
7930 }
7931 }
7932 }
7933}
7934#endif
7935
7936/*
7937 * make the replace stack empty
7938 * (called when exiting replace mode)
7939 */
7940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007941replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007943 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 replace_stack_len = 0;
7945 replace_stack_nr = 0;
7946}
7947
7948/*
7949 * Handle doing a BS for one character.
7950 * cc < 0: replace stack empty, just move cursor
7951 * cc == 0: character was inserted, delete it
7952 * cc > 0: character was replaced, put cc (first byte of original char) back
7953 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007954 * When "limit_col" is >= 0, don't delete before this column. Matters when
7955 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 */
7957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007958replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
7960 int cc;
7961#ifdef FEAT_VREPLACE
7962 int orig_len = 0;
7963 int ins_len;
7964 int orig_vcols = 0;
7965 colnr_T start_vcol;
7966 char_u *p;
7967 int i;
7968 int vcol;
7969#endif
7970
7971 cc = replace_pop();
7972 if (cc > 0)
7973 {
7974#ifdef FEAT_VREPLACE
7975 if (State & VREPLACE_FLAG)
7976 {
7977 /* Get the number of screen cells used by the character we are
7978 * going to delete. */
7979 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7980 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7981 }
7982#endif
7983#ifdef FEAT_MBYTE
7984 if (has_mbyte)
7985 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007986 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987# ifdef FEAT_VREPLACE
7988 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007989 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990# endif
7991 replace_push(cc);
7992 }
7993 else
7994#endif
7995 {
7996 pchar_cursor(cc);
7997#ifdef FEAT_VREPLACE
7998 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007999 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000#endif
8001 }
8002 replace_pop_ins();
8003
8004#ifdef FEAT_VREPLACE
8005 if (State & VREPLACE_FLAG)
8006 {
8007 /* Get the number of screen cells used by the inserted characters */
8008 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008009 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 vcol = start_vcol;
8011 for (i = 0; i < ins_len; ++i)
8012 {
8013 vcol += chartabsize(p + i, vcol);
8014#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008015 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016#endif
8017 }
8018 vcol -= start_vcol;
8019
8020 /* Delete spaces that were inserted after the cursor to keep the
8021 * text aligned. */
8022 curwin->w_cursor.col += ins_len;
8023 while (vcol > orig_vcols && gchar_cursor() == ' ')
8024 {
8025 del_char(FALSE);
8026 ++orig_vcols;
8027 }
8028 curwin->w_cursor.col -= ins_len;
8029 }
8030#endif
8031
8032 /* mark the buffer as changed and prepare for displaying */
8033 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8034 }
8035 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008036 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037}
8038
8039#ifdef FEAT_CINDENT
8040/*
8041 * Return TRUE if C-indenting is on.
8042 */
8043 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008044cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
8046 return (!p_paste && (curbuf->b_p_cin
8047# ifdef FEAT_EVAL
8048 || *curbuf->b_p_inde != NUL
8049# endif
8050 ));
8051}
8052#endif
8053
8054#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8055/*
8056 * Re-indent the current line, based on the current contents of it and the
8057 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8058 * confused what all the part that handles Control-T is doing that I'm not.
8059 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8060 */
8061
8062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008063fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008065 int amount = get_the_indent();
8066
8067 if (amount >= 0)
8068 {
8069 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8070 if (linewhite(curwin->w_cursor.lnum))
8071 did_ai = TRUE; /* delete the indent if the line stays empty */
8072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073}
8074
8075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008076fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078 if (p_paste)
8079 return;
8080# ifdef FEAT_LISP
8081 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8082 fixthisline(get_lisp_indent);
8083# endif
8084# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8085 else
8086# endif
8087# ifdef FEAT_CINDENT
8088 if (cindent_on())
8089 do_c_expr_indent();
8090# endif
8091}
8092
8093#endif
8094
8095#ifdef FEAT_CINDENT
8096/*
8097 * return TRUE if 'cinkeys' contains the key "keytyped",
8098 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008099 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8101 *
8102 * "keytyped" can have a few special values:
8103 * KEY_OPEN_FORW
8104 * KEY_OPEN_BACK
8105 * KEY_COMPLETE just finished completion.
8106 *
8107 * If line_is_empty is TRUE accept keys with '0' before them.
8108 */
8109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008110in_cinkeys(
8111 int keytyped,
8112 int when,
8113 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114{
8115 char_u *look;
8116 int try_match;
8117 int try_match_word;
8118 char_u *p;
8119 char_u *line;
8120 int icase;
8121 int i;
8122
Bram Moolenaar30848942009-12-24 14:46:12 +00008123 if (keytyped == NUL)
8124 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8125 return FALSE;
8126
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127#ifdef FEAT_EVAL
8128 if (*curbuf->b_p_inde != NUL)
8129 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8130 else
8131#endif
8132 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8133 while (*look)
8134 {
8135 /*
8136 * Find out if we want to try a match with this key, depending on
8137 * 'when' and a '*' or '!' before the key.
8138 */
8139 switch (when)
8140 {
8141 case '*': try_match = (*look == '*'); break;
8142 case '!': try_match = (*look == '!'); break;
8143 default: try_match = (*look != '*'); break;
8144 }
8145 if (*look == '*' || *look == '!')
8146 ++look;
8147
8148 /*
8149 * If there is a '0', only accept a match if the line is empty.
8150 * But may still match when typing last char of a word.
8151 */
8152 if (*look == '0')
8153 {
8154 try_match_word = try_match;
8155 if (!line_is_empty)
8156 try_match = FALSE;
8157 ++look;
8158 }
8159 else
8160 try_match_word = FALSE;
8161
8162 /*
8163 * does it look like a control character?
8164 */
8165 if (*look == '^'
8166#ifdef EBCDIC
8167 && (Ctrl_chr(look[1]) != 0)
8168#else
8169 && look[1] >= '?' && look[1] <= '_'
8170#endif
8171 )
8172 {
8173 if (try_match && keytyped == Ctrl_chr(look[1]))
8174 return TRUE;
8175 look += 2;
8176 }
8177 /*
8178 * 'o' means "o" command, open forward.
8179 * 'O' means "O" command, open backward.
8180 */
8181 else if (*look == 'o')
8182 {
8183 if (try_match && keytyped == KEY_OPEN_FORW)
8184 return TRUE;
8185 ++look;
8186 }
8187 else if (*look == 'O')
8188 {
8189 if (try_match && keytyped == KEY_OPEN_BACK)
8190 return TRUE;
8191 ++look;
8192 }
8193
8194 /*
8195 * 'e' means to check for "else" at start of line and just before the
8196 * cursor.
8197 */
8198 else if (*look == 'e')
8199 {
8200 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8201 {
8202 p = ml_get_curline();
8203 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8204 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8205 return TRUE;
8206 }
8207 ++look;
8208 }
8209
8210 /*
8211 * ':' only causes an indent if it is at the end of a label or case
8212 * statement, or when it was before typing the ':' (to fix
8213 * class::method for C++).
8214 */
8215 else if (*look == ':')
8216 {
8217 if (try_match && keytyped == ':')
8218 {
8219 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008220 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008222 /* Need to get the line again after cin_islabel(). */
8223 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 if (curwin->w_cursor.col > 2
8225 && p[curwin->w_cursor.col - 1] == ':'
8226 && p[curwin->w_cursor.col - 2] == ':')
8227 {
8228 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008229 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008230 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 p = ml_get_curline();
8232 p[curwin->w_cursor.col - 1] = ':';
8233 if (i)
8234 return TRUE;
8235 }
8236 }
8237 ++look;
8238 }
8239
8240
8241 /*
8242 * Is it a key in <>, maybe?
8243 */
8244 else if (*look == '<')
8245 {
8246 if (try_match)
8247 {
8248 /*
8249 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8250 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8251 * >, *, : and ! keys if they really really want to.
8252 */
8253 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8254 && keytyped == look[1])
8255 return TRUE;
8256
8257 if (keytyped == get_special_key_code(look + 1))
8258 return TRUE;
8259 }
8260 while (*look && *look != '>')
8261 look++;
8262 while (*look == '>')
8263 look++;
8264 }
8265
8266 /*
8267 * Is it a word: "=word"?
8268 */
8269 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8270 {
8271 ++look;
8272 if (*look == '~')
8273 {
8274 icase = TRUE;
8275 ++look;
8276 }
8277 else
8278 icase = FALSE;
8279 p = vim_strchr(look, ',');
8280 if (p == NULL)
8281 p = look + STRLEN(look);
8282 if ((try_match || try_match_word)
8283 && curwin->w_cursor.col >= (colnr_T)(p - look))
8284 {
8285 int match = FALSE;
8286
8287#ifdef FEAT_INS_EXPAND
8288 if (keytyped == KEY_COMPLETE)
8289 {
8290 char_u *s;
8291
8292 /* Just completed a word, check if it starts with "look".
8293 * search back for the start of a word. */
8294 line = ml_get_curline();
8295# ifdef FEAT_MBYTE
8296 if (has_mbyte)
8297 {
8298 char_u *n;
8299
8300 for (s = line + curwin->w_cursor.col; s > line; s = n)
8301 {
8302 n = mb_prevptr(line, s);
8303 if (!vim_iswordp(n))
8304 break;
8305 }
8306 }
8307 else
8308# endif
8309 for (s = line + curwin->w_cursor.col; s > line; --s)
8310 if (!vim_iswordc(s[-1]))
8311 break;
8312 if (s + (p - look) <= line + curwin->w_cursor.col
8313 && (icase
8314 ? MB_STRNICMP(s, look, p - look)
8315 : STRNCMP(s, look, p - look)) == 0)
8316 match = TRUE;
8317 }
8318 else
8319#endif
8320 /* TODO: multi-byte */
8321 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8322 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8323 {
8324 line = ml_get_cursor();
8325 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8326 || !vim_iswordc(line[-(p - look) - 1]))
8327 && (icase
8328 ? MB_STRNICMP(line - (p - look), look, p - look)
8329 : STRNCMP(line - (p - look), look, p - look))
8330 == 0)
8331 match = TRUE;
8332 }
8333 if (match && try_match_word && !try_match)
8334 {
8335 /* "0=word": Check if there are only blanks before the
8336 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008337 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 (int)(curwin->w_cursor.col - (p - look)))
8339 match = FALSE;
8340 }
8341 if (match)
8342 return TRUE;
8343 }
8344 look = p;
8345 }
8346
8347 /*
8348 * ok, it's a boring generic character.
8349 */
8350 else
8351 {
8352 if (try_match && *look == keytyped)
8353 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008354 if (*look != NUL)
8355 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 }
8357
8358 /*
8359 * Skip over ", ".
8360 */
8361 look = skip_to_option_part(look);
8362 }
8363 return FALSE;
8364}
8365#endif /* FEAT_CINDENT */
8366
8367#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8368/*
8369 * Map Hebrew keyboard when in hkmap mode.
8370 */
8371 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008372hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8375 {
8376 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8377 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8378 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8379 static char_u map[26] =
8380 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8381 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8382 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8383 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8384 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8385 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8386 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8387 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8388 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8389
8390 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8391 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8392 /* '-1'='sofit' */
8393 else if (c == 'x')
8394 return 'X';
8395 else if (c == 'q')
8396 return '\''; /* {geresh}={'} */
8397 else if (c == 246)
8398 return ' '; /* \"o --> ' ' for a german keyboard */
8399 else if (c == 228)
8400 return ' '; /* \"a --> ' ' -- / -- */
8401 else if (c == 252)
8402 return ' '; /* \"u --> ' ' -- / -- */
8403#ifdef EBCDIC
8404 else if (islower(c))
8405#else
8406 /* NOTE: islower() does not do the right thing for us on Linux so we
8407 * do this the same was as 5.7 and previous, so it works correctly on
8408 * all systems. Specifically, the e.g. Delete and Arrow keys are
8409 * munged and won't work if e.g. searching for Hebrew text.
8410 */
8411 else if (c >= 'a' && c <= 'z')
8412#endif
8413 return (int)(map[CharOrdLow(c)] + p_aleph);
8414 else
8415 return c;
8416 }
8417 else
8418 {
8419 switch (c)
8420 {
8421 case '`': return ';';
8422 case '/': return '.';
8423 case '\'': return ',';
8424 case 'q': return '/';
8425 case 'w': return '\'';
8426
8427 /* Hebrew letters - set offset from 'a' */
8428 case ',': c = '{'; break;
8429 case '.': c = 'v'; break;
8430 case ';': c = 't'; break;
8431 default: {
8432 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8433
8434#ifdef EBCDIC
8435 /* see note about islower() above */
8436 if (!islower(c))
8437#else
8438 if (c < 'a' || c > 'z')
8439#endif
8440 return c;
8441 c = str[CharOrdLow(c)];
8442 break;
8443 }
8444 }
8445
8446 return (int)(CharOrdLow(c) + p_aleph);
8447 }
8448}
8449#endif
8450
8451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008452ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
8454 int need_redraw = FALSE;
8455 int regname;
8456 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008457 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458
8459 /*
8460 * If we are going to wait for a character, show a '"'.
8461 */
8462 pc_status = PC_STATUS_UNSET;
8463 if (redrawing() && !char_avail())
8464 {
8465 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008466 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
8468 edit_putchar('"', TRUE);
8469#ifdef FEAT_CMDL_INFO
8470 add_to_showcmd_c(Ctrl_R);
8471#endif
8472 }
8473
8474#ifdef USE_ON_FLY_SCROLL
8475 dont_scroll = TRUE; /* disallow scrolling here */
8476#endif
8477
8478 /*
8479 * Don't map the register name. This also prevents the mode message to be
8480 * deleted when ESC is hit.
8481 */
8482 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008483 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8486 {
8487 /* Get a third key for literal register insertion */
8488 literally = regname;
8489#ifdef FEAT_CMDL_INFO
8490 add_to_showcmd_c(literally);
8491#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008492 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 }
8495 --no_mapping;
8496
8497#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008498 /* Don't call u_sync() while typing the expression or giving an error
8499 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 ++no_u_sync;
8501 if (regname == '=')
8502 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008503# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008505# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008506 /* Sync undo when evaluating the expression calls setline() or
8507 * append(), so that it can be undone separately. */
8508 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008511# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 /* Restore the Input Method. */
8513 if (im_on)
8514 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008517 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8518 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008519 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 else
8523 {
8524#endif
8525 if (literally == Ctrl_O || literally == Ctrl_P)
8526 {
8527 /* Append the command to the redo buffer. */
8528 AppendCharToRedobuff(Ctrl_R);
8529 AppendCharToRedobuff(literally);
8530 AppendCharToRedobuff(regname);
8531
8532 do_put(regname, BACKWARD, 1L,
8533 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8534 }
8535 else if (insert_reg(regname, literally) == FAIL)
8536 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008537 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 need_redraw = TRUE; /* remove the '"' */
8539 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008540 else if (stop_insert_mode)
8541 /* When the '=' register was used and a function was invoked that
8542 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8543 * insert anything, need to remove the '"' */
8544 need_redraw = TRUE;
8545
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#ifdef FEAT_EVAL
8547 }
8548 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008549 if (u_sync_once == 1)
8550 ins_need_undo = TRUE;
8551 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552#endif
8553#ifdef FEAT_CMDL_INFO
8554 clear_showcmd();
8555#endif
8556
8557 /* If the inserted register is empty, we need to remove the '"' */
8558 if (need_redraw || stuff_empty())
8559 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008560
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008561 /* Disallow starting Visual mode here, would get a weird mode. */
8562 if (!vis_active && VIsual_active)
8563 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564}
8565
8566/*
8567 * CTRL-G commands in Insert mode.
8568 */
8569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008570ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 int c;
8573
8574#ifdef FEAT_INS_EXPAND
8575 /* Right after CTRL-X the cursor will be after the ruler. */
8576 setcursor();
8577#endif
8578
8579 /*
8580 * Don't map the second key. This also prevents the mode message to be
8581 * deleted when ESC is hit.
8582 */
8583 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008584 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 --no_mapping;
8586 switch (c)
8587 {
8588 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8589 case K_UP:
8590 case Ctrl_K:
8591 case 'k': ins_up(TRUE);
8592 break;
8593
8594 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8595 case K_DOWN:
8596 case Ctrl_J:
8597 case 'j': ins_down(TRUE);
8598 break;
8599
8600 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008601 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008603
8604 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008605 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008606 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008607 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 break;
8609
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008610 /* CTRL-G U: do not break undo with the next char */
8611 case 'U':
8612 /* Allow one left/right cursor movement with the next char,
8613 * without breaking undo. */
8614 dont_sync_undo = MAYBE;
8615 break;
8616
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008618 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 }
8620}
8621
8622/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008623 * CTRL-^ in Insert mode.
8624 */
8625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008626ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008627{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008628 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008629 {
8630 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8631 if (State & LANGMAP)
8632 {
8633 curbuf->b_p_iminsert = B_IMODE_NONE;
8634 State &= ~LANGMAP;
8635 }
8636 else
8637 {
8638 curbuf->b_p_iminsert = B_IMODE_LMAP;
8639 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008640#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008641 im_set_active(FALSE);
8642#endif
8643 }
8644 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008645#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008646 else
8647 {
8648 /* There are no ":lmap" mappings, toggle IM */
8649 if (im_get_status())
8650 {
8651 curbuf->b_p_iminsert = B_IMODE_NONE;
8652 im_set_active(FALSE);
8653 }
8654 else
8655 {
8656 curbuf->b_p_iminsert = B_IMODE_IM;
8657 State &= ~LANGMAP;
8658 im_set_active(TRUE);
8659 }
8660 }
8661#endif
8662 set_iminsert_global();
8663 showmode();
8664#ifdef FEAT_GUI
8665 /* may show different cursor shape or color */
8666 if (gui.in_use)
8667 gui_update_cursor(TRUE, FALSE);
8668#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008669#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008670 /* Show/unshow value of 'keymap' in status lines. */
8671 status_redraw_curbuf();
8672#endif
8673}
8674
8675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 * Handle ESC in insert mode.
8677 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8678 * insert.
8679 */
8680 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008681ins_esc(
8682 long *count,
8683 int cmdchar,
8684 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686 int temp;
8687 static int disabled_redraw = FALSE;
8688
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008689#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008690 check_spell_redraw();
8691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692#if defined(FEAT_HANGULIN)
8693# if defined(ESC_CHG_TO_ENG_MODE)
8694 hangul_input_state_set(0);
8695# endif
8696 if (composing_hangul)
8697 {
8698 push_raw_key(composing_hangul_buffer, 2);
8699 composing_hangul = 0;
8700 }
8701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702
8703 temp = curwin->w_cursor.col;
8704 if (disabled_redraw)
8705 {
8706 --RedrawingDisabled;
8707 disabled_redraw = FALSE;
8708 }
8709 if (!arrow_used)
8710 {
8711 /*
8712 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008713 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8714 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 */
8716 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008717 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718
8719 /*
8720 * Repeating insert may take a long time. Check for
8721 * interrupt now and then.
8722 */
8723 if (*count > 0)
8724 {
8725 line_breakcheck();
8726 if (got_int)
8727 *count = 0;
8728 }
8729
8730 if (--*count > 0) /* repeat what was typed */
8731 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008732 /* Vi repeats the insert without replacing characters. */
8733 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8734 State &= ~REPLACE_FLAG;
8735
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 (void)start_redo_ins();
8737 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008738 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 ++RedrawingDisabled;
8740 disabled_redraw = TRUE;
8741 return FALSE; /* repeat the insert */
8742 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008743 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 undisplay_dollar();
8745 }
8746
8747 /* When an autoindent was removed, curswant stays after the
8748 * indent */
8749 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8750 curwin->w_set_curswant = TRUE;
8751
8752 /* Remember the last Insert position in the '^ mark. */
8753 if (!cmdmod.keepjumps)
8754 curbuf->b_last_insert = curwin->w_cursor;
8755
8756 /*
8757 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008758 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008760 if (!nomove
8761 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762#ifdef FEAT_VIRTUALEDIT
8763 || curwin->w_cursor.coladd > 0
8764#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008765 )
8766 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008767 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768#ifdef FEAT_RIGHTLEFT
8769 && !revins_on
8770#endif
8771 )
8772 {
8773#ifdef FEAT_VIRTUALEDIT
8774 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8775 {
8776 oneleft();
8777 if (restart_edit != NUL)
8778 ++curwin->w_cursor.coladd;
8779 }
8780 else
8781#endif
8782 {
8783 --curwin->w_cursor.col;
8784#ifdef FEAT_MBYTE
8785 /* Correct cursor for multi-byte character. */
8786 if (has_mbyte)
8787 mb_adjust_cursor();
8788#endif
8789 }
8790 }
8791
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008792#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 /* Disable IM to allow typing English directly for Normal mode commands.
8794 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8795 * well). */
8796 if (!(State & LANGMAP))
8797 im_save_status(&curbuf->b_p_iminsert);
8798 im_set_active(FALSE);
8799#endif
8800
8801 State = NORMAL;
8802 /* need to position cursor again (e.g. when on a TAB ) */
8803 changed_cline_bef_curs();
8804
8805#ifdef FEAT_MOUSE
8806 setmouse();
8807#endif
8808#ifdef CURSOR_SHAPE
8809 ui_cursor_shape(); /* may show different cursor shape */
8810#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008811 if (!p_ek)
8812 /* Re-enable bracketed paste mode. */
8813 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814
8815 /*
8816 * When recording or for CTRL-O, need to display the new mode.
8817 * Otherwise remove the mode message.
8818 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008819 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 showmode();
8821 else if (p_smd)
8822 MSG("");
8823
8824 return TRUE; /* exit Insert mode */
8825}
8826
8827#ifdef FEAT_RIGHTLEFT
8828/*
8829 * Toggle language: hkmap and revins_on.
8830 * Move to end of reverse inserted text.
8831 */
8832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008833ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835 if (revins_on && revins_chars && revins_scol >= 0)
8836 {
8837 while (gchar_cursor() != NUL && revins_chars--)
8838 ++curwin->w_cursor.col;
8839 }
8840 p_ri = !p_ri;
8841 revins_on = (State == INSERT && p_ri);
8842 if (revins_on)
8843 {
8844 revins_scol = curwin->w_cursor.col;
8845 revins_legal++;
8846 revins_chars = 0;
8847 undisplay_dollar();
8848 }
8849 else
8850 revins_scol = -1;
8851#ifdef FEAT_FKMAP
8852 if (p_altkeymap)
8853 {
8854 /*
8855 * to be consistent also for redo command, using '.'
8856 * set arrow_used to true and stop it - causing to redo
8857 * characters entered in one mode (normal/reverse insert).
8858 */
8859 arrow_used = TRUE;
8860 (void)stop_arrow();
8861 p_fkmap = curwin->w_p_rl ^ p_ri;
8862 if (p_fkmap && p_ri)
8863 State = INSERT;
8864 }
8865 else
8866#endif
8867 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8868 showmode();
8869}
8870#endif
8871
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872/*
8873 * If 'keymodel' contains "startsel", may start selection.
8874 * Returns TRUE when a CTRL-O and other keys stuffed.
8875 */
8876 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008877ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 if (km_startsel)
8880 switch (c)
8881 {
8882 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 case K_PAGEUP:
8885 case K_KPAGEUP:
8886 case K_PAGEDOWN:
8887 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008888# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 case K_LEFT:
8890 case K_RIGHT:
8891 case K_UP:
8892 case K_DOWN:
8893 case K_END:
8894 case K_HOME:
8895# endif
8896 if (!(mod_mask & MOD_MASK_SHIFT))
8897 break;
8898 /* FALLTHROUGH */
8899 case K_S_LEFT:
8900 case K_S_RIGHT:
8901 case K_S_UP:
8902 case K_S_DOWN:
8903 case K_S_END:
8904 case K_S_HOME:
8905 /* Start selection right away, the cursor can move with
8906 * CTRL-O when beyond the end of the line. */
8907 start_selection();
8908
8909 /* Execute the key in (insert) Select mode. */
8910 stuffcharReadbuff(Ctrl_O);
8911 if (mod_mask)
8912 {
8913 char_u buf[4];
8914
8915 buf[0] = K_SPECIAL;
8916 buf[1] = KS_MODIFIER;
8917 buf[2] = mod_mask;
8918 buf[3] = NUL;
8919 stuffReadbuff(buf);
8920 }
8921 stuffcharReadbuff(c);
8922 return TRUE;
8923 }
8924 return FALSE;
8925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926
8927/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008928 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008929 */
8930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008931ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008932{
8933#ifdef FEAT_FKMAP
8934 if (p_fkmap && p_ri)
8935 {
8936 beep_flush();
8937 EMSG(farsi_text_3); /* encoded in Farsi */
8938 return;
8939 }
8940#endif
8941
Bram Moolenaar1e015462005-09-25 22:16:38 +00008942# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008943 set_vim_var_string(VV_INSERTMODE,
8944 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008945# ifdef FEAT_VREPLACE
8946 replaceState == VREPLACE ? "v" :
8947# endif
8948 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008949# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008950 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008951 if (State & REPLACE_FLAG)
8952 State = INSERT | (State & LANGMAP);
8953 else
8954 State = replaceState | (State & LANGMAP);
8955 AppendCharToRedobuff(K_INS);
8956 showmode();
8957#ifdef CURSOR_SHAPE
8958 ui_cursor_shape(); /* may show different cursor shape */
8959#endif
8960}
8961
8962/*
8963 * Pressed CTRL-O in Insert mode.
8964 */
8965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008966ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008967{
8968#ifdef FEAT_VREPLACE
8969 if (State & VREPLACE_FLAG)
8970 restart_edit = 'V';
8971 else
8972#endif
8973 if (State & REPLACE_FLAG)
8974 restart_edit = 'R';
8975 else
8976 restart_edit = 'I';
8977#ifdef FEAT_VIRTUALEDIT
8978 if (virtual_active())
8979 ins_at_eol = FALSE; /* cursor always keeps its column */
8980 else
8981#endif
8982 ins_at_eol = (gchar_cursor() == NUL);
8983}
8984
8985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 * If the cursor is on an indent, ^T/^D insert/delete one
8987 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008988 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 * with vi. But vi only supports ^T and ^D after an
8990 * autoindent, we support it everywhere.
8991 */
8992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008993ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
8995 if (stop_arrow() == FAIL)
8996 return;
8997 AppendCharToRedobuff(c);
8998
8999 /*
9000 * 0^D and ^^D: remove all indent.
9001 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009002 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9003 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 {
9005 --curwin->w_cursor.col;
9006 (void)del_char(FALSE); /* delete the '^' or '0' */
9007 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9008 if (State & REPLACE_FLAG)
9009 replace_pop_ins();
9010 if (lastc == '^')
9011 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009012 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 }
9014 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009015 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016
9017 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9018 did_ai = FALSE;
9019#ifdef FEAT_SMARTINDENT
9020 did_si = FALSE;
9021 can_si = FALSE;
9022 can_si_back = FALSE;
9023#endif
9024#ifdef FEAT_CINDENT
9025 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9026#endif
9027}
9028
9029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009030ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
9032 int temp;
9033
9034 if (stop_arrow() == FAIL)
9035 return;
9036 if (gchar_cursor() == NUL) /* delete newline */
9037 {
9038 temp = curwin->w_cursor.col;
9039 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009040 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009041 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009045#ifdef FEAT_VREPLACE
9046 /* Adjust orig_line_count in case more lines have been deleted than
9047 * have been added. That makes sure, that open_line() later
9048 * can access all buffer lines correctly */
9049 if (State & VREPLACE_FLAG &&
9050 orig_line_count > curbuf->b_ml.ml_line_count)
9051 orig_line_count = curbuf->b_ml.ml_line_count;
9052#endif
9053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009055 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9056 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 did_ai = FALSE;
9058#ifdef FEAT_SMARTINDENT
9059 did_si = FALSE;
9060 can_si = FALSE;
9061 can_si_back = FALSE;
9062#endif
9063 AppendCharToRedobuff(K_DEL);
9064}
9065
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009066static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009067
9068/*
9069 * Delete one character for ins_bs().
9070 */
9071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009072ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009073{
9074 dec_cursor();
9075 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9076 if (State & REPLACE_FLAG)
9077 {
9078 /* Don't delete characters before the insert point when in
9079 * Replace mode */
9080 if (curwin->w_cursor.lnum != Insstart.lnum
9081 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009082 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009083 }
9084 else
9085 (void)del_char(FALSE);
9086}
9087
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088/*
9089 * Handle Backspace, delete-word and delete-line in Insert mode.
9090 * Return TRUE when backspace was actually used.
9091 */
9092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009093ins_bs(
9094 int c,
9095 int mode,
9096 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
9098 linenr_T lnum;
9099 int cc;
9100 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009101 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 colnr_T mincol;
9103 int did_backspace = FALSE;
9104 int in_indent;
9105 int oldState;
9106#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009107 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108#endif
9109
9110 /*
9111 * can't delete anything in an empty file
9112 * can't backup past first character in buffer
9113 * can't backup past starting point unless 'backspace' > 1
9114 * can backup to a previous line if 'backspace' == 0
9115 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009116 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 || (
9118#ifdef FEAT_RIGHTLEFT
9119 !revins_on &&
9120#endif
9121 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9122 || (!can_bs(BS_START)
9123 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009124 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9125 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9127 && curwin->w_cursor.col <= ai_col)
9128 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9129 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009130 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 return FALSE;
9132 }
9133
9134 if (stop_arrow() == FAIL)
9135 return FALSE;
9136 in_indent = inindent(0);
9137#ifdef FEAT_CINDENT
9138 if (in_indent)
9139 can_cindent = FALSE;
9140#endif
9141#ifdef FEAT_COMMENTS
9142 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9143#endif
9144#ifdef FEAT_RIGHTLEFT
9145 if (revins_on) /* put cursor after last inserted char */
9146 inc_cursor();
9147#endif
9148
9149#ifdef FEAT_VIRTUALEDIT
9150 /* Virtualedit:
9151 * BACKSPACE_CHAR eats a virtual space
9152 * BACKSPACE_WORD eats all coladd
9153 * BACKSPACE_LINE eats all coladd and keeps going
9154 */
9155 if (curwin->w_cursor.coladd > 0)
9156 {
9157 if (mode == BACKSPACE_CHAR)
9158 {
9159 --curwin->w_cursor.coladd;
9160 return TRUE;
9161 }
9162 if (mode == BACKSPACE_WORD)
9163 {
9164 curwin->w_cursor.coladd = 0;
9165 return TRUE;
9166 }
9167 curwin->w_cursor.coladd = 0;
9168 }
9169#endif
9170
9171 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009172 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 */
9174 if (curwin->w_cursor.col == 0)
9175 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009176 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009177 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178#ifdef FEAT_RIGHTLEFT
9179 || revins_on
9180#endif
9181 )
9182 {
9183 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9184 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9185 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009186 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009187 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 }
9189 /*
9190 * In replace mode:
9191 * cc < 0: NL was inserted, delete it
9192 * cc >= 0: NL was replaced, put original characters back
9193 */
9194 cc = -1;
9195 if (State & REPLACE_FLAG)
9196 cc = replace_pop(); /* returns -1 if NL was inserted */
9197 /*
9198 * In replace mode, in the line we started replacing, we only move the
9199 * cursor.
9200 */
9201 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9202 {
9203 dec_cursor();
9204 }
9205 else
9206 {
9207#ifdef FEAT_VREPLACE
9208 if (!(State & VREPLACE_FLAG)
9209 || curwin->w_cursor.lnum > orig_line_count)
9210#endif
9211 {
9212 temp = gchar_cursor(); /* remember current char */
9213 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009214
9215 /* When "aw" is in 'formatoptions' we must delete the space at
9216 * the end of the line, otherwise the line will be broken
9217 * again when auto-formatting. */
9218 if (has_format_option(FO_AUTO)
9219 && has_format_option(FO_WHITE_PAR))
9220 {
9221 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9222 TRUE);
9223 int len;
9224
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009225 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009226 if (len > 0 && ptr[len - 1] == ' ')
9227 ptr[len - 1] = NUL;
9228 }
9229
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009230 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 if (temp == NUL && gchar_cursor() != NUL)
9232 inc_cursor();
9233 }
9234#ifdef FEAT_VREPLACE
9235 else
9236 dec_cursor();
9237#endif
9238
9239 /*
9240 * In REPLACE mode we have to put back the text that was replaced
9241 * by the NL. On the replace stack is first a NUL-terminated
9242 * sequence of characters that were deleted and then the
9243 * characters that NL replaced.
9244 */
9245 if (State & REPLACE_FLAG)
9246 {
9247 /*
9248 * Do the next ins_char() in NORMAL state, to
9249 * prevent ins_char() from replacing characters and
9250 * avoiding showmatch().
9251 */
9252 oldState = State;
9253 State = NORMAL;
9254 /*
9255 * restore characters (blanks) deleted after cursor
9256 */
9257 while (cc > 0)
9258 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009259 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260#ifdef FEAT_MBYTE
9261 mb_replace_pop_ins(cc);
9262#else
9263 ins_char(cc);
9264#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009265 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 cc = replace_pop();
9267 }
9268 /* restore the characters that NL replaced */
9269 replace_pop_ins();
9270 State = oldState;
9271 }
9272 }
9273 did_ai = FALSE;
9274 }
9275 else
9276 {
9277 /*
9278 * Delete character(s) before the cursor.
9279 */
9280#ifdef FEAT_RIGHTLEFT
9281 if (revins_on) /* put cursor on last inserted char */
9282 dec_cursor();
9283#endif
9284 mincol = 0;
9285 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009286 if (mode == BACKSPACE_LINE
9287 && (curbuf->b_p_ai
9288#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009289 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009290#endif
9291 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292#ifdef FEAT_RIGHTLEFT
9293 && !revins_on
9294#endif
9295 )
9296 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009297 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009299 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009301 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
9303
9304 /*
9305 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9306 */
9307 if ( mode == BACKSPACE_CHAR
9308 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009309 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009310 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 && (*(ml_get_cursor() - 1) == TAB
9312 || (*(ml_get_cursor() - 1) == ' '
9313 && (!*inserted_space_p
9314 || arrow_used))))))
9315 {
9316 int ts;
9317 colnr_T vcol;
9318 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009319 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320
9321 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009322 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009323 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009325 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 /* Compute the virtual column where we want to be. Since
9327 * 'showbreak' may get in the way, need to get the last column of
9328 * the previous character. */
9329 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009330 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 dec_cursor();
9332 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9333 inc_cursor();
9334 want_vcol = (want_vcol / ts) * ts;
9335
9336 /* delete characters until we are at or before want_vcol */
9337 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009338 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009339 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340
9341 /* insert extra spaces until we are at want_vcol */
9342 while (vcol < want_vcol)
9343 {
9344 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009345 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9346 && curwin->w_cursor.col < Insstart_orig.col)
9347 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
9349#ifdef FEAT_VREPLACE
9350 if (State & VREPLACE_FLAG)
9351 ins_char(' ');
9352 else
9353#endif
9354 {
9355 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009356 if ((State & REPLACE_FLAG))
9357 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 }
9359 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9360 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009361
9362 /* If we are now back where we started delete one character. Can
9363 * happen when using 'sts' and 'linebreak'. */
9364 if (vcol >= start_vcol)
9365 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 }
9367
9368 /*
9369 * Delete upto starting point, start of line or previous word.
9370 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009373#ifdef FEAT_MBYTE
9374 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009376 if (has_mbyte)
9377 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009379 do
9380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009382 if (!revins_on) /* put cursor on char to be deleted */
9383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009385
9386 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009388 /* look multi-byte character class */
9389 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009391 prev_cclass = cclass;
9392 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009395
9396 /* start of word? */
9397 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9398 {
9399 mode = BACKSPACE_WORD_NOT_SPACE;
9400 temp = vim_iswordc(cc);
9401 }
9402 /* end of word? */
9403 else if (mode == BACKSPACE_WORD_NOT_SPACE
9404 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9405#ifdef FEAT_MBYTE
9406 || prev_cclass != cclass
9407#endif
9408 ))
9409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009411 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009413 inc_cursor();
9414#ifdef FEAT_RIGHTLEFT
9415 else if (State & REPLACE_FLAG)
9416 dec_cursor();
9417#endif
9418 break;
9419 }
9420 if (State & REPLACE_FLAG)
9421 replace_do_bs(-1);
9422 else
9423 {
9424#ifdef FEAT_MBYTE
9425 if (enc_utf8 && p_deco)
9426 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9427#endif
9428 (void)del_char(FALSE);
9429#ifdef FEAT_MBYTE
9430 /*
9431 * If there are combining characters and 'delcombine' is set
9432 * move the cursor back. Don't back up before the base
9433 * character.
9434 */
9435 if (enc_utf8 && p_deco && cpc[0] != NUL)
9436 inc_cursor();
9437#endif
9438#ifdef FEAT_RIGHTLEFT
9439 if (revins_chars)
9440 {
9441 revins_chars--;
9442 revins_legal++;
9443 }
9444 if (revins_on && gchar_cursor() == NUL)
9445 break;
9446#endif
9447 }
9448 /* Just a single backspace?: */
9449 if (mode == BACKSPACE_CHAR)
9450 break;
9451 } while (
9452#ifdef FEAT_RIGHTLEFT
9453 revins_on ||
9454#endif
9455 (curwin->w_cursor.col > mincol
9456 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9457 || curwin->w_cursor.col != Insstart_orig.col)));
9458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 did_backspace = TRUE;
9460 }
9461#ifdef FEAT_SMARTINDENT
9462 did_si = FALSE;
9463 can_si = FALSE;
9464 can_si_back = FALSE;
9465#endif
9466 if (curwin->w_cursor.col <= 1)
9467 did_ai = FALSE;
9468 /*
9469 * It's a little strange to put backspaces into the redo
9470 * buffer, but it makes auto-indent a lot easier to deal
9471 * with.
9472 */
9473 AppendCharToRedobuff(c);
9474
9475 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009476 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009477 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009478 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
9480 /* vi behaviour: the cursor moves backward but the character that
9481 * was there remains visible
9482 * Vim behaviour: the cursor moves backward and the character that
9483 * was there is erased from the screen.
9484 * We can emulate the vi behaviour by pretending there is a dollar
9485 * displayed even when there isn't.
9486 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009487 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 dollar_vcol = curwin->w_virtcol;
9489
Bram Moolenaarce3be472008-01-14 19:12:28 +00009490#ifdef FEAT_FOLDING
9491 /* When deleting a char the cursor line must never be in a closed fold.
9492 * E.g., when 'foldmethod' is indent and deleting the first non-white
9493 * char before a Tab. */
9494 if (did_backspace)
9495 foldOpenCursor();
9496#endif
9497
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 return did_backspace;
9499}
9500
9501#ifdef FEAT_MOUSE
9502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009503ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504{
9505 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009506 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507
9508# ifdef FEAT_GUI
9509 /* When GUI is active, also move/paste when 'mouse' is empty */
9510 if (!gui.in_use)
9511# endif
9512 if (!mouse_has(MOUSE_INSERT))
9513 return;
9514
9515 undisplay_dollar();
9516 tpos = curwin->w_cursor;
9517 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9518 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009519 win_T *new_curwin = curwin;
9520
9521 if (curwin != old_curwin && win_valid(old_curwin))
9522 {
9523 /* Mouse took us to another window. We need to go back to the
9524 * previous one to stop insert there properly. */
9525 curwin = old_curwin;
9526 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009527#ifdef FEAT_JOB_CHANNEL
9528 if (bt_prompt(curbuf))
9529 // Restart Insert mode when re-entering the prompt buffer.
9530 curbuf->b_prompt_insert = 'A';
9531#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009532 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009533 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009534 if (curwin != new_curwin && win_valid(new_curwin))
9535 {
9536 curwin = new_curwin;
9537 curbuf = curwin->w_buffer;
9538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539# ifdef FEAT_CINDENT
9540 can_cindent = TRUE;
9541# endif
9542 }
9543
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 /* redraw status lines (in case another window became active) */
9545 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546}
9547
9548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009549ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009552 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009553# ifdef FEAT_INS_EXPAND
9554 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555# endif
9556
9557 tpos = curwin->w_cursor;
9558
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009559 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 {
9561 int row, col;
9562
9563 row = mouse_row;
9564 col = mouse_col;
9565
9566 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009567 wp = mouse_find_win(&row, &col);
9568 if (wp == NULL)
9569 return;
9570 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 curbuf = curwin->w_buffer;
9572 }
9573 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 undisplay_dollar();
9575
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009576# ifdef FEAT_INS_EXPAND
9577 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009578 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009579# endif
9580 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009581 if (dir == MSCR_DOWN || dir == MSCR_UP)
9582 {
9583 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9584 scroll_redraw(dir,
9585 (long)(curwin->w_botline - curwin->w_topline));
9586 else
9587 scroll_redraw(dir, 3L);
9588 }
9589#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009590 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009591 {
9592 int val, step = 6;
9593
9594 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009595 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009596 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9597 if (val < 0)
9598 val = 0;
9599 gui_do_horiz_scroll(val, TRUE);
9600 }
9601#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009602# ifdef FEAT_INS_EXPAND
9603 did_scroll = TRUE;
9604# endif
9605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 curwin->w_redr_status = TRUE;
9608
9609 curwin = old_curwin;
9610 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009612# ifdef FEAT_INS_EXPAND
9613 /* The popup menu may overlay the window, need to redraw it.
9614 * TODO: Would be more efficient to only redraw the windows that are
9615 * overlapped by the popup menu. */
9616 if (pum_visible() && did_scroll)
9617 {
9618 redraw_all_later(NOT_VALID);
9619 ins_compl_show_pum();
9620 }
9621# endif
9622
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009623 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
9625 start_arrow(&tpos);
9626# ifdef FEAT_CINDENT
9627 can_cindent = TRUE;
9628# endif
9629 }
9630}
9631#endif
9632
Bram Moolenaarec2da362017-01-21 20:04:22 +01009633/*
9634 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9635 * P_PE literally.
9636 * When "drop" is TRUE then consume the text and drop it.
9637 */
9638 int
9639bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9640{
9641 int c;
9642 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9643 int idx = 0;
9644 char_u *end = find_termcode((char_u *)"PE");
9645 int ret_char = -1;
9646 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009647 int save_paste = p_paste;
9648 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009649
9650 /* If the end code is too long we can't detect it, read everything. */
9651 if (STRLEN(end) >= NUMBUFLEN)
9652 end = NULL;
9653 ++no_mapping;
9654 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009655 p_paste = TRUE;
9656 curbuf->b_p_ai = FALSE;
9657
Bram Moolenaarec2da362017-01-21 20:04:22 +01009658 for (;;)
9659 {
9660 /* When the end is not defined read everything. */
9661 if (end == NULL && vpeekc() == NUL)
9662 break;
9663 c = plain_vgetc();
9664#ifdef FEAT_MBYTE
9665 if (has_mbyte)
9666 idx += (*mb_char2bytes)(c, buf + idx);
9667 else
9668#endif
9669 buf[idx++] = c;
9670 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009671 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009672 {
9673 if (end[idx] == NUL)
9674 break; /* Found the end of paste code. */
9675 continue;
9676 }
9677 if (!drop)
9678 {
9679 switch (mode)
9680 {
9681 case PASTE_CMDLINE:
9682 put_on_cmdline(buf, idx, TRUE);
9683 break;
9684
9685 case PASTE_EX:
9686 if (gap != NULL && ga_grow(gap, idx) == OK)
9687 {
9688 mch_memmove((char *)gap->ga_data + gap->ga_len,
9689 buf, (size_t)idx);
9690 gap->ga_len += idx;
9691 }
9692 break;
9693
9694 case PASTE_INSERT:
9695 if (stop_arrow() == OK)
9696 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009697 c = buf[0];
9698 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9699 ins_eol(c);
9700 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009701 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009702 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009703 AppendToRedobuffLit(buf, idx);
9704 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009705 }
9706 break;
9707
9708 case PASTE_ONE_CHAR:
9709 if (ret_char == -1)
9710 {
9711#ifdef FEAT_MBYTE
9712 if (has_mbyte)
9713 ret_char = (*mb_ptr2char)(buf);
9714 else
9715#endif
9716 ret_char = buf[0];
9717 }
9718 break;
9719 }
9720 }
9721 idx = 0;
9722 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009723
Bram Moolenaarec2da362017-01-21 20:04:22 +01009724 --no_mapping;
9725 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009726 p_paste = save_paste;
9727 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009728
9729 return ret_char;
9730}
9731
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009732#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009734ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009735{
9736 /* We will be leaving the current window, unless closing another tab. */
9737 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9738 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9739 {
9740 undisplay_dollar();
9741 start_arrow(&curwin->w_cursor);
9742# ifdef FEAT_CINDENT
9743 can_cindent = TRUE;
9744# endif
9745 }
9746
9747 if (c == K_TABLINE)
9748 goto_tabpage(current_tab);
9749 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009750 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009751 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009752 redraw_statuslines(); /* will redraw the tabline when needed */
9753 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009754}
9755#endif
9756
9757#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009759ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
9761 pos_T tpos;
9762
9763 undisplay_dollar();
9764 tpos = curwin->w_cursor;
9765 if (gui_do_scroll())
9766 {
9767 start_arrow(&tpos);
9768# ifdef FEAT_CINDENT
9769 can_cindent = TRUE;
9770# endif
9771 }
9772}
9773
9774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009775ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776{
9777 pos_T tpos;
9778
9779 undisplay_dollar();
9780 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009781 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 {
9783 start_arrow(&tpos);
9784# ifdef FEAT_CINDENT
9785 can_cindent = TRUE;
9786# endif
9787 }
9788}
9789#endif
9790
9791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009792ins_left(
9793 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795 pos_T tpos;
9796
9797#ifdef FEAT_FOLDING
9798 if ((fdo_flags & FDO_HOR) && KeyTyped)
9799 foldOpenCursor();
9800#endif
9801 undisplay_dollar();
9802 tpos = curwin->w_cursor;
9803 if (oneleft() == OK)
9804 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009805#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9806 /* Only call start_arrow() when not busy with preediting, it will
9807 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009808 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009809#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009810 {
9811 start_arrow_with_change(&tpos, end_change);
9812 if (!end_change)
9813 AppendCharToRedobuff(K_LEFT);
9814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815#ifdef FEAT_RIGHTLEFT
9816 /* If exit reversed string, position is fixed */
9817 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9818 revins_legal++;
9819 revins_chars++;
9820#endif
9821 }
9822
9823 /*
9824 * if 'whichwrap' set for cursor in insert mode may go to
9825 * previous line
9826 */
9827 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9828 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009829 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 start_arrow(&tpos);
9831 --(curwin->w_cursor.lnum);
9832 coladvance((colnr_T)MAXCOL);
9833 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9834 }
9835 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009836 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009837 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838}
9839
9840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009841ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842{
9843 pos_T tpos;
9844
9845#ifdef FEAT_FOLDING
9846 if ((fdo_flags & FDO_HOR) && KeyTyped)
9847 foldOpenCursor();
9848#endif
9849 undisplay_dollar();
9850 tpos = curwin->w_cursor;
9851 if (c == K_C_HOME)
9852 curwin->w_cursor.lnum = 1;
9853 curwin->w_cursor.col = 0;
9854#ifdef FEAT_VIRTUALEDIT
9855 curwin->w_cursor.coladd = 0;
9856#endif
9857 curwin->w_curswant = 0;
9858 start_arrow(&tpos);
9859}
9860
9861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009862ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863{
9864 pos_T tpos;
9865
9866#ifdef FEAT_FOLDING
9867 if ((fdo_flags & FDO_HOR) && KeyTyped)
9868 foldOpenCursor();
9869#endif
9870 undisplay_dollar();
9871 tpos = curwin->w_cursor;
9872 if (c == K_C_END)
9873 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9874 coladvance((colnr_T)MAXCOL);
9875 curwin->w_curswant = MAXCOL;
9876
9877 start_arrow(&tpos);
9878}
9879
9880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009881ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882{
9883#ifdef FEAT_FOLDING
9884 if ((fdo_flags & FDO_HOR) && KeyTyped)
9885 foldOpenCursor();
9886#endif
9887 undisplay_dollar();
9888 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9889 {
9890 start_arrow(&curwin->w_cursor);
9891 (void)bck_word(1L, FALSE, FALSE);
9892 curwin->w_set_curswant = TRUE;
9893 }
9894 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009895 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896}
9897
9898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009899ins_right(
9900 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901{
9902#ifdef FEAT_FOLDING
9903 if ((fdo_flags & FDO_HOR) && KeyTyped)
9904 foldOpenCursor();
9905#endif
9906 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009907 if (gchar_cursor() != NUL
9908#ifdef FEAT_VIRTUALEDIT
9909 || virtual_active()
9910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 )
9912 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009913 start_arrow_with_change(&curwin->w_cursor, end_change);
9914 if (!end_change)
9915 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 curwin->w_set_curswant = TRUE;
9917#ifdef FEAT_VIRTUALEDIT
9918 if (virtual_active())
9919 oneright();
9920 else
9921#endif
9922 {
9923#ifdef FEAT_MBYTE
9924 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009925 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 else
9927#endif
9928 ++curwin->w_cursor.col;
9929 }
9930
9931#ifdef FEAT_RIGHTLEFT
9932 revins_legal++;
9933 if (revins_chars)
9934 revins_chars--;
9935#endif
9936 }
9937 /* if 'whichwrap' set for cursor in insert mode, may move the
9938 * cursor to the next line */
9939 else if (vim_strchr(p_ww, ']') != NULL
9940 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9941 {
9942 start_arrow(&curwin->w_cursor);
9943 curwin->w_set_curswant = TRUE;
9944 ++curwin->w_cursor.lnum;
9945 curwin->w_cursor.col = 0;
9946 }
9947 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009948 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009949 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950}
9951
9952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009953ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954{
9955#ifdef FEAT_FOLDING
9956 if ((fdo_flags & FDO_HOR) && KeyTyped)
9957 foldOpenCursor();
9958#endif
9959 undisplay_dollar();
9960 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9961 || gchar_cursor() != NUL)
9962 {
9963 start_arrow(&curwin->w_cursor);
9964 (void)fwd_word(1L, FALSE, 0);
9965 curwin->w_set_curswant = TRUE;
9966 }
9967 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009968 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969}
9970
9971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009972ins_up(
9973 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974{
9975 pos_T tpos;
9976 linenr_T old_topline = curwin->w_topline;
9977#ifdef FEAT_DIFF
9978 int old_topfill = curwin->w_topfill;
9979#endif
9980
9981 undisplay_dollar();
9982 tpos = curwin->w_cursor;
9983 if (cursor_up(1L, TRUE) == OK)
9984 {
9985 if (startcol)
9986 coladvance(getvcol_nolist(&Insstart));
9987 if (old_topline != curwin->w_topline
9988#ifdef FEAT_DIFF
9989 || old_topfill != curwin->w_topfill
9990#endif
9991 )
9992 redraw_later(VALID);
9993 start_arrow(&tpos);
9994#ifdef FEAT_CINDENT
9995 can_cindent = TRUE;
9996#endif
9997 }
9998 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009999 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000}
10001
10002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010003ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005 pos_T tpos;
10006
10007 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010008
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010009 if (mod_mask & MOD_MASK_CTRL)
10010 {
10011 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010012 if (first_tabpage->tp_next != NULL)
10013 {
10014 start_arrow(&curwin->w_cursor);
10015 goto_tabpage(-1);
10016 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010017 return;
10018 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010019
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 tpos = curwin->w_cursor;
10021 if (onepage(BACKWARD, 1L) == OK)
10022 {
10023 start_arrow(&tpos);
10024#ifdef FEAT_CINDENT
10025 can_cindent = TRUE;
10026#endif
10027 }
10028 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010029 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030}
10031
10032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010033ins_down(
10034 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035{
10036 pos_T tpos;
10037 linenr_T old_topline = curwin->w_topline;
10038#ifdef FEAT_DIFF
10039 int old_topfill = curwin->w_topfill;
10040#endif
10041
10042 undisplay_dollar();
10043 tpos = curwin->w_cursor;
10044 if (cursor_down(1L, TRUE) == OK)
10045 {
10046 if (startcol)
10047 coladvance(getvcol_nolist(&Insstart));
10048 if (old_topline != curwin->w_topline
10049#ifdef FEAT_DIFF
10050 || old_topfill != curwin->w_topfill
10051#endif
10052 )
10053 redraw_later(VALID);
10054 start_arrow(&tpos);
10055#ifdef FEAT_CINDENT
10056 can_cindent = TRUE;
10057#endif
10058 }
10059 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010060 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061}
10062
10063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010064ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065{
10066 pos_T tpos;
10067
10068 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010069
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010070 if (mod_mask & MOD_MASK_CTRL)
10071 {
10072 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010073 if (first_tabpage->tp_next != NULL)
10074 {
10075 start_arrow(&curwin->w_cursor);
10076 goto_tabpage(0);
10077 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010078 return;
10079 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010080
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 tpos = curwin->w_cursor;
10082 if (onepage(FORWARD, 1L) == OK)
10083 {
10084 start_arrow(&tpos);
10085#ifdef FEAT_CINDENT
10086 can_cindent = TRUE;
10087#endif
10088 }
10089 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010090 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091}
10092
10093#ifdef FEAT_DND
10094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010095ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
10097 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10098}
10099#endif
10100
10101/*
10102 * Handle TAB in Insert or Replace mode.
10103 * Return TRUE when the TAB needs to be inserted like a normal character.
10104 */
10105 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010106ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107{
10108 int ind;
10109 int i;
10110 int temp;
10111
10112 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10113 Insstart_blank_vcol = get_nolist_virtcol();
10114 if (echeck_abbr(TAB + ABBR_OFF))
10115 return FALSE;
10116
10117 ind = inindent(0);
10118#ifdef FEAT_CINDENT
10119 if (ind)
10120 can_cindent = FALSE;
10121#endif
10122
10123 /*
10124 * When nothing special, insert TAB like a normal character
10125 */
10126 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010127 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010128 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 return TRUE;
10130
10131 if (stop_arrow() == FAIL)
10132 return TRUE;
10133
10134 did_ai = FALSE;
10135#ifdef FEAT_SMARTINDENT
10136 did_si = FALSE;
10137 can_si = FALSE;
10138 can_si_back = FALSE;
10139#endif
10140 AppendToRedobuff((char_u *)"\t");
10141
10142 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010143 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010144 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10145 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 else /* otherwise use 'tabstop' */
10147 temp = (int)curbuf->b_p_ts;
10148 temp -= get_nolist_virtcol() % temp;
10149
10150 /*
10151 * Insert the first space with ins_char(). It will delete one char in
10152 * replace mode. Insert the rest with ins_str(); it will not delete any
10153 * chars. For VREPLACE mode, we use ins_char() for all characters.
10154 */
10155 ins_char(' ');
10156 while (--temp > 0)
10157 {
10158#ifdef FEAT_VREPLACE
10159 if (State & VREPLACE_FLAG)
10160 ins_char(' ');
10161 else
10162#endif
10163 {
10164 ins_str((char_u *)" ");
10165 if (State & REPLACE_FLAG) /* no char replaced */
10166 replace_push(NUL);
10167 }
10168 }
10169
10170 /*
10171 * When 'expandtab' not set: Replace spaces by TABs where possible.
10172 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010173 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 {
10175 char_u *ptr;
10176#ifdef FEAT_VREPLACE
10177 char_u *saved_line = NULL; /* init for GCC */
10178 pos_T pos;
10179#endif
10180 pos_T fpos;
10181 pos_T *cursor;
10182 colnr_T want_vcol, vcol;
10183 int change_col = -1;
10184 int save_list = curwin->w_p_list;
10185
10186 /*
10187 * Get the current line. For VREPLACE mode, don't make real changes
10188 * yet, just work on a copy of the line.
10189 */
10190#ifdef FEAT_VREPLACE
10191 if (State & VREPLACE_FLAG)
10192 {
10193 pos = curwin->w_cursor;
10194 cursor = &pos;
10195 saved_line = vim_strsave(ml_get_curline());
10196 if (saved_line == NULL)
10197 return FALSE;
10198 ptr = saved_line + pos.col;
10199 }
10200 else
10201#endif
10202 {
10203 ptr = ml_get_cursor();
10204 cursor = &curwin->w_cursor;
10205 }
10206
10207 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10208 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10209 curwin->w_p_list = FALSE;
10210
10211 /* Find first white before the cursor */
10212 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010213 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 {
10215 --fpos.col;
10216 --ptr;
10217 }
10218
10219 /* In Replace mode, don't change characters before the insert point. */
10220 if ((State & REPLACE_FLAG)
10221 && fpos.lnum == Insstart.lnum
10222 && fpos.col < Insstart.col)
10223 {
10224 ptr += Insstart.col - fpos.col;
10225 fpos.col = Insstart.col;
10226 }
10227
10228 /* compute virtual column numbers of first white and cursor */
10229 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10230 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10231
Bram Moolenaar597a4222014-06-25 14:39:50 +020010232 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10233 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010234 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010236 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 if (vcol + i > want_vcol)
10238 break;
10239 if (*ptr != TAB)
10240 {
10241 *ptr = TAB;
10242 if (change_col < 0)
10243 {
10244 change_col = fpos.col; /* Column of first change */
10245 /* May have to adjust Insstart */
10246 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10247 Insstart.col = fpos.col;
10248 }
10249 }
10250 ++fpos.col;
10251 ++ptr;
10252 vcol += i;
10253 }
10254
10255 if (change_col >= 0)
10256 {
10257 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010258 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259
10260 /* Skip over the spaces we need. */
10261 while (vcol < want_vcol && *ptr == ' ')
10262 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010263 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 ++ptr;
10265 ++repl_off;
10266 }
10267 if (vcol > want_vcol)
10268 {
10269 /* Must have a char with 'showbreak' just before it. */
10270 --ptr;
10271 --repl_off;
10272 }
10273 fpos.col += repl_off;
10274
10275 /* Delete following spaces. */
10276 i = cursor->col - fpos.col;
10277 if (i > 0)
10278 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010279 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 /* correct replace stack. */
10281 if ((State & REPLACE_FLAG)
10282#ifdef FEAT_VREPLACE
10283 && !(State & VREPLACE_FLAG)
10284#endif
10285 )
10286 for (temp = i; --temp >= 0; )
10287 replace_join(repl_off);
10288 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010289#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010290 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010291 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010292 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010293 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10294 (char_u *)"\t", 1);
10295 }
10296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 cursor->col -= i;
10298
10299#ifdef FEAT_VREPLACE
10300 /*
10301 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10302 * backspacing over the changed spacing and then inserting the new
10303 * spacing.
10304 */
10305 if (State & VREPLACE_FLAG)
10306 {
10307 /* Backspace from real cursor to change_col */
10308 backspace_until_column(change_col);
10309
10310 /* Insert each char in saved_line from changed_col to
10311 * ptr-cursor */
10312 ins_bytes_len(saved_line + change_col,
10313 cursor->col - change_col);
10314 }
10315#endif
10316 }
10317
10318#ifdef FEAT_VREPLACE
10319 if (State & VREPLACE_FLAG)
10320 vim_free(saved_line);
10321#endif
10322 curwin->w_p_list = save_list;
10323 }
10324
10325 return FALSE;
10326}
10327
10328/*
10329 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010330 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 */
10332 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010333ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334{
10335 int i;
10336
10337 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010338 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 undisplay_dollar();
10342
10343 /*
10344 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10345 * character under the cursor. Only push a NUL on the replace stack,
10346 * nothing to put back when the NL is deleted.
10347 */
10348 if ((State & REPLACE_FLAG)
10349#ifdef FEAT_VREPLACE
10350 && !(State & VREPLACE_FLAG)
10351#endif
10352 )
10353 replace_push(NUL);
10354
10355 /*
10356 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10357 * replacing the next line, so we push all of the characters left on the
10358 * line onto the replace stack. This is not done here though, it is done
10359 * in open_line().
10360 */
10361
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010362#ifdef FEAT_VIRTUALEDIT
10363 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10364 * CTRL-O). */
10365 if (virtual_active() && curwin->w_cursor.coladd > 0)
10366 coladvance(getviscol());
10367#endif
10368
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369#ifdef FEAT_RIGHTLEFT
10370# ifdef FEAT_FKMAP
10371 if (p_altkeymap && p_fkmap)
10372 fkmap(NL);
10373# endif
10374 /* NL in reverse insert will always start in the end of
10375 * current line. */
10376 if (revins_on)
10377 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10378#endif
10379
10380 AppendToRedobuff(NL_STR);
10381 i = open_line(FORWARD,
10382#ifdef FEAT_COMMENTS
10383 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10384#endif
10385 0, old_indent);
10386 old_indent = 0;
10387#ifdef FEAT_CINDENT
10388 can_cindent = TRUE;
10389#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010390#ifdef FEAT_FOLDING
10391 /* When inserting a line the cursor line must never be in a closed fold. */
10392 foldOpenCursor();
10393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010395 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396}
10397
10398#ifdef FEAT_DIGRAPHS
10399/*
10400 * Handle digraph in insert mode.
10401 * Returns character still to be inserted, or NUL when nothing remaining to be
10402 * done.
10403 */
10404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010405ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
10407 int c;
10408 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010409 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410
10411 pc_status = PC_STATUS_UNSET;
10412 if (redrawing() && !char_avail())
10413 {
10414 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010415 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416
10417 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010418 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419#ifdef FEAT_CMDL_INFO
10420 add_to_showcmd_c(Ctrl_K);
10421#endif
10422 }
10423
10424#ifdef USE_ON_FLY_SCROLL
10425 dont_scroll = TRUE; /* disallow scrolling here */
10426#endif
10427
10428 /* don't map the digraph chars. This also prevents the
10429 * mode message to be deleted when ESC is hit */
10430 ++no_mapping;
10431 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010432 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 --no_mapping;
10434 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010435 if (did_putchar)
10436 /* when the line fits in 'columns' the '?' is at the start of the next
10437 * line and will not be removed by the redraw */
10438 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010439
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 if (IS_SPECIAL(c) || mod_mask) /* special key */
10441 {
10442#ifdef FEAT_CMDL_INFO
10443 clear_showcmd();
10444#endif
10445 insert_special(c, TRUE, FALSE);
10446 return NUL;
10447 }
10448 if (c != ESC)
10449 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010450 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 if (redrawing() && !char_avail())
10452 {
10453 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010454 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455
10456 if (char2cells(c) == 1)
10457 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010458 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010460 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 }
10462#ifdef FEAT_CMDL_INFO
10463 add_to_showcmd_c(c);
10464#endif
10465 }
10466 ++no_mapping;
10467 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010468 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 --no_mapping;
10470 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010471 if (did_putchar)
10472 /* when the line fits in 'columns' the '?' is at the start of the
10473 * next line and will not be removed by a redraw */
10474 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 if (cc != ESC)
10476 {
10477 AppendToRedobuff((char_u *)CTRL_V_STR);
10478 c = getdigraph(c, cc, TRUE);
10479#ifdef FEAT_CMDL_INFO
10480 clear_showcmd();
10481#endif
10482 return c;
10483 }
10484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485#ifdef FEAT_CMDL_INFO
10486 clear_showcmd();
10487#endif
10488 return NUL;
10489}
10490#endif
10491
10492/*
10493 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10494 * Returns the char to be inserted, or NUL if none found.
10495 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010497ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498{
10499 int c;
10500 int temp;
10501 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010502 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503
10504 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10505 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010506 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 return NUL;
10508 }
10509
10510 /* try to advance to the cursor column */
10511 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010512 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 prev_ptr = ptr;
10514 validate_virtcol();
10515 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10516 {
10517 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010518 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 }
10520 if ((colnr_T)temp > curwin->w_virtcol)
10521 ptr = prev_ptr;
10522
10523#ifdef FEAT_MBYTE
10524 c = (*mb_ptr2char)(ptr);
10525#else
10526 c = *ptr;
10527#endif
10528 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010529 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 return c;
10531}
10532
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010533/*
10534 * CTRL-Y or CTRL-E typed in Insert mode.
10535 */
10536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010537ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010538{
10539 int c = tc;
10540
10541#ifdef FEAT_INS_EXPAND
10542 if (ctrl_x_mode == CTRL_X_SCROLL)
10543 {
10544 if (c == Ctrl_Y)
10545 scrolldown_clamp();
10546 else
10547 scrollup_clamp();
10548 redraw_later(VALID);
10549 }
10550 else
10551#endif
10552 {
10553 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10554 if (c != NUL)
10555 {
10556 long tw_save;
10557
10558 /* The character must be taken literally, insert like it
10559 * was typed after a CTRL-V, and pretend 'textwidth'
10560 * wasn't set. Digits, 'o' and 'x' are special after a
10561 * CTRL-V, don't use it for these. */
10562 if (c < 256 && !isalnum(c))
10563 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10564 tw_save = curbuf->b_p_tw;
10565 curbuf->b_p_tw = -1;
10566 insert_special(c, TRUE, FALSE);
10567 curbuf->b_p_tw = tw_save;
10568#ifdef FEAT_RIGHTLEFT
10569 revins_chars++;
10570 revins_legal++;
10571#endif
10572 c = Ctrl_V; /* pretend CTRL-V is last character */
10573 auto_format(FALSE, TRUE);
10574 }
10575 }
10576 return c;
10577}
10578
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579#ifdef FEAT_SMARTINDENT
10580/*
10581 * Try to do some very smart auto-indenting.
10582 * Used when inserting a "normal" character.
10583 */
10584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010585ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586{
10587 pos_T *pos, old_pos;
10588 char_u *ptr;
10589 int i;
10590 int temp;
10591
10592 /*
10593 * do some very smart indenting when entering '{' or '}'
10594 */
10595 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10596 {
10597 /*
10598 * for '}' set indent equal to indent of line containing matching '{'
10599 */
10600 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10601 {
10602 old_pos = curwin->w_cursor;
10603 /*
10604 * If the matching '{' has a ')' immediately before it (ignoring
10605 * white-space), then line up with the start of the line
10606 * containing the matching '(' if there is one. This handles the
10607 * case where an "if (..\n..) {" statement continues over multiple
10608 * lines -- webb
10609 */
10610 ptr = ml_get(pos->lnum);
10611 i = pos->col;
10612 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010613 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 ;
10615 curwin->w_cursor.lnum = pos->lnum;
10616 curwin->w_cursor.col = i;
10617 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10618 curwin->w_cursor = *pos;
10619 i = get_indent();
10620 curwin->w_cursor = old_pos;
10621#ifdef FEAT_VREPLACE
10622 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010623 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 else
10625#endif
10626 (void)set_indent(i, SIN_CHANGED);
10627 }
10628 else if (curwin->w_cursor.col > 0)
10629 {
10630 /*
10631 * when inserting '{' after "O" reduce indent, but not
10632 * more than indent of previous line
10633 */
10634 temp = TRUE;
10635 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10636 {
10637 old_pos = curwin->w_cursor;
10638 i = get_indent();
10639 while (curwin->w_cursor.lnum > 1)
10640 {
10641 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10642
10643 /* ignore empty lines and lines starting with '#'. */
10644 if (*ptr != '#' && *ptr != NUL)
10645 break;
10646 }
10647 if (get_indent() >= i)
10648 temp = FALSE;
10649 curwin->w_cursor = old_pos;
10650 }
10651 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010652 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 }
10654 }
10655
10656 /*
10657 * set indent of '#' always to 0
10658 */
10659 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10660 {
10661 /* remember current indent for next line */
10662 old_indent = get_indent();
10663 (void)set_indent(0, SIN_CHANGED);
10664 }
10665
10666 /* Adjust ai_col, the char at this position can be deleted. */
10667 if (ai_col > curwin->w_cursor.col)
10668 ai_col = curwin->w_cursor.col;
10669}
10670#endif
10671
10672/*
10673 * Get the value that w_virtcol would have when 'list' is off.
10674 * Unless 'cpo' contains the 'L' flag.
10675 */
10676 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010677get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
10679 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10680 return getvcol_nolist(&curwin->w_cursor);
10681 validate_virtcol();
10682 return curwin->w_virtcol;
10683}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010684
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010685#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010686/*
10687 * Handle the InsertCharPre autocommand.
10688 * "c" is the character that was typed.
10689 * Return a pointer to allocated memory with the replacement string.
10690 * Return NULL to continue inserting "c".
10691 */
10692 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010693do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010694{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010695 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010696 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010697
10698 /* Return quickly when there is nothing to do. */
10699 if (!has_insertcharpre())
10700 return NULL;
10701
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010702# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010703 if (has_mbyte)
10704 buf[(*mb_char2bytes)(c, buf)] = NUL;
10705 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010706# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010707 {
10708 buf[0] = c;
10709 buf[1] = NUL;
10710 }
10711
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010712 /* Lock the text to avoid weird things from happening. */
10713 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010714 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010715
Bram Moolenaar704984a2012-06-01 14:57:51 +020010716 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010717 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010718 {
10719 /* Get the value of v:char. It may be empty or more than one
10720 * character. Only use it when changed, otherwise continue with the
10721 * original character to avoid breaking autoindent. */
10722 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10723 res = vim_strsave(get_vim_var_str(VV_CHAR));
10724 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010725
10726 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10727 --textlock;
10728
10729 return res;
10730}
10731#endif