blob: 45176d79f497272d56326c39134e6338ba41cd61 [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);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 if (cmdchar == 'V' || cmdchar == 'v')
467 {
468 /* "gR" or "gr" command */
469 AppendCharToRedobuff('g');
470 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
471 }
472 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100474 if (cmdchar == K_PS)
475 AppendCharToRedobuff('a');
476 else
477 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (cmdchar == 'g') /* "gI" command */
479 AppendCharToRedobuff('I');
480 else if (cmdchar == 'r') /* "r<CR>" command */
481 count = 1; /* insert only one <CR> */
482 }
483 }
484
485 if (cmdchar == 'R')
486 {
487#ifdef FEAT_FKMAP
488 if (p_fkmap && p_ri)
489 {
490 beep_flush();
491 EMSG(farsi_text_3); /* encoded in Farsi */
492 State = INSERT;
493 }
494 else
495#endif
496 State = REPLACE;
497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 else if (cmdchar == 'V' || cmdchar == 'v')
499 {
500 State = VREPLACE;
501 replaceState = VREPLACE;
502 orig_line_count = curbuf->b_ml.ml_line_count;
503 vr_lines_changed = 1;
504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 else
506 State = INSERT;
507
508 stop_insert_mode = FALSE;
509
510 /*
511 * Need to recompute the cursor position, it might move when the cursor is
512 * on a TAB or special character.
513 */
514 curs_columns(TRUE);
515
516 /*
517 * Enable langmap or IME, indicated by 'iminsert'.
518 * Note that IME may enabled/disabled without us noticing here, thus the
519 * 'iminsert' value may not reflect what is actually used. It is updated
520 * when hitting <Esc>.
521 */
522 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
523 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100524#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
526#endif
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#ifdef FEAT_MOUSE
529 setmouse();
530#endif
531#ifdef FEAT_CMDL_INFO
532 clear_showcmd();
533#endif
534#ifdef FEAT_RIGHTLEFT
535 /* there is no reverse replace mode */
536 revins_on = (State == INSERT && p_ri);
537 if (revins_on)
538 undisplay_dollar();
539 revins_chars = 0;
540 revins_legal = 0;
541 revins_scol = -1;
542#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100543 if (!p_ek)
544 /* Disable bracketed paste mode, we won't recognize the escape
545 * sequences. */
546 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 /*
549 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100550 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
551 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 if (restart_edit != 0 && stuff_empty())
554 {
555#ifdef FEAT_MOUSE
556 /*
557 * After a paste we consider text typed to be part of the insert for
558 * the pasted text. You can backspace over the pasted text too.
559 */
560 if (where_paste_started.lnum)
561 arrow_used = FALSE;
562 else
563#endif
564 arrow_used = TRUE;
565 restart_edit = 0;
566
567 /*
568 * If the cursor was after the end-of-line before the CTRL-O and it is
569 * now at the end-of-line, put it after the end-of-line (this is not
570 * correct in very rare cases).
571 * Also do this if curswant is greater than the current virtual
572 * column. Eg after "^O$" or "^O80|".
573 */
574 validate_virtcol();
575 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 || curwin->w_curswant > curwin->w_virtcol)
578 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
579 {
580 if (ptr[1] == NUL)
581 ++curwin->w_cursor.col;
582#ifdef FEAT_MBYTE
583 else if (has_mbyte)
584 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000585 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (ptr[i] == NUL)
587 curwin->w_cursor.col += i;
588 }
589#endif
590 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000591 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 else
594 arrow_used = FALSE;
595
596 /* we are in insert mode now, don't need to start it anymore */
597 need_start_insertmode = FALSE;
598
599 /* Need to save the line for undo before inserting the first char. */
600 ins_need_undo = TRUE;
601
602#ifdef FEAT_MOUSE
603 where_paste_started.lnum = 0;
604#endif
605#ifdef FEAT_CINDENT
606 can_cindent = TRUE;
607#endif
608#ifdef FEAT_FOLDING
609 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
610 * restarting. */
611 if (!p_im && did_restart_edit == 0)
612 foldOpenCursor();
613#endif
614
615 /*
616 * If 'showmode' is set, show the current (insert/replace/..) mode.
617 * A warning message for changing a readonly file is given here, before
618 * actually changing anything. It's put after the mode, if any.
619 */
620 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000621 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 i = showmode();
623
624 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000625 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627#ifdef CURSOR_SHAPE
628 ui_cursor_shape(); /* may show different cursor shape */
629#endif
630#ifdef FEAT_DIGRAPHS
631 do_digraph(-1); /* clear digraphs */
632#endif
633
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000634 /*
635 * Get the current length of the redo buffer, those characters have to be
636 * skipped if we want to get to the inserted characters.
637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ptr = get_inserted();
639 if (ptr == NULL)
640 new_insert_skip = 0;
641 else
642 {
643 new_insert_skip = (int)STRLEN(ptr);
644 vim_free(ptr);
645 }
646
647 old_indent = 0;
648
649 /*
650 * Main loop in Insert mode: repeat until Insert mode is left.
651 */
652 for (;;)
653 {
654#ifdef FEAT_RIGHTLEFT
655 if (!revins_legal)
656 revins_scol = -1; /* reset on illegal motions */
657 else
658 revins_legal = 0;
659#endif
660 if (arrow_used) /* don't repeat insert when arrow key used */
661 count = 0;
662
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100663 if (update_Insstart_orig)
664 Insstart_orig = Insstart;
665
Bram Moolenaar00672e12016-06-26 18:38:13 +0200666 if (stop_insert_mode
667#ifdef FEAT_INS_EXPAND
668 && !pum_visible()
669#endif
670 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
672 /* ":stopinsert" used or 'insertmode' reset */
673 count = 0;
674 goto doESCkey;
675 }
676
677 /* set curwin->w_curswant for next K_DOWN or K_UP */
678 if (!arrow_used)
679 curwin->w_set_curswant = TRUE;
680
681 /* If there is no typeahead may check for timestamps (e.g., for when a
682 * menu invoked a shell command). */
683 if (stuff_empty())
684 {
685 did_check_timestamps = FALSE;
686 if (need_check_timestamps)
687 check_timestamps(FALSE);
688 }
689
690 /*
691 * When emsg() was called msg_scroll will have been set.
692 */
693 msg_scroll = FALSE;
694
695#ifdef FEAT_GUI
696 /* When 'mousefocus' is set a mouse movement may have taken us to
697 * another window. "need_mouse_correct" may then be set because of an
698 * autocommand. */
699 if (need_mouse_correct)
700 gui_mouse_correct();
701#endif
702
703#ifdef FEAT_FOLDING
704 /* Open fold at the cursor line, according to 'foldopen'. */
705 if (fdo_flags & FDO_INSERT)
706 foldOpenCursor();
707 /* Close folds where the cursor isn't, according to 'foldclose' */
708 if (!char_avail())
709 foldCheckClose();
710#endif
711
Bram Moolenaarf2732452018-06-03 14:47:35 +0200712#ifdef FEAT_JOB_CHANNEL
713 if (bt_prompt(curbuf))
714 {
715 init_prompt(cmdchar_todo);
716 cmdchar_todo = NUL;
717 }
718#endif
719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 /*
721 * If we inserted a character at the last position of the last line in
722 * the window, scroll the window one line up. This avoids an extra
723 * redraw.
724 * This is detected when the cursor column is smaller after inserting
725 * something.
726 * Don't do this when the topline changed already, it has
727 * already been adjusted (by insertchar() calling open_line())).
728 */
729 if (curbuf->b_mod_set
730 && curwin->w_p_wrap
731 && !did_backspace
732 && curwin->w_topline == old_topline
733#ifdef FEAT_DIFF
734 && curwin->w_topfill == old_topfill
735#endif
736 )
737 {
738 mincol = curwin->w_wcol;
739 validate_cursor_col();
740
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200741 if (
742#ifdef FEAT_VARTABS
743 (int)curwin->w_wcol < mincol - tabstop_at(
744 get_nolist_virtcol(), curbuf->b_p_ts,
745 curbuf->b_p_vts_array)
746#else
747 (int)curwin->w_wcol < mincol - curbuf->b_p_ts
748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 && curwin->w_wrow == W_WINROW(curwin)
750 + curwin->w_height - 1 - p_so
751 && (curwin->w_cursor.lnum != curwin->w_topline
752#ifdef FEAT_DIFF
753 || curwin->w_topfill > 0
754#endif
755 ))
756 {
757#ifdef FEAT_DIFF
758 if (curwin->w_topfill > 0)
759 --curwin->w_topfill;
760 else
761#endif
762#ifdef FEAT_FOLDING
763 if (hasFolding(curwin->w_topline, NULL, &old_topline))
764 set_topline(curwin, old_topline + 1);
765 else
766#endif
767 set_topline(curwin, curwin->w_topline + 1);
768 }
769 }
770
771 /* May need to adjust w_topline to show the cursor. */
772 update_topline();
773
774 did_backspace = FALSE;
775
776 validate_cursor(); /* may set must_redraw */
777
778 /*
779 * Redraw the display when no characters are waiting.
780 * Also shows mode, ruler and positions cursor.
781 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000782 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (curwin->w_p_scb)
785 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
Bram Moolenaar860cae12010-06-05 23:22:07 +0200787 if (curwin->w_p_crb)
788 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 update_curswant();
790 old_topline = curwin->w_topline;
791#ifdef FEAT_DIFF
792 old_topfill = curwin->w_topfill;
793#endif
794
795#ifdef USE_ON_FLY_SCROLL
796 dont_scroll = FALSE; /* allow scrolling here */
797#endif
798
799 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100800 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100802 if (c != K_CURSORHOLD)
803 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200804
805 /* After using CTRL-G U the next cursor key will not break undo. */
806 if (dont_sync_undo == MAYBE)
807 dont_sync_undo = TRUE;
808 else
809 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100810 if (cmdchar == K_PS)
811 /* Got here from normal mode when bracketed paste started. */
812 c = K_PS;
813 else
814 do
815 {
816 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200817
818 if (stop_insert_mode)
819 {
820 // Insert mode ended, possibly from a callback.
821 count = 0;
822 nomove = TRUE;
823 goto doESCkey;
824 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100825 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000827 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
828 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef FEAT_RIGHTLEFT
831 if (p_hkmap && KeyTyped)
832 c = hkmap(c); /* Hebrew mode mapping */
833#endif
834#ifdef FEAT_FKMAP
835 if (p_fkmap && KeyTyped)
836 c = fkmap(c); /* Farsi mode mapping */
837#endif
838
839#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 /*
841 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000842 * and the cursor is still in the completed word. Only when there is
843 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000844 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000845 if (compl_started
846 && pum_wanted()
847 && curwin->w_cursor.col >= compl_col
848 && (compl_shown_match == NULL
849 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000850 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000851 /* BS: Delete one character from "compl_leader". */
852 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000853 && curwin->w_cursor.col > compl_col
854 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000855 continue;
856
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000857 /* When no match was selected or it was edited. */
858 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000859 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000860 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000861 * "compl_leader". Except when at the original match and
862 * there is nothing to add, CTRL-L works like CTRL-P then. */
863 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100864 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000865 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000866 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000867 {
868 ins_compl_addfrommatch();
869 continue;
870 }
871
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000872 /* A non-white character that fits in with the current
873 * completion: Add to "compl_leader". */
874 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000875 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100876#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100877 /* Trigger InsertCharPre. */
878 char_u *str = do_insert_char_pre(c);
879 char_u *p;
880
881 if (str != NULL)
882 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100883 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100884 ins_compl_addleader(PTR2CHAR(p));
885 vim_free(str);
886 }
887 else
888#endif
889 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000890 continue;
891 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000892
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000893 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000894 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200895 if ((c == Ctrl_Y || (compl_enter_selects
896 && (c == CAR || c == K_KENTER || c == NL)))
897 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000898 {
899 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200900 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000901 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000902 }
903 }
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
906 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000907 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000908 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000909 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
911
Bram Moolenaar488c6512005-08-11 20:09:58 +0000912 /* CTRL-\ CTRL-N goes to Normal mode,
913 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
914 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (c == Ctrl_BSL)
916 {
917 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000918 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 ++no_mapping;
920 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000921 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 --no_mapping;
923 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000924 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000926 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 vungetc(c);
928 c = Ctrl_BSL;
929 }
930 else if (c == Ctrl_G && p_im)
931 continue;
932 else
933 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000934 if (c == Ctrl_O)
935 {
936 ins_ctrl_o();
937 ins_at_eol = FALSE; /* cursor keeps its column */
938 nomove = TRUE;
939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 count = 0;
941 goto doESCkey;
942 }
943 }
944
945#ifdef FEAT_DIGRAPHS
946 c = do_digraph(c);
947#endif
948
949#ifdef FEAT_INS_EXPAND
950 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
951 goto docomplete;
952#endif
953 if (c == Ctrl_V || c == Ctrl_Q)
954 {
955 ins_ctrl_v();
956 c = Ctrl_V; /* pretend CTRL-V is last typed character */
957 continue;
958 }
959
960#ifdef FEAT_CINDENT
961 if (cindent_on()
962# ifdef FEAT_INS_EXPAND
963 && ctrl_x_mode == 0
964# endif
965 )
966 {
967 /* A key name preceded by a bang means this key is not to be
968 * inserted. Skip ahead to the re-indenting below.
969 * A key name preceded by a star means that indenting has to be
970 * done before inserting the key. */
971 line_is_white = inindent(0);
972 if (in_cinkeys(c, '!', line_is_white))
973 goto force_cindent;
974 if (can_cindent && in_cinkeys(c, '*', line_is_white)
975 && stop_arrow() == OK)
976 do_c_expr_indent();
977 }
978#endif
979
980#ifdef FEAT_RIGHTLEFT
981 if (curwin->w_p_rl)
982 switch (c)
983 {
984 case K_LEFT: c = K_RIGHT; break;
985 case K_S_LEFT: c = K_S_RIGHT; break;
986 case K_C_LEFT: c = K_C_RIGHT; break;
987 case K_RIGHT: c = K_LEFT; break;
988 case K_S_RIGHT: c = K_S_LEFT; break;
989 case K_C_RIGHT: c = K_C_LEFT; break;
990 }
991#endif
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 /*
994 * If 'keymodel' contains "startsel", may start selection. If it
995 * does, a CTRL-O and c will be stuffed, we need to get these
996 * characters.
997 */
998 if (ins_start_select(c))
999 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 /*
1002 * The big switch to handle a character in insert mode.
1003 */
1004 switch (c)
1005 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001006 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (echeck_abbr(ESC + ABBR_OFF))
1008 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001009 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001011 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#ifdef FEAT_CMDWIN
1013 if (c == Ctrl_C && cmdwin_type != 0)
1014 {
1015 /* Close the cmdline window. */
1016 cmdwin_result = K_IGNORE;
1017 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001018 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 goto doESCkey;
1020 }
1021#endif
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02001022#ifdef FEAT_JOB_CHANNEL
1023 if (c == Ctrl_C && bt_prompt(curbuf))
1024 {
1025 if (invoke_prompt_interrupt())
1026 {
1027 if (!bt_prompt(curbuf))
1028 // buffer changed to a non-prompt buffer, get out of
1029 // Insert mode
1030 goto doESCkey;
1031 break;
1032 }
1033 }
1034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036#ifdef UNIX
1037do_intr:
1038#endif
1039 /* when 'insertmode' set, and not halfway a mapping, don't leave
1040 * Insert mode */
1041 if (goto_im())
1042 {
1043 if (got_int)
1044 {
1045 (void)vgetc(); /* flush all buffers */
1046 got_int = FALSE;
1047 }
1048 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001049 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 break;
1051 }
1052doESCkey:
1053 /*
1054 * This is the ONLY return from edit()!
1055 */
1056 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1057 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001058 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 o_lnum = curwin->w_cursor.lnum;
1060
Bram Moolenaar488c6512005-08-11 20:09:58 +00001061 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001062 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00001063 if (cmdchar != 'r' && cmdchar != 'v')
1064 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1065 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001066 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 continue;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case Ctrl_Z: /* suspend when 'insertmode' set */
1072 if (!p_im)
1073 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001074 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001075#ifdef CURSOR_SHAPE
1076 ui_cursor_shape(); /* may need to update cursor shape */
1077#endif
1078 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079
1080 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001081#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001082 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 goto docomplete;
1084#endif
1085 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1086 break;
1087 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001088
1089#ifdef FEAT_VIRTUALEDIT
1090 /* don't move the cursor left when 'virtualedit' has "onemore". */
1091 if (ve_flags & VE_ONEMORE)
1092 {
1093 ins_at_eol = FALSE;
1094 nomove = TRUE;
1095 }
1096#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 count = 0;
1098 goto doESCkey;
1099
Bram Moolenaar572cb562005-08-05 21:35:02 +00001100 case K_INS: /* toggle insert/replace mode */
1101 case K_KINS:
1102 ins_insert(replaceState);
1103 break;
1104
1105 case K_SELECT: /* end of Select mode mapping - ignore */
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_HELP: /* Help key works like <ESC> <Help> */
1109 case K_F1:
1110 case K_XF1:
1111 stuffcharReadbuff(K_HELP);
1112 if (p_im)
1113 need_start_insertmode = TRUE;
1114 goto doESCkey;
1115
1116#ifdef FEAT_NETBEANS_INTG
1117 case K_F21: /* NetBeans command */
1118 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001119 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 --no_mapping;
1121 netbeans_keycommand(i);
1122 break;
1123#endif
1124
1125 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 case NUL:
1127 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 /* For ^@ the trailing ESC will end the insert, unless there is an
1129 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1131 && c != Ctrl_A && !p_im)
1132 goto doESCkey; /* quit insert mode */
1133 inserted_space = FALSE;
1134 break;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 ins_reg();
1138 auto_format(FALSE, TRUE);
1139 inserted_space = FALSE;
1140 break;
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 ins_ctrl_g();
1144 break;
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case Ctrl_HAT: /* switch input mode and/or langmap */
1147 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 break;
1149
1150#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (!p_ari)
1153 goto normalchar;
1154 ins_ctrl_();
1155 break;
1156#endif
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1160 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1161 goto docomplete;
1162#endif
1163 /* FALLTHROUGH */
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166# ifdef FEAT_INS_EXPAND
1167 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1168 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 if (has_compl_option(FALSE))
1170 goto docomplete;
1171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173# endif
1174 ins_shift(c, lastc);
1175 auto_format(FALSE, TRUE);
1176 inserted_space = FALSE;
1177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 case K_KDEL:
1181 ins_del();
1182 auto_format(FALSE, TRUE);
1183 break;
1184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case Ctrl_H:
1187 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1188 auto_format(FALSE, TRUE);
1189 break;
1190
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001191 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001192#ifdef FEAT_JOB_CHANNEL
1193 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1194 {
1195 // In a prompt window CTRL-W is used for window commands.
1196 // Use Shift-CTRL-W to delete a word.
1197 stuffcharReadbuff(Ctrl_W);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001198 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001199 nomove = TRUE;
1200 count = 0;
1201 goto doESCkey;
1202 }
1203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1205 auto_format(FALSE, TRUE);
1206 break;
1207
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001208 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001209# ifdef FEAT_COMPL_FUNC
1210 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001212 goto docomplete;
1213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1215 auto_format(FALSE, TRUE);
1216 inserted_space = FALSE;
1217 break;
1218
1219#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 case K_LEFTMOUSE_NM:
1222 case K_LEFTDRAG:
1223 case K_LEFTRELEASE:
1224 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001225 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 case K_MIDDLEMOUSE:
1227 case K_MIDDLEDRAG:
1228 case K_MIDDLERELEASE:
1229 case K_RIGHTMOUSE:
1230 case K_RIGHTDRAG:
1231 case K_RIGHTRELEASE:
1232 case K_X1MOUSE:
1233 case K_X1DRAG:
1234 case K_X1RELEASE:
1235 case K_X2MOUSE:
1236 case K_X2DRAG:
1237 case K_X2RELEASE:
1238 ins_mouse(c);
1239 break;
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001242 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 break;
1244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001245 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001246 ins_mousescroll(MSCR_UP);
1247 break;
1248
1249 case K_MOUSELEFT: /* Scroll wheel left */
1250 ins_mousescroll(MSCR_LEFT);
1251 break;
1252
1253 case K_MOUSERIGHT: /* Scroll wheel right */
1254 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 break;
1256#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001257 case K_PS:
1258 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1259 if (cmdchar == K_PS)
1260 /* invoked from normal mode, bail out */
1261 goto doESCkey;
1262 break;
1263 case K_PE:
1264 /* Got K_PE without K_PS, ignore. */
1265 break;
1266
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001267#ifdef FEAT_GUI_TABLINE
1268 case K_TABLINE:
1269 case K_TABMENU:
1270 ins_tabline(c);
1271 break;
1272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001274 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 break;
1276
Bram Moolenaar754b5602006-02-09 23:53:20 +00001277 case K_CURSORHOLD: /* Didn't type something for a while. */
1278 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1279 did_cursorhold = TRUE;
1280 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001281
Bram Moolenaar4770d092006-01-12 23:22:24 +00001282#ifdef FEAT_GUI_W32
1283 /* On Win32 ignore <M-F4>, we get it when closing the window was
1284 * cancelled. */
1285 case K_F4:
1286 if (mod_mask != MOD_MASK_ALT)
1287 goto normalchar;
1288 break;
1289#endif
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#ifdef FEAT_GUI
1292 case K_VER_SCROLLBAR:
1293 ins_scroll();
1294 break;
1295
1296 case K_HOR_SCROLLBAR:
1297 ins_horscroll();
1298 break;
1299#endif
1300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001301 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 case K_S_HOME:
1304 case K_C_HOME:
1305 ins_home(c);
1306 break;
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 case K_S_END:
1311 case K_C_END:
1312 ins_end(c);
1313 break;
1314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001315 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001316 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1317 ins_s_left();
1318 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001319 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 break;
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 case K_C_LEFT:
1324 ins_s_left();
1325 break;
1326
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001327 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001328 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1329 ins_s_right();
1330 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001331 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 case K_C_RIGHT:
1336 ins_s_right();
1337 break;
1338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001340#ifdef FEAT_INS_EXPAND
1341 if (pum_visible())
1342 goto docomplete;
1343#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001344 if (mod_mask & MOD_MASK_SHIFT)
1345 ins_pageup();
1346 else
1347 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 break;
1349
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001350 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 case K_PAGEUP:
1352 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001353#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001354 if (pum_visible())
1355 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 ins_pageup();
1358 break;
1359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001361#ifdef FEAT_INS_EXPAND
1362 if (pum_visible())
1363 goto docomplete;
1364#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001365 if (mod_mask & MOD_MASK_SHIFT)
1366 ins_pagedown();
1367 else
1368 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 break;
1370
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001371 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 case K_PAGEDOWN:
1373 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001374#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001375 if (pum_visible())
1376 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 ins_pagedown();
1379 break;
1380
1381#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 ins_drop();
1384 break;
1385#endif
1386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 c = TAB;
1389 /* FALLTHROUGH */
1390
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1393 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1394 goto docomplete;
1395#endif
1396 inserted_space = FALSE;
1397 if (ins_tab())
1398 goto normalchar; /* insert TAB as a normal char */
1399 auto_format(FALSE, TRUE);
1400 break;
1401
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001402 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 c = CAR;
1404 /* FALLTHROUGH */
1405 case CAR:
1406 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001407#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /* In a quickfix window a <CR> jumps to the error under the
1409 * cursor. */
1410 if (bt_quickfix(curbuf) && c == CAR)
1411 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001412 if (curwin->w_llist_ref == NULL) /* quickfix window */
1413 do_cmdline_cmd((char_u *)".cc");
1414 else /* location list window */
1415 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 break;
1417 }
1418#endif
1419#ifdef FEAT_CMDWIN
1420 if (cmdwin_type != 0)
1421 {
1422 /* Execute the command in the cmdline window. */
1423 cmdwin_result = CAR;
1424 goto doESCkey;
1425 }
1426#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001427#ifdef FEAT_JOB_CHANNEL
1428 if (bt_prompt(curbuf))
1429 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001430 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001431 if (!bt_prompt(curbuf))
1432 // buffer changed to a non-prompt buffer, get out of
1433 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001434 goto doESCkey;
1435 break;
1436 }
1437#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001438 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 goto doESCkey; /* out of memory */
1440 auto_format(FALSE, FALSE);
1441 inserted_space = FALSE;
1442 break;
1443
Bram Moolenaar860cae12010-06-05 23:22:07 +02001444#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001445 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446# ifdef FEAT_INS_EXPAND
1447 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1448 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001449 if (has_compl_option(TRUE))
1450 goto docomplete;
1451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453# endif
1454# ifdef FEAT_DIGRAPHS
1455 c = ins_digraph();
1456 if (c == NUL)
1457 break;
1458# endif
1459 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001463 case Ctrl_X: /* Enter CTRL-X mode */
1464 ins_ctrl_x();
1465 break;
1466
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001467 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (ctrl_x_mode != CTRL_X_TAGS)
1469 goto normalchar;
1470 goto docomplete;
1471
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001472 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (ctrl_x_mode != CTRL_X_FILES)
1474 goto normalchar;
1475 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001476
1477 case 's': /* Spelling completion after ^X */
1478 case Ctrl_S:
1479 if (ctrl_x_mode != CTRL_X_SPELL)
1480 goto normalchar;
1481 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#endif
1483
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001484 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#ifdef FEAT_INS_EXPAND
1486 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1487#endif
1488 {
1489 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1490 if (p_im)
1491 {
1492 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1493 break;
1494 goto doESCkey;
1495 }
1496 goto normalchar;
1497 }
1498#ifdef FEAT_INS_EXPAND
1499 /* FALLTHROUGH */
1500
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001501 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 case Ctrl_N:
1503 /* if 'complete' is empty then plain ^P is no longer special,
1504 * but it is under other ^X modes */
1505 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001506 && (ctrl_x_mode == CTRL_X_NORMAL
1507 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001508 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 goto normalchar;
1510
1511docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001512 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001513#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001514 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001515#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001516 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001517 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001518#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001519 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001520#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001521 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 break;
1523#endif /* FEAT_INS_EXPAND */
1524
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001525 case Ctrl_Y: /* copy from previous line or scroll down */
1526 case Ctrl_E: /* copy from next line or scroll up */
1527 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 break;
1529
1530 default:
1531#ifdef UNIX
1532 if (c == intr_char) /* special interrupt char */
1533 goto do_intr;
1534#endif
1535
Bram Moolenaare659c952011-05-19 17:25:41 +02001536normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001538 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001540#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001541 if (!p_paste)
1542 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001543 /* Trigger InsertCharPre. */
1544 char_u *str = do_insert_char_pre(c);
1545 char_u *p;
1546
1547 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001548 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001549 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001550 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001551 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001552 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001553 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001554 c = PTR2CHAR(p);
1555 if (c == CAR || c == K_KENTER || c == NL)
1556 ins_eol(c);
1557 else
1558 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001559 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001560 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001561 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001562 vim_free(str);
1563 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001564 }
1565
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001566 /* If the new value is already inserted or an empty string
1567 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001568 if (c == NUL)
1569 break;
1570 }
1571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572#ifdef FEAT_SMARTINDENT
1573 /* Try to perform smart-indenting. */
1574 ins_try_si(c);
1575#endif
1576
1577 if (c == ' ')
1578 {
1579 inserted_space = TRUE;
1580#ifdef FEAT_CINDENT
1581 if (inindent(0))
1582 can_cindent = FALSE;
1583#endif
1584 if (Insstart_blank_vcol == MAXCOL
1585 && curwin->w_cursor.lnum == Insstart.lnum)
1586 Insstart_blank_vcol = get_nolist_virtcol();
1587 }
1588
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001589 /* Insert a normal character and check for abbreviations on a
1590 * special character. Let CTRL-] expand abbreviations without
1591 * inserting it. */
1592 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#ifdef FEAT_MBYTE
1594 /* Add ABBR_OFF for characters above 0x100, this is
1595 * what check_abbr() expects. */
1596 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1597#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001598 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 insert_special(c, FALSE, FALSE);
1601#ifdef FEAT_RIGHTLEFT
1602 revins_legal++;
1603 revins_chars++;
1604#endif
1605 }
1606
1607 auto_format(FALSE, TRUE);
1608
1609#ifdef FEAT_FOLDING
1610 /* When inserting a character the cursor line must never be in a
1611 * closed fold. */
1612 foldOpenCursor();
1613#endif
1614 break;
1615 } /* end of switch (c) */
1616
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001617 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001618 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001619#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001620 /* but not in CTRL-X mode, a script can't restore the state */
1621 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001622#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001623 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001624 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 /* If the cursor was moved we didn't just insert a space */
1627 if (arrow_used)
1628 inserted_space = FALSE;
1629
1630#ifdef FEAT_CINDENT
1631 if (can_cindent && cindent_on()
1632# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001633 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634# endif
1635 )
1636 {
1637force_cindent:
1638 /*
1639 * Indent now if a key was typed that is in 'cinkeys'.
1640 */
1641 if (in_cinkeys(c, ' ', line_is_white))
1642 {
1643 if (stop_arrow() == OK)
1644 /* re-indent the current line */
1645 do_c_expr_indent();
1646 }
1647 }
1648#endif /* FEAT_CINDENT */
1649
1650 } /* for (;;) */
1651 /* NOTREACHED */
1652}
1653
1654/*
1655 * Redraw for Insert mode.
1656 * This is postponed until getting the next character to make '$' in the 'cpo'
1657 * option work correctly.
1658 * Only redraw when there are no characters available. This speeds up
1659 * inserting sequences of characters (e.g., for CTRL-R).
1660 */
1661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662ins_redraw(
1663 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001665#ifdef FEAT_CONCEAL
1666 linenr_T conceal_old_cursor_line = 0;
1667 linenr_T conceal_new_cursor_line = 0;
1668 int conceal_update_lines = FALSE;
1669#endif
1670
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 if (char_avail())
1672 return;
1673
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001675 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1676 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001677 if (ready && (has_cursormovedI()
1678# if defined(FEAT_CONCEAL)
1679 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001680# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001681 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001682 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001683# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001684 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001685# endif
1686 )
1687 {
1688# ifdef FEAT_SYN_HL
1689 /* Need to update the screen first, to make sure syntax
1690 * highlighting is correct after making a change (e.g., inserting
1691 * a "(". The autocommand may also require a redraw, so it's done
1692 * again below, unfortunately. */
1693 if (syntax_present(curwin) && must_redraw)
1694 update_screen(0);
1695# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001696 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001697 {
1698 /* Make sure curswant is correct, an autocommand may call
1699 * getcurpos(). */
1700 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001701 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001702 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001703# ifdef FEAT_CONCEAL
1704 if (curwin->w_p_cole > 0)
1705 {
1706 conceal_old_cursor_line = last_cursormoved.lnum;
1707 conceal_new_cursor_line = curwin->w_cursor.lnum;
1708 conceal_update_lines = TRUE;
1709 }
1710# endif
1711 last_cursormoved = curwin->w_cursor;
1712 }
1713#endif
1714
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001715 /* Trigger TextChangedI if b_changedtick differs. */
1716 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001717 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001718#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001719 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001721 )
1722 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001723 aco_save_T aco;
1724
1725 // save and restore curwin and curbuf, in case the autocmd changes them
1726 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001727 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001728 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001729 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001731
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001732#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001733 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1734 * TextChangedI will need to trigger for backwards compatibility, thus use
1735 * different b_last_changedtick* variables. */
1736 if (ready && has_textchangedP()
1737 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1738 && pum_visible())
1739 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001740 aco_save_T aco;
1741
1742 // save and restore curwin and curbuf, in case the autocmd changes them
1743 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001744 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001745 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001746 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1747 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001748#endif
1749
1750 if (must_redraw)
1751 update_screen(0);
1752 else if (clear_cmdline || redraw_cmdline)
1753 showmode(); /* clear cmdline and show mode */
1754# if defined(FEAT_CONCEAL)
1755 if ((conceal_update_lines
1756 && (conceal_old_cursor_line != conceal_new_cursor_line
1757 || conceal_cursor_line(curwin)))
1758 || need_cursor_line_redraw)
1759 {
1760 if (conceal_old_cursor_line != conceal_new_cursor_line)
1761 update_single_line(curwin, conceal_old_cursor_line);
1762 update_single_line(curwin, conceal_new_cursor_line == 0
1763 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1764 curwin->w_valid &= ~VALID_CROW;
1765 }
1766# endif
1767 showruler(FALSE);
1768 setcursor();
1769 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770}
1771
1772/*
1773 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1774 */
1775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001779 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001782 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001787 did_putchar = TRUE;
1788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1790
1791#ifdef FEAT_CMDL_INFO
1792 add_to_showcmd_c(Ctrl_V);
1793#endif
1794
1795 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001796 if (did_putchar)
1797 /* when the line fits in 'columns' the '^' is at the start of the next
1798 * line and will not removed by the redraw */
1799 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#ifdef FEAT_CMDL_INFO
1801 clear_showcmd();
1802#endif
1803 insert_special(c, FALSE, TRUE);
1804#ifdef FEAT_RIGHTLEFT
1805 revins_chars++;
1806 revins_legal++;
1807#endif
1808}
1809
1810/*
1811 * Put a character directly onto the screen. It's not stored in a buffer.
1812 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1813 */
1814static int pc_status;
1815#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1816#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1817#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1818#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820static int pc_attr;
1821static int pc_row;
1822static int pc_col;
1823
1824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001825edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 int attr;
1828
1829 if (ScreenLines != NULL)
1830 {
1831 update_topline(); /* just in case w_topline isn't valid */
1832 validate_cursor();
1833 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001834 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 else
1836 attr = 0;
1837 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001838 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1840 pc_status = PC_STATUS_UNSET;
1841#endif
1842#ifdef FEAT_RIGHTLEFT
1843 if (curwin->w_p_rl)
1844 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001845 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846# ifdef FEAT_MBYTE
1847 if (has_mbyte)
1848 {
1849 int fix_col = mb_fix_col(pc_col, pc_row);
1850
1851 if (fix_col != pc_col)
1852 {
1853 screen_putchar(' ', pc_row, fix_col, attr);
1854 --curwin->w_wcol;
1855 pc_status = PC_STATUS_RIGHT;
1856 }
1857 }
1858# endif
1859 }
1860 else
1861#endif
1862 {
1863 pc_col += curwin->w_wcol;
1864#ifdef FEAT_MBYTE
1865 if (mb_lefthalve(pc_row, pc_col))
1866 pc_status = PC_STATUS_LEFT;
1867#endif
1868 }
1869
1870 /* save the character to be able to put it back */
1871#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1872 if (pc_status == PC_STATUS_UNSET)
1873#endif
1874 {
1875 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1876 pc_status = PC_STATUS_SET;
1877 }
1878 screen_putchar(c, pc_row, pc_col, attr);
1879 }
1880}
1881
Bram Moolenaarf2732452018-06-03 14:47:35 +02001882#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1883/*
1884 * Return the effective prompt for the current buffer.
1885 */
1886 char_u *
1887prompt_text(void)
1888{
1889 if (curbuf->b_prompt_text == NULL)
1890 return (char_u *)"% ";
1891 return curbuf->b_prompt_text;
1892}
1893
1894/*
1895 * Prepare for prompt mode: Make sure the last line has the prompt text.
1896 * Move the cursor to this line.
1897 */
1898 static void
1899init_prompt(int cmdchar_todo)
1900{
1901 char_u *prompt = prompt_text();
1902 char_u *text;
1903
1904 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1905 text = ml_get_curline();
1906 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1907 {
1908 // prompt is missing, insert it or append a line with it
1909 if (*text == NUL)
1910 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1911 else
1912 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1913 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1914 coladvance((colnr_T)MAXCOL);
1915 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1916 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001917
1918 // Insert always starts after the prompt, allow editing text after it.
1919 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1920 || Insstart_orig.col != (int)STRLEN(prompt))
1921 {
1922 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001923 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001924 Insstart_orig = Insstart;
1925 Insstart_textlen = Insstart.col;
1926 Insstart_blank_vcol = MAXCOL;
1927 arrow_used = FALSE;
1928 }
1929
Bram Moolenaarf2732452018-06-03 14:47:35 +02001930 if (cmdchar_todo == 'A')
1931 coladvance((colnr_T)MAXCOL);
1932 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001933 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001934 /* Make sure the cursor is in a valid position. */
1935 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001936}
1937
1938/*
1939 * Return TRUE if the cursor is in the editable position of the prompt line.
1940 */
1941 int
1942prompt_curpos_editable()
1943{
1944 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1945 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1946}
1947#endif
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949/*
1950 * Undo the previous edit_putchar().
1951 */
1952 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001953edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
1955 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1956 {
1957#if defined(FEAT_MBYTE)
1958 if (pc_status == PC_STATUS_RIGHT)
1959 ++curwin->w_wcol;
1960 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1961 redrawWinline(curwin->w_cursor.lnum, FALSE);
1962 else
1963#endif
1964 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1965 }
1966}
1967
1968/*
1969 * Called when p_dollar is set: display a '$' at the end of the changed text
1970 * Only works when cursor is in the line that changes.
1971 */
1972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001973display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 colnr_T save_col;
1976
1977 if (!redrawing())
1978 return;
1979
1980 cursor_off();
1981 save_col = curwin->w_cursor.col;
1982 curwin->w_cursor.col = col;
1983#ifdef FEAT_MBYTE
1984 if (has_mbyte)
1985 {
1986 char_u *p;
1987
1988 /* If on the last byte of a multi-byte move to the first byte. */
1989 p = ml_get_curline();
1990 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1991 }
1992#endif
1993 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001994 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
1996 edit_putchar('$', FALSE);
1997 dollar_vcol = curwin->w_virtcol;
1998 }
1999 curwin->w_cursor.col = save_col;
2000}
2001
2002/*
2003 * Call this function before moving the cursor from the normal insert position
2004 * in insert mode.
2005 */
2006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002007undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002009 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002011 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 redrawWinline(curwin->w_cursor.lnum, FALSE);
2013 }
2014}
2015
2016/*
2017 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2018 * Keep the cursor on the same character.
2019 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2020 * type == INDENT_DEC decrease indent (for CTRL-D)
2021 * type == INDENT_SET set indent to "amount"
2022 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2023 */
2024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002025change_indent(
2026 int type,
2027 int amount,
2028 int round,
2029 int replaced, /* replaced character, put on replace stack */
2030 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 int vcol;
2033 int last_vcol;
2034 int insstart_less; /* reduction for Insstart.col */
2035 int new_cursor_col;
2036 int i;
2037 char_u *ptr;
2038 int save_p_list;
2039 int start_col;
2040 colnr_T vc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 colnr_T orig_col = 0; /* init for GCC */
2042 char_u *new_line, *orig_line = NULL; /* init for GCC */
2043
2044 /* VREPLACE mode needs to know what the line was like before changing */
2045 if (State & VREPLACE_FLAG)
2046 {
2047 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2048 orig_col = curwin->w_cursor.col;
2049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 /* for the following tricks we don't want list mode */
2052 save_p_list = curwin->w_p_list;
2053 curwin->w_p_list = FALSE;
2054 vc = getvcol_nolist(&curwin->w_cursor);
2055 vcol = vc;
2056
2057 /*
2058 * For Replace mode we need to fix the replace stack later, which is only
2059 * possible when the cursor is in the indent. Remember the number of
2060 * characters before the cursor if it's possible.
2061 */
2062 start_col = curwin->w_cursor.col;
2063
2064 /* determine offset from first non-blank */
2065 new_cursor_col = curwin->w_cursor.col;
2066 beginline(BL_WHITE);
2067 new_cursor_col -= curwin->w_cursor.col;
2068
2069 insstart_less = curwin->w_cursor.col;
2070
2071 /*
2072 * If the cursor is in the indent, compute how many screen columns the
2073 * cursor is to the left of the first non-blank.
2074 */
2075 if (new_cursor_col < 0)
2076 vcol = get_indent() - vcol;
2077
2078 if (new_cursor_col > 0) /* can't fix replace stack */
2079 start_col = -1;
2080
2081 /*
2082 * Set the new indent. The cursor will be put on the first non-blank.
2083 */
2084 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002085 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 else
2087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 int save_State = State;
2089
2090 /* Avoid being called recursively. */
2091 if (State & VREPLACE_FLAG)
2092 State = INSERT;
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002093 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096 insstart_less -= curwin->w_cursor.col;
2097
2098 /*
2099 * Try to put cursor on same character.
2100 * If the cursor is at or after the first non-blank in the line,
2101 * compute the cursor column relative to the column of the first
2102 * non-blank character.
2103 * If we are not in insert mode, leave the cursor on the first non-blank.
2104 * If the cursor is before the first non-blank, position it relative
2105 * to the first non-blank, counted in screen columns.
2106 */
2107 if (new_cursor_col >= 0)
2108 {
2109 /*
2110 * When changing the indent while the cursor is touching it, reset
2111 * Insstart_col to 0.
2112 */
2113 if (new_cursor_col == 0)
2114 insstart_less = MAXCOL;
2115 new_cursor_col += curwin->w_cursor.col;
2116 }
2117 else if (!(State & INSERT))
2118 new_cursor_col = curwin->w_cursor.col;
2119 else
2120 {
2121 /*
2122 * Compute the screen column where the cursor should be.
2123 */
2124 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002125 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
2127 /*
2128 * Advance the cursor until we reach the right screen column.
2129 */
2130 vcol = last_vcol = 0;
2131 new_cursor_col = -1;
2132 ptr = ml_get_curline();
2133 while (vcol <= (int)curwin->w_virtcol)
2134 {
2135 last_vcol = vcol;
2136#ifdef FEAT_MBYTE
2137 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002138 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 else
2140#endif
2141 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002142 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144 vcol = last_vcol;
2145
2146 /*
2147 * May need to insert spaces to be able to position the cursor on
2148 * the right screen column.
2149 */
2150 if (vcol != (int)curwin->w_virtcol)
2151 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002152 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002154 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (ptr != NULL)
2156 {
2157 new_cursor_col += i;
2158 ptr[i] = NUL;
2159 while (--i >= 0)
2160 ptr[i] = ' ';
2161 ins_str(ptr);
2162 vim_free(ptr);
2163 }
2164 }
2165
2166 /*
2167 * When changing the indent while the cursor is in it, reset
2168 * Insstart_col to 0.
2169 */
2170 insstart_less = MAXCOL;
2171 }
2172
2173 curwin->w_p_list = save_p_list;
2174
2175 if (new_cursor_col <= 0)
2176 curwin->w_cursor.col = 0;
2177 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002178 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 curwin->w_set_curswant = TRUE;
2180 changed_cline_bef_curs();
2181
2182 /*
2183 * May have to adjust the start of the insert.
2184 */
2185 if (State & INSERT)
2186 {
2187 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2188 {
2189 if ((int)Insstart.col <= insstart_less)
2190 Insstart.col = 0;
2191 else
2192 Insstart.col -= insstart_less;
2193 }
2194 if ((int)ai_col <= insstart_less)
2195 ai_col = 0;
2196 else
2197 ai_col -= insstart_less;
2198 }
2199
2200 /*
2201 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2202 * If the number of characters before the cursor decreased, need to pop a
2203 * few characters from the replace stack.
2204 * If the number of characters before the cursor increased, need to push a
2205 * few NULs onto the replace stack.
2206 */
2207 if (REPLACE_NORMAL(State) && start_col >= 0)
2208 {
2209 while (start_col > (int)curwin->w_cursor.col)
2210 {
2211 replace_join(0); /* remove a NUL from the replace stack */
2212 --start_col;
2213 }
2214 while (start_col < (int)curwin->w_cursor.col || replaced)
2215 {
2216 replace_push(NUL);
2217 if (replaced)
2218 {
2219 replace_push(replaced);
2220 replaced = NUL;
2221 }
2222 ++start_col;
2223 }
2224 }
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 /*
2227 * For VREPLACE mode, we also have to fix the replace stack. In this case
2228 * it is always possible because we backspace over the whole line and then
2229 * put it back again the way we wanted it.
2230 */
2231 if (State & VREPLACE_FLAG)
2232 {
2233 /* If orig_line didn't allocate, just return. At least we did the job,
2234 * even if you can't backspace. */
2235 if (orig_line == NULL)
2236 return;
2237
2238 /* Save new line */
2239 new_line = vim_strsave(ml_get_curline());
2240 if (new_line == NULL)
2241 return;
2242
2243 /* We only put back the new line up to the cursor */
2244 new_line[curwin->w_cursor.col] = NUL;
2245
2246 /* Put back original line */
2247 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2248 curwin->w_cursor.col = orig_col;
2249
2250 /* Backspace from cursor to start of line */
2251 backspace_until_column(0);
2252
2253 /* Insert new stuff into line again */
2254 ins_bytes(new_line);
2255
2256 vim_free(new_line);
2257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258}
2259
2260/*
2261 * Truncate the space at the end of a line. This is to be used only in an
2262 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2263 * modes.
2264 */
2265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002266truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 int i;
2269
2270 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002271 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 if (State & REPLACE_FLAG)
2274 replace_join(0); /* remove a NUL from the replace stack */
2275 }
2276 line[i + 1] = NUL;
2277}
2278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279/*
2280 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2281 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002282 * Will attempt not to go before "col" even when there is a composing
2283 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 */
2285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002286backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 while ((int)curwin->w_cursor.col > col)
2289 {
2290 curwin->w_cursor.col--;
2291 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002292 replace_do_bs(col);
2293 else if (!del_char_after_col(col))
2294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002298/*
2299 * Like del_char(), but make sure not to go before column "limit_col".
2300 * Only matters when there are composing characters.
2301 * Return TRUE when something was deleted.
2302 */
2303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002304del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002305{
2306#ifdef FEAT_MBYTE
2307 if (enc_utf8 && limit_col >= 0)
2308 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002309 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002310
2311 /* Make sure the cursor is at the start of a character, but
2312 * skip forward again when going too far back because of a
2313 * composing character. */
2314 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002315 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002316 {
2317 int l = utf_ptr2len(ml_get_cursor());
2318
2319 if (l == 0) /* end of line */
2320 break;
2321 curwin->w_cursor.col += l;
2322 }
2323 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2324 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002325 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002326 }
2327 else
2328#endif
2329 (void)del_char(FALSE);
2330 return TRUE;
2331}
2332
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2334/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002335 * CTRL-X pressed in Insert mode.
2336 */
2337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002338ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002339{
2340 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2341 * CTRL-V works like CTRL-N */
2342 if (ctrl_x_mode != CTRL_X_CMDLINE)
2343 {
2344 /* if the next ^X<> won't ADD nothing, then reset
2345 * compl_cont_status */
2346 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002347 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002348 else
2349 compl_cont_status = 0;
2350 /* We're not sure which CTRL-X mode it will be yet */
2351 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2352 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2353 edit_submode_pre = NULL;
2354 showmode();
2355 }
2356}
2357
2358/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002359 * Whether other than default completion has been selected.
2360 */
2361 int
2362ctrl_x_mode_not_default(void)
2363{
2364 return ctrl_x_mode != CTRL_X_NORMAL;
2365}
2366
2367/*
2368 * Whether CTRL-X was typed without a following character.
2369 */
2370 int
2371ctrl_x_mode_not_defined_yet(void)
2372{
2373 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2374}
2375
2376/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002377 * Return TRUE if the 'dict' or 'tsr' option can be used.
2378 */
2379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002380has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002381{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002382 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002383# ifdef FEAT_SPELL
2384 && !curwin->w_p_spell
2385# endif
2386 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002387 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2388 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002389 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002390 edit_submode = NULL;
2391 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2392 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002393 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002394 if (emsg_silent == 0)
2395 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002396 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002397 setcursor();
2398 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002399#ifdef FEAT_EVAL
2400 if (!get_vim_var_nr(VV_TESTING))
2401#endif
2402 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002403 }
2404 return FALSE;
2405 }
2406 return TRUE;
2407}
2408
2409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2411 * This depends on the current mode.
2412 */
2413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002414vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 /* Always allow ^R - let it's results then be checked */
2417 if (c == Ctrl_R)
2418 return TRUE;
2419
Bram Moolenaare3226be2005-12-18 22:10:00 +00002420 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002421 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002422 return TRUE;
2423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 switch (ctrl_x_mode)
2425 {
2426 case 0: /* Not in any CTRL-X mode */
2427 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2428 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002429 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2431 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2432 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002433 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002434 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CTRL_X_SCROLL:
2436 return (c == Ctrl_Y || c == Ctrl_E);
2437 case CTRL_X_WHOLE_LINE:
2438 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2439 case CTRL_X_FILES:
2440 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2441 case CTRL_X_DICTIONARY:
2442 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2443 case CTRL_X_THESAURUS:
2444 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2445 case CTRL_X_TAGS:
2446 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2447#ifdef FEAT_FIND_ID
2448 case CTRL_X_PATH_PATTERNS:
2449 return (c == Ctrl_P || c == Ctrl_N);
2450 case CTRL_X_PATH_DEFINES:
2451 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2452#endif
2453 case CTRL_X_CMDLINE:
2454 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2455 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002456#ifdef FEAT_COMPL_FUNC
2457 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002458 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002459 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002460 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002461#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002462 case CTRL_X_SPELL:
2463 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002464 case CTRL_X_EVAL:
2465 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002467 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 return FALSE;
2469}
2470
2471/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002472 * Return TRUE when character "c" is part of the item currently being
2473 * completed. Used to decide whether to abandon complete mode when the menu
2474 * is visible.
2475 */
2476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002477ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002478{
2479 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2480 /* When expanding an identifier only accept identifier chars. */
2481 return vim_isIDc(c);
2482
2483 switch (ctrl_x_mode)
2484 {
2485 case CTRL_X_FILES:
2486 /* When expanding file name only accept file name chars. But not
2487 * path separators, so that "proto/<Tab>" expands files in
2488 * "proto", not "proto/" as a whole */
2489 return vim_isfilec(c) && !vim_ispathsep(c);
2490
2491 case CTRL_X_CMDLINE:
2492 case CTRL_X_OMNI:
2493 /* Command line and Omni completion can work with just about any
2494 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002495 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002496
2497 case CTRL_X_WHOLE_LINE:
2498 /* For while line completion a space can be part of the line. */
2499 return vim_isprintc(c);
2500 }
2501 return vim_iswordc(c);
2502}
2503
2504/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002505 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002507 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 * the rest of the word to be in -- webb
2509 */
2510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002511ins_compl_add_infercase(
2512 char_u *str,
2513 int len,
2514 int icase,
2515 char_u *fname,
2516 int dir,
2517 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002519 char_u *p;
2520 int i, c;
2521 int actual_len; /* Take multi-byte characters */
2522 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002523 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002524 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 int has_lower = FALSE;
2526 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002528 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002530 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
Bram Moolenaara2993e12007-08-12 14:38:46 +00002532 /* Find actual length of completion. */
2533#ifdef FEAT_MBYTE
2534 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002536 p = str;
2537 actual_len = 0;
2538 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002540 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002541 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002544 else
2545#endif
2546 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
Bram Moolenaara2993e12007-08-12 14:38:46 +00002548 /* Find actual length of original text. */
2549#ifdef FEAT_MBYTE
2550 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002552 p = compl_orig_text;
2553 actual_compl_length = 0;
2554 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002556 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002557 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
2559 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002560 else
2561#endif
2562 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002564 /* "actual_len" may be smaller than "actual_compl_length" when using
2565 * thesaurus, only use the minimum when comparing. */
2566 min_len = actual_len < actual_compl_length
2567 ? actual_len : actual_compl_length;
2568
Bram Moolenaara2993e12007-08-12 14:38:46 +00002569 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002570 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002571 if (wca != NULL)
2572 {
2573 p = str;
2574 for (i = 0; i < actual_len; ++i)
2575#ifdef FEAT_MBYTE
2576 if (has_mbyte)
2577 wca[i] = mb_ptr2char_adv(&p);
2578 else
2579#endif
2580 wca[i] = *(p++);
2581
2582 /* Rule 1: Were any chars converted to lower? */
2583 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002584 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002585 {
2586#ifdef FEAT_MBYTE
2587 if (has_mbyte)
2588 c = mb_ptr2char_adv(&p);
2589 else
2590#endif
2591 c = *(p++);
2592 if (MB_ISLOWER(c))
2593 {
2594 has_lower = TRUE;
2595 if (MB_ISUPPER(wca[i]))
2596 {
2597 /* Rule 1 is satisfied. */
2598 for (i = actual_compl_length; i < actual_len; ++i)
2599 wca[i] = MB_TOLOWER(wca[i]);
2600 break;
2601 }
2602 }
2603 }
2604
2605 /*
2606 * Rule 2: No lower case, 2nd consecutive letter converted to
2607 * upper case.
2608 */
2609 if (!has_lower)
2610 {
2611 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002612 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002613 {
2614#ifdef FEAT_MBYTE
2615 if (has_mbyte)
2616 c = mb_ptr2char_adv(&p);
2617 else
2618#endif
2619 c = *(p++);
2620 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2621 {
2622 /* Rule 2 is satisfied. */
2623 for (i = actual_compl_length; i < actual_len; ++i)
2624 wca[i] = MB_TOUPPER(wca[i]);
2625 break;
2626 }
2627 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2628 }
2629 }
2630
2631 /* Copy the original case of the part we typed. */
2632 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002633 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002634 {
2635#ifdef FEAT_MBYTE
2636 if (has_mbyte)
2637 c = mb_ptr2char_adv(&p);
2638 else
2639#endif
2640 c = *(p++);
2641 if (MB_ISLOWER(c))
2642 wca[i] = MB_TOLOWER(wca[i]);
2643 else if (MB_ISUPPER(c))
2644 wca[i] = MB_TOUPPER(wca[i]);
2645 }
2646
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002647 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002648 * Generate encoding specific output from wide character array.
2649 * Multi-byte characters can occupy up to five bytes more than
2650 * ASCII characters, and we also need one byte for NUL, so stay
2651 * six bytes away from the edge of IObuff.
2652 */
2653 p = IObuff;
2654 i = 0;
2655 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2656#ifdef FEAT_MBYTE
2657 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002658 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002659 else
2660#endif
2661 *(p++) = wca[i++];
2662 *p = NUL;
2663
2664 vim_free(wca);
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002667 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2668 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002670 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671}
2672
2673/*
2674 * Add a match to the list of matches.
2675 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002676 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002677 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002679 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002680ins_compl_add(
2681 char_u *str,
2682 int len,
2683 int icase,
2684 char_u *fname,
2685 char_u **cptext, /* extra text for popup menu or NULL */
2686 int cdir,
2687 int flags,
2688 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002690 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002691 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 ui_breakcheck();
2694 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002695 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (len < 0)
2697 len = (int)STRLEN(str);
2698
2699 /*
2700 * If the same match is already present, don't add it.
2701 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002704 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 do
2706 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002707 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002708 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002709 && match->cp_str[len] == NUL)
2710 return NOTDONE;
2711 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002712 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002715 /* Remove any popup menu before changing the list of matches. */
2716 ins_compl_del_pum();
2717
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 /*
2719 * Allocate a new match structure.
2720 * Copy the values to the new match structure.
2721 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002722 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002724 return FAIL;
2725 match->cp_number = -1;
2726 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002727 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002728 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
2730 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002731 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002733 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002736 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2738 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002739 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002740 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002741 && compl_curr_match->cp_fname != NULL
2742 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002743 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002744 else if (fname != NULL)
2745 {
2746 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002747 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002750 match->cp_fname = NULL;
2751 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002752
2753 if (cptext != NULL)
2754 {
2755 int i;
2756
2757 for (i = 0; i < CPT_COUNT; ++i)
2758 if (cptext[i] != NULL && *cptext[i] != NUL)
2759 match->cp_text[i] = vim_strsave(cptext[i]);
2760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 /*
2763 * Link the new match structure in the list of matches.
2764 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002765 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002766 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 else if (dir == FORWARD)
2768 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002769 match->cp_next = compl_curr_match->cp_next;
2770 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772 else /* BACKWARD */
2773 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002774 match->cp_next = compl_curr_match;
2775 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002777 if (match->cp_next)
2778 match->cp_next->cp_prev = match;
2779 if (match->cp_prev)
2780 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002782 compl_first_match = match;
2783 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002785 /*
2786 * Find the longest common string if still doing that.
2787 */
2788 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2789 ins_compl_longest_match(match);
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 return OK;
2792}
2793
2794/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002795 * Return TRUE if "str[len]" matches with match->cp_str, considering
2796 * match->cp_icase.
2797 */
2798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002799ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002800{
2801 if (match->cp_icase)
2802 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2803 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2804}
2805
2806/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002807 * Reduce the longest common string for match "match".
2808 */
2809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002810ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002811{
2812 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002813 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002814 int had_match;
2815
2816 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002817 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002818 /* First match, use it as a whole. */
2819 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002820 if (compl_leader != NULL)
2821 {
2822 had_match = (curwin->w_cursor.col > compl_col);
2823 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002824 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002825 ins_redraw(FALSE);
2826
2827 /* When the match isn't there (to avoid matching itself) remove it
2828 * again after redrawing. */
2829 if (!had_match)
2830 ins_compl_delete();
2831 compl_used_match = FALSE;
2832 }
2833 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002834 else
2835 {
2836 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002837 p = compl_leader;
2838 s = match->cp_str;
2839 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002840 {
2841#ifdef FEAT_MBYTE
2842 if (has_mbyte)
2843 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002844 c1 = mb_ptr2char(p);
2845 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002846 }
2847 else
2848#endif
2849 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002850 c1 = *p;
2851 c2 = *s;
2852 }
2853 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2854 : (c1 != c2))
2855 break;
2856#ifdef FEAT_MBYTE
2857 if (has_mbyte)
2858 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002859 MB_PTR_ADV(p);
2860 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002861 }
2862 else
2863#endif
2864 {
2865 ++p;
2866 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002867 }
2868 }
2869
2870 if (*p != NUL)
2871 {
2872 /* Leader was shortened, need to change the inserted text. */
2873 *p = NUL;
2874 had_match = (curwin->w_cursor.col > compl_col);
2875 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002876 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002877 ins_redraw(FALSE);
2878
2879 /* When the match isn't there (to avoid matching itself) remove it
2880 * again after redrawing. */
2881 if (!had_match)
2882 ins_compl_delete();
2883 }
2884
2885 compl_used_match = FALSE;
2886 }
2887}
2888
2889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 * Add an array of matches to the list of matches.
2891 * Frees matches[].
2892 */
2893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002894ins_compl_add_matches(
2895 int num_matches,
2896 char_u **matches,
2897 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
2899 int i;
2900 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002901 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
Bram Moolenaar572cb562005-08-05 21:35:02 +00002903 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002904 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002905 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002907 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 FreeWild(num_matches, matches);
2909}
2910
2911/* Make the completion list cyclic.
2912 * Return the number of matches (excluding the original).
2913 */
2914 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002915ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002917 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 int count = 0;
2919
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002920 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 /*
2923 * Find the end of the list.
2924 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002925 match = compl_first_match;
2926 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002927 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002929 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 ++count;
2931 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002932 match->cp_next = compl_first_match;
2933 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 }
2935 return count;
2936}
2937
Bram Moolenaara94bc432006-03-10 21:42:59 +00002938/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002939 * Set variables that store noselect and noinsert behavior from the
2940 * 'completeopt' value.
2941 */
2942 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002943completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002944{
2945 compl_no_insert = FALSE;
2946 compl_no_select = FALSE;
2947 if (strstr((char *)p_cot, "noselect") != NULL)
2948 compl_no_select = TRUE;
2949 if (strstr((char *)p_cot, "noinsert") != NULL)
2950 compl_no_insert = TRUE;
2951}
2952
2953/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002954 * Start completion for the complete() function.
2955 * "startcol" is where the matched text starts (1 is first column).
2956 * "list" is the list of matches.
2957 */
2958 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002959set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002960{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002961 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002962 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002963
Bram Moolenaara94bc432006-03-10 21:42:59 +00002964 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002965 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002966 ins_compl_prep(' ');
2967 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002968 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002969
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002970 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002971 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002972 startcol = curwin->w_cursor.col;
2973 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002974 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002975 /* compl_pattern doesn't need to be set */
2976 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2977 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002978 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002979 return;
2980
Bram Moolenaare4214502015-03-05 18:08:43 +01002981 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002982
2983 ins_compl_add_list(list);
2984 compl_matches = ins_compl_make_cyclic();
2985 compl_started = TRUE;
2986 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002987 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002988
2989 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002990 if (compl_no_insert || compl_no_select)
2991 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002992 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002993 if (compl_no_select)
2994 /* Down/Up has no real effect. */
2995 ins_complete(K_UP, FALSE);
2996 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002997 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002998 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002999 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003000
3001 /* Lazily show the popup menu, unless we got interrupted. */
3002 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01003003 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003004 out_flush();
3005}
3006
3007
Bram Moolenaar9372a112005-12-06 19:59:18 +00003008/* "compl_match_array" points the currently displayed list of entries in the
3009 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003010static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003011static int compl_match_arraysize;
3012
3013/*
3014 * Update the screen and when there is any scrolling remove the popup menu.
3015 */
3016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003017ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003018{
3019 int h;
3020
3021 if (compl_match_array != NULL)
3022 {
3023 h = curwin->w_cline_height;
3024 update_screen(0);
3025 if (h != curwin->w_cline_height)
3026 ins_compl_del_pum();
3027 }
3028}
3029
3030/*
3031 * Remove any popup menu.
3032 */
3033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003034ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003035{
3036 if (compl_match_array != NULL)
3037 {
3038 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003039 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003040 }
3041}
3042
3043/*
3044 * Return TRUE if the popup menu should be displayed.
3045 */
3046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003047pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003048{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003049 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003050 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003051 return FALSE;
3052
3053 /* The display looks bad on a B&W display. */
3054 if (t_colors < 8
3055#ifdef FEAT_GUI
3056 && !gui.in_use
3057#endif
3058 )
3059 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003060 return TRUE;
3061}
3062
3063/*
3064 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003065 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003066 */
3067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003068pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003069{
3070 compl_T *compl;
3071 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003072
3073 /* Don't display the popup menu if there are no matches or there is only
3074 * one (ignoring the original text). */
3075 compl = compl_first_match;
3076 i = 0;
3077 do
3078 {
3079 if (compl == NULL
3080 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3081 break;
3082 compl = compl->cp_next;
3083 } while (compl != compl_first_match);
3084
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003085 if (strstr((char *)p_cot, "menuone") != NULL)
3086 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003087 return (i >= 2);
3088}
3089
3090/*
3091 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003092 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003093 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003095ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003096{
3097 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003098 compl_T *shown_compl = NULL;
3099 int did_find_shown_match = FALSE;
3100 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003101 int i;
3102 int cur = -1;
3103 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003104 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003105
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003106 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003107 return;
3108
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003109#if defined(FEAT_EVAL)
3110 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3111 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3112#endif
3113
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003114 /* Update the screen before drawing the popup menu over it. */
3115 update_screen(0);
3116
3117 if (compl_match_array == NULL)
3118 {
3119 /* Need to build the popup menu list. */
3120 compl_match_arraysize = 0;
3121 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003122 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003123 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003124 do
3125 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003126 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3127 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003128 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003129 ++compl_match_arraysize;
3130 compl = compl->cp_next;
3131 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003132 if (compl_match_arraysize == 0)
3133 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003134 compl_match_array = (pumitem_T *)alloc_clear(
3135 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003136 * compl_match_arraysize));
3137 if (compl_match_array != NULL)
3138 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003139 /* If the current match is the original text don't find the first
3140 * match after it, don't highlight anything. */
3141 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3142 shown_match_ok = TRUE;
3143
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003144 i = 0;
3145 compl = compl_first_match;
3146 do
3147 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003148 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3149 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003150 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003151 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003152 if (!shown_match_ok)
3153 {
3154 if (compl == compl_shown_match || did_find_shown_match)
3155 {
3156 /* This item is the shown match or this is the
3157 * first displayed item after the shown match. */
3158 compl_shown_match = compl;
3159 did_find_shown_match = TRUE;
3160 shown_match_ok = TRUE;
3161 }
3162 else
3163 /* Remember this displayed match for when the
3164 * shown match is just below it. */
3165 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003166 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003167 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003168
3169 if (compl->cp_text[CPT_ABBR] != NULL)
3170 compl_match_array[i].pum_text =
3171 compl->cp_text[CPT_ABBR];
3172 else
3173 compl_match_array[i].pum_text = compl->cp_str;
3174 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3175 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3176 if (compl->cp_text[CPT_MENU] != NULL)
3177 compl_match_array[i++].pum_extra =
3178 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003179 else
3180 compl_match_array[i++].pum_extra = compl->cp_fname;
3181 }
3182
3183 if (compl == compl_shown_match)
3184 {
3185 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003186
3187 /* When the original text is the shown match don't set
3188 * compl_shown_match. */
3189 if (compl->cp_flags & ORIGINAL_TEXT)
3190 shown_match_ok = TRUE;
3191
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003192 if (!shown_match_ok && shown_compl != NULL)
3193 {
3194 /* The shown match isn't displayed, set it to the
3195 * previously displayed match. */
3196 compl_shown_match = shown_compl;
3197 shown_match_ok = TRUE;
3198 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003199 }
3200 compl = compl->cp_next;
3201 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003202
3203 if (!shown_match_ok) /* no displayed match at all */
3204 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003205 }
3206 }
3207 else
3208 {
3209 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003210 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003211 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3212 || compl_match_array[i].pum_text
3213 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003214 {
3215 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003216 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003217 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003218 }
3219
3220 if (compl_match_array != NULL)
3221 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003222 /* In Replace mode when a $ is displayed at the end of the line only
3223 * part of the screen would be updated. We do need to redraw here. */
3224 dollar_vcol = -1;
3225
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003226 /* Compute the screen column of the start of the completed text.
3227 * Use the cursor to get all wrapping and other settings right. */
3228 col = curwin->w_cursor.col;
3229 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003230 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003231 curwin->w_cursor.col = col;
3232 }
3233}
3234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235#define DICT_FIRST (1) /* use just first element in "dict" */
3236#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003239 * Add any identifiers that match the given pattern in the list of dictionary
3240 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 */
3242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243ins_compl_dictionaries(
3244 char_u *dict_start,
3245 char_u *pat,
3246 int flags, /* DICT_FIRST and/or DICT_EXACT */
3247 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003249 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 char_u *ptr;
3251 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 char_u **files;
3254 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003256 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
Bram Moolenaar0b238792006-03-02 22:49:12 +00003258 if (*dict == NUL)
3259 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003260#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003261 /* When 'dictionary' is empty and spell checking is enabled use
3262 * "spell". */
3263 if (!thesaurus && curwin->w_p_spell)
3264 dict = (char_u *)"spell";
3265 else
3266#endif
3267 return;
3268 }
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003271 if (buf == NULL)
3272 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003273 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 /* If 'infercase' is set, don't use 'smartcase' here */
3276 save_p_scs = p_scs;
3277 if (curbuf->b_p_inf)
3278 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003279
3280 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3281 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003282 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003283 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003284 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003285 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003286 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003287
3288 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003289 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003290 len = STRLEN(pat_esc) + 10;
3291 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003292 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003293 {
3294 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003295 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003296 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003297 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003298 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3299 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003300 vim_free(ptr);
3301 }
3302 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003303 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003304 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003305 if (regmatch.regprog == NULL)
3306 goto theend;
3307 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3310 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003311 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 /* copy one dictionary file name into buf */
3314 if (flags == DICT_EXACT)
3315 {
3316 count = 1;
3317 files = &dict;
3318 }
3319 else
3320 {
3321 /* Expand wildcards in the dictionary name, but do not allow
3322 * backticks (for security, the 'dict' option may have been set in
3323 * a modeline). */
3324 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003325# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003326 if (!thesaurus && STRCMP(buf, "spell") == 0)
3327 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003328 else
3329# endif
3330 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 || expand_wildcards(1, &buf, &count, &files,
3332 EW_FILE|EW_SILENT) != OK)
3333 count = 0;
3334 }
3335
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003336# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003337 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003339 /* Complete from active spelling. Skip "\<" in the pattern, we
3340 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003341 if (pat[0] == '\\' && pat[1] == '<')
3342 ptr = pat + 2;
3343 else
3344 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003345 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003347 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003348# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003349 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003350 {
3351 ins_compl_files(count, files, thesaurus, flags,
3352 &regmatch, buf, &dir);
3353 if (flags != DICT_EXACT)
3354 FreeWild(count, files);
3355 }
3356 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 break;
3358 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003359
3360theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003362 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 vim_free(buf);
3364}
3365
Bram Moolenaar0b238792006-03-02 22:49:12 +00003366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003367ins_compl_files(
3368 int count,
3369 char_u **files,
3370 int thesaurus,
3371 int flags,
3372 regmatch_T *regmatch,
3373 char_u *buf,
3374 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003375{
3376 char_u *ptr;
3377 int i;
3378 FILE *fp;
3379 int add_r;
3380
3381 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3382 {
3383 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3384 if (flags != DICT_EXACT)
3385 {
3386 vim_snprintf((char *)IObuff, IOSIZE,
3387 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003388 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003389 }
3390
3391 if (fp != NULL)
3392 {
3393 /*
3394 * Read dictionary file line by line.
3395 * Check each line for a match.
3396 */
3397 while (!got_int && !compl_interrupted
3398 && !vim_fgets(buf, LSIZE, fp))
3399 {
3400 ptr = buf;
3401 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3402 {
3403 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003404 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003405 ptr = find_line_end(ptr);
3406 else
3407 ptr = find_word_end(ptr);
3408 add_r = ins_compl_add_infercase(regmatch->startp[0],
3409 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003410 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003411 if (thesaurus)
3412 {
3413 char_u *wstart;
3414
3415 /*
3416 * Add the other matches on the line
3417 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003418 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003419 while (!got_int)
3420 {
3421 /* Find start of the next word. Skip white
3422 * space and punctuation. */
3423 ptr = find_word_start(ptr);
3424 if (*ptr == NUL || *ptr == NL)
3425 break;
3426 wstart = ptr;
3427
Bram Moolenaara2993e12007-08-12 14:38:46 +00003428 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003429#ifdef FEAT_MBYTE
3430 if (has_mbyte)
3431 /* Japanese words may have characters in
3432 * different classes, only separate words
3433 * with single-byte non-word characters. */
3434 while (*ptr != NUL)
3435 {
3436 int l = (*mb_ptr2len)(ptr);
3437
3438 if (l < 2 && !vim_iswordc(*ptr))
3439 break;
3440 ptr += l;
3441 }
3442 else
3443#endif
3444 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003445
3446 /* Add the word. Skip the regexp match. */
3447 if (wstart != regmatch->startp[0])
3448 add_r = ins_compl_add_infercase(wstart,
3449 (int)(ptr - wstart),
3450 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003451 }
3452 }
3453 if (add_r == OK)
3454 /* if dir was BACKWARD then honor it just once */
3455 *dir = FORWARD;
3456 else if (add_r == FAIL)
3457 break;
3458 /* avoid expensive call to vim_regexec() when at end
3459 * of line */
3460 if (*ptr == '\n' || got_int)
3461 break;
3462 }
3463 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003464 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003465 }
3466 fclose(fp);
3467 }
3468 }
3469}
3470
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471/*
3472 * Find the start of the next word.
3473 * Returns a pointer to the first char of the word. Also stops at a NUL.
3474 */
3475 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003476find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478#ifdef FEAT_MBYTE
3479 if (has_mbyte)
3480 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003481 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else
3483#endif
3484 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3485 ++ptr;
3486 return ptr;
3487}
3488
3489/*
3490 * Find the end of the word. Assumes it starts inside a word.
3491 * Returns a pointer to just after the word.
3492 */
3493 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496#ifdef FEAT_MBYTE
3497 int start_class;
3498
3499 if (has_mbyte)
3500 {
3501 start_class = mb_get_class(ptr);
3502 if (start_class > 1)
3503 while (*ptr != NUL)
3504 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003505 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (mb_get_class(ptr) != start_class)
3507 break;
3508 }
3509 }
3510 else
3511#endif
3512 while (vim_iswordc(*ptr))
3513 ++ptr;
3514 return ptr;
3515}
3516
3517/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003518 * Find the end of the line, omitting CR and NL at the end.
3519 * Returns a pointer to just after the line.
3520 */
3521 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003522find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003523{
3524 char_u *s;
3525
3526 s = ptr + STRLEN(ptr);
3527 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3528 --s;
3529 return s;
3530}
3531
3532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 * Free the list of completions
3534 */
3535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003536ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003538 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003539 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaard23a8232018-02-10 18:45:26 +01003541 VIM_CLEAR(compl_pattern);
3542 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003544 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003546
3547 ins_compl_del_pum();
3548 pum_clear();
3549
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 do
3552 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003554 compl_curr_match = compl_curr_match->cp_next;
3555 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003557 if (match->cp_flags & FREE_FNAME)
3558 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003559 for (i = 0; i < CPT_COUNT; ++i)
3560 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3563 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003564 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003565 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003569ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003571 compl_cont_status = 0;
3572 compl_started = FALSE;
3573 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003574 VIM_CLEAR(compl_pattern);
3575 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003577 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003578 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003579 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003580 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
3582
3583/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003584 * Return TRUE when Insert completion is active.
3585 */
3586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003588{
3589 return compl_started;
3590}
3591
3592/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003593 * Delete one character before the cursor and show the subset of the matches
3594 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003595 * Returns the character to be used, NUL if the work is done and another char
3596 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003597 */
3598 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003599ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003600{
3601 char_u *line;
3602 char_u *p;
3603
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003604 line = ml_get_curline();
3605 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003606 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003607
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003608 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003609 * allow the word to be deleted, we won't match everything.
3610 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003611 if ((int)(p - line) - (int)compl_col < 0
3612 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003613 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3614 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3615 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003616 return K_BS;
3617
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003618 /* Deleted more than what was used to find matches or didn't finish
3619 * finding all matches: need to look for matches all over again. */
3620 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003621 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003622 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003623
Bram Moolenaara6557602006-02-04 22:43:20 +00003624 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003625 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003626 if (compl_leader != NULL)
3627 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003628 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003629 if (compl_shown_match != NULL)
3630 /* Make sure current match is not a hidden item. */
3631 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003632 return NUL;
3633 }
3634 return K_BS;
3635}
Bram Moolenaara6557602006-02-04 22:43:20 +00003636
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003637/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003638 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3639 * be called.
3640 */
3641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003643{
3644 /* Return TRUE if we didn't complete finding matches or when the
3645 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3646 return compl_was_interrupted
3647 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3648 && compl_opt_refresh_always);
3649}
3650
3651/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003652 * Called after changing "compl_leader".
3653 * Show the popup menu with a different set of matches.
3654 * May also search for matches again if the previous search was interrupted.
3655 */
3656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003658{
3659 ins_compl_del_pum();
3660 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003661 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003662 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003663
3664 if (compl_started)
3665 ins_compl_set_original_text(compl_leader);
3666 else
3667 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003668#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003669 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003670#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003671 /*
3672 * Matches were cleared, need to search for them now. First display
3673 * the changed text before the cursor. Set "compl_restarting" to
3674 * avoid that the first match is inserted.
3675 */
3676 update_screen(0);
3677#ifdef FEAT_GUI
3678 if (gui.in_use)
3679 {
3680 /* Show the cursor after the match, not after the redrawn text. */
3681 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003682 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003683 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003684#endif
3685 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003686 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003687 compl_cont_status = 0;
3688 compl_restarting = FALSE;
3689 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003690
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003691 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003692
3693 /* Show the popup menu with a different set of matches. */
3694 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003695
3696 /* Don't let Enter select the original text when there is no popup menu. */
3697 if (compl_match_array == NULL)
3698 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003699}
3700
3701/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003702 * Return the length of the completion, from the completion start column to
3703 * the cursor column. Making sure it never goes below zero.
3704 */
3705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003706ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003707{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003708 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003709
3710 if (off < 0)
3711 return 0;
3712 return off;
3713}
3714
3715/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003716 * Append one character to the match leader. May reduce the number of
3717 * matches.
3718 */
3719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003721{
3722#ifdef FEAT_MBYTE
3723 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003724#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003725
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003726 if (stop_arrow() == FAIL)
3727 return;
3728#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003729 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3730 {
3731 char_u buf[MB_MAXBYTES + 1];
3732
3733 (*mb_char2bytes)(c, buf);
3734 buf[cc] = NUL;
3735 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003736 if (compl_opt_refresh_always)
3737 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003738 }
3739 else
3740#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003741 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003742 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003743 if (compl_opt_refresh_always)
3744 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003745 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003746
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003747 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003748 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003749 ins_compl_restart();
3750
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003751 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003752 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003753 * break redo. */
3754 if (!compl_opt_refresh_always)
3755 {
3756 vim_free(compl_leader);
3757 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003758 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003759 if (compl_leader != NULL)
3760 ins_compl_new_leader();
3761 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003762}
3763
3764/*
3765 * Setup for finding completions again without leaving CTRL-X mode. Used when
3766 * BS or a key was typed while still searching for matches.
3767 */
3768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003769ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003770{
3771 ins_compl_free();
3772 compl_started = FALSE;
3773 compl_matches = 0;
3774 compl_cont_status = 0;
3775 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003776}
3777
3778/*
3779 * Set the first match, the original text.
3780 */
3781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003783{
3784 char_u *p;
3785
Bram Moolenaare87edf32018-04-17 22:14:32 +02003786 /* Replace the original text entry.
3787 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3788 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003789 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3790 {
3791 p = vim_strsave(str);
3792 if (p != NULL)
3793 {
3794 vim_free(compl_first_match->cp_str);
3795 compl_first_match->cp_str = p;
3796 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003797 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003798 else if (compl_first_match->cp_prev != NULL
3799 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3800 {
3801 p = vim_strsave(str);
3802 if (p != NULL)
3803 {
3804 vim_free(compl_first_match->cp_prev->cp_str);
3805 compl_first_match->cp_prev->cp_str = p;
3806 }
3807 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003808}
3809
3810/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003811 * Append one character to the match leader. May reduce the number of
3812 * matches.
3813 */
3814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003815ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003816{
3817 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003818 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003819 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003820 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003821
3822 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003823 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003824 {
3825 /* When still at the original match use the first entry that matches
3826 * the leader. */
3827 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3828 {
3829 p = NULL;
3830 for (cp = compl_shown_match->cp_next; cp != NULL
3831 && cp != compl_first_match; cp = cp->cp_next)
3832 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003833 if (compl_leader == NULL
3834 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003835 (int)STRLEN(compl_leader)))
3836 {
3837 p = cp->cp_str;
3838 break;
3839 }
3840 }
3841 if (p == NULL || (int)STRLEN(p) <= len)
3842 return;
3843 }
3844 else
3845 return;
3846 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003847 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003848 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003849 ins_compl_addleader(c);
3850}
3851
3852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003854 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003855 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003858ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003862 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 /* Forget any previous 'special' messages if this is actually
3865 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3866 */
3867 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3868 edit_submode_extra = NULL;
3869
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003870 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003871 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3872 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003873 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003875 /* Set "compl_get_longest" when finding the first matches. */
3876 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003877 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003878 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003879 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003880 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003881
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003882 }
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3885 {
3886 /*
3887 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3888 * it will be yet. Now we decide.
3889 */
3890 switch (c)
3891 {
3892 case Ctrl_E:
3893 case Ctrl_Y:
3894 ctrl_x_mode = CTRL_X_SCROLL;
3895 if (!(State & REPLACE_FLAG))
3896 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3897 else
3898 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3899 edit_submode_pre = NULL;
3900 showmode();
3901 break;
3902 case Ctrl_L:
3903 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3904 break;
3905 case Ctrl_F:
3906 ctrl_x_mode = CTRL_X_FILES;
3907 break;
3908 case Ctrl_K:
3909 ctrl_x_mode = CTRL_X_DICTIONARY;
3910 break;
3911 case Ctrl_R:
3912 /* Simply allow ^R to happen without affecting ^X mode */
3913 break;
3914 case Ctrl_T:
3915 ctrl_x_mode = CTRL_X_THESAURUS;
3916 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003917#ifdef FEAT_COMPL_FUNC
3918 case Ctrl_U:
3919 ctrl_x_mode = CTRL_X_FUNCTION;
3920 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003922 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003923 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003924#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003925 case 's':
3926 case Ctrl_S:
3927 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003928#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003929 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003930 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003932#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 case Ctrl_RSB:
3935 ctrl_x_mode = CTRL_X_TAGS;
3936 break;
3937#ifdef FEAT_FIND_ID
3938 case Ctrl_I:
3939 case K_S_TAB:
3940 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3941 break;
3942 case Ctrl_D:
3943 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3944 break;
3945#endif
3946 case Ctrl_V:
3947 case Ctrl_Q:
3948 ctrl_x_mode = CTRL_X_CMDLINE;
3949 break;
3950 case Ctrl_P:
3951 case Ctrl_N:
3952 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3953 * just started ^X mode, or there were enough ^X's to cancel
3954 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3955 * do normal expansion when interrupting a different mode (say
3956 * ^X^F^X^P or ^P^X^X^P, see below)
3957 * nothing changes if interrupting mode 0, (eg, the flag
3958 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003959 if (!(compl_cont_status & CONT_INTRPT))
3960 compl_cont_status |= CONT_LOCAL;
3961 else if (compl_cont_mode != 0)
3962 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 /* FALLTHROUGH */
3964 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003965 /* If we have typed at least 2 ^X's... for modes != 0, we set
3966 * compl_cont_status = 0 (eg, as if we had just started ^X
3967 * mode).
3968 * For mode 0, we set "compl_cont_mode" to an impossible
3969 * value, in both cases ^X^X can be used to restart the same
3970 * mode (avoiding ADDING mode).
3971 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3972 * 'complete' and local ^P expansions respectively.
3973 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3974 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 if (c == Ctrl_X)
3976 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003977 if (compl_cont_mode != 0)
3978 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003980 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003982 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 edit_submode = NULL;
3984 showmode();
3985 break;
3986 }
3987 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003988 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
3990 /* We're already in CTRL-X mode, do we stay in it? */
3991 if (!vim_is_ctrl_x_key(c))
3992 {
3993 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003994 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
3996 ctrl_x_mode = CTRL_X_FINISHED;
3997 edit_submode = NULL;
3998 }
3999 showmode();
4000 }
4001
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004002 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
4004 /* Show error message from attempted keyword completion (probably
4005 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004006 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004008 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4009 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 || ctrl_x_mode == CTRL_X_FINISHED)
4011 {
4012 /* Get here when we have finished typing a sequence of ^N and
4013 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004014 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004015 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
4017 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004018 * If any of the original typed text has been changed, eg when
4019 * ignorecase is set, we must add back-spaces to the redo
4020 * buffer. We add as few as necessary to delete just the part
4021 * of the original text that has changed.
4022 * When using the longest match, edited the match or used
4023 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004025 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4026 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004027 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004028 ptr = NULL;
4029 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031
4032#ifdef FEAT_CINDENT
4033 want_cindent = (can_cindent && cindent_on());
4034#endif
4035 /*
4036 * When completing whole lines: fix indent for 'cindent'.
4037 * Otherwise, break line if it's too long.
4038 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
4041#ifdef FEAT_CINDENT
4042 /* re-indent the current line */
4043 if (want_cindent)
4044 {
4045 do_c_expr_indent();
4046 want_cindent = FALSE; /* don't do it again */
4047 }
4048#endif
4049 }
4050 else
4051 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004052 int prev_col = curwin->w_cursor.col;
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004055 if (prev_col > 0)
4056 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004057 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004058 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004060 if (prev_col > 0
4061 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4062 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004065 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004066 * the selection without inserting anything. When
4067 * compl_enter_selects is set the Enter key does the same. */
4068 if ((c == Ctrl_Y || (compl_enter_selects
4069 && (c == CAR || c == K_KENTER || c == NL)))
4070 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004071 retval = TRUE;
4072
Bram Moolenaar47247282016-08-02 22:36:02 +02004073 /* CTRL-E means completion is Ended, go back to the typed text.
4074 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004075 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004076 {
4077 ins_compl_delete();
4078 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004079 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004080 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004081 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004082 retval = TRUE;
4083 }
4084
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004085 auto_format(FALSE, TRUE);
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004088 compl_started = FALSE;
4089 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004090 if (!shortmess(SHM_COMPLETIONMENU))
4091 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004092 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004093 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (edit_submode != NULL)
4095 {
4096 edit_submode = NULL;
4097 showmode();
4098 }
4099
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004100#ifdef FEAT_CMDWIN
4101 if (c == Ctrl_C && cmdwin_type != 0)
4102 /* Avoid the popup menu remains displayed when leaving the
4103 * command line window. */
4104 update_screen(0);
4105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#ifdef FEAT_CINDENT
4107 /*
4108 * Indent now if a key was typed that is in 'cinkeys'.
4109 */
4110 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4111 do_c_expr_indent();
4112#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004113 /* Trigger the CompleteDone event to give scripts a chance to act
4114 * upon the completion. */
4115 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004118 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4119 /* Trigger the CompleteDone event to give scripts a chance to act
4120 * upon the (possibly failed) completion. */
4121 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 /* reset continue_* if we left expansion-mode, if we stay they'll be
4124 * (re)set properly in ins_complete() */
4125 if (!vim_is_ctrl_x_key(c))
4126 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 compl_cont_status = 0;
4128 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004130
4131 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132}
4133
4134/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004135 * Fix the redo buffer for the completion leader replacing some of the typed
4136 * text. This inserts backspaces and appends the changed text.
4137 * "ptr" is the known leader text or NUL.
4138 */
4139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004140ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004141{
4142 int len;
4143 char_u *p;
4144 char_u *ptr = ptr_arg;
4145
4146 if (ptr == NULL)
4147 {
4148 if (compl_leader != NULL)
4149 ptr = compl_leader;
4150 else
4151 return; /* nothing to do */
4152 }
4153 if (compl_orig_text != NULL)
4154 {
4155 p = compl_orig_text;
4156 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4157 ;
4158#ifdef FEAT_MBYTE
4159 if (len > 0)
4160 len -= (*mb_head_off)(p, p + len);
4161#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004162 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004163 AppendCharToRedobuff(K_BS);
4164 }
4165 else
4166 len = 0;
4167 if (ptr != NULL)
4168 AppendToRedobuffLit(ptr + len, -1);
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4173 * (depending on flag) starting from buf and looking for a non-scanned
4174 * buffer (other than curbuf). curbuf is special, if it is called with
4175 * buf=curbuf then it has to be the first call for a given flag/expansion.
4176 *
4177 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4178 */
4179 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004180ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 if (flag == 'w') /* just windows */
4185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 if (buf == curbuf) /* first call for this flag/expansion */
4187 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004188 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 && wp->w_buffer->b_scanned)
4190 ;
4191 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 else
4194 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4195 * (unlisted buffers)
4196 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004197 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 && ((flag == 'U'
4199 ? buf->b_p_bl
4200 : (!buf->b_p_bl
4201 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004202 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 ;
4204 return buf;
4205}
4206
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004207#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004208/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004209 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004210 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004211 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004213expand_by_function(
4214 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4215 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004216{
Bram Moolenaar82139082011-09-14 16:52:09 +02004217 list_T *matchlist = NULL;
4218 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004219 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004220 char_u *funcname;
4221 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004222 win_T *curwin_save;
4223 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004224 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004225
Bram Moolenaare344bea2005-09-01 20:46:49 +00004226 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4227 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004228 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004229
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004230 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004231 args[0].v_type = VAR_NUMBER;
4232 args[0].vval.v_number = 0;
4233 args[1].v_type = VAR_STRING;
4234 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4235 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004236
Bram Moolenaare344bea2005-09-01 20:46:49 +00004237 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004238 curwin_save = curwin;
4239 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004240
4241 /* Call a function, which returns a list or dict. */
Bram Moolenaarded27a12018-08-01 19:06:03 +02004242 if (call_vim_function(funcname, 2, args, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004243 {
4244 switch (rettv.v_type)
4245 {
4246 case VAR_LIST:
4247 matchlist = rettv.vval.v_list;
4248 break;
4249 case VAR_DICT:
4250 matchdict = rettv.vval.v_dict;
4251 break;
4252 default:
4253 /* TODO: Give error message? */
4254 clear_tv(&rettv);
4255 break;
4256 }
4257 }
4258
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004259 if (curwin_save != curwin || curbuf_save != curbuf)
4260 {
4261 EMSG(_(e_complwin));
4262 goto theend;
4263 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004264 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004265 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004266 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004267 {
4268 EMSG(_(e_compldel));
4269 goto theend;
4270 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004271
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004272 if (matchlist != NULL)
4273 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004274 else if (matchdict != NULL)
4275 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004276
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004277theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004278 if (matchdict != NULL)
4279 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004280 if (matchlist != NULL)
4281 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004282}
4283#endif /* FEAT_COMPL_FUNC */
4284
Bram Moolenaar39f05632006-03-19 22:15:26 +00004285#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004286/*
4287 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004288 */
4289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004290ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004291{
4292 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004293 int dir = compl_direction;
4294
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004295 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004296 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004297 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004298 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4299 /* if dir was BACKWARD then honor it just once */
4300 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004301 else if (did_emsg)
4302 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004303 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004304}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004305
4306/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004307 * Add completions from a dict.
4308 */
4309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004310ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004311{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004312 dictitem_T *di_refresh;
4313 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004314
4315 /* Check for optional "refresh" item. */
4316 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004317 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4318 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004319 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004320 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004321
4322 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4323 compl_opt_refresh_always = TRUE;
4324 }
4325
4326 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004327 di_words = dict_find(dict, (char_u *)"words", 5);
4328 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4329 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004330}
4331
4332/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004333 * Add a match to the list of matches from a typeval_T.
4334 * If the given string is already in the list of completions, then return
4335 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4336 * maybe because alloc() returns NULL, then FAIL is returned.
4337 */
4338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004339ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004340{
4341 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004342 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004343 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004344 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004345 char_u *(cptext[CPT_COUNT]);
4346
4347 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4348 {
4349 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4350 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4351 (char_u *)"abbr", FALSE);
4352 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4353 (char_u *)"menu", FALSE);
4354 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4355 (char_u *)"kind", FALSE);
4356 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4357 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004358 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4359 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004360 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4361 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004362 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004363 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004364 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4365 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004366 }
4367 else
4368 {
4369 word = get_tv_string_chk(tv);
4370 vim_memset(cptext, 0, sizeof(cptext));
4371 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004372 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004373 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004374 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004375}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004376#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004377
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004378/*
4379 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004380 * The search starts at position "ini" in curbuf and in the direction
4381 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004382 * When "compl_started" is FALSE start at that position, otherwise continue
4383 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 * This may return before finding all the matches.
4385 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 */
4387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004388ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 static pos_T first_match_pos;
4391 static pos_T last_match_pos;
4392 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393 static int found_all = FALSE; /* Found all matches of a
4394 certain type. */
4395 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
Bram Moolenaar572cb562005-08-05 21:35:02 +00004397 pos_T *pos;
4398 char_u **matches;
4399 int save_p_scs;
4400 int save_p_ws;
4401 int save_p_ic;
4402 int i;
4403 int num_matches;
4404 int len;
4405 int found_new_match;
4406 int type = ctrl_x_mode;
4407 char_u *ptr;
4408 char_u *dict = NULL;
4409 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004410 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004414 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 ins_buf->b_scanned = 0;
4416 found_all = FALSE;
4417 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004419 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 last_match_pos = first_match_pos = *ini;
4421 }
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004422 else if (ins_buf != curbuf && !buf_valid(ins_buf))
4423 ins_buf = curbuf; // In case the buffer was wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
Bram Moolenaar4475b622017-05-01 20:46:52 +02004425 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004426 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar02ab9772018-08-07 14:55:09 +02004427
4428 /*
4429 * For ^N/^P loop over all the flags/windows/buffers in 'complete'.
4430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 for (;;)
4432 {
4433 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004434 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004436 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 * or if found_all says this entry is done. For ^X^L only use the
4438 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004439 if ((ctrl_x_mode == CTRL_X_NORMAL
4440 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004441 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
4443 found_all = FALSE;
4444 while (*e_cpt == ',' || *e_cpt == ' ')
4445 e_cpt++;
4446 if (*e_cpt == '.' && !curbuf->b_scanned)
4447 {
4448 ins_buf = curbuf;
4449 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004450 /* Move the cursor back one character so that ^N can match the
4451 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004452 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004453 {
4454 /* Move the cursor to after the last character in the
4455 * buffer, so that word at start of buffer is found
4456 * correctly. */
4457 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4458 first_match_pos.col =
4459 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 last_match_pos = first_match_pos;
4462 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004463
4464 /* Remember the first match so that the loop stops when we
4465 * wrap and come back there a second time. */
4466 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4469 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4470 {
4471 /* Scan a buffer, but not the current one. */
4472 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4473 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 first_match_pos.col = last_match_pos.col = 0;
4476 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4477 last_match_pos.lnum = 0;
4478 type = 0;
4479 }
4480 else /* unloaded buffer, scan like dictionary */
4481 {
4482 found_all = TRUE;
4483 if (ins_buf->b_fname == NULL)
4484 continue;
4485 type = CTRL_X_DICTIONARY;
4486 dict = ins_buf->b_fname;
4487 dict_f = DICT_EXACT;
4488 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004489 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 ins_buf->b_fname == NULL
4491 ? buf_spname(ins_buf)
4492 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004493 ? ins_buf->b_fname
4494 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004495 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497 else if (*e_cpt == NUL)
4498 break;
4499 else
4500 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004501 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 type = -1;
4503 else if (*e_cpt == 'k' || *e_cpt == 's')
4504 {
4505 if (*e_cpt == 'k')
4506 type = CTRL_X_DICTIONARY;
4507 else
4508 type = CTRL_X_THESAURUS;
4509 if (*++e_cpt != ',' && *e_cpt != NUL)
4510 {
4511 dict = e_cpt;
4512 dict_f = DICT_FIRST;
4513 }
4514 }
4515#ifdef FEAT_FIND_ID
4516 else if (*e_cpt == 'i')
4517 type = CTRL_X_PATH_PATTERNS;
4518 else if (*e_cpt == 'd')
4519 type = CTRL_X_PATH_DEFINES;
4520#endif
4521 else if (*e_cpt == ']' || *e_cpt == 't')
4522 {
4523 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004524 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004525 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527 else
4528 type = -1;
4529
4530 /* in any case e_cpt is advanced to the next entry */
4531 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4532
4533 found_all = TRUE;
4534 if (type == -1)
4535 continue;
4536 }
4537 }
4538
Bram Moolenaar4475b622017-05-01 20:46:52 +02004539 /* If complete() was called then compl_pattern has been reset. The
4540 * following won't work then, bail out. */
4541 if (compl_pattern == NULL)
4542 break;
4543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 switch (type)
4545 {
4546 case -1:
4547 break;
4548#ifdef FEAT_FIND_ID
4549 case CTRL_X_PATH_PATTERNS:
4550 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004551 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4556 (linenr_T)1, (linenr_T)MAXLNUM);
4557 break;
4558#endif
4559
4560 case CTRL_X_DICTIONARY:
4561 case CTRL_X_THESAURUS:
4562 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004563 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 : (type == CTRL_X_THESAURUS
4565 ? (*curbuf->b_p_tsr == NUL
4566 ? p_tsr
4567 : curbuf->b_p_tsr)
4568 : (*curbuf->b_p_dict == NUL
4569 ? p_dict
4570 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004571 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004572 dict != NULL ? dict_f
4573 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 dict = NULL;
4575 break;
4576
4577 case CTRL_X_TAGS:
4578 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4579 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004582 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583 * of matches is found when compl_pattern is empty */
4584 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004585 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4586 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4588 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004589 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 p_ic = save_p_ic;
4592 break;
4593
4594 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4597 {
4598
4599 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004600 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004601 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 break;
4604
4605 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 if (expand_cmdline(&compl_xp, compl_pattern,
4607 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004609 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 break;
4611
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004612#ifdef FEAT_COMPL_FUNC
4613 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004614 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004615 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004616 break;
4617#endif
4618
Bram Moolenaar488c6512005-08-11 20:09:58 +00004619 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004620#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004621 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004622 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004623 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004624 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004625#endif
4626 break;
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 default: /* normal ^P/^N and ^X^L */
4629 /*
4630 * If 'infercase' is set, don't use 'smartcase' here
4631 */
4632 save_p_scs = p_scs;
4633 if (ins_buf->b_p_inf)
4634 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635
Bram Moolenaar361aa502014-01-23 22:45:58 +01004636 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 * end but never from the middle, thus setting nowrapscan in this
4638 * buffers is a good idea, on the other hand, we always set
4639 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4640 save_p_ws = p_ws;
4641 if (ins_buf != curbuf)
4642 p_ws = FALSE;
4643 else if (*e_cpt == '.')
4644 p_ws = TRUE;
4645 for (;;)
4646 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004647 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004649 ++msg_silent; /* Don't want messages for wrapscan. */
4650
Bram Moolenaare4214502015-03-05 18:08:43 +01004651 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4652 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004653 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004654 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004655 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004657 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004659 found_new_match = searchit(NULL, ins_buf, pos,
4660 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004662 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004663 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004664 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004666 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004667 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 first_match_pos = *pos;
4669 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004670 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004673 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 found_new_match = FAIL;
4675 if (found_new_match == FAIL)
4676 {
4677 if (ins_buf == curbuf)
4678 found_all = TRUE;
4679 break;
4680 }
4681
4682 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 && ini->lnum == pos->lnum
4685 && ini->col == pos->col)
4686 continue;
4687 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004688 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004690 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
4692 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4693 continue;
4694 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4695 if (!p_paste)
4696 ptr = skipwhite(ptr);
4697 }
4698 len = (int)STRLEN(ptr);
4699 }
4700 else
4701 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004702 char_u *tmp_ptr = ptr;
4703
4704 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004706 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 /* Skip if already inside a word. */
4708 if (vim_iswordp(tmp_ptr))
4709 continue;
4710 /* Find start of next word. */
4711 tmp_ptr = find_word_start(tmp_ptr);
4712 }
4713 /* Find end of this word. */
4714 tmp_ptr = find_word_end(tmp_ptr);
4715 len = (int)(tmp_ptr - ptr);
4716
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004717 if ((compl_cont_status & CONT_ADDING)
4718 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
4720 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4721 {
4722 /* Try next line, if any. the new word will be
4723 * "join" as if the normal command "J" was used.
4724 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004725 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 * works -- Acevedo */
4727 STRNCPY(IObuff, ptr, len);
4728 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4729 tmp_ptr = ptr = skipwhite(ptr);
4730 /* Find start of next word. */
4731 tmp_ptr = find_word_start(tmp_ptr);
4732 /* Find end of next word. */
4733 tmp_ptr = find_word_end(tmp_ptr);
4734 if (tmp_ptr > ptr)
4735 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004736 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004738 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 IObuff[len++] = ' ';
4740 /* IObuf =~ "\k.* ", thus len >= 2 */
4741 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004742 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 || (vim_strchr(p_cpo, CPO_JOINSP)
4744 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004745 && (IObuff[len - 2] == '?'
4746 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 IObuff[len++] = ' ';
4748 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004749 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 if (tmp_ptr - ptr >= IOSIZE - len)
4751 tmp_ptr = ptr + IOSIZE - len - 1;
4752 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4753 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004754 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 IObuff[len] = NUL;
4757 ptr = IObuff;
4758 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004759 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 continue;
4761 }
4762 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004763 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004764 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004765 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 found_new_match = OK;
4768 break;
4769 }
4770 }
4771 p_scs = save_p_scs;
4772 p_ws = save_p_ws;
4773 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004774
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004775 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004776 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004777 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 found_new_match = OK;
4779
4780 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004781 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4782 * match */
4783 if ((ctrl_x_mode != CTRL_X_NORMAL
4784 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004785 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004786 {
4787 if (got_int)
4788 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004789 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004790 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004791 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004792
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004793 if ((ctrl_x_mode != CTRL_X_NORMAL
4794 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004795 || compl_interrupted)
4796 break;
4797 compl_started = TRUE;
4798 }
4799 else
4800 {
4801 /* Mark a buffer scanned when it has been scanned completely */
4802 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4803 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004805 compl_started = FALSE;
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004808 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004810 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 && *e_cpt == NUL) /* Got to end of 'complete' */
4812 found_new_match = FAIL;
4813
4814 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004815 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4816 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 i = ins_compl_make_cyclic();
4818
Bram Moolenaar4475b622017-05-01 20:46:52 +02004819 if (compl_old_match != NULL)
4820 {
4821 /* If several matches were added (FORWARD) or the search failed and has
4822 * just been made cyclic then we have to move compl_curr_match to the
4823 * next or previous entry (if any) -- Acevedo */
4824 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4825 : compl_old_match->cp_prev;
4826 if (compl_curr_match == NULL)
4827 compl_curr_match = compl_old_match;
4828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 return i;
4830}
4831
4832/* Delete the old text being completed. */
4833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004834ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004836 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
4838 /*
4839 * In insert mode: Delete the typed part.
4840 * In replace mode: Put the old characters back, if any.
4841 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004842 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4843 if ((int)curwin->w_cursor.col > col)
4844 {
4845 if (stop_arrow() == FAIL)
4846 return;
4847 backspace_until_column(col);
4848 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004849
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004850 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4851 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004853 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004854 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855}
4856
Bram Moolenaar472e8592016-10-15 17:06:47 +02004857/*
4858 * Insert the new text being completed.
4859 * "in_compl_func" is TRUE when called from complete_check().
4860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004862ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004864 dict_T *dict;
4865
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004866 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004867 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4868 compl_used_match = FALSE;
4869 else
4870 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004871
4872 /* Set completed item. */
4873 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004874 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004875 if (dict != NULL)
4876 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004877 dict_add_string(dict, "word", compl_shown_match->cp_str);
4878 dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4879 dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4880 dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4881 dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4882 dict_add_string(dict, "user_data",
4883 compl_shown_match->cp_text[CPT_USER_DATA]);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004884 }
4885 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004886 if (!in_compl_func)
4887 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888}
4889
4890/*
4891 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004892 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4893 * get more completions. If it is FALSE, then we just do nothing when there
4894 * are no more completions in a given direction. The latter case is used when
4895 * we are still in the middle of finding completions, to allow browsing
4896 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 * Return the total number of matches, or -1 if still unknown -- webb.
4898 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004899 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4900 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 *
4902 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004903 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4904 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 */
4906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004907ins_compl_next(
4908 int allow_get_expansion,
4909 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004910 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004911 int insert_match, /* Insert the newly selected match */
4912 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
4914 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004915 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004916 compl_T *found_compl = NULL;
4917 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004918 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004919 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004921 /* When user complete function return -1 for findstart which is next
4922 * time of 'always', compl_shown_match become NULL. */
4923 if (compl_shown_match == NULL)
4924 return -1;
4925
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004926 if (compl_leader != NULL
4927 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004929 /* Set "compl_shown_match" to the actually shown match, it may differ
4930 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004931 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004932 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004933 && compl_shown_match->cp_next != NULL
4934 && compl_shown_match->cp_next != compl_first_match)
4935 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004936
4937 /* If we didn't find it searching forward, and compl_shows_dir is
4938 * backward, find the last match. */
4939 if (compl_shows_dir == BACKWARD
4940 && !ins_compl_equal(compl_shown_match,
4941 compl_leader, (int)STRLEN(compl_leader))
4942 && (compl_shown_match->cp_next == NULL
4943 || compl_shown_match->cp_next == compl_first_match))
4944 {
4945 while (!ins_compl_equal(compl_shown_match,
4946 compl_leader, (int)STRLEN(compl_leader))
4947 && compl_shown_match->cp_prev != NULL
4948 && compl_shown_match->cp_prev != compl_first_match)
4949 compl_shown_match = compl_shown_match->cp_prev;
4950 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004951 }
4952
4953 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004954 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 /* Delete old text to be replaced */
4956 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004957
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004958 /* When finding the longest common text we stick at the original text,
4959 * don't let CTRL-N or CTRL-P move to the first match. */
4960 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4961
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004962 /* When restarting the search don't insert the first match either. */
4963 if (compl_restarting)
4964 {
4965 advance = FALSE;
4966 compl_restarting = FALSE;
4967 }
4968
Bram Moolenaare3226be2005-12-18 22:10:00 +00004969 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4970 * around. */
4971 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004973 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004975 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004976 found_end = (compl_first_match != NULL
4977 && (compl_shown_match->cp_next == compl_first_match
4978 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004979 }
4980 else if (compl_shows_dir == BACKWARD
4981 && compl_shown_match->cp_prev != NULL)
4982 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004983 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004984 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004985 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004988 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004989 if (!allow_get_expansion)
4990 {
4991 if (advance)
4992 {
4993 if (compl_shows_dir == BACKWARD)
4994 compl_pending -= todo + 1;
4995 else
4996 compl_pending += todo + 1;
4997 }
4998 return -1;
4999 }
5000
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005001 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005002 {
5003 if (compl_shows_dir == BACKWARD)
5004 --compl_pending;
5005 else
5006 ++compl_pending;
5007 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005008
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005009 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005010 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005011
5012 /* handle any pending completions */
5013 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005014 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005015 {
5016 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5017 {
5018 compl_shown_match = compl_shown_match->cp_next;
5019 --compl_pending;
5020 }
5021 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5022 {
5023 compl_shown_match = compl_shown_match->cp_prev;
5024 ++compl_pending;
5025 }
5026 else
5027 break;
5028 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005029 found_end = FALSE;
5030 }
5031 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5032 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005033 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005034 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005035 ++todo;
5036 else
5037 /* Remember a matching item. */
5038 found_compl = compl_shown_match;
5039
5040 /* Stop at the end of the list when we found a usable match. */
5041 if (found_end)
5042 {
5043 if (found_compl != NULL)
5044 {
5045 compl_shown_match = found_compl;
5046 break;
5047 }
5048 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005052 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005053 if (compl_no_insert && !started)
5054 {
5055 ins_bytes(compl_orig_text + ins_compl_len());
5056 compl_used_match = FALSE;
5057 }
5058 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005059 {
5060 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005061 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005062 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005063 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005064 }
5065 else
5066 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 if (!allow_get_expansion)
5069 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005070 /* may undisplay the popup menu first */
5071 ins_compl_upd_pum();
5072
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005073 /* redraw to show the user what was inserted */
5074 update_screen(0);
5075
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005076 /* display the updated popup menu */
5077 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005078#ifdef FEAT_GUI
5079 if (gui.in_use)
5080 {
5081 /* Show the cursor after the match, not after the redrawn text. */
5082 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005083 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005084 }
5085#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 /* Delete old text to be replaced, since we're still searching and
5088 * don't want to match ourselves! */
5089 ins_compl_delete();
5090 }
5091
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005092 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005093 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005094 if (compl_no_insert && !started)
5095 compl_enter_selects = TRUE;
5096 else
5097 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /*
5100 * Show the file name for the match (if any)
5101 * Truncate the file name to avoid a wait for return.
5102 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005103 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005105 char *lead = _("match in file");
5106 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5107 char_u *s;
5108 char_u *e;
5109
5110 if (space > 0)
5111 {
5112 /* We need the tail that fits. With double-byte encoding going
5113 * back from the end is very slow, thus go from the start and keep
5114 * the text that fits in "space" between "s" and "e". */
5115 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5116 {
5117 space -= ptr2cells(e);
5118 while (space < 0)
5119 {
5120 space += ptr2cells(s);
5121 MB_PTR_ADV(s);
5122 }
5123 }
5124 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5125 s > compl_shown_match->cp_fname ? "<" : "", s);
5126 msg(IObuff);
5127 redraw_cmdline = FALSE; /* don't overwrite! */
5128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130
5131 return num_matches;
5132}
5133
5134/*
5135 * Call this while finding completions, to check whether the user has hit a key
5136 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005137 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005139 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005140 * "in_compl_func" is TRUE when called from complete_check(), don't set
5141 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 */
5143 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005144ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005147 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005149 /* Don't check when reading keys from a script, :normal or feedkeys().
5150 * That would break the test scripts. But do check for keys when called
5151 * from complete_check(). */
5152 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 return;
5154
5155 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005156 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 return;
5158 count = 0;
5159
Bram Moolenaara260a972006-06-23 19:36:29 +00005160 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5161 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (c != NUL)
5164 {
5165 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5166 {
5167 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005168 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005169 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005170 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005172 else
5173 {
5174 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005175 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005176 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005177 if (c != K_IGNORE)
5178 {
5179 /* Don't interrupt completion when the character wasn't typed,
5180 * e.g., when doing @q to replay keys. */
5181 if (c != Ctrl_R && KeyTyped)
5182 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005183
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005184 vungetc(c);
5185 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005188 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005189 {
5190 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5191
5192 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005193 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005194 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005195}
5196
5197/*
5198 * Decide the direction of Insert mode complete from the key typed.
5199 * Returns BACKWARD or FORWARD.
5200 */
5201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005202ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005203{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005204 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005205 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005206 return BACKWARD;
5207 return FORWARD;
5208}
5209
5210/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005211 * Return TRUE for keys that are used for completion only when the popup menu
5212 * is visible.
5213 */
5214 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005215ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005216{
5217 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005218 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5219 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005220}
5221
5222/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005223 * Decide the number of completions to move forward.
5224 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5225 */
5226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005227ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005228{
5229 int h;
5230
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005231 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005232 {
5233 h = pum_get_height();
5234 if (h > 3)
5235 h -= 2; /* keep some context */
5236 return h;
5237 }
5238 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239}
5240
5241/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005242 * Return TRUE if completion with "c" should insert the match, FALSE if only
5243 * to change the currently selected completion.
5244 */
5245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005246ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005247{
5248 switch (c)
5249 {
5250 case K_UP:
5251 case K_DOWN:
5252 case K_PAGEDOWN:
5253 case K_KPAGEDOWN:
5254 case K_S_DOWN:
5255 case K_PAGEUP:
5256 case K_KPAGEUP:
5257 case K_S_UP:
5258 return FALSE;
5259 }
5260 return TRUE;
5261}
5262
5263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 * Do Insert mode completion.
5265 * Called when character "c" was typed, which has a meaning for completion.
5266 * Returns OK if completion was done, FAIL if something failed (out of mem).
5267 */
5268 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005269ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005271 char_u *line;
5272 int startcol = 0; /* column where searched text starts */
5273 colnr_T curs_col; /* cursor column */
5274 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005275 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005276 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005277 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005278 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
Bram Moolenaare3226be2005-12-18 22:10:00 +00005280 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005281 insert_match = ins_compl_use_match(c);
5282
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
5285 /* First time we hit ^N or ^P (in a row, I mean) */
5286
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 did_ai = FALSE;
5288#ifdef FEAT_SMARTINDENT
5289 did_si = FALSE;
5290 can_si = FALSE;
5291 can_si_back = FALSE;
5292#endif
5293 if (stop_arrow() == FAIL)
5294 return FAIL;
5295
5296 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005297 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005298 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005300 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 * "compl_startpos" to the cursor as a pattern to add a new word
5302 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005303 * "compl_startpos" is not in the same line as the cursor then fix it
5304 * (the line has been split because it was longer than 'tw'). if SOL
5305 * is set then skip the previous pattern, a word at the beginning of
5306 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005307 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5308 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 /*
5311 * it is a continued search
5312 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005314 if (ctrl_x_mode == CTRL_X_NORMAL
5315 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5316 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005318 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005320 /* line (probably) wrapped, set compl_startpos to the
5321 * first non_blank in the line, if it is not a wordchar
5322 * include it to get a better pattern, but then we don't
5323 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005324 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 compl_startpos.col = compl_col;
5326 compl_startpos.lnum = curwin->w_cursor.lnum;
5327 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329 else
5330 {
5331 /* S_IPOS was set when we inserted a word that was at the
5332 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 * mode but first we need to redefine compl_startpos */
5334 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 compl_cont_status |= CONT_SOL;
5337 compl_startpos.col = (colnr_T)(skipwhite(
5338 line + compl_length
5339 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005343 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005344 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005345 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 compl_cont_status &= ~CONT_SOL;
5350 compl_length = (IOSIZE - MIN_SPACE);
5351 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5354 if (compl_length < 1)
5355 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005357 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
5362 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005363 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005367 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005368 if (ctrl_x_mode != CTRL_X_NORMAL)
5369 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 compl_cont_status = 0;
5371 compl_cont_status |= CONT_N_ADDS;
5372 compl_startpos = curwin->w_cursor;
5373 startcol = (int)curs_col;
5374 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
5376
5377 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005378 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5382 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005383 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005385 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 compl_col += ++startcol;
5388 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005391 compl_pattern = str_foldcase(line + compl_col,
5392 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 compl_pattern = vim_strnsave(line + compl_col,
5395 compl_length);
5396 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 return FAIL;
5398 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
5401 char_u *prefix = (char_u *)"\\<";
5402
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005403 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005404 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005405 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005406 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005408 if (!vim_iswordp(line + compl_col)
5409 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 && (
5411#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415#endif
5416 )))
5417 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 STRCPY((char *)compl_pattern, prefix);
5419 (void)quote_meta(compl_pattern + STRLEN(prefix),
5420 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005422 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005424 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427#endif
5428 )
5429 {
5430 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005431 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5432 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005434 compl_col += curs_col;
5435 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
5437 else
5438 {
5439#ifdef FEAT_MBYTE
5440 /* Search the point of change class of multibyte character
5441 * or not a word single byte character backward. */
5442 if (has_mbyte)
5443 {
5444 int base_class;
5445 int head_off;
5446
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005447 startcol -= (*mb_head_off)(line, line + startcol);
5448 base_class = mb_get_class(line + startcol);
5449 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005451 head_off = (*mb_head_off)(line, line + startcol);
5452 if (base_class != mb_get_class(line + startcol
5453 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
5457 }
5458 else
5459#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005460 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005462 compl_col += ++startcol;
5463 compl_length = (int)curs_col - startcol;
5464 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 /* Only match word with at least two chars -- webb
5467 * there's no need to call quote_meta,
5468 * alloc(7) is enough -- Acevedo
5469 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005470 compl_pattern = alloc(7);
5471 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005473 STRCPY((char *)compl_pattern, "\\<");
5474 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5475 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 }
5477 else
5478 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005480 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005483 STRCPY((char *)compl_pattern, "\\<");
5484 (void)quote_meta(compl_pattern + 2, line + compl_col,
5485 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 }
5488 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005489 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005491 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005492 compl_length = (int)curs_col - (int)compl_col;
5493 if (compl_length < 0) /* cursor in indent: empty pattern */
5494 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005496 compl_pattern = str_foldcase(line + compl_col, compl_length,
5497 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005499 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5500 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 return FAIL;
5502 }
5503 else if (ctrl_x_mode == CTRL_X_FILES)
5504 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005505 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005506 if (startcol > 0)
5507 {
5508 char_u *p = line + startcol;
5509
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005510 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005511 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005512 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005513 if (p == line && vim_isfilec(PTR2CHAR(p)))
5514 startcol = 0;
5515 else
5516 startcol = (int)(p - line) + 1;
5517 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005518
Bram Moolenaar0300e462013-09-08 16:03:45 +02005519 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005520 compl_length = (int)curs_col - startcol;
5521 compl_pattern = addstar(line + compl_col, compl_length,
5522 EXPAND_FILES);
5523 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 return FAIL;
5525 }
5526 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5527 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005528 compl_pattern = vim_strnsave(line, curs_col);
5529 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005531 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005532 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005533 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5534 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005535 /* No completion possible, use an empty pattern to get a
5536 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005537 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005538 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005539 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5540 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005542 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005543 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005544#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005545 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005546 * Call user defined function 'completefunc' with "a:findstart"
5547 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005548 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005549 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005550 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005551 char_u *funcname;
5552 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005553 win_T *curwin_save;
5554 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005555
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005556 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005557 * string */
5558 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5559 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5560 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005561 {
5562 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5563 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005564 /* restore did_ai, so that adding comment leader works */
5565 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005566 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005567 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005568
Bram Moolenaarffa96842018-06-12 22:05:14 +02005569 args[0].v_type = VAR_NUMBER;
5570 args[0].vval.v_number = 1;
5571 args[1].v_type = VAR_STRING;
5572 args[1].vval.v_string = (char_u *)"";
5573 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005574 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005575 curwin_save = curwin;
5576 curbuf_save = curbuf;
Bram Moolenaarded27a12018-08-01 19:06:03 +02005577 col = call_func_retnr(funcname, 2, args);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005578 if (curwin_save != curwin || curbuf_save != curbuf)
5579 {
5580 EMSG(_(e_complwin));
5581 return FAIL;
5582 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005583 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005584 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005585 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005586 {
5587 EMSG(_(e_compldel));
5588 return FAIL;
5589 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005590
Bram Moolenaar6110a002012-01-26 18:58:38 +01005591 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005592 * cancel the complete without an error.
5593 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005594 if (col == -2)
5595 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005596 if (col == -3)
5597 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005598 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005599 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005600 if (!shortmess(SHM_COMPLETIONMENU))
5601 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005602 return FAIL;
5603 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005604
Bram Moolenaar82139082011-09-14 16:52:09 +02005605 /*
5606 * Reset extended parameters of completion, when start new
5607 * completion.
5608 */
5609 compl_opt_refresh_always = FALSE;
5610
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005611 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005612 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005613 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005614 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005615 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005616
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005617 /* Setup variables for completion. Need to obtain "line" again,
5618 * it may have become invalid. */
5619 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005620 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005621 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5622 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005623#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005624 return FAIL;
5625 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005626 else if (ctrl_x_mode == CTRL_X_SPELL)
5627 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005628#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005629 if (spell_bad_len > 0)
5630 compl_col = curs_col - spell_bad_len;
5631 else
5632 compl_col = spell_word_start(startcol);
5633 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005634 {
5635 compl_length = 0;
5636 compl_col = curs_col;
5637 }
5638 else
5639 {
5640 spell_expand_check_cap(compl_col);
5641 compl_length = (int)curs_col - compl_col;
5642 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005643 /* Need to obtain "line" again, it may have become invalid. */
5644 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005645 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5646 if (compl_pattern == NULL)
5647#endif
5648 return FAIL;
5649 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005650 else
5651 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005652 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005653 return FAIL;
5654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005656 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
5658 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005659 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 /* Insert a new line, keep indentation but ignore 'comments' */
5662#ifdef FEAT_COMMENTS
5663 char_u *old = curbuf->b_p_com;
5664
5665 curbuf->b_p_com = (char_u *)"";
5666#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005667 compl_startpos.lnum = curwin->w_cursor.lnum;
5668 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 ins_eol('\r');
5670#ifdef FEAT_COMMENTS
5671 curbuf->b_p_com = old;
5672#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005673 compl_length = 0;
5674 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676 }
5677 else
5678 {
5679 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005680 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005683 if (compl_cont_status & CONT_LOCAL)
5684 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 else
5686 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5687
Bram Moolenaar62951b12011-09-21 18:23:05 +02005688 /* If any of the original typed text has been changed we need to fix
5689 * the redo buffer. */
5690 ins_compl_fixRedoBufForLeader(NULL);
5691
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005692 /* Always add completion for the original text. */
5693 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005694 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5695 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005696 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005698 VIM_CLEAR(compl_pattern);
5699 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 return FAIL;
5701 }
5702
5703 /* showmode might reset the internal line pointers, so it must
5704 * be called before line = ml_get(), or when this address is no
5705 * longer needed. -- Acevedo.
5706 */
5707 edit_submode_extra = (char_u *)_("-- Searching...");
5708 edit_submode_highl = HLF_COUNT;
5709 showmode();
5710 edit_submode_extra = NULL;
5711 out_flush();
5712 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005713 else if (insert_match && stop_arrow() == FAIL)
5714 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005716 compl_shown_match = compl_curr_match;
5717 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
5719 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005720 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005722 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005723 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005724 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005726 /* may undisplay the popup menu */
5727 ins_compl_upd_pum();
5728
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005729 if (n > 1) /* all matches have been found */
5730 compl_matches = n;
5731 compl_curr_match = compl_shown_match;
5732 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
Bram Moolenaard68071d2006-05-02 22:08:30 +00005734 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5735 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 if (got_int && !global_busy)
5737 {
5738 (void)vgetc();
5739 got_int = FALSE;
5740 }
5741
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005742 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005743 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005745 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5746 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5748 edit_submode_highl = HLF_E;
5749 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5750 * because we couldn't expand anything at first place, but if we used
5751 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5752 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005753 if ( compl_length > 1
5754 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005755 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5757 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005758 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
5760
Bram Moolenaar572cb562005-08-05 21:35:02 +00005761 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005762 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005764 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 if (edit_submode_extra == NULL)
5767 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005768 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
5770 edit_submode_extra = (char_u *)_("Back at original");
5771 edit_submode_highl = HLF_W;
5772 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005773 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 edit_submode_extra = (char_u *)_("Word from other line");
5776 edit_submode_highl = HLF_COUNT;
5777 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005778 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
5780 edit_submode_extra = (char_u *)_("The only match");
5781 edit_submode_highl = HLF_COUNT;
5782 }
5783 else
5784 {
5785 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005786 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005788 int number = 0;
5789 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005791 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
5793 /* search backwards for the first valid (!= -1) number.
5794 * This should normally succeed already at the first loop
5795 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005796 for (match = compl_curr_match->cp_prev; match != NULL
5797 && match != compl_first_match;
5798 match = match->cp_prev)
5799 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005801 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 break;
5803 }
5804 if (match != NULL)
5805 /* go up and assign all numbers which are not assigned
5806 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005807 for (match = match->cp_next;
5808 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005809 match = match->cp_next)
5810 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
5812 else /* BACKWARD */
5813 {
5814 /* search forwards (upwards) for the first valid (!= -1)
5815 * number. This should normally succeed already at the
5816 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005817 for (match = compl_curr_match->cp_next; match != NULL
5818 && match != compl_first_match;
5819 match = match->cp_next)
5820 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005822 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 break;
5824 }
5825 if (match != NULL)
5826 /* go down and assign all numbers which are not
5827 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005828 for (match = match->cp_prev; match
5829 && match->cp_number == -1;
5830 match = match->cp_prev)
5831 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
5833 }
5834
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005835 /* The match should always have a sequence number now, this is
5836 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005837 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005839 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5840 * Translations may need more than twice that. */
5841 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005843 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005844 vim_snprintf((char *)match_ref, sizeof(match_ref),
5845 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005846 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005848 vim_snprintf((char *)match_ref, sizeof(match_ref),
5849 _("match %d"),
5850 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 edit_submode_extra = match_ref;
5852 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005853 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 curs_columns(FALSE);
5855 }
5856 }
5857 }
5858
5859 /* Show a message about what (completion) mode we're in. */
5860 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005861 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005863 if (edit_submode_extra != NULL)
5864 {
5865 if (!p_smd)
5866 msg_attr(edit_submode_extra,
5867 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005868 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005869 }
5870 else
5871 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
Bram Moolenaard68071d2006-05-02 22:08:30 +00005874 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005875 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005876 show_pum(save_w_wrow, save_w_leftcol);
5877
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005878 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005879 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005880
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 return OK;
5882}
5883
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005884 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005885show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005886{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005887 /* RedrawingDisabled may be set when invoked through complete(). */
5888 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005889
Bram Moolenaarcb036422017-03-01 12:29:10 +01005890 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005891
Bram Moolenaarcb036422017-03-01 12:29:10 +01005892 /* If the cursor moved or the display scrolled we need to remove the pum
5893 * first. */
5894 setcursor();
5895 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5896 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005897
Bram Moolenaarcb036422017-03-01 12:29:10 +01005898 ins_compl_show_pum();
5899 setcursor();
5900 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005901}
5902
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903/*
5904 * Looks in the first "len" chars. of "src" for search-metachars.
5905 * If dest is not NULL the chars. are copied there quoting (with
5906 * a backslash) the metachars, and dest would be NUL terminated.
5907 * Returns the length (needed) of dest
5908 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005909 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005910quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005912 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005914 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 {
5916 switch (*src)
5917 {
5918 case '.':
5919 case '*':
5920 case '[':
5921 if (ctrl_x_mode == CTRL_X_DICTIONARY
5922 || ctrl_x_mode == CTRL_X_THESAURUS)
5923 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005924 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 case '~':
5926 if (!p_magic) /* quote these only if magic is set */
5927 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005928 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 case '\\':
5930 if (ctrl_x_mode == CTRL_X_DICTIONARY
5931 || ctrl_x_mode == CTRL_X_THESAURUS)
5932 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005933 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 case '^': /* currently it's not needed. */
5935 case '$':
5936 m++;
5937 if (dest != NULL)
5938 *dest++ = '\\';
5939 break;
5940 }
5941 if (dest != NULL)
5942 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005943# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 /* Copy remaining bytes of a multibyte character. */
5945 if (has_mbyte)
5946 {
5947 int i, mb_len;
5948
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005949 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 if (mb_len > 0 && len >= mb_len)
5951 for (i = 0; i < mb_len; ++i)
5952 {
5953 --len;
5954 ++src;
5955 if (dest != NULL)
5956 *dest++ = *src;
5957 }
5958 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 }
5961 if (dest != NULL)
5962 *dest = NUL;
5963
5964 return m;
5965}
5966#endif /* FEAT_INS_EXPAND */
5967
5968/*
5969 * Next character is interpreted literally.
5970 * A one, two or three digit decimal number is interpreted as its byte value.
5971 * If one or two digits are entered, the next character is given to vungetc().
5972 * For Unicode a character > 255 may be returned.
5973 */
5974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005975get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 int cc;
5978 int nc;
5979 int i;
5980 int hex = FALSE;
5981 int octal = FALSE;
5982#ifdef FEAT_MBYTE
5983 int unicode = 0;
5984#endif
5985
5986 if (got_int)
5987 return Ctrl_C;
5988
5989#ifdef FEAT_GUI
5990 /*
5991 * In GUI there is no point inserting the internal code for a special key.
5992 * It is more useful to insert the string "<KEY>" instead. This would
5993 * probably be useful in a text window too, but it would not be
5994 * vi-compatible (maybe there should be an option for it?) -- webb
5995 */
5996 if (gui.in_use)
5997 ++allow_keys;
5998#endif
5999#ifdef USE_ON_FLY_SCROLL
6000 dont_scroll = TRUE; /* disallow scrolling here */
6001#endif
6002 ++no_mapping; /* don't map the next key hits */
6003 cc = 0;
6004 i = 0;
6005 for (;;)
6006 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006007 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#ifdef FEAT_CMDL_INFO
6009 if (!(State & CMDLINE)
6010# ifdef FEAT_MBYTE
6011 && MB_BYTE2LEN_CHECK(nc) == 1
6012# endif
6013 )
6014 add_to_showcmd(nc);
6015#endif
6016 if (nc == 'x' || nc == 'X')
6017 hex = TRUE;
6018 else if (nc == 'o' || nc == 'O')
6019 octal = TRUE;
6020#ifdef FEAT_MBYTE
6021 else if (nc == 'u' || nc == 'U')
6022 unicode = nc;
6023#endif
6024 else
6025 {
6026 if (hex
6027#ifdef FEAT_MBYTE
6028 || unicode != 0
6029#endif
6030 )
6031 {
6032 if (!vim_isxdigit(nc))
6033 break;
6034 cc = cc * 16 + hex2nr(nc);
6035 }
6036 else if (octal)
6037 {
6038 if (nc < '0' || nc > '7')
6039 break;
6040 cc = cc * 8 + nc - '0';
6041 }
6042 else
6043 {
6044 if (!VIM_ISDIGIT(nc))
6045 break;
6046 cc = cc * 10 + nc - '0';
6047 }
6048
6049 ++i;
6050 }
6051
6052 if (cc > 255
6053#ifdef FEAT_MBYTE
6054 && unicode == 0
6055#endif
6056 )
6057 cc = 255; /* limit range to 0-255 */
6058 nc = 0;
6059
6060 if (hex) /* hex: up to two chars */
6061 {
6062 if (i >= 2)
6063 break;
6064 }
6065#ifdef FEAT_MBYTE
6066 else if (unicode) /* Unicode: up to four or eight chars */
6067 {
6068 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6069 break;
6070 }
6071#endif
6072 else if (i >= 3) /* decimal or octal: up to three chars */
6073 break;
6074 }
6075 if (i == 0) /* no number entered */
6076 {
6077 if (nc == K_ZERO) /* NUL is stored as NL */
6078 {
6079 cc = '\n';
6080 nc = 0;
6081 }
6082 else
6083 {
6084 cc = nc;
6085 nc = 0;
6086 }
6087 }
6088
6089 if (cc == 0) /* NUL is stored as NL */
6090 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006091#ifdef FEAT_MBYTE
6092 if (enc_dbcs && (cc & 0xff) == 0)
6093 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6094 second byte will cause trouble! */
6095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
6097 --no_mapping;
6098#ifdef FEAT_GUI
6099 if (gui.in_use)
6100 --allow_keys;
6101#endif
6102 if (nc)
6103 vungetc(nc);
6104 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6105 return cc;
6106}
6107
6108/*
6109 * Insert character, taking care of special keys and mod_mask
6110 */
6111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006112insert_special(
6113 int c,
6114 int allow_modmask,
6115 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
6117 char_u *p;
6118 int len;
6119
6120 /*
6121 * Special function key, translate into "<Key>". Up to the last '>' is
6122 * inserted with ins_str(), so as not to replace characters in replace
6123 * mode.
6124 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6125 * unless 'allow_modmask' is TRUE.
6126 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006127#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 /* Command-key never produces a normal key */
6129 if (mod_mask & MOD_MASK_CMD)
6130 allow_modmask = TRUE;
6131#endif
6132 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6133 {
6134 p = get_special_key_name(c, mod_mask);
6135 len = (int)STRLEN(p);
6136 c = p[len - 1];
6137 if (len > 2)
6138 {
6139 if (stop_arrow() == FAIL)
6140 return;
6141 p[len - 1] = NUL;
6142 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006143 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 ctrlv = FALSE;
6145 }
6146 }
6147 if (stop_arrow() == OK)
6148 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6149}
6150
6151/*
6152 * Special characters in this context are those that need processing other
6153 * than the simple insertion that can be performed here. This includes ESC
6154 * which terminates the insert, and CR/NL which need special processing to
6155 * open up a new line. This routine tries to optimize insertions performed by
6156 * the "redo", "undo" or "put" commands, so it needs to know when it should
6157 * stop and defer processing to the "normal" mechanism.
6158 * '0' and '^' are special, because they can be followed by CTRL-D.
6159 */
6160#ifdef EBCDIC
6161# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6162#else
6163# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6164#endif
6165
6166#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006167# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006169# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170#endif
6171
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006172/*
6173 * "flags": INSCHAR_FORMAT - force formatting
6174 * INSCHAR_CTRLV - char typed just after CTRL-V
6175 * INSCHAR_NO_FEX - don't use 'formatexpr'
6176 *
6177 * NOTE: passes the flags value straight through to internal_format() which,
6178 * beside INSCHAR_FORMAT (above), is also looking for these:
6179 * INSCHAR_DO_COM - format comments
6180 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006183insertchar(
6184 int c, /* character to insert or NUL */
6185 int flags, /* INSCHAR_FORMAT, etc. */
6186 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 int textwidth;
6189#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006193 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006195 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
6198 /*
6199 * Try to break the line in two or more pieces when:
6200 * - Always do this if we have been called to do formatting only.
6201 * - Always do this when 'formatoptions' has the 'a' flag and the line
6202 * ends in white space.
6203 * - Otherwise:
6204 * - Don't do this if inserting a blank
6205 * - Don't do this if an existing character is being replaced, unless
6206 * we're in VREPLACE mode.
6207 * - Do this if the cursor is not on the line where insert started
6208 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6209 * before the insert.
6210 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6211 * before 'textwidth'
6212 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006214 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006215 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 && !((State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 && !(State & VREPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 && *ml_get_cursor() != NUL)
6219 && (curwin->w_cursor.lnum != Insstart.lnum
6220 || ((!has_format_option(FO_INS_LONG)
6221 || Insstart_textlen <= (colnr_T)textwidth)
6222 && (!fo_ins_blank
6223 || Insstart_blank_vcol <= (colnr_T)textwidth
6224 ))))))
6225 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226 /* Format with 'formatexpr' when it's set. Use internal formatting
6227 * when 'formatexpr' isn't set or it returns non-zero. */
6228#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006229 int do_internal = TRUE;
6230 colnr_T virtcol = get_nolist_virtcol()
6231 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006232
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006233 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6234 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006235 {
6236 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6237 /* It may be required to save for undo again, e.g. when setline()
6238 * was called. */
6239 ins_need_undo = TRUE;
6240 }
6241 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006243 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006245
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 if (c == NUL) /* only formatting was wanted */
6247 return;
6248
6249#ifdef FEAT_COMMENTS
6250 /* Check whether this character should end a comment. */
6251 if (did_ai && (int)c == end_comment_pending)
6252 {
6253 char_u *line;
6254 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6255 int middle_len, end_len;
6256 int i;
6257
6258 /*
6259 * Need to remove existing (middle) comment leader and insert end
6260 * comment leader. First, check what comment leader we can find.
6261 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006262 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6264 {
6265 /* Skip middle-comment string */
6266 while (*p && p[-1] != ':') /* find end of middle flags */
6267 ++p;
6268 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6269 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006270 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 --middle_len;
6272
6273 /* Find the end-comment string */
6274 while (*p && p[-1] != ':') /* find end of end flags */
6275 ++p;
6276 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6277
6278 /* Skip white space before the cursor */
6279 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006280 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 ;
6282 i++;
6283
6284 /* Skip to before the middle leader */
6285 i -= middle_len;
6286
6287 /* Check some expected things before we go on */
6288 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6289 {
6290 /* Backspace over all the stuff we want to replace */
6291 backspace_until_column(i);
6292
6293 /*
6294 * Insert the end-comment string, except for the last
6295 * character, which will get inserted as normal later.
6296 */
6297 ins_bytes_len(lead_end, end_len - 1);
6298 }
6299 }
6300 }
6301 end_comment_pending = NUL;
6302#endif
6303
6304 did_ai = FALSE;
6305#ifdef FEAT_SMARTINDENT
6306 did_si = FALSE;
6307 can_si = FALSE;
6308 can_si_back = FALSE;
6309#endif
6310
6311 /*
6312 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6313 * This speeds up normal text input considerably.
6314 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6315 * need to re-indent at a ':', or any other character (but not what
6316 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006317 * Don't do this when there an InsertCharPre autocommand is defined,
6318 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006319 * Do the check for InsertCharPre before the call to vpeekc() because the
6320 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 */
6322#ifdef USE_ON_FLY_SCROLL
6323 dont_scroll = FALSE; /* allow scrolling here */
6324#endif
6325
6326 if ( !ISSPECIAL(c)
6327#ifdef FEAT_MBYTE
6328 && (!has_mbyte || (*mb_char2len)(c) == 1)
6329#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006330 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 && vpeekc() != NUL
6332 && !(State & REPLACE_FLAG)
6333#ifdef FEAT_CINDENT
6334 && !cindent_on()
6335#endif
6336#ifdef FEAT_RIGHTLEFT
6337 && !p_ri
6338#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006339 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
6341#define INPUT_BUFLEN 100
6342 char_u buf[INPUT_BUFLEN + 1];
6343 int i;
6344 colnr_T virtcol = 0;
6345
6346 buf[0] = c;
6347 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006348 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 virtcol = get_nolist_virtcol();
6350 /*
6351 * Stop the string when:
6352 * - no more chars available
6353 * - finding a special character (command key)
6354 * - buffer is full
6355 * - running into the 'textwidth' boundary
6356 * - need to check for abbreviation: A non-word char after a word-char
6357 */
6358 while ( (c = vpeekc()) != NUL
6359 && !ISSPECIAL(c)
6360#ifdef FEAT_MBYTE
6361 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6362#endif
6363 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006364# ifdef FEAT_FKMAP
6365 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 && (textwidth == 0
6368 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6369 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6370 {
6371#ifdef FEAT_RIGHTLEFT
6372 c = vgetc();
6373 if (p_hkmap && KeyTyped)
6374 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 buf[i++] = c;
6376#else
6377 buf[i++] = vgetc();
6378#endif
6379 }
6380
6381#ifdef FEAT_DIGRAPHS
6382 do_digraph(-1); /* clear digraphs */
6383 do_digraph(buf[i-1]); /* may be the start of a digraph */
6384#endif
6385 buf[i] = NUL;
6386 ins_str(buf);
6387 if (flags & INSCHAR_CTRLV)
6388 {
6389 redo_literal(*buf);
6390 i = 1;
6391 }
6392 else
6393 i = 0;
6394 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006395 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 }
6397 else
6398 {
6399#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006400 int cc;
6401
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6403 {
6404 char_u buf[MB_MAXBYTES + 1];
6405
6406 (*mb_char2bytes)(c, buf);
6407 buf[cc] = NUL;
6408 ins_char_bytes(buf, cc);
6409 AppendCharToRedobuff(c);
6410 }
6411 else
6412#endif
6413 {
6414 ins_char(c);
6415 if (flags & INSCHAR_CTRLV)
6416 redo_literal(c);
6417 else
6418 AppendCharToRedobuff(c);
6419 }
6420 }
6421}
6422
6423/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006424 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006425 *
6426 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6427 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006428 */
6429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006430internal_format(
6431 int textwidth,
6432 int second_indent,
6433 int flags,
6434 int format_only,
6435 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006436{
6437 int cc;
6438 int save_char = NUL;
6439 int haveto_redraw = FALSE;
6440 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6441#ifdef FEAT_MBYTE
6442 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6443#endif
6444 int fo_white_par = has_format_option(FO_WHITE_PAR);
6445 int first_line = TRUE;
6446#ifdef FEAT_COMMENTS
6447 colnr_T leader_len;
6448 int no_leader = FALSE;
6449 int do_comments = (flags & INSCHAR_DO_COM);
6450#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006451#ifdef FEAT_LINEBREAK
6452 int has_lbr = curwin->w_p_lbr;
6453
6454 /* make sure win_lbr_chartabsize() counts correctly */
6455 curwin->w_p_lbr = FALSE;
6456#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006457
6458 /*
6459 * When 'ai' is off we don't want a space under the cursor to be
6460 * deleted. Replace it with an 'x' temporarily.
6461 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02006462 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006463 {
6464 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006465 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466 {
6467 save_char = cc;
6468 pchar_cursor('x');
6469 }
6470 }
6471
6472 /*
6473 * Repeat breaking lines, until the current line is not too long.
6474 */
6475 while (!got_int)
6476 {
6477 int startcol; /* Cursor column at entry */
6478 int wantcol; /* column at textwidth border */
6479 int foundcol; /* column for start of spaces */
6480 int end_foundcol = 0; /* column for start of word */
6481 colnr_T len;
6482 colnr_T virtcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006483 int orig_col = 0;
6484 char_u *saved_text = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006485 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006486 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006487
Bram Moolenaar97b98102009-11-17 16:41:01 +00006488 virtcol = get_nolist_virtcol()
6489 + char2cells(c != NUL ? c : gchar_cursor());
6490 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491 break;
6492
6493#ifdef FEAT_COMMENTS
6494 if (no_leader)
6495 do_comments = FALSE;
6496 else if (!(flags & INSCHAR_FORMAT)
6497 && has_format_option(FO_WRAP_COMS))
6498 do_comments = TRUE;
6499
6500 /* Don't break until after the comment leader */
6501 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006502 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006503 else
6504 leader_len = 0;
6505
6506 /* If the line doesn't start with a comment leader, then don't
6507 * start one in a following broken line. Avoids that a %word
6508 * moved to the start of the next line causes all following lines
6509 * to start with %. */
6510 if (leader_len == 0)
6511 no_leader = TRUE;
6512#endif
6513 if (!(flags & INSCHAR_FORMAT)
6514#ifdef FEAT_COMMENTS
6515 && leader_len == 0
6516#endif
6517 && !has_format_option(FO_WRAP))
6518
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006519 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006520 if ((startcol = curwin->w_cursor.col) == 0)
6521 break;
6522
6523 /* find column of textwidth border */
6524 coladvance((colnr_T)textwidth);
6525 wantcol = curwin->w_cursor.col;
6526
Bram Moolenaar97b98102009-11-17 16:41:01 +00006527 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006528 foundcol = 0;
6529
6530 /*
6531 * Find position to break at.
6532 * Stop at first entered white when 'formatoptions' has 'v'
6533 */
6534 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006535 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006536 || curwin->w_cursor.lnum != Insstart.lnum
6537 || curwin->w_cursor.col >= Insstart.col)
6538 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006539 if (curwin->w_cursor.col == startcol && c != NUL)
6540 cc = c;
6541 else
6542 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006543 if (WHITECHAR(cc))
6544 {
6545 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006546 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006547
6548 /* find start of sequence of blanks */
6549 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6550 {
6551 dec_cursor();
6552 cc = gchar_cursor();
6553 }
6554 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6555 break; /* only spaces in front of text */
6556#ifdef FEAT_COMMENTS
6557 /* Don't break until after the comment leader */
6558 if (curwin->w_cursor.col < leader_len)
6559 break;
6560#endif
6561 if (has_format_option(FO_ONE_LETTER))
6562 {
6563 /* do not break after one-letter words */
6564 if (curwin->w_cursor.col == 0)
6565 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006566#ifdef FEAT_COMMENTS
6567 /* do not break "#a b" when 'tw' is 2 */
6568 if (curwin->w_cursor.col <= leader_len)
6569 break;
6570#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006571 col = curwin->w_cursor.col;
6572 dec_cursor();
6573 cc = gchar_cursor();
6574
6575 if (WHITECHAR(cc))
6576 continue; /* one-letter, continue */
6577 curwin->w_cursor.col = col;
6578 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006579
6580 inc_cursor();
6581
6582 end_foundcol = end_col + 1;
6583 foundcol = curwin->w_cursor.col;
6584 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006585 break;
6586 }
6587#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006588 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006589 {
6590 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006591 if (curwin->w_cursor.col != startcol)
6592 {
6593#ifdef FEAT_COMMENTS
6594 /* Don't break until after the comment leader */
6595 if (curwin->w_cursor.col < leader_len)
6596 break;
6597#endif
6598 col = curwin->w_cursor.col;
6599 inc_cursor();
6600 /* Don't change end_foundcol if already set. */
6601 if (foundcol != curwin->w_cursor.col)
6602 {
6603 foundcol = curwin->w_cursor.col;
6604 end_foundcol = foundcol;
6605 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6606 break;
6607 }
6608 curwin->w_cursor.col = col;
6609 }
6610
6611 if (curwin->w_cursor.col == 0)
6612 break;
6613
6614 col = curwin->w_cursor.col;
6615
6616 dec_cursor();
6617 cc = gchar_cursor();
6618
6619 if (WHITECHAR(cc))
6620 continue; /* break with space */
6621#ifdef FEAT_COMMENTS
6622 /* Don't break until after the comment leader */
6623 if (curwin->w_cursor.col < leader_len)
6624 break;
6625#endif
6626
6627 curwin->w_cursor.col = col;
6628
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006629 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006630 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006631 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6632 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006633 }
6634#endif
6635 if (curwin->w_cursor.col == 0)
6636 break;
6637 dec_cursor();
6638 }
6639
6640 if (foundcol == 0) /* no spaces, cannot break line */
6641 {
6642 curwin->w_cursor.col = startcol;
6643 break;
6644 }
6645
6646 /* Going to break the line, remove any "$" now. */
6647 undisplay_dollar();
6648
6649 /*
6650 * Offset between cursor position and line break is used by replace
6651 * stack functions. VREPLACE does not use this, and backspaces
6652 * over the text instead.
6653 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006654 if (State & VREPLACE_FLAG)
6655 orig_col = startcol; /* Will start backspacing from here */
6656 else
Bram Moolenaar97b98102009-11-17 16:41:01 +00006657 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006658
6659 /*
6660 * adjust startcol for spaces that will be deleted and
6661 * characters that will remain on top line
6662 */
6663 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006664 while ((cc = gchar_cursor(), WHITECHAR(cc))
6665 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006666 inc_cursor();
6667 startcol -= curwin->w_cursor.col;
6668 if (startcol < 0)
6669 startcol = 0;
6670
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006671 if (State & VREPLACE_FLAG)
6672 {
6673 /*
6674 * In VREPLACE mode, we will backspace over the text to be
6675 * wrapped, so save a copy now to put on the next line.
6676 */
6677 saved_text = vim_strsave(ml_get_cursor());
6678 curwin->w_cursor.col = orig_col;
6679 if (saved_text == NULL)
6680 break; /* Can't do it, out of memory */
6681 saved_text[startcol] = NUL;
6682
6683 /* Backspace over characters that will move to the next line */
6684 if (!fo_white_par)
6685 backspace_until_column(foundcol);
6686 }
6687 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006688 {
6689 /* put cursor after pos. to break line */
6690 if (!fo_white_par)
6691 curwin->w_cursor.col = foundcol;
6692 }
6693
6694 /*
6695 * Split the line just before the margin.
6696 * Only insert/delete lines, but don't really redraw the window.
6697 */
6698 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6699 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6700#ifdef FEAT_COMMENTS
6701 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006702 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006703#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006704 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6705 if (!(flags & INSCHAR_COM_LIST))
6706 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707
6708 replace_offset = 0;
6709 if (first_line)
6710 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006711 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006712 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006713 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006714 * This section is for auto-wrap of numeric lists. When not
6715 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6716 * flag will be set and open_line() will handle it (as seen
6717 * above). The code here (and in get_number_indent()) will
6718 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006719 */
6720 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006721 second_indent =
6722 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006723 if (second_indent >= 0)
6724 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006725 if (State & VREPLACE_FLAG)
6726 change_indent(INDENT_SET, second_indent,
6727 FALSE, NUL, TRUE);
6728 else
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006729#ifdef FEAT_COMMENTS
6730 if (leader_len > 0 && second_indent - leader_len > 0)
6731 {
6732 int i;
6733 int padding = second_indent - leader_len;
6734
6735 /* We started at the first_line of a numbered list
6736 * that has a comment. the open_line() function has
6737 * inserted the proper comment leader and positioned
6738 * the cursor at the end of the split line. Now we
6739 * add the additional whitespace needed after the
6740 * comment leader for the numbered list. */
6741 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006742 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006743 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006744 }
6745 else
6746 {
6747#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006748 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006749#ifdef FEAT_COMMENTS
6750 }
6751#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006752 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006753 }
6754 first_line = FALSE;
6755 }
6756
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006757 if (State & VREPLACE_FLAG)
6758 {
6759 /*
6760 * In VREPLACE mode we have backspaced over the text to be
6761 * moved, now we re-insert it into the new line.
6762 */
6763 ins_bytes(saved_text);
6764 vim_free(saved_text);
6765 }
6766 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006767 {
6768 /*
6769 * Check if cursor is not past the NUL off the line, cindent
6770 * may have added or removed indent.
6771 */
6772 curwin->w_cursor.col += startcol;
6773 len = (colnr_T)STRLEN(ml_get_curline());
6774 if (curwin->w_cursor.col > len)
6775 curwin->w_cursor.col = len;
6776 }
6777
6778 haveto_redraw = TRUE;
6779#ifdef FEAT_CINDENT
6780 can_cindent = TRUE;
6781#endif
6782 /* moved the cursor, don't autoindent or cindent now */
6783 did_ai = FALSE;
6784#ifdef FEAT_SMARTINDENT
6785 did_si = FALSE;
6786 can_si = FALSE;
6787 can_si_back = FALSE;
6788#endif
6789 line_breakcheck();
6790 }
6791
6792 if (save_char != NUL) /* put back space after cursor */
6793 pchar_cursor(save_char);
6794
Bram Moolenaar0026d472014-09-09 16:32:39 +02006795#ifdef FEAT_LINEBREAK
6796 curwin->w_p_lbr = has_lbr;
6797#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006798 if (!format_only && haveto_redraw)
6799 {
6800 update_topline();
6801 redraw_curbuf_later(VALID);
6802 }
6803}
6804
6805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 * Called after inserting or deleting text: When 'formatoptions' includes the
6807 * 'a' flag format from the current line until the end of the paragraph.
6808 * Keep the cursor at the same position relative to the text.
6809 * The caller must have saved the cursor line for undo, following ones will be
6810 * saved here.
6811 */
6812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006813auto_format(
6814 int trailblank, /* when TRUE also format with trailing blank */
6815 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
6817 pos_T pos;
6818 colnr_T len;
6819 char_u *old;
6820 char_u *new, *pnew;
6821 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006822 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
6824 if (!has_format_option(FO_AUTO))
6825 return;
6826
6827 pos = curwin->w_cursor;
6828 old = ml_get_curline();
6829
6830 /* may remove added space */
6831 check_auto_format(FALSE);
6832
6833 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6834 * user might insert normal text next. Also skip formatting when "1" is
6835 * in 'formatoptions' and there is a single character before the cursor.
6836 * Otherwise the line would be broken and when typing another non-white
6837 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006838 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 if (*old != NUL && !trailblank && wasatend)
6840 {
6841 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006842 cc = gchar_cursor();
6843 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6844 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006846 cc = gchar_cursor();
6847 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
6849 curwin->w_cursor = pos;
6850 return;
6851 }
6852 curwin->w_cursor = pos;
6853 }
6854
6855#ifdef FEAT_COMMENTS
6856 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6857 * comments. */
6858 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006859 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 return;
6861#endif
6862
6863 /*
6864 * May start formatting in a previous line, so that after "x" a word is
6865 * moved to the previous line if it fits there now. Only when this is not
6866 * the start of a paragraph.
6867 */
6868 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6869 {
6870 --curwin->w_cursor.lnum;
6871 if (u_save_cursor() == FAIL)
6872 return;
6873 }
6874
6875 /*
6876 * Do the formatting and restore the cursor position. "saved_cursor" will
6877 * be adjusted for the text formatting.
6878 */
6879 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006880 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 curwin->w_cursor = saved_cursor;
6882 saved_cursor.lnum = 0;
6883
6884 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6885 {
6886 /* "cannot happen" */
6887 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6888 coladvance((colnr_T)MAXCOL);
6889 }
6890 else
6891 check_cursor_col();
6892
6893 /* Insert mode: If the cursor is now after the end of the line while it
6894 * previously wasn't, the line was broken. Because of the rule above we
6895 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6896 * formatted. */
6897 if (!wasatend && has_format_option(FO_WHITE_PAR))
6898 {
6899 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006900 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (curwin->w_cursor.col == len)
6902 {
6903 pnew = vim_strnsave(new, len + 2);
6904 pnew[len] = ' ';
6905 pnew[len + 1] = NUL;
6906 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6907 /* remove the space later */
6908 did_add_space = TRUE;
6909 }
6910 else
6911 /* may remove added space */
6912 check_auto_format(FALSE);
6913 }
6914
6915 check_cursor();
6916}
6917
6918/*
6919 * When an extra space was added to continue a paragraph for auto-formatting,
6920 * delete it now. The space must be under the cursor, just after the insert
6921 * position.
6922 */
6923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006924check_auto_format(
6925 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926{
6927 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006928 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
6930 if (did_add_space)
6931 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006932 cc = gchar_cursor();
6933 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 /* Somehow the space was removed already. */
6935 did_add_space = FALSE;
6936 else
6937 {
6938 if (!end_insert)
6939 {
6940 inc_cursor();
6941 c = gchar_cursor();
6942 dec_cursor();
6943 }
6944 if (c != NUL)
6945 {
6946 /* The space is no longer at the end of the line, delete it. */
6947 del_char(FALSE);
6948 did_add_space = FALSE;
6949 }
6950 }
6951 }
6952}
6953
6954/*
6955 * Find out textwidth to be used for formatting:
6956 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006957 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 * if invalid value, use 0.
6959 * Set default to window width (maximum 79) for "gq" operator.
6960 */
6961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006962comp_textwidth(
6963 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 int textwidth;
6966
6967 textwidth = curbuf->b_p_tw;
6968 if (textwidth == 0 && curbuf->b_p_wm)
6969 {
6970 /* The width is the window width minus 'wrapmargin' minus all the
6971 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006972 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973#ifdef FEAT_CMDWIN
6974 if (cmdwin_type != 0)
6975 textwidth -= 1;
6976#endif
6977#ifdef FEAT_FOLDING
6978 textwidth -= curwin->w_p_fdc;
6979#endif
6980#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006981 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 textwidth -= 1;
6983#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006984 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 textwidth -= 8;
6986 }
6987 if (textwidth < 0)
6988 textwidth = 0;
6989 if (ff && textwidth == 0)
6990 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006991 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (textwidth > 79)
6993 textwidth = 79;
6994 }
6995 return textwidth;
6996}
6997
6998/*
6999 * Put a character in the redo buffer, for when just after a CTRL-V.
7000 */
7001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007002redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003{
7004 char_u buf[10];
7005
7006 /* Only digits need special treatment. Translate them into a string of
7007 * three digits. */
7008 if (VIM_ISDIGIT(c))
7009 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007010 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 AppendToRedobuff(buf);
7012 }
7013 else
7014 AppendCharToRedobuff(c);
7015}
7016
7017/*
7018 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007019 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 */
7021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007022start_arrow(
7023 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007025 start_arrow_common(end_insert_pos, TRUE);
7026}
7027
7028/*
7029 * Like start_arrow() but with end_change argument.
7030 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7031 */
7032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007033start_arrow_with_change(
7034 pos_T *end_insert_pos, /* can be NULL */
7035 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007036{
7037 start_arrow_common(end_insert_pos, end_change);
7038 if (!end_change)
7039 {
7040 AppendCharToRedobuff(Ctrl_G);
7041 AppendCharToRedobuff('U');
7042 }
7043}
7044
7045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007046start_arrow_common(
7047 pos_T *end_insert_pos, /* can be NULL */
7048 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007049{
7050 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
7052 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007053 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 arrow_used = TRUE; /* this means we stopped the current insert */
7055 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007056#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007057 check_spell_redraw();
7058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059}
7060
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007061#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007062/*
7063 * If we skipped highlighting word at cursor, do it now.
7064 * It may be skipped again, thus reset spell_redraw_lnum first.
7065 */
7066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007067check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007068{
7069 if (spell_redraw_lnum != 0)
7070 {
7071 linenr_T lnum = spell_redraw_lnum;
7072
7073 spell_redraw_lnum = 0;
7074 redrawWinline(lnum, FALSE);
7075 }
7076}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007077
7078/*
7079 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7080 * spelled word, if there is one.
7081 */
7082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007083spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007084{
7085 pos_T tpos = curwin->w_cursor;
7086
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007087 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007088 if (curwin->w_cursor.col != tpos.col)
7089 start_arrow(&tpos);
7090}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007091#endif
7092
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093/*
7094 * stop_arrow() is called before a change is made in insert mode.
7095 * If an arrow key has been used, start a new insertion.
7096 * Returns FAIL if undo is impossible, shouldn't insert then.
7097 */
7098 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007099stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 if (arrow_used)
7102 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007103 Insstart = curwin->w_cursor; /* new insertion starts here */
7104 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7105 /* Don't update the original insert position when moved to the
7106 * right, except when nothing was inserted yet. */
7107 update_Insstart_orig = FALSE;
7108 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7109
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 if (u_save_cursor() == OK)
7111 {
7112 arrow_used = FALSE;
7113 ins_need_undo = FALSE;
7114 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007115
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 ai_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 if (State & VREPLACE_FLAG)
7118 {
7119 orig_line_count = curbuf->b_ml.ml_line_count;
7120 vr_lines_changed = 1;
7121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 ResetRedobuff();
7123 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007124 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 }
7126 else if (ins_need_undo)
7127 {
7128 if (u_save_cursor() == OK)
7129 ins_need_undo = FALSE;
7130 }
7131
7132#ifdef FEAT_FOLDING
7133 /* Always open fold at the cursor line when inserting something. */
7134 foldOpenCursor();
7135#endif
7136
7137 return (arrow_used || ins_need_undo ? FAIL : OK);
7138}
7139
7140/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007141 * Do a few things to stop inserting.
7142 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7143 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 */
7145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007146stop_insert(
7147 pos_T *end_insert_pos,
7148 int esc, /* called by ins_esc() */
7149 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007151 int cc;
7152 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154 stop_redo_ins();
7155 replace_flush(); /* abandon replace stack */
7156
7157 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007158 * Save the inserted text for later redo with ^@ and CTRL-A.
7159 * Don't do it when "restart_edit" was set and nothing was inserted,
7160 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007162 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007163 if (did_restart_edit == 0 || (ptr != NULL
7164 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007165 {
7166 vim_free(last_insert);
7167 last_insert = ptr;
7168 last_insert_skip = new_insert_skip;
7169 }
7170 else
7171 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007173 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {
7175 /* Auto-format now. It may seem strange to do this when stopping an
7176 * insertion (or moving the cursor), but it's required when appending
7177 * a line and having it end in a space. But only do it when something
7178 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007179 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007181 pos_T tpos = curwin->w_cursor;
7182
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 /* When the cursor is at the end of the line after a space the
7184 * formatting will move it to the following word. Avoid that by
7185 * moving the cursor onto the space. */
7186 cc = 'x';
7187 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7188 {
7189 dec_cursor();
7190 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007191 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007192 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
7194
7195 auto_format(TRUE, FALSE);
7196
Bram Moolenaar1c465442017-03-12 20:10:05 +01007197 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007198 {
7199 if (gchar_cursor() != NUL)
7200 inc_cursor();
7201#ifdef FEAT_VIRTUALEDIT
7202 /* If the cursor is still at the same character, also keep
7203 * the "coladd". */
7204 if (gchar_cursor() == NUL
7205 && curwin->w_cursor.lnum == tpos.lnum
7206 && curwin->w_cursor.col == tpos.col)
7207 curwin->w_cursor.coladd = tpos.coladd;
7208#endif
7209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
7211
7212 /* If a space was inserted for auto-formatting, remove it now. */
7213 check_auto_format(TRUE);
7214
7215 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007216 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007217 * Do this when ESC was used or moving the cursor up/down.
7218 * Check for the old position still being valid, just in case the text
7219 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007220 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007221 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7222 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007224 pos_T tpos = curwin->w_cursor;
7225
7226 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007227 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007228 for (;;)
7229 {
7230 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7231 --curwin->w_cursor.col;
7232 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007233 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007234 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007235 if (del_char(TRUE) == FAIL)
7236 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007237 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007238 if (curwin->w_cursor.lnum != tpos.lnum)
7239 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007240 else
7241 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007242 /* reset tpos, could have been invalidated in the loop above */
7243 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007244 tpos.col++;
7245 if (cc != NUL && gchar_pos(&tpos) == NUL)
7246 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 /* <C-S-Right> may have started Visual mode, adjust the position for
7250 * deleted characters. */
7251 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7252 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007253 int len = (int)STRLEN(ml_get_curline());
7254
7255 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007257 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007258#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
7262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 }
7264 }
7265 did_ai = FALSE;
7266#ifdef FEAT_SMARTINDENT
7267 did_si = FALSE;
7268 can_si = FALSE;
7269 can_si_back = FALSE;
7270#endif
7271
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007272 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7273 * now in a different buffer. */
7274 if (end_insert_pos != NULL)
7275 {
7276 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007277 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007278 curbuf->b_op_end = *end_insert_pos;
7279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280}
7281
7282/*
7283 * Set the last inserted text to a single character.
7284 * Used for the replace command.
7285 */
7286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007287set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288{
7289 char_u *s;
7290
7291 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (last_insert != NULL)
7294 {
7295 s = last_insert;
7296 /* Use the CTRL-V only when entering a special char */
7297 if (c < ' ' || c == DEL)
7298 *s++ = Ctrl_V;
7299 s = add_char2buf(c, s);
7300 *s++ = ESC;
7301 *s++ = NUL;
7302 last_insert_skip = 0;
7303 }
7304}
7305
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007306#if defined(EXITFREE) || defined(PROTO)
7307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007308free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007309{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007310 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007311# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007312 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007313# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007314}
7315#endif
7316
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317/*
7318 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7319 * and CSI. Handle multi-byte characters.
7320 * Returns a pointer to after the added bytes.
7321 */
7322 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007323add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007326 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 int i;
7328 int len;
7329
7330 len = (*mb_char2bytes)(c, temp);
7331 for (i = 0; i < len; ++i)
7332 {
7333 c = temp[i];
7334#endif
7335 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7336 if (c == K_SPECIAL)
7337 {
7338 *s++ = K_SPECIAL;
7339 *s++ = KS_SPECIAL;
7340 *s++ = KE_FILLER;
7341 }
7342#ifdef FEAT_GUI
7343 else if (c == CSI)
7344 {
7345 *s++ = CSI;
7346 *s++ = KS_EXTRA;
7347 *s++ = (int)KE_CSI;
7348 }
7349#endif
7350 else
7351 *s++ = c;
7352#ifdef FEAT_MBYTE
7353 }
7354#endif
7355 return s;
7356}
7357
7358/*
7359 * move cursor to start of line
7360 * if flags & BL_WHITE move to first non-white
7361 * if flags & BL_SOL move to first non-white if startofline is set,
7362 * otherwise keep "curswant" column
7363 * if flags & BL_FIX don't leave the cursor on a NUL.
7364 */
7365 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007366beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367{
7368 if ((flags & BL_SOL) && !p_sol)
7369 coladvance(curwin->w_curswant);
7370 else
7371 {
7372 curwin->w_cursor.col = 0;
7373#ifdef FEAT_VIRTUALEDIT
7374 curwin->w_cursor.coladd = 0;
7375#endif
7376
7377 if (flags & (BL_WHITE | BL_SOL))
7378 {
7379 char_u *ptr;
7380
Bram Moolenaar1c465442017-03-12 20:10:05 +01007381 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7383 ++curwin->w_cursor.col;
7384 }
7385 curwin->w_set_curswant = TRUE;
7386 }
7387}
7388
7389/*
7390 * oneright oneleft cursor_down cursor_up
7391 *
7392 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007393 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 * Return OK when successful, FAIL when we hit a line of file boundary.
7395 */
7396
7397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007398oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399{
7400 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
7403#ifdef FEAT_VIRTUALEDIT
7404 if (virtual_active())
7405 {
7406 pos_T prevpos = curwin->w_cursor;
7407
7408 /* Adjust for multi-wide char (excluding TAB) */
7409 ptr = ml_get_cursor();
7410 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007411# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007413# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 ))
7417 ? ptr2cells(ptr) : 1));
7418 curwin->w_set_curswant = TRUE;
7419 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7420 return (prevpos.col != curwin->w_cursor.col
7421 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7422 }
7423#endif
7424
7425 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007426 if (*ptr == NUL)
7427 return FAIL; /* already at the very end */
7428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007430 if (has_mbyte)
7431 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 else
7433#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007434 l = 1;
7435
7436 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7437 * contains "onemore". */
7438 if (ptr[l] == NUL
7439#ifdef FEAT_VIRTUALEDIT
7440 && (ve_flags & VE_ONEMORE) == 0
7441#endif
7442 )
7443 return FAIL;
7444 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445
7446 curwin->w_set_curswant = TRUE;
7447 return OK;
7448}
7449
7450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007451oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453#ifdef FEAT_VIRTUALEDIT
7454 if (virtual_active())
7455 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007456# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 int v = getviscol();
7460
7461 if (v == 0)
7462 return FAIL;
7463
7464# ifdef FEAT_LINEBREAK
7465 /* We might get stuck on 'showbreak', skip over it. */
7466 width = 1;
7467 for (;;)
7468 {
7469 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007470 /* getviscol() is slow, skip it when 'showbreak' is empty,
7471 * 'breakindent' is not set and there are no multi-byte
7472 * characters */
7473 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474# ifdef FEAT_MBYTE
7475 && !has_mbyte
7476# endif
7477 ) || getviscol() < v)
7478 break;
7479 ++width;
7480 }
7481# else
7482 coladvance(v - 1);
7483# endif
7484
7485 if (curwin->w_cursor.coladd == 1)
7486 {
7487 char_u *ptr;
7488
7489 /* Adjust for multi-wide char (not a TAB) */
7490 ptr = ml_get_cursor();
7491 if (*ptr != TAB && vim_isprintc(
7492# ifdef FEAT_MBYTE
7493 (*mb_ptr2char)(ptr)
7494# else
7495 *ptr
7496# endif
7497 ) && ptr2cells(ptr) > 1)
7498 curwin->w_cursor.coladd = 0;
7499 }
7500
7501 curwin->w_set_curswant = TRUE;
7502 return OK;
7503 }
7504#endif
7505
7506 if (curwin->w_cursor.col == 0)
7507 return FAIL;
7508
7509 curwin->w_set_curswant = TRUE;
7510 --curwin->w_cursor.col;
7511
7512#ifdef FEAT_MBYTE
7513 /* if the character on the left of the current cursor is a multi-byte
7514 * character, move to its first byte */
7515 if (has_mbyte)
7516 mb_adjust_cursor();
7517#endif
7518 return OK;
7519}
7520
7521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007522cursor_up(
7523 long n,
7524 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525{
7526 linenr_T lnum;
7527
7528 if (n > 0)
7529 {
7530 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007531 /* This fails if the cursor is already in the first line or the count
7532 * is larger than the line number and '-' is in 'cpoptions' */
7533 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 return FAIL;
7535 if (n >= lnum)
7536 lnum = 1;
7537 else
7538#ifdef FEAT_FOLDING
7539 if (hasAnyFolding(curwin))
7540 {
7541 /*
7542 * Count each sequence of folded lines as one logical line.
7543 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007544 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 (void)hasFolding(lnum, &lnum, NULL);
7546
7547 while (n--)
7548 {
7549 /* move up one line */
7550 --lnum;
7551 if (lnum <= 1)
7552 break;
7553 /* If we entered a fold, move to the beginning, unless in
7554 * Insert mode or when 'foldopen' contains "all": it will open
7555 * in a moment. */
7556 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7557 (void)hasFolding(lnum, &lnum, NULL);
7558 }
7559 if (lnum < 1)
7560 lnum = 1;
7561 }
7562 else
7563#endif
7564 lnum -= n;
7565 curwin->w_cursor.lnum = lnum;
7566 }
7567
7568 /* try to advance to the column we want to be at */
7569 coladvance(curwin->w_curswant);
7570
7571 if (upd_topline)
7572 update_topline(); /* make sure curwin->w_topline is valid */
7573
7574 return OK;
7575}
7576
7577/*
7578 * Cursor down a number of logical lines.
7579 */
7580 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007581cursor_down(
7582 long n,
7583 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584{
7585 linenr_T lnum;
7586
7587 if (n > 0)
7588 {
7589 lnum = curwin->w_cursor.lnum;
7590#ifdef FEAT_FOLDING
7591 /* Move to last line of fold, will fail if it's the end-of-file. */
7592 (void)hasFolding(lnum, NULL, &lnum);
7593#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007594 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007595 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007596 if (lnum >= curbuf->b_ml.ml_line_count
7597 || (lnum + n > curbuf->b_ml.ml_line_count
7598 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 return FAIL;
7600 if (lnum + n >= curbuf->b_ml.ml_line_count)
7601 lnum = curbuf->b_ml.ml_line_count;
7602 else
7603#ifdef FEAT_FOLDING
7604 if (hasAnyFolding(curwin))
7605 {
7606 linenr_T last;
7607
7608 /* count each sequence of folded lines as one logical line */
7609 while (n--)
7610 {
7611 if (hasFolding(lnum, NULL, &last))
7612 lnum = last + 1;
7613 else
7614 ++lnum;
7615 if (lnum >= curbuf->b_ml.ml_line_count)
7616 break;
7617 }
7618 if (lnum > curbuf->b_ml.ml_line_count)
7619 lnum = curbuf->b_ml.ml_line_count;
7620 }
7621 else
7622#endif
7623 lnum += n;
7624 curwin->w_cursor.lnum = lnum;
7625 }
7626
7627 /* try to advance to the column we want to be at */
7628 coladvance(curwin->w_curswant);
7629
7630 if (upd_topline)
7631 update_topline(); /* make sure curwin->w_topline is valid */
7632
7633 return OK;
7634}
7635
7636/*
7637 * Stuff the last inserted text in the read buffer.
7638 * Last_insert actually is a copy of the redo buffer, so we
7639 * first have to remove the command.
7640 */
7641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007642stuff_inserted(
7643 int c, /* Command character to be inserted */
7644 long count, /* Repeat this many times */
7645 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
7647 char_u *esc_ptr;
7648 char_u *ptr;
7649 char_u *last_ptr;
7650 char_u last = NUL;
7651
7652 ptr = get_last_insert();
7653 if (ptr == NULL)
7654 {
7655 EMSG(_(e_noinstext));
7656 return FAIL;
7657 }
7658
7659 /* may want to stuff the command character, to start Insert mode */
7660 if (c != NUL)
7661 stuffcharReadbuff(c);
7662 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7663 *esc_ptr = NUL; /* remove the ESC */
7664
7665 /* when the last char is either "0" or "^" it will be quoted if no ESC
7666 * comes after it OR if it will inserted more than once and "ptr"
7667 * starts with ^D. -- Acevedo
7668 */
7669 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7670 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7671 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7672 {
7673 last = *last_ptr;
7674 *last_ptr = NUL;
7675 }
7676
7677 do
7678 {
7679 stuffReadbuff(ptr);
7680 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7681 if (last)
7682 stuffReadbuff((char_u *)(last == '0'
7683 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7684 : IF_EB("\026^", CTRL_V_STR "^")));
7685 }
7686 while (--count > 0);
7687
7688 if (last)
7689 *last_ptr = last;
7690
7691 if (esc_ptr != NULL)
7692 *esc_ptr = ESC; /* put the ESC back */
7693
7694 /* may want to stuff a trailing ESC, to get out of Insert mode */
7695 if (!no_esc)
7696 stuffcharReadbuff(ESC);
7697
7698 return OK;
7699}
7700
7701 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007702get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
7704 if (last_insert == NULL)
7705 return NULL;
7706 return last_insert + last_insert_skip;
7707}
7708
7709/*
7710 * Get last inserted string, and remove trailing <Esc>.
7711 * Returns pointer to allocated memory (must be freed) or NULL.
7712 */
7713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007714get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
7716 char_u *s;
7717 int len;
7718
7719 if (last_insert == NULL)
7720 return NULL;
7721 s = vim_strsave(last_insert + last_insert_skip);
7722 if (s != NULL)
7723 {
7724 len = (int)STRLEN(s);
7725 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7726 s[len - 1] = NUL;
7727 }
7728 return s;
7729}
7730
7731/*
7732 * Check the word in front of the cursor for an abbreviation.
7733 * Called when the non-id character "c" has been entered.
7734 * When an abbreviation is recognized it is removed from the text and
7735 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7736 */
7737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007738echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 /* Don't check for abbreviation in paste mode, when disabled and just
7741 * after moving around with cursor keys. */
7742 if (p_paste || no_abbr || arrow_used)
7743 return FALSE;
7744
7745 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7746 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7747}
7748
7749/*
7750 * replace-stack functions
7751 *
7752 * When replacing characters, the replaced characters are remembered for each
7753 * new character. This is used to re-insert the old text when backspacing.
7754 *
7755 * There is a NUL headed list of characters for each character that is
7756 * currently in the file after the insertion point. When BS is used, one NUL
7757 * headed list is put back for the deleted character.
7758 *
7759 * For a newline, there are two NUL headed lists. One contains the characters
7760 * that the NL replaced. The extra one stores the characters after the cursor
7761 * that were deleted (always white space).
7762 *
7763 * Replace_offset is normally 0, in which case replace_push will add a new
7764 * character at the end of the stack. If replace_offset is not 0, that many
7765 * characters will be left on the stack above the newly inserted character.
7766 */
7767
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007768static char_u *replace_stack = NULL;
7769static long replace_stack_nr = 0; /* next entry in replace stack */
7770static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771
7772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007773replace_push(
7774 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 char_u *p;
7777
7778 if (replace_stack_nr < replace_offset) /* nothing to do */
7779 return;
7780 if (replace_stack_len <= replace_stack_nr)
7781 {
7782 replace_stack_len += 50;
7783 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7784 if (p == NULL) /* out of memory */
7785 {
7786 replace_stack_len -= 50;
7787 return;
7788 }
7789 if (replace_stack != NULL)
7790 {
7791 mch_memmove(p, replace_stack,
7792 (size_t)(replace_stack_nr * sizeof(char_u)));
7793 vim_free(replace_stack);
7794 }
7795 replace_stack = p;
7796 }
7797 p = replace_stack + replace_stack_nr - replace_offset;
7798 if (replace_offset)
7799 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7800 *p = c;
7801 ++replace_stack_nr;
7802}
7803
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007804#if defined(FEAT_MBYTE) || defined(PROTO)
7805/*
7806 * Push a character onto the replace stack. Handles a multi-byte character in
7807 * reverse byte order, so that the first byte is popped off first.
7808 * Return the number of bytes done (includes composing characters).
7809 */
7810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007811replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007812{
7813 int l = (*mb_ptr2len)(p);
7814 int j;
7815
7816 for (j = l - 1; j >= 0; --j)
7817 replace_push(p[j]);
7818 return l;
7819}
7820#endif
7821
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822/*
7823 * Pop one item from the replace stack.
7824 * return -1 if stack empty
7825 * return replaced character or NUL otherwise
7826 */
7827 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
7830 if (replace_stack_nr == 0)
7831 return -1;
7832 return (int)replace_stack[--replace_stack_nr];
7833}
7834
7835/*
7836 * Join the top two items on the replace stack. This removes to "off"'th NUL
7837 * encountered.
7838 */
7839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007840replace_join(
7841 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 int i;
7844
7845 for (i = replace_stack_nr; --i >= 0; )
7846 if (replace_stack[i] == NUL && off-- <= 0)
7847 {
7848 --replace_stack_nr;
7849 mch_memmove(replace_stack + i, replace_stack + i + 1,
7850 (size_t)(replace_stack_nr - i));
7851 return;
7852 }
7853}
7854
7855/*
7856 * Pop bytes from the replace stack until a NUL is found, and insert them
7857 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7858 */
7859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007860replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862 int cc;
7863 int oldState = State;
7864
7865 State = NORMAL; /* don't want REPLACE here */
7866 while ((cc = replace_pop()) > 0)
7867 {
7868#ifdef FEAT_MBYTE
7869 mb_replace_pop_ins(cc);
7870#else
7871 ins_char(cc);
7872#endif
7873 dec_cursor();
7874 }
7875 State = oldState;
7876}
7877
7878#ifdef FEAT_MBYTE
7879/*
7880 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7881 * indicates a multi-byte char, pop the other bytes too.
7882 */
7883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007884mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
7886 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007887 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 int i;
7889 int c;
7890
7891 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7892 {
7893 buf[0] = cc;
7894 for (i = 1; i < n; ++i)
7895 buf[i] = replace_pop();
7896 ins_bytes_len(buf, n);
7897 }
7898 else
7899 ins_char(cc);
7900
7901 if (enc_utf8)
7902 /* Handle composing chars. */
7903 for (;;)
7904 {
7905 c = replace_pop();
7906 if (c == -1) /* stack empty */
7907 break;
7908 if ((n = MB_BYTE2LEN(c)) == 1)
7909 {
7910 /* Not a multi-byte char, put it back. */
7911 replace_push(c);
7912 break;
7913 }
7914 else
7915 {
7916 buf[0] = c;
7917 for (i = 1; i < n; ++i)
7918 buf[i] = replace_pop();
7919 if (utf_iscomposing(utf_ptr2char(buf)))
7920 ins_bytes_len(buf, n);
7921 else
7922 {
7923 /* Not a composing char, put it back. */
7924 for (i = n - 1; i >= 0; --i)
7925 replace_push(buf[i]);
7926 break;
7927 }
7928 }
7929 }
7930}
7931#endif
7932
7933/*
7934 * make the replace stack empty
7935 * (called when exiting replace mode)
7936 */
7937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007938replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007940 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 replace_stack_len = 0;
7942 replace_stack_nr = 0;
7943}
7944
7945/*
7946 * Handle doing a BS for one character.
7947 * cc < 0: replace stack empty, just move cursor
7948 * cc == 0: character was inserted, delete it
7949 * cc > 0: character was replaced, put cc (first byte of original char) back
7950 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007951 * When "limit_col" is >= 0, don't delete before this column. Matters when
7952 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 */
7954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007955replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956{
7957 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 int orig_len = 0;
7959 int ins_len;
7960 int orig_vcols = 0;
7961 colnr_T start_vcol;
7962 char_u *p;
7963 int i;
7964 int vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965
7966 cc = replace_pop();
7967 if (cc > 0)
7968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (State & VREPLACE_FLAG)
7970 {
7971 /* Get the number of screen cells used by the character we are
7972 * going to delete. */
7973 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7974 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976#ifdef FEAT_MBYTE
7977 if (has_mbyte)
7978 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007979 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007981 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 replace_push(cc);
7983 }
7984 else
7985#endif
7986 {
7987 pchar_cursor(cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007989 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 }
7991 replace_pop_ins();
7992
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (State & VREPLACE_FLAG)
7994 {
7995 /* Get the number of screen cells used by the inserted characters */
7996 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007997 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 vcol = start_vcol;
7999 for (i = 0; i < ins_len; ++i)
8000 {
8001 vcol += chartabsize(p + i, vcol);
8002#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008003 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004#endif
8005 }
8006 vcol -= start_vcol;
8007
8008 /* Delete spaces that were inserted after the cursor to keep the
8009 * text aligned. */
8010 curwin->w_cursor.col += ins_len;
8011 while (vcol > orig_vcols && gchar_cursor() == ' ')
8012 {
8013 del_char(FALSE);
8014 ++orig_vcols;
8015 }
8016 curwin->w_cursor.col -= ins_len;
8017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
8019 /* mark the buffer as changed and prepare for displaying */
8020 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8021 }
8022 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008023 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024}
8025
8026#ifdef FEAT_CINDENT
8027/*
8028 * Return TRUE if C-indenting is on.
8029 */
8030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008031cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032{
8033 return (!p_paste && (curbuf->b_p_cin
8034# ifdef FEAT_EVAL
8035 || *curbuf->b_p_inde != NUL
8036# endif
8037 ));
8038}
8039#endif
8040
8041#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8042/*
8043 * Re-indent the current line, based on the current contents of it and the
8044 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8045 * confused what all the part that handles Control-T is doing that I'm not.
8046 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8047 */
8048
8049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008050fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008052 int amount = get_the_indent();
8053
8054 if (amount >= 0)
8055 {
8056 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8057 if (linewhite(curwin->w_cursor.lnum))
8058 did_ai = TRUE; /* delete the indent if the line stays empty */
8059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
8062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008063fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
8065 if (p_paste)
8066 return;
8067# ifdef FEAT_LISP
8068 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8069 fixthisline(get_lisp_indent);
8070# endif
8071# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8072 else
8073# endif
8074# ifdef FEAT_CINDENT
8075 if (cindent_on())
8076 do_c_expr_indent();
8077# endif
8078}
8079
8080#endif
8081
8082#ifdef FEAT_CINDENT
8083/*
8084 * return TRUE if 'cinkeys' contains the key "keytyped",
8085 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008086 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8088 *
8089 * "keytyped" can have a few special values:
8090 * KEY_OPEN_FORW
8091 * KEY_OPEN_BACK
8092 * KEY_COMPLETE just finished completion.
8093 *
8094 * If line_is_empty is TRUE accept keys with '0' before them.
8095 */
8096 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008097in_cinkeys(
8098 int keytyped,
8099 int when,
8100 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101{
8102 char_u *look;
8103 int try_match;
8104 int try_match_word;
8105 char_u *p;
8106 char_u *line;
8107 int icase;
8108 int i;
8109
Bram Moolenaar30848942009-12-24 14:46:12 +00008110 if (keytyped == NUL)
8111 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8112 return FALSE;
8113
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114#ifdef FEAT_EVAL
8115 if (*curbuf->b_p_inde != NUL)
8116 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8117 else
8118#endif
8119 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8120 while (*look)
8121 {
8122 /*
8123 * Find out if we want to try a match with this key, depending on
8124 * 'when' and a '*' or '!' before the key.
8125 */
8126 switch (when)
8127 {
8128 case '*': try_match = (*look == '*'); break;
8129 case '!': try_match = (*look == '!'); break;
8130 default: try_match = (*look != '*'); break;
8131 }
8132 if (*look == '*' || *look == '!')
8133 ++look;
8134
8135 /*
8136 * If there is a '0', only accept a match if the line is empty.
8137 * But may still match when typing last char of a word.
8138 */
8139 if (*look == '0')
8140 {
8141 try_match_word = try_match;
8142 if (!line_is_empty)
8143 try_match = FALSE;
8144 ++look;
8145 }
8146 else
8147 try_match_word = FALSE;
8148
8149 /*
8150 * does it look like a control character?
8151 */
8152 if (*look == '^'
8153#ifdef EBCDIC
8154 && (Ctrl_chr(look[1]) != 0)
8155#else
8156 && look[1] >= '?' && look[1] <= '_'
8157#endif
8158 )
8159 {
8160 if (try_match && keytyped == Ctrl_chr(look[1]))
8161 return TRUE;
8162 look += 2;
8163 }
8164 /*
8165 * 'o' means "o" command, open forward.
8166 * 'O' means "O" command, open backward.
8167 */
8168 else if (*look == 'o')
8169 {
8170 if (try_match && keytyped == KEY_OPEN_FORW)
8171 return TRUE;
8172 ++look;
8173 }
8174 else if (*look == 'O')
8175 {
8176 if (try_match && keytyped == KEY_OPEN_BACK)
8177 return TRUE;
8178 ++look;
8179 }
8180
8181 /*
8182 * 'e' means to check for "else" at start of line and just before the
8183 * cursor.
8184 */
8185 else if (*look == 'e')
8186 {
8187 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8188 {
8189 p = ml_get_curline();
8190 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8191 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8192 return TRUE;
8193 }
8194 ++look;
8195 }
8196
8197 /*
8198 * ':' only causes an indent if it is at the end of a label or case
8199 * statement, or when it was before typing the ':' (to fix
8200 * class::method for C++).
8201 */
8202 else if (*look == ':')
8203 {
8204 if (try_match && keytyped == ':')
8205 {
8206 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008207 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008209 /* Need to get the line again after cin_islabel(). */
8210 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 if (curwin->w_cursor.col > 2
8212 && p[curwin->w_cursor.col - 1] == ':'
8213 && p[curwin->w_cursor.col - 2] == ':')
8214 {
8215 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008216 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008217 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 p = ml_get_curline();
8219 p[curwin->w_cursor.col - 1] = ':';
8220 if (i)
8221 return TRUE;
8222 }
8223 }
8224 ++look;
8225 }
8226
8227
8228 /*
8229 * Is it a key in <>, maybe?
8230 */
8231 else if (*look == '<')
8232 {
8233 if (try_match)
8234 {
8235 /*
8236 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8237 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8238 * >, *, : and ! keys if they really really want to.
8239 */
8240 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8241 && keytyped == look[1])
8242 return TRUE;
8243
8244 if (keytyped == get_special_key_code(look + 1))
8245 return TRUE;
8246 }
8247 while (*look && *look != '>')
8248 look++;
8249 while (*look == '>')
8250 look++;
8251 }
8252
8253 /*
8254 * Is it a word: "=word"?
8255 */
8256 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8257 {
8258 ++look;
8259 if (*look == '~')
8260 {
8261 icase = TRUE;
8262 ++look;
8263 }
8264 else
8265 icase = FALSE;
8266 p = vim_strchr(look, ',');
8267 if (p == NULL)
8268 p = look + STRLEN(look);
8269 if ((try_match || try_match_word)
8270 && curwin->w_cursor.col >= (colnr_T)(p - look))
8271 {
8272 int match = FALSE;
8273
8274#ifdef FEAT_INS_EXPAND
8275 if (keytyped == KEY_COMPLETE)
8276 {
8277 char_u *s;
8278
8279 /* Just completed a word, check if it starts with "look".
8280 * search back for the start of a word. */
8281 line = ml_get_curline();
8282# ifdef FEAT_MBYTE
8283 if (has_mbyte)
8284 {
8285 char_u *n;
8286
8287 for (s = line + curwin->w_cursor.col; s > line; s = n)
8288 {
8289 n = mb_prevptr(line, s);
8290 if (!vim_iswordp(n))
8291 break;
8292 }
8293 }
8294 else
8295# endif
8296 for (s = line + curwin->w_cursor.col; s > line; --s)
8297 if (!vim_iswordc(s[-1]))
8298 break;
8299 if (s + (p - look) <= line + curwin->w_cursor.col
8300 && (icase
8301 ? MB_STRNICMP(s, look, p - look)
8302 : STRNCMP(s, look, p - look)) == 0)
8303 match = TRUE;
8304 }
8305 else
8306#endif
8307 /* TODO: multi-byte */
8308 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8309 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8310 {
8311 line = ml_get_cursor();
8312 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8313 || !vim_iswordc(line[-(p - look) - 1]))
8314 && (icase
8315 ? MB_STRNICMP(line - (p - look), look, p - look)
8316 : STRNCMP(line - (p - look), look, p - look))
8317 == 0)
8318 match = TRUE;
8319 }
8320 if (match && try_match_word && !try_match)
8321 {
8322 /* "0=word": Check if there are only blanks before the
8323 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008324 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 (int)(curwin->w_cursor.col - (p - look)))
8326 match = FALSE;
8327 }
8328 if (match)
8329 return TRUE;
8330 }
8331 look = p;
8332 }
8333
8334 /*
8335 * ok, it's a boring generic character.
8336 */
8337 else
8338 {
8339 if (try_match && *look == keytyped)
8340 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008341 if (*look != NUL)
8342 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 }
8344
8345 /*
8346 * Skip over ", ".
8347 */
8348 look = skip_to_option_part(look);
8349 }
8350 return FALSE;
8351}
8352#endif /* FEAT_CINDENT */
8353
8354#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8355/*
8356 * Map Hebrew keyboard when in hkmap mode.
8357 */
8358 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008359hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360{
8361 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8362 {
8363 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8364 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8365 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8366 static char_u map[26] =
8367 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8368 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8369 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8370 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8371 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8372 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8373 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8374 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8375 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8376
8377 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8378 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8379 /* '-1'='sofit' */
8380 else if (c == 'x')
8381 return 'X';
8382 else if (c == 'q')
8383 return '\''; /* {geresh}={'} */
8384 else if (c == 246)
8385 return ' '; /* \"o --> ' ' for a german keyboard */
8386 else if (c == 228)
8387 return ' '; /* \"a --> ' ' -- / -- */
8388 else if (c == 252)
8389 return ' '; /* \"u --> ' ' -- / -- */
8390#ifdef EBCDIC
8391 else if (islower(c))
8392#else
8393 /* NOTE: islower() does not do the right thing for us on Linux so we
8394 * do this the same was as 5.7 and previous, so it works correctly on
8395 * all systems. Specifically, the e.g. Delete and Arrow keys are
8396 * munged and won't work if e.g. searching for Hebrew text.
8397 */
8398 else if (c >= 'a' && c <= 'z')
8399#endif
8400 return (int)(map[CharOrdLow(c)] + p_aleph);
8401 else
8402 return c;
8403 }
8404 else
8405 {
8406 switch (c)
8407 {
8408 case '`': return ';';
8409 case '/': return '.';
8410 case '\'': return ',';
8411 case 'q': return '/';
8412 case 'w': return '\'';
8413
8414 /* Hebrew letters - set offset from 'a' */
8415 case ',': c = '{'; break;
8416 case '.': c = 'v'; break;
8417 case ';': c = 't'; break;
8418 default: {
8419 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8420
8421#ifdef EBCDIC
8422 /* see note about islower() above */
8423 if (!islower(c))
8424#else
8425 if (c < 'a' || c > 'z')
8426#endif
8427 return c;
8428 c = str[CharOrdLow(c)];
8429 break;
8430 }
8431 }
8432
8433 return (int)(CharOrdLow(c) + p_aleph);
8434 }
8435}
8436#endif
8437
8438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008439ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
8441 int need_redraw = FALSE;
8442 int regname;
8443 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008444 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
8446 /*
8447 * If we are going to wait for a character, show a '"'.
8448 */
8449 pc_status = PC_STATUS_UNSET;
8450 if (redrawing() && !char_avail())
8451 {
8452 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008453 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454
8455 edit_putchar('"', TRUE);
8456#ifdef FEAT_CMDL_INFO
8457 add_to_showcmd_c(Ctrl_R);
8458#endif
8459 }
8460
8461#ifdef USE_ON_FLY_SCROLL
8462 dont_scroll = TRUE; /* disallow scrolling here */
8463#endif
8464
8465 /*
8466 * Don't map the register name. This also prevents the mode message to be
8467 * deleted when ESC is hit.
8468 */
8469 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008470 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8473 {
8474 /* Get a third key for literal register insertion */
8475 literally = regname;
8476#ifdef FEAT_CMDL_INFO
8477 add_to_showcmd_c(literally);
8478#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008479 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 }
8482 --no_mapping;
8483
8484#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008485 /* Don't call u_sync() while typing the expression or giving an error
8486 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 ++no_u_sync;
8488 if (regname == '=')
8489 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008490# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008492# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008493 /* Sync undo when evaluating the expression calls setline() or
8494 * append(), so that it can be undone separately. */
8495 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008496
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008498# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 /* Restore the Input Method. */
8500 if (im_on)
8501 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008502# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008504 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8505 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008506 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 else
8510 {
8511#endif
8512 if (literally == Ctrl_O || literally == Ctrl_P)
8513 {
8514 /* Append the command to the redo buffer. */
8515 AppendCharToRedobuff(Ctrl_R);
8516 AppendCharToRedobuff(literally);
8517 AppendCharToRedobuff(regname);
8518
8519 do_put(regname, BACKWARD, 1L,
8520 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8521 }
8522 else if (insert_reg(regname, literally) == FAIL)
8523 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008524 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 need_redraw = TRUE; /* remove the '"' */
8526 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008527 else if (stop_insert_mode)
8528 /* When the '=' register was used and a function was invoked that
8529 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8530 * insert anything, need to remove the '"' */
8531 need_redraw = TRUE;
8532
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533#ifdef FEAT_EVAL
8534 }
8535 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008536 if (u_sync_once == 1)
8537 ins_need_undo = TRUE;
8538 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539#endif
8540#ifdef FEAT_CMDL_INFO
8541 clear_showcmd();
8542#endif
8543
8544 /* If the inserted register is empty, we need to remove the '"' */
8545 if (need_redraw || stuff_empty())
8546 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008547
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008548 /* Disallow starting Visual mode here, would get a weird mode. */
8549 if (!vis_active && VIsual_active)
8550 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553/*
8554 * CTRL-G commands in Insert mode.
8555 */
8556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008557ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
8559 int c;
8560
8561#ifdef FEAT_INS_EXPAND
8562 /* Right after CTRL-X the cursor will be after the ruler. */
8563 setcursor();
8564#endif
8565
8566 /*
8567 * Don't map the second key. This also prevents the mode message to be
8568 * deleted when ESC is hit.
8569 */
8570 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008571 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 --no_mapping;
8573 switch (c)
8574 {
8575 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8576 case K_UP:
8577 case Ctrl_K:
8578 case 'k': ins_up(TRUE);
8579 break;
8580
8581 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8582 case K_DOWN:
8583 case Ctrl_J:
8584 case 'j': ins_down(TRUE);
8585 break;
8586
8587 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008588 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008590
8591 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008592 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008593 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008594 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 break;
8596
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008597 /* CTRL-G U: do not break undo with the next char */
8598 case 'U':
8599 /* Allow one left/right cursor movement with the next char,
8600 * without breaking undo. */
8601 dont_sync_undo = MAYBE;
8602 break;
8603
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008605 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 }
8607}
8608
8609/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008610 * CTRL-^ in Insert mode.
8611 */
8612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008613ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008614{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008615 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008616 {
8617 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8618 if (State & LANGMAP)
8619 {
8620 curbuf->b_p_iminsert = B_IMODE_NONE;
8621 State &= ~LANGMAP;
8622 }
8623 else
8624 {
8625 curbuf->b_p_iminsert = B_IMODE_LMAP;
8626 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008627#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008628 im_set_active(FALSE);
8629#endif
8630 }
8631 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008632#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008633 else
8634 {
8635 /* There are no ":lmap" mappings, toggle IM */
8636 if (im_get_status())
8637 {
8638 curbuf->b_p_iminsert = B_IMODE_NONE;
8639 im_set_active(FALSE);
8640 }
8641 else
8642 {
8643 curbuf->b_p_iminsert = B_IMODE_IM;
8644 State &= ~LANGMAP;
8645 im_set_active(TRUE);
8646 }
8647 }
8648#endif
8649 set_iminsert_global();
8650 showmode();
8651#ifdef FEAT_GUI
8652 /* may show different cursor shape or color */
8653 if (gui.in_use)
8654 gui_update_cursor(TRUE, FALSE);
8655#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008656#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008657 /* Show/unshow value of 'keymap' in status lines. */
8658 status_redraw_curbuf();
8659#endif
8660}
8661
8662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 * Handle ESC in insert mode.
8664 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8665 * insert.
8666 */
8667 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008668ins_esc(
8669 long *count,
8670 int cmdchar,
8671 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
8673 int temp;
8674 static int disabled_redraw = FALSE;
8675
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008676#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008677 check_spell_redraw();
8678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679#if defined(FEAT_HANGULIN)
8680# if defined(ESC_CHG_TO_ENG_MODE)
8681 hangul_input_state_set(0);
8682# endif
8683 if (composing_hangul)
8684 {
8685 push_raw_key(composing_hangul_buffer, 2);
8686 composing_hangul = 0;
8687 }
8688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
8690 temp = curwin->w_cursor.col;
8691 if (disabled_redraw)
8692 {
8693 --RedrawingDisabled;
8694 disabled_redraw = FALSE;
8695 }
8696 if (!arrow_used)
8697 {
8698 /*
8699 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008700 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8701 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 */
8703 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008704 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
8706 /*
8707 * Repeating insert may take a long time. Check for
8708 * interrupt now and then.
8709 */
8710 if (*count > 0)
8711 {
8712 line_breakcheck();
8713 if (got_int)
8714 *count = 0;
8715 }
8716
8717 if (--*count > 0) /* repeat what was typed */
8718 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008719 /* Vi repeats the insert without replacing characters. */
8720 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8721 State &= ~REPLACE_FLAG;
8722
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 (void)start_redo_ins();
8724 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008725 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 ++RedrawingDisabled;
8727 disabled_redraw = TRUE;
8728 return FALSE; /* repeat the insert */
8729 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008730 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 undisplay_dollar();
8732 }
8733
8734 /* When an autoindent was removed, curswant stays after the
8735 * indent */
8736 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8737 curwin->w_set_curswant = TRUE;
8738
8739 /* Remember the last Insert position in the '^ mark. */
8740 if (!cmdmod.keepjumps)
8741 curbuf->b_last_insert = curwin->w_cursor;
8742
8743 /*
8744 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008745 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008747 if (!nomove
8748 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749#ifdef FEAT_VIRTUALEDIT
8750 || curwin->w_cursor.coladd > 0
8751#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008752 )
8753 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008754 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755#ifdef FEAT_RIGHTLEFT
8756 && !revins_on
8757#endif
8758 )
8759 {
8760#ifdef FEAT_VIRTUALEDIT
8761 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8762 {
8763 oneleft();
8764 if (restart_edit != NUL)
8765 ++curwin->w_cursor.coladd;
8766 }
8767 else
8768#endif
8769 {
8770 --curwin->w_cursor.col;
8771#ifdef FEAT_MBYTE
8772 /* Correct cursor for multi-byte character. */
8773 if (has_mbyte)
8774 mb_adjust_cursor();
8775#endif
8776 }
8777 }
8778
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008779#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 /* Disable IM to allow typing English directly for Normal mode commands.
8781 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8782 * well). */
8783 if (!(State & LANGMAP))
8784 im_save_status(&curbuf->b_p_iminsert);
8785 im_set_active(FALSE);
8786#endif
8787
8788 State = NORMAL;
8789 /* need to position cursor again (e.g. when on a TAB ) */
8790 changed_cline_bef_curs();
8791
8792#ifdef FEAT_MOUSE
8793 setmouse();
8794#endif
8795#ifdef CURSOR_SHAPE
8796 ui_cursor_shape(); /* may show different cursor shape */
8797#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008798 if (!p_ek)
8799 /* Re-enable bracketed paste mode. */
8800 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
8802 /*
8803 * When recording or for CTRL-O, need to display the new mode.
8804 * Otherwise remove the mode message.
8805 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008806 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 showmode();
8808 else if (p_smd)
8809 MSG("");
8810
8811 return TRUE; /* exit Insert mode */
8812}
8813
8814#ifdef FEAT_RIGHTLEFT
8815/*
8816 * Toggle language: hkmap and revins_on.
8817 * Move to end of reverse inserted text.
8818 */
8819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008820ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 if (revins_on && revins_chars && revins_scol >= 0)
8823 {
8824 while (gchar_cursor() != NUL && revins_chars--)
8825 ++curwin->w_cursor.col;
8826 }
8827 p_ri = !p_ri;
8828 revins_on = (State == INSERT && p_ri);
8829 if (revins_on)
8830 {
8831 revins_scol = curwin->w_cursor.col;
8832 revins_legal++;
8833 revins_chars = 0;
8834 undisplay_dollar();
8835 }
8836 else
8837 revins_scol = -1;
8838#ifdef FEAT_FKMAP
8839 if (p_altkeymap)
8840 {
8841 /*
8842 * to be consistent also for redo command, using '.'
8843 * set arrow_used to true and stop it - causing to redo
8844 * characters entered in one mode (normal/reverse insert).
8845 */
8846 arrow_used = TRUE;
8847 (void)stop_arrow();
8848 p_fkmap = curwin->w_p_rl ^ p_ri;
8849 if (p_fkmap && p_ri)
8850 State = INSERT;
8851 }
8852 else
8853#endif
8854 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8855 showmode();
8856}
8857#endif
8858
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859/*
8860 * If 'keymodel' contains "startsel", may start selection.
8861 * Returns TRUE when a CTRL-O and other keys stuffed.
8862 */
8863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008864ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 if (km_startsel)
8867 switch (c)
8868 {
8869 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 case K_PAGEUP:
8872 case K_KPAGEUP:
8873 case K_PAGEDOWN:
8874 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008875# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 case K_LEFT:
8877 case K_RIGHT:
8878 case K_UP:
8879 case K_DOWN:
8880 case K_END:
8881 case K_HOME:
8882# endif
8883 if (!(mod_mask & MOD_MASK_SHIFT))
8884 break;
8885 /* FALLTHROUGH */
8886 case K_S_LEFT:
8887 case K_S_RIGHT:
8888 case K_S_UP:
8889 case K_S_DOWN:
8890 case K_S_END:
8891 case K_S_HOME:
8892 /* Start selection right away, the cursor can move with
8893 * CTRL-O when beyond the end of the line. */
8894 start_selection();
8895
8896 /* Execute the key in (insert) Select mode. */
8897 stuffcharReadbuff(Ctrl_O);
8898 if (mod_mask)
8899 {
8900 char_u buf[4];
8901
8902 buf[0] = K_SPECIAL;
8903 buf[1] = KS_MODIFIER;
8904 buf[2] = mod_mask;
8905 buf[3] = NUL;
8906 stuffReadbuff(buf);
8907 }
8908 stuffcharReadbuff(c);
8909 return TRUE;
8910 }
8911 return FALSE;
8912}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913
8914/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008915 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008916 */
8917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008918ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008919{
8920#ifdef FEAT_FKMAP
8921 if (p_fkmap && p_ri)
8922 {
8923 beep_flush();
8924 EMSG(farsi_text_3); /* encoded in Farsi */
8925 return;
8926 }
8927#endif
8928
Bram Moolenaar1e015462005-09-25 22:16:38 +00008929# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008930 set_vim_var_string(VV_INSERTMODE,
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02008931 (char_u *)((State & REPLACE_FLAG) ? "i"
8932 : replaceState == VREPLACE ? "v"
8933 : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008934# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008935 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008936 if (State & REPLACE_FLAG)
8937 State = INSERT | (State & LANGMAP);
8938 else
8939 State = replaceState | (State & LANGMAP);
8940 AppendCharToRedobuff(K_INS);
8941 showmode();
8942#ifdef CURSOR_SHAPE
8943 ui_cursor_shape(); /* may show different cursor shape */
8944#endif
8945}
8946
8947/*
8948 * Pressed CTRL-O in Insert mode.
8949 */
8950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008951ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008952{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008953 if (State & VREPLACE_FLAG)
8954 restart_edit = 'V';
8955 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008956 if (State & REPLACE_FLAG)
8957 restart_edit = 'R';
8958 else
8959 restart_edit = 'I';
8960#ifdef FEAT_VIRTUALEDIT
8961 if (virtual_active())
8962 ins_at_eol = FALSE; /* cursor always keeps its column */
8963 else
8964#endif
8965 ins_at_eol = (gchar_cursor() == NUL);
8966}
8967
8968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 * If the cursor is on an indent, ^T/^D insert/delete one
8970 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008971 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 * with vi. But vi only supports ^T and ^D after an
8973 * autoindent, we support it everywhere.
8974 */
8975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008976ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978 if (stop_arrow() == FAIL)
8979 return;
8980 AppendCharToRedobuff(c);
8981
8982 /*
8983 * 0^D and ^^D: remove all indent.
8984 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008985 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8986 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 {
8988 --curwin->w_cursor.col;
8989 (void)del_char(FALSE); /* delete the '^' or '0' */
8990 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8991 if (State & REPLACE_FLAG)
8992 replace_pop_ins();
8993 if (lastc == '^')
8994 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008995 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 }
8997 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008998 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999
9000 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9001 did_ai = FALSE;
9002#ifdef FEAT_SMARTINDENT
9003 did_si = FALSE;
9004 can_si = FALSE;
9005 can_si_back = FALSE;
9006#endif
9007#ifdef FEAT_CINDENT
9008 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9009#endif
9010}
9011
9012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009013ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
9015 int temp;
9016
9017 if (stop_arrow() == FAIL)
9018 return;
9019 if (gchar_cursor() == NUL) /* delete newline */
9020 {
9021 temp = curwin->w_cursor.col;
9022 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009023 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009024 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009028 /* Adjust orig_line_count in case more lines have been deleted than
9029 * have been added. That makes sure, that open_line() later
9030 * can access all buffer lines correctly */
9031 if (State & VREPLACE_FLAG &&
9032 orig_line_count > curbuf->b_ml.ml_line_count)
9033 orig_line_count = curbuf->b_ml.ml_line_count;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009036 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9037 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 did_ai = FALSE;
9039#ifdef FEAT_SMARTINDENT
9040 did_si = FALSE;
9041 can_si = FALSE;
9042 can_si_back = FALSE;
9043#endif
9044 AppendCharToRedobuff(K_DEL);
9045}
9046
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009047static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009048
9049/*
9050 * Delete one character for ins_bs().
9051 */
9052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009053ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009054{
9055 dec_cursor();
9056 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9057 if (State & REPLACE_FLAG)
9058 {
9059 /* Don't delete characters before the insert point when in
9060 * Replace mode */
9061 if (curwin->w_cursor.lnum != Insstart.lnum
9062 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009063 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009064 }
9065 else
9066 (void)del_char(FALSE);
9067}
9068
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069/*
9070 * Handle Backspace, delete-word and delete-line in Insert mode.
9071 * Return TRUE when backspace was actually used.
9072 */
9073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009074ins_bs(
9075 int c,
9076 int mode,
9077 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
9079 linenr_T lnum;
9080 int cc;
9081 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009082 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 colnr_T mincol;
9084 int did_backspace = FALSE;
9085 int in_indent;
9086 int oldState;
9087#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009088 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089#endif
9090
9091 /*
9092 * can't delete anything in an empty file
9093 * can't backup past first character in buffer
9094 * can't backup past starting point unless 'backspace' > 1
9095 * can backup to a previous line if 'backspace' == 0
9096 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009097 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 || (
9099#ifdef FEAT_RIGHTLEFT
9100 !revins_on &&
9101#endif
9102 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9103 || (!can_bs(BS_START)
9104 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009105 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9106 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9108 && curwin->w_cursor.col <= ai_col)
9109 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9110 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009111 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 return FALSE;
9113 }
9114
9115 if (stop_arrow() == FAIL)
9116 return FALSE;
9117 in_indent = inindent(0);
9118#ifdef FEAT_CINDENT
9119 if (in_indent)
9120 can_cindent = FALSE;
9121#endif
9122#ifdef FEAT_COMMENTS
9123 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9124#endif
9125#ifdef FEAT_RIGHTLEFT
9126 if (revins_on) /* put cursor after last inserted char */
9127 inc_cursor();
9128#endif
9129
9130#ifdef FEAT_VIRTUALEDIT
9131 /* Virtualedit:
9132 * BACKSPACE_CHAR eats a virtual space
9133 * BACKSPACE_WORD eats all coladd
9134 * BACKSPACE_LINE eats all coladd and keeps going
9135 */
9136 if (curwin->w_cursor.coladd > 0)
9137 {
9138 if (mode == BACKSPACE_CHAR)
9139 {
9140 --curwin->w_cursor.coladd;
9141 return TRUE;
9142 }
9143 if (mode == BACKSPACE_WORD)
9144 {
9145 curwin->w_cursor.coladd = 0;
9146 return TRUE;
9147 }
9148 curwin->w_cursor.coladd = 0;
9149 }
9150#endif
9151
9152 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009153 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 */
9155 if (curwin->w_cursor.col == 0)
9156 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009157 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009158 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159#ifdef FEAT_RIGHTLEFT
9160 || revins_on
9161#endif
9162 )
9163 {
9164 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9165 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9166 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009167 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009168 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 }
9170 /*
9171 * In replace mode:
9172 * cc < 0: NL was inserted, delete it
9173 * cc >= 0: NL was replaced, put original characters back
9174 */
9175 cc = -1;
9176 if (State & REPLACE_FLAG)
9177 cc = replace_pop(); /* returns -1 if NL was inserted */
9178 /*
9179 * In replace mode, in the line we started replacing, we only move the
9180 * cursor.
9181 */
9182 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9183 {
9184 dec_cursor();
9185 }
9186 else
9187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 if (!(State & VREPLACE_FLAG)
9189 || curwin->w_cursor.lnum > orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 {
9191 temp = gchar_cursor(); /* remember current char */
9192 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009193
9194 /* When "aw" is in 'formatoptions' we must delete the space at
9195 * the end of the line, otherwise the line will be broken
9196 * again when auto-formatting. */
9197 if (has_format_option(FO_AUTO)
9198 && has_format_option(FO_WHITE_PAR))
9199 {
9200 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9201 TRUE);
9202 int len;
9203
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009204 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009205 if (len > 0 && ptr[len - 1] == ' ')
9206 ptr[len - 1] = NUL;
9207 }
9208
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009209 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 if (temp == NUL && gchar_cursor() != NUL)
9211 inc_cursor();
9212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 else
9214 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215
9216 /*
9217 * In REPLACE mode we have to put back the text that was replaced
9218 * by the NL. On the replace stack is first a NUL-terminated
9219 * sequence of characters that were deleted and then the
9220 * characters that NL replaced.
9221 */
9222 if (State & REPLACE_FLAG)
9223 {
9224 /*
9225 * Do the next ins_char() in NORMAL state, to
9226 * prevent ins_char() from replacing characters and
9227 * avoiding showmatch().
9228 */
9229 oldState = State;
9230 State = NORMAL;
9231 /*
9232 * restore characters (blanks) deleted after cursor
9233 */
9234 while (cc > 0)
9235 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009236 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237#ifdef FEAT_MBYTE
9238 mb_replace_pop_ins(cc);
9239#else
9240 ins_char(cc);
9241#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009242 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 cc = replace_pop();
9244 }
9245 /* restore the characters that NL replaced */
9246 replace_pop_ins();
9247 State = oldState;
9248 }
9249 }
9250 did_ai = FALSE;
9251 }
9252 else
9253 {
9254 /*
9255 * Delete character(s) before the cursor.
9256 */
9257#ifdef FEAT_RIGHTLEFT
9258 if (revins_on) /* put cursor on last inserted char */
9259 dec_cursor();
9260#endif
9261 mincol = 0;
9262 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009263 if (mode == BACKSPACE_LINE
9264 && (curbuf->b_p_ai
9265#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009266 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009267#endif
9268 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269#ifdef FEAT_RIGHTLEFT
9270 && !revins_on
9271#endif
9272 )
9273 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009274 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009276 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009278 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 }
9280
9281 /*
9282 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9283 */
9284 if ( mode == BACKSPACE_CHAR
9285 && ((p_sta && in_indent)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009286 || ((get_sts_value() != 0
9287#ifdef FEAT_VARTABS
9288 || tabstop_count(curbuf->b_p_vsts_array)
9289#endif
9290 )
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009291 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 && (*(ml_get_cursor() - 1) == TAB
9293 || (*(ml_get_cursor() - 1) == ' '
9294 && (!*inserted_space_p
9295 || arrow_used))))))
9296 {
9297 int ts;
9298 colnr_T vcol;
9299 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009300 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301
9302 *inserted_space_p = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 /* Compute the virtual column where we want to be. Since
9304 * 'showbreak' may get in the way, need to get the last column of
9305 * the previous character. */
9306 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009307 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 dec_cursor();
9309 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9310 inc_cursor();
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009311#ifdef FEAT_VARTABS
9312 if (p_sta && in_indent)
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009313 {
9314 ts = (int)get_sw_value(curbuf);
9315 want_vcol = (want_vcol / ts) * ts;
9316 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009317 else
Bram Moolenaar33d5ab32018-07-02 20:51:24 +02009318 want_vcol = tabstop_start(want_vcol, get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009319 curbuf->b_p_vsts_array);
9320#else
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +02009321 if (p_sta && in_indent)
9322 ts = (int)get_sw_value(curbuf);
9323 else
9324 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 want_vcol = (want_vcol / ts) * ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327
9328 /* delete characters until we are at or before want_vcol */
9329 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009330 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009331 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
9333 /* insert extra spaces until we are at want_vcol */
9334 while (vcol < want_vcol)
9335 {
9336 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009337 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9338 && curwin->w_cursor.col < Insstart_orig.col)
9339 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 if (State & VREPLACE_FLAG)
9342 ins_char(' ');
9343 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 {
9345 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009346 if ((State & REPLACE_FLAG))
9347 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 }
9349 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9350 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009351
9352 /* If we are now back where we started delete one character. Can
9353 * happen when using 'sts' and 'linebreak'. */
9354 if (vcol >= start_vcol)
9355 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 }
9357
9358 /*
9359 * Delete upto starting point, start of line or previous word.
9360 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009361 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009363#ifdef FEAT_MBYTE
9364 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009366 if (has_mbyte)
9367 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009369 do
9370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009372 if (!revins_on) /* put cursor on char to be deleted */
9373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009375
9376 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009378 /* look multi-byte character class */
9379 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009381 prev_cclass = cclass;
9382 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009385
9386 /* start of word? */
9387 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9388 {
9389 mode = BACKSPACE_WORD_NOT_SPACE;
9390 temp = vim_iswordc(cc);
9391 }
9392 /* end of word? */
9393 else if (mode == BACKSPACE_WORD_NOT_SPACE
9394 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9395#ifdef FEAT_MBYTE
9396 || prev_cclass != cclass
9397#endif
9398 ))
9399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009401 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009403 inc_cursor();
9404#ifdef FEAT_RIGHTLEFT
9405 else if (State & REPLACE_FLAG)
9406 dec_cursor();
9407#endif
9408 break;
9409 }
9410 if (State & REPLACE_FLAG)
9411 replace_do_bs(-1);
9412 else
9413 {
9414#ifdef FEAT_MBYTE
9415 if (enc_utf8 && p_deco)
9416 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9417#endif
9418 (void)del_char(FALSE);
9419#ifdef FEAT_MBYTE
9420 /*
9421 * If there are combining characters and 'delcombine' is set
9422 * move the cursor back. Don't back up before the base
9423 * character.
9424 */
9425 if (enc_utf8 && p_deco && cpc[0] != NUL)
9426 inc_cursor();
9427#endif
9428#ifdef FEAT_RIGHTLEFT
9429 if (revins_chars)
9430 {
9431 revins_chars--;
9432 revins_legal++;
9433 }
9434 if (revins_on && gchar_cursor() == NUL)
9435 break;
9436#endif
9437 }
9438 /* Just a single backspace?: */
9439 if (mode == BACKSPACE_CHAR)
9440 break;
9441 } while (
9442#ifdef FEAT_RIGHTLEFT
9443 revins_on ||
9444#endif
9445 (curwin->w_cursor.col > mincol
9446 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9447 || curwin->w_cursor.col != Insstart_orig.col)));
9448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 did_backspace = TRUE;
9450 }
9451#ifdef FEAT_SMARTINDENT
9452 did_si = FALSE;
9453 can_si = FALSE;
9454 can_si_back = FALSE;
9455#endif
9456 if (curwin->w_cursor.col <= 1)
9457 did_ai = FALSE;
9458 /*
9459 * It's a little strange to put backspaces into the redo
9460 * buffer, but it makes auto-indent a lot easier to deal
9461 * with.
9462 */
9463 AppendCharToRedobuff(c);
9464
9465 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009466 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009467 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009468 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
9470 /* vi behaviour: the cursor moves backward but the character that
9471 * was there remains visible
9472 * Vim behaviour: the cursor moves backward and the character that
9473 * was there is erased from the screen.
9474 * We can emulate the vi behaviour by pretending there is a dollar
9475 * displayed even when there isn't.
9476 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009477 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 dollar_vcol = curwin->w_virtcol;
9479
Bram Moolenaarce3be472008-01-14 19:12:28 +00009480#ifdef FEAT_FOLDING
9481 /* When deleting a char the cursor line must never be in a closed fold.
9482 * E.g., when 'foldmethod' is indent and deleting the first non-white
9483 * char before a Tab. */
9484 if (did_backspace)
9485 foldOpenCursor();
9486#endif
9487
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 return did_backspace;
9489}
9490
9491#ifdef FEAT_MOUSE
9492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009493ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009496 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497
9498# ifdef FEAT_GUI
9499 /* When GUI is active, also move/paste when 'mouse' is empty */
9500 if (!gui.in_use)
9501# endif
9502 if (!mouse_has(MOUSE_INSERT))
9503 return;
9504
9505 undisplay_dollar();
9506 tpos = curwin->w_cursor;
9507 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9508 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009509 win_T *new_curwin = curwin;
9510
9511 if (curwin != old_curwin && win_valid(old_curwin))
9512 {
9513 /* Mouse took us to another window. We need to go back to the
9514 * previous one to stop insert there properly. */
9515 curwin = old_curwin;
9516 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009517#ifdef FEAT_JOB_CHANNEL
9518 if (bt_prompt(curbuf))
9519 // Restart Insert mode when re-entering the prompt buffer.
9520 curbuf->b_prompt_insert = 'A';
9521#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009522 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009523 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009524 if (curwin != new_curwin && win_valid(new_curwin))
9525 {
9526 curwin = new_curwin;
9527 curbuf = curwin->w_buffer;
9528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529# ifdef FEAT_CINDENT
9530 can_cindent = TRUE;
9531# endif
9532 }
9533
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 /* redraw status lines (in case another window became active) */
9535 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536}
9537
9538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009539ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540{
9541 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009542 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009543# ifdef FEAT_INS_EXPAND
9544 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545# endif
9546
9547 tpos = curwin->w_cursor;
9548
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009549 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 {
9551 int row, col;
9552
9553 row = mouse_row;
9554 col = mouse_col;
9555
9556 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009557 wp = mouse_find_win(&row, &col);
9558 if (wp == NULL)
9559 return;
9560 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 curbuf = curwin->w_buffer;
9562 }
9563 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 undisplay_dollar();
9565
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009566# ifdef FEAT_INS_EXPAND
9567 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009568 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009569# endif
9570 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009571 if (dir == MSCR_DOWN || dir == MSCR_UP)
9572 {
9573 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9574 scroll_redraw(dir,
9575 (long)(curwin->w_botline - curwin->w_topline));
9576 else
9577 scroll_redraw(dir, 3L);
9578 }
9579#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009580 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009581 {
9582 int val, step = 6;
9583
9584 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009585 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009586 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9587 if (val < 0)
9588 val = 0;
9589 gui_do_horiz_scroll(val, TRUE);
9590 }
9591#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009592# ifdef FEAT_INS_EXPAND
9593 did_scroll = TRUE;
9594# endif
9595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 curwin->w_redr_status = TRUE;
9598
9599 curwin = old_curwin;
9600 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009602# ifdef FEAT_INS_EXPAND
9603 /* The popup menu may overlay the window, need to redraw it.
9604 * TODO: Would be more efficient to only redraw the windows that are
9605 * overlapped by the popup menu. */
9606 if (pum_visible() && did_scroll)
9607 {
9608 redraw_all_later(NOT_VALID);
9609 ins_compl_show_pum();
9610 }
9611# endif
9612
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009613 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 {
9615 start_arrow(&tpos);
9616# ifdef FEAT_CINDENT
9617 can_cindent = TRUE;
9618# endif
9619 }
9620}
9621#endif
9622
Bram Moolenaarec2da362017-01-21 20:04:22 +01009623/*
9624 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9625 * P_PE literally.
9626 * When "drop" is TRUE then consume the text and drop it.
9627 */
9628 int
9629bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9630{
9631 int c;
9632 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9633 int idx = 0;
9634 char_u *end = find_termcode((char_u *)"PE");
9635 int ret_char = -1;
9636 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009637 int save_paste = p_paste;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009638
9639 /* If the end code is too long we can't detect it, read everything. */
9640 if (STRLEN(end) >= NUMBUFLEN)
9641 end = NULL;
9642 ++no_mapping;
9643 allow_keys = 0;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009644 if (!p_paste)
9645 // Also have the side effects of setting 'paste' to make it work much
9646 // faster.
9647 set_option_value((char_u *)"paste", TRUE, NULL, 0);
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009648
Bram Moolenaarec2da362017-01-21 20:04:22 +01009649 for (;;)
9650 {
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009651 // When the end is not defined read everything there is.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009652 if (end == NULL && vpeekc() == NUL)
9653 break;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009654 do
9655 {
9656 c = vgetc();
9657 } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
9658 if (c == NUL || got_int)
9659 // When CTRL-C was encountered the typeahead will be flushed and we
9660 // won't get the end sequence.
9661 break;
9662
Bram Moolenaarec2da362017-01-21 20:04:22 +01009663#ifdef FEAT_MBYTE
9664 if (has_mbyte)
9665 idx += (*mb_char2bytes)(c, buf + idx);
9666 else
9667#endif
9668 buf[idx++] = c;
9669 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009670 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009671 {
9672 if (end[idx] == NUL)
9673 break; /* Found the end of paste code. */
9674 continue;
9675 }
9676 if (!drop)
9677 {
9678 switch (mode)
9679 {
9680 case PASTE_CMDLINE:
9681 put_on_cmdline(buf, idx, TRUE);
9682 break;
9683
9684 case PASTE_EX:
9685 if (gap != NULL && ga_grow(gap, idx) == OK)
9686 {
9687 mch_memmove((char *)gap->ga_data + gap->ga_len,
9688 buf, (size_t)idx);
9689 gap->ga_len += idx;
9690 }
9691 break;
9692
9693 case PASTE_INSERT:
9694 if (stop_arrow() == OK)
9695 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009696 c = buf[0];
9697 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9698 ins_eol(c);
9699 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009700 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009701 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009702 AppendToRedobuffLit(buf, idx);
9703 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009704 }
9705 break;
9706
9707 case PASTE_ONE_CHAR:
9708 if (ret_char == -1)
9709 {
9710#ifdef FEAT_MBYTE
9711 if (has_mbyte)
9712 ret_char = (*mb_ptr2char)(buf);
9713 else
9714#endif
9715 ret_char = buf[0];
9716 }
9717 break;
9718 }
9719 }
9720 idx = 0;
9721 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009722
Bram Moolenaarec2da362017-01-21 20:04:22 +01009723 --no_mapping;
9724 allow_keys = save_allow_keys;
Bram Moolenaarfdd71552018-07-28 23:12:05 +02009725 if (!save_paste)
9726 set_option_value((char_u *)"paste", FALSE, NULL, 0);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009727
9728 return ret_char;
9729}
9730
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009731#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009733ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009734{
9735 /* We will be leaving the current window, unless closing another tab. */
9736 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9737 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9738 {
9739 undisplay_dollar();
9740 start_arrow(&curwin->w_cursor);
9741# ifdef FEAT_CINDENT
9742 can_cindent = TRUE;
9743# endif
9744 }
9745
9746 if (c == K_TABLINE)
9747 goto_tabpage(current_tab);
9748 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009749 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009750 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009751 redraw_statuslines(); /* will redraw the tabline when needed */
9752 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009753}
9754#endif
9755
9756#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009758ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760 pos_T tpos;
9761
9762 undisplay_dollar();
9763 tpos = curwin->w_cursor;
9764 if (gui_do_scroll())
9765 {
9766 start_arrow(&tpos);
9767# ifdef FEAT_CINDENT
9768 can_cindent = TRUE;
9769# endif
9770 }
9771}
9772
9773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009774ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
9776 pos_T tpos;
9777
9778 undisplay_dollar();
9779 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009780 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 {
9782 start_arrow(&tpos);
9783# ifdef FEAT_CINDENT
9784 can_cindent = TRUE;
9785# endif
9786 }
9787}
9788#endif
9789
9790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009791ins_left(
9792 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793{
9794 pos_T tpos;
9795
9796#ifdef FEAT_FOLDING
9797 if ((fdo_flags & FDO_HOR) && KeyTyped)
9798 foldOpenCursor();
9799#endif
9800 undisplay_dollar();
9801 tpos = curwin->w_cursor;
9802 if (oneleft() == OK)
9803 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009804#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9805 /* Only call start_arrow() when not busy with preediting, it will
9806 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009807 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009808#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009809 {
9810 start_arrow_with_change(&tpos, end_change);
9811 if (!end_change)
9812 AppendCharToRedobuff(K_LEFT);
9813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814#ifdef FEAT_RIGHTLEFT
9815 /* If exit reversed string, position is fixed */
9816 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9817 revins_legal++;
9818 revins_chars++;
9819#endif
9820 }
9821
9822 /*
9823 * if 'whichwrap' set for cursor in insert mode may go to
9824 * previous line
9825 */
9826 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9827 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009828 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 start_arrow(&tpos);
9830 --(curwin->w_cursor.lnum);
9831 coladvance((colnr_T)MAXCOL);
9832 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9833 }
9834 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009835 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009836 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837}
9838
9839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009840ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841{
9842 pos_T tpos;
9843
9844#ifdef FEAT_FOLDING
9845 if ((fdo_flags & FDO_HOR) && KeyTyped)
9846 foldOpenCursor();
9847#endif
9848 undisplay_dollar();
9849 tpos = curwin->w_cursor;
9850 if (c == K_C_HOME)
9851 curwin->w_cursor.lnum = 1;
9852 curwin->w_cursor.col = 0;
9853#ifdef FEAT_VIRTUALEDIT
9854 curwin->w_cursor.coladd = 0;
9855#endif
9856 curwin->w_curswant = 0;
9857 start_arrow(&tpos);
9858}
9859
9860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009861ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862{
9863 pos_T tpos;
9864
9865#ifdef FEAT_FOLDING
9866 if ((fdo_flags & FDO_HOR) && KeyTyped)
9867 foldOpenCursor();
9868#endif
9869 undisplay_dollar();
9870 tpos = curwin->w_cursor;
9871 if (c == K_C_END)
9872 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9873 coladvance((colnr_T)MAXCOL);
9874 curwin->w_curswant = MAXCOL;
9875
9876 start_arrow(&tpos);
9877}
9878
9879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009880ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881{
9882#ifdef FEAT_FOLDING
9883 if ((fdo_flags & FDO_HOR) && KeyTyped)
9884 foldOpenCursor();
9885#endif
9886 undisplay_dollar();
9887 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9888 {
9889 start_arrow(&curwin->w_cursor);
9890 (void)bck_word(1L, FALSE, FALSE);
9891 curwin->w_set_curswant = TRUE;
9892 }
9893 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009894 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895}
9896
9897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009898ins_right(
9899 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
9901#ifdef FEAT_FOLDING
9902 if ((fdo_flags & FDO_HOR) && KeyTyped)
9903 foldOpenCursor();
9904#endif
9905 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009906 if (gchar_cursor() != NUL
9907#ifdef FEAT_VIRTUALEDIT
9908 || virtual_active()
9909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 )
9911 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009912 start_arrow_with_change(&curwin->w_cursor, end_change);
9913 if (!end_change)
9914 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 curwin->w_set_curswant = TRUE;
9916#ifdef FEAT_VIRTUALEDIT
9917 if (virtual_active())
9918 oneright();
9919 else
9920#endif
9921 {
9922#ifdef FEAT_MBYTE
9923 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009924 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 else
9926#endif
9927 ++curwin->w_cursor.col;
9928 }
9929
9930#ifdef FEAT_RIGHTLEFT
9931 revins_legal++;
9932 if (revins_chars)
9933 revins_chars--;
9934#endif
9935 }
9936 /* if 'whichwrap' set for cursor in insert mode, may move the
9937 * cursor to the next line */
9938 else if (vim_strchr(p_ww, ']') != NULL
9939 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9940 {
9941 start_arrow(&curwin->w_cursor);
9942 curwin->w_set_curswant = TRUE;
9943 ++curwin->w_cursor.lnum;
9944 curwin->w_cursor.col = 0;
9945 }
9946 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009947 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009948 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949}
9950
9951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009952ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
9954#ifdef FEAT_FOLDING
9955 if ((fdo_flags & FDO_HOR) && KeyTyped)
9956 foldOpenCursor();
9957#endif
9958 undisplay_dollar();
9959 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9960 || gchar_cursor() != NUL)
9961 {
9962 start_arrow(&curwin->w_cursor);
9963 (void)fwd_word(1L, FALSE, 0);
9964 curwin->w_set_curswant = TRUE;
9965 }
9966 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009967 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968}
9969
9970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009971ins_up(
9972 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973{
9974 pos_T tpos;
9975 linenr_T old_topline = curwin->w_topline;
9976#ifdef FEAT_DIFF
9977 int old_topfill = curwin->w_topfill;
9978#endif
9979
9980 undisplay_dollar();
9981 tpos = curwin->w_cursor;
9982 if (cursor_up(1L, TRUE) == OK)
9983 {
9984 if (startcol)
9985 coladvance(getvcol_nolist(&Insstart));
9986 if (old_topline != curwin->w_topline
9987#ifdef FEAT_DIFF
9988 || old_topfill != curwin->w_topfill
9989#endif
9990 )
9991 redraw_later(VALID);
9992 start_arrow(&tpos);
9993#ifdef FEAT_CINDENT
9994 can_cindent = TRUE;
9995#endif
9996 }
9997 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009998 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
10001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010002ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004 pos_T tpos;
10005
10006 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010007
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010008 if (mod_mask & MOD_MASK_CTRL)
10009 {
10010 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010011 if (first_tabpage->tp_next != NULL)
10012 {
10013 start_arrow(&curwin->w_cursor);
10014 goto_tabpage(-1);
10015 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010016 return;
10017 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010018
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 tpos = curwin->w_cursor;
10020 if (onepage(BACKWARD, 1L) == OK)
10021 {
10022 start_arrow(&tpos);
10023#ifdef FEAT_CINDENT
10024 can_cindent = TRUE;
10025#endif
10026 }
10027 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010028 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029}
10030
10031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010032ins_down(
10033 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034{
10035 pos_T tpos;
10036 linenr_T old_topline = curwin->w_topline;
10037#ifdef FEAT_DIFF
10038 int old_topfill = curwin->w_topfill;
10039#endif
10040
10041 undisplay_dollar();
10042 tpos = curwin->w_cursor;
10043 if (cursor_down(1L, TRUE) == OK)
10044 {
10045 if (startcol)
10046 coladvance(getvcol_nolist(&Insstart));
10047 if (old_topline != curwin->w_topline
10048#ifdef FEAT_DIFF
10049 || old_topfill != curwin->w_topfill
10050#endif
10051 )
10052 redraw_later(VALID);
10053 start_arrow(&tpos);
10054#ifdef FEAT_CINDENT
10055 can_cindent = TRUE;
10056#endif
10057 }
10058 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010059 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060}
10061
10062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010063ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
10065 pos_T tpos;
10066
10067 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010068
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010069 if (mod_mask & MOD_MASK_CTRL)
10070 {
10071 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010072 if (first_tabpage->tp_next != NULL)
10073 {
10074 start_arrow(&curwin->w_cursor);
10075 goto_tabpage(0);
10076 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010077 return;
10078 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010079
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 tpos = curwin->w_cursor;
10081 if (onepage(FORWARD, 1L) == OK)
10082 {
10083 start_arrow(&tpos);
10084#ifdef FEAT_CINDENT
10085 can_cindent = TRUE;
10086#endif
10087 }
10088 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010089 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090}
10091
10092#ifdef FEAT_DND
10093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010094ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095{
10096 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10097}
10098#endif
10099
10100/*
10101 * Handle TAB in Insert or Replace mode.
10102 * Return TRUE when the TAB needs to be inserted like a normal character.
10103 */
10104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010105ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
10107 int ind;
10108 int i;
10109 int temp;
10110
10111 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10112 Insstart_blank_vcol = get_nolist_virtcol();
10113 if (echeck_abbr(TAB + ABBR_OFF))
10114 return FALSE;
10115
10116 ind = inindent(0);
10117#ifdef FEAT_CINDENT
10118 if (ind)
10119 can_cindent = FALSE;
10120#endif
10121
10122 /*
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010123 * When nothing special, insert TAB like a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 */
10125 if (!curbuf->b_p_et
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010126#ifdef FEAT_VARTABS
10127 && !(p_sta && ind
10128 /* These five lines mean 'tabstop' != 'shiftwidth' */
10129 && ((tabstop_count(curbuf->b_p_vts_array) > 1)
10130 || (tabstop_count(curbuf->b_p_vts_array) == 1
10131 && tabstop_first(curbuf->b_p_vts_array)
10132 != get_sw_value(curbuf))
10133 || (tabstop_count(curbuf->b_p_vts_array) == 0
10134 && curbuf->b_p_ts != get_sw_value(curbuf))))
10135 && tabstop_count(curbuf->b_p_vsts_array) == 0
10136#else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010137 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010138#endif
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010139 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 return TRUE;
10141
10142 if (stop_arrow() == FAIL)
10143 return TRUE;
10144
10145 did_ai = FALSE;
10146#ifdef FEAT_SMARTINDENT
10147 did_si = FALSE;
10148 can_si = FALSE;
10149 can_si_back = FALSE;
10150#endif
10151 AppendToRedobuff((char_u *)"\t");
10152
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010153#ifdef FEAT_VARTABS
10154 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
10155 {
Bram Moolenaarc9fe5ab2018-07-05 22:27:08 +020010156 temp = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010157 temp -= get_nolist_virtcol() % temp;
10158 }
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010159 else if (tabstop_count(curbuf->b_p_vsts_array) > 0 || curbuf->b_p_sts != 0)
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010160 /* use 'softtabstop' when set */
Bram Moolenaar33d5ab32018-07-02 20:51:24 +020010161 temp = tabstop_padding(get_nolist_virtcol(), get_sts_value(),
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010162 curbuf->b_p_vsts_array);
10163 else /* otherwise use 'tabstop' */
10164 temp = tabstop_padding(get_nolist_virtcol(), curbuf->b_p_ts,
10165 curbuf->b_p_vts_array);
10166#else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010168 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010169 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10170 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 else /* otherwise use 'tabstop' */
10172 temp = (int)curbuf->b_p_ts;
10173 temp -= get_nolist_virtcol() % temp;
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175
10176 /*
10177 * Insert the first space with ins_char(). It will delete one char in
10178 * replace mode. Insert the rest with ins_str(); it will not delete any
10179 * chars. For VREPLACE mode, we use ins_char() for all characters.
10180 */
10181 ins_char(' ');
10182 while (--temp > 0)
10183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 if (State & VREPLACE_FLAG)
10185 ins_char(' ');
10186 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 {
10188 ins_str((char_u *)" ");
10189 if (State & REPLACE_FLAG) /* no char replaced */
10190 replace_push(NUL);
10191 }
10192 }
10193
10194 /*
10195 * When 'expandtab' not set: Replace spaces by TABs where possible.
10196 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010197#ifdef FEAT_VARTABS
10198 if (!curbuf->b_p_et && (tabstop_count(curbuf->b_p_vsts_array) > 0
10199 || get_sts_value() > 0
10200 || (p_sta && ind)))
10201#else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010202 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar04958cb2018-06-23 19:23:02 +020010203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 {
10205 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 char_u *saved_line = NULL; /* init for GCC */
10207 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 pos_T fpos;
10209 pos_T *cursor;
10210 colnr_T want_vcol, vcol;
10211 int change_col = -1;
10212 int save_list = curwin->w_p_list;
10213
10214 /*
10215 * Get the current line. For VREPLACE mode, don't make real changes
10216 * yet, just work on a copy of the line.
10217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 if (State & VREPLACE_FLAG)
10219 {
10220 pos = curwin->w_cursor;
10221 cursor = &pos;
10222 saved_line = vim_strsave(ml_get_curline());
10223 if (saved_line == NULL)
10224 return FALSE;
10225 ptr = saved_line + pos.col;
10226 }
10227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 {
10229 ptr = ml_get_cursor();
10230 cursor = &curwin->w_cursor;
10231 }
10232
10233 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10234 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10235 curwin->w_p_list = FALSE;
10236
10237 /* Find first white before the cursor */
10238 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010239 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 {
10241 --fpos.col;
10242 --ptr;
10243 }
10244
10245 /* In Replace mode, don't change characters before the insert point. */
10246 if ((State & REPLACE_FLAG)
10247 && fpos.lnum == Insstart.lnum
10248 && fpos.col < Insstart.col)
10249 {
10250 ptr += Insstart.col - fpos.col;
10251 fpos.col = Insstart.col;
10252 }
10253
10254 /* compute virtual column numbers of first white and cursor */
10255 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10256 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10257
Bram Moolenaar597a4222014-06-25 14:39:50 +020010258 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10259 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010260 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010262 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 if (vcol + i > want_vcol)
10264 break;
10265 if (*ptr != TAB)
10266 {
10267 *ptr = TAB;
10268 if (change_col < 0)
10269 {
10270 change_col = fpos.col; /* Column of first change */
10271 /* May have to adjust Insstart */
10272 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10273 Insstart.col = fpos.col;
10274 }
10275 }
10276 ++fpos.col;
10277 ++ptr;
10278 vcol += i;
10279 }
10280
10281 if (change_col >= 0)
10282 {
10283 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010284 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285
10286 /* Skip over the spaces we need. */
10287 while (vcol < want_vcol && *ptr == ' ')
10288 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010289 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 ++ptr;
10291 ++repl_off;
10292 }
10293 if (vcol > want_vcol)
10294 {
10295 /* Must have a char with 'showbreak' just before it. */
10296 --ptr;
10297 --repl_off;
10298 }
10299 fpos.col += repl_off;
10300
10301 /* Delete following spaces. */
10302 i = cursor->col - fpos.col;
10303 if (i > 0)
10304 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010305 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 /* correct replace stack. */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010307 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 for (temp = i; --temp >= 0; )
10309 replace_join(repl_off);
10310 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010311#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010312 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010313 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010314 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010315 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10316 (char_u *)"\t", 1);
10317 }
10318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 cursor->col -= i;
10320
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 /*
10322 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10323 * backspacing over the changed spacing and then inserting the new
10324 * spacing.
10325 */
10326 if (State & VREPLACE_FLAG)
10327 {
10328 /* Backspace from real cursor to change_col */
10329 backspace_until_column(change_col);
10330
10331 /* Insert each char in saved_line from changed_col to
10332 * ptr-cursor */
10333 ins_bytes_len(saved_line + change_col,
10334 cursor->col - change_col);
10335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 }
10337
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 if (State & VREPLACE_FLAG)
10339 vim_free(saved_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 curwin->w_p_list = save_list;
10341 }
10342
10343 return FALSE;
10344}
10345
10346/*
10347 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010348 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 */
10350 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010351ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352{
10353 int i;
10354
10355 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010356 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 undisplay_dollar();
10360
10361 /*
10362 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10363 * character under the cursor. Only push a NUL on the replace stack,
10364 * nothing to put back when the NL is deleted.
10365 */
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010366 if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 replace_push(NUL);
10368
10369 /*
10370 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10371 * replacing the next line, so we push all of the characters left on the
10372 * line onto the replace stack. This is not done here though, it is done
10373 * in open_line().
10374 */
10375
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010376#ifdef FEAT_VIRTUALEDIT
10377 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10378 * CTRL-O). */
10379 if (virtual_active() && curwin->w_cursor.coladd > 0)
10380 coladvance(getviscol());
10381#endif
10382
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383#ifdef FEAT_RIGHTLEFT
10384# ifdef FEAT_FKMAP
10385 if (p_altkeymap && p_fkmap)
10386 fkmap(NL);
10387# endif
10388 /* NL in reverse insert will always start in the end of
10389 * current line. */
10390 if (revins_on)
10391 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10392#endif
10393
10394 AppendToRedobuff(NL_STR);
10395 i = open_line(FORWARD,
10396#ifdef FEAT_COMMENTS
10397 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10398#endif
10399 0, old_indent);
10400 old_indent = 0;
10401#ifdef FEAT_CINDENT
10402 can_cindent = TRUE;
10403#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010404#ifdef FEAT_FOLDING
10405 /* When inserting a line the cursor line must never be in a closed fold. */
10406 foldOpenCursor();
10407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010409 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410}
10411
10412#ifdef FEAT_DIGRAPHS
10413/*
10414 * Handle digraph in insert mode.
10415 * Returns character still to be inserted, or NUL when nothing remaining to be
10416 * done.
10417 */
10418 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010419ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420{
10421 int c;
10422 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010423 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424
10425 pc_status = PC_STATUS_UNSET;
10426 if (redrawing() && !char_avail())
10427 {
10428 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010429 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430
10431 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010432 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433#ifdef FEAT_CMDL_INFO
10434 add_to_showcmd_c(Ctrl_K);
10435#endif
10436 }
10437
10438#ifdef USE_ON_FLY_SCROLL
10439 dont_scroll = TRUE; /* disallow scrolling here */
10440#endif
10441
10442 /* don't map the digraph chars. This also prevents the
10443 * mode message to be deleted when ESC is hit */
10444 ++no_mapping;
10445 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010446 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 --no_mapping;
10448 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010449 if (did_putchar)
10450 /* when the line fits in 'columns' the '?' is at the start of the next
10451 * line and will not be removed by the redraw */
10452 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010453
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 if (IS_SPECIAL(c) || mod_mask) /* special key */
10455 {
10456#ifdef FEAT_CMDL_INFO
10457 clear_showcmd();
10458#endif
10459 insert_special(c, TRUE, FALSE);
10460 return NUL;
10461 }
10462 if (c != ESC)
10463 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010464 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 if (redrawing() && !char_avail())
10466 {
10467 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010468 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469
10470 if (char2cells(c) == 1)
10471 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010472 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010474 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 }
10476#ifdef FEAT_CMDL_INFO
10477 add_to_showcmd_c(c);
10478#endif
10479 }
10480 ++no_mapping;
10481 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010482 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 --no_mapping;
10484 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010485 if (did_putchar)
10486 /* when the line fits in 'columns' the '?' is at the start of the
10487 * next line and will not be removed by a redraw */
10488 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 if (cc != ESC)
10490 {
10491 AppendToRedobuff((char_u *)CTRL_V_STR);
10492 c = getdigraph(c, cc, TRUE);
10493#ifdef FEAT_CMDL_INFO
10494 clear_showcmd();
10495#endif
10496 return c;
10497 }
10498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499#ifdef FEAT_CMDL_INFO
10500 clear_showcmd();
10501#endif
10502 return NUL;
10503}
10504#endif
10505
10506/*
10507 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10508 * Returns the char to be inserted, or NUL if none found.
10509 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010511ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512{
10513 int c;
10514 int temp;
10515 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010516 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517
10518 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10519 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010520 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 return NUL;
10522 }
10523
10524 /* try to advance to the cursor column */
10525 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010526 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 prev_ptr = ptr;
10528 validate_virtcol();
10529 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10530 {
10531 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010532 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 }
10534 if ((colnr_T)temp > curwin->w_virtcol)
10535 ptr = prev_ptr;
10536
10537#ifdef FEAT_MBYTE
10538 c = (*mb_ptr2char)(ptr);
10539#else
10540 c = *ptr;
10541#endif
10542 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010543 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 return c;
10545}
10546
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010547/*
10548 * CTRL-Y or CTRL-E typed in Insert mode.
10549 */
10550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010551ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010552{
10553 int c = tc;
10554
10555#ifdef FEAT_INS_EXPAND
10556 if (ctrl_x_mode == CTRL_X_SCROLL)
10557 {
10558 if (c == Ctrl_Y)
10559 scrolldown_clamp();
10560 else
10561 scrollup_clamp();
10562 redraw_later(VALID);
10563 }
10564 else
10565#endif
10566 {
10567 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10568 if (c != NUL)
10569 {
10570 long tw_save;
10571
10572 /* The character must be taken literally, insert like it
10573 * was typed after a CTRL-V, and pretend 'textwidth'
10574 * wasn't set. Digits, 'o' and 'x' are special after a
10575 * CTRL-V, don't use it for these. */
10576 if (c < 256 && !isalnum(c))
10577 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10578 tw_save = curbuf->b_p_tw;
10579 curbuf->b_p_tw = -1;
10580 insert_special(c, TRUE, FALSE);
10581 curbuf->b_p_tw = tw_save;
10582#ifdef FEAT_RIGHTLEFT
10583 revins_chars++;
10584 revins_legal++;
10585#endif
10586 c = Ctrl_V; /* pretend CTRL-V is last character */
10587 auto_format(FALSE, TRUE);
10588 }
10589 }
10590 return c;
10591}
10592
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593#ifdef FEAT_SMARTINDENT
10594/*
10595 * Try to do some very smart auto-indenting.
10596 * Used when inserting a "normal" character.
10597 */
10598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010599ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600{
10601 pos_T *pos, old_pos;
10602 char_u *ptr;
10603 int i;
10604 int temp;
10605
10606 /*
10607 * do some very smart indenting when entering '{' or '}'
10608 */
10609 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10610 {
10611 /*
10612 * for '}' set indent equal to indent of line containing matching '{'
10613 */
10614 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10615 {
10616 old_pos = curwin->w_cursor;
10617 /*
10618 * If the matching '{' has a ')' immediately before it (ignoring
10619 * white-space), then line up with the start of the line
10620 * containing the matching '(' if there is one. This handles the
10621 * case where an "if (..\n..) {" statement continues over multiple
10622 * lines -- webb
10623 */
10624 ptr = ml_get(pos->lnum);
10625 i = pos->col;
10626 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010627 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 ;
10629 curwin->w_cursor.lnum = pos->lnum;
10630 curwin->w_cursor.col = i;
10631 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10632 curwin->w_cursor = *pos;
10633 i = get_indent();
10634 curwin->w_cursor = old_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010636 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 (void)set_indent(i, SIN_CHANGED);
10639 }
10640 else if (curwin->w_cursor.col > 0)
10641 {
10642 /*
10643 * when inserting '{' after "O" reduce indent, but not
10644 * more than indent of previous line
10645 */
10646 temp = TRUE;
10647 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10648 {
10649 old_pos = curwin->w_cursor;
10650 i = get_indent();
10651 while (curwin->w_cursor.lnum > 1)
10652 {
10653 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10654
10655 /* ignore empty lines and lines starting with '#'. */
10656 if (*ptr != '#' && *ptr != NUL)
10657 break;
10658 }
10659 if (get_indent() >= i)
10660 temp = FALSE;
10661 curwin->w_cursor = old_pos;
10662 }
10663 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010664 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 }
10666 }
10667
10668 /*
10669 * set indent of '#' always to 0
10670 */
10671 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10672 {
10673 /* remember current indent for next line */
10674 old_indent = get_indent();
10675 (void)set_indent(0, SIN_CHANGED);
10676 }
10677
10678 /* Adjust ai_col, the char at this position can be deleted. */
10679 if (ai_col > curwin->w_cursor.col)
10680 ai_col = curwin->w_cursor.col;
10681}
10682#endif
10683
10684/*
10685 * Get the value that w_virtcol would have when 'list' is off.
10686 * Unless 'cpo' contains the 'L' flag.
10687 */
10688 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010689get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690{
10691 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10692 return getvcol_nolist(&curwin->w_cursor);
10693 validate_virtcol();
10694 return curwin->w_virtcol;
10695}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010696
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010697#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010698/*
10699 * Handle the InsertCharPre autocommand.
10700 * "c" is the character that was typed.
10701 * Return a pointer to allocated memory with the replacement string.
10702 * Return NULL to continue inserting "c".
10703 */
10704 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010705do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010706{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010707 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010708 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010709
10710 /* Return quickly when there is nothing to do. */
10711 if (!has_insertcharpre())
10712 return NULL;
10713
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010714# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010715 if (has_mbyte)
10716 buf[(*mb_char2bytes)(c, buf)] = NUL;
10717 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010718# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010719 {
10720 buf[0] = c;
10721 buf[1] = NUL;
10722 }
10723
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010724 /* Lock the text to avoid weird things from happening. */
10725 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010726 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010727
Bram Moolenaar704984a2012-06-01 14:57:51 +020010728 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010729 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010730 {
10731 /* Get the value of v:char. It may be empty or more than one
10732 * character. Only use it when changed, otherwise continue with the
10733 * original character to avoid breaking autoindent. */
10734 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10735 res = vim_strsave(get_vim_var_str(VV_CHAR));
10736 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010737
10738 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10739 --textlock;
10740
10741 return res;
10742}
10743#endif