blob: 6a636b9e1282836a55aa9f0ba96318a69b3e06c4 [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
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001019#ifdef FEAT_JOB_CHANNEL
1020 if (c == Ctrl_C && bt_prompt(curbuf))
1021 {
1022 if (invoke_prompt_interrupt())
1023 {
1024 if (!bt_prompt(curbuf))
1025 // buffer changed to a non-prompt buffer, get out of
1026 // Insert mode
1027 goto doESCkey;
1028 break;
1029 }
1030 }
1031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033#ifdef UNIX
1034do_intr:
1035#endif
1036 /* when 'insertmode' set, and not halfway a mapping, don't leave
1037 * Insert mode */
1038 if (goto_im())
1039 {
1040 if (got_int)
1041 {
1042 (void)vgetc(); /* flush all buffers */
1043 got_int = FALSE;
1044 }
1045 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001046 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 break;
1048 }
1049doESCkey:
1050 /*
1051 * This is the ONLY return from edit()!
1052 */
1053 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1054 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001055 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 o_lnum = curwin->w_cursor.lnum;
1057
Bram Moolenaar488c6512005-08-11 20:09:58 +00001058 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001059 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00001060 if (cmdchar != 'r' && cmdchar != 'v')
1061 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1062 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001063 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 continue;
1067
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068 case Ctrl_Z: /* suspend when 'insertmode' set */
1069 if (!p_im)
1070 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001071 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001072#ifdef CURSOR_SHAPE
1073 ui_cursor_shape(); /* may need to update cursor shape */
1074#endif
1075 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001076
1077 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001078#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001079 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 goto docomplete;
1081#endif
1082 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1083 break;
1084 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001085
1086#ifdef FEAT_VIRTUALEDIT
1087 /* don't move the cursor left when 'virtualedit' has "onemore". */
1088 if (ve_flags & VE_ONEMORE)
1089 {
1090 ins_at_eol = FALSE;
1091 nomove = TRUE;
1092 }
1093#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 count = 0;
1095 goto doESCkey;
1096
Bram Moolenaar572cb562005-08-05 21:35:02 +00001097 case K_INS: /* toggle insert/replace mode */
1098 case K_KINS:
1099 ins_insert(replaceState);
1100 break;
1101
1102 case K_SELECT: /* end of Select mode mapping - ignore */
1103 break;
1104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001105 case K_HELP: /* Help key works like <ESC> <Help> */
1106 case K_F1:
1107 case K_XF1:
1108 stuffcharReadbuff(K_HELP);
1109 if (p_im)
1110 need_start_insertmode = TRUE;
1111 goto doESCkey;
1112
1113#ifdef FEAT_NETBEANS_INTG
1114 case K_F21: /* NetBeans command */
1115 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001116 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 --no_mapping;
1118 netbeans_keycommand(i);
1119 break;
1120#endif
1121
1122 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 case NUL:
1124 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001125 /* For ^@ the trailing ESC will end the insert, unless there is an
1126 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1128 && c != Ctrl_A && !p_im)
1129 goto doESCkey; /* quit insert mode */
1130 inserted_space = FALSE;
1131 break;
1132
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 ins_reg();
1135 auto_format(FALSE, TRUE);
1136 inserted_space = FALSE;
1137 break;
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 ins_ctrl_g();
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case Ctrl_HAT: /* switch input mode and/or langmap */
1144 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 break;
1146
1147#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (!p_ari)
1150 goto normalchar;
1151 ins_ctrl_();
1152 break;
1153#endif
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1157 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1158 goto docomplete;
1159#endif
1160 /* FALLTHROUGH */
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163# ifdef FEAT_INS_EXPAND
1164 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1165 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001166 if (has_compl_option(FALSE))
1167 goto docomplete;
1168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
1170# endif
1171 ins_shift(c, lastc);
1172 auto_format(FALSE, TRUE);
1173 inserted_space = FALSE;
1174 break;
1175
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001176 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 case K_KDEL:
1178 ins_del();
1179 auto_format(FALSE, TRUE);
1180 break;
1181
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001182 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 case Ctrl_H:
1184 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1185 auto_format(FALSE, TRUE);
1186 break;
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001189#ifdef FEAT_JOB_CHANNEL
1190 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1191 {
1192 // In a prompt window CTRL-W is used for window commands.
1193 // Use Shift-CTRL-W to delete a word.
1194 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001195 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001196 nomove = TRUE;
1197 count = 0;
1198 goto doESCkey;
1199 }
1200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1202 auto_format(FALSE, TRUE);
1203 break;
1204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001206# ifdef FEAT_COMPL_FUNC
1207 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001208 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001209 goto docomplete;
1210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1212 auto_format(FALSE, TRUE);
1213 inserted_space = FALSE;
1214 break;
1215
1216#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 case K_LEFTMOUSE_NM:
1219 case K_LEFTDRAG:
1220 case K_LEFTRELEASE:
1221 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001222 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 case K_MIDDLEMOUSE:
1224 case K_MIDDLEDRAG:
1225 case K_MIDDLERELEASE:
1226 case K_RIGHTMOUSE:
1227 case K_RIGHTDRAG:
1228 case K_RIGHTRELEASE:
1229 case K_X1MOUSE:
1230 case K_X1DRAG:
1231 case K_X1RELEASE:
1232 case K_X2MOUSE:
1233 case K_X2DRAG:
1234 case K_X2RELEASE:
1235 ins_mouse(c);
1236 break;
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001239 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 break;
1241
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001242 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001243 ins_mousescroll(MSCR_UP);
1244 break;
1245
1246 case K_MOUSELEFT: /* Scroll wheel left */
1247 ins_mousescroll(MSCR_LEFT);
1248 break;
1249
1250 case K_MOUSERIGHT: /* Scroll wheel right */
1251 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 break;
1253#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001254 case K_PS:
1255 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1256 if (cmdchar == K_PS)
1257 /* invoked from normal mode, bail out */
1258 goto doESCkey;
1259 break;
1260 case K_PE:
1261 /* Got K_PE without K_PS, ignore. */
1262 break;
1263
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001264#ifdef FEAT_GUI_TABLINE
1265 case K_TABLINE:
1266 case K_TABMENU:
1267 ins_tabline(c);
1268 break;
1269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001271 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 break;
1273
Bram Moolenaar754b5602006-02-09 23:53:20 +00001274 case K_CURSORHOLD: /* Didn't type something for a while. */
1275 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1276 did_cursorhold = TRUE;
1277 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001278
Bram Moolenaar4770d092006-01-12 23:22:24 +00001279#ifdef FEAT_GUI_W32
1280 /* On Win32 ignore <M-F4>, we get it when closing the window was
1281 * cancelled. */
1282 case K_F4:
1283 if (mod_mask != MOD_MASK_ALT)
1284 goto normalchar;
1285 break;
1286#endif
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#ifdef FEAT_GUI
1289 case K_VER_SCROLLBAR:
1290 ins_scroll();
1291 break;
1292
1293 case K_HOR_SCROLLBAR:
1294 ins_horscroll();
1295 break;
1296#endif
1297
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001298 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 case K_S_HOME:
1301 case K_C_HOME:
1302 ins_home(c);
1303 break;
1304
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001305 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 case K_S_END:
1308 case K_C_END:
1309 ins_end(c);
1310 break;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001313 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1314 ins_s_left();
1315 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001316 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 break;
1318
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001319 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 case K_C_LEFT:
1321 ins_s_left();
1322 break;
1323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001325 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1326 ins_s_right();
1327 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001328 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 break;
1330
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001331 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 case K_C_RIGHT:
1333 ins_s_right();
1334 break;
1335
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001336 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001337#ifdef FEAT_INS_EXPAND
1338 if (pum_visible())
1339 goto docomplete;
1340#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001341 if (mod_mask & MOD_MASK_SHIFT)
1342 ins_pageup();
1343 else
1344 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 break;
1346
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001347 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 case K_PAGEUP:
1349 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001350#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001351 if (pum_visible())
1352 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 ins_pageup();
1355 break;
1356
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001357 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001358#ifdef FEAT_INS_EXPAND
1359 if (pum_visible())
1360 goto docomplete;
1361#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001362 if (mod_mask & MOD_MASK_SHIFT)
1363 ins_pagedown();
1364 else
1365 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 break;
1367
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001368 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 case K_PAGEDOWN:
1370 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001371#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001372 if (pum_visible())
1373 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 ins_pagedown();
1376 break;
1377
1378#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001379 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 ins_drop();
1381 break;
1382#endif
1383
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001384 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 c = TAB;
1386 /* FALLTHROUGH */
1387
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001388 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1390 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1391 goto docomplete;
1392#endif
1393 inserted_space = FALSE;
1394 if (ins_tab())
1395 goto normalchar; /* insert TAB as a normal char */
1396 auto_format(FALSE, TRUE);
1397 break;
1398
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001399 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 c = CAR;
1401 /* FALLTHROUGH */
1402 case CAR:
1403 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001404#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 /* In a quickfix window a <CR> jumps to the error under the
1406 * cursor. */
1407 if (bt_quickfix(curbuf) && c == CAR)
1408 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001409 if (curwin->w_llist_ref == NULL) /* quickfix window */
1410 do_cmdline_cmd((char_u *)".cc");
1411 else /* location list window */
1412 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 break;
1414 }
1415#endif
1416#ifdef FEAT_CMDWIN
1417 if (cmdwin_type != 0)
1418 {
1419 /* Execute the command in the cmdline window. */
1420 cmdwin_result = CAR;
1421 goto doESCkey;
1422 }
1423#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001424#ifdef FEAT_JOB_CHANNEL
1425 if (bt_prompt(curbuf))
1426 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001427 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001428 if (!bt_prompt(curbuf))
1429 // buffer changed to a non-prompt buffer, get out of
1430 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001431 goto doESCkey;
1432 break;
1433 }
1434#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001435 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 goto doESCkey; /* out of memory */
1437 auto_format(FALSE, FALSE);
1438 inserted_space = FALSE;
1439 break;
1440
Bram Moolenaar860cae12010-06-05 23:22:07 +02001441#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001442 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443# ifdef FEAT_INS_EXPAND
1444 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1445 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001446 if (has_compl_option(TRUE))
1447 goto docomplete;
1448 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450# endif
1451# ifdef FEAT_DIGRAPHS
1452 c = ins_digraph();
1453 if (c == NUL)
1454 break;
1455# endif
1456 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
1459#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001460 case Ctrl_X: /* Enter CTRL-X mode */
1461 ins_ctrl_x();
1462 break;
1463
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001464 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 if (ctrl_x_mode != CTRL_X_TAGS)
1466 goto normalchar;
1467 goto docomplete;
1468
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001469 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (ctrl_x_mode != CTRL_X_FILES)
1471 goto normalchar;
1472 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001473
1474 case 's': /* Spelling completion after ^X */
1475 case Ctrl_S:
1476 if (ctrl_x_mode != CTRL_X_SPELL)
1477 goto normalchar;
1478 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479#endif
1480
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001481 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#ifdef FEAT_INS_EXPAND
1483 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1484#endif
1485 {
1486 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1487 if (p_im)
1488 {
1489 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1490 break;
1491 goto doESCkey;
1492 }
1493 goto normalchar;
1494 }
1495#ifdef FEAT_INS_EXPAND
1496 /* FALLTHROUGH */
1497
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001498 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 case Ctrl_N:
1500 /* if 'complete' is empty then plain ^P is no longer special,
1501 * but it is under other ^X modes */
1502 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001503 && (ctrl_x_mode == CTRL_X_NORMAL
1504 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001505 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 goto normalchar;
1507
1508docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001509 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001510#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001511 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001512#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001513 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001514 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001515#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001516 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001517#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001518 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 break;
1520#endif /* FEAT_INS_EXPAND */
1521
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001522 case Ctrl_Y: /* copy from previous line or scroll down */
1523 case Ctrl_E: /* copy from next line or scroll up */
1524 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 break;
1526
1527 default:
1528#ifdef UNIX
1529 if (c == intr_char) /* special interrupt char */
1530 goto do_intr;
1531#endif
1532
Bram Moolenaare659c952011-05-19 17:25:41 +02001533normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001535 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001537#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001538 if (!p_paste)
1539 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001540 /* Trigger InsertCharPre. */
1541 char_u *str = do_insert_char_pre(c);
1542 char_u *p;
1543
1544 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001545 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001546 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001547 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001548 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001549 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001550 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001551 c = PTR2CHAR(p);
1552 if (c == CAR || c == K_KENTER || c == NL)
1553 ins_eol(c);
1554 else
1555 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001556 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001557 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001558 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001559 vim_free(str);
1560 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001561 }
1562
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001563 /* If the new value is already inserted or an empty string
1564 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001565 if (c == NUL)
1566 break;
1567 }
1568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#ifdef FEAT_SMARTINDENT
1570 /* Try to perform smart-indenting. */
1571 ins_try_si(c);
1572#endif
1573
1574 if (c == ' ')
1575 {
1576 inserted_space = TRUE;
1577#ifdef FEAT_CINDENT
1578 if (inindent(0))
1579 can_cindent = FALSE;
1580#endif
1581 if (Insstart_blank_vcol == MAXCOL
1582 && curwin->w_cursor.lnum == Insstart.lnum)
1583 Insstart_blank_vcol = get_nolist_virtcol();
1584 }
1585
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001586 /* Insert a normal character and check for abbreviations on a
1587 * special character. Let CTRL-] expand abbreviations without
1588 * inserting it. */
1589 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590#ifdef FEAT_MBYTE
1591 /* Add ABBR_OFF for characters above 0x100, this is
1592 * what check_abbr() expects. */
1593 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1594#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001595 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 insert_special(c, FALSE, FALSE);
1598#ifdef FEAT_RIGHTLEFT
1599 revins_legal++;
1600 revins_chars++;
1601#endif
1602 }
1603
1604 auto_format(FALSE, TRUE);
1605
1606#ifdef FEAT_FOLDING
1607 /* When inserting a character the cursor line must never be in a
1608 * closed fold. */
1609 foldOpenCursor();
1610#endif
1611 break;
1612 } /* end of switch (c) */
1613
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001614 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001615 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001616#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001617 /* but not in CTRL-X mode, a script can't restore the state */
1618 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001619#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001620 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001621 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 /* If the cursor was moved we didn't just insert a space */
1624 if (arrow_used)
1625 inserted_space = FALSE;
1626
1627#ifdef FEAT_CINDENT
1628 if (can_cindent && cindent_on()
1629# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001630 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631# endif
1632 )
1633 {
1634force_cindent:
1635 /*
1636 * Indent now if a key was typed that is in 'cinkeys'.
1637 */
1638 if (in_cinkeys(c, ' ', line_is_white))
1639 {
1640 if (stop_arrow() == OK)
1641 /* re-indent the current line */
1642 do_c_expr_indent();
1643 }
1644 }
1645#endif /* FEAT_CINDENT */
1646
1647 } /* for (;;) */
1648 /* NOTREACHED */
1649}
1650
1651/*
1652 * Redraw for Insert mode.
1653 * This is postponed until getting the next character to make '$' in the 'cpo'
1654 * option work correctly.
1655 * Only redraw when there are no characters available. This speeds up
1656 * inserting sequences of characters (e.g., for CTRL-R).
1657 */
1658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659ins_redraw(
1660 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001662#ifdef FEAT_CONCEAL
1663 linenr_T conceal_old_cursor_line = 0;
1664 linenr_T conceal_new_cursor_line = 0;
1665 int conceal_update_lines = FALSE;
1666#endif
1667
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001668 if (char_avail())
1669 return;
1670
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001671#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001672 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1673 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674 if (ready && (has_cursormovedI()
1675# if defined(FEAT_CONCEAL)
1676 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001677# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001678 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001679 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001680# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001681 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001682# endif
1683 )
1684 {
1685# ifdef FEAT_SYN_HL
1686 /* Need to update the screen first, to make sure syntax
1687 * highlighting is correct after making a change (e.g., inserting
1688 * a "(". The autocommand may also require a redraw, so it's done
1689 * again below, unfortunately. */
1690 if (syntax_present(curwin) && must_redraw)
1691 update_screen(0);
1692# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001693 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001694 {
1695 /* Make sure curswant is correct, an autocommand may call
1696 * getcurpos(). */
1697 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001698 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001699 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001700# ifdef FEAT_CONCEAL
1701 if (curwin->w_p_cole > 0)
1702 {
1703 conceal_old_cursor_line = last_cursormoved.lnum;
1704 conceal_new_cursor_line = curwin->w_cursor.lnum;
1705 conceal_update_lines = TRUE;
1706 }
1707# endif
1708 last_cursormoved = curwin->w_cursor;
1709 }
1710#endif
1711
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001712 /* Trigger TextChangedI if b_changedtick differs. */
1713 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001714 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001715#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001716 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001717#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001718 )
1719 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001720 aco_save_T aco;
1721
1722 // save and restore curwin and curbuf, in case the autocmd changes them
1723 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001724 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001725 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001726 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001728
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001729#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001730 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1731 * TextChangedI will need to trigger for backwards compatibility, thus use
1732 * different b_last_changedtick* variables. */
1733 if (ready && has_textchangedP()
1734 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1735 && pum_visible())
1736 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001737 aco_save_T aco;
1738
1739 // save and restore curwin and curbuf, in case the autocmd changes them
1740 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001741 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001742 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001743 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1744 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001745#endif
1746
1747 if (must_redraw)
1748 update_screen(0);
1749 else if (clear_cmdline || redraw_cmdline)
1750 showmode(); /* clear cmdline and show mode */
1751# if defined(FEAT_CONCEAL)
1752 if ((conceal_update_lines
1753 && (conceal_old_cursor_line != conceal_new_cursor_line
1754 || conceal_cursor_line(curwin)))
1755 || need_cursor_line_redraw)
1756 {
1757 if (conceal_old_cursor_line != conceal_new_cursor_line)
1758 update_single_line(curwin, conceal_old_cursor_line);
1759 update_single_line(curwin, conceal_new_cursor_line == 0
1760 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1761 curwin->w_valid &= ~VALID_CROW;
1762 }
1763# endif
1764 showruler(FALSE);
1765 setcursor();
1766 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767}
1768
1769/*
1770 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1771 */
1772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001773ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001776 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001779 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001784 did_putchar = TRUE;
1785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1787
1788#ifdef FEAT_CMDL_INFO
1789 add_to_showcmd_c(Ctrl_V);
1790#endif
1791
1792 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001793 if (did_putchar)
1794 /* when the line fits in 'columns' the '^' is at the start of the next
1795 * line and will not removed by the redraw */
1796 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797#ifdef FEAT_CMDL_INFO
1798 clear_showcmd();
1799#endif
1800 insert_special(c, FALSE, TRUE);
1801#ifdef FEAT_RIGHTLEFT
1802 revins_chars++;
1803 revins_legal++;
1804#endif
1805}
1806
1807/*
1808 * Put a character directly onto the screen. It's not stored in a buffer.
1809 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1810 */
1811static int pc_status;
1812#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1813#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1814#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1815#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817static int pc_attr;
1818static int pc_row;
1819static int pc_col;
1820
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 int attr;
1825
1826 if (ScreenLines != NULL)
1827 {
1828 update_topline(); /* just in case w_topline isn't valid */
1829 validate_cursor();
1830 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001831 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 else
1833 attr = 0;
1834 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001835 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1837 pc_status = PC_STATUS_UNSET;
1838#endif
1839#ifdef FEAT_RIGHTLEFT
1840 if (curwin->w_p_rl)
1841 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001842 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843# ifdef FEAT_MBYTE
1844 if (has_mbyte)
1845 {
1846 int fix_col = mb_fix_col(pc_col, pc_row);
1847
1848 if (fix_col != pc_col)
1849 {
1850 screen_putchar(' ', pc_row, fix_col, attr);
1851 --curwin->w_wcol;
1852 pc_status = PC_STATUS_RIGHT;
1853 }
1854 }
1855# endif
1856 }
1857 else
1858#endif
1859 {
1860 pc_col += curwin->w_wcol;
1861#ifdef FEAT_MBYTE
1862 if (mb_lefthalve(pc_row, pc_col))
1863 pc_status = PC_STATUS_LEFT;
1864#endif
1865 }
1866
1867 /* save the character to be able to put it back */
1868#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1869 if (pc_status == PC_STATUS_UNSET)
1870#endif
1871 {
1872 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1873 pc_status = PC_STATUS_SET;
1874 }
1875 screen_putchar(c, pc_row, pc_col, attr);
1876 }
1877}
1878
Bram Moolenaarf2732452018-06-03 14:47:35 +02001879#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1880/*
1881 * Return the effective prompt for the current buffer.
1882 */
1883 char_u *
1884prompt_text(void)
1885{
1886 if (curbuf->b_prompt_text == NULL)
1887 return (char_u *)"% ";
1888 return curbuf->b_prompt_text;
1889}
1890
1891/*
1892 * Prepare for prompt mode: Make sure the last line has the prompt text.
1893 * Move the cursor to this line.
1894 */
1895 static void
1896init_prompt(int cmdchar_todo)
1897{
1898 char_u *prompt = prompt_text();
1899 char_u *text;
1900
1901 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1902 text = ml_get_curline();
1903 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1904 {
1905 // prompt is missing, insert it or append a line with it
1906 if (*text == NUL)
1907 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1908 else
1909 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1910 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1911 coladvance((colnr_T)MAXCOL);
1912 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1913 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001914
1915 // Insert always starts after the prompt, allow editing text after it.
1916 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1917 || Insstart_orig.col != (int)STRLEN(prompt))
1918 {
1919 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001920 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001921 Insstart_orig = Insstart;
1922 Insstart_textlen = Insstart.col;
1923 Insstart_blank_vcol = MAXCOL;
1924 arrow_used = FALSE;
1925 }
1926
Bram Moolenaarf2732452018-06-03 14:47:35 +02001927 if (cmdchar_todo == 'A')
1928 coladvance((colnr_T)MAXCOL);
1929 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001930 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001931 /* Make sure the cursor is in a valid position. */
1932 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001933}
1934
1935/*
1936 * Return TRUE if the cursor is in the editable position of the prompt line.
1937 */
1938 int
1939prompt_curpos_editable()
1940{
1941 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1942 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1943}
1944#endif
1945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946/*
1947 * Undo the previous edit_putchar().
1948 */
1949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001950edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
1952 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1953 {
1954#if defined(FEAT_MBYTE)
1955 if (pc_status == PC_STATUS_RIGHT)
1956 ++curwin->w_wcol;
1957 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1958 redrawWinline(curwin->w_cursor.lnum, FALSE);
1959 else
1960#endif
1961 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1962 }
1963}
1964
1965/*
1966 * Called when p_dollar is set: display a '$' at the end of the changed text
1967 * Only works when cursor is in the line that changes.
1968 */
1969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001970display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972 colnr_T save_col;
1973
1974 if (!redrawing())
1975 return;
1976
1977 cursor_off();
1978 save_col = curwin->w_cursor.col;
1979 curwin->w_cursor.col = col;
1980#ifdef FEAT_MBYTE
1981 if (has_mbyte)
1982 {
1983 char_u *p;
1984
1985 /* If on the last byte of a multi-byte move to the first byte. */
1986 p = ml_get_curline();
1987 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1988 }
1989#endif
1990 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001991 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 edit_putchar('$', FALSE);
1994 dollar_vcol = curwin->w_virtcol;
1995 }
1996 curwin->w_cursor.col = save_col;
1997}
1998
1999/*
2000 * Call this function before moving the cursor from the normal insert position
2001 * in insert mode.
2002 */
2003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002004undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002006 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002008 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 redrawWinline(curwin->w_cursor.lnum, FALSE);
2010 }
2011}
2012
2013/*
2014 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2015 * Keep the cursor on the same character.
2016 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2017 * type == INDENT_DEC decrease indent (for CTRL-D)
2018 * type == INDENT_SET set indent to "amount"
2019 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2020 */
2021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002022change_indent(
2023 int type,
2024 int amount,
2025 int round,
2026 int replaced, /* replaced character, put on replace stack */
2027 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 int vcol;
2030 int last_vcol;
2031 int insstart_less; /* reduction for Insstart.col */
2032 int new_cursor_col;
2033 int i;
2034 char_u *ptr;
2035 int save_p_list;
2036 int start_col;
2037 colnr_T vc;
2038#ifdef FEAT_VREPLACE
2039 colnr_T orig_col = 0; /* init for GCC */
2040 char_u *new_line, *orig_line = NULL; /* init for GCC */
2041
2042 /* VREPLACE mode needs to know what the line was like before changing */
2043 if (State & VREPLACE_FLAG)
2044 {
2045 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2046 orig_col = curwin->w_cursor.col;
2047 }
2048#endif
2049
2050 /* for the following tricks we don't want list mode */
2051 save_p_list = curwin->w_p_list;
2052 curwin->w_p_list = FALSE;
2053 vc = getvcol_nolist(&curwin->w_cursor);
2054 vcol = vc;
2055
2056 /*
2057 * For Replace mode we need to fix the replace stack later, which is only
2058 * possible when the cursor is in the indent. Remember the number of
2059 * characters before the cursor if it's possible.
2060 */
2061 start_col = curwin->w_cursor.col;
2062
2063 /* determine offset from first non-blank */
2064 new_cursor_col = curwin->w_cursor.col;
2065 beginline(BL_WHITE);
2066 new_cursor_col -= curwin->w_cursor.col;
2067
2068 insstart_less = curwin->w_cursor.col;
2069
2070 /*
2071 * If the cursor is in the indent, compute how many screen columns the
2072 * cursor is to the left of the first non-blank.
2073 */
2074 if (new_cursor_col < 0)
2075 vcol = get_indent() - vcol;
2076
2077 if (new_cursor_col > 0) /* can't fix replace stack */
2078 start_col = -1;
2079
2080 /*
2081 * Set the new indent. The cursor will be put on the first non-blank.
2082 */
2083 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002084 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 else
2086 {
2087#ifdef FEAT_VREPLACE
2088 int save_State = State;
2089
2090 /* Avoid being called recursively. */
2091 if (State & VREPLACE_FLAG)
2092 State = INSERT;
2093#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002094 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#ifdef FEAT_VREPLACE
2096 State = save_State;
2097#endif
2098 }
2099 insstart_less -= curwin->w_cursor.col;
2100
2101 /*
2102 * Try to put cursor on same character.
2103 * If the cursor is at or after the first non-blank in the line,
2104 * compute the cursor column relative to the column of the first
2105 * non-blank character.
2106 * If we are not in insert mode, leave the cursor on the first non-blank.
2107 * If the cursor is before the first non-blank, position it relative
2108 * to the first non-blank, counted in screen columns.
2109 */
2110 if (new_cursor_col >= 0)
2111 {
2112 /*
2113 * When changing the indent while the cursor is touching it, reset
2114 * Insstart_col to 0.
2115 */
2116 if (new_cursor_col == 0)
2117 insstart_less = MAXCOL;
2118 new_cursor_col += curwin->w_cursor.col;
2119 }
2120 else if (!(State & INSERT))
2121 new_cursor_col = curwin->w_cursor.col;
2122 else
2123 {
2124 /*
2125 * Compute the screen column where the cursor should be.
2126 */
2127 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002128 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
2130 /*
2131 * Advance the cursor until we reach the right screen column.
2132 */
2133 vcol = last_vcol = 0;
2134 new_cursor_col = -1;
2135 ptr = ml_get_curline();
2136 while (vcol <= (int)curwin->w_virtcol)
2137 {
2138 last_vcol = vcol;
2139#ifdef FEAT_MBYTE
2140 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002141 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 else
2143#endif
2144 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002145 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
2147 vcol = last_vcol;
2148
2149 /*
2150 * May need to insert spaces to be able to position the cursor on
2151 * the right screen column.
2152 */
2153 if (vcol != (int)curwin->w_virtcol)
2154 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002155 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002157 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (ptr != NULL)
2159 {
2160 new_cursor_col += i;
2161 ptr[i] = NUL;
2162 while (--i >= 0)
2163 ptr[i] = ' ';
2164 ins_str(ptr);
2165 vim_free(ptr);
2166 }
2167 }
2168
2169 /*
2170 * When changing the indent while the cursor is in it, reset
2171 * Insstart_col to 0.
2172 */
2173 insstart_less = MAXCOL;
2174 }
2175
2176 curwin->w_p_list = save_p_list;
2177
2178 if (new_cursor_col <= 0)
2179 curwin->w_cursor.col = 0;
2180 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002181 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 curwin->w_set_curswant = TRUE;
2183 changed_cline_bef_curs();
2184
2185 /*
2186 * May have to adjust the start of the insert.
2187 */
2188 if (State & INSERT)
2189 {
2190 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2191 {
2192 if ((int)Insstart.col <= insstart_less)
2193 Insstart.col = 0;
2194 else
2195 Insstart.col -= insstart_less;
2196 }
2197 if ((int)ai_col <= insstart_less)
2198 ai_col = 0;
2199 else
2200 ai_col -= insstart_less;
2201 }
2202
2203 /*
2204 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2205 * If the number of characters before the cursor decreased, need to pop a
2206 * few characters from the replace stack.
2207 * If the number of characters before the cursor increased, need to push a
2208 * few NULs onto the replace stack.
2209 */
2210 if (REPLACE_NORMAL(State) && start_col >= 0)
2211 {
2212 while (start_col > (int)curwin->w_cursor.col)
2213 {
2214 replace_join(0); /* remove a NUL from the replace stack */
2215 --start_col;
2216 }
2217 while (start_col < (int)curwin->w_cursor.col || replaced)
2218 {
2219 replace_push(NUL);
2220 if (replaced)
2221 {
2222 replace_push(replaced);
2223 replaced = NUL;
2224 }
2225 ++start_col;
2226 }
2227 }
2228
2229#ifdef FEAT_VREPLACE
2230 /*
2231 * For VREPLACE mode, we also have to fix the replace stack. In this case
2232 * it is always possible because we backspace over the whole line and then
2233 * put it back again the way we wanted it.
2234 */
2235 if (State & VREPLACE_FLAG)
2236 {
2237 /* If orig_line didn't allocate, just return. At least we did the job,
2238 * even if you can't backspace. */
2239 if (orig_line == NULL)
2240 return;
2241
2242 /* Save new line */
2243 new_line = vim_strsave(ml_get_curline());
2244 if (new_line == NULL)
2245 return;
2246
2247 /* We only put back the new line up to the cursor */
2248 new_line[curwin->w_cursor.col] = NUL;
2249
2250 /* Put back original line */
2251 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2252 curwin->w_cursor.col = orig_col;
2253
2254 /* Backspace from cursor to start of line */
2255 backspace_until_column(0);
2256
2257 /* Insert new stuff into line again */
2258 ins_bytes(new_line);
2259
2260 vim_free(new_line);
2261 }
2262#endif
2263}
2264
2265/*
2266 * Truncate the space at the end of a line. This is to be used only in an
2267 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2268 * modes.
2269 */
2270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 int i;
2274
2275 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002276 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 if (State & REPLACE_FLAG)
2279 replace_join(0); /* remove a NUL from the replace stack */
2280 }
2281 line[i + 1] = NUL;
2282}
2283
2284#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2285 || defined(FEAT_COMMENTS) || defined(PROTO)
2286/*
2287 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2288 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002289 * Will attempt not to go before "col" even when there is a composing
2290 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 */
2292 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002293backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
2295 while ((int)curwin->w_cursor.col > col)
2296 {
2297 curwin->w_cursor.col--;
2298 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002299 replace_do_bs(col);
2300 else if (!del_char_after_col(col))
2301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303}
2304#endif
2305
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002306/*
2307 * Like del_char(), but make sure not to go before column "limit_col".
2308 * Only matters when there are composing characters.
2309 * Return TRUE when something was deleted.
2310 */
2311 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002312del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002313{
2314#ifdef FEAT_MBYTE
2315 if (enc_utf8 && limit_col >= 0)
2316 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002317 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002318
2319 /* Make sure the cursor is at the start of a character, but
2320 * skip forward again when going too far back because of a
2321 * composing character. */
2322 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002323 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002324 {
2325 int l = utf_ptr2len(ml_get_cursor());
2326
2327 if (l == 0) /* end of line */
2328 break;
2329 curwin->w_cursor.col += l;
2330 }
2331 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2332 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002333 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002334 }
2335 else
2336#endif
2337 (void)del_char(FALSE);
2338 return TRUE;
2339}
2340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2342/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002343 * CTRL-X pressed in Insert mode.
2344 */
2345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002346ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002347{
2348 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2349 * CTRL-V works like CTRL-N */
2350 if (ctrl_x_mode != CTRL_X_CMDLINE)
2351 {
2352 /* if the next ^X<> won't ADD nothing, then reset
2353 * compl_cont_status */
2354 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002355 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002356 else
2357 compl_cont_status = 0;
2358 /* We're not sure which CTRL-X mode it will be yet */
2359 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2360 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2361 edit_submode_pre = NULL;
2362 showmode();
2363 }
2364}
2365
2366/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002367 * Whether other than default completion has been selected.
2368 */
2369 int
2370ctrl_x_mode_not_default(void)
2371{
2372 return ctrl_x_mode != CTRL_X_NORMAL;
2373}
2374
2375/*
2376 * Whether CTRL-X was typed without a following character.
2377 */
2378 int
2379ctrl_x_mode_not_defined_yet(void)
2380{
2381 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2382}
2383
2384/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002385 * Return TRUE if the 'dict' or 'tsr' option can be used.
2386 */
2387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002388has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002389{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002390 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002391# ifdef FEAT_SPELL
2392 && !curwin->w_p_spell
2393# endif
2394 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002395 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2396 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002397 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002398 edit_submode = NULL;
2399 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2400 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002401 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002402 if (emsg_silent == 0)
2403 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002404 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002405 setcursor();
2406 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002407#ifdef FEAT_EVAL
2408 if (!get_vim_var_nr(VV_TESTING))
2409#endif
2410 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002411 }
2412 return FALSE;
2413 }
2414 return TRUE;
2415}
2416
2417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2419 * This depends on the current mode.
2420 */
2421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002422vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 /* Always allow ^R - let it's results then be checked */
2425 if (c == Ctrl_R)
2426 return TRUE;
2427
Bram Moolenaare3226be2005-12-18 22:10:00 +00002428 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002429 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002430 return TRUE;
2431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 switch (ctrl_x_mode)
2433 {
2434 case 0: /* Not in any CTRL-X mode */
2435 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2436 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002437 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2439 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2440 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002441 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002442 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CTRL_X_SCROLL:
2444 return (c == Ctrl_Y || c == Ctrl_E);
2445 case CTRL_X_WHOLE_LINE:
2446 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2447 case CTRL_X_FILES:
2448 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2449 case CTRL_X_DICTIONARY:
2450 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2451 case CTRL_X_THESAURUS:
2452 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2453 case CTRL_X_TAGS:
2454 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2455#ifdef FEAT_FIND_ID
2456 case CTRL_X_PATH_PATTERNS:
2457 return (c == Ctrl_P || c == Ctrl_N);
2458 case CTRL_X_PATH_DEFINES:
2459 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2460#endif
2461 case CTRL_X_CMDLINE:
2462 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2463 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002464#ifdef FEAT_COMPL_FUNC
2465 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002466 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002467 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002468 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002469#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002470 case CTRL_X_SPELL:
2471 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002472 case CTRL_X_EVAL:
2473 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002475 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 return FALSE;
2477}
2478
2479/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002480 * Return TRUE when character "c" is part of the item currently being
2481 * completed. Used to decide whether to abandon complete mode when the menu
2482 * is visible.
2483 */
2484 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002485ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002486{
2487 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2488 /* When expanding an identifier only accept identifier chars. */
2489 return vim_isIDc(c);
2490
2491 switch (ctrl_x_mode)
2492 {
2493 case CTRL_X_FILES:
2494 /* When expanding file name only accept file name chars. But not
2495 * path separators, so that "proto/<Tab>" expands files in
2496 * "proto", not "proto/" as a whole */
2497 return vim_isfilec(c) && !vim_ispathsep(c);
2498
2499 case CTRL_X_CMDLINE:
2500 case CTRL_X_OMNI:
2501 /* Command line and Omni completion can work with just about any
2502 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002503 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002504
2505 case CTRL_X_WHOLE_LINE:
2506 /* For while line completion a space can be part of the line. */
2507 return vim_isprintc(c);
2508 }
2509 return vim_iswordc(c);
2510}
2511
2512/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002513 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002515 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 * the rest of the word to be in -- webb
2517 */
2518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002519ins_compl_add_infercase(
2520 char_u *str,
2521 int len,
2522 int icase,
2523 char_u *fname,
2524 int dir,
2525 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002527 char_u *p;
2528 int i, c;
2529 int actual_len; /* Take multi-byte characters */
2530 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002531 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002532 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 int has_lower = FALSE;
2534 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002536 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002538 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaara2993e12007-08-12 14:38:46 +00002540 /* Find actual length of completion. */
2541#ifdef FEAT_MBYTE
2542 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002544 p = str;
2545 actual_len = 0;
2546 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002548 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002549 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
2551 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002552 else
2553#endif
2554 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaara2993e12007-08-12 14:38:46 +00002556 /* Find actual length of original text. */
2557#ifdef FEAT_MBYTE
2558 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002560 p = compl_orig_text;
2561 actual_compl_length = 0;
2562 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002564 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002565 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002568 else
2569#endif
2570 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002572 /* "actual_len" may be smaller than "actual_compl_length" when using
2573 * thesaurus, only use the minimum when comparing. */
2574 min_len = actual_len < actual_compl_length
2575 ? actual_len : actual_compl_length;
2576
Bram Moolenaara2993e12007-08-12 14:38:46 +00002577 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002578 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002579 if (wca != NULL)
2580 {
2581 p = str;
2582 for (i = 0; i < actual_len; ++i)
2583#ifdef FEAT_MBYTE
2584 if (has_mbyte)
2585 wca[i] = mb_ptr2char_adv(&p);
2586 else
2587#endif
2588 wca[i] = *(p++);
2589
2590 /* Rule 1: Were any chars converted to lower? */
2591 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002592 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002593 {
2594#ifdef FEAT_MBYTE
2595 if (has_mbyte)
2596 c = mb_ptr2char_adv(&p);
2597 else
2598#endif
2599 c = *(p++);
2600 if (MB_ISLOWER(c))
2601 {
2602 has_lower = TRUE;
2603 if (MB_ISUPPER(wca[i]))
2604 {
2605 /* Rule 1 is satisfied. */
2606 for (i = actual_compl_length; i < actual_len; ++i)
2607 wca[i] = MB_TOLOWER(wca[i]);
2608 break;
2609 }
2610 }
2611 }
2612
2613 /*
2614 * Rule 2: No lower case, 2nd consecutive letter converted to
2615 * upper case.
2616 */
2617 if (!has_lower)
2618 {
2619 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002620 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002621 {
2622#ifdef FEAT_MBYTE
2623 if (has_mbyte)
2624 c = mb_ptr2char_adv(&p);
2625 else
2626#endif
2627 c = *(p++);
2628 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2629 {
2630 /* Rule 2 is satisfied. */
2631 for (i = actual_compl_length; i < actual_len; ++i)
2632 wca[i] = MB_TOUPPER(wca[i]);
2633 break;
2634 }
2635 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2636 }
2637 }
2638
2639 /* Copy the original case of the part we typed. */
2640 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002641 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002642 {
2643#ifdef FEAT_MBYTE
2644 if (has_mbyte)
2645 c = mb_ptr2char_adv(&p);
2646 else
2647#endif
2648 c = *(p++);
2649 if (MB_ISLOWER(c))
2650 wca[i] = MB_TOLOWER(wca[i]);
2651 else if (MB_ISUPPER(c))
2652 wca[i] = MB_TOUPPER(wca[i]);
2653 }
2654
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002655 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002656 * Generate encoding specific output from wide character array.
2657 * Multi-byte characters can occupy up to five bytes more than
2658 * ASCII characters, and we also need one byte for NUL, so stay
2659 * six bytes away from the edge of IObuff.
2660 */
2661 p = IObuff;
2662 i = 0;
2663 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2664#ifdef FEAT_MBYTE
2665 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002666 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002667 else
2668#endif
2669 *(p++) = wca[i++];
2670 *p = NUL;
2671
2672 vim_free(wca);
2673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002675 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2676 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002678 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679}
2680
2681/*
2682 * Add a match to the list of matches.
2683 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002684 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002685 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002688ins_compl_add(
2689 char_u *str,
2690 int len,
2691 int icase,
2692 char_u *fname,
2693 char_u **cptext, /* extra text for popup menu or NULL */
2694 int cdir,
2695 int flags,
2696 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002698 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002699 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 ui_breakcheck();
2702 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002703 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (len < 0)
2705 len = (int)STRLEN(str);
2706
2707 /*
2708 * If the same match is already present, don't add it.
2709 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002710 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002712 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 do
2714 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002715 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002716 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002717 && match->cp_str[len] == NUL)
2718 return NOTDONE;
2719 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002720 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002723 /* Remove any popup menu before changing the list of matches. */
2724 ins_compl_del_pum();
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 /*
2727 * Allocate a new match structure.
2728 * Copy the values to the new match structure.
2729 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002730 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002732 return FAIL;
2733 match->cp_number = -1;
2734 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002735 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002736 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
2738 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002739 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002741 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002742
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002744 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2746 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002747 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002748 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002749 && compl_curr_match->cp_fname != NULL
2750 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002751 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002752 else if (fname != NULL)
2753 {
2754 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002755 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002758 match->cp_fname = NULL;
2759 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002760
2761 if (cptext != NULL)
2762 {
2763 int i;
2764
2765 for (i = 0; i < CPT_COUNT; ++i)
2766 if (cptext[i] != NULL && *cptext[i] != NUL)
2767 match->cp_text[i] = vim_strsave(cptext[i]);
2768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
2770 /*
2771 * Link the new match structure in the list of matches.
2772 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002773 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002774 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 else if (dir == FORWARD)
2776 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002777 match->cp_next = compl_curr_match->cp_next;
2778 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 else /* BACKWARD */
2781 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002782 match->cp_next = compl_curr_match;
2783 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002785 if (match->cp_next)
2786 match->cp_next->cp_prev = match;
2787 if (match->cp_prev)
2788 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002790 compl_first_match = match;
2791 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002793 /*
2794 * Find the longest common string if still doing that.
2795 */
2796 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2797 ins_compl_longest_match(match);
2798
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 return OK;
2800}
2801
2802/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002803 * Return TRUE if "str[len]" matches with match->cp_str, considering
2804 * match->cp_icase.
2805 */
2806 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002807ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002808{
2809 if (match->cp_icase)
2810 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2811 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2812}
2813
2814/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002815 * Reduce the longest common string for match "match".
2816 */
2817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002818ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002819{
2820 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002821 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002822 int had_match;
2823
2824 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002825 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002826 /* First match, use it as a whole. */
2827 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002828 if (compl_leader != NULL)
2829 {
2830 had_match = (curwin->w_cursor.col > compl_col);
2831 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002832 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002833 ins_redraw(FALSE);
2834
2835 /* When the match isn't there (to avoid matching itself) remove it
2836 * again after redrawing. */
2837 if (!had_match)
2838 ins_compl_delete();
2839 compl_used_match = FALSE;
2840 }
2841 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002842 else
2843 {
2844 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002845 p = compl_leader;
2846 s = match->cp_str;
2847 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002848 {
2849#ifdef FEAT_MBYTE
2850 if (has_mbyte)
2851 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002852 c1 = mb_ptr2char(p);
2853 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002854 }
2855 else
2856#endif
2857 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002858 c1 = *p;
2859 c2 = *s;
2860 }
2861 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2862 : (c1 != c2))
2863 break;
2864#ifdef FEAT_MBYTE
2865 if (has_mbyte)
2866 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002867 MB_PTR_ADV(p);
2868 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002869 }
2870 else
2871#endif
2872 {
2873 ++p;
2874 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002875 }
2876 }
2877
2878 if (*p != NUL)
2879 {
2880 /* Leader was shortened, need to change the inserted text. */
2881 *p = NUL;
2882 had_match = (curwin->w_cursor.col > compl_col);
2883 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002884 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002885 ins_redraw(FALSE);
2886
2887 /* When the match isn't there (to avoid matching itself) remove it
2888 * again after redrawing. */
2889 if (!had_match)
2890 ins_compl_delete();
2891 }
2892
2893 compl_used_match = FALSE;
2894 }
2895}
2896
2897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 * Add an array of matches to the list of matches.
2899 * Frees matches[].
2900 */
2901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002902ins_compl_add_matches(
2903 int num_matches,
2904 char_u **matches,
2905 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
2907 int i;
2908 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002909 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
Bram Moolenaar572cb562005-08-05 21:35:02 +00002911 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002912 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002913 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002915 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 FreeWild(num_matches, matches);
2917}
2918
2919/* Make the completion list cyclic.
2920 * Return the number of matches (excluding the original).
2921 */
2922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002923ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002925 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 int count = 0;
2927
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002928 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 {
2930 /*
2931 * Find the end of the list.
2932 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002933 match = compl_first_match;
2934 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002935 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002937 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 ++count;
2939 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002940 match->cp_next = compl_first_match;
2941 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
2943 return count;
2944}
2945
Bram Moolenaara94bc432006-03-10 21:42:59 +00002946/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002947 * Set variables that store noselect and noinsert behavior from the
2948 * 'completeopt' value.
2949 */
2950 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002951completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002952{
2953 compl_no_insert = FALSE;
2954 compl_no_select = FALSE;
2955 if (strstr((char *)p_cot, "noselect") != NULL)
2956 compl_no_select = TRUE;
2957 if (strstr((char *)p_cot, "noinsert") != NULL)
2958 compl_no_insert = TRUE;
2959}
2960
2961/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002962 * Start completion for the complete() function.
2963 * "startcol" is where the matched text starts (1 is first column).
2964 * "list" is the list of matches.
2965 */
2966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002967set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002968{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002969 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002970 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002971
Bram Moolenaara94bc432006-03-10 21:42:59 +00002972 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002973 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002974 ins_compl_prep(' ');
2975 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002976 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002977
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002978 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002979 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002980 startcol = curwin->w_cursor.col;
2981 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002982 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002983 /* compl_pattern doesn't need to be set */
2984 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2985 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002986 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002987 return;
2988
Bram Moolenaare4214502015-03-05 18:08:43 +01002989 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002990
2991 ins_compl_add_list(list);
2992 compl_matches = ins_compl_make_cyclic();
2993 compl_started = TRUE;
2994 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002995 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002996
2997 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002998 if (compl_no_insert || compl_no_select)
2999 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003000 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02003001 if (compl_no_select)
3002 /* Down/Up has no real effect. */
3003 ins_complete(K_UP, FALSE);
3004 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003005 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003006 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02003007 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003008
3009 /* Lazily show the popup menu, unless we got interrupted. */
3010 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003011 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003012 out_flush();
3013}
3014
3015
Bram Moolenaar9372a112005-12-06 19:59:18 +00003016/* "compl_match_array" points the currently displayed list of entries in the
3017 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003018static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003019static int compl_match_arraysize;
3020
3021/*
3022 * Update the screen and when there is any scrolling remove the popup menu.
3023 */
3024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003025ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003026{
3027 int h;
3028
3029 if (compl_match_array != NULL)
3030 {
3031 h = curwin->w_cline_height;
3032 update_screen(0);
3033 if (h != curwin->w_cline_height)
3034 ins_compl_del_pum();
3035 }
3036}
3037
3038/*
3039 * Remove any popup menu.
3040 */
3041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003042ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003043{
3044 if (compl_match_array != NULL)
3045 {
3046 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003047 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003048 }
3049}
3050
3051/*
3052 * Return TRUE if the popup menu should be displayed.
3053 */
3054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003055pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003057 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003058 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003059 return FALSE;
3060
3061 /* The display looks bad on a B&W display. */
3062 if (t_colors < 8
3063#ifdef FEAT_GUI
3064 && !gui.in_use
3065#endif
3066 )
3067 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003068 return TRUE;
3069}
3070
3071/*
3072 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003073 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003074 */
3075 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003076pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003077{
3078 compl_T *compl;
3079 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003080
3081 /* Don't display the popup menu if there are no matches or there is only
3082 * one (ignoring the original text). */
3083 compl = compl_first_match;
3084 i = 0;
3085 do
3086 {
3087 if (compl == NULL
3088 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3089 break;
3090 compl = compl->cp_next;
3091 } while (compl != compl_first_match);
3092
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003093 if (strstr((char *)p_cot, "menuone") != NULL)
3094 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003095 return (i >= 2);
3096}
3097
3098/*
3099 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003100 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003101 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003103ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003104{
3105 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003106 compl_T *shown_compl = NULL;
3107 int did_find_shown_match = FALSE;
3108 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003109 int i;
3110 int cur = -1;
3111 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003112 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003113
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003114 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003115 return;
3116
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003117#if defined(FEAT_EVAL)
3118 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3119 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3120#endif
3121
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003122 /* Update the screen before drawing the popup menu over it. */
3123 update_screen(0);
3124
3125 if (compl_match_array == NULL)
3126 {
3127 /* Need to build the popup menu list. */
3128 compl_match_arraysize = 0;
3129 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003130 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003131 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003132 do
3133 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003134 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3135 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003136 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003137 ++compl_match_arraysize;
3138 compl = compl->cp_next;
3139 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003140 if (compl_match_arraysize == 0)
3141 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003142 compl_match_array = (pumitem_T *)alloc_clear(
3143 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003144 * compl_match_arraysize));
3145 if (compl_match_array != NULL)
3146 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003147 /* If the current match is the original text don't find the first
3148 * match after it, don't highlight anything. */
3149 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3150 shown_match_ok = TRUE;
3151
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003152 i = 0;
3153 compl = compl_first_match;
3154 do
3155 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003156 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3157 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003158 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003159 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003160 if (!shown_match_ok)
3161 {
3162 if (compl == compl_shown_match || did_find_shown_match)
3163 {
3164 /* This item is the shown match or this is the
3165 * first displayed item after the shown match. */
3166 compl_shown_match = compl;
3167 did_find_shown_match = TRUE;
3168 shown_match_ok = TRUE;
3169 }
3170 else
3171 /* Remember this displayed match for when the
3172 * shown match is just below it. */
3173 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003174 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003175 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003176
3177 if (compl->cp_text[CPT_ABBR] != NULL)
3178 compl_match_array[i].pum_text =
3179 compl->cp_text[CPT_ABBR];
3180 else
3181 compl_match_array[i].pum_text = compl->cp_str;
3182 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3183 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3184 if (compl->cp_text[CPT_MENU] != NULL)
3185 compl_match_array[i++].pum_extra =
3186 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003187 else
3188 compl_match_array[i++].pum_extra = compl->cp_fname;
3189 }
3190
3191 if (compl == compl_shown_match)
3192 {
3193 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003194
3195 /* When the original text is the shown match don't set
3196 * compl_shown_match. */
3197 if (compl->cp_flags & ORIGINAL_TEXT)
3198 shown_match_ok = TRUE;
3199
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003200 if (!shown_match_ok && shown_compl != NULL)
3201 {
3202 /* The shown match isn't displayed, set it to the
3203 * previously displayed match. */
3204 compl_shown_match = shown_compl;
3205 shown_match_ok = TRUE;
3206 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003207 }
3208 compl = compl->cp_next;
3209 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003210
3211 if (!shown_match_ok) /* no displayed match at all */
3212 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003213 }
3214 }
3215 else
3216 {
3217 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003218 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003219 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3220 || compl_match_array[i].pum_text
3221 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003222 {
3223 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003224 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003225 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003226 }
3227
3228 if (compl_match_array != NULL)
3229 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003230 /* In Replace mode when a $ is displayed at the end of the line only
3231 * part of the screen would be updated. We do need to redraw here. */
3232 dollar_vcol = -1;
3233
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003234 /* Compute the screen column of the start of the completed text.
3235 * Use the cursor to get all wrapping and other settings right. */
3236 col = curwin->w_cursor.col;
3237 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003238 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003239 curwin->w_cursor.col = col;
3240 }
3241}
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243#define DICT_FIRST (1) /* use just first element in "dict" */
3244#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003247 * Add any identifiers that match the given pattern in the list of dictionary
3248 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 */
3250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003251ins_compl_dictionaries(
3252 char_u *dict_start,
3253 char_u *pat,
3254 int flags, /* DICT_FIRST and/or DICT_EXACT */
3255 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003257 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 char_u *ptr;
3259 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 char_u **files;
3262 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003264 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
Bram Moolenaar0b238792006-03-02 22:49:12 +00003266 if (*dict == NUL)
3267 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003268#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003269 /* When 'dictionary' is empty and spell checking is enabled use
3270 * "spell". */
3271 if (!thesaurus && curwin->w_p_spell)
3272 dict = (char_u *)"spell";
3273 else
3274#endif
3275 return;
3276 }
3277
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003279 if (buf == NULL)
3280 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003281 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 /* If 'infercase' is set, don't use 'smartcase' here */
3284 save_p_scs = p_scs;
3285 if (curbuf->b_p_inf)
3286 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003287
3288 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3289 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003290 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003291 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003292 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003293 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003294 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003295
3296 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003297 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003298 len = STRLEN(pat_esc) + 10;
3299 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003300 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003301 {
3302 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003303 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003304 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003305 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003306 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3307 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003308 vim_free(ptr);
3309 }
3310 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003311 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003312 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003313 if (regmatch.regprog == NULL)
3314 goto theend;
3315 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003316
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3318 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003319 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
3321 /* copy one dictionary file name into buf */
3322 if (flags == DICT_EXACT)
3323 {
3324 count = 1;
3325 files = &dict;
3326 }
3327 else
3328 {
3329 /* Expand wildcards in the dictionary name, but do not allow
3330 * backticks (for security, the 'dict' option may have been set in
3331 * a modeline). */
3332 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003333# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003334 if (!thesaurus && STRCMP(buf, "spell") == 0)
3335 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003336 else
3337# endif
3338 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 || expand_wildcards(1, &buf, &count, &files,
3340 EW_FILE|EW_SILENT) != OK)
3341 count = 0;
3342 }
3343
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003344# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003345 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003347 /* Complete from active spelling. Skip "\<" in the pattern, we
3348 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003349 if (pat[0] == '\\' && pat[1] == '<')
3350 ptr = pat + 2;
3351 else
3352 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003353 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003355 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003356# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003357 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003358 {
3359 ins_compl_files(count, files, thesaurus, flags,
3360 &regmatch, buf, &dir);
3361 if (flags != DICT_EXACT)
3362 FreeWild(count, files);
3363 }
3364 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 break;
3366 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003367
3368theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003370 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 vim_free(buf);
3372}
3373
Bram Moolenaar0b238792006-03-02 22:49:12 +00003374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003375ins_compl_files(
3376 int count,
3377 char_u **files,
3378 int thesaurus,
3379 int flags,
3380 regmatch_T *regmatch,
3381 char_u *buf,
3382 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003383{
3384 char_u *ptr;
3385 int i;
3386 FILE *fp;
3387 int add_r;
3388
3389 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3390 {
3391 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3392 if (flags != DICT_EXACT)
3393 {
3394 vim_snprintf((char *)IObuff, IOSIZE,
3395 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003396 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003397 }
3398
3399 if (fp != NULL)
3400 {
3401 /*
3402 * Read dictionary file line by line.
3403 * Check each line for a match.
3404 */
3405 while (!got_int && !compl_interrupted
3406 && !vim_fgets(buf, LSIZE, fp))
3407 {
3408 ptr = buf;
3409 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3410 {
3411 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003412 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003413 ptr = find_line_end(ptr);
3414 else
3415 ptr = find_word_end(ptr);
3416 add_r = ins_compl_add_infercase(regmatch->startp[0],
3417 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003418 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003419 if (thesaurus)
3420 {
3421 char_u *wstart;
3422
3423 /*
3424 * Add the other matches on the line
3425 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003426 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003427 while (!got_int)
3428 {
3429 /* Find start of the next word. Skip white
3430 * space and punctuation. */
3431 ptr = find_word_start(ptr);
3432 if (*ptr == NUL || *ptr == NL)
3433 break;
3434 wstart = ptr;
3435
Bram Moolenaara2993e12007-08-12 14:38:46 +00003436 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003437#ifdef FEAT_MBYTE
3438 if (has_mbyte)
3439 /* Japanese words may have characters in
3440 * different classes, only separate words
3441 * with single-byte non-word characters. */
3442 while (*ptr != NUL)
3443 {
3444 int l = (*mb_ptr2len)(ptr);
3445
3446 if (l < 2 && !vim_iswordc(*ptr))
3447 break;
3448 ptr += l;
3449 }
3450 else
3451#endif
3452 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003453
3454 /* Add the word. Skip the regexp match. */
3455 if (wstart != regmatch->startp[0])
3456 add_r = ins_compl_add_infercase(wstart,
3457 (int)(ptr - wstart),
3458 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003459 }
3460 }
3461 if (add_r == OK)
3462 /* if dir was BACKWARD then honor it just once */
3463 *dir = FORWARD;
3464 else if (add_r == FAIL)
3465 break;
3466 /* avoid expensive call to vim_regexec() when at end
3467 * of line */
3468 if (*ptr == '\n' || got_int)
3469 break;
3470 }
3471 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003472 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003473 }
3474 fclose(fp);
3475 }
3476 }
3477}
3478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479/*
3480 * Find the start of the next word.
3481 * Returns a pointer to the first char of the word. Also stops at a NUL.
3482 */
3483 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003484find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
3486#ifdef FEAT_MBYTE
3487 if (has_mbyte)
3488 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003489 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 else
3491#endif
3492 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3493 ++ptr;
3494 return ptr;
3495}
3496
3497/*
3498 * Find the end of the word. Assumes it starts inside a word.
3499 * Returns a pointer to just after the word.
3500 */
3501 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003502find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
3504#ifdef FEAT_MBYTE
3505 int start_class;
3506
3507 if (has_mbyte)
3508 {
3509 start_class = mb_get_class(ptr);
3510 if (start_class > 1)
3511 while (*ptr != NUL)
3512 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003513 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (mb_get_class(ptr) != start_class)
3515 break;
3516 }
3517 }
3518 else
3519#endif
3520 while (vim_iswordc(*ptr))
3521 ++ptr;
3522 return ptr;
3523}
3524
3525/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003526 * Find the end of the line, omitting CR and NL at the end.
3527 * Returns a pointer to just after the line.
3528 */
3529 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003530find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003531{
3532 char_u *s;
3533
3534 s = ptr + STRLEN(ptr);
3535 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3536 --s;
3537 return s;
3538}
3539
3540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 * Free the list of completions
3542 */
3543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003544ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003546 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003547 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
Bram Moolenaard23a8232018-02-10 18:45:26 +01003549 VIM_CLEAR(compl_pattern);
3550 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003552 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003554
3555 ins_compl_del_pum();
3556 pum_clear();
3557
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003558 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 do
3560 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003561 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003562 compl_curr_match = compl_curr_match->cp_next;
3563 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003565 if (match->cp_flags & FREE_FNAME)
3566 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003567 for (i = 0; i < CPT_COUNT; ++i)
3568 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003570 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3571 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003572 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003573 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574}
3575
3576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003579 compl_cont_status = 0;
3580 compl_started = FALSE;
3581 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003582 VIM_CLEAR(compl_pattern);
3583 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003585 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003586 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003587 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003588 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589}
3590
3591/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003592 * Return TRUE when Insert completion is active.
3593 */
3594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003595ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003596{
3597 return compl_started;
3598}
3599
3600/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003601 * Delete one character before the cursor and show the subset of the matches
3602 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003603 * Returns the character to be used, NUL if the work is done and another char
3604 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003605 */
3606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003607ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003608{
3609 char_u *line;
3610 char_u *p;
3611
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003612 line = ml_get_curline();
3613 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003614 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003615
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003616 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003617 * allow the word to be deleted, we won't match everything.
3618 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003619 if ((int)(p - line) - (int)compl_col < 0
3620 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003621 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3622 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3623 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003624 return K_BS;
3625
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003626 /* Deleted more than what was used to find matches or didn't finish
3627 * finding all matches: need to look for matches all over again. */
3628 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003629 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003630 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003631
Bram Moolenaara6557602006-02-04 22:43:20 +00003632 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003633 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003634 if (compl_leader != NULL)
3635 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003636 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003637 if (compl_shown_match != NULL)
3638 /* Make sure current match is not a hidden item. */
3639 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003640 return NUL;
3641 }
3642 return K_BS;
3643}
Bram Moolenaara6557602006-02-04 22:43:20 +00003644
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003645/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003646 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3647 * be called.
3648 */
3649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003650ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003651{
3652 /* Return TRUE if we didn't complete finding matches or when the
3653 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3654 return compl_was_interrupted
3655 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3656 && compl_opt_refresh_always);
3657}
3658
3659/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003660 * Called after changing "compl_leader".
3661 * Show the popup menu with a different set of matches.
3662 * May also search for matches again if the previous search was interrupted.
3663 */
3664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003665ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003666{
3667 ins_compl_del_pum();
3668 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003669 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003670 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003671
3672 if (compl_started)
3673 ins_compl_set_original_text(compl_leader);
3674 else
3675 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003676#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003677 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003678#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003679 /*
3680 * Matches were cleared, need to search for them now. First display
3681 * the changed text before the cursor. Set "compl_restarting" to
3682 * avoid that the first match is inserted.
3683 */
3684 update_screen(0);
3685#ifdef FEAT_GUI
3686 if (gui.in_use)
3687 {
3688 /* Show the cursor after the match, not after the redrawn text. */
3689 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003690 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003691 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003692#endif
3693 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003694 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003695 compl_cont_status = 0;
3696 compl_restarting = FALSE;
3697 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003698
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003699 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003700
3701 /* Show the popup menu with a different set of matches. */
3702 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003703
3704 /* Don't let Enter select the original text when there is no popup menu. */
3705 if (compl_match_array == NULL)
3706 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003707}
3708
3709/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003710 * Return the length of the completion, from the completion start column to
3711 * the cursor column. Making sure it never goes below zero.
3712 */
3713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003714ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003715{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003716 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003717
3718 if (off < 0)
3719 return 0;
3720 return off;
3721}
3722
3723/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003724 * Append one character to the match leader. May reduce the number of
3725 * matches.
3726 */
3727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003728ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003729{
3730#ifdef FEAT_MBYTE
3731 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003732#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003733
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003734 if (stop_arrow() == FAIL)
3735 return;
3736#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003737 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3738 {
3739 char_u buf[MB_MAXBYTES + 1];
3740
3741 (*mb_char2bytes)(c, buf);
3742 buf[cc] = NUL;
3743 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003744 if (compl_opt_refresh_always)
3745 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003746 }
3747 else
3748#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003749 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003750 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003751 if (compl_opt_refresh_always)
3752 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003753 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003754
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003755 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003756 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003757 ins_compl_restart();
3758
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003759 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003760 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003761 * break redo. */
3762 if (!compl_opt_refresh_always)
3763 {
3764 vim_free(compl_leader);
3765 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003766 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003767 if (compl_leader != NULL)
3768 ins_compl_new_leader();
3769 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003770}
3771
3772/*
3773 * Setup for finding completions again without leaving CTRL-X mode. Used when
3774 * BS or a key was typed while still searching for matches.
3775 */
3776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003778{
3779 ins_compl_free();
3780 compl_started = FALSE;
3781 compl_matches = 0;
3782 compl_cont_status = 0;
3783 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003784}
3785
3786/*
3787 * Set the first match, the original text.
3788 */
3789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003790ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003791{
3792 char_u *p;
3793
Bram Moolenaare87edf32018-04-17 22:14:32 +02003794 /* Replace the original text entry.
3795 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3796 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003797 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3798 {
3799 p = vim_strsave(str);
3800 if (p != NULL)
3801 {
3802 vim_free(compl_first_match->cp_str);
3803 compl_first_match->cp_str = p;
3804 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003805 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003806 else if (compl_first_match->cp_prev != NULL
3807 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3808 {
3809 p = vim_strsave(str);
3810 if (p != NULL)
3811 {
3812 vim_free(compl_first_match->cp_prev->cp_str);
3813 compl_first_match->cp_prev->cp_str = p;
3814 }
3815 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003816}
3817
3818/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003819 * Append one character to the match leader. May reduce the number of
3820 * matches.
3821 */
3822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003823ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003824{
3825 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003826 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003827 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003828 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003829
3830 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003831 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003832 {
3833 /* When still at the original match use the first entry that matches
3834 * the leader. */
3835 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3836 {
3837 p = NULL;
3838 for (cp = compl_shown_match->cp_next; cp != NULL
3839 && cp != compl_first_match; cp = cp->cp_next)
3840 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003841 if (compl_leader == NULL
3842 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003843 (int)STRLEN(compl_leader)))
3844 {
3845 p = cp->cp_str;
3846 break;
3847 }
3848 }
3849 if (p == NULL || (int)STRLEN(p) <= len)
3850 return;
3851 }
3852 else
3853 return;
3854 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003855 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003856 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003857 ins_compl_addleader(c);
3858}
3859
3860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003862 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003863 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003866ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
3868 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003870 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
3872 /* Forget any previous 'special' messages if this is actually
3873 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3874 */
3875 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3876 edit_submode_extra = NULL;
3877
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003878 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003879 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3880 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003881 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003883 /* Set "compl_get_longest" when finding the first matches. */
3884 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003885 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003886 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003887 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003888 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003889
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003890 }
3891
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3893 {
3894 /*
3895 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3896 * it will be yet. Now we decide.
3897 */
3898 switch (c)
3899 {
3900 case Ctrl_E:
3901 case Ctrl_Y:
3902 ctrl_x_mode = CTRL_X_SCROLL;
3903 if (!(State & REPLACE_FLAG))
3904 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3905 else
3906 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3907 edit_submode_pre = NULL;
3908 showmode();
3909 break;
3910 case Ctrl_L:
3911 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3912 break;
3913 case Ctrl_F:
3914 ctrl_x_mode = CTRL_X_FILES;
3915 break;
3916 case Ctrl_K:
3917 ctrl_x_mode = CTRL_X_DICTIONARY;
3918 break;
3919 case Ctrl_R:
3920 /* Simply allow ^R to happen without affecting ^X mode */
3921 break;
3922 case Ctrl_T:
3923 ctrl_x_mode = CTRL_X_THESAURUS;
3924 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003925#ifdef FEAT_COMPL_FUNC
3926 case Ctrl_U:
3927 ctrl_x_mode = CTRL_X_FUNCTION;
3928 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003930 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003932#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003933 case 's':
3934 case Ctrl_S:
3935 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003936#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003937 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003938 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003940#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003941 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 case Ctrl_RSB:
3943 ctrl_x_mode = CTRL_X_TAGS;
3944 break;
3945#ifdef FEAT_FIND_ID
3946 case Ctrl_I:
3947 case K_S_TAB:
3948 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3949 break;
3950 case Ctrl_D:
3951 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3952 break;
3953#endif
3954 case Ctrl_V:
3955 case Ctrl_Q:
3956 ctrl_x_mode = CTRL_X_CMDLINE;
3957 break;
3958 case Ctrl_P:
3959 case Ctrl_N:
3960 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3961 * just started ^X mode, or there were enough ^X's to cancel
3962 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3963 * do normal expansion when interrupting a different mode (say
3964 * ^X^F^X^P or ^P^X^X^P, see below)
3965 * nothing changes if interrupting mode 0, (eg, the flag
3966 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003967 if (!(compl_cont_status & CONT_INTRPT))
3968 compl_cont_status |= CONT_LOCAL;
3969 else if (compl_cont_mode != 0)
3970 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 /* FALLTHROUGH */
3972 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003973 /* If we have typed at least 2 ^X's... for modes != 0, we set
3974 * compl_cont_status = 0 (eg, as if we had just started ^X
3975 * mode).
3976 * For mode 0, we set "compl_cont_mode" to an impossible
3977 * value, in both cases ^X^X can be used to restart the same
3978 * mode (avoiding ADDING mode).
3979 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3980 * 'complete' and local ^P expansions respectively.
3981 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3982 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 if (c == Ctrl_X)
3984 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003985 if (compl_cont_mode != 0)
3986 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003988 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003990 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 edit_submode = NULL;
3992 showmode();
3993 break;
3994 }
3995 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003996 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 {
3998 /* We're already in CTRL-X mode, do we stay in it? */
3999 if (!vim_is_ctrl_x_key(c))
4000 {
4001 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004002 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 else
4004 ctrl_x_mode = CTRL_X_FINISHED;
4005 edit_submode = NULL;
4006 }
4007 showmode();
4008 }
4009
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004010 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
4012 /* Show error message from attempted keyword completion (probably
4013 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004014 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004016 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4017 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 || ctrl_x_mode == CTRL_X_FINISHED)
4019 {
4020 /* Get here when we have finished typing a sequence of ^N and
4021 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004022 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004023 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
4025 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004026 * If any of the original typed text has been changed, eg when
4027 * ignorecase is set, we must add back-spaces to the redo
4028 * buffer. We add as few as necessary to delete just the part
4029 * of the original text that has changed.
4030 * When using the longest match, edited the match or used
4031 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004033 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4034 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004035 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004036 ptr = NULL;
4037 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039
4040#ifdef FEAT_CINDENT
4041 want_cindent = (can_cindent && cindent_on());
4042#endif
4043 /*
4044 * When completing whole lines: fix indent for 'cindent'.
4045 * Otherwise, break line if it's too long.
4046 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
4049#ifdef FEAT_CINDENT
4050 /* re-indent the current line */
4051 if (want_cindent)
4052 {
4053 do_c_expr_indent();
4054 want_cindent = FALSE; /* don't do it again */
4055 }
4056#endif
4057 }
4058 else
4059 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004060 int prev_col = curwin->w_cursor.col;
4061
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004063 if (prev_col > 0)
4064 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004065 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004066 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004068 if (prev_col > 0
4069 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4070 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004073 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004074 * the selection without inserting anything. When
4075 * compl_enter_selects is set the Enter key does the same. */
4076 if ((c == Ctrl_Y || (compl_enter_selects
4077 && (c == CAR || c == K_KENTER || c == NL)))
4078 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004079 retval = TRUE;
4080
Bram Moolenaar47247282016-08-02 22:36:02 +02004081 /* CTRL-E means completion is Ended, go back to the typed text.
4082 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004083 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004084 {
4085 ins_compl_delete();
4086 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004087 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004088 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004089 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004090 retval = TRUE;
4091 }
4092
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004093 auto_format(FALSE, TRUE);
4094
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004096 compl_started = FALSE;
4097 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004098 if (!shortmess(SHM_COMPLETIONMENU))
4099 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004100 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004101 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (edit_submode != NULL)
4103 {
4104 edit_submode = NULL;
4105 showmode();
4106 }
4107
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004108#ifdef FEAT_CMDWIN
4109 if (c == Ctrl_C && cmdwin_type != 0)
4110 /* Avoid the popup menu remains displayed when leaving the
4111 * command line window. */
4112 update_screen(0);
4113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114#ifdef FEAT_CINDENT
4115 /*
4116 * Indent now if a key was typed that is in 'cinkeys'.
4117 */
4118 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4119 do_c_expr_indent();
4120#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004121 /* Trigger the CompleteDone event to give scripts a chance to act
4122 * upon the completion. */
4123 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004126 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4127 /* Trigger the CompleteDone event to give scripts a chance to act
4128 * upon the (possibly failed) completion. */
4129 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131 /* reset continue_* if we left expansion-mode, if we stay they'll be
4132 * (re)set properly in ins_complete() */
4133 if (!vim_is_ctrl_x_key(c))
4134 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004135 compl_cont_status = 0;
4136 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004138
4139 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140}
4141
4142/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004143 * Fix the redo buffer for the completion leader replacing some of the typed
4144 * text. This inserts backspaces and appends the changed text.
4145 * "ptr" is the known leader text or NUL.
4146 */
4147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004148ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004149{
4150 int len;
4151 char_u *p;
4152 char_u *ptr = ptr_arg;
4153
4154 if (ptr == NULL)
4155 {
4156 if (compl_leader != NULL)
4157 ptr = compl_leader;
4158 else
4159 return; /* nothing to do */
4160 }
4161 if (compl_orig_text != NULL)
4162 {
4163 p = compl_orig_text;
4164 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4165 ;
4166#ifdef FEAT_MBYTE
4167 if (len > 0)
4168 len -= (*mb_head_off)(p, p + len);
4169#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004170 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004171 AppendCharToRedobuff(K_BS);
4172 }
4173 else
4174 len = 0;
4175 if (ptr != NULL)
4176 AppendToRedobuffLit(ptr + len, -1);
4177}
4178
4179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4181 * (depending on flag) starting from buf and looking for a non-scanned
4182 * buffer (other than curbuf). curbuf is special, if it is called with
4183 * buf=curbuf then it has to be the first call for a given flag/expansion.
4184 *
4185 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4186 */
4187 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004188ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192 if (flag == 'w') /* just windows */
4193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 if (buf == curbuf) /* first call for this flag/expansion */
4195 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004196 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 && wp->w_buffer->b_scanned)
4198 ;
4199 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 else
4202 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4203 * (unlisted buffers)
4204 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004205 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 && ((flag == 'U'
4207 ? buf->b_p_bl
4208 : (!buf->b_p_bl
4209 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004210 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 ;
4212 return buf;
4213}
4214
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004215#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004216/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004217 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004218 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004219 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004221expand_by_function(
4222 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4223 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004224{
Bram Moolenaar82139082011-09-14 16:52:09 +02004225 list_T *matchlist = NULL;
4226 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004227 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004228 char_u *funcname;
4229 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004230 win_T *curwin_save;
4231 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004232 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004233
Bram Moolenaare344bea2005-09-01 20:46:49 +00004234 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4235 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004236 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004237
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004238 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004239 args[0].v_type = VAR_NUMBER;
4240 args[0].vval.v_number = 0;
4241 args[1].v_type = VAR_STRING;
4242 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4243 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004244
Bram Moolenaare344bea2005-09-01 20:46:49 +00004245 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004246 curwin_save = curwin;
4247 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004248
4249 /* Call a function, which returns a list or dict. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004250 if (call_vim_function(funcname, 2, args, &rettv, FALSE) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004251 {
4252 switch (rettv.v_type)
4253 {
4254 case VAR_LIST:
4255 matchlist = rettv.vval.v_list;
4256 break;
4257 case VAR_DICT:
4258 matchdict = rettv.vval.v_dict;
4259 break;
4260 default:
4261 /* TODO: Give error message? */
4262 clear_tv(&rettv);
4263 break;
4264 }
4265 }
4266
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004267 if (curwin_save != curwin || curbuf_save != curbuf)
4268 {
4269 EMSG(_(e_complwin));
4270 goto theend;
4271 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004272 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004273 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004274 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004275 {
4276 EMSG(_(e_compldel));
4277 goto theend;
4278 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004279
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004280 if (matchlist != NULL)
4281 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004282 else if (matchdict != NULL)
4283 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004284
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004285theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004286 if (matchdict != NULL)
4287 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004288 if (matchlist != NULL)
4289 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004290}
4291#endif /* FEAT_COMPL_FUNC */
4292
Bram Moolenaar39f05632006-03-19 22:15:26 +00004293#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004294/*
4295 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004296 */
4297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004298ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004299{
4300 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004301 int dir = compl_direction;
4302
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004303 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004304 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004305 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004306 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4307 /* if dir was BACKWARD then honor it just once */
4308 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004309 else if (did_emsg)
4310 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004311 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004312}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004313
4314/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004315 * Add completions from a dict.
4316 */
4317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004318ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004319{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004320 dictitem_T *di_refresh;
4321 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004322
4323 /* Check for optional "refresh" item. */
4324 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004325 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4326 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004327 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004328 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004329
4330 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4331 compl_opt_refresh_always = TRUE;
4332 }
4333
4334 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004335 di_words = dict_find(dict, (char_u *)"words", 5);
4336 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4337 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004338}
4339
4340/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004341 * Add a match to the list of matches from a typeval_T.
4342 * If the given string is already in the list of completions, then return
4343 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4344 * maybe because alloc() returns NULL, then FAIL is returned.
4345 */
4346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004347ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004348{
4349 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004350 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004351 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004352 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004353 char_u *(cptext[CPT_COUNT]);
4354
4355 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4356 {
4357 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4358 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4359 (char_u *)"abbr", FALSE);
4360 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4361 (char_u *)"menu", FALSE);
4362 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4363 (char_u *)"kind", FALSE);
4364 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4365 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004366 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4367 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004368 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4369 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004370 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004371 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004372 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4373 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004374 }
4375 else
4376 {
4377 word = get_tv_string_chk(tv);
4378 vim_memset(cptext, 0, sizeof(cptext));
4379 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004380 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004381 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004382 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004383}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004384#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004385
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386/*
4387 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004388 * The search starts at position "ini" in curbuf and in the direction
4389 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004390 * When "compl_started" is FALSE start at that position, otherwise continue
4391 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 * This may return before finding all the matches.
4393 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 */
4395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004396ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
4398 static pos_T first_match_pos;
4399 static pos_T last_match_pos;
4400 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 static int found_all = FALSE; /* Found all matches of a
4402 certain type. */
4403 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
Bram Moolenaar572cb562005-08-05 21:35:02 +00004405 pos_T *pos;
4406 char_u **matches;
4407 int save_p_scs;
4408 int save_p_ws;
4409 int save_p_ic;
4410 int i;
4411 int num_matches;
4412 int len;
4413 int found_new_match;
4414 int type = ctrl_x_mode;
4415 char_u *ptr;
4416 char_u *dict = NULL;
4417 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004418 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004422 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 ins_buf->b_scanned = 0;
4424 found_all = FALSE;
4425 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004427 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 last_match_pos = first_match_pos = *ini;
4429 }
4430
Bram Moolenaar4475b622017-05-01 20:46:52 +02004431 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004432 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4434 for (;;)
4435 {
4436 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004437 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004439 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 * or if found_all says this entry is done. For ^X^L only use the
4441 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004442 if ((ctrl_x_mode == CTRL_X_NORMAL
4443 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
4446 found_all = FALSE;
4447 while (*e_cpt == ',' || *e_cpt == ' ')
4448 e_cpt++;
4449 if (*e_cpt == '.' && !curbuf->b_scanned)
4450 {
4451 ins_buf = curbuf;
4452 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004453 /* Move the cursor back one character so that ^N can match the
4454 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004455 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004456 {
4457 /* Move the cursor to after the last character in the
4458 * buffer, so that word at start of buffer is found
4459 * correctly. */
4460 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4461 first_match_pos.col =
4462 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 last_match_pos = first_match_pos;
4465 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004466
4467 /* Remember the first match so that the loop stops when we
4468 * wrap and come back there a second time. */
4469 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4472 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4473 {
4474 /* Scan a buffer, but not the current one. */
4475 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4476 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004477 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 first_match_pos.col = last_match_pos.col = 0;
4479 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4480 last_match_pos.lnum = 0;
4481 type = 0;
4482 }
4483 else /* unloaded buffer, scan like dictionary */
4484 {
4485 found_all = TRUE;
4486 if (ins_buf->b_fname == NULL)
4487 continue;
4488 type = CTRL_X_DICTIONARY;
4489 dict = ins_buf->b_fname;
4490 dict_f = DICT_EXACT;
4491 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004492 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 ins_buf->b_fname == NULL
4494 ? buf_spname(ins_buf)
4495 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004496 ? ins_buf->b_fname
4497 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004498 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 else if (*e_cpt == NUL)
4501 break;
4502 else
4503 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004504 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 type = -1;
4506 else if (*e_cpt == 'k' || *e_cpt == 's')
4507 {
4508 if (*e_cpt == 'k')
4509 type = CTRL_X_DICTIONARY;
4510 else
4511 type = CTRL_X_THESAURUS;
4512 if (*++e_cpt != ',' && *e_cpt != NUL)
4513 {
4514 dict = e_cpt;
4515 dict_f = DICT_FIRST;
4516 }
4517 }
4518#ifdef FEAT_FIND_ID
4519 else if (*e_cpt == 'i')
4520 type = CTRL_X_PATH_PATTERNS;
4521 else if (*e_cpt == 'd')
4522 type = CTRL_X_PATH_DEFINES;
4523#endif
4524 else if (*e_cpt == ']' || *e_cpt == 't')
4525 {
4526 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004527 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004528 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530 else
4531 type = -1;
4532
4533 /* in any case e_cpt is advanced to the next entry */
4534 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4535
4536 found_all = TRUE;
4537 if (type == -1)
4538 continue;
4539 }
4540 }
4541
Bram Moolenaar4475b622017-05-01 20:46:52 +02004542 /* If complete() was called then compl_pattern has been reset. The
4543 * following won't work then, bail out. */
4544 if (compl_pattern == NULL)
4545 break;
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 switch (type)
4548 {
4549 case -1:
4550 break;
4551#ifdef FEAT_FIND_ID
4552 case CTRL_X_PATH_PATTERNS:
4553 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004554 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4559 (linenr_T)1, (linenr_T)MAXLNUM);
4560 break;
4561#endif
4562
4563 case CTRL_X_DICTIONARY:
4564 case CTRL_X_THESAURUS:
4565 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004566 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 : (type == CTRL_X_THESAURUS
4568 ? (*curbuf->b_p_tsr == NUL
4569 ? p_tsr
4570 : curbuf->b_p_tsr)
4571 : (*curbuf->b_p_dict == NUL
4572 ? p_dict
4573 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004574 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004575 dict != NULL ? dict_f
4576 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 dict = NULL;
4578 break;
4579
4580 case CTRL_X_TAGS:
4581 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4582 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004585 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004586 * of matches is found when compl_pattern is empty */
4587 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004588 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4589 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4591 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004592 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594 p_ic = save_p_ic;
4595 break;
4596
4597 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4600 {
4601
4602 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004604 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 break;
4607
4608 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004609 if (expand_cmdline(&compl_xp, compl_pattern,
4610 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004612 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004615#ifdef FEAT_COMPL_FUNC
4616 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004617 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004618 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004619 break;
4620#endif
4621
Bram Moolenaar488c6512005-08-11 20:09:58 +00004622 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004623#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004624 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004625 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004626 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004627 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004628#endif
4629 break;
4630
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 default: /* normal ^P/^N and ^X^L */
4632 /*
4633 * If 'infercase' is set, don't use 'smartcase' here
4634 */
4635 save_p_scs = p_scs;
4636 if (ins_buf->b_p_inf)
4637 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638
Bram Moolenaar361aa502014-01-23 22:45:58 +01004639 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 * end but never from the middle, thus setting nowrapscan in this
4641 * buffers is a good idea, on the other hand, we always set
4642 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4643 save_p_ws = p_ws;
4644 if (ins_buf != curbuf)
4645 p_ws = FALSE;
4646 else if (*e_cpt == '.')
4647 p_ws = TRUE;
4648 for (;;)
4649 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004650 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004652 ++msg_silent; /* Don't want messages for wrapscan. */
4653
Bram Moolenaare4214502015-03-05 18:08:43 +01004654 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4655 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004656 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004657 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004658 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004660 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004662 found_new_match = searchit(NULL, ins_buf, pos,
4663 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004664 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004665 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004666 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004667 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004669 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004670 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 first_match_pos = *pos;
4672 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004673 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004676 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 found_new_match = FAIL;
4678 if (found_new_match == FAIL)
4679 {
4680 if (ins_buf == curbuf)
4681 found_all = TRUE;
4682 break;
4683 }
4684
4685 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004686 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 && ini->lnum == pos->lnum
4688 && ini->col == pos->col)
4689 continue;
4690 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004691 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004693 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4696 continue;
4697 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4698 if (!p_paste)
4699 ptr = skipwhite(ptr);
4700 }
4701 len = (int)STRLEN(ptr);
4702 }
4703 else
4704 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004705 char_u *tmp_ptr = ptr;
4706
4707 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004709 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 /* Skip if already inside a word. */
4711 if (vim_iswordp(tmp_ptr))
4712 continue;
4713 /* Find start of next word. */
4714 tmp_ptr = find_word_start(tmp_ptr);
4715 }
4716 /* Find end of this word. */
4717 tmp_ptr = find_word_end(tmp_ptr);
4718 len = (int)(tmp_ptr - ptr);
4719
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004720 if ((compl_cont_status & CONT_ADDING)
4721 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
4723 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4724 {
4725 /* Try next line, if any. the new word will be
4726 * "join" as if the normal command "J" was used.
4727 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004728 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 * works -- Acevedo */
4730 STRNCPY(IObuff, ptr, len);
4731 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4732 tmp_ptr = ptr = skipwhite(ptr);
4733 /* Find start of next word. */
4734 tmp_ptr = find_word_start(tmp_ptr);
4735 /* Find end of next word. */
4736 tmp_ptr = find_word_end(tmp_ptr);
4737 if (tmp_ptr > ptr)
4738 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004739 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004741 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 IObuff[len++] = ' ';
4743 /* IObuf =~ "\k.* ", thus len >= 2 */
4744 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004745 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 || (vim_strchr(p_cpo, CPO_JOINSP)
4747 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004748 && (IObuff[len - 2] == '?'
4749 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 IObuff[len++] = ' ';
4751 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004752 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 if (tmp_ptr - ptr >= IOSIZE - len)
4754 tmp_ptr = ptr + IOSIZE - len - 1;
4755 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4756 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004757 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
4759 IObuff[len] = NUL;
4760 ptr = IObuff;
4761 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004762 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 continue;
4764 }
4765 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004766 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004767 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004768 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 found_new_match = OK;
4771 break;
4772 }
4773 }
4774 p_scs = save_p_scs;
4775 p_ws = save_p_ws;
4776 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004777
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004778 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004779 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004780 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 found_new_match = OK;
4782
4783 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004784 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4785 * match */
4786 if ((ctrl_x_mode != CTRL_X_NORMAL
4787 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004788 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004789 {
4790 if (got_int)
4791 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004792 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004793 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004794 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004795
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004796 if ((ctrl_x_mode != CTRL_X_NORMAL
4797 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004798 || compl_interrupted)
4799 break;
4800 compl_started = TRUE;
4801 }
4802 else
4803 {
4804 /* Mark a buffer scanned when it has been scanned completely */
4805 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4806 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004808 compl_started = FALSE;
4809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004811 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004813 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 && *e_cpt == NUL) /* Got to end of 'complete' */
4815 found_new_match = FAIL;
4816
4817 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004818 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4819 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 i = ins_compl_make_cyclic();
4821
Bram Moolenaar4475b622017-05-01 20:46:52 +02004822 if (compl_old_match != NULL)
4823 {
4824 /* If several matches were added (FORWARD) or the search failed and has
4825 * just been made cyclic then we have to move compl_curr_match to the
4826 * next or previous entry (if any) -- Acevedo */
4827 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4828 : compl_old_match->cp_prev;
4829 if (compl_curr_match == NULL)
4830 compl_curr_match = compl_old_match;
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 return i;
4833}
4834
4835/* Delete the old text being completed. */
4836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004837ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004839 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
4841 /*
4842 * In insert mode: Delete the typed part.
4843 * In replace mode: Put the old characters back, if any.
4844 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004845 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4846 if ((int)curwin->w_cursor.col > col)
4847 {
4848 if (stop_arrow() == FAIL)
4849 return;
4850 backspace_until_column(col);
4851 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004852
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004853 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4854 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004856 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004857 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858}
4859
Bram Moolenaar472e8592016-10-15 17:06:47 +02004860/*
4861 * Insert the new text being completed.
4862 * "in_compl_func" is TRUE when called from complete_check().
4863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004865ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004867 dict_T *dict;
4868
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004869 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004870 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4871 compl_used_match = FALSE;
4872 else
4873 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004874
4875 /* Set completed item. */
4876 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004877 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004878 if (dict != NULL)
4879 {
4880 dict_add_nr_str(dict, "word", 0L,
4881 EMPTY_IF_NULL(compl_shown_match->cp_str));
4882 dict_add_nr_str(dict, "abbr", 0L,
4883 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4884 dict_add_nr_str(dict, "menu", 0L,
4885 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4886 dict_add_nr_str(dict, "kind", 0L,
4887 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4888 dict_add_nr_str(dict, "info", 0L,
4889 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004890 dict_add_nr_str(dict, "user_data", 0L,
4891 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004892 }
4893 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004894 if (!in_compl_func)
4895 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896}
4897
4898/*
4899 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004900 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4901 * get more completions. If it is FALSE, then we just do nothing when there
4902 * are no more completions in a given direction. The latter case is used when
4903 * we are still in the middle of finding completions, to allow browsing
4904 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 * Return the total number of matches, or -1 if still unknown -- webb.
4906 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004907 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4908 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 *
4910 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004911 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4912 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 */
4914 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004915ins_compl_next(
4916 int allow_get_expansion,
4917 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004918 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004919 int insert_match, /* Insert the newly selected match */
4920 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
4922 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004923 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004924 compl_T *found_compl = NULL;
4925 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004926 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004927 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004929 /* When user complete function return -1 for findstart which is next
4930 * time of 'always', compl_shown_match become NULL. */
4931 if (compl_shown_match == NULL)
4932 return -1;
4933
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004934 if (compl_leader != NULL
4935 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004937 /* Set "compl_shown_match" to the actually shown match, it may differ
4938 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004939 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004940 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004941 && compl_shown_match->cp_next != NULL
4942 && compl_shown_match->cp_next != compl_first_match)
4943 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004944
4945 /* If we didn't find it searching forward, and compl_shows_dir is
4946 * backward, find the last match. */
4947 if (compl_shows_dir == BACKWARD
4948 && !ins_compl_equal(compl_shown_match,
4949 compl_leader, (int)STRLEN(compl_leader))
4950 && (compl_shown_match->cp_next == NULL
4951 || compl_shown_match->cp_next == compl_first_match))
4952 {
4953 while (!ins_compl_equal(compl_shown_match,
4954 compl_leader, (int)STRLEN(compl_leader))
4955 && compl_shown_match->cp_prev != NULL
4956 && compl_shown_match->cp_prev != compl_first_match)
4957 compl_shown_match = compl_shown_match->cp_prev;
4958 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004959 }
4960
4961 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004962 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 /* Delete old text to be replaced */
4964 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004965
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004966 /* When finding the longest common text we stick at the original text,
4967 * don't let CTRL-N or CTRL-P move to the first match. */
4968 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4969
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004970 /* When restarting the search don't insert the first match either. */
4971 if (compl_restarting)
4972 {
4973 advance = FALSE;
4974 compl_restarting = FALSE;
4975 }
4976
Bram Moolenaare3226be2005-12-18 22:10:00 +00004977 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4978 * around. */
4979 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004981 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004983 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004984 found_end = (compl_first_match != NULL
4985 && (compl_shown_match->cp_next == compl_first_match
4986 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004987 }
4988 else if (compl_shows_dir == BACKWARD
4989 && compl_shown_match->cp_prev != NULL)
4990 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004991 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004992 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004993 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004996 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004997 if (!allow_get_expansion)
4998 {
4999 if (advance)
5000 {
5001 if (compl_shows_dir == BACKWARD)
5002 compl_pending -= todo + 1;
5003 else
5004 compl_pending += todo + 1;
5005 }
5006 return -1;
5007 }
5008
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005009 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005010 {
5011 if (compl_shows_dir == BACKWARD)
5012 --compl_pending;
5013 else
5014 ++compl_pending;
5015 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005016
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005017 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005018 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005019
5020 /* handle any pending completions */
5021 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005022 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005023 {
5024 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5025 {
5026 compl_shown_match = compl_shown_match->cp_next;
5027 --compl_pending;
5028 }
5029 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5030 {
5031 compl_shown_match = compl_shown_match->cp_prev;
5032 ++compl_pending;
5033 }
5034 else
5035 break;
5036 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005037 found_end = FALSE;
5038 }
5039 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5040 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005041 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005042 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005043 ++todo;
5044 else
5045 /* Remember a matching item. */
5046 found_compl = compl_shown_match;
5047
5048 /* Stop at the end of the list when we found a usable match. */
5049 if (found_end)
5050 {
5051 if (found_compl != NULL)
5052 {
5053 compl_shown_match = found_compl;
5054 break;
5055 }
5056 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005060 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005061 if (compl_no_insert && !started)
5062 {
5063 ins_bytes(compl_orig_text + ins_compl_len());
5064 compl_used_match = FALSE;
5065 }
5066 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005067 {
5068 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005069 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005070 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005071 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005072 }
5073 else
5074 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
5076 if (!allow_get_expansion)
5077 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005078 /* may undisplay the popup menu first */
5079 ins_compl_upd_pum();
5080
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005081 /* redraw to show the user what was inserted */
5082 update_screen(0);
5083
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005084 /* display the updated popup menu */
5085 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005086#ifdef FEAT_GUI
5087 if (gui.in_use)
5088 {
5089 /* Show the cursor after the match, not after the redrawn text. */
5090 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005091 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005092 }
5093#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005094
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 /* Delete old text to be replaced, since we're still searching and
5096 * don't want to match ourselves! */
5097 ins_compl_delete();
5098 }
5099
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005100 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005101 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005102 if (compl_no_insert && !started)
5103 compl_enter_selects = TRUE;
5104 else
5105 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 /*
5108 * Show the file name for the match (if any)
5109 * Truncate the file name to avoid a wait for return.
5110 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005111 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005113 char *lead = _("match in file");
5114 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5115 char_u *s;
5116 char_u *e;
5117
5118 if (space > 0)
5119 {
5120 /* We need the tail that fits. With double-byte encoding going
5121 * back from the end is very slow, thus go from the start and keep
5122 * the text that fits in "space" between "s" and "e". */
5123 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5124 {
5125 space -= ptr2cells(e);
5126 while (space < 0)
5127 {
5128 space += ptr2cells(s);
5129 MB_PTR_ADV(s);
5130 }
5131 }
5132 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5133 s > compl_shown_match->cp_fname ? "<" : "", s);
5134 msg(IObuff);
5135 redraw_cmdline = FALSE; /* don't overwrite! */
5136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138
5139 return num_matches;
5140}
5141
5142/*
5143 * Call this while finding completions, to check whether the user has hit a key
5144 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005145 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005147 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005148 * "in_compl_func" is TRUE when called from complete_check(), don't set
5149 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
5151 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005152ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005155 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005157 /* Don't check when reading keys from a script, :normal or feedkeys().
5158 * That would break the test scripts. But do check for keys when called
5159 * from complete_check(). */
5160 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 return;
5162
5163 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005164 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 return;
5166 count = 0;
5167
Bram Moolenaara260a972006-06-23 19:36:29 +00005168 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5169 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (c != NUL)
5172 {
5173 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5174 {
5175 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005176 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005177 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005178 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005180 else
5181 {
5182 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005183 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005184 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005185 if (c != K_IGNORE)
5186 {
5187 /* Don't interrupt completion when the character wasn't typed,
5188 * e.g., when doing @q to replay keys. */
5189 if (c != Ctrl_R && KeyTyped)
5190 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005191
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005192 vungetc(c);
5193 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005196 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005197 {
5198 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5199
5200 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005201 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005202 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005203}
5204
5205/*
5206 * Decide the direction of Insert mode complete from the key typed.
5207 * Returns BACKWARD or FORWARD.
5208 */
5209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005210ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005211{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005212 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005213 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005214 return BACKWARD;
5215 return FORWARD;
5216}
5217
5218/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005219 * Return TRUE for keys that are used for completion only when the popup menu
5220 * is visible.
5221 */
5222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005223ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005224{
5225 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005226 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5227 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005228}
5229
5230/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005231 * Decide the number of completions to move forward.
5232 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5233 */
5234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005235ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005236{
5237 int h;
5238
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005239 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005240 {
5241 h = pum_get_height();
5242 if (h > 3)
5243 h -= 2; /* keep some context */
5244 return h;
5245 }
5246 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247}
5248
5249/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005250 * Return TRUE if completion with "c" should insert the match, FALSE if only
5251 * to change the currently selected completion.
5252 */
5253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005254ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005255{
5256 switch (c)
5257 {
5258 case K_UP:
5259 case K_DOWN:
5260 case K_PAGEDOWN:
5261 case K_KPAGEDOWN:
5262 case K_S_DOWN:
5263 case K_PAGEUP:
5264 case K_KPAGEUP:
5265 case K_S_UP:
5266 return FALSE;
5267 }
5268 return TRUE;
5269}
5270
5271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 * Do Insert mode completion.
5273 * Called when character "c" was typed, which has a meaning for completion.
5274 * Returns OK if completion was done, FAIL if something failed (out of mem).
5275 */
5276 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005277ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005279 char_u *line;
5280 int startcol = 0; /* column where searched text starts */
5281 colnr_T curs_col; /* cursor column */
5282 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005283 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005284 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005285 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005286 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
Bram Moolenaare3226be2005-12-18 22:10:00 +00005288 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005289 insert_match = ins_compl_use_match(c);
5290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
5293 /* First time we hit ^N or ^P (in a row, I mean) */
5294
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 did_ai = FALSE;
5296#ifdef FEAT_SMARTINDENT
5297 did_si = FALSE;
5298 can_si = FALSE;
5299 can_si_back = FALSE;
5300#endif
5301 if (stop_arrow() == FAIL)
5302 return FAIL;
5303
5304 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005306 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005308 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 * "compl_startpos" to the cursor as a pattern to add a new word
5310 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005311 * "compl_startpos" is not in the same line as the cursor then fix it
5312 * (the line has been split because it was longer than 'tw'). if SOL
5313 * is set then skip the previous pattern, a word at the beginning of
5314 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005315 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5316 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
5318 /*
5319 * it is a continued search
5320 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005321 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005322 if (ctrl_x_mode == CTRL_X_NORMAL
5323 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5324 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 /* line (probably) wrapped, set compl_startpos to the
5329 * first non_blank in the line, if it is not a wordchar
5330 * include it to get a better pattern, but then we don't
5331 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005332 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 compl_startpos.col = compl_col;
5334 compl_startpos.lnum = curwin->w_cursor.lnum;
5335 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337 else
5338 {
5339 /* S_IPOS was set when we inserted a word that was at the
5340 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 * mode but first we need to redefine compl_startpos */
5342 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 compl_cont_status |= CONT_SOL;
5345 compl_startpos.col = (colnr_T)(skipwhite(
5346 line + compl_length
5347 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005352 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005353 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005357 compl_cont_status &= ~CONT_SOL;
5358 compl_length = (IOSIZE - MIN_SPACE);
5359 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005361 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5362 if (compl_length < 1)
5363 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005365 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005366 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005371 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005376 if (ctrl_x_mode != CTRL_X_NORMAL)
5377 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005378 compl_cont_status = 0;
5379 compl_cont_status |= CONT_N_ADDS;
5380 compl_startpos = curwin->w_cursor;
5381 startcol = (int)curs_col;
5382 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384
5385 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005386 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005388 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5390 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005391 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005393 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005395 compl_col += ++startcol;
5396 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 compl_pattern = str_foldcase(line + compl_col,
5400 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005402 compl_pattern = vim_strnsave(line + compl_col,
5403 compl_length);
5404 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 return FAIL;
5406 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 char_u *prefix = (char_u *)"\\<";
5410
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005411 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005413 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005416 if (!vim_iswordp(line + compl_col)
5417 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 && (
5419#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005420 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005422 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423#endif
5424 )))
5425 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 STRCPY((char *)compl_pattern, prefix);
5427 (void)quote_meta(compl_pattern + STRLEN(prefix),
5428 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005430 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005434 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435#endif
5436 )
5437 {
5438 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005439 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5440 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005442 compl_col += curs_col;
5443 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445 else
5446 {
5447#ifdef FEAT_MBYTE
5448 /* Search the point of change class of multibyte character
5449 * or not a word single byte character backward. */
5450 if (has_mbyte)
5451 {
5452 int base_class;
5453 int head_off;
5454
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 startcol -= (*mb_head_off)(line, line + startcol);
5456 base_class = mb_get_class(line + startcol);
5457 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005459 head_off = (*mb_head_off)(line, line + startcol);
5460 if (base_class != mb_get_class(line + startcol
5461 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005463 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465 }
5466 else
5467#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 compl_col += ++startcol;
5471 compl_length = (int)curs_col - startcol;
5472 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 {
5474 /* Only match word with at least two chars -- webb
5475 * there's no need to call quote_meta,
5476 * alloc(7) is enough -- Acevedo
5477 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005478 compl_pattern = alloc(7);
5479 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 STRCPY((char *)compl_pattern, "\\<");
5482 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5483 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485 else
5486 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005487 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005488 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005489 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005491 STRCPY((char *)compl_pattern, "\\<");
5492 (void)quote_meta(compl_pattern + 2, line + compl_col,
5493 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495 }
5496 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005497 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005499 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005500 compl_length = (int)curs_col - (int)compl_col;
5501 if (compl_length < 0) /* cursor in indent: empty pattern */
5502 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005504 compl_pattern = str_foldcase(line + compl_col, compl_length,
5505 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005507 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5508 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 return FAIL;
5510 }
5511 else if (ctrl_x_mode == CTRL_X_FILES)
5512 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005513 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005514 if (startcol > 0)
5515 {
5516 char_u *p = line + startcol;
5517
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005518 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005519 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005520 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005521 if (p == line && vim_isfilec(PTR2CHAR(p)))
5522 startcol = 0;
5523 else
5524 startcol = (int)(p - line) + 1;
5525 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005526
Bram Moolenaar0300e462013-09-08 16:03:45 +02005527 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005528 compl_length = (int)curs_col - startcol;
5529 compl_pattern = addstar(line + compl_col, compl_length,
5530 EXPAND_FILES);
5531 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 return FAIL;
5533 }
5534 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5535 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005536 compl_pattern = vim_strnsave(line, curs_col);
5537 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005539 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005540 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005541 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5542 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005543 /* No completion possible, use an empty pattern to get a
5544 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005545 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005546 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005547 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5548 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005550 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005551 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005552#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005553 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005554 * Call user defined function 'completefunc' with "a:findstart"
5555 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005556 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005557 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005558 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005559 char_u *funcname;
5560 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005561 win_T *curwin_save;
5562 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005563
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005564 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005565 * string */
5566 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5567 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5568 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005569 {
5570 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5571 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005572 /* restore did_ai, so that adding comment leader works */
5573 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005574 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005575 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005576
Bram Moolenaarffa96842018-06-12 22:05:14 +02005577 args[0].v_type = VAR_NUMBER;
5578 args[0].vval.v_number = 1;
5579 args[1].v_type = VAR_STRING;
5580 args[1].vval.v_string = (char_u *)"";
5581 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005582 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005583 curwin_save = curwin;
5584 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005585 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005586 if (curwin_save != curwin || curbuf_save != curbuf)
5587 {
5588 EMSG(_(e_complwin));
5589 return FAIL;
5590 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005591 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005592 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005593 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005594 {
5595 EMSG(_(e_compldel));
5596 return FAIL;
5597 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005598
Bram Moolenaar6110a002012-01-26 18:58:38 +01005599 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005600 * cancel the complete without an error.
5601 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005602 if (col == -2)
5603 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005604 if (col == -3)
5605 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005606 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005607 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005608 if (!shortmess(SHM_COMPLETIONMENU))
5609 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005610 return FAIL;
5611 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005612
Bram Moolenaar82139082011-09-14 16:52:09 +02005613 /*
5614 * Reset extended parameters of completion, when start new
5615 * completion.
5616 */
5617 compl_opt_refresh_always = FALSE;
5618
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005619 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005620 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005621 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005622 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005623 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005624
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005625 /* Setup variables for completion. Need to obtain "line" again,
5626 * it may have become invalid. */
5627 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005628 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005629 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5630 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005631#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005632 return FAIL;
5633 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005634 else if (ctrl_x_mode == CTRL_X_SPELL)
5635 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005636#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005637 if (spell_bad_len > 0)
5638 compl_col = curs_col - spell_bad_len;
5639 else
5640 compl_col = spell_word_start(startcol);
5641 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005642 {
5643 compl_length = 0;
5644 compl_col = curs_col;
5645 }
5646 else
5647 {
5648 spell_expand_check_cap(compl_col);
5649 compl_length = (int)curs_col - compl_col;
5650 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005651 /* Need to obtain "line" again, it may have become invalid. */
5652 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005653 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5654 if (compl_pattern == NULL)
5655#endif
5656 return FAIL;
5657 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005658 else
5659 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005660 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005661 return FAIL;
5662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005664 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
5666 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005667 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 /* Insert a new line, keep indentation but ignore 'comments' */
5670#ifdef FEAT_COMMENTS
5671 char_u *old = curbuf->b_p_com;
5672
5673 curbuf->b_p_com = (char_u *)"";
5674#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005675 compl_startpos.lnum = curwin->w_cursor.lnum;
5676 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 ins_eol('\r');
5678#ifdef FEAT_COMMENTS
5679 curbuf->b_p_com = old;
5680#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005681 compl_length = 0;
5682 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 }
5685 else
5686 {
5687 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005688 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005691 if (compl_cont_status & CONT_LOCAL)
5692 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 else
5694 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5695
Bram Moolenaar62951b12011-09-21 18:23:05 +02005696 /* If any of the original typed text has been changed we need to fix
5697 * the redo buffer. */
5698 ins_compl_fixRedoBufForLeader(NULL);
5699
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005700 /* Always add completion for the original text. */
5701 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005702 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5703 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005704 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005706 VIM_CLEAR(compl_pattern);
5707 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 return FAIL;
5709 }
5710
5711 /* showmode might reset the internal line pointers, so it must
5712 * be called before line = ml_get(), or when this address is no
5713 * longer needed. -- Acevedo.
5714 */
5715 edit_submode_extra = (char_u *)_("-- Searching...");
5716 edit_submode_highl = HLF_COUNT;
5717 showmode();
5718 edit_submode_extra = NULL;
5719 out_flush();
5720 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005721 else if (insert_match && stop_arrow() == FAIL)
5722 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005724 compl_shown_match = compl_curr_match;
5725 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
5727 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005728 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005730 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005731 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005732 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005734 /* may undisplay the popup menu */
5735 ins_compl_upd_pum();
5736
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005737 if (n > 1) /* all matches have been found */
5738 compl_matches = n;
5739 compl_curr_match = compl_shown_match;
5740 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaard68071d2006-05-02 22:08:30 +00005742 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5743 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 if (got_int && !global_busy)
5745 {
5746 (void)vgetc();
5747 got_int = FALSE;
5748 }
5749
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005750 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005751 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005753 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5754 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5756 edit_submode_highl = HLF_E;
5757 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5758 * because we couldn't expand anything at first place, but if we used
5759 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5760 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005761 if ( compl_length > 1
5762 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005763 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5765 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005766 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
5768
Bram Moolenaar572cb562005-08-05 21:35:02 +00005769 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005770 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005772 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
5774 if (edit_submode_extra == NULL)
5775 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005776 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 {
5778 edit_submode_extra = (char_u *)_("Back at original");
5779 edit_submode_highl = HLF_W;
5780 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005781 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
5783 edit_submode_extra = (char_u *)_("Word from other line");
5784 edit_submode_highl = HLF_COUNT;
5785 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005786 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 edit_submode_extra = (char_u *)_("The only match");
5789 edit_submode_highl = HLF_COUNT;
5790 }
5791 else
5792 {
5793 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005794 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005796 int number = 0;
5797 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005799 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
5801 /* search backwards for the first valid (!= -1) number.
5802 * This should normally succeed already at the first loop
5803 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005804 for (match = compl_curr_match->cp_prev; match != NULL
5805 && match != compl_first_match;
5806 match = match->cp_prev)
5807 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005809 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 break;
5811 }
5812 if (match != NULL)
5813 /* go up and assign all numbers which are not assigned
5814 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005815 for (match = match->cp_next;
5816 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005817 match = match->cp_next)
5818 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 }
5820 else /* BACKWARD */
5821 {
5822 /* search forwards (upwards) for the first valid (!= -1)
5823 * number. This should normally succeed already at the
5824 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005825 for (match = compl_curr_match->cp_next; match != NULL
5826 && match != compl_first_match;
5827 match = match->cp_next)
5828 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005830 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 break;
5832 }
5833 if (match != NULL)
5834 /* go down and assign all numbers which are not
5835 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005836 for (match = match->cp_prev; match
5837 && match->cp_number == -1;
5838 match = match->cp_prev)
5839 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 }
5841 }
5842
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005843 /* The match should always have a sequence number now, this is
5844 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005845 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005847 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5848 * Translations may need more than twice that. */
5849 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005851 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005852 vim_snprintf((char *)match_ref, sizeof(match_ref),
5853 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005854 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005856 vim_snprintf((char *)match_ref, sizeof(match_ref),
5857 _("match %d"),
5858 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 edit_submode_extra = match_ref;
5860 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005861 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 curs_columns(FALSE);
5863 }
5864 }
5865 }
5866
5867 /* Show a message about what (completion) mode we're in. */
5868 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005869 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005871 if (edit_submode_extra != NULL)
5872 {
5873 if (!p_smd)
5874 msg_attr(edit_submode_extra,
5875 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005876 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005877 }
5878 else
5879 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
Bram Moolenaard68071d2006-05-02 22:08:30 +00005882 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005883 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005884 show_pum(save_w_wrow, save_w_leftcol);
5885
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005886 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005887 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005888
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 return OK;
5890}
5891
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005892 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005893show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005894{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005895 /* RedrawingDisabled may be set when invoked through complete(). */
5896 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005897
Bram Moolenaarcb036422017-03-01 12:29:10 +01005898 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005899
Bram Moolenaarcb036422017-03-01 12:29:10 +01005900 /* If the cursor moved or the display scrolled we need to remove the pum
5901 * first. */
5902 setcursor();
5903 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5904 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005905
Bram Moolenaarcb036422017-03-01 12:29:10 +01005906 ins_compl_show_pum();
5907 setcursor();
5908 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005909}
5910
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911/*
5912 * Looks in the first "len" chars. of "src" for search-metachars.
5913 * If dest is not NULL the chars. are copied there quoting (with
5914 * a backslash) the metachars, and dest would be NUL terminated.
5915 * Returns the length (needed) of dest
5916 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005917 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005918quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005920 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005922 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 {
5924 switch (*src)
5925 {
5926 case '.':
5927 case '*':
5928 case '[':
5929 if (ctrl_x_mode == CTRL_X_DICTIONARY
5930 || ctrl_x_mode == CTRL_X_THESAURUS)
5931 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005932 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 case '~':
5934 if (!p_magic) /* quote these only if magic is set */
5935 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005936 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 case '\\':
5938 if (ctrl_x_mode == CTRL_X_DICTIONARY
5939 || ctrl_x_mode == CTRL_X_THESAURUS)
5940 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005941 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 case '^': /* currently it's not needed. */
5943 case '$':
5944 m++;
5945 if (dest != NULL)
5946 *dest++ = '\\';
5947 break;
5948 }
5949 if (dest != NULL)
5950 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005951# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 /* Copy remaining bytes of a multibyte character. */
5953 if (has_mbyte)
5954 {
5955 int i, mb_len;
5956
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005957 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (mb_len > 0 && len >= mb_len)
5959 for (i = 0; i < mb_len; ++i)
5960 {
5961 --len;
5962 ++src;
5963 if (dest != NULL)
5964 *dest++ = *src;
5965 }
5966 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 }
5969 if (dest != NULL)
5970 *dest = NUL;
5971
5972 return m;
5973}
5974#endif /* FEAT_INS_EXPAND */
5975
5976/*
5977 * Next character is interpreted literally.
5978 * A one, two or three digit decimal number is interpreted as its byte value.
5979 * If one or two digits are entered, the next character is given to vungetc().
5980 * For Unicode a character > 255 may be returned.
5981 */
5982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005983get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 int cc;
5986 int nc;
5987 int i;
5988 int hex = FALSE;
5989 int octal = FALSE;
5990#ifdef FEAT_MBYTE
5991 int unicode = 0;
5992#endif
5993
5994 if (got_int)
5995 return Ctrl_C;
5996
5997#ifdef FEAT_GUI
5998 /*
5999 * In GUI there is no point inserting the internal code for a special key.
6000 * It is more useful to insert the string "<KEY>" instead. This would
6001 * probably be useful in a text window too, but it would not be
6002 * vi-compatible (maybe there should be an option for it?) -- webb
6003 */
6004 if (gui.in_use)
6005 ++allow_keys;
6006#endif
6007#ifdef USE_ON_FLY_SCROLL
6008 dont_scroll = TRUE; /* disallow scrolling here */
6009#endif
6010 ++no_mapping; /* don't map the next key hits */
6011 cc = 0;
6012 i = 0;
6013 for (;;)
6014 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006015 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016#ifdef FEAT_CMDL_INFO
6017 if (!(State & CMDLINE)
6018# ifdef FEAT_MBYTE
6019 && MB_BYTE2LEN_CHECK(nc) == 1
6020# endif
6021 )
6022 add_to_showcmd(nc);
6023#endif
6024 if (nc == 'x' || nc == 'X')
6025 hex = TRUE;
6026 else if (nc == 'o' || nc == 'O')
6027 octal = TRUE;
6028#ifdef FEAT_MBYTE
6029 else if (nc == 'u' || nc == 'U')
6030 unicode = nc;
6031#endif
6032 else
6033 {
6034 if (hex
6035#ifdef FEAT_MBYTE
6036 || unicode != 0
6037#endif
6038 )
6039 {
6040 if (!vim_isxdigit(nc))
6041 break;
6042 cc = cc * 16 + hex2nr(nc);
6043 }
6044 else if (octal)
6045 {
6046 if (nc < '0' || nc > '7')
6047 break;
6048 cc = cc * 8 + nc - '0';
6049 }
6050 else
6051 {
6052 if (!VIM_ISDIGIT(nc))
6053 break;
6054 cc = cc * 10 + nc - '0';
6055 }
6056
6057 ++i;
6058 }
6059
6060 if (cc > 255
6061#ifdef FEAT_MBYTE
6062 && unicode == 0
6063#endif
6064 )
6065 cc = 255; /* limit range to 0-255 */
6066 nc = 0;
6067
6068 if (hex) /* hex: up to two chars */
6069 {
6070 if (i >= 2)
6071 break;
6072 }
6073#ifdef FEAT_MBYTE
6074 else if (unicode) /* Unicode: up to four or eight chars */
6075 {
6076 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6077 break;
6078 }
6079#endif
6080 else if (i >= 3) /* decimal or octal: up to three chars */
6081 break;
6082 }
6083 if (i == 0) /* no number entered */
6084 {
6085 if (nc == K_ZERO) /* NUL is stored as NL */
6086 {
6087 cc = '\n';
6088 nc = 0;
6089 }
6090 else
6091 {
6092 cc = nc;
6093 nc = 0;
6094 }
6095 }
6096
6097 if (cc == 0) /* NUL is stored as NL */
6098 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006099#ifdef FEAT_MBYTE
6100 if (enc_dbcs && (cc & 0xff) == 0)
6101 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6102 second byte will cause trouble! */
6103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
6105 --no_mapping;
6106#ifdef FEAT_GUI
6107 if (gui.in_use)
6108 --allow_keys;
6109#endif
6110 if (nc)
6111 vungetc(nc);
6112 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6113 return cc;
6114}
6115
6116/*
6117 * Insert character, taking care of special keys and mod_mask
6118 */
6119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120insert_special(
6121 int c,
6122 int allow_modmask,
6123 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 char_u *p;
6126 int len;
6127
6128 /*
6129 * Special function key, translate into "<Key>". Up to the last '>' is
6130 * inserted with ins_str(), so as not to replace characters in replace
6131 * mode.
6132 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6133 * unless 'allow_modmask' is TRUE.
6134 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006135#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 /* Command-key never produces a normal key */
6137 if (mod_mask & MOD_MASK_CMD)
6138 allow_modmask = TRUE;
6139#endif
6140 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6141 {
6142 p = get_special_key_name(c, mod_mask);
6143 len = (int)STRLEN(p);
6144 c = p[len - 1];
6145 if (len > 2)
6146 {
6147 if (stop_arrow() == FAIL)
6148 return;
6149 p[len - 1] = NUL;
6150 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006151 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 ctrlv = FALSE;
6153 }
6154 }
6155 if (stop_arrow() == OK)
6156 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6157}
6158
6159/*
6160 * Special characters in this context are those that need processing other
6161 * than the simple insertion that can be performed here. This includes ESC
6162 * which terminates the insert, and CR/NL which need special processing to
6163 * open up a new line. This routine tries to optimize insertions performed by
6164 * the "redo", "undo" or "put" commands, so it needs to know when it should
6165 * stop and defer processing to the "normal" mechanism.
6166 * '0' and '^' are special, because they can be followed by CTRL-D.
6167 */
6168#ifdef EBCDIC
6169# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6170#else
6171# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6172#endif
6173
6174#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006175# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006177# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178#endif
6179
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006180/*
6181 * "flags": INSCHAR_FORMAT - force formatting
6182 * INSCHAR_CTRLV - char typed just after CTRL-V
6183 * INSCHAR_NO_FEX - don't use 'formatexpr'
6184 *
6185 * NOTE: passes the flags value straight through to internal_format() which,
6186 * beside INSCHAR_FORMAT (above), is also looking for these:
6187 * INSCHAR_DO_COM - format comments
6188 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006191insertchar(
6192 int c, /* character to insert or NUL */
6193 int flags, /* INSCHAR_FORMAT, etc. */
6194 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 int textwidth;
6197#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006201 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006203 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
6206 /*
6207 * Try to break the line in two or more pieces when:
6208 * - Always do this if we have been called to do formatting only.
6209 * - Always do this when 'formatoptions' has the 'a' flag and the line
6210 * ends in white space.
6211 * - Otherwise:
6212 * - Don't do this if inserting a blank
6213 * - Don't do this if an existing character is being replaced, unless
6214 * we're in VREPLACE mode.
6215 * - Do this if the cursor is not on the line where insert started
6216 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6217 * before the insert.
6218 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6219 * before 'textwidth'
6220 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006222 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006223 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 && !((State & REPLACE_FLAG)
6225#ifdef FEAT_VREPLACE
6226 && !(State & VREPLACE_FLAG)
6227#endif
6228 && *ml_get_cursor() != NUL)
6229 && (curwin->w_cursor.lnum != Insstart.lnum
6230 || ((!has_format_option(FO_INS_LONG)
6231 || Insstart_textlen <= (colnr_T)textwidth)
6232 && (!fo_ins_blank
6233 || Insstart_blank_vcol <= (colnr_T)textwidth
6234 ))))))
6235 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006236 /* Format with 'formatexpr' when it's set. Use internal formatting
6237 * when 'formatexpr' isn't set or it returns non-zero. */
6238#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006239 int do_internal = TRUE;
6240 colnr_T virtcol = get_nolist_virtcol()
6241 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006242
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006243 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6244 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006245 {
6246 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6247 /* It may be required to save for undo again, e.g. when setline()
6248 * was called. */
6249 ins_need_undo = TRUE;
6250 }
6251 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006253 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006255
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 if (c == NUL) /* only formatting was wanted */
6257 return;
6258
6259#ifdef FEAT_COMMENTS
6260 /* Check whether this character should end a comment. */
6261 if (did_ai && (int)c == end_comment_pending)
6262 {
6263 char_u *line;
6264 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6265 int middle_len, end_len;
6266 int i;
6267
6268 /*
6269 * Need to remove existing (middle) comment leader and insert end
6270 * comment leader. First, check what comment leader we can find.
6271 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006272 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6274 {
6275 /* Skip middle-comment string */
6276 while (*p && p[-1] != ':') /* find end of middle flags */
6277 ++p;
6278 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6279 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006280 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 --middle_len;
6282
6283 /* Find the end-comment string */
6284 while (*p && p[-1] != ':') /* find end of end flags */
6285 ++p;
6286 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6287
6288 /* Skip white space before the cursor */
6289 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006290 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 ;
6292 i++;
6293
6294 /* Skip to before the middle leader */
6295 i -= middle_len;
6296
6297 /* Check some expected things before we go on */
6298 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6299 {
6300 /* Backspace over all the stuff we want to replace */
6301 backspace_until_column(i);
6302
6303 /*
6304 * Insert the end-comment string, except for the last
6305 * character, which will get inserted as normal later.
6306 */
6307 ins_bytes_len(lead_end, end_len - 1);
6308 }
6309 }
6310 }
6311 end_comment_pending = NUL;
6312#endif
6313
6314 did_ai = FALSE;
6315#ifdef FEAT_SMARTINDENT
6316 did_si = FALSE;
6317 can_si = FALSE;
6318 can_si_back = FALSE;
6319#endif
6320
6321 /*
6322 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6323 * This speeds up normal text input considerably.
6324 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6325 * need to re-indent at a ':', or any other character (but not what
6326 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006327 * Don't do this when there an InsertCharPre autocommand is defined,
6328 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006329 * Do the check for InsertCharPre before the call to vpeekc() because the
6330 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 */
6332#ifdef USE_ON_FLY_SCROLL
6333 dont_scroll = FALSE; /* allow scrolling here */
6334#endif
6335
6336 if ( !ISSPECIAL(c)
6337#ifdef FEAT_MBYTE
6338 && (!has_mbyte || (*mb_char2len)(c) == 1)
6339#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006340 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 && vpeekc() != NUL
6342 && !(State & REPLACE_FLAG)
6343#ifdef FEAT_CINDENT
6344 && !cindent_on()
6345#endif
6346#ifdef FEAT_RIGHTLEFT
6347 && !p_ri
6348#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006349 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 {
6351#define INPUT_BUFLEN 100
6352 char_u buf[INPUT_BUFLEN + 1];
6353 int i;
6354 colnr_T virtcol = 0;
6355
6356 buf[0] = c;
6357 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 virtcol = get_nolist_virtcol();
6360 /*
6361 * Stop the string when:
6362 * - no more chars available
6363 * - finding a special character (command key)
6364 * - buffer is full
6365 * - running into the 'textwidth' boundary
6366 * - need to check for abbreviation: A non-word char after a word-char
6367 */
6368 while ( (c = vpeekc()) != NUL
6369 && !ISSPECIAL(c)
6370#ifdef FEAT_MBYTE
6371 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6372#endif
6373 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006374# ifdef FEAT_FKMAP
6375 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 && (textwidth == 0
6378 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6379 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6380 {
6381#ifdef FEAT_RIGHTLEFT
6382 c = vgetc();
6383 if (p_hkmap && KeyTyped)
6384 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 buf[i++] = c;
6386#else
6387 buf[i++] = vgetc();
6388#endif
6389 }
6390
6391#ifdef FEAT_DIGRAPHS
6392 do_digraph(-1); /* clear digraphs */
6393 do_digraph(buf[i-1]); /* may be the start of a digraph */
6394#endif
6395 buf[i] = NUL;
6396 ins_str(buf);
6397 if (flags & INSCHAR_CTRLV)
6398 {
6399 redo_literal(*buf);
6400 i = 1;
6401 }
6402 else
6403 i = 0;
6404 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006405 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 }
6407 else
6408 {
6409#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006410 int cc;
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6413 {
6414 char_u buf[MB_MAXBYTES + 1];
6415
6416 (*mb_char2bytes)(c, buf);
6417 buf[cc] = NUL;
6418 ins_char_bytes(buf, cc);
6419 AppendCharToRedobuff(c);
6420 }
6421 else
6422#endif
6423 {
6424 ins_char(c);
6425 if (flags & INSCHAR_CTRLV)
6426 redo_literal(c);
6427 else
6428 AppendCharToRedobuff(c);
6429 }
6430 }
6431}
6432
6433/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006434 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006435 *
6436 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6437 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006438 */
6439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440internal_format(
6441 int textwidth,
6442 int second_indent,
6443 int flags,
6444 int format_only,
6445 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006446{
6447 int cc;
6448 int save_char = NUL;
6449 int haveto_redraw = FALSE;
6450 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6451#ifdef FEAT_MBYTE
6452 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6453#endif
6454 int fo_white_par = has_format_option(FO_WHITE_PAR);
6455 int first_line = TRUE;
6456#ifdef FEAT_COMMENTS
6457 colnr_T leader_len;
6458 int no_leader = FALSE;
6459 int do_comments = (flags & INSCHAR_DO_COM);
6460#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006461#ifdef FEAT_LINEBREAK
6462 int has_lbr = curwin->w_p_lbr;
6463
6464 /* make sure win_lbr_chartabsize() counts correctly */
6465 curwin->w_p_lbr = FALSE;
6466#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006467
6468 /*
6469 * When 'ai' is off we don't want a space under the cursor to be
6470 * deleted. Replace it with an 'x' temporarily.
6471 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006472 if (!curbuf->b_p_ai
6473#ifdef FEAT_VREPLACE
6474 && !(State & VREPLACE_FLAG)
6475#endif
6476 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477 {
6478 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006479 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006480 {
6481 save_char = cc;
6482 pchar_cursor('x');
6483 }
6484 }
6485
6486 /*
6487 * Repeat breaking lines, until the current line is not too long.
6488 */
6489 while (!got_int)
6490 {
6491 int startcol; /* Cursor column at entry */
6492 int wantcol; /* column at textwidth border */
6493 int foundcol; /* column for start of spaces */
6494 int end_foundcol = 0; /* column for start of word */
6495 colnr_T len;
6496 colnr_T virtcol;
6497#ifdef FEAT_VREPLACE
6498 int orig_col = 0;
6499 char_u *saved_text = NULL;
6500#endif
6501 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006502 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006503
Bram Moolenaar97b98102009-11-17 16:41:01 +00006504 virtcol = get_nolist_virtcol()
6505 + char2cells(c != NUL ? c : gchar_cursor());
6506 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006507 break;
6508
6509#ifdef FEAT_COMMENTS
6510 if (no_leader)
6511 do_comments = FALSE;
6512 else if (!(flags & INSCHAR_FORMAT)
6513 && has_format_option(FO_WRAP_COMS))
6514 do_comments = TRUE;
6515
6516 /* Don't break until after the comment leader */
6517 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006518 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006519 else
6520 leader_len = 0;
6521
6522 /* If the line doesn't start with a comment leader, then don't
6523 * start one in a following broken line. Avoids that a %word
6524 * moved to the start of the next line causes all following lines
6525 * to start with %. */
6526 if (leader_len == 0)
6527 no_leader = TRUE;
6528#endif
6529 if (!(flags & INSCHAR_FORMAT)
6530#ifdef FEAT_COMMENTS
6531 && leader_len == 0
6532#endif
6533 && !has_format_option(FO_WRAP))
6534
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006535 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006536 if ((startcol = curwin->w_cursor.col) == 0)
6537 break;
6538
6539 /* find column of textwidth border */
6540 coladvance((colnr_T)textwidth);
6541 wantcol = curwin->w_cursor.col;
6542
Bram Moolenaar97b98102009-11-17 16:41:01 +00006543 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006544 foundcol = 0;
6545
6546 /*
6547 * Find position to break at.
6548 * Stop at first entered white when 'formatoptions' has 'v'
6549 */
6550 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006551 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006552 || curwin->w_cursor.lnum != Insstart.lnum
6553 || curwin->w_cursor.col >= Insstart.col)
6554 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006555 if (curwin->w_cursor.col == startcol && c != NUL)
6556 cc = c;
6557 else
6558 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006559 if (WHITECHAR(cc))
6560 {
6561 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006562 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006563
6564 /* find start of sequence of blanks */
6565 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6566 {
6567 dec_cursor();
6568 cc = gchar_cursor();
6569 }
6570 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6571 break; /* only spaces in front of text */
6572#ifdef FEAT_COMMENTS
6573 /* Don't break until after the comment leader */
6574 if (curwin->w_cursor.col < leader_len)
6575 break;
6576#endif
6577 if (has_format_option(FO_ONE_LETTER))
6578 {
6579 /* do not break after one-letter words */
6580 if (curwin->w_cursor.col == 0)
6581 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006582#ifdef FEAT_COMMENTS
6583 /* do not break "#a b" when 'tw' is 2 */
6584 if (curwin->w_cursor.col <= leader_len)
6585 break;
6586#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006587 col = curwin->w_cursor.col;
6588 dec_cursor();
6589 cc = gchar_cursor();
6590
6591 if (WHITECHAR(cc))
6592 continue; /* one-letter, continue */
6593 curwin->w_cursor.col = col;
6594 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006595
6596 inc_cursor();
6597
6598 end_foundcol = end_col + 1;
6599 foundcol = curwin->w_cursor.col;
6600 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006601 break;
6602 }
6603#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006604 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006605 {
6606 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006607 if (curwin->w_cursor.col != startcol)
6608 {
6609#ifdef FEAT_COMMENTS
6610 /* Don't break until after the comment leader */
6611 if (curwin->w_cursor.col < leader_len)
6612 break;
6613#endif
6614 col = curwin->w_cursor.col;
6615 inc_cursor();
6616 /* Don't change end_foundcol if already set. */
6617 if (foundcol != curwin->w_cursor.col)
6618 {
6619 foundcol = curwin->w_cursor.col;
6620 end_foundcol = foundcol;
6621 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6622 break;
6623 }
6624 curwin->w_cursor.col = col;
6625 }
6626
6627 if (curwin->w_cursor.col == 0)
6628 break;
6629
6630 col = curwin->w_cursor.col;
6631
6632 dec_cursor();
6633 cc = gchar_cursor();
6634
6635 if (WHITECHAR(cc))
6636 continue; /* break with space */
6637#ifdef FEAT_COMMENTS
6638 /* Don't break until after the comment leader */
6639 if (curwin->w_cursor.col < leader_len)
6640 break;
6641#endif
6642
6643 curwin->w_cursor.col = col;
6644
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006645 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006646 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006647 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6648 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006649 }
6650#endif
6651 if (curwin->w_cursor.col == 0)
6652 break;
6653 dec_cursor();
6654 }
6655
6656 if (foundcol == 0) /* no spaces, cannot break line */
6657 {
6658 curwin->w_cursor.col = startcol;
6659 break;
6660 }
6661
6662 /* Going to break the line, remove any "$" now. */
6663 undisplay_dollar();
6664
6665 /*
6666 * Offset between cursor position and line break is used by replace
6667 * stack functions. VREPLACE does not use this, and backspaces
6668 * over the text instead.
6669 */
6670#ifdef FEAT_VREPLACE
6671 if (State & VREPLACE_FLAG)
6672 orig_col = startcol; /* Will start backspacing from here */
6673 else
6674#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006675 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006676
6677 /*
6678 * adjust startcol for spaces that will be deleted and
6679 * characters that will remain on top line
6680 */
6681 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006682 while ((cc = gchar_cursor(), WHITECHAR(cc))
6683 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006684 inc_cursor();
6685 startcol -= curwin->w_cursor.col;
6686 if (startcol < 0)
6687 startcol = 0;
6688
6689#ifdef FEAT_VREPLACE
6690 if (State & VREPLACE_FLAG)
6691 {
6692 /*
6693 * In VREPLACE mode, we will backspace over the text to be
6694 * wrapped, so save a copy now to put on the next line.
6695 */
6696 saved_text = vim_strsave(ml_get_cursor());
6697 curwin->w_cursor.col = orig_col;
6698 if (saved_text == NULL)
6699 break; /* Can't do it, out of memory */
6700 saved_text[startcol] = NUL;
6701
6702 /* Backspace over characters that will move to the next line */
6703 if (!fo_white_par)
6704 backspace_until_column(foundcol);
6705 }
6706 else
6707#endif
6708 {
6709 /* put cursor after pos. to break line */
6710 if (!fo_white_par)
6711 curwin->w_cursor.col = foundcol;
6712 }
6713
6714 /*
6715 * Split the line just before the margin.
6716 * Only insert/delete lines, but don't really redraw the window.
6717 */
6718 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6719 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6720#ifdef FEAT_COMMENTS
6721 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006722 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006723#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006724 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6725 if (!(flags & INSCHAR_COM_LIST))
6726 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006727
6728 replace_offset = 0;
6729 if (first_line)
6730 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006731 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006732 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006733 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006734 * This section is for auto-wrap of numeric lists. When not
6735 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6736 * flag will be set and open_line() will handle it (as seen
6737 * above). The code here (and in get_number_indent()) will
6738 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006739 */
6740 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006741 second_indent =
6742 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006743 if (second_indent >= 0)
6744 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006745#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006746 if (State & VREPLACE_FLAG)
6747 change_indent(INDENT_SET, second_indent,
6748 FALSE, NUL, TRUE);
6749 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006750#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006751#ifdef FEAT_COMMENTS
6752 if (leader_len > 0 && second_indent - leader_len > 0)
6753 {
6754 int i;
6755 int padding = second_indent - leader_len;
6756
6757 /* We started at the first_line of a numbered list
6758 * that has a comment. the open_line() function has
6759 * inserted the proper comment leader and positioned
6760 * the cursor at the end of the split line. Now we
6761 * add the additional whitespace needed after the
6762 * comment leader for the numbered list. */
6763 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006764 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006765 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006766 }
6767 else
6768 {
6769#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006770 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006771#ifdef FEAT_COMMENTS
6772 }
6773#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006774 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006775 }
6776 first_line = FALSE;
6777 }
6778
6779#ifdef FEAT_VREPLACE
6780 if (State & VREPLACE_FLAG)
6781 {
6782 /*
6783 * In VREPLACE mode we have backspaced over the text to be
6784 * moved, now we re-insert it into the new line.
6785 */
6786 ins_bytes(saved_text);
6787 vim_free(saved_text);
6788 }
6789 else
6790#endif
6791 {
6792 /*
6793 * Check if cursor is not past the NUL off the line, cindent
6794 * may have added or removed indent.
6795 */
6796 curwin->w_cursor.col += startcol;
6797 len = (colnr_T)STRLEN(ml_get_curline());
6798 if (curwin->w_cursor.col > len)
6799 curwin->w_cursor.col = len;
6800 }
6801
6802 haveto_redraw = TRUE;
6803#ifdef FEAT_CINDENT
6804 can_cindent = TRUE;
6805#endif
6806 /* moved the cursor, don't autoindent or cindent now */
6807 did_ai = FALSE;
6808#ifdef FEAT_SMARTINDENT
6809 did_si = FALSE;
6810 can_si = FALSE;
6811 can_si_back = FALSE;
6812#endif
6813 line_breakcheck();
6814 }
6815
6816 if (save_char != NUL) /* put back space after cursor */
6817 pchar_cursor(save_char);
6818
Bram Moolenaar0026d472014-09-09 16:32:39 +02006819#ifdef FEAT_LINEBREAK
6820 curwin->w_p_lbr = has_lbr;
6821#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006822 if (!format_only && haveto_redraw)
6823 {
6824 update_topline();
6825 redraw_curbuf_later(VALID);
6826 }
6827}
6828
6829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 * Called after inserting or deleting text: When 'formatoptions' includes the
6831 * 'a' flag format from the current line until the end of the paragraph.
6832 * Keep the cursor at the same position relative to the text.
6833 * The caller must have saved the cursor line for undo, following ones will be
6834 * saved here.
6835 */
6836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006837auto_format(
6838 int trailblank, /* when TRUE also format with trailing blank */
6839 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840{
6841 pos_T pos;
6842 colnr_T len;
6843 char_u *old;
6844 char_u *new, *pnew;
6845 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006846 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
6848 if (!has_format_option(FO_AUTO))
6849 return;
6850
6851 pos = curwin->w_cursor;
6852 old = ml_get_curline();
6853
6854 /* may remove added space */
6855 check_auto_format(FALSE);
6856
6857 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6858 * user might insert normal text next. Also skip formatting when "1" is
6859 * in 'formatoptions' and there is a single character before the cursor.
6860 * Otherwise the line would be broken and when typing another non-white
6861 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006862 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 if (*old != NUL && !trailblank && wasatend)
6864 {
6865 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006866 cc = gchar_cursor();
6867 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6868 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006870 cc = gchar_cursor();
6871 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
6873 curwin->w_cursor = pos;
6874 return;
6875 }
6876 curwin->w_cursor = pos;
6877 }
6878
6879#ifdef FEAT_COMMENTS
6880 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6881 * comments. */
6882 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006883 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 return;
6885#endif
6886
6887 /*
6888 * May start formatting in a previous line, so that after "x" a word is
6889 * moved to the previous line if it fits there now. Only when this is not
6890 * the start of a paragraph.
6891 */
6892 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6893 {
6894 --curwin->w_cursor.lnum;
6895 if (u_save_cursor() == FAIL)
6896 return;
6897 }
6898
6899 /*
6900 * Do the formatting and restore the cursor position. "saved_cursor" will
6901 * be adjusted for the text formatting.
6902 */
6903 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006904 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 curwin->w_cursor = saved_cursor;
6906 saved_cursor.lnum = 0;
6907
6908 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6909 {
6910 /* "cannot happen" */
6911 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6912 coladvance((colnr_T)MAXCOL);
6913 }
6914 else
6915 check_cursor_col();
6916
6917 /* Insert mode: If the cursor is now after the end of the line while it
6918 * previously wasn't, the line was broken. Because of the rule above we
6919 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6920 * formatted. */
6921 if (!wasatend && has_format_option(FO_WHITE_PAR))
6922 {
6923 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006924 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 if (curwin->w_cursor.col == len)
6926 {
6927 pnew = vim_strnsave(new, len + 2);
6928 pnew[len] = ' ';
6929 pnew[len + 1] = NUL;
6930 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6931 /* remove the space later */
6932 did_add_space = TRUE;
6933 }
6934 else
6935 /* may remove added space */
6936 check_auto_format(FALSE);
6937 }
6938
6939 check_cursor();
6940}
6941
6942/*
6943 * When an extra space was added to continue a paragraph for auto-formatting,
6944 * delete it now. The space must be under the cursor, just after the insert
6945 * position.
6946 */
6947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006948check_auto_format(
6949 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950{
6951 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006952 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954 if (did_add_space)
6955 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006956 cc = gchar_cursor();
6957 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 /* Somehow the space was removed already. */
6959 did_add_space = FALSE;
6960 else
6961 {
6962 if (!end_insert)
6963 {
6964 inc_cursor();
6965 c = gchar_cursor();
6966 dec_cursor();
6967 }
6968 if (c != NUL)
6969 {
6970 /* The space is no longer at the end of the line, delete it. */
6971 del_char(FALSE);
6972 did_add_space = FALSE;
6973 }
6974 }
6975 }
6976}
6977
6978/*
6979 * Find out textwidth to be used for formatting:
6980 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006981 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 * if invalid value, use 0.
6983 * Set default to window width (maximum 79) for "gq" operator.
6984 */
6985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986comp_textwidth(
6987 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 int textwidth;
6990
6991 textwidth = curbuf->b_p_tw;
6992 if (textwidth == 0 && curbuf->b_p_wm)
6993 {
6994 /* The width is the window width minus 'wrapmargin' minus all the
6995 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006996 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997#ifdef FEAT_CMDWIN
6998 if (cmdwin_type != 0)
6999 textwidth -= 1;
7000#endif
7001#ifdef FEAT_FOLDING
7002 textwidth -= curwin->w_p_fdc;
7003#endif
7004#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007005 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 textwidth -= 1;
7007#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02007008 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 textwidth -= 8;
7010 }
7011 if (textwidth < 0)
7012 textwidth = 0;
7013 if (ff && textwidth == 0)
7014 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007015 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 if (textwidth > 79)
7017 textwidth = 79;
7018 }
7019 return textwidth;
7020}
7021
7022/*
7023 * Put a character in the redo buffer, for when just after a CTRL-V.
7024 */
7025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007026redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 char_u buf[10];
7029
7030 /* Only digits need special treatment. Translate them into a string of
7031 * three digits. */
7032 if (VIM_ISDIGIT(c))
7033 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007034 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 AppendToRedobuff(buf);
7036 }
7037 else
7038 AppendCharToRedobuff(c);
7039}
7040
7041/*
7042 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007043 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 */
7045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007046start_arrow(
7047 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007049 start_arrow_common(end_insert_pos, TRUE);
7050}
7051
7052/*
7053 * Like start_arrow() but with end_change argument.
7054 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7055 */
7056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007057start_arrow_with_change(
7058 pos_T *end_insert_pos, /* can be NULL */
7059 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007060{
7061 start_arrow_common(end_insert_pos, end_change);
7062 if (!end_change)
7063 {
7064 AppendCharToRedobuff(Ctrl_G);
7065 AppendCharToRedobuff('U');
7066 }
7067}
7068
7069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007070start_arrow_common(
7071 pos_T *end_insert_pos, /* can be NULL */
7072 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007073{
7074 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {
7076 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007077 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 arrow_used = TRUE; /* this means we stopped the current insert */
7079 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007080#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007081 check_spell_redraw();
7082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083}
7084
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007085#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007086/*
7087 * If we skipped highlighting word at cursor, do it now.
7088 * It may be skipped again, thus reset spell_redraw_lnum first.
7089 */
7090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007091check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007092{
7093 if (spell_redraw_lnum != 0)
7094 {
7095 linenr_T lnum = spell_redraw_lnum;
7096
7097 spell_redraw_lnum = 0;
7098 redrawWinline(lnum, FALSE);
7099 }
7100}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007101
7102/*
7103 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7104 * spelled word, if there is one.
7105 */
7106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007107spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007108{
7109 pos_T tpos = curwin->w_cursor;
7110
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007111 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007112 if (curwin->w_cursor.col != tpos.col)
7113 start_arrow(&tpos);
7114}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007115#endif
7116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117/*
7118 * stop_arrow() is called before a change is made in insert mode.
7119 * If an arrow key has been used, start a new insertion.
7120 * Returns FAIL if undo is impossible, shouldn't insert then.
7121 */
7122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007123stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
7125 if (arrow_used)
7126 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007127 Insstart = curwin->w_cursor; /* new insertion starts here */
7128 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7129 /* Don't update the original insert position when moved to the
7130 * right, except when nothing was inserted yet. */
7131 update_Insstart_orig = FALSE;
7132 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (u_save_cursor() == OK)
7135 {
7136 arrow_used = FALSE;
7137 ins_need_undo = FALSE;
7138 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 ai_col = 0;
7141#ifdef FEAT_VREPLACE
7142 if (State & VREPLACE_FLAG)
7143 {
7144 orig_line_count = curbuf->b_ml.ml_line_count;
7145 vr_lines_changed = 1;
7146 }
7147#endif
7148 ResetRedobuff();
7149 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007150 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
7152 else if (ins_need_undo)
7153 {
7154 if (u_save_cursor() == OK)
7155 ins_need_undo = FALSE;
7156 }
7157
7158#ifdef FEAT_FOLDING
7159 /* Always open fold at the cursor line when inserting something. */
7160 foldOpenCursor();
7161#endif
7162
7163 return (arrow_used || ins_need_undo ? FAIL : OK);
7164}
7165
7166/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007167 * Do a few things to stop inserting.
7168 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7169 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 */
7171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007172stop_insert(
7173 pos_T *end_insert_pos,
7174 int esc, /* called by ins_esc() */
7175 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007177 int cc;
7178 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
7180 stop_redo_ins();
7181 replace_flush(); /* abandon replace stack */
7182
7183 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007184 * Save the inserted text for later redo with ^@ and CTRL-A.
7185 * Don't do it when "restart_edit" was set and nothing was inserted,
7186 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007188 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007189 if (did_restart_edit == 0 || (ptr != NULL
7190 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007191 {
7192 vim_free(last_insert);
7193 last_insert = ptr;
7194 last_insert_skip = new_insert_skip;
7195 }
7196 else
7197 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007199 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
7201 /* Auto-format now. It may seem strange to do this when stopping an
7202 * insertion (or moving the cursor), but it's required when appending
7203 * a line and having it end in a space. But only do it when something
7204 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007205 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007207 pos_T tpos = curwin->w_cursor;
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 /* When the cursor is at the end of the line after a space the
7210 * formatting will move it to the following word. Avoid that by
7211 * moving the cursor onto the space. */
7212 cc = 'x';
7213 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7214 {
7215 dec_cursor();
7216 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007217 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007218 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
7220
7221 auto_format(TRUE, FALSE);
7222
Bram Moolenaar1c465442017-03-12 20:10:05 +01007223 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007224 {
7225 if (gchar_cursor() != NUL)
7226 inc_cursor();
7227#ifdef FEAT_VIRTUALEDIT
7228 /* If the cursor is still at the same character, also keep
7229 * the "coladd". */
7230 if (gchar_cursor() == NUL
7231 && curwin->w_cursor.lnum == tpos.lnum
7232 && curwin->w_cursor.col == tpos.col)
7233 curwin->w_cursor.coladd = tpos.coladd;
7234#endif
7235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 }
7237
7238 /* If a space was inserted for auto-formatting, remove it now. */
7239 check_auto_format(TRUE);
7240
7241 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007242 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007243 * Do this when ESC was used or moving the cursor up/down.
7244 * Check for the old position still being valid, just in case the text
7245 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007246 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007247 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7248 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007250 pos_T tpos = curwin->w_cursor;
7251
7252 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007253 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007254 for (;;)
7255 {
7256 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7257 --curwin->w_cursor.col;
7258 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007259 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007260 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007261 if (del_char(TRUE) == FAIL)
7262 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007263 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007264 if (curwin->w_cursor.lnum != tpos.lnum)
7265 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007266 else
7267 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007268 /* reset tpos, could have been invalidated in the loop above */
7269 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007270 tpos.col++;
7271 if (cc != NUL && gchar_pos(&tpos) == NUL)
7272 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 /* <C-S-Right> may have started Visual mode, adjust the position for
7276 * deleted characters. */
7277 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7278 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007279 int len = (int)STRLEN(ml_get_curline());
7280
7281 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007283 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007284#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 }
7288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290 }
7291 did_ai = FALSE;
7292#ifdef FEAT_SMARTINDENT
7293 did_si = FALSE;
7294 can_si = FALSE;
7295 can_si_back = FALSE;
7296#endif
7297
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007298 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7299 * now in a different buffer. */
7300 if (end_insert_pos != NULL)
7301 {
7302 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007303 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007304 curbuf->b_op_end = *end_insert_pos;
7305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306}
7307
7308/*
7309 * Set the last inserted text to a single character.
7310 * Used for the replace command.
7311 */
7312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
7315 char_u *s;
7316
7317 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 if (last_insert != NULL)
7320 {
7321 s = last_insert;
7322 /* Use the CTRL-V only when entering a special char */
7323 if (c < ' ' || c == DEL)
7324 *s++ = Ctrl_V;
7325 s = add_char2buf(c, s);
7326 *s++ = ESC;
7327 *s++ = NUL;
7328 last_insert_skip = 0;
7329 }
7330}
7331
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007332#if defined(EXITFREE) || defined(PROTO)
7333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007334free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007335{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007336 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007337# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007338 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007339# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007340}
7341#endif
7342
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343/*
7344 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7345 * and CSI. Handle multi-byte characters.
7346 * Returns a pointer to after the added bytes.
7347 */
7348 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007349add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
7351#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007352 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 int i;
7354 int len;
7355
7356 len = (*mb_char2bytes)(c, temp);
7357 for (i = 0; i < len; ++i)
7358 {
7359 c = temp[i];
7360#endif
7361 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7362 if (c == K_SPECIAL)
7363 {
7364 *s++ = K_SPECIAL;
7365 *s++ = KS_SPECIAL;
7366 *s++ = KE_FILLER;
7367 }
7368#ifdef FEAT_GUI
7369 else if (c == CSI)
7370 {
7371 *s++ = CSI;
7372 *s++ = KS_EXTRA;
7373 *s++ = (int)KE_CSI;
7374 }
7375#endif
7376 else
7377 *s++ = c;
7378#ifdef FEAT_MBYTE
7379 }
7380#endif
7381 return s;
7382}
7383
7384/*
7385 * move cursor to start of line
7386 * if flags & BL_WHITE move to first non-white
7387 * if flags & BL_SOL move to first non-white if startofline is set,
7388 * otherwise keep "curswant" column
7389 * if flags & BL_FIX don't leave the cursor on a NUL.
7390 */
7391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007392beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 if ((flags & BL_SOL) && !p_sol)
7395 coladvance(curwin->w_curswant);
7396 else
7397 {
7398 curwin->w_cursor.col = 0;
7399#ifdef FEAT_VIRTUALEDIT
7400 curwin->w_cursor.coladd = 0;
7401#endif
7402
7403 if (flags & (BL_WHITE | BL_SOL))
7404 {
7405 char_u *ptr;
7406
Bram Moolenaar1c465442017-03-12 20:10:05 +01007407 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7409 ++curwin->w_cursor.col;
7410 }
7411 curwin->w_set_curswant = TRUE;
7412 }
7413}
7414
7415/*
7416 * oneright oneleft cursor_down cursor_up
7417 *
7418 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007419 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 * Return OK when successful, FAIL when we hit a line of file boundary.
7421 */
7422
7423 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
7429#ifdef FEAT_VIRTUALEDIT
7430 if (virtual_active())
7431 {
7432 pos_T prevpos = curwin->w_cursor;
7433
7434 /* Adjust for multi-wide char (excluding TAB) */
7435 ptr = ml_get_cursor();
7436 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007437# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007439# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 ))
7443 ? ptr2cells(ptr) : 1));
7444 curwin->w_set_curswant = TRUE;
7445 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7446 return (prevpos.col != curwin->w_cursor.col
7447 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7448 }
7449#endif
7450
7451 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007452 if (*ptr == NUL)
7453 return FAIL; /* already at the very end */
7454
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007456 if (has_mbyte)
7457 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 else
7459#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007460 l = 1;
7461
7462 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7463 * contains "onemore". */
7464 if (ptr[l] == NUL
7465#ifdef FEAT_VIRTUALEDIT
7466 && (ve_flags & VE_ONEMORE) == 0
7467#endif
7468 )
7469 return FAIL;
7470 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
7472 curwin->w_set_curswant = TRUE;
7473 return OK;
7474}
7475
7476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007477oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478{
7479#ifdef FEAT_VIRTUALEDIT
7480 if (virtual_active())
7481 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007482# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 int v = getviscol();
7486
7487 if (v == 0)
7488 return FAIL;
7489
7490# ifdef FEAT_LINEBREAK
7491 /* We might get stuck on 'showbreak', skip over it. */
7492 width = 1;
7493 for (;;)
7494 {
7495 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007496 /* getviscol() is slow, skip it when 'showbreak' is empty,
7497 * 'breakindent' is not set and there are no multi-byte
7498 * characters */
7499 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500# ifdef FEAT_MBYTE
7501 && !has_mbyte
7502# endif
7503 ) || getviscol() < v)
7504 break;
7505 ++width;
7506 }
7507# else
7508 coladvance(v - 1);
7509# endif
7510
7511 if (curwin->w_cursor.coladd == 1)
7512 {
7513 char_u *ptr;
7514
7515 /* Adjust for multi-wide char (not a TAB) */
7516 ptr = ml_get_cursor();
7517 if (*ptr != TAB && vim_isprintc(
7518# ifdef FEAT_MBYTE
7519 (*mb_ptr2char)(ptr)
7520# else
7521 *ptr
7522# endif
7523 ) && ptr2cells(ptr) > 1)
7524 curwin->w_cursor.coladd = 0;
7525 }
7526
7527 curwin->w_set_curswant = TRUE;
7528 return OK;
7529 }
7530#endif
7531
7532 if (curwin->w_cursor.col == 0)
7533 return FAIL;
7534
7535 curwin->w_set_curswant = TRUE;
7536 --curwin->w_cursor.col;
7537
7538#ifdef FEAT_MBYTE
7539 /* if the character on the left of the current cursor is a multi-byte
7540 * character, move to its first byte */
7541 if (has_mbyte)
7542 mb_adjust_cursor();
7543#endif
7544 return OK;
7545}
7546
7547 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548cursor_up(
7549 long n,
7550 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551{
7552 linenr_T lnum;
7553
7554 if (n > 0)
7555 {
7556 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007557 /* This fails if the cursor is already in the first line or the count
7558 * is larger than the line number and '-' is in 'cpoptions' */
7559 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 return FAIL;
7561 if (n >= lnum)
7562 lnum = 1;
7563 else
7564#ifdef FEAT_FOLDING
7565 if (hasAnyFolding(curwin))
7566 {
7567 /*
7568 * Count each sequence of folded lines as one logical line.
7569 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007570 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 (void)hasFolding(lnum, &lnum, NULL);
7572
7573 while (n--)
7574 {
7575 /* move up one line */
7576 --lnum;
7577 if (lnum <= 1)
7578 break;
7579 /* If we entered a fold, move to the beginning, unless in
7580 * Insert mode or when 'foldopen' contains "all": it will open
7581 * in a moment. */
7582 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7583 (void)hasFolding(lnum, &lnum, NULL);
7584 }
7585 if (lnum < 1)
7586 lnum = 1;
7587 }
7588 else
7589#endif
7590 lnum -= n;
7591 curwin->w_cursor.lnum = lnum;
7592 }
7593
7594 /* try to advance to the column we want to be at */
7595 coladvance(curwin->w_curswant);
7596
7597 if (upd_topline)
7598 update_topline(); /* make sure curwin->w_topline is valid */
7599
7600 return OK;
7601}
7602
7603/*
7604 * Cursor down a number of logical lines.
7605 */
7606 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007607cursor_down(
7608 long n,
7609 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610{
7611 linenr_T lnum;
7612
7613 if (n > 0)
7614 {
7615 lnum = curwin->w_cursor.lnum;
7616#ifdef FEAT_FOLDING
7617 /* Move to last line of fold, will fail if it's the end-of-file. */
7618 (void)hasFolding(lnum, NULL, &lnum);
7619#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007620 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007621 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007622 if (lnum >= curbuf->b_ml.ml_line_count
7623 || (lnum + n > curbuf->b_ml.ml_line_count
7624 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 return FAIL;
7626 if (lnum + n >= curbuf->b_ml.ml_line_count)
7627 lnum = curbuf->b_ml.ml_line_count;
7628 else
7629#ifdef FEAT_FOLDING
7630 if (hasAnyFolding(curwin))
7631 {
7632 linenr_T last;
7633
7634 /* count each sequence of folded lines as one logical line */
7635 while (n--)
7636 {
7637 if (hasFolding(lnum, NULL, &last))
7638 lnum = last + 1;
7639 else
7640 ++lnum;
7641 if (lnum >= curbuf->b_ml.ml_line_count)
7642 break;
7643 }
7644 if (lnum > curbuf->b_ml.ml_line_count)
7645 lnum = curbuf->b_ml.ml_line_count;
7646 }
7647 else
7648#endif
7649 lnum += n;
7650 curwin->w_cursor.lnum = lnum;
7651 }
7652
7653 /* try to advance to the column we want to be at */
7654 coladvance(curwin->w_curswant);
7655
7656 if (upd_topline)
7657 update_topline(); /* make sure curwin->w_topline is valid */
7658
7659 return OK;
7660}
7661
7662/*
7663 * Stuff the last inserted text in the read buffer.
7664 * Last_insert actually is a copy of the redo buffer, so we
7665 * first have to remove the command.
7666 */
7667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007668stuff_inserted(
7669 int c, /* Command character to be inserted */
7670 long count, /* Repeat this many times */
7671 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
7673 char_u *esc_ptr;
7674 char_u *ptr;
7675 char_u *last_ptr;
7676 char_u last = NUL;
7677
7678 ptr = get_last_insert();
7679 if (ptr == NULL)
7680 {
7681 EMSG(_(e_noinstext));
7682 return FAIL;
7683 }
7684
7685 /* may want to stuff the command character, to start Insert mode */
7686 if (c != NUL)
7687 stuffcharReadbuff(c);
7688 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7689 *esc_ptr = NUL; /* remove the ESC */
7690
7691 /* when the last char is either "0" or "^" it will be quoted if no ESC
7692 * comes after it OR if it will inserted more than once and "ptr"
7693 * starts with ^D. -- Acevedo
7694 */
7695 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7696 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7697 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7698 {
7699 last = *last_ptr;
7700 *last_ptr = NUL;
7701 }
7702
7703 do
7704 {
7705 stuffReadbuff(ptr);
7706 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7707 if (last)
7708 stuffReadbuff((char_u *)(last == '0'
7709 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7710 : IF_EB("\026^", CTRL_V_STR "^")));
7711 }
7712 while (--count > 0);
7713
7714 if (last)
7715 *last_ptr = last;
7716
7717 if (esc_ptr != NULL)
7718 *esc_ptr = ESC; /* put the ESC back */
7719
7720 /* may want to stuff a trailing ESC, to get out of Insert mode */
7721 if (!no_esc)
7722 stuffcharReadbuff(ESC);
7723
7724 return OK;
7725}
7726
7727 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007728get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 if (last_insert == NULL)
7731 return NULL;
7732 return last_insert + last_insert_skip;
7733}
7734
7735/*
7736 * Get last inserted string, and remove trailing <Esc>.
7737 * Returns pointer to allocated memory (must be freed) or NULL.
7738 */
7739 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007740get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
7742 char_u *s;
7743 int len;
7744
7745 if (last_insert == NULL)
7746 return NULL;
7747 s = vim_strsave(last_insert + last_insert_skip);
7748 if (s != NULL)
7749 {
7750 len = (int)STRLEN(s);
7751 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7752 s[len - 1] = NUL;
7753 }
7754 return s;
7755}
7756
7757/*
7758 * Check the word in front of the cursor for an abbreviation.
7759 * Called when the non-id character "c" has been entered.
7760 * When an abbreviation is recognized it is removed from the text and
7761 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7762 */
7763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007764echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
7766 /* Don't check for abbreviation in paste mode, when disabled and just
7767 * after moving around with cursor keys. */
7768 if (p_paste || no_abbr || arrow_used)
7769 return FALSE;
7770
7771 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7772 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7773}
7774
7775/*
7776 * replace-stack functions
7777 *
7778 * When replacing characters, the replaced characters are remembered for each
7779 * new character. This is used to re-insert the old text when backspacing.
7780 *
7781 * There is a NUL headed list of characters for each character that is
7782 * currently in the file after the insertion point. When BS is used, one NUL
7783 * headed list is put back for the deleted character.
7784 *
7785 * For a newline, there are two NUL headed lists. One contains the characters
7786 * that the NL replaced. The extra one stores the characters after the cursor
7787 * that were deleted (always white space).
7788 *
7789 * Replace_offset is normally 0, in which case replace_push will add a new
7790 * character at the end of the stack. If replace_offset is not 0, that many
7791 * characters will be left on the stack above the newly inserted character.
7792 */
7793
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007794static char_u *replace_stack = NULL;
7795static long replace_stack_nr = 0; /* next entry in replace stack */
7796static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
7798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007799replace_push(
7800 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802 char_u *p;
7803
7804 if (replace_stack_nr < replace_offset) /* nothing to do */
7805 return;
7806 if (replace_stack_len <= replace_stack_nr)
7807 {
7808 replace_stack_len += 50;
7809 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7810 if (p == NULL) /* out of memory */
7811 {
7812 replace_stack_len -= 50;
7813 return;
7814 }
7815 if (replace_stack != NULL)
7816 {
7817 mch_memmove(p, replace_stack,
7818 (size_t)(replace_stack_nr * sizeof(char_u)));
7819 vim_free(replace_stack);
7820 }
7821 replace_stack = p;
7822 }
7823 p = replace_stack + replace_stack_nr - replace_offset;
7824 if (replace_offset)
7825 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7826 *p = c;
7827 ++replace_stack_nr;
7828}
7829
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007830#if defined(FEAT_MBYTE) || defined(PROTO)
7831/*
7832 * Push a character onto the replace stack. Handles a multi-byte character in
7833 * reverse byte order, so that the first byte is popped off first.
7834 * Return the number of bytes done (includes composing characters).
7835 */
7836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007837replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007838{
7839 int l = (*mb_ptr2len)(p);
7840 int j;
7841
7842 for (j = l - 1; j >= 0; --j)
7843 replace_push(p[j]);
7844 return l;
7845}
7846#endif
7847
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848/*
7849 * Pop one item from the replace stack.
7850 * return -1 if stack empty
7851 * return replaced character or NUL otherwise
7852 */
7853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007854replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 if (replace_stack_nr == 0)
7857 return -1;
7858 return (int)replace_stack[--replace_stack_nr];
7859}
7860
7861/*
7862 * Join the top two items on the replace stack. This removes to "off"'th NUL
7863 * encountered.
7864 */
7865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007866replace_join(
7867 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 int i;
7870
7871 for (i = replace_stack_nr; --i >= 0; )
7872 if (replace_stack[i] == NUL && off-- <= 0)
7873 {
7874 --replace_stack_nr;
7875 mch_memmove(replace_stack + i, replace_stack + i + 1,
7876 (size_t)(replace_stack_nr - i));
7877 return;
7878 }
7879}
7880
7881/*
7882 * Pop bytes from the replace stack until a NUL is found, and insert them
7883 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7884 */
7885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007886replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 int cc;
7889 int oldState = State;
7890
7891 State = NORMAL; /* don't want REPLACE here */
7892 while ((cc = replace_pop()) > 0)
7893 {
7894#ifdef FEAT_MBYTE
7895 mb_replace_pop_ins(cc);
7896#else
7897 ins_char(cc);
7898#endif
7899 dec_cursor();
7900 }
7901 State = oldState;
7902}
7903
7904#ifdef FEAT_MBYTE
7905/*
7906 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7907 * indicates a multi-byte char, pop the other bytes too.
7908 */
7909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007910mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007913 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 int i;
7915 int c;
7916
7917 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7918 {
7919 buf[0] = cc;
7920 for (i = 1; i < n; ++i)
7921 buf[i] = replace_pop();
7922 ins_bytes_len(buf, n);
7923 }
7924 else
7925 ins_char(cc);
7926
7927 if (enc_utf8)
7928 /* Handle composing chars. */
7929 for (;;)
7930 {
7931 c = replace_pop();
7932 if (c == -1) /* stack empty */
7933 break;
7934 if ((n = MB_BYTE2LEN(c)) == 1)
7935 {
7936 /* Not a multi-byte char, put it back. */
7937 replace_push(c);
7938 break;
7939 }
7940 else
7941 {
7942 buf[0] = c;
7943 for (i = 1; i < n; ++i)
7944 buf[i] = replace_pop();
7945 if (utf_iscomposing(utf_ptr2char(buf)))
7946 ins_bytes_len(buf, n);
7947 else
7948 {
7949 /* Not a composing char, put it back. */
7950 for (i = n - 1; i >= 0; --i)
7951 replace_push(buf[i]);
7952 break;
7953 }
7954 }
7955 }
7956}
7957#endif
7958
7959/*
7960 * make the replace stack empty
7961 * (called when exiting replace mode)
7962 */
7963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007964replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007966 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 replace_stack_len = 0;
7968 replace_stack_nr = 0;
7969}
7970
7971/*
7972 * Handle doing a BS for one character.
7973 * cc < 0: replace stack empty, just move cursor
7974 * cc == 0: character was inserted, delete it
7975 * cc > 0: character was replaced, put cc (first byte of original char) back
7976 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007977 * When "limit_col" is >= 0, don't delete before this column. Matters when
7978 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 */
7980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007981replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983 int cc;
7984#ifdef FEAT_VREPLACE
7985 int orig_len = 0;
7986 int ins_len;
7987 int orig_vcols = 0;
7988 colnr_T start_vcol;
7989 char_u *p;
7990 int i;
7991 int vcol;
7992#endif
7993
7994 cc = replace_pop();
7995 if (cc > 0)
7996 {
7997#ifdef FEAT_VREPLACE
7998 if (State & VREPLACE_FLAG)
7999 {
8000 /* Get the number of screen cells used by the character we are
8001 * going to delete. */
8002 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
8003 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
8004 }
8005#endif
8006#ifdef FEAT_MBYTE
8007 if (has_mbyte)
8008 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008009 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010# ifdef FEAT_VREPLACE
8011 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008012 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013# endif
8014 replace_push(cc);
8015 }
8016 else
8017#endif
8018 {
8019 pchar_cursor(cc);
8020#ifdef FEAT_VREPLACE
8021 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008022 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023#endif
8024 }
8025 replace_pop_ins();
8026
8027#ifdef FEAT_VREPLACE
8028 if (State & VREPLACE_FLAG)
8029 {
8030 /* Get the number of screen cells used by the inserted characters */
8031 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008032 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 vcol = start_vcol;
8034 for (i = 0; i < ins_len; ++i)
8035 {
8036 vcol += chartabsize(p + i, vcol);
8037#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008038 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039#endif
8040 }
8041 vcol -= start_vcol;
8042
8043 /* Delete spaces that were inserted after the cursor to keep the
8044 * text aligned. */
8045 curwin->w_cursor.col += ins_len;
8046 while (vcol > orig_vcols && gchar_cursor() == ' ')
8047 {
8048 del_char(FALSE);
8049 ++orig_vcols;
8050 }
8051 curwin->w_cursor.col -= ins_len;
8052 }
8053#endif
8054
8055 /* mark the buffer as changed and prepare for displaying */
8056 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8057 }
8058 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008059 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
8062#ifdef FEAT_CINDENT
8063/*
8064 * Return TRUE if C-indenting is on.
8065 */
8066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008067cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069 return (!p_paste && (curbuf->b_p_cin
8070# ifdef FEAT_EVAL
8071 || *curbuf->b_p_inde != NUL
8072# endif
8073 ));
8074}
8075#endif
8076
8077#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8078/*
8079 * Re-indent the current line, based on the current contents of it and the
8080 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8081 * confused what all the part that handles Control-T is doing that I'm not.
8082 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8083 */
8084
8085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008086fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008088 int amount = get_the_indent();
8089
8090 if (amount >= 0)
8091 {
8092 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8093 if (linewhite(curwin->w_cursor.lnum))
8094 did_ai = TRUE; /* delete the indent if the line stays empty */
8095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096}
8097
8098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008099fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100{
8101 if (p_paste)
8102 return;
8103# ifdef FEAT_LISP
8104 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8105 fixthisline(get_lisp_indent);
8106# endif
8107# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8108 else
8109# endif
8110# ifdef FEAT_CINDENT
8111 if (cindent_on())
8112 do_c_expr_indent();
8113# endif
8114}
8115
8116#endif
8117
8118#ifdef FEAT_CINDENT
8119/*
8120 * return TRUE if 'cinkeys' contains the key "keytyped",
8121 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008122 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8124 *
8125 * "keytyped" can have a few special values:
8126 * KEY_OPEN_FORW
8127 * KEY_OPEN_BACK
8128 * KEY_COMPLETE just finished completion.
8129 *
8130 * If line_is_empty is TRUE accept keys with '0' before them.
8131 */
8132 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008133in_cinkeys(
8134 int keytyped,
8135 int when,
8136 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137{
8138 char_u *look;
8139 int try_match;
8140 int try_match_word;
8141 char_u *p;
8142 char_u *line;
8143 int icase;
8144 int i;
8145
Bram Moolenaar30848942009-12-24 14:46:12 +00008146 if (keytyped == NUL)
8147 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8148 return FALSE;
8149
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150#ifdef FEAT_EVAL
8151 if (*curbuf->b_p_inde != NUL)
8152 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8153 else
8154#endif
8155 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8156 while (*look)
8157 {
8158 /*
8159 * Find out if we want to try a match with this key, depending on
8160 * 'when' and a '*' or '!' before the key.
8161 */
8162 switch (when)
8163 {
8164 case '*': try_match = (*look == '*'); break;
8165 case '!': try_match = (*look == '!'); break;
8166 default: try_match = (*look != '*'); break;
8167 }
8168 if (*look == '*' || *look == '!')
8169 ++look;
8170
8171 /*
8172 * If there is a '0', only accept a match if the line is empty.
8173 * But may still match when typing last char of a word.
8174 */
8175 if (*look == '0')
8176 {
8177 try_match_word = try_match;
8178 if (!line_is_empty)
8179 try_match = FALSE;
8180 ++look;
8181 }
8182 else
8183 try_match_word = FALSE;
8184
8185 /*
8186 * does it look like a control character?
8187 */
8188 if (*look == '^'
8189#ifdef EBCDIC
8190 && (Ctrl_chr(look[1]) != 0)
8191#else
8192 && look[1] >= '?' && look[1] <= '_'
8193#endif
8194 )
8195 {
8196 if (try_match && keytyped == Ctrl_chr(look[1]))
8197 return TRUE;
8198 look += 2;
8199 }
8200 /*
8201 * 'o' means "o" command, open forward.
8202 * 'O' means "O" command, open backward.
8203 */
8204 else if (*look == 'o')
8205 {
8206 if (try_match && keytyped == KEY_OPEN_FORW)
8207 return TRUE;
8208 ++look;
8209 }
8210 else if (*look == 'O')
8211 {
8212 if (try_match && keytyped == KEY_OPEN_BACK)
8213 return TRUE;
8214 ++look;
8215 }
8216
8217 /*
8218 * 'e' means to check for "else" at start of line and just before the
8219 * cursor.
8220 */
8221 else if (*look == 'e')
8222 {
8223 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8224 {
8225 p = ml_get_curline();
8226 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8227 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8228 return TRUE;
8229 }
8230 ++look;
8231 }
8232
8233 /*
8234 * ':' only causes an indent if it is at the end of a label or case
8235 * statement, or when it was before typing the ':' (to fix
8236 * class::method for C++).
8237 */
8238 else if (*look == ':')
8239 {
8240 if (try_match && keytyped == ':')
8241 {
8242 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008243 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008245 /* Need to get the line again after cin_islabel(). */
8246 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 if (curwin->w_cursor.col > 2
8248 && p[curwin->w_cursor.col - 1] == ':'
8249 && p[curwin->w_cursor.col - 2] == ':')
8250 {
8251 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008252 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008253 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 p = ml_get_curline();
8255 p[curwin->w_cursor.col - 1] = ':';
8256 if (i)
8257 return TRUE;
8258 }
8259 }
8260 ++look;
8261 }
8262
8263
8264 /*
8265 * Is it a key in <>, maybe?
8266 */
8267 else if (*look == '<')
8268 {
8269 if (try_match)
8270 {
8271 /*
8272 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8273 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8274 * >, *, : and ! keys if they really really want to.
8275 */
8276 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8277 && keytyped == look[1])
8278 return TRUE;
8279
8280 if (keytyped == get_special_key_code(look + 1))
8281 return TRUE;
8282 }
8283 while (*look && *look != '>')
8284 look++;
8285 while (*look == '>')
8286 look++;
8287 }
8288
8289 /*
8290 * Is it a word: "=word"?
8291 */
8292 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8293 {
8294 ++look;
8295 if (*look == '~')
8296 {
8297 icase = TRUE;
8298 ++look;
8299 }
8300 else
8301 icase = FALSE;
8302 p = vim_strchr(look, ',');
8303 if (p == NULL)
8304 p = look + STRLEN(look);
8305 if ((try_match || try_match_word)
8306 && curwin->w_cursor.col >= (colnr_T)(p - look))
8307 {
8308 int match = FALSE;
8309
8310#ifdef FEAT_INS_EXPAND
8311 if (keytyped == KEY_COMPLETE)
8312 {
8313 char_u *s;
8314
8315 /* Just completed a word, check if it starts with "look".
8316 * search back for the start of a word. */
8317 line = ml_get_curline();
8318# ifdef FEAT_MBYTE
8319 if (has_mbyte)
8320 {
8321 char_u *n;
8322
8323 for (s = line + curwin->w_cursor.col; s > line; s = n)
8324 {
8325 n = mb_prevptr(line, s);
8326 if (!vim_iswordp(n))
8327 break;
8328 }
8329 }
8330 else
8331# endif
8332 for (s = line + curwin->w_cursor.col; s > line; --s)
8333 if (!vim_iswordc(s[-1]))
8334 break;
8335 if (s + (p - look) <= line + curwin->w_cursor.col
8336 && (icase
8337 ? MB_STRNICMP(s, look, p - look)
8338 : STRNCMP(s, look, p - look)) == 0)
8339 match = TRUE;
8340 }
8341 else
8342#endif
8343 /* TODO: multi-byte */
8344 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8345 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8346 {
8347 line = ml_get_cursor();
8348 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8349 || !vim_iswordc(line[-(p - look) - 1]))
8350 && (icase
8351 ? MB_STRNICMP(line - (p - look), look, p - look)
8352 : STRNCMP(line - (p - look), look, p - look))
8353 == 0)
8354 match = TRUE;
8355 }
8356 if (match && try_match_word && !try_match)
8357 {
8358 /* "0=word": Check if there are only blanks before the
8359 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008360 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 (int)(curwin->w_cursor.col - (p - look)))
8362 match = FALSE;
8363 }
8364 if (match)
8365 return TRUE;
8366 }
8367 look = p;
8368 }
8369
8370 /*
8371 * ok, it's a boring generic character.
8372 */
8373 else
8374 {
8375 if (try_match && *look == keytyped)
8376 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008377 if (*look != NUL)
8378 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 }
8380
8381 /*
8382 * Skip over ", ".
8383 */
8384 look = skip_to_option_part(look);
8385 }
8386 return FALSE;
8387}
8388#endif /* FEAT_CINDENT */
8389
8390#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8391/*
8392 * Map Hebrew keyboard when in hkmap mode.
8393 */
8394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008395hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8398 {
8399 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8400 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8401 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8402 static char_u map[26] =
8403 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8404 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8405 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8406 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8407 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8408 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8409 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8410 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8411 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8412
8413 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8414 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8415 /* '-1'='sofit' */
8416 else if (c == 'x')
8417 return 'X';
8418 else if (c == 'q')
8419 return '\''; /* {geresh}={'} */
8420 else if (c == 246)
8421 return ' '; /* \"o --> ' ' for a german keyboard */
8422 else if (c == 228)
8423 return ' '; /* \"a --> ' ' -- / -- */
8424 else if (c == 252)
8425 return ' '; /* \"u --> ' ' -- / -- */
8426#ifdef EBCDIC
8427 else if (islower(c))
8428#else
8429 /* NOTE: islower() does not do the right thing for us on Linux so we
8430 * do this the same was as 5.7 and previous, so it works correctly on
8431 * all systems. Specifically, the e.g. Delete and Arrow keys are
8432 * munged and won't work if e.g. searching for Hebrew text.
8433 */
8434 else if (c >= 'a' && c <= 'z')
8435#endif
8436 return (int)(map[CharOrdLow(c)] + p_aleph);
8437 else
8438 return c;
8439 }
8440 else
8441 {
8442 switch (c)
8443 {
8444 case '`': return ';';
8445 case '/': return '.';
8446 case '\'': return ',';
8447 case 'q': return '/';
8448 case 'w': return '\'';
8449
8450 /* Hebrew letters - set offset from 'a' */
8451 case ',': c = '{'; break;
8452 case '.': c = 'v'; break;
8453 case ';': c = 't'; break;
8454 default: {
8455 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8456
8457#ifdef EBCDIC
8458 /* see note about islower() above */
8459 if (!islower(c))
8460#else
8461 if (c < 'a' || c > 'z')
8462#endif
8463 return c;
8464 c = str[CharOrdLow(c)];
8465 break;
8466 }
8467 }
8468
8469 return (int)(CharOrdLow(c) + p_aleph);
8470 }
8471}
8472#endif
8473
8474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008475ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
8477 int need_redraw = FALSE;
8478 int regname;
8479 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008480 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481
8482 /*
8483 * If we are going to wait for a character, show a '"'.
8484 */
8485 pc_status = PC_STATUS_UNSET;
8486 if (redrawing() && !char_avail())
8487 {
8488 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008489 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490
8491 edit_putchar('"', TRUE);
8492#ifdef FEAT_CMDL_INFO
8493 add_to_showcmd_c(Ctrl_R);
8494#endif
8495 }
8496
8497#ifdef USE_ON_FLY_SCROLL
8498 dont_scroll = TRUE; /* disallow scrolling here */
8499#endif
8500
8501 /*
8502 * Don't map the register name. This also prevents the mode message to be
8503 * deleted when ESC is hit.
8504 */
8505 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008506 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8509 {
8510 /* Get a third key for literal register insertion */
8511 literally = regname;
8512#ifdef FEAT_CMDL_INFO
8513 add_to_showcmd_c(literally);
8514#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008515 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 }
8518 --no_mapping;
8519
8520#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008521 /* Don't call u_sync() while typing the expression or giving an error
8522 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 ++no_u_sync;
8524 if (regname == '=')
8525 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008526# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008528# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008529 /* Sync undo when evaluating the expression calls setline() or
8530 * append(), so that it can be undone separately. */
8531 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008532
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008534# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 /* Restore the Input Method. */
8536 if (im_on)
8537 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008540 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8541 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008542 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 else
8546 {
8547#endif
8548 if (literally == Ctrl_O || literally == Ctrl_P)
8549 {
8550 /* Append the command to the redo buffer. */
8551 AppendCharToRedobuff(Ctrl_R);
8552 AppendCharToRedobuff(literally);
8553 AppendCharToRedobuff(regname);
8554
8555 do_put(regname, BACKWARD, 1L,
8556 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8557 }
8558 else if (insert_reg(regname, literally) == FAIL)
8559 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008560 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 need_redraw = TRUE; /* remove the '"' */
8562 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008563 else if (stop_insert_mode)
8564 /* When the '=' register was used and a function was invoked that
8565 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8566 * insert anything, need to remove the '"' */
8567 need_redraw = TRUE;
8568
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569#ifdef FEAT_EVAL
8570 }
8571 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008572 if (u_sync_once == 1)
8573 ins_need_undo = TRUE;
8574 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575#endif
8576#ifdef FEAT_CMDL_INFO
8577 clear_showcmd();
8578#endif
8579
8580 /* If the inserted register is empty, we need to remove the '"' */
8581 if (need_redraw || stuff_empty())
8582 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008583
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008584 /* Disallow starting Visual mode here, would get a weird mode. */
8585 if (!vis_active && VIsual_active)
8586 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587}
8588
8589/*
8590 * CTRL-G commands in Insert mode.
8591 */
8592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008593ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
8595 int c;
8596
8597#ifdef FEAT_INS_EXPAND
8598 /* Right after CTRL-X the cursor will be after the ruler. */
8599 setcursor();
8600#endif
8601
8602 /*
8603 * Don't map the second key. This also prevents the mode message to be
8604 * deleted when ESC is hit.
8605 */
8606 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008607 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 --no_mapping;
8609 switch (c)
8610 {
8611 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8612 case K_UP:
8613 case Ctrl_K:
8614 case 'k': ins_up(TRUE);
8615 break;
8616
8617 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8618 case K_DOWN:
8619 case Ctrl_J:
8620 case 'j': ins_down(TRUE);
8621 break;
8622
8623 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008624 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008626
8627 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008628 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008629 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008630 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 break;
8632
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008633 /* CTRL-G U: do not break undo with the next char */
8634 case 'U':
8635 /* Allow one left/right cursor movement with the next char,
8636 * without breaking undo. */
8637 dont_sync_undo = MAYBE;
8638 break;
8639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008641 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 }
8643}
8644
8645/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008646 * CTRL-^ in Insert mode.
8647 */
8648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008649ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008650{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008651 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008652 {
8653 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8654 if (State & LANGMAP)
8655 {
8656 curbuf->b_p_iminsert = B_IMODE_NONE;
8657 State &= ~LANGMAP;
8658 }
8659 else
8660 {
8661 curbuf->b_p_iminsert = B_IMODE_LMAP;
8662 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008663#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008664 im_set_active(FALSE);
8665#endif
8666 }
8667 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008668#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008669 else
8670 {
8671 /* There are no ":lmap" mappings, toggle IM */
8672 if (im_get_status())
8673 {
8674 curbuf->b_p_iminsert = B_IMODE_NONE;
8675 im_set_active(FALSE);
8676 }
8677 else
8678 {
8679 curbuf->b_p_iminsert = B_IMODE_IM;
8680 State &= ~LANGMAP;
8681 im_set_active(TRUE);
8682 }
8683 }
8684#endif
8685 set_iminsert_global();
8686 showmode();
8687#ifdef FEAT_GUI
8688 /* may show different cursor shape or color */
8689 if (gui.in_use)
8690 gui_update_cursor(TRUE, FALSE);
8691#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008692#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008693 /* Show/unshow value of 'keymap' in status lines. */
8694 status_redraw_curbuf();
8695#endif
8696}
8697
8698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 * Handle ESC in insert mode.
8700 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8701 * insert.
8702 */
8703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008704ins_esc(
8705 long *count,
8706 int cmdchar,
8707 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708{
8709 int temp;
8710 static int disabled_redraw = FALSE;
8711
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008712#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008713 check_spell_redraw();
8714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715#if defined(FEAT_HANGULIN)
8716# if defined(ESC_CHG_TO_ENG_MODE)
8717 hangul_input_state_set(0);
8718# endif
8719 if (composing_hangul)
8720 {
8721 push_raw_key(composing_hangul_buffer, 2);
8722 composing_hangul = 0;
8723 }
8724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725
8726 temp = curwin->w_cursor.col;
8727 if (disabled_redraw)
8728 {
8729 --RedrawingDisabled;
8730 disabled_redraw = FALSE;
8731 }
8732 if (!arrow_used)
8733 {
8734 /*
8735 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008736 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8737 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 */
8739 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008740 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
8742 /*
8743 * Repeating insert may take a long time. Check for
8744 * interrupt now and then.
8745 */
8746 if (*count > 0)
8747 {
8748 line_breakcheck();
8749 if (got_int)
8750 *count = 0;
8751 }
8752
8753 if (--*count > 0) /* repeat what was typed */
8754 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008755 /* Vi repeats the insert without replacing characters. */
8756 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8757 State &= ~REPLACE_FLAG;
8758
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 (void)start_redo_ins();
8760 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008761 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 ++RedrawingDisabled;
8763 disabled_redraw = TRUE;
8764 return FALSE; /* repeat the insert */
8765 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008766 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 undisplay_dollar();
8768 }
8769
8770 /* When an autoindent was removed, curswant stays after the
8771 * indent */
8772 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8773 curwin->w_set_curswant = TRUE;
8774
8775 /* Remember the last Insert position in the '^ mark. */
8776 if (!cmdmod.keepjumps)
8777 curbuf->b_last_insert = curwin->w_cursor;
8778
8779 /*
8780 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008781 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008783 if (!nomove
8784 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785#ifdef FEAT_VIRTUALEDIT
8786 || curwin->w_cursor.coladd > 0
8787#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008788 )
8789 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008790 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791#ifdef FEAT_RIGHTLEFT
8792 && !revins_on
8793#endif
8794 )
8795 {
8796#ifdef FEAT_VIRTUALEDIT
8797 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8798 {
8799 oneleft();
8800 if (restart_edit != NUL)
8801 ++curwin->w_cursor.coladd;
8802 }
8803 else
8804#endif
8805 {
8806 --curwin->w_cursor.col;
8807#ifdef FEAT_MBYTE
8808 /* Correct cursor for multi-byte character. */
8809 if (has_mbyte)
8810 mb_adjust_cursor();
8811#endif
8812 }
8813 }
8814
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008815#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 /* Disable IM to allow typing English directly for Normal mode commands.
8817 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8818 * well). */
8819 if (!(State & LANGMAP))
8820 im_save_status(&curbuf->b_p_iminsert);
8821 im_set_active(FALSE);
8822#endif
8823
8824 State = NORMAL;
8825 /* need to position cursor again (e.g. when on a TAB ) */
8826 changed_cline_bef_curs();
8827
8828#ifdef FEAT_MOUSE
8829 setmouse();
8830#endif
8831#ifdef CURSOR_SHAPE
8832 ui_cursor_shape(); /* may show different cursor shape */
8833#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008834 if (!p_ek)
8835 /* Re-enable bracketed paste mode. */
8836 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837
8838 /*
8839 * When recording or for CTRL-O, need to display the new mode.
8840 * Otherwise remove the mode message.
8841 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008842 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 showmode();
8844 else if (p_smd)
8845 MSG("");
8846
8847 return TRUE; /* exit Insert mode */
8848}
8849
8850#ifdef FEAT_RIGHTLEFT
8851/*
8852 * Toggle language: hkmap and revins_on.
8853 * Move to end of reverse inserted text.
8854 */
8855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008856ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
8858 if (revins_on && revins_chars && revins_scol >= 0)
8859 {
8860 while (gchar_cursor() != NUL && revins_chars--)
8861 ++curwin->w_cursor.col;
8862 }
8863 p_ri = !p_ri;
8864 revins_on = (State == INSERT && p_ri);
8865 if (revins_on)
8866 {
8867 revins_scol = curwin->w_cursor.col;
8868 revins_legal++;
8869 revins_chars = 0;
8870 undisplay_dollar();
8871 }
8872 else
8873 revins_scol = -1;
8874#ifdef FEAT_FKMAP
8875 if (p_altkeymap)
8876 {
8877 /*
8878 * to be consistent also for redo command, using '.'
8879 * set arrow_used to true and stop it - causing to redo
8880 * characters entered in one mode (normal/reverse insert).
8881 */
8882 arrow_used = TRUE;
8883 (void)stop_arrow();
8884 p_fkmap = curwin->w_p_rl ^ p_ri;
8885 if (p_fkmap && p_ri)
8886 State = INSERT;
8887 }
8888 else
8889#endif
8890 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8891 showmode();
8892}
8893#endif
8894
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895/*
8896 * If 'keymodel' contains "startsel", may start selection.
8897 * Returns TRUE when a CTRL-O and other keys stuffed.
8898 */
8899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008900ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
8902 if (km_startsel)
8903 switch (c)
8904 {
8905 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 case K_PAGEUP:
8908 case K_KPAGEUP:
8909 case K_PAGEDOWN:
8910 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008911# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 case K_LEFT:
8913 case K_RIGHT:
8914 case K_UP:
8915 case K_DOWN:
8916 case K_END:
8917 case K_HOME:
8918# endif
8919 if (!(mod_mask & MOD_MASK_SHIFT))
8920 break;
8921 /* FALLTHROUGH */
8922 case K_S_LEFT:
8923 case K_S_RIGHT:
8924 case K_S_UP:
8925 case K_S_DOWN:
8926 case K_S_END:
8927 case K_S_HOME:
8928 /* Start selection right away, the cursor can move with
8929 * CTRL-O when beyond the end of the line. */
8930 start_selection();
8931
8932 /* Execute the key in (insert) Select mode. */
8933 stuffcharReadbuff(Ctrl_O);
8934 if (mod_mask)
8935 {
8936 char_u buf[4];
8937
8938 buf[0] = K_SPECIAL;
8939 buf[1] = KS_MODIFIER;
8940 buf[2] = mod_mask;
8941 buf[3] = NUL;
8942 stuffReadbuff(buf);
8943 }
8944 stuffcharReadbuff(c);
8945 return TRUE;
8946 }
8947 return FALSE;
8948}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
8950/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008951 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008952 */
8953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008954ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008955{
8956#ifdef FEAT_FKMAP
8957 if (p_fkmap && p_ri)
8958 {
8959 beep_flush();
8960 EMSG(farsi_text_3); /* encoded in Farsi */
8961 return;
8962 }
8963#endif
8964
Bram Moolenaar1e015462005-09-25 22:16:38 +00008965# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008966 set_vim_var_string(VV_INSERTMODE,
8967 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008968# ifdef FEAT_VREPLACE
8969 replaceState == VREPLACE ? "v" :
8970# endif
8971 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008972# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008973 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008974 if (State & REPLACE_FLAG)
8975 State = INSERT | (State & LANGMAP);
8976 else
8977 State = replaceState | (State & LANGMAP);
8978 AppendCharToRedobuff(K_INS);
8979 showmode();
8980#ifdef CURSOR_SHAPE
8981 ui_cursor_shape(); /* may show different cursor shape */
8982#endif
8983}
8984
8985/*
8986 * Pressed CTRL-O in Insert mode.
8987 */
8988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008989ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008990{
8991#ifdef FEAT_VREPLACE
8992 if (State & VREPLACE_FLAG)
8993 restart_edit = 'V';
8994 else
8995#endif
8996 if (State & REPLACE_FLAG)
8997 restart_edit = 'R';
8998 else
8999 restart_edit = 'I';
9000#ifdef FEAT_VIRTUALEDIT
9001 if (virtual_active())
9002 ins_at_eol = FALSE; /* cursor always keeps its column */
9003 else
9004#endif
9005 ins_at_eol = (gchar_cursor() == NUL);
9006}
9007
9008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 * If the cursor is on an indent, ^T/^D insert/delete one
9010 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00009011 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 * with vi. But vi only supports ^T and ^D after an
9013 * autoindent, we support it everywhere.
9014 */
9015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009016ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
9018 if (stop_arrow() == FAIL)
9019 return;
9020 AppendCharToRedobuff(c);
9021
9022 /*
9023 * 0^D and ^^D: remove all indent.
9024 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009025 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9026 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 {
9028 --curwin->w_cursor.col;
9029 (void)del_char(FALSE); /* delete the '^' or '0' */
9030 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9031 if (State & REPLACE_FLAG)
9032 replace_pop_ins();
9033 if (lastc == '^')
9034 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009035 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 }
9037 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009038 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039
9040 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9041 did_ai = FALSE;
9042#ifdef FEAT_SMARTINDENT
9043 did_si = FALSE;
9044 can_si = FALSE;
9045 can_si_back = FALSE;
9046#endif
9047#ifdef FEAT_CINDENT
9048 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9049#endif
9050}
9051
9052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009053ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054{
9055 int temp;
9056
9057 if (stop_arrow() == FAIL)
9058 return;
9059 if (gchar_cursor() == NUL) /* delete newline */
9060 {
9061 temp = curwin->w_cursor.col;
9062 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009063 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009064 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009068#ifdef FEAT_VREPLACE
9069 /* Adjust orig_line_count in case more lines have been deleted than
9070 * have been added. That makes sure, that open_line() later
9071 * can access all buffer lines correctly */
9072 if (State & VREPLACE_FLAG &&
9073 orig_line_count > curbuf->b_ml.ml_line_count)
9074 orig_line_count = curbuf->b_ml.ml_line_count;
9075#endif
9076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009078 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9079 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 did_ai = FALSE;
9081#ifdef FEAT_SMARTINDENT
9082 did_si = FALSE;
9083 can_si = FALSE;
9084 can_si_back = FALSE;
9085#endif
9086 AppendCharToRedobuff(K_DEL);
9087}
9088
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009089static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009090
9091/*
9092 * Delete one character for ins_bs().
9093 */
9094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009095ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009096{
9097 dec_cursor();
9098 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9099 if (State & REPLACE_FLAG)
9100 {
9101 /* Don't delete characters before the insert point when in
9102 * Replace mode */
9103 if (curwin->w_cursor.lnum != Insstart.lnum
9104 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009105 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009106 }
9107 else
9108 (void)del_char(FALSE);
9109}
9110
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111/*
9112 * Handle Backspace, delete-word and delete-line in Insert mode.
9113 * Return TRUE when backspace was actually used.
9114 */
9115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009116ins_bs(
9117 int c,
9118 int mode,
9119 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 linenr_T lnum;
9122 int cc;
9123 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009124 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 colnr_T mincol;
9126 int did_backspace = FALSE;
9127 int in_indent;
9128 int oldState;
9129#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009130 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131#endif
9132
9133 /*
9134 * can't delete anything in an empty file
9135 * can't backup past first character in buffer
9136 * can't backup past starting point unless 'backspace' > 1
9137 * can backup to a previous line if 'backspace' == 0
9138 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009139 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 || (
9141#ifdef FEAT_RIGHTLEFT
9142 !revins_on &&
9143#endif
9144 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9145 || (!can_bs(BS_START)
9146 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009147 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9148 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9150 && curwin->w_cursor.col <= ai_col)
9151 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9152 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009153 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 return FALSE;
9155 }
9156
9157 if (stop_arrow() == FAIL)
9158 return FALSE;
9159 in_indent = inindent(0);
9160#ifdef FEAT_CINDENT
9161 if (in_indent)
9162 can_cindent = FALSE;
9163#endif
9164#ifdef FEAT_COMMENTS
9165 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9166#endif
9167#ifdef FEAT_RIGHTLEFT
9168 if (revins_on) /* put cursor after last inserted char */
9169 inc_cursor();
9170#endif
9171
9172#ifdef FEAT_VIRTUALEDIT
9173 /* Virtualedit:
9174 * BACKSPACE_CHAR eats a virtual space
9175 * BACKSPACE_WORD eats all coladd
9176 * BACKSPACE_LINE eats all coladd and keeps going
9177 */
9178 if (curwin->w_cursor.coladd > 0)
9179 {
9180 if (mode == BACKSPACE_CHAR)
9181 {
9182 --curwin->w_cursor.coladd;
9183 return TRUE;
9184 }
9185 if (mode == BACKSPACE_WORD)
9186 {
9187 curwin->w_cursor.coladd = 0;
9188 return TRUE;
9189 }
9190 curwin->w_cursor.coladd = 0;
9191 }
9192#endif
9193
9194 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009195 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 */
9197 if (curwin->w_cursor.col == 0)
9198 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009199 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009200 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201#ifdef FEAT_RIGHTLEFT
9202 || revins_on
9203#endif
9204 )
9205 {
9206 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9207 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9208 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009209 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009210 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 }
9212 /*
9213 * In replace mode:
9214 * cc < 0: NL was inserted, delete it
9215 * cc >= 0: NL was replaced, put original characters back
9216 */
9217 cc = -1;
9218 if (State & REPLACE_FLAG)
9219 cc = replace_pop(); /* returns -1 if NL was inserted */
9220 /*
9221 * In replace mode, in the line we started replacing, we only move the
9222 * cursor.
9223 */
9224 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9225 {
9226 dec_cursor();
9227 }
9228 else
9229 {
9230#ifdef FEAT_VREPLACE
9231 if (!(State & VREPLACE_FLAG)
9232 || curwin->w_cursor.lnum > orig_line_count)
9233#endif
9234 {
9235 temp = gchar_cursor(); /* remember current char */
9236 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009237
9238 /* When "aw" is in 'formatoptions' we must delete the space at
9239 * the end of the line, otherwise the line will be broken
9240 * again when auto-formatting. */
9241 if (has_format_option(FO_AUTO)
9242 && has_format_option(FO_WHITE_PAR))
9243 {
9244 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9245 TRUE);
9246 int len;
9247
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009248 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009249 if (len > 0 && ptr[len - 1] == ' ')
9250 ptr[len - 1] = NUL;
9251 }
9252
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009253 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 if (temp == NUL && gchar_cursor() != NUL)
9255 inc_cursor();
9256 }
9257#ifdef FEAT_VREPLACE
9258 else
9259 dec_cursor();
9260#endif
9261
9262 /*
9263 * In REPLACE mode we have to put back the text that was replaced
9264 * by the NL. On the replace stack is first a NUL-terminated
9265 * sequence of characters that were deleted and then the
9266 * characters that NL replaced.
9267 */
9268 if (State & REPLACE_FLAG)
9269 {
9270 /*
9271 * Do the next ins_char() in NORMAL state, to
9272 * prevent ins_char() from replacing characters and
9273 * avoiding showmatch().
9274 */
9275 oldState = State;
9276 State = NORMAL;
9277 /*
9278 * restore characters (blanks) deleted after cursor
9279 */
9280 while (cc > 0)
9281 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009282 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283#ifdef FEAT_MBYTE
9284 mb_replace_pop_ins(cc);
9285#else
9286 ins_char(cc);
9287#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009288 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 cc = replace_pop();
9290 }
9291 /* restore the characters that NL replaced */
9292 replace_pop_ins();
9293 State = oldState;
9294 }
9295 }
9296 did_ai = FALSE;
9297 }
9298 else
9299 {
9300 /*
9301 * Delete character(s) before the cursor.
9302 */
9303#ifdef FEAT_RIGHTLEFT
9304 if (revins_on) /* put cursor on last inserted char */
9305 dec_cursor();
9306#endif
9307 mincol = 0;
9308 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009309 if (mode == BACKSPACE_LINE
9310 && (curbuf->b_p_ai
9311#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009312 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009313#endif
9314 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315#ifdef FEAT_RIGHTLEFT
9316 && !revins_on
9317#endif
9318 )
9319 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009320 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009322 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009324 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 }
9326
9327 /*
9328 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9329 */
9330 if ( mode == BACKSPACE_CHAR
9331 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009332 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009333 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 && (*(ml_get_cursor() - 1) == TAB
9335 || (*(ml_get_cursor() - 1) == ' '
9336 && (!*inserted_space_p
9337 || arrow_used))))))
9338 {
9339 int ts;
9340 colnr_T vcol;
9341 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009342 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343
9344 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009345 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009346 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009348 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 /* Compute the virtual column where we want to be. Since
9350 * 'showbreak' may get in the way, need to get the last column of
9351 * the previous character. */
9352 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009353 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 dec_cursor();
9355 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9356 inc_cursor();
9357 want_vcol = (want_vcol / ts) * ts;
9358
9359 /* delete characters until we are at or before want_vcol */
9360 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009361 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009362 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363
9364 /* insert extra spaces until we are at want_vcol */
9365 while (vcol < want_vcol)
9366 {
9367 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009368 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9369 && curwin->w_cursor.col < Insstart_orig.col)
9370 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371
9372#ifdef FEAT_VREPLACE
9373 if (State & VREPLACE_FLAG)
9374 ins_char(' ');
9375 else
9376#endif
9377 {
9378 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009379 if ((State & REPLACE_FLAG))
9380 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 }
9382 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9383 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009384
9385 /* If we are now back where we started delete one character. Can
9386 * happen when using 'sts' and 'linebreak'. */
9387 if (vcol >= start_vcol)
9388 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 }
9390
9391 /*
9392 * Delete upto starting point, start of line or previous word.
9393 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009394 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009396#ifdef FEAT_MBYTE
9397 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009399 if (has_mbyte)
9400 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009402 do
9403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009405 if (!revins_on) /* put cursor on char to be deleted */
9406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009408
9409 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009411 /* look multi-byte character class */
9412 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009414 prev_cclass = cclass;
9415 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009418
9419 /* start of word? */
9420 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9421 {
9422 mode = BACKSPACE_WORD_NOT_SPACE;
9423 temp = vim_iswordc(cc);
9424 }
9425 /* end of word? */
9426 else if (mode == BACKSPACE_WORD_NOT_SPACE
9427 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9428#ifdef FEAT_MBYTE
9429 || prev_cclass != cclass
9430#endif
9431 ))
9432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009434 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009436 inc_cursor();
9437#ifdef FEAT_RIGHTLEFT
9438 else if (State & REPLACE_FLAG)
9439 dec_cursor();
9440#endif
9441 break;
9442 }
9443 if (State & REPLACE_FLAG)
9444 replace_do_bs(-1);
9445 else
9446 {
9447#ifdef FEAT_MBYTE
9448 if (enc_utf8 && p_deco)
9449 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9450#endif
9451 (void)del_char(FALSE);
9452#ifdef FEAT_MBYTE
9453 /*
9454 * If there are combining characters and 'delcombine' is set
9455 * move the cursor back. Don't back up before the base
9456 * character.
9457 */
9458 if (enc_utf8 && p_deco && cpc[0] != NUL)
9459 inc_cursor();
9460#endif
9461#ifdef FEAT_RIGHTLEFT
9462 if (revins_chars)
9463 {
9464 revins_chars--;
9465 revins_legal++;
9466 }
9467 if (revins_on && gchar_cursor() == NUL)
9468 break;
9469#endif
9470 }
9471 /* Just a single backspace?: */
9472 if (mode == BACKSPACE_CHAR)
9473 break;
9474 } while (
9475#ifdef FEAT_RIGHTLEFT
9476 revins_on ||
9477#endif
9478 (curwin->w_cursor.col > mincol
9479 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9480 || curwin->w_cursor.col != Insstart_orig.col)));
9481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 did_backspace = TRUE;
9483 }
9484#ifdef FEAT_SMARTINDENT
9485 did_si = FALSE;
9486 can_si = FALSE;
9487 can_si_back = FALSE;
9488#endif
9489 if (curwin->w_cursor.col <= 1)
9490 did_ai = FALSE;
9491 /*
9492 * It's a little strange to put backspaces into the redo
9493 * buffer, but it makes auto-indent a lot easier to deal
9494 * with.
9495 */
9496 AppendCharToRedobuff(c);
9497
9498 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009499 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009500 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009501 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502
9503 /* vi behaviour: the cursor moves backward but the character that
9504 * was there remains visible
9505 * Vim behaviour: the cursor moves backward and the character that
9506 * was there is erased from the screen.
9507 * We can emulate the vi behaviour by pretending there is a dollar
9508 * displayed even when there isn't.
9509 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009510 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 dollar_vcol = curwin->w_virtcol;
9512
Bram Moolenaarce3be472008-01-14 19:12:28 +00009513#ifdef FEAT_FOLDING
9514 /* When deleting a char the cursor line must never be in a closed fold.
9515 * E.g., when 'foldmethod' is indent and deleting the first non-white
9516 * char before a Tab. */
9517 if (did_backspace)
9518 foldOpenCursor();
9519#endif
9520
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 return did_backspace;
9522}
9523
9524#ifdef FEAT_MOUSE
9525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009526ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
9528 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009529 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530
9531# ifdef FEAT_GUI
9532 /* When GUI is active, also move/paste when 'mouse' is empty */
9533 if (!gui.in_use)
9534# endif
9535 if (!mouse_has(MOUSE_INSERT))
9536 return;
9537
9538 undisplay_dollar();
9539 tpos = curwin->w_cursor;
9540 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9541 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009542 win_T *new_curwin = curwin;
9543
9544 if (curwin != old_curwin && win_valid(old_curwin))
9545 {
9546 /* Mouse took us to another window. We need to go back to the
9547 * previous one to stop insert there properly. */
9548 curwin = old_curwin;
9549 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009550#ifdef FEAT_JOB_CHANNEL
9551 if (bt_prompt(curbuf))
9552 // Restart Insert mode when re-entering the prompt buffer.
9553 curbuf->b_prompt_insert = 'A';
9554#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009555 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009556 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009557 if (curwin != new_curwin && win_valid(new_curwin))
9558 {
9559 curwin = new_curwin;
9560 curbuf = curwin->w_buffer;
9561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562# ifdef FEAT_CINDENT
9563 can_cindent = TRUE;
9564# endif
9565 }
9566
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 /* redraw status lines (in case another window became active) */
9568 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009572ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
9574 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009575 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009576# ifdef FEAT_INS_EXPAND
9577 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578# endif
9579
9580 tpos = curwin->w_cursor;
9581
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009582 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
9584 int row, col;
9585
9586 row = mouse_row;
9587 col = mouse_col;
9588
9589 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009590 wp = mouse_find_win(&row, &col);
9591 if (wp == NULL)
9592 return;
9593 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 curbuf = curwin->w_buffer;
9595 }
9596 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 undisplay_dollar();
9598
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009599# ifdef FEAT_INS_EXPAND
9600 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009601 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009602# endif
9603 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009604 if (dir == MSCR_DOWN || dir == MSCR_UP)
9605 {
9606 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9607 scroll_redraw(dir,
9608 (long)(curwin->w_botline - curwin->w_topline));
9609 else
9610 scroll_redraw(dir, 3L);
9611 }
9612#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009613 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009614 {
9615 int val, step = 6;
9616
9617 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009618 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009619 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9620 if (val < 0)
9621 val = 0;
9622 gui_do_horiz_scroll(val, TRUE);
9623 }
9624#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009625# ifdef FEAT_INS_EXPAND
9626 did_scroll = TRUE;
9627# endif
9628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 curwin->w_redr_status = TRUE;
9631
9632 curwin = old_curwin;
9633 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009635# ifdef FEAT_INS_EXPAND
9636 /* The popup menu may overlay the window, need to redraw it.
9637 * TODO: Would be more efficient to only redraw the windows that are
9638 * overlapped by the popup menu. */
9639 if (pum_visible() && did_scroll)
9640 {
9641 redraw_all_later(NOT_VALID);
9642 ins_compl_show_pum();
9643 }
9644# endif
9645
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009646 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 {
9648 start_arrow(&tpos);
9649# ifdef FEAT_CINDENT
9650 can_cindent = TRUE;
9651# endif
9652 }
9653}
9654#endif
9655
Bram Moolenaarec2da362017-01-21 20:04:22 +01009656/*
9657 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9658 * P_PE literally.
9659 * When "drop" is TRUE then consume the text and drop it.
9660 */
9661 int
9662bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9663{
9664 int c;
9665 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9666 int idx = 0;
9667 char_u *end = find_termcode((char_u *)"PE");
9668 int ret_char = -1;
9669 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009670 int save_paste = p_paste;
9671 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009672
9673 /* If the end code is too long we can't detect it, read everything. */
9674 if (STRLEN(end) >= NUMBUFLEN)
9675 end = NULL;
9676 ++no_mapping;
9677 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009678 p_paste = TRUE;
9679 curbuf->b_p_ai = FALSE;
9680
Bram Moolenaarec2da362017-01-21 20:04:22 +01009681 for (;;)
9682 {
9683 /* When the end is not defined read everything. */
9684 if (end == NULL && vpeekc() == NUL)
9685 break;
9686 c = plain_vgetc();
9687#ifdef FEAT_MBYTE
9688 if (has_mbyte)
9689 idx += (*mb_char2bytes)(c, buf + idx);
9690 else
9691#endif
9692 buf[idx++] = c;
9693 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009694 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009695 {
9696 if (end[idx] == NUL)
9697 break; /* Found the end of paste code. */
9698 continue;
9699 }
9700 if (!drop)
9701 {
9702 switch (mode)
9703 {
9704 case PASTE_CMDLINE:
9705 put_on_cmdline(buf, idx, TRUE);
9706 break;
9707
9708 case PASTE_EX:
9709 if (gap != NULL && ga_grow(gap, idx) == OK)
9710 {
9711 mch_memmove((char *)gap->ga_data + gap->ga_len,
9712 buf, (size_t)idx);
9713 gap->ga_len += idx;
9714 }
9715 break;
9716
9717 case PASTE_INSERT:
9718 if (stop_arrow() == OK)
9719 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009720 c = buf[0];
9721 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9722 ins_eol(c);
9723 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009724 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009725 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009726 AppendToRedobuffLit(buf, idx);
9727 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009728 }
9729 break;
9730
9731 case PASTE_ONE_CHAR:
9732 if (ret_char == -1)
9733 {
9734#ifdef FEAT_MBYTE
9735 if (has_mbyte)
9736 ret_char = (*mb_ptr2char)(buf);
9737 else
9738#endif
9739 ret_char = buf[0];
9740 }
9741 break;
9742 }
9743 }
9744 idx = 0;
9745 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009746
Bram Moolenaarec2da362017-01-21 20:04:22 +01009747 --no_mapping;
9748 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009749 p_paste = save_paste;
9750 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009751
9752 return ret_char;
9753}
9754
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009755#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009757ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009758{
9759 /* We will be leaving the current window, unless closing another tab. */
9760 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9761 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9762 {
9763 undisplay_dollar();
9764 start_arrow(&curwin->w_cursor);
9765# ifdef FEAT_CINDENT
9766 can_cindent = TRUE;
9767# endif
9768 }
9769
9770 if (c == K_TABLINE)
9771 goto_tabpage(current_tab);
9772 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009773 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009774 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009775 redraw_statuslines(); /* will redraw the tabline when needed */
9776 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009777}
9778#endif
9779
9780#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009782ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 pos_T tpos;
9785
9786 undisplay_dollar();
9787 tpos = curwin->w_cursor;
9788 if (gui_do_scroll())
9789 {
9790 start_arrow(&tpos);
9791# ifdef FEAT_CINDENT
9792 can_cindent = TRUE;
9793# endif
9794 }
9795}
9796
9797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009798ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
9800 pos_T tpos;
9801
9802 undisplay_dollar();
9803 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009804 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 {
9806 start_arrow(&tpos);
9807# ifdef FEAT_CINDENT
9808 can_cindent = TRUE;
9809# endif
9810 }
9811}
9812#endif
9813
9814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815ins_left(
9816 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817{
9818 pos_T tpos;
9819
9820#ifdef FEAT_FOLDING
9821 if ((fdo_flags & FDO_HOR) && KeyTyped)
9822 foldOpenCursor();
9823#endif
9824 undisplay_dollar();
9825 tpos = curwin->w_cursor;
9826 if (oneleft() == OK)
9827 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009828#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9829 /* Only call start_arrow() when not busy with preediting, it will
9830 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009831 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009832#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009833 {
9834 start_arrow_with_change(&tpos, end_change);
9835 if (!end_change)
9836 AppendCharToRedobuff(K_LEFT);
9837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838#ifdef FEAT_RIGHTLEFT
9839 /* If exit reversed string, position is fixed */
9840 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9841 revins_legal++;
9842 revins_chars++;
9843#endif
9844 }
9845
9846 /*
9847 * if 'whichwrap' set for cursor in insert mode may go to
9848 * previous line
9849 */
9850 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9851 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009852 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 start_arrow(&tpos);
9854 --(curwin->w_cursor.lnum);
9855 coladvance((colnr_T)MAXCOL);
9856 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9857 }
9858 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009859 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009860 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861}
9862
9863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009864ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865{
9866 pos_T tpos;
9867
9868#ifdef FEAT_FOLDING
9869 if ((fdo_flags & FDO_HOR) && KeyTyped)
9870 foldOpenCursor();
9871#endif
9872 undisplay_dollar();
9873 tpos = curwin->w_cursor;
9874 if (c == K_C_HOME)
9875 curwin->w_cursor.lnum = 1;
9876 curwin->w_cursor.col = 0;
9877#ifdef FEAT_VIRTUALEDIT
9878 curwin->w_cursor.coladd = 0;
9879#endif
9880 curwin->w_curswant = 0;
9881 start_arrow(&tpos);
9882}
9883
9884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009885ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886{
9887 pos_T tpos;
9888
9889#ifdef FEAT_FOLDING
9890 if ((fdo_flags & FDO_HOR) && KeyTyped)
9891 foldOpenCursor();
9892#endif
9893 undisplay_dollar();
9894 tpos = curwin->w_cursor;
9895 if (c == K_C_END)
9896 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9897 coladvance((colnr_T)MAXCOL);
9898 curwin->w_curswant = MAXCOL;
9899
9900 start_arrow(&tpos);
9901}
9902
9903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009904ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905{
9906#ifdef FEAT_FOLDING
9907 if ((fdo_flags & FDO_HOR) && KeyTyped)
9908 foldOpenCursor();
9909#endif
9910 undisplay_dollar();
9911 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9912 {
9913 start_arrow(&curwin->w_cursor);
9914 (void)bck_word(1L, FALSE, FALSE);
9915 curwin->w_set_curswant = TRUE;
9916 }
9917 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009918 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919}
9920
9921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922ins_right(
9923 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925#ifdef FEAT_FOLDING
9926 if ((fdo_flags & FDO_HOR) && KeyTyped)
9927 foldOpenCursor();
9928#endif
9929 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009930 if (gchar_cursor() != NUL
9931#ifdef FEAT_VIRTUALEDIT
9932 || virtual_active()
9933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 )
9935 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009936 start_arrow_with_change(&curwin->w_cursor, end_change);
9937 if (!end_change)
9938 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 curwin->w_set_curswant = TRUE;
9940#ifdef FEAT_VIRTUALEDIT
9941 if (virtual_active())
9942 oneright();
9943 else
9944#endif
9945 {
9946#ifdef FEAT_MBYTE
9947 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009948 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 else
9950#endif
9951 ++curwin->w_cursor.col;
9952 }
9953
9954#ifdef FEAT_RIGHTLEFT
9955 revins_legal++;
9956 if (revins_chars)
9957 revins_chars--;
9958#endif
9959 }
9960 /* if 'whichwrap' set for cursor in insert mode, may move the
9961 * cursor to the next line */
9962 else if (vim_strchr(p_ww, ']') != NULL
9963 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9964 {
9965 start_arrow(&curwin->w_cursor);
9966 curwin->w_set_curswant = TRUE;
9967 ++curwin->w_cursor.lnum;
9968 curwin->w_cursor.col = 0;
9969 }
9970 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009971 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009972 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973}
9974
9975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009976ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
9978#ifdef FEAT_FOLDING
9979 if ((fdo_flags & FDO_HOR) && KeyTyped)
9980 foldOpenCursor();
9981#endif
9982 undisplay_dollar();
9983 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9984 || gchar_cursor() != NUL)
9985 {
9986 start_arrow(&curwin->w_cursor);
9987 (void)fwd_word(1L, FALSE, 0);
9988 curwin->w_set_curswant = TRUE;
9989 }
9990 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009991 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992}
9993
9994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009995ins_up(
9996 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
9998 pos_T tpos;
9999 linenr_T old_topline = curwin->w_topline;
10000#ifdef FEAT_DIFF
10001 int old_topfill = curwin->w_topfill;
10002#endif
10003
10004 undisplay_dollar();
10005 tpos = curwin->w_cursor;
10006 if (cursor_up(1L, TRUE) == OK)
10007 {
10008 if (startcol)
10009 coladvance(getvcol_nolist(&Insstart));
10010 if (old_topline != curwin->w_topline
10011#ifdef FEAT_DIFF
10012 || old_topfill != curwin->w_topfill
10013#endif
10014 )
10015 redraw_later(VALID);
10016 start_arrow(&tpos);
10017#ifdef FEAT_CINDENT
10018 can_cindent = TRUE;
10019#endif
10020 }
10021 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010022 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023}
10024
10025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010026ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027{
10028 pos_T tpos;
10029
10030 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010031
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010032 if (mod_mask & MOD_MASK_CTRL)
10033 {
10034 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010035 if (first_tabpage->tp_next != NULL)
10036 {
10037 start_arrow(&curwin->w_cursor);
10038 goto_tabpage(-1);
10039 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010040 return;
10041 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010042
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 tpos = curwin->w_cursor;
10044 if (onepage(BACKWARD, 1L) == OK)
10045 {
10046 start_arrow(&tpos);
10047#ifdef FEAT_CINDENT
10048 can_cindent = TRUE;
10049#endif
10050 }
10051 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010052 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053}
10054
10055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010056ins_down(
10057 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
10059 pos_T tpos;
10060 linenr_T old_topline = curwin->w_topline;
10061#ifdef FEAT_DIFF
10062 int old_topfill = curwin->w_topfill;
10063#endif
10064
10065 undisplay_dollar();
10066 tpos = curwin->w_cursor;
10067 if (cursor_down(1L, TRUE) == OK)
10068 {
10069 if (startcol)
10070 coladvance(getvcol_nolist(&Insstart));
10071 if (old_topline != curwin->w_topline
10072#ifdef FEAT_DIFF
10073 || old_topfill != curwin->w_topfill
10074#endif
10075 )
10076 redraw_later(VALID);
10077 start_arrow(&tpos);
10078#ifdef FEAT_CINDENT
10079 can_cindent = TRUE;
10080#endif
10081 }
10082 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010083 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
10086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010087ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088{
10089 pos_T tpos;
10090
10091 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010092
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010093 if (mod_mask & MOD_MASK_CTRL)
10094 {
10095 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010096 if (first_tabpage->tp_next != NULL)
10097 {
10098 start_arrow(&curwin->w_cursor);
10099 goto_tabpage(0);
10100 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010101 return;
10102 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010103
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 tpos = curwin->w_cursor;
10105 if (onepage(FORWARD, 1L) == OK)
10106 {
10107 start_arrow(&tpos);
10108#ifdef FEAT_CINDENT
10109 can_cindent = TRUE;
10110#endif
10111 }
10112 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010113 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114}
10115
10116#ifdef FEAT_DND
10117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010118ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
10120 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10121}
10122#endif
10123
10124/*
10125 * Handle TAB in Insert or Replace mode.
10126 * Return TRUE when the TAB needs to be inserted like a normal character.
10127 */
10128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010129ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130{
10131 int ind;
10132 int i;
10133 int temp;
10134
10135 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10136 Insstart_blank_vcol = get_nolist_virtcol();
10137 if (echeck_abbr(TAB + ABBR_OFF))
10138 return FALSE;
10139
10140 ind = inindent(0);
10141#ifdef FEAT_CINDENT
10142 if (ind)
10143 can_cindent = FALSE;
10144#endif
10145
10146 /*
10147 * When nothing special, insert TAB like a normal character
10148 */
10149 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010150 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010151 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 return TRUE;
10153
10154 if (stop_arrow() == FAIL)
10155 return TRUE;
10156
10157 did_ai = FALSE;
10158#ifdef FEAT_SMARTINDENT
10159 did_si = FALSE;
10160 can_si = FALSE;
10161 can_si_back = FALSE;
10162#endif
10163 AppendToRedobuff((char_u *)"\t");
10164
10165 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010166 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010167 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10168 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 else /* otherwise use 'tabstop' */
10170 temp = (int)curbuf->b_p_ts;
10171 temp -= get_nolist_virtcol() % temp;
10172
10173 /*
10174 * Insert the first space with ins_char(). It will delete one char in
10175 * replace mode. Insert the rest with ins_str(); it will not delete any
10176 * chars. For VREPLACE mode, we use ins_char() for all characters.
10177 */
10178 ins_char(' ');
10179 while (--temp > 0)
10180 {
10181#ifdef FEAT_VREPLACE
10182 if (State & VREPLACE_FLAG)
10183 ins_char(' ');
10184 else
10185#endif
10186 {
10187 ins_str((char_u *)" ");
10188 if (State & REPLACE_FLAG) /* no char replaced */
10189 replace_push(NUL);
10190 }
10191 }
10192
10193 /*
10194 * When 'expandtab' not set: Replace spaces by TABs where possible.
10195 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010196 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 {
10198 char_u *ptr;
10199#ifdef FEAT_VREPLACE
10200 char_u *saved_line = NULL; /* init for GCC */
10201 pos_T pos;
10202#endif
10203 pos_T fpos;
10204 pos_T *cursor;
10205 colnr_T want_vcol, vcol;
10206 int change_col = -1;
10207 int save_list = curwin->w_p_list;
10208
10209 /*
10210 * Get the current line. For VREPLACE mode, don't make real changes
10211 * yet, just work on a copy of the line.
10212 */
10213#ifdef FEAT_VREPLACE
10214 if (State & VREPLACE_FLAG)
10215 {
10216 pos = curwin->w_cursor;
10217 cursor = &pos;
10218 saved_line = vim_strsave(ml_get_curline());
10219 if (saved_line == NULL)
10220 return FALSE;
10221 ptr = saved_line + pos.col;
10222 }
10223 else
10224#endif
10225 {
10226 ptr = ml_get_cursor();
10227 cursor = &curwin->w_cursor;
10228 }
10229
10230 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10231 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10232 curwin->w_p_list = FALSE;
10233
10234 /* Find first white before the cursor */
10235 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010236 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 {
10238 --fpos.col;
10239 --ptr;
10240 }
10241
10242 /* In Replace mode, don't change characters before the insert point. */
10243 if ((State & REPLACE_FLAG)
10244 && fpos.lnum == Insstart.lnum
10245 && fpos.col < Insstart.col)
10246 {
10247 ptr += Insstart.col - fpos.col;
10248 fpos.col = Insstart.col;
10249 }
10250
10251 /* compute virtual column numbers of first white and cursor */
10252 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10253 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10254
Bram Moolenaar597a4222014-06-25 14:39:50 +020010255 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10256 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010257 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010259 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 if (vcol + i > want_vcol)
10261 break;
10262 if (*ptr != TAB)
10263 {
10264 *ptr = TAB;
10265 if (change_col < 0)
10266 {
10267 change_col = fpos.col; /* Column of first change */
10268 /* May have to adjust Insstart */
10269 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10270 Insstart.col = fpos.col;
10271 }
10272 }
10273 ++fpos.col;
10274 ++ptr;
10275 vcol += i;
10276 }
10277
10278 if (change_col >= 0)
10279 {
10280 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010281 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282
10283 /* Skip over the spaces we need. */
10284 while (vcol < want_vcol && *ptr == ' ')
10285 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010286 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 ++ptr;
10288 ++repl_off;
10289 }
10290 if (vcol > want_vcol)
10291 {
10292 /* Must have a char with 'showbreak' just before it. */
10293 --ptr;
10294 --repl_off;
10295 }
10296 fpos.col += repl_off;
10297
10298 /* Delete following spaces. */
10299 i = cursor->col - fpos.col;
10300 if (i > 0)
10301 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010302 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 /* correct replace stack. */
10304 if ((State & REPLACE_FLAG)
10305#ifdef FEAT_VREPLACE
10306 && !(State & VREPLACE_FLAG)
10307#endif
10308 )
10309 for (temp = i; --temp >= 0; )
10310 replace_join(repl_off);
10311 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010312#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010313 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010314 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010315 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010316 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10317 (char_u *)"\t", 1);
10318 }
10319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 cursor->col -= i;
10321
10322#ifdef FEAT_VREPLACE
10323 /*
10324 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10325 * backspacing over the changed spacing and then inserting the new
10326 * spacing.
10327 */
10328 if (State & VREPLACE_FLAG)
10329 {
10330 /* Backspace from real cursor to change_col */
10331 backspace_until_column(change_col);
10332
10333 /* Insert each char in saved_line from changed_col to
10334 * ptr-cursor */
10335 ins_bytes_len(saved_line + change_col,
10336 cursor->col - change_col);
10337 }
10338#endif
10339 }
10340
10341#ifdef FEAT_VREPLACE
10342 if (State & VREPLACE_FLAG)
10343 vim_free(saved_line);
10344#endif
10345 curwin->w_p_list = save_list;
10346 }
10347
10348 return FALSE;
10349}
10350
10351/*
10352 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010353 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 */
10355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010356ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357{
10358 int i;
10359
10360 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010361 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 undisplay_dollar();
10365
10366 /*
10367 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10368 * character under the cursor. Only push a NUL on the replace stack,
10369 * nothing to put back when the NL is deleted.
10370 */
10371 if ((State & REPLACE_FLAG)
10372#ifdef FEAT_VREPLACE
10373 && !(State & VREPLACE_FLAG)
10374#endif
10375 )
10376 replace_push(NUL);
10377
10378 /*
10379 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10380 * replacing the next line, so we push all of the characters left on the
10381 * line onto the replace stack. This is not done here though, it is done
10382 * in open_line().
10383 */
10384
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010385#ifdef FEAT_VIRTUALEDIT
10386 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10387 * CTRL-O). */
10388 if (virtual_active() && curwin->w_cursor.coladd > 0)
10389 coladvance(getviscol());
10390#endif
10391
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392#ifdef FEAT_RIGHTLEFT
10393# ifdef FEAT_FKMAP
10394 if (p_altkeymap && p_fkmap)
10395 fkmap(NL);
10396# endif
10397 /* NL in reverse insert will always start in the end of
10398 * current line. */
10399 if (revins_on)
10400 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10401#endif
10402
10403 AppendToRedobuff(NL_STR);
10404 i = open_line(FORWARD,
10405#ifdef FEAT_COMMENTS
10406 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10407#endif
10408 0, old_indent);
10409 old_indent = 0;
10410#ifdef FEAT_CINDENT
10411 can_cindent = TRUE;
10412#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010413#ifdef FEAT_FOLDING
10414 /* When inserting a line the cursor line must never be in a closed fold. */
10415 foldOpenCursor();
10416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010418 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419}
10420
10421#ifdef FEAT_DIGRAPHS
10422/*
10423 * Handle digraph in insert mode.
10424 * Returns character still to be inserted, or NUL when nothing remaining to be
10425 * done.
10426 */
10427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010428ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429{
10430 int c;
10431 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010432 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433
10434 pc_status = PC_STATUS_UNSET;
10435 if (redrawing() && !char_avail())
10436 {
10437 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010438 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439
10440 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010441 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442#ifdef FEAT_CMDL_INFO
10443 add_to_showcmd_c(Ctrl_K);
10444#endif
10445 }
10446
10447#ifdef USE_ON_FLY_SCROLL
10448 dont_scroll = TRUE; /* disallow scrolling here */
10449#endif
10450
10451 /* don't map the digraph chars. This also prevents the
10452 * mode message to be deleted when ESC is hit */
10453 ++no_mapping;
10454 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010455 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 --no_mapping;
10457 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010458 if (did_putchar)
10459 /* when the line fits in 'columns' the '?' is at the start of the next
10460 * line and will not be removed by the redraw */
10461 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010462
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 if (IS_SPECIAL(c) || mod_mask) /* special key */
10464 {
10465#ifdef FEAT_CMDL_INFO
10466 clear_showcmd();
10467#endif
10468 insert_special(c, TRUE, FALSE);
10469 return NUL;
10470 }
10471 if (c != ESC)
10472 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010473 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 if (redrawing() && !char_avail())
10475 {
10476 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010477 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478
10479 if (char2cells(c) == 1)
10480 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010481 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010483 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 }
10485#ifdef FEAT_CMDL_INFO
10486 add_to_showcmd_c(c);
10487#endif
10488 }
10489 ++no_mapping;
10490 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010491 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 --no_mapping;
10493 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010494 if (did_putchar)
10495 /* when the line fits in 'columns' the '?' is at the start of the
10496 * next line and will not be removed by a redraw */
10497 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 if (cc != ESC)
10499 {
10500 AppendToRedobuff((char_u *)CTRL_V_STR);
10501 c = getdigraph(c, cc, TRUE);
10502#ifdef FEAT_CMDL_INFO
10503 clear_showcmd();
10504#endif
10505 return c;
10506 }
10507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508#ifdef FEAT_CMDL_INFO
10509 clear_showcmd();
10510#endif
10511 return NUL;
10512}
10513#endif
10514
10515/*
10516 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10517 * Returns the char to be inserted, or NUL if none found.
10518 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010520ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521{
10522 int c;
10523 int temp;
10524 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010525 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526
10527 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10528 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010529 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 return NUL;
10531 }
10532
10533 /* try to advance to the cursor column */
10534 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010535 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 prev_ptr = ptr;
10537 validate_virtcol();
10538 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10539 {
10540 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010541 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 }
10543 if ((colnr_T)temp > curwin->w_virtcol)
10544 ptr = prev_ptr;
10545
10546#ifdef FEAT_MBYTE
10547 c = (*mb_ptr2char)(ptr);
10548#else
10549 c = *ptr;
10550#endif
10551 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010552 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 return c;
10554}
10555
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010556/*
10557 * CTRL-Y or CTRL-E typed in Insert mode.
10558 */
10559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010560ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010561{
10562 int c = tc;
10563
10564#ifdef FEAT_INS_EXPAND
10565 if (ctrl_x_mode == CTRL_X_SCROLL)
10566 {
10567 if (c == Ctrl_Y)
10568 scrolldown_clamp();
10569 else
10570 scrollup_clamp();
10571 redraw_later(VALID);
10572 }
10573 else
10574#endif
10575 {
10576 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10577 if (c != NUL)
10578 {
10579 long tw_save;
10580
10581 /* The character must be taken literally, insert like it
10582 * was typed after a CTRL-V, and pretend 'textwidth'
10583 * wasn't set. Digits, 'o' and 'x' are special after a
10584 * CTRL-V, don't use it for these. */
10585 if (c < 256 && !isalnum(c))
10586 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10587 tw_save = curbuf->b_p_tw;
10588 curbuf->b_p_tw = -1;
10589 insert_special(c, TRUE, FALSE);
10590 curbuf->b_p_tw = tw_save;
10591#ifdef FEAT_RIGHTLEFT
10592 revins_chars++;
10593 revins_legal++;
10594#endif
10595 c = Ctrl_V; /* pretend CTRL-V is last character */
10596 auto_format(FALSE, TRUE);
10597 }
10598 }
10599 return c;
10600}
10601
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602#ifdef FEAT_SMARTINDENT
10603/*
10604 * Try to do some very smart auto-indenting.
10605 * Used when inserting a "normal" character.
10606 */
10607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010608ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609{
10610 pos_T *pos, old_pos;
10611 char_u *ptr;
10612 int i;
10613 int temp;
10614
10615 /*
10616 * do some very smart indenting when entering '{' or '}'
10617 */
10618 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10619 {
10620 /*
10621 * for '}' set indent equal to indent of line containing matching '{'
10622 */
10623 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10624 {
10625 old_pos = curwin->w_cursor;
10626 /*
10627 * If the matching '{' has a ')' immediately before it (ignoring
10628 * white-space), then line up with the start of the line
10629 * containing the matching '(' if there is one. This handles the
10630 * case where an "if (..\n..) {" statement continues over multiple
10631 * lines -- webb
10632 */
10633 ptr = ml_get(pos->lnum);
10634 i = pos->col;
10635 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010636 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 ;
10638 curwin->w_cursor.lnum = pos->lnum;
10639 curwin->w_cursor.col = i;
10640 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10641 curwin->w_cursor = *pos;
10642 i = get_indent();
10643 curwin->w_cursor = old_pos;
10644#ifdef FEAT_VREPLACE
10645 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010646 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 else
10648#endif
10649 (void)set_indent(i, SIN_CHANGED);
10650 }
10651 else if (curwin->w_cursor.col > 0)
10652 {
10653 /*
10654 * when inserting '{' after "O" reduce indent, but not
10655 * more than indent of previous line
10656 */
10657 temp = TRUE;
10658 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10659 {
10660 old_pos = curwin->w_cursor;
10661 i = get_indent();
10662 while (curwin->w_cursor.lnum > 1)
10663 {
10664 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10665
10666 /* ignore empty lines and lines starting with '#'. */
10667 if (*ptr != '#' && *ptr != NUL)
10668 break;
10669 }
10670 if (get_indent() >= i)
10671 temp = FALSE;
10672 curwin->w_cursor = old_pos;
10673 }
10674 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010675 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 }
10677 }
10678
10679 /*
10680 * set indent of '#' always to 0
10681 */
10682 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10683 {
10684 /* remember current indent for next line */
10685 old_indent = get_indent();
10686 (void)set_indent(0, SIN_CHANGED);
10687 }
10688
10689 /* Adjust ai_col, the char at this position can be deleted. */
10690 if (ai_col > curwin->w_cursor.col)
10691 ai_col = curwin->w_cursor.col;
10692}
10693#endif
10694
10695/*
10696 * Get the value that w_virtcol would have when 'list' is off.
10697 * Unless 'cpo' contains the 'L' flag.
10698 */
10699 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010700get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
10702 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10703 return getvcol_nolist(&curwin->w_cursor);
10704 validate_virtcol();
10705 return curwin->w_virtcol;
10706}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010707
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010708#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010709/*
10710 * Handle the InsertCharPre autocommand.
10711 * "c" is the character that was typed.
10712 * Return a pointer to allocated memory with the replacement string.
10713 * Return NULL to continue inserting "c".
10714 */
10715 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010716do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010717{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010718 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010719 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010720
10721 /* Return quickly when there is nothing to do. */
10722 if (!has_insertcharpre())
10723 return NULL;
10724
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010725# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010726 if (has_mbyte)
10727 buf[(*mb_char2bytes)(c, buf)] = NUL;
10728 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010729# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010730 {
10731 buf[0] = c;
10732 buf[1] = NUL;
10733 }
10734
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010735 /* Lock the text to avoid weird things from happening. */
10736 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010737 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010738
Bram Moolenaar704984a2012-06-01 14:57:51 +020010739 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010740 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010741 {
10742 /* Get the value of v:char. It may be empty or more than one
10743 * character. Only use it when changed, otherwise continue with the
10744 * original character to avoid breaking autoindent. */
10745 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10746 res = vim_strsave(get_vim_var_str(VV_CHAR));
10747 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010748
10749 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10750 --textlock;
10751
10752 return res;
10753}
10754#endif