blob: 7c7fadd10a1abd52f7f96deff6d531e44fb34757 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010020# define CTRL_X_WANT_IDENT 0x100
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010022# define CTRL_X_NORMAL 0 /* CTRL-N CTRL-P completion, default */
23# define CTRL_X_NOT_DEFINED_YET 1
24# define CTRL_X_SCROLL 2
25# define CTRL_X_WHOLE_LINE 3
26# define CTRL_X_FILES 4
27# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
28# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
29# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
30# define CTRL_X_FINISHED 8
31# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
32# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
33# define CTRL_X_CMDLINE 11
34# define CTRL_X_FUNCTION 12
35# define CTRL_X_OMNI 13
36# define CTRL_X_SPELL 14
37# define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
38# define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010040# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
41# define CTRL_X_MODE_LINE_OR_EVAL(m) ((m) == CTRL_X_WHOLE_LINE || (m) == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010043/* Message for CTRL-X mode, index is ctrl_x_mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static char *ctrl_x_msgs[] =
45{
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010046 N_(" Keyword completion (^N^P)"), /* CTRL_X_NORMAL, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000047 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010048 NULL, /* CTRL_X_SCROLL: depends on state */
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 N_(" Whole line completion (^L^N^P)"),
50 N_(" File name completion (^F^N^P)"),
51 N_(" Tag completion (^]^N^P)"),
52 N_(" Path pattern completion (^N^P)"),
53 N_(" Definition completion (^D^N^P)"),
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010054 NULL, /* CTRL_X_FINISHED */
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 N_(" Dictionary completion (^K^N^P)"),
56 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000057 N_(" Command-line completion (^V^N^P)"),
58 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000059 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000060 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000061 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010062 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000063};
64
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000065static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010066# ifdef FEAT_COMPL_FUNC
Bram Moolenaar37dd0182010-11-10 16:54:20 +010067static char e_complwin[] = N_("E839: Completion function changed window");
68static char e_compldel[] = N_("E840: Completion function deleted text");
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71/*
72 * Structure used to store one match for insert completion.
73 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000074typedef struct compl_S compl_T;
75struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 compl_T *cp_next;
78 compl_T *cp_prev;
79 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000080 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000081 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000082 char_u *cp_fname; /* file containing the match, allocated when
83 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000084 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
85 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086};
87
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +010088# define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
89# define FREE_FNAME (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91/*
92 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000093 * "compl_first_match" points to the start of the list.
94 * "compl_curr_match" points to the currently selected entry.
95 * "compl_shown_match" is different from compl_curr_match during
96 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000098static compl_T *compl_first_match = NULL;
99static compl_T *compl_curr_match = NULL;
100static compl_T *compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +0200101static compl_T *compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000103/* After using a cursor key <Enter> selects a match in the popup menu,
104 * otherwise it inserts a line break. */
105static int compl_enter_selects = FALSE;
106
Bram Moolenaara6557602006-02-04 22:43:20 +0000107/* When "compl_leader" is not NULL only matches that start with this string
108 * are used. */
109static char_u *compl_leader = NULL;
110
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000111static int compl_get_longest = FALSE; /* put longest common string
112 in compl_leader */
113
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200114static int compl_no_insert = FALSE; /* FALSE: select & insert
115 TRUE: noinsert */
116static int compl_no_select = FALSE; /* FALSE: select & insert
117 TRUE: noselect */
118
Bram Moolenaara6557602006-02-04 22:43:20 +0000119static int compl_used_match; /* Selected one of the matches. When
120 FALSE the match was edited or using
121 the longest common string. */
122
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000123static int compl_was_interrupted = FALSE; /* didn't finish finding
124 completions. */
125
126static int compl_restarting = FALSE; /* don't insert match */
127
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128/* When the first completion is done "compl_started" is set. When it's
129 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000131
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100132/* Which Ctrl-X mode are we in? */
133static int ctrl_x_mode = CTRL_X_NORMAL;
134
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000135/* Set when doing something for completion that may call edit() recursively,
136 * which is not allowed. */
137static int compl_busy = FALSE;
138
Bram Moolenaar572cb562005-08-05 21:35:02 +0000139static int compl_matches = 0;
140static char_u *compl_pattern = NULL;
141static int compl_direction = FORWARD;
142static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000143static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000144static pos_T compl_startpos;
145static colnr_T compl_col = 0; /* column where the text starts
146 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000147static char_u *compl_orig_text = NULL; /* text as it was before
148 * completion started */
149static int compl_cont_mode = 0;
150static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
Bram Moolenaar82139082011-09-14 16:52:09 +0200152static int compl_opt_refresh_always = FALSE;
153
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static void ins_ctrl_x(void);
155static int has_compl_option(int dict_opt);
156static int ins_compl_accept_char(int c);
157static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
158static int ins_compl_equal(compl_T *match, char_u *str, int len);
159static void ins_compl_longest_match(compl_T *match);
160static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
161static int ins_compl_make_cyclic(void);
162static void ins_compl_upd_pum(void);
163static void ins_compl_del_pum(void);
164static int pum_wanted(void);
165static int pum_enough_matches(void);
166static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
167static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
168static char_u *find_line_end(char_u *ptr);
169static void ins_compl_free(void);
170static void ins_compl_clear(void);
171static int ins_compl_bs(void);
172static int ins_compl_need_restart(void);
173static void ins_compl_new_leader(void);
174static void ins_compl_addleader(int c);
175static int ins_compl_len(void);
176static void ins_compl_restart(void);
177static void ins_compl_set_original_text(char_u *str);
178static void ins_compl_addfrommatch(void);
179static int ins_compl_prep(int c);
180static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
181static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100182# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100183static void ins_compl_add_list(list_T *list);
184static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100185# endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static int ins_compl_get_exp(pos_T *ini);
187static void ins_compl_delete(void);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200188static void ins_compl_insert(int in_compl_func);
189static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static int ins_compl_key2dir(int c);
191static int ins_compl_pum_key(int c);
192static int ins_compl_key2count(int c);
193static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100194static int ins_complete(int c, int enable_pum);
Bram Moolenaarcb036422017-03-01 12:29:10 +0100195static void show_pum(int prev_w_wrow, int prev_w_leftcol);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100196static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif /* FEAT_INS_EXPAND */
198
199#define BACKSPACE_CHAR 1
200#define BACKSPACE_WORD 2
201#define BACKSPACE_WORD_NOT_SPACE 3
202#define BACKSPACE_LINE 4
203
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ins_redraw(int ready);
205static void ins_ctrl_v(void);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200206#ifdef FEAT_JOB_CHANNEL
207static void init_prompt(int cmdchar_todo);
208#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void undisplay_dollar(void);
210static void insert_special(int, int, int);
211static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
212static void check_auto_format(int);
213static void redo_literal(int c);
214static void start_arrow(pos_T *end_insert_pos);
215static void start_arrow_with_change(pos_T *end_insert_pos, int change);
216static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000217#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100218static void check_spell_redraw(void);
219static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000220static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000221#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
223static int echeck_abbr(int);
224static int replace_pop(void);
225static void replace_join(int off);
226static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void replace_flush(void);
231static void replace_do_bs(int limit_col);
232static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ins_reg(void);
237static void ins_ctrl_g(void);
238static void ins_ctrl_hat(void);
239static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static int ins_start_select(int c);
244static void ins_insert(int replaceState);
245static void ins_ctrl_o(void);
246static void ins_shift(int c, int lastc);
247static void ins_del(void);
248static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ins_mouse(int c);
251static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000253#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000255#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ins_left(int end_change);
257static void ins_home(int c);
258static void ins_end(int c);
259static void ins_s_left(void);
260static void ins_right(int end_change);
261static void ins_s_right(void);
262static void ins_up(int startcol);
263static void ins_pageup(void);
264static void ins_down(int startcol);
265static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static int ins_tab(void);
270static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100272static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100274static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100276static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100278static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100279#if defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100280static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
283static colnr_T Insstart_textlen; /* length of line when insert started */
284static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100285static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286
287static char_u *last_insert = NULL; /* the text of the previous insert,
288 K_SPECIAL and CSI are escaped */
289static int last_insert_skip; /* nr of chars in front of previous insert */
290static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000291static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
293#ifdef FEAT_CINDENT
294static int can_cindent; /* may do cindenting on this line */
295#endif
296
297static int old_indent = 0; /* for ^^D command in insert mode */
298
299#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000300static int revins_on; /* reverse insert mode on */
301static int revins_chars; /* how much to skip after edit */
302static int revins_legal; /* was the last char 'legal'? */
303static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#endif
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static int ins_need_undo; /* call u_save() before inserting a
307 char. Set when edit() is called.
308 after that arrow_used is used. */
309
310static int did_add_space = FALSE; /* auto_format() added an extra space
311 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200312static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
313 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * edit(): Start inserting text.
317 *
318 * "cmdchar" can be:
319 * 'i' normal insert command
320 * 'a' normal append command
Bram Moolenaarec2da362017-01-21 20:04:22 +0100321 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 * 'R' replace command
323 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
324 * but still only one <CR> is inserted. The <Esc> is not used for redo.
325 * 'g' "gI" command.
326 * 'V' "gR" command for Virtual Replace mode.
327 * 'v' "gr" command for single character Virtual Replace mode.
328 *
329 * This function is not called recursively. For CTRL-O commands, it returns
330 * and lets the caller handle the Normal-mode command.
331 *
332 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
333 */
334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100335edit(
336 int cmdchar,
337 int startln, /* if set, insert at start of line */
338 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 int c = 0;
341 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100342 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000343 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 int i;
346 int did_backspace = TRUE; /* previous char was backspace */
347#ifdef FEAT_CINDENT
348 int line_is_white = FALSE; /* line is empty before insert */
349#endif
350 linenr_T old_topline = 0; /* topline before insertion */
351#ifdef FEAT_DIFF
352 int old_topfill = -1;
353#endif
354 int inserted_space = FALSE; /* just inserted a space */
355 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000356 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaarf2732452018-06-03 14:47:35 +0200357#ifdef FEAT_JOB_CHANNEL
358 int cmdchar_todo = cmdchar;
359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000361 /* Remember whether editing was restarted after CTRL-O. */
362 did_restart_edit = restart_edit;
363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 /* sleep before redrawing, needed for "CTRL-O :" that results in an
365 * error message */
366 check_for_delay(TRUE);
367
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100368 /* set Insstart_orig to Insstart */
369 update_Insstart_orig = TRUE;
370
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371#ifdef HAVE_SANDBOX
372 /* Don't allow inserting in the sandbox. */
373 if (sandbox != 0)
374 {
375 EMSG(_(e_sandbox));
376 return FALSE;
377 }
378#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000379 /* Don't allow changes in the buffer while editing the cmdline. The
380 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000381 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000382 {
383 EMSG(_(e_secure));
384 return FALSE;
385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
387#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000388 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000389 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000390 {
391 EMSG(_(e_secure));
392 return FALSE;
393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 ins_compl_clear(); /* clear stuff for CTRL-X mode */
395#endif
396
Bram Moolenaar843ee412004-06-30 16:16:41 +0000397 /*
398 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
399 */
400 if (cmdchar != 'r' && cmdchar != 'v')
401 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100402 pos_T save_cursor = curwin->w_cursor;
403
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100404#ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000405 if (cmdchar == 'R')
406 ptr = (char_u *)"r";
407 else if (cmdchar == 'V')
408 ptr = (char_u *)"v";
409 else
410 ptr = (char_u *)"i";
411 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200412 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100413#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000414 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100415
Bram Moolenaar097c9922013-05-19 21:15:15 +0200416 /* Make sure the cursor didn't move. Do call check_cursor_col() in
417 * case the text was modified. Since Insert mode was not started yet
418 * a call to check_cursor_col() may move the cursor, especially with
419 * the "A" command, thus set State to avoid that. Also check that the
420 * line number is still valid (lines may have been deleted).
421 * Do not restore if v:char was set to a non-empty string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100422 if (!EQUAL_POS(curwin->w_cursor, save_cursor)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100423#ifdef FEAT_EVAL
Bram Moolenaar097c9922013-05-19 21:15:15 +0200424 && *get_vim_var_str(VV_CHAR) == NUL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100425#endif
Bram Moolenaar097c9922013-05-19 21:15:15 +0200426 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100427 {
428 int save_state = State;
429
430 curwin->w_cursor = save_cursor;
431 State = INSERT;
432 check_cursor_col();
433 State = save_state;
434 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000435 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000436
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200437#ifdef FEAT_CONCEAL
438 /* Check if the cursor line needs redrawing before changing State. If
439 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaarb9464822018-05-10 15:09:49 +0200440 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200441#endif
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#ifdef FEAT_MOUSE
444 /*
445 * When doing a paste with the middle mouse button, Insstart is set to
446 * where the paste started.
447 */
448 if (where_paste_started.lnum != 0)
449 Insstart = where_paste_started;
450 else
451#endif
452 {
453 Insstart = curwin->w_cursor;
454 if (startln)
455 Insstart.col = 0;
456 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000457 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 Insstart_blank_vcol = MAXCOL;
459 if (!did_ai)
460 ai_col = 0;
461
462 if (cmdchar != NUL && restart_edit == 0)
463 {
464 ResetRedobuff();
465 AppendNumberToRedobuff(count);
466#ifdef FEAT_VREPLACE
467 if (cmdchar == 'V' || cmdchar == 'v')
468 {
469 /* "gR" or "gr" command */
470 AppendCharToRedobuff('g');
471 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
472 }
473 else
474#endif
475 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100476 if (cmdchar == K_PS)
477 AppendCharToRedobuff('a');
478 else
479 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 if (cmdchar == 'g') /* "gI" command */
481 AppendCharToRedobuff('I');
482 else if (cmdchar == 'r') /* "r<CR>" command */
483 count = 1; /* insert only one <CR> */
484 }
485 }
486
487 if (cmdchar == 'R')
488 {
489#ifdef FEAT_FKMAP
490 if (p_fkmap && p_ri)
491 {
492 beep_flush();
493 EMSG(farsi_text_3); /* encoded in Farsi */
494 State = INSERT;
495 }
496 else
497#endif
498 State = REPLACE;
499 }
500#ifdef FEAT_VREPLACE
501 else if (cmdchar == 'V' || cmdchar == 'v')
502 {
503 State = VREPLACE;
504 replaceState = VREPLACE;
505 orig_line_count = curbuf->b_ml.ml_line_count;
506 vr_lines_changed = 1;
507 }
508#endif
509 else
510 State = INSERT;
511
512 stop_insert_mode = FALSE;
513
514 /*
515 * Need to recompute the cursor position, it might move when the cursor is
516 * on a TAB or special character.
517 */
518 curs_columns(TRUE);
519
520 /*
521 * Enable langmap or IME, indicated by 'iminsert'.
522 * Note that IME may enabled/disabled without us noticing here, thus the
523 * 'iminsert' value may not reflect what is actually used. It is updated
524 * when hitting <Esc>.
525 */
526 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
527 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100528#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532#ifdef FEAT_MOUSE
533 setmouse();
534#endif
535#ifdef FEAT_CMDL_INFO
536 clear_showcmd();
537#endif
538#ifdef FEAT_RIGHTLEFT
539 /* there is no reverse replace mode */
540 revins_on = (State == INSERT && p_ri);
541 if (revins_on)
542 undisplay_dollar();
543 revins_chars = 0;
544 revins_legal = 0;
545 revins_scol = -1;
546#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +0100547 if (!p_ek)
548 /* Disable bracketed paste mode, we won't recognize the escape
549 * sequences. */
550 out_str(T_BD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552 /*
553 * Handle restarting Insert mode.
Bram Moolenaara6c07602017-03-05 21:18:27 +0100554 * Don't do this for "CTRL-O ." (repeat an insert): In that case we get
555 * here with something in the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 */
557 if (restart_edit != 0 && stuff_empty())
558 {
559#ifdef FEAT_MOUSE
560 /*
561 * After a paste we consider text typed to be part of the insert for
562 * the pasted text. You can backspace over the pasted text too.
563 */
564 if (where_paste_started.lnum)
565 arrow_used = FALSE;
566 else
567#endif
568 arrow_used = TRUE;
569 restart_edit = 0;
570
571 /*
572 * If the cursor was after the end-of-line before the CTRL-O and it is
573 * now at the end-of-line, put it after the end-of-line (this is not
574 * correct in very rare cases).
575 * Also do this if curswant is greater than the current virtual
576 * column. Eg after "^O$" or "^O80|".
577 */
578 validate_virtcol();
579 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000580 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 || curwin->w_curswant > curwin->w_virtcol)
582 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
583 {
584 if (ptr[1] == NUL)
585 ++curwin->w_cursor.col;
586#ifdef FEAT_MBYTE
587 else if (has_mbyte)
588 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000589 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (ptr[i] == NUL)
591 curwin->w_cursor.col += i;
592 }
593#endif
594 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000595 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 else
598 arrow_used = FALSE;
599
600 /* we are in insert mode now, don't need to start it anymore */
601 need_start_insertmode = FALSE;
602
603 /* Need to save the line for undo before inserting the first char. */
604 ins_need_undo = TRUE;
605
606#ifdef FEAT_MOUSE
607 where_paste_started.lnum = 0;
608#endif
609#ifdef FEAT_CINDENT
610 can_cindent = TRUE;
611#endif
612#ifdef FEAT_FOLDING
613 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
614 * restarting. */
615 if (!p_im && did_restart_edit == 0)
616 foldOpenCursor();
617#endif
618
619 /*
620 * If 'showmode' is set, show the current (insert/replace/..) mode.
621 * A warning message for changing a readonly file is given here, before
622 * actually changing anything. It's put after the mode, if any.
623 */
624 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000625 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 i = showmode();
627
628 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000629 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631#ifdef CURSOR_SHAPE
632 ui_cursor_shape(); /* may show different cursor shape */
633#endif
634#ifdef FEAT_DIGRAPHS
635 do_digraph(-1); /* clear digraphs */
636#endif
637
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000638 /*
639 * Get the current length of the redo buffer, those characters have to be
640 * skipped if we want to get to the inserted characters.
641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 ptr = get_inserted();
643 if (ptr == NULL)
644 new_insert_skip = 0;
645 else
646 {
647 new_insert_skip = (int)STRLEN(ptr);
648 vim_free(ptr);
649 }
650
651 old_indent = 0;
652
653 /*
654 * Main loop in Insert mode: repeat until Insert mode is left.
655 */
656 for (;;)
657 {
658#ifdef FEAT_RIGHTLEFT
659 if (!revins_legal)
660 revins_scol = -1; /* reset on illegal motions */
661 else
662 revins_legal = 0;
663#endif
664 if (arrow_used) /* don't repeat insert when arrow key used */
665 count = 0;
666
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100667 if (update_Insstart_orig)
668 Insstart_orig = Insstart;
669
Bram Moolenaar00672e12016-06-26 18:38:13 +0200670 if (stop_insert_mode
671#ifdef FEAT_INS_EXPAND
672 && !pum_visible()
673#endif
674 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
676 /* ":stopinsert" used or 'insertmode' reset */
677 count = 0;
678 goto doESCkey;
679 }
680
681 /* set curwin->w_curswant for next K_DOWN or K_UP */
682 if (!arrow_used)
683 curwin->w_set_curswant = TRUE;
684
685 /* If there is no typeahead may check for timestamps (e.g., for when a
686 * menu invoked a shell command). */
687 if (stuff_empty())
688 {
689 did_check_timestamps = FALSE;
690 if (need_check_timestamps)
691 check_timestamps(FALSE);
692 }
693
694 /*
695 * When emsg() was called msg_scroll will have been set.
696 */
697 msg_scroll = FALSE;
698
699#ifdef FEAT_GUI
700 /* When 'mousefocus' is set a mouse movement may have taken us to
701 * another window. "need_mouse_correct" may then be set because of an
702 * autocommand. */
703 if (need_mouse_correct)
704 gui_mouse_correct();
705#endif
706
707#ifdef FEAT_FOLDING
708 /* Open fold at the cursor line, according to 'foldopen'. */
709 if (fdo_flags & FDO_INSERT)
710 foldOpenCursor();
711 /* Close folds where the cursor isn't, according to 'foldclose' */
712 if (!char_avail())
713 foldCheckClose();
714#endif
715
Bram Moolenaarf2732452018-06-03 14:47:35 +0200716#ifdef FEAT_JOB_CHANNEL
717 if (bt_prompt(curbuf))
718 {
719 init_prompt(cmdchar_todo);
720 cmdchar_todo = NUL;
721 }
722#endif
723
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 /*
725 * If we inserted a character at the last position of the last line in
726 * the window, scroll the window one line up. This avoids an extra
727 * redraw.
728 * This is detected when the cursor column is smaller after inserting
729 * something.
730 * Don't do this when the topline changed already, it has
731 * already been adjusted (by insertchar() calling open_line())).
732 */
733 if (curbuf->b_mod_set
734 && curwin->w_p_wrap
735 && !did_backspace
736 && curwin->w_topline == old_topline
737#ifdef FEAT_DIFF
738 && curwin->w_topfill == old_topfill
739#endif
740 )
741 {
742 mincol = curwin->w_wcol;
743 validate_cursor_col();
744
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000745 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 && curwin->w_wrow == W_WINROW(curwin)
747 + curwin->w_height - 1 - p_so
748 && (curwin->w_cursor.lnum != curwin->w_topline
749#ifdef FEAT_DIFF
750 || curwin->w_topfill > 0
751#endif
752 ))
753 {
754#ifdef FEAT_DIFF
755 if (curwin->w_topfill > 0)
756 --curwin->w_topfill;
757 else
758#endif
759#ifdef FEAT_FOLDING
760 if (hasFolding(curwin->w_topline, NULL, &old_topline))
761 set_topline(curwin, old_topline + 1);
762 else
763#endif
764 set_topline(curwin, curwin->w_topline + 1);
765 }
766 }
767
768 /* May need to adjust w_topline to show the cursor. */
769 update_topline();
770
771 did_backspace = FALSE;
772
773 validate_cursor(); /* may set must_redraw */
774
775 /*
776 * Redraw the display when no characters are waiting.
777 * Also shows mode, ruler and positions cursor.
778 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000779 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (curwin->w_p_scb)
782 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar860cae12010-06-05 23:22:07 +0200784 if (curwin->w_p_crb)
785 do_check_cursorbind();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 update_curswant();
787 old_topline = curwin->w_topline;
788#ifdef FEAT_DIFF
789 old_topfill = curwin->w_topfill;
790#endif
791
792#ifdef USE_ON_FLY_SCROLL
793 dont_scroll = FALSE; /* allow scrolling here */
794#endif
795
796 /*
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100797 * Get a character for Insert mode. Ignore K_IGNORE and K_NOP.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100799 if (c != K_CURSORHOLD)
800 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200801
802 /* After using CTRL-G U the next cursor key will not break undo. */
803 if (dont_sync_undo == MAYBE)
804 dont_sync_undo = TRUE;
805 else
806 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100807 if (cmdchar == K_PS)
808 /* Got here from normal mode when bracketed paste started. */
809 c = K_PS;
810 else
811 do
812 {
813 c = safe_vgetc();
Bram Moolenaar6d41c782018-06-06 09:11:12 +0200814
815 if (stop_insert_mode)
816 {
817 // Insert mode ended, possibly from a callback.
818 count = 0;
819 nomove = TRUE;
820 goto doESCkey;
821 }
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100822 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000824 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
825 did_cursorhold = TRUE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827#ifdef FEAT_RIGHTLEFT
828 if (p_hkmap && KeyTyped)
829 c = hkmap(c); /* Hebrew mode mapping */
830#endif
831#ifdef FEAT_FKMAP
832 if (p_fkmap && KeyTyped)
833 c = fkmap(c); /* Farsi mode mapping */
834#endif
835
836#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000837 /*
838 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000839 * and the cursor is still in the completed word. Only when there is
840 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000841 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000842 if (compl_started
843 && pum_wanted()
844 && curwin->w_cursor.col >= compl_col
845 && (compl_shown_match == NULL
846 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000847 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000848 /* BS: Delete one character from "compl_leader". */
849 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000850 && curwin->w_cursor.col > compl_col
851 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000852 continue;
853
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000854 /* When no match was selected or it was edited. */
855 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000856 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000857 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000858 * "compl_leader". Except when at the original match and
859 * there is nothing to add, CTRL-L works like CTRL-P then. */
860 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100861 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000862 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000863 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000864 {
865 ins_compl_addfrommatch();
866 continue;
867 }
868
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000869 /* A non-white character that fits in with the current
870 * completion: Add to "compl_leader". */
871 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000872 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100873#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100874 /* Trigger InsertCharPre. */
875 char_u *str = do_insert_char_pre(c);
876 char_u *p;
877
878 if (str != NULL)
879 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100880 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100881 ins_compl_addleader(PTR2CHAR(p));
882 vim_free(str);
883 }
884 else
885#endif
886 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000887 continue;
888 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000889
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000890 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000891 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200892 if ((c == Ctrl_Y || (compl_enter_selects
893 && (c == CAR || c == K_KENTER || c == NL)))
894 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000895 {
896 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200897 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000898 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000899 }
900 }
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
903 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000904 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000905 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000906 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908
Bram Moolenaar488c6512005-08-11 20:09:58 +0000909 /* CTRL-\ CTRL-N goes to Normal mode,
910 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
911 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (c == Ctrl_BSL)
913 {
914 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000915 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 ++no_mapping;
917 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000918 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 --no_mapping;
920 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000921 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000923 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 vungetc(c);
925 c = Ctrl_BSL;
926 }
927 else if (c == Ctrl_G && p_im)
928 continue;
929 else
930 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000931 if (c == Ctrl_O)
932 {
933 ins_ctrl_o();
934 ins_at_eol = FALSE; /* cursor keeps its column */
935 nomove = TRUE;
936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 count = 0;
938 goto doESCkey;
939 }
940 }
941
942#ifdef FEAT_DIGRAPHS
943 c = do_digraph(c);
944#endif
945
946#ifdef FEAT_INS_EXPAND
947 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
948 goto docomplete;
949#endif
950 if (c == Ctrl_V || c == Ctrl_Q)
951 {
952 ins_ctrl_v();
953 c = Ctrl_V; /* pretend CTRL-V is last typed character */
954 continue;
955 }
956
957#ifdef FEAT_CINDENT
958 if (cindent_on()
959# ifdef FEAT_INS_EXPAND
960 && ctrl_x_mode == 0
961# endif
962 )
963 {
964 /* A key name preceded by a bang means this key is not to be
965 * inserted. Skip ahead to the re-indenting below.
966 * A key name preceded by a star means that indenting has to be
967 * done before inserting the key. */
968 line_is_white = inindent(0);
969 if (in_cinkeys(c, '!', line_is_white))
970 goto force_cindent;
971 if (can_cindent && in_cinkeys(c, '*', line_is_white)
972 && stop_arrow() == OK)
973 do_c_expr_indent();
974 }
975#endif
976
977#ifdef FEAT_RIGHTLEFT
978 if (curwin->w_p_rl)
979 switch (c)
980 {
981 case K_LEFT: c = K_RIGHT; break;
982 case K_S_LEFT: c = K_S_RIGHT; break;
983 case K_C_LEFT: c = K_C_RIGHT; break;
984 case K_RIGHT: c = K_LEFT; break;
985 case K_S_RIGHT: c = K_S_LEFT; break;
986 case K_C_RIGHT: c = K_C_LEFT; break;
987 }
988#endif
989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 /*
991 * If 'keymodel' contains "startsel", may start selection. If it
992 * does, a CTRL-O and c will be stuffed, we need to get these
993 * characters.
994 */
995 if (ins_start_select(c))
996 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998 /*
999 * The big switch to handle a character in insert mode.
1000 */
1001 switch (c)
1002 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001003 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (echeck_abbr(ESC + ABBR_OFF))
1005 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001006 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009#ifdef FEAT_CMDWIN
1010 if (c == Ctrl_C && cmdwin_type != 0)
1011 {
1012 /* Close the cmdline window. */
1013 cmdwin_result = K_IGNORE;
1014 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +00001015 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 goto doESCkey;
1017 }
1018#endif
1019
1020#ifdef UNIX
1021do_intr:
1022#endif
1023 /* when 'insertmode' set, and not halfway a mapping, don't leave
1024 * Insert mode */
1025 if (goto_im())
1026 {
1027 if (got_int)
1028 {
1029 (void)vgetc(); /* flush all buffers */
1030 got_int = FALSE;
1031 }
1032 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001033 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 break;
1035 }
1036doESCkey:
1037 /*
1038 * This is the ONLY return from edit()!
1039 */
1040 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1041 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001042 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 o_lnum = curwin->w_cursor.lnum;
1044
Bram Moolenaar488c6512005-08-11 20:09:58 +00001045 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001046 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00001047 if (cmdchar != 'r' && cmdchar != 'v')
1048 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1049 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001050 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 continue;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case Ctrl_Z: /* suspend when 'insertmode' set */
1056 if (!p_im)
1057 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001058 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar74a47162017-02-26 19:09:05 +01001059#ifdef CURSOR_SHAPE
1060 ui_cursor_shape(); /* may need to update cursor shape */
1061#endif
1062 continue;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063
1064 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001065#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001066 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 goto docomplete;
1068#endif
1069 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1070 break;
1071 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001072
1073#ifdef FEAT_VIRTUALEDIT
1074 /* don't move the cursor left when 'virtualedit' has "onemore". */
1075 if (ve_flags & VE_ONEMORE)
1076 {
1077 ins_at_eol = FALSE;
1078 nomove = TRUE;
1079 }
1080#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001081 count = 0;
1082 goto doESCkey;
1083
Bram Moolenaar572cb562005-08-05 21:35:02 +00001084 case K_INS: /* toggle insert/replace mode */
1085 case K_KINS:
1086 ins_insert(replaceState);
1087 break;
1088
1089 case K_SELECT: /* end of Select mode mapping - ignore */
1090 break;
1091
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 case K_HELP: /* Help key works like <ESC> <Help> */
1093 case K_F1:
1094 case K_XF1:
1095 stuffcharReadbuff(K_HELP);
1096 if (p_im)
1097 need_start_insertmode = TRUE;
1098 goto doESCkey;
1099
1100#ifdef FEAT_NETBEANS_INTG
1101 case K_F21: /* NetBeans command */
1102 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001103 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 --no_mapping;
1105 netbeans_keycommand(i);
1106 break;
1107#endif
1108
1109 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 case NUL:
1111 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 /* For ^@ the trailing ESC will end the insert, unless there is an
1113 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1115 && c != Ctrl_A && !p_im)
1116 goto doESCkey; /* quit insert mode */
1117 inserted_space = FALSE;
1118 break;
1119
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 ins_reg();
1122 auto_format(FALSE, TRUE);
1123 inserted_space = FALSE;
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ins_ctrl_g();
1128 break;
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case Ctrl_HAT: /* switch input mode and/or langmap */
1131 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 break;
1133
1134#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 if (!p_ari)
1137 goto normalchar;
1138 ins_ctrl_();
1139 break;
1140#endif
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1144 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1145 goto docomplete;
1146#endif
1147 /* FALLTHROUGH */
1148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150# ifdef FEAT_INS_EXPAND
1151 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1152 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153 if (has_compl_option(FALSE))
1154 goto docomplete;
1155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157# endif
1158 ins_shift(c, lastc);
1159 auto_format(FALSE, TRUE);
1160 inserted_space = FALSE;
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 case K_KDEL:
1165 ins_del();
1166 auto_format(FALSE, TRUE);
1167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 case Ctrl_H:
1171 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1172 auto_format(FALSE, TRUE);
1173 break;
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001176#ifdef FEAT_JOB_CHANNEL
1177 if (bt_prompt(curbuf) && (mod_mask & MOD_MASK_SHIFT) == 0)
1178 {
1179 // In a prompt window CTRL-W is used for window commands.
1180 // Use Shift-CTRL-W to delete a word.
1181 stuffcharReadbuff(Ctrl_W);
1182 restart_edit = 'i';
1183 nomove = TRUE;
1184 count = 0;
1185 goto doESCkey;
1186 }
1187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1189 auto_format(FALSE, TRUE);
1190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001193# ifdef FEAT_COMPL_FUNC
1194 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001195 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001196 goto docomplete;
1197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1199 auto_format(FALSE, TRUE);
1200 inserted_space = FALSE;
1201 break;
1202
1203#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 case K_LEFTMOUSE_NM:
1206 case K_LEFTDRAG:
1207 case K_LEFTRELEASE:
1208 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001209 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 case K_MIDDLEMOUSE:
1211 case K_MIDDLEDRAG:
1212 case K_MIDDLERELEASE:
1213 case K_RIGHTMOUSE:
1214 case K_RIGHTDRAG:
1215 case K_RIGHTRELEASE:
1216 case K_X1MOUSE:
1217 case K_X1DRAG:
1218 case K_X1RELEASE:
1219 case K_X2MOUSE:
1220 case K_X2DRAG:
1221 case K_X2RELEASE:
1222 ins_mouse(c);
1223 break;
1224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001226 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 break;
1228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001230 ins_mousescroll(MSCR_UP);
1231 break;
1232
1233 case K_MOUSELEFT: /* Scroll wheel left */
1234 ins_mousescroll(MSCR_LEFT);
1235 break;
1236
1237 case K_MOUSERIGHT: /* Scroll wheel right */
1238 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 break;
1240#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001241 case K_PS:
1242 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1243 if (cmdchar == K_PS)
1244 /* invoked from normal mode, bail out */
1245 goto doESCkey;
1246 break;
1247 case K_PE:
1248 /* Got K_PE without K_PS, ignore. */
1249 break;
1250
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001251#ifdef FEAT_GUI_TABLINE
1252 case K_TABLINE:
1253 case K_TABMENU:
1254 ins_tabline(c);
1255 break;
1256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 break;
1260
Bram Moolenaar754b5602006-02-09 23:53:20 +00001261 case K_CURSORHOLD: /* Didn't type something for a while. */
1262 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1263 did_cursorhold = TRUE;
1264 break;
Bram Moolenaar754b5602006-02-09 23:53:20 +00001265
Bram Moolenaar4770d092006-01-12 23:22:24 +00001266#ifdef FEAT_GUI_W32
1267 /* On Win32 ignore <M-F4>, we get it when closing the window was
1268 * cancelled. */
1269 case K_F4:
1270 if (mod_mask != MOD_MASK_ALT)
1271 goto normalchar;
1272 break;
1273#endif
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#ifdef FEAT_GUI
1276 case K_VER_SCROLLBAR:
1277 ins_scroll();
1278 break;
1279
1280 case K_HOR_SCROLLBAR:
1281 ins_horscroll();
1282 break;
1283#endif
1284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 case K_S_HOME:
1288 case K_C_HOME:
1289 ins_home(c);
1290 break;
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 case K_S_END:
1295 case K_C_END:
1296 ins_end(c);
1297 break;
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001300 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1301 ins_s_left();
1302 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001303 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 break;
1305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001306 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 case K_C_LEFT:
1308 ins_s_left();
1309 break;
1310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001311 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001312 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1313 ins_s_right();
1314 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001315 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 break;
1317
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001318 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 case K_C_RIGHT:
1320 ins_s_right();
1321 break;
1322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001323 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001324#ifdef FEAT_INS_EXPAND
1325 if (pum_visible())
1326 goto docomplete;
1327#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001328 if (mod_mask & MOD_MASK_SHIFT)
1329 ins_pageup();
1330 else
1331 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 case K_PAGEUP:
1336 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001337#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001338 if (pum_visible())
1339 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ins_pageup();
1342 break;
1343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001345#ifdef FEAT_INS_EXPAND
1346 if (pum_visible())
1347 goto docomplete;
1348#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001349 if (mod_mask & MOD_MASK_SHIFT)
1350 ins_pagedown();
1351 else
1352 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 break;
1354
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001355 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 case K_PAGEDOWN:
1357 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001358#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001359 if (pum_visible())
1360 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 ins_pagedown();
1363 break;
1364
1365#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001366 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ins_drop();
1368 break;
1369#endif
1370
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001371 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 c = TAB;
1373 /* FALLTHROUGH */
1374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001375 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1377 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1378 goto docomplete;
1379#endif
1380 inserted_space = FALSE;
1381 if (ins_tab())
1382 goto normalchar; /* insert TAB as a normal char */
1383 auto_format(FALSE, TRUE);
1384 break;
1385
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001386 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 c = CAR;
1388 /* FALLTHROUGH */
1389 case CAR:
1390 case NL:
Bram Moolenaar4033c552017-09-16 20:54:51 +02001391#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 /* In a quickfix window a <CR> jumps to the error under the
1393 * cursor. */
1394 if (bt_quickfix(curbuf) && c == CAR)
1395 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001396 if (curwin->w_llist_ref == NULL) /* quickfix window */
1397 do_cmdline_cmd((char_u *)".cc");
1398 else /* location list window */
1399 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 break;
1401 }
1402#endif
1403#ifdef FEAT_CMDWIN
1404 if (cmdwin_type != 0)
1405 {
1406 /* Execute the command in the cmdline window. */
1407 cmdwin_result = CAR;
1408 goto doESCkey;
1409 }
1410#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001411#ifdef FEAT_JOB_CHANNEL
1412 if (bt_prompt(curbuf))
1413 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02001414 invoke_prompt_callback();
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001415 if (!bt_prompt(curbuf))
1416 // buffer changed to a non-prompt buffer, get out of
1417 // Insert mode
Bram Moolenaarf2732452018-06-03 14:47:35 +02001418 goto doESCkey;
1419 break;
1420 }
1421#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001422 if (ins_eol(c) == FAIL && !p_im)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 goto doESCkey; /* out of memory */
1424 auto_format(FALSE, FALSE);
1425 inserted_space = FALSE;
1426 break;
1427
Bram Moolenaar860cae12010-06-05 23:22:07 +02001428#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001429 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430# ifdef FEAT_INS_EXPAND
1431 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1432 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001433 if (has_compl_option(TRUE))
1434 goto docomplete;
1435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437# endif
1438# ifdef FEAT_DIGRAPHS
1439 c = ins_digraph();
1440 if (c == NUL)
1441 break;
1442# endif
1443 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001447 case Ctrl_X: /* Enter CTRL-X mode */
1448 ins_ctrl_x();
1449 break;
1450
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001451 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (ctrl_x_mode != CTRL_X_TAGS)
1453 goto normalchar;
1454 goto docomplete;
1455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001456 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (ctrl_x_mode != CTRL_X_FILES)
1458 goto normalchar;
1459 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001460
1461 case 's': /* Spelling completion after ^X */
1462 case Ctrl_S:
1463 if (ctrl_x_mode != CTRL_X_SPELL)
1464 goto normalchar;
1465 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
1467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001468 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#ifdef FEAT_INS_EXPAND
1470 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1471#endif
1472 {
1473 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1474 if (p_im)
1475 {
1476 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1477 break;
1478 goto doESCkey;
1479 }
1480 goto normalchar;
1481 }
1482#ifdef FEAT_INS_EXPAND
1483 /* FALLTHROUGH */
1484
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001485 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 case Ctrl_N:
1487 /* if 'complete' is empty then plain ^P is no longer special,
1488 * but it is under other ^X modes */
1489 if (*curbuf->b_p_cpt == NUL
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001490 && (ctrl_x_mode == CTRL_X_NORMAL
1491 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001492 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 goto normalchar;
1494
1495docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001496 compl_busy = TRUE;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001497#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001498 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001499#endif
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001500 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001501 compl_cont_status = 0;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001502#ifdef FEAT_FOLDING
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001503 disable_fold_update--;
Bram Moolenaara6c07602017-03-05 21:18:27 +01001504#endif
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001505 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 break;
1507#endif /* FEAT_INS_EXPAND */
1508
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001509 case Ctrl_Y: /* copy from previous line or scroll down */
1510 case Ctrl_E: /* copy from next line or scroll up */
1511 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 break;
1513
1514 default:
1515#ifdef UNIX
1516 if (c == intr_char) /* special interrupt char */
1517 goto do_intr;
1518#endif
1519
Bram Moolenaare659c952011-05-19 17:25:41 +02001520normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001522 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001524#if defined(FEAT_EVAL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001525 if (!p_paste)
1526 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001527 /* Trigger InsertCharPre. */
1528 char_u *str = do_insert_char_pre(c);
1529 char_u *p;
1530
1531 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001532 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001533 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001534 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001535 /* Insert the new value of v:char literally. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001536 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001537 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001538 c = PTR2CHAR(p);
1539 if (c == CAR || c == K_KENTER || c == NL)
1540 ins_eol(c);
1541 else
1542 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001543 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001544 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001545 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001546 vim_free(str);
1547 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001548 }
1549
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001550 /* If the new value is already inserted or an empty string
1551 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001552 if (c == NUL)
1553 break;
1554 }
1555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#ifdef FEAT_SMARTINDENT
1557 /* Try to perform smart-indenting. */
1558 ins_try_si(c);
1559#endif
1560
1561 if (c == ' ')
1562 {
1563 inserted_space = TRUE;
1564#ifdef FEAT_CINDENT
1565 if (inindent(0))
1566 can_cindent = FALSE;
1567#endif
1568 if (Insstart_blank_vcol == MAXCOL
1569 && curwin->w_cursor.lnum == Insstart.lnum)
1570 Insstart_blank_vcol = get_nolist_virtcol();
1571 }
1572
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001573 /* Insert a normal character and check for abbreviations on a
1574 * special character. Let CTRL-] expand abbreviations without
1575 * inserting it. */
1576 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577#ifdef FEAT_MBYTE
1578 /* Add ABBR_OFF for characters above 0x100, this is
1579 * what check_abbr() expects. */
1580 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1581#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001582 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 insert_special(c, FALSE, FALSE);
1585#ifdef FEAT_RIGHTLEFT
1586 revins_legal++;
1587 revins_chars++;
1588#endif
1589 }
1590
1591 auto_format(FALSE, TRUE);
1592
1593#ifdef FEAT_FOLDING
1594 /* When inserting a character the cursor line must never be in a
1595 * closed fold. */
1596 foldOpenCursor();
1597#endif
1598 break;
1599 } /* end of switch (c) */
1600
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001601 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001602 if (c != K_CURSORHOLD
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001603#ifdef FEAT_COMPL_FUNC
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001604 /* but not in CTRL-X mode, a script can't restore the state */
1605 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001606#endif
Bram Moolenaar245c4102016-04-20 17:37:41 +02001607 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001608 did_cursorhold = FALSE;
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 /* If the cursor was moved we didn't just insert a space */
1611 if (arrow_used)
1612 inserted_space = FALSE;
1613
1614#ifdef FEAT_CINDENT
1615 if (can_cindent && cindent_on()
1616# ifdef FEAT_INS_EXPAND
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01001617 && ctrl_x_mode == CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618# endif
1619 )
1620 {
1621force_cindent:
1622 /*
1623 * Indent now if a key was typed that is in 'cinkeys'.
1624 */
1625 if (in_cinkeys(c, ' ', line_is_white))
1626 {
1627 if (stop_arrow() == OK)
1628 /* re-indent the current line */
1629 do_c_expr_indent();
1630 }
1631 }
1632#endif /* FEAT_CINDENT */
1633
1634 } /* for (;;) */
1635 /* NOTREACHED */
1636}
1637
1638/*
1639 * Redraw for Insert mode.
1640 * This is postponed until getting the next character to make '$' in the 'cpo'
1641 * option work correctly.
1642 * Only redraw when there are no characters available. This speeds up
1643 * inserting sequences of characters (e.g., for CTRL-R).
1644 */
1645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646ins_redraw(
1647 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001649#ifdef FEAT_CONCEAL
1650 linenr_T conceal_old_cursor_line = 0;
1651 linenr_T conceal_new_cursor_line = 0;
1652 int conceal_update_lines = FALSE;
1653#endif
1654
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001655 if (char_avail())
1656 return;
1657
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001658#if defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001659 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1660 * visible, the command might delete it. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001661 if (ready && (has_cursormovedI()
1662# if defined(FEAT_CONCEAL)
1663 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001664# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001665 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001667# ifdef FEAT_INS_EXPAND
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668 && !pum_visible()
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001669# endif
1670 )
1671 {
1672# ifdef FEAT_SYN_HL
1673 /* Need to update the screen first, to make sure syntax
1674 * highlighting is correct after making a change (e.g., inserting
1675 * a "(". The autocommand may also require a redraw, so it's done
1676 * again below, unfortunately. */
1677 if (syntax_present(curwin) && must_redraw)
1678 update_screen(0);
1679# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001680 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001681 {
1682 /* Make sure curswant is correct, an autocommand may call
1683 * getcurpos(). */
1684 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001685 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001686 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001687# ifdef FEAT_CONCEAL
1688 if (curwin->w_p_cole > 0)
1689 {
1690 conceal_old_cursor_line = last_cursormoved.lnum;
1691 conceal_new_cursor_line = curwin->w_cursor.lnum;
1692 conceal_update_lines = TRUE;
1693 }
1694# endif
1695 last_cursormoved = curwin->w_cursor;
1696 }
1697#endif
1698
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001699 /* Trigger TextChangedI if b_changedtick differs. */
1700 if (ready && has_textchangedI()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001701 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001702#ifdef FEAT_INS_EXPAND
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001703 && !pum_visible()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001704#endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001705 )
1706 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001707 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1708 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001710
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001711#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001712 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1713 * TextChangedI will need to trigger for backwards compatibility, thus use
1714 * different b_last_changedtick* variables. */
1715 if (ready && has_textchangedP()
1716 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1717 && pum_visible())
1718 {
1719 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
1720 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1721 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001722#endif
1723
1724 if (must_redraw)
1725 update_screen(0);
1726 else if (clear_cmdline || redraw_cmdline)
1727 showmode(); /* clear cmdline and show mode */
1728# if defined(FEAT_CONCEAL)
1729 if ((conceal_update_lines
1730 && (conceal_old_cursor_line != conceal_new_cursor_line
1731 || conceal_cursor_line(curwin)))
1732 || need_cursor_line_redraw)
1733 {
1734 if (conceal_old_cursor_line != conceal_new_cursor_line)
1735 update_single_line(curwin, conceal_old_cursor_line);
1736 update_single_line(curwin, conceal_new_cursor_line == 0
1737 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1738 curwin->w_valid &= ~VALID_CROW;
1739 }
1740# endif
1741 showruler(FALSE);
1742 setcursor();
1743 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744}
1745
1746/*
1747 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1748 */
1749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001753 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001756 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001761 did_putchar = TRUE;
1762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1764
1765#ifdef FEAT_CMDL_INFO
1766 add_to_showcmd_c(Ctrl_V);
1767#endif
1768
1769 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001770 if (did_putchar)
1771 /* when the line fits in 'columns' the '^' is at the start of the next
1772 * line and will not removed by the redraw */
1773 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#ifdef FEAT_CMDL_INFO
1775 clear_showcmd();
1776#endif
1777 insert_special(c, FALSE, TRUE);
1778#ifdef FEAT_RIGHTLEFT
1779 revins_chars++;
1780 revins_legal++;
1781#endif
1782}
1783
1784/*
1785 * Put a character directly onto the screen. It's not stored in a buffer.
1786 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1787 */
1788static int pc_status;
1789#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1790#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1791#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1792#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794static int pc_attr;
1795static int pc_row;
1796static int pc_col;
1797
1798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 int attr;
1802
1803 if (ScreenLines != NULL)
1804 {
1805 update_topline(); /* just in case w_topline isn't valid */
1806 validate_cursor();
1807 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001808 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 else
1810 attr = 0;
1811 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001812 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1814 pc_status = PC_STATUS_UNSET;
1815#endif
1816#ifdef FEAT_RIGHTLEFT
1817 if (curwin->w_p_rl)
1818 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001819 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820# ifdef FEAT_MBYTE
1821 if (has_mbyte)
1822 {
1823 int fix_col = mb_fix_col(pc_col, pc_row);
1824
1825 if (fix_col != pc_col)
1826 {
1827 screen_putchar(' ', pc_row, fix_col, attr);
1828 --curwin->w_wcol;
1829 pc_status = PC_STATUS_RIGHT;
1830 }
1831 }
1832# endif
1833 }
1834 else
1835#endif
1836 {
1837 pc_col += curwin->w_wcol;
1838#ifdef FEAT_MBYTE
1839 if (mb_lefthalve(pc_row, pc_col))
1840 pc_status = PC_STATUS_LEFT;
1841#endif
1842 }
1843
1844 /* save the character to be able to put it back */
1845#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1846 if (pc_status == PC_STATUS_UNSET)
1847#endif
1848 {
1849 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1850 pc_status = PC_STATUS_SET;
1851 }
1852 screen_putchar(c, pc_row, pc_col, attr);
1853 }
1854}
1855
Bram Moolenaarf2732452018-06-03 14:47:35 +02001856#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1857/*
1858 * Return the effective prompt for the current buffer.
1859 */
1860 char_u *
1861prompt_text(void)
1862{
1863 if (curbuf->b_prompt_text == NULL)
1864 return (char_u *)"% ";
1865 return curbuf->b_prompt_text;
1866}
1867
1868/*
1869 * Prepare for prompt mode: Make sure the last line has the prompt text.
1870 * Move the cursor to this line.
1871 */
1872 static void
1873init_prompt(int cmdchar_todo)
1874{
1875 char_u *prompt = prompt_text();
1876 char_u *text;
1877
1878 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1879 text = ml_get_curline();
1880 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1881 {
1882 // prompt is missing, insert it or append a line with it
1883 if (*text == NUL)
1884 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1885 else
1886 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1887 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1888 coladvance((colnr_T)MAXCOL);
1889 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1890 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001891
1892 // Insert always starts after the prompt, allow editing text after it.
1893 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1894 || Insstart_orig.col != (int)STRLEN(prompt))
1895 {
1896 Insstart.lnum = curwin->w_cursor.lnum;
1897 Insstart.col = STRLEN(prompt);
1898 Insstart_orig = Insstart;
1899 Insstart_textlen = Insstart.col;
1900 Insstart_blank_vcol = MAXCOL;
1901 arrow_used = FALSE;
1902 }
1903
Bram Moolenaarf2732452018-06-03 14:47:35 +02001904 if (cmdchar_todo == 'A')
1905 coladvance((colnr_T)MAXCOL);
1906 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
1907 curwin->w_cursor.col = STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001908 /* Make sure the cursor is in a valid position. */
1909 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001910}
1911
1912/*
1913 * Return TRUE if the cursor is in the editable position of the prompt line.
1914 */
1915 int
1916prompt_curpos_editable()
1917{
1918 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1919 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1920}
1921#endif
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923/*
1924 * Undo the previous edit_putchar().
1925 */
1926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001927edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1930 {
1931#if defined(FEAT_MBYTE)
1932 if (pc_status == PC_STATUS_RIGHT)
1933 ++curwin->w_wcol;
1934 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1935 redrawWinline(curwin->w_cursor.lnum, FALSE);
1936 else
1937#endif
1938 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1939 }
1940}
1941
1942/*
1943 * Called when p_dollar is set: display a '$' at the end of the changed text
1944 * Only works when cursor is in the line that changes.
1945 */
1946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001947display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 colnr_T save_col;
1950
1951 if (!redrawing())
1952 return;
1953
1954 cursor_off();
1955 save_col = curwin->w_cursor.col;
1956 curwin->w_cursor.col = col;
1957#ifdef FEAT_MBYTE
1958 if (has_mbyte)
1959 {
1960 char_u *p;
1961
1962 /* If on the last byte of a multi-byte move to the first byte. */
1963 p = ml_get_curline();
1964 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1965 }
1966#endif
1967 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001968 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
1970 edit_putchar('$', FALSE);
1971 dollar_vcol = curwin->w_virtcol;
1972 }
1973 curwin->w_cursor.col = save_col;
1974}
1975
1976/*
1977 * Call this function before moving the cursor from the normal insert position
1978 * in insert mode.
1979 */
1980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001981undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001983 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001985 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 redrawWinline(curwin->w_cursor.lnum, FALSE);
1987 }
1988}
1989
1990/*
1991 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1992 * Keep the cursor on the same character.
1993 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1994 * type == INDENT_DEC decrease indent (for CTRL-D)
1995 * type == INDENT_SET set indent to "amount"
1996 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1997 */
1998 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001999change_indent(
2000 int type,
2001 int amount,
2002 int round,
2003 int replaced, /* replaced character, put on replace stack */
2004 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 int vcol;
2007 int last_vcol;
2008 int insstart_less; /* reduction for Insstart.col */
2009 int new_cursor_col;
2010 int i;
2011 char_u *ptr;
2012 int save_p_list;
2013 int start_col;
2014 colnr_T vc;
2015#ifdef FEAT_VREPLACE
2016 colnr_T orig_col = 0; /* init for GCC */
2017 char_u *new_line, *orig_line = NULL; /* init for GCC */
2018
2019 /* VREPLACE mode needs to know what the line was like before changing */
2020 if (State & VREPLACE_FLAG)
2021 {
2022 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2023 orig_col = curwin->w_cursor.col;
2024 }
2025#endif
2026
2027 /* for the following tricks we don't want list mode */
2028 save_p_list = curwin->w_p_list;
2029 curwin->w_p_list = FALSE;
2030 vc = getvcol_nolist(&curwin->w_cursor);
2031 vcol = vc;
2032
2033 /*
2034 * For Replace mode we need to fix the replace stack later, which is only
2035 * possible when the cursor is in the indent. Remember the number of
2036 * characters before the cursor if it's possible.
2037 */
2038 start_col = curwin->w_cursor.col;
2039
2040 /* determine offset from first non-blank */
2041 new_cursor_col = curwin->w_cursor.col;
2042 beginline(BL_WHITE);
2043 new_cursor_col -= curwin->w_cursor.col;
2044
2045 insstart_less = curwin->w_cursor.col;
2046
2047 /*
2048 * If the cursor is in the indent, compute how many screen columns the
2049 * cursor is to the left of the first non-blank.
2050 */
2051 if (new_cursor_col < 0)
2052 vcol = get_indent() - vcol;
2053
2054 if (new_cursor_col > 0) /* can't fix replace stack */
2055 start_col = -1;
2056
2057 /*
2058 * Set the new indent. The cursor will be put on the first non-blank.
2059 */
2060 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002061 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 else
2063 {
2064#ifdef FEAT_VREPLACE
2065 int save_State = State;
2066
2067 /* Avoid being called recursively. */
2068 if (State & VREPLACE_FLAG)
2069 State = INSERT;
2070#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002071 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072#ifdef FEAT_VREPLACE
2073 State = save_State;
2074#endif
2075 }
2076 insstart_less -= curwin->w_cursor.col;
2077
2078 /*
2079 * Try to put cursor on same character.
2080 * If the cursor is at or after the first non-blank in the line,
2081 * compute the cursor column relative to the column of the first
2082 * non-blank character.
2083 * If we are not in insert mode, leave the cursor on the first non-blank.
2084 * If the cursor is before the first non-blank, position it relative
2085 * to the first non-blank, counted in screen columns.
2086 */
2087 if (new_cursor_col >= 0)
2088 {
2089 /*
2090 * When changing the indent while the cursor is touching it, reset
2091 * Insstart_col to 0.
2092 */
2093 if (new_cursor_col == 0)
2094 insstart_less = MAXCOL;
2095 new_cursor_col += curwin->w_cursor.col;
2096 }
2097 else if (!(State & INSERT))
2098 new_cursor_col = curwin->w_cursor.col;
2099 else
2100 {
2101 /*
2102 * Compute the screen column where the cursor should be.
2103 */
2104 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002105 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
2107 /*
2108 * Advance the cursor until we reach the right screen column.
2109 */
2110 vcol = last_vcol = 0;
2111 new_cursor_col = -1;
2112 ptr = ml_get_curline();
2113 while (vcol <= (int)curwin->w_virtcol)
2114 {
2115 last_vcol = vcol;
2116#ifdef FEAT_MBYTE
2117 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002118 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 else
2120#endif
2121 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002122 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124 vcol = last_vcol;
2125
2126 /*
2127 * May need to insert spaces to be able to position the cursor on
2128 * the right screen column.
2129 */
2130 if (vcol != (int)curwin->w_virtcol)
2131 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002132 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002134 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (ptr != NULL)
2136 {
2137 new_cursor_col += i;
2138 ptr[i] = NUL;
2139 while (--i >= 0)
2140 ptr[i] = ' ';
2141 ins_str(ptr);
2142 vim_free(ptr);
2143 }
2144 }
2145
2146 /*
2147 * When changing the indent while the cursor is in it, reset
2148 * Insstart_col to 0.
2149 */
2150 insstart_less = MAXCOL;
2151 }
2152
2153 curwin->w_p_list = save_p_list;
2154
2155 if (new_cursor_col <= 0)
2156 curwin->w_cursor.col = 0;
2157 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002158 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 curwin->w_set_curswant = TRUE;
2160 changed_cline_bef_curs();
2161
2162 /*
2163 * May have to adjust the start of the insert.
2164 */
2165 if (State & INSERT)
2166 {
2167 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2168 {
2169 if ((int)Insstart.col <= insstart_less)
2170 Insstart.col = 0;
2171 else
2172 Insstart.col -= insstart_less;
2173 }
2174 if ((int)ai_col <= insstart_less)
2175 ai_col = 0;
2176 else
2177 ai_col -= insstart_less;
2178 }
2179
2180 /*
2181 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2182 * If the number of characters before the cursor decreased, need to pop a
2183 * few characters from the replace stack.
2184 * If the number of characters before the cursor increased, need to push a
2185 * few NULs onto the replace stack.
2186 */
2187 if (REPLACE_NORMAL(State) && start_col >= 0)
2188 {
2189 while (start_col > (int)curwin->w_cursor.col)
2190 {
2191 replace_join(0); /* remove a NUL from the replace stack */
2192 --start_col;
2193 }
2194 while (start_col < (int)curwin->w_cursor.col || replaced)
2195 {
2196 replace_push(NUL);
2197 if (replaced)
2198 {
2199 replace_push(replaced);
2200 replaced = NUL;
2201 }
2202 ++start_col;
2203 }
2204 }
2205
2206#ifdef FEAT_VREPLACE
2207 /*
2208 * For VREPLACE mode, we also have to fix the replace stack. In this case
2209 * it is always possible because we backspace over the whole line and then
2210 * put it back again the way we wanted it.
2211 */
2212 if (State & VREPLACE_FLAG)
2213 {
2214 /* If orig_line didn't allocate, just return. At least we did the job,
2215 * even if you can't backspace. */
2216 if (orig_line == NULL)
2217 return;
2218
2219 /* Save new line */
2220 new_line = vim_strsave(ml_get_curline());
2221 if (new_line == NULL)
2222 return;
2223
2224 /* We only put back the new line up to the cursor */
2225 new_line[curwin->w_cursor.col] = NUL;
2226
2227 /* Put back original line */
2228 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2229 curwin->w_cursor.col = orig_col;
2230
2231 /* Backspace from cursor to start of line */
2232 backspace_until_column(0);
2233
2234 /* Insert new stuff into line again */
2235 ins_bytes(new_line);
2236
2237 vim_free(new_line);
2238 }
2239#endif
2240}
2241
2242/*
2243 * Truncate the space at the end of a line. This is to be used only in an
2244 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2245 * modes.
2246 */
2247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
2250 int i;
2251
2252 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002253 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
2255 if (State & REPLACE_FLAG)
2256 replace_join(0); /* remove a NUL from the replace stack */
2257 }
2258 line[i + 1] = NUL;
2259}
2260
2261#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2262 || defined(FEAT_COMMENTS) || defined(PROTO)
2263/*
2264 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2265 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002266 * Will attempt not to go before "col" even when there is a composing
2267 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 */
2269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002270backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 while ((int)curwin->w_cursor.col > col)
2273 {
2274 curwin->w_cursor.col--;
2275 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002276 replace_do_bs(col);
2277 else if (!del_char_after_col(col))
2278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280}
2281#endif
2282
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002283/*
2284 * Like del_char(), but make sure not to go before column "limit_col".
2285 * Only matters when there are composing characters.
2286 * Return TRUE when something was deleted.
2287 */
2288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002289del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002290{
2291#ifdef FEAT_MBYTE
2292 if (enc_utf8 && limit_col >= 0)
2293 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002294 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002295
2296 /* Make sure the cursor is at the start of a character, but
2297 * skip forward again when going too far back because of a
2298 * composing character. */
2299 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002300 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002301 {
2302 int l = utf_ptr2len(ml_get_cursor());
2303
2304 if (l == 0) /* end of line */
2305 break;
2306 curwin->w_cursor.col += l;
2307 }
2308 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2309 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002310 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002311 }
2312 else
2313#endif
2314 (void)del_char(FALSE);
2315 return TRUE;
2316}
2317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2319/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002320 * CTRL-X pressed in Insert mode.
2321 */
2322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002323ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002324{
2325 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2326 * CTRL-V works like CTRL-N */
2327 if (ctrl_x_mode != CTRL_X_CMDLINE)
2328 {
2329 /* if the next ^X<> won't ADD nothing, then reset
2330 * compl_cont_status */
2331 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002332 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002333 else
2334 compl_cont_status = 0;
2335 /* We're not sure which CTRL-X mode it will be yet */
2336 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2337 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2338 edit_submode_pre = NULL;
2339 showmode();
2340 }
2341}
2342
2343/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002344 * Whether other than default completion has been selected.
2345 */
2346 int
2347ctrl_x_mode_not_default(void)
2348{
2349 return ctrl_x_mode != CTRL_X_NORMAL;
2350}
2351
2352/*
2353 * Whether CTRL-X was typed without a following character.
2354 */
2355 int
2356ctrl_x_mode_not_defined_yet(void)
2357{
2358 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2359}
2360
2361/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002362 * Return TRUE if the 'dict' or 'tsr' option can be used.
2363 */
2364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002365has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002366{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002367 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002368# ifdef FEAT_SPELL
2369 && !curwin->w_p_spell
2370# endif
2371 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002372 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2373 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002374 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002375 edit_submode = NULL;
2376 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2377 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002378 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002379 if (emsg_silent == 0)
2380 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002381 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002382 setcursor();
2383 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002384#ifdef FEAT_EVAL
2385 if (!get_vim_var_nr(VV_TESTING))
2386#endif
2387 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002388 }
2389 return FALSE;
2390 }
2391 return TRUE;
2392}
2393
2394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2396 * This depends on the current mode.
2397 */
2398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002399vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
2401 /* Always allow ^R - let it's results then be checked */
2402 if (c == Ctrl_R)
2403 return TRUE;
2404
Bram Moolenaare3226be2005-12-18 22:10:00 +00002405 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002406 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002407 return TRUE;
2408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 switch (ctrl_x_mode)
2410 {
2411 case 0: /* Not in any CTRL-X mode */
2412 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2413 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002414 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2416 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2417 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002418 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002419 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 case CTRL_X_SCROLL:
2421 return (c == Ctrl_Y || c == Ctrl_E);
2422 case CTRL_X_WHOLE_LINE:
2423 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2424 case CTRL_X_FILES:
2425 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2426 case CTRL_X_DICTIONARY:
2427 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2428 case CTRL_X_THESAURUS:
2429 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2430 case CTRL_X_TAGS:
2431 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2432#ifdef FEAT_FIND_ID
2433 case CTRL_X_PATH_PATTERNS:
2434 return (c == Ctrl_P || c == Ctrl_N);
2435 case CTRL_X_PATH_DEFINES:
2436 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2437#endif
2438 case CTRL_X_CMDLINE:
2439 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2440 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002441#ifdef FEAT_COMPL_FUNC
2442 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002443 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002444 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002445 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002446#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002447 case CTRL_X_SPELL:
2448 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002449 case CTRL_X_EVAL:
2450 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002452 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 return FALSE;
2454}
2455
2456/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002457 * Return TRUE when character "c" is part of the item currently being
2458 * completed. Used to decide whether to abandon complete mode when the menu
2459 * is visible.
2460 */
2461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002462ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002463{
2464 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2465 /* When expanding an identifier only accept identifier chars. */
2466 return vim_isIDc(c);
2467
2468 switch (ctrl_x_mode)
2469 {
2470 case CTRL_X_FILES:
2471 /* When expanding file name only accept file name chars. But not
2472 * path separators, so that "proto/<Tab>" expands files in
2473 * "proto", not "proto/" as a whole */
2474 return vim_isfilec(c) && !vim_ispathsep(c);
2475
2476 case CTRL_X_CMDLINE:
2477 case CTRL_X_OMNI:
2478 /* Command line and Omni completion can work with just about any
2479 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002480 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002481
2482 case CTRL_X_WHOLE_LINE:
2483 /* For while line completion a space can be part of the line. */
2484 return vim_isprintc(c);
2485 }
2486 return vim_iswordc(c);
2487}
2488
2489/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002490 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002492 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 * the rest of the word to be in -- webb
2494 */
2495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002496ins_compl_add_infercase(
2497 char_u *str,
2498 int len,
2499 int icase,
2500 char_u *fname,
2501 int dir,
2502 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002504 char_u *p;
2505 int i, c;
2506 int actual_len; /* Take multi-byte characters */
2507 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002508 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002509 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 int has_lower = FALSE;
2511 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002513 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002515 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaara2993e12007-08-12 14:38:46 +00002517 /* Find actual length of completion. */
2518#ifdef FEAT_MBYTE
2519 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002521 p = str;
2522 actual_len = 0;
2523 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002525 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002526 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002529 else
2530#endif
2531 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaara2993e12007-08-12 14:38:46 +00002533 /* Find actual length of original text. */
2534#ifdef FEAT_MBYTE
2535 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002537 p = compl_orig_text;
2538 actual_compl_length = 0;
2539 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002541 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002542 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002545 else
2546#endif
2547 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002549 /* "actual_len" may be smaller than "actual_compl_length" when using
2550 * thesaurus, only use the minimum when comparing. */
2551 min_len = actual_len < actual_compl_length
2552 ? actual_len : actual_compl_length;
2553
Bram Moolenaara2993e12007-08-12 14:38:46 +00002554 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002555 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002556 if (wca != NULL)
2557 {
2558 p = str;
2559 for (i = 0; i < actual_len; ++i)
2560#ifdef FEAT_MBYTE
2561 if (has_mbyte)
2562 wca[i] = mb_ptr2char_adv(&p);
2563 else
2564#endif
2565 wca[i] = *(p++);
2566
2567 /* Rule 1: Were any chars converted to lower? */
2568 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002569 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002570 {
2571#ifdef FEAT_MBYTE
2572 if (has_mbyte)
2573 c = mb_ptr2char_adv(&p);
2574 else
2575#endif
2576 c = *(p++);
2577 if (MB_ISLOWER(c))
2578 {
2579 has_lower = TRUE;
2580 if (MB_ISUPPER(wca[i]))
2581 {
2582 /* Rule 1 is satisfied. */
2583 for (i = actual_compl_length; i < actual_len; ++i)
2584 wca[i] = MB_TOLOWER(wca[i]);
2585 break;
2586 }
2587 }
2588 }
2589
2590 /*
2591 * Rule 2: No lower case, 2nd consecutive letter converted to
2592 * upper case.
2593 */
2594 if (!has_lower)
2595 {
2596 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002597 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002598 {
2599#ifdef FEAT_MBYTE
2600 if (has_mbyte)
2601 c = mb_ptr2char_adv(&p);
2602 else
2603#endif
2604 c = *(p++);
2605 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2606 {
2607 /* Rule 2 is satisfied. */
2608 for (i = actual_compl_length; i < actual_len; ++i)
2609 wca[i] = MB_TOUPPER(wca[i]);
2610 break;
2611 }
2612 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2613 }
2614 }
2615
2616 /* Copy the original case of the part we typed. */
2617 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002618 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002619 {
2620#ifdef FEAT_MBYTE
2621 if (has_mbyte)
2622 c = mb_ptr2char_adv(&p);
2623 else
2624#endif
2625 c = *(p++);
2626 if (MB_ISLOWER(c))
2627 wca[i] = MB_TOLOWER(wca[i]);
2628 else if (MB_ISUPPER(c))
2629 wca[i] = MB_TOUPPER(wca[i]);
2630 }
2631
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002632 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002633 * Generate encoding specific output from wide character array.
2634 * Multi-byte characters can occupy up to five bytes more than
2635 * ASCII characters, and we also need one byte for NUL, so stay
2636 * six bytes away from the edge of IObuff.
2637 */
2638 p = IObuff;
2639 i = 0;
2640 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2641#ifdef FEAT_MBYTE
2642 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002643 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002644 else
2645#endif
2646 *(p++) = wca[i++];
2647 *p = NUL;
2648
2649 vim_free(wca);
2650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002652 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2653 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002655 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656}
2657
2658/*
2659 * Add a match to the list of matches.
2660 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002661 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002662 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002664 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002665ins_compl_add(
2666 char_u *str,
2667 int len,
2668 int icase,
2669 char_u *fname,
2670 char_u **cptext, /* extra text for popup menu or NULL */
2671 int cdir,
2672 int flags,
2673 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002675 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002676 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
2678 ui_breakcheck();
2679 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (len < 0)
2682 len = (int)STRLEN(str);
2683
2684 /*
2685 * If the same match is already present, don't add it.
2686 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002687 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002689 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 do
2691 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002692 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002693 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002694 && match->cp_str[len] == NUL)
2695 return NOTDONE;
2696 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002697 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002700 /* Remove any popup menu before changing the list of matches. */
2701 ins_compl_del_pum();
2702
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 /*
2704 * Allocate a new match structure.
2705 * Copy the values to the new match structure.
2706 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002707 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002709 return FAIL;
2710 match->cp_number = -1;
2711 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002712 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002713 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
2715 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002716 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002718 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002719
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002721 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2723 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002724 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002725 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002726 && compl_curr_match->cp_fname != NULL
2727 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002728 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002729 else if (fname != NULL)
2730 {
2731 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002732 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002735 match->cp_fname = NULL;
2736 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002737
2738 if (cptext != NULL)
2739 {
2740 int i;
2741
2742 for (i = 0; i < CPT_COUNT; ++i)
2743 if (cptext[i] != NULL && *cptext[i] != NUL)
2744 match->cp_text[i] = vim_strsave(cptext[i]);
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 /*
2748 * Link the new match structure in the list of matches.
2749 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002750 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002751 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else if (dir == FORWARD)
2753 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002754 match->cp_next = compl_curr_match->cp_next;
2755 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 else /* BACKWARD */
2758 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002759 match->cp_next = compl_curr_match;
2760 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002762 if (match->cp_next)
2763 match->cp_next->cp_prev = match;
2764 if (match->cp_prev)
2765 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002767 compl_first_match = match;
2768 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002770 /*
2771 * Find the longest common string if still doing that.
2772 */
2773 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2774 ins_compl_longest_match(match);
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 return OK;
2777}
2778
2779/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002780 * Return TRUE if "str[len]" matches with match->cp_str, considering
2781 * match->cp_icase.
2782 */
2783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002784ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002785{
2786 if (match->cp_icase)
2787 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2788 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2789}
2790
2791/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002792 * Reduce the longest common string for match "match".
2793 */
2794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002795ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002796{
2797 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002798 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002799 int had_match;
2800
2801 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002802 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002803 /* First match, use it as a whole. */
2804 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002805 if (compl_leader != NULL)
2806 {
2807 had_match = (curwin->w_cursor.col > compl_col);
2808 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002809 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002810 ins_redraw(FALSE);
2811
2812 /* When the match isn't there (to avoid matching itself) remove it
2813 * again after redrawing. */
2814 if (!had_match)
2815 ins_compl_delete();
2816 compl_used_match = FALSE;
2817 }
2818 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002819 else
2820 {
2821 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002822 p = compl_leader;
2823 s = match->cp_str;
2824 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002825 {
2826#ifdef FEAT_MBYTE
2827 if (has_mbyte)
2828 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002829 c1 = mb_ptr2char(p);
2830 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002831 }
2832 else
2833#endif
2834 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002835 c1 = *p;
2836 c2 = *s;
2837 }
2838 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2839 : (c1 != c2))
2840 break;
2841#ifdef FEAT_MBYTE
2842 if (has_mbyte)
2843 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002844 MB_PTR_ADV(p);
2845 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002846 }
2847 else
2848#endif
2849 {
2850 ++p;
2851 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002852 }
2853 }
2854
2855 if (*p != NUL)
2856 {
2857 /* Leader was shortened, need to change the inserted text. */
2858 *p = NUL;
2859 had_match = (curwin->w_cursor.col > compl_col);
2860 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002861 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002862 ins_redraw(FALSE);
2863
2864 /* When the match isn't there (to avoid matching itself) remove it
2865 * again after redrawing. */
2866 if (!had_match)
2867 ins_compl_delete();
2868 }
2869
2870 compl_used_match = FALSE;
2871 }
2872}
2873
2874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 * Add an array of matches to the list of matches.
2876 * Frees matches[].
2877 */
2878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002879ins_compl_add_matches(
2880 int num_matches,
2881 char_u **matches,
2882 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
2884 int i;
2885 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002886 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
Bram Moolenaar572cb562005-08-05 21:35:02 +00002888 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002889 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002890 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002892 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 FreeWild(num_matches, matches);
2894}
2895
2896/* Make the completion list cyclic.
2897 * Return the number of matches (excluding the original).
2898 */
2899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002900ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002902 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 int count = 0;
2904
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002905 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
2907 /*
2908 * Find the end of the list.
2909 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002910 match = compl_first_match;
2911 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002912 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002914 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 ++count;
2916 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002917 match->cp_next = compl_first_match;
2918 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920 return count;
2921}
2922
Bram Moolenaara94bc432006-03-10 21:42:59 +00002923/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002924 * Set variables that store noselect and noinsert behavior from the
2925 * 'completeopt' value.
2926 */
2927 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002928completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002929{
2930 compl_no_insert = FALSE;
2931 compl_no_select = FALSE;
2932 if (strstr((char *)p_cot, "noselect") != NULL)
2933 compl_no_select = TRUE;
2934 if (strstr((char *)p_cot, "noinsert") != NULL)
2935 compl_no_insert = TRUE;
2936}
2937
2938/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002939 * Start completion for the complete() function.
2940 * "startcol" is where the matched text starts (1 is first column).
2941 * "list" is the list of matches.
2942 */
2943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002945{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002946 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002947 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002948
Bram Moolenaara94bc432006-03-10 21:42:59 +00002949 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002950 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002951 ins_compl_prep(' ');
2952 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002953 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002954
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002955 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002956 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002957 startcol = curwin->w_cursor.col;
2958 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002959 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002960 /* compl_pattern doesn't need to be set */
2961 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2962 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002963 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002964 return;
2965
Bram Moolenaare4214502015-03-05 18:08:43 +01002966 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002967
2968 ins_compl_add_list(list);
2969 compl_matches = ins_compl_make_cyclic();
2970 compl_started = TRUE;
2971 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002972 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002973
2974 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002975 if (compl_no_insert || compl_no_select)
2976 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002977 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002978 if (compl_no_select)
2979 /* Down/Up has no real effect. */
2980 ins_complete(K_UP, FALSE);
2981 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002982 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002983 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002984 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002985
2986 /* Lazily show the popup menu, unless we got interrupted. */
2987 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002988 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002989 out_flush();
2990}
2991
2992
Bram Moolenaar9372a112005-12-06 19:59:18 +00002993/* "compl_match_array" points the currently displayed list of entries in the
2994 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002995static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002996static int compl_match_arraysize;
2997
2998/*
2999 * Update the screen and when there is any scrolling remove the popup menu.
3000 */
3001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003002ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003003{
3004 int h;
3005
3006 if (compl_match_array != NULL)
3007 {
3008 h = curwin->w_cline_height;
3009 update_screen(0);
3010 if (h != curwin->w_cline_height)
3011 ins_compl_del_pum();
3012 }
3013}
3014
3015/*
3016 * Remove any popup menu.
3017 */
3018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003019ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003020{
3021 if (compl_match_array != NULL)
3022 {
3023 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003024 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003025 }
3026}
3027
3028/*
3029 * Return TRUE if the popup menu should be displayed.
3030 */
3031 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003032pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003033{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003034 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003035 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003036 return FALSE;
3037
3038 /* The display looks bad on a B&W display. */
3039 if (t_colors < 8
3040#ifdef FEAT_GUI
3041 && !gui.in_use
3042#endif
3043 )
3044 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003045 return TRUE;
3046}
3047
3048/*
3049 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003050 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003051 */
3052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003053pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003054{
3055 compl_T *compl;
3056 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003057
3058 /* Don't display the popup menu if there are no matches or there is only
3059 * one (ignoring the original text). */
3060 compl = compl_first_match;
3061 i = 0;
3062 do
3063 {
3064 if (compl == NULL
3065 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3066 break;
3067 compl = compl->cp_next;
3068 } while (compl != compl_first_match);
3069
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003070 if (strstr((char *)p_cot, "menuone") != NULL)
3071 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003072 return (i >= 2);
3073}
3074
3075/*
3076 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003077 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003078 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003081{
3082 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003083 compl_T *shown_compl = NULL;
3084 int did_find_shown_match = FALSE;
3085 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003086 int i;
3087 int cur = -1;
3088 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003089 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003090
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003091 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003092 return;
3093
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003094#if defined(FEAT_EVAL)
3095 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3096 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3097#endif
3098
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003099 /* Update the screen before drawing the popup menu over it. */
3100 update_screen(0);
3101
3102 if (compl_match_array == NULL)
3103 {
3104 /* Need to build the popup menu list. */
3105 compl_match_arraysize = 0;
3106 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003107 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003108 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003109 do
3110 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003111 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3112 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003113 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003114 ++compl_match_arraysize;
3115 compl = compl->cp_next;
3116 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003117 if (compl_match_arraysize == 0)
3118 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003119 compl_match_array = (pumitem_T *)alloc_clear(
3120 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003121 * compl_match_arraysize));
3122 if (compl_match_array != NULL)
3123 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003124 /* If the current match is the original text don't find the first
3125 * match after it, don't highlight anything. */
3126 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3127 shown_match_ok = TRUE;
3128
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003129 i = 0;
3130 compl = compl_first_match;
3131 do
3132 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003133 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3134 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003135 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003136 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003137 if (!shown_match_ok)
3138 {
3139 if (compl == compl_shown_match || did_find_shown_match)
3140 {
3141 /* This item is the shown match or this is the
3142 * first displayed item after the shown match. */
3143 compl_shown_match = compl;
3144 did_find_shown_match = TRUE;
3145 shown_match_ok = TRUE;
3146 }
3147 else
3148 /* Remember this displayed match for when the
3149 * shown match is just below it. */
3150 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003151 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003152 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003153
3154 if (compl->cp_text[CPT_ABBR] != NULL)
3155 compl_match_array[i].pum_text =
3156 compl->cp_text[CPT_ABBR];
3157 else
3158 compl_match_array[i].pum_text = compl->cp_str;
3159 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3160 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3161 if (compl->cp_text[CPT_MENU] != NULL)
3162 compl_match_array[i++].pum_extra =
3163 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003164 else
3165 compl_match_array[i++].pum_extra = compl->cp_fname;
3166 }
3167
3168 if (compl == compl_shown_match)
3169 {
3170 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003171
3172 /* When the original text is the shown match don't set
3173 * compl_shown_match. */
3174 if (compl->cp_flags & ORIGINAL_TEXT)
3175 shown_match_ok = TRUE;
3176
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003177 if (!shown_match_ok && shown_compl != NULL)
3178 {
3179 /* The shown match isn't displayed, set it to the
3180 * previously displayed match. */
3181 compl_shown_match = shown_compl;
3182 shown_match_ok = TRUE;
3183 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003184 }
3185 compl = compl->cp_next;
3186 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003187
3188 if (!shown_match_ok) /* no displayed match at all */
3189 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003190 }
3191 }
3192 else
3193 {
3194 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003195 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003196 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3197 || compl_match_array[i].pum_text
3198 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003199 {
3200 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003201 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003202 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003203 }
3204
3205 if (compl_match_array != NULL)
3206 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003207 /* In Replace mode when a $ is displayed at the end of the line only
3208 * part of the screen would be updated. We do need to redraw here. */
3209 dollar_vcol = -1;
3210
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003211 /* Compute the screen column of the start of the completed text.
3212 * Use the cursor to get all wrapping and other settings right. */
3213 col = curwin->w_cursor.col;
3214 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003215 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003216 curwin->w_cursor.col = col;
3217 }
3218}
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#define DICT_FIRST (1) /* use just first element in "dict" */
3221#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003224 * Add any identifiers that match the given pattern in the list of dictionary
3225 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 */
3227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003228ins_compl_dictionaries(
3229 char_u *dict_start,
3230 char_u *pat,
3231 int flags, /* DICT_FIRST and/or DICT_EXACT */
3232 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003234 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 char_u *ptr;
3236 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 char_u **files;
3239 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003241 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
Bram Moolenaar0b238792006-03-02 22:49:12 +00003243 if (*dict == NUL)
3244 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003245#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003246 /* When 'dictionary' is empty and spell checking is enabled use
3247 * "spell". */
3248 if (!thesaurus && curwin->w_p_spell)
3249 dict = (char_u *)"spell";
3250 else
3251#endif
3252 return;
3253 }
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003256 if (buf == NULL)
3257 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003258 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 /* If 'infercase' is set, don't use 'smartcase' here */
3261 save_p_scs = p_scs;
3262 if (curbuf->b_p_inf)
3263 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003264
3265 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3266 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003267 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003268 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003269 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003270 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003271 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003272
3273 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003274 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003275 len = STRLEN(pat_esc) + 10;
3276 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003277 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003278 {
3279 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003280 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003281 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003282 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003283 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3284 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003285 vim_free(ptr);
3286 }
3287 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003288 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003289 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003290 if (regmatch.regprog == NULL)
3291 goto theend;
3292 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3295 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003296 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 /* copy one dictionary file name into buf */
3299 if (flags == DICT_EXACT)
3300 {
3301 count = 1;
3302 files = &dict;
3303 }
3304 else
3305 {
3306 /* Expand wildcards in the dictionary name, but do not allow
3307 * backticks (for security, the 'dict' option may have been set in
3308 * a modeline). */
3309 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003310# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003311 if (!thesaurus && STRCMP(buf, "spell") == 0)
3312 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003313 else
3314# endif
3315 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 || expand_wildcards(1, &buf, &count, &files,
3317 EW_FILE|EW_SILENT) != OK)
3318 count = 0;
3319 }
3320
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003321# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003322 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003324 /* Complete from active spelling. Skip "\<" in the pattern, we
3325 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003326 if (pat[0] == '\\' && pat[1] == '<')
3327 ptr = pat + 2;
3328 else
3329 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003330 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003332 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003333# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003334 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003335 {
3336 ins_compl_files(count, files, thesaurus, flags,
3337 &regmatch, buf, &dir);
3338 if (flags != DICT_EXACT)
3339 FreeWild(count, files);
3340 }
3341 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 break;
3343 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003344
3345theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003347 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 vim_free(buf);
3349}
3350
Bram Moolenaar0b238792006-03-02 22:49:12 +00003351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003352ins_compl_files(
3353 int count,
3354 char_u **files,
3355 int thesaurus,
3356 int flags,
3357 regmatch_T *regmatch,
3358 char_u *buf,
3359 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003360{
3361 char_u *ptr;
3362 int i;
3363 FILE *fp;
3364 int add_r;
3365
3366 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3367 {
3368 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3369 if (flags != DICT_EXACT)
3370 {
3371 vim_snprintf((char *)IObuff, IOSIZE,
3372 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003373 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003374 }
3375
3376 if (fp != NULL)
3377 {
3378 /*
3379 * Read dictionary file line by line.
3380 * Check each line for a match.
3381 */
3382 while (!got_int && !compl_interrupted
3383 && !vim_fgets(buf, LSIZE, fp))
3384 {
3385 ptr = buf;
3386 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3387 {
3388 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003389 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003390 ptr = find_line_end(ptr);
3391 else
3392 ptr = find_word_end(ptr);
3393 add_r = ins_compl_add_infercase(regmatch->startp[0],
3394 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003395 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003396 if (thesaurus)
3397 {
3398 char_u *wstart;
3399
3400 /*
3401 * Add the other matches on the line
3402 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003403 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003404 while (!got_int)
3405 {
3406 /* Find start of the next word. Skip white
3407 * space and punctuation. */
3408 ptr = find_word_start(ptr);
3409 if (*ptr == NUL || *ptr == NL)
3410 break;
3411 wstart = ptr;
3412
Bram Moolenaara2993e12007-08-12 14:38:46 +00003413 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003414#ifdef FEAT_MBYTE
3415 if (has_mbyte)
3416 /* Japanese words may have characters in
3417 * different classes, only separate words
3418 * with single-byte non-word characters. */
3419 while (*ptr != NUL)
3420 {
3421 int l = (*mb_ptr2len)(ptr);
3422
3423 if (l < 2 && !vim_iswordc(*ptr))
3424 break;
3425 ptr += l;
3426 }
3427 else
3428#endif
3429 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003430
3431 /* Add the word. Skip the regexp match. */
3432 if (wstart != regmatch->startp[0])
3433 add_r = ins_compl_add_infercase(wstart,
3434 (int)(ptr - wstart),
3435 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003436 }
3437 }
3438 if (add_r == OK)
3439 /* if dir was BACKWARD then honor it just once */
3440 *dir = FORWARD;
3441 else if (add_r == FAIL)
3442 break;
3443 /* avoid expensive call to vim_regexec() when at end
3444 * of line */
3445 if (*ptr == '\n' || got_int)
3446 break;
3447 }
3448 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003449 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003450 }
3451 fclose(fp);
3452 }
3453 }
3454}
3455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456/*
3457 * Find the start of the next word.
3458 * Returns a pointer to the first char of the word. Also stops at a NUL.
3459 */
3460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003461find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
3463#ifdef FEAT_MBYTE
3464 if (has_mbyte)
3465 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003466 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 else
3468#endif
3469 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3470 ++ptr;
3471 return ptr;
3472}
3473
3474/*
3475 * Find the end of the word. Assumes it starts inside a word.
3476 * Returns a pointer to just after the word.
3477 */
3478 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003479find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481#ifdef FEAT_MBYTE
3482 int start_class;
3483
3484 if (has_mbyte)
3485 {
3486 start_class = mb_get_class(ptr);
3487 if (start_class > 1)
3488 while (*ptr != NUL)
3489 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003490 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (mb_get_class(ptr) != start_class)
3492 break;
3493 }
3494 }
3495 else
3496#endif
3497 while (vim_iswordc(*ptr))
3498 ++ptr;
3499 return ptr;
3500}
3501
3502/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003503 * Find the end of the line, omitting CR and NL at the end.
3504 * Returns a pointer to just after the line.
3505 */
3506 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003507find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003508{
3509 char_u *s;
3510
3511 s = ptr + STRLEN(ptr);
3512 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3513 --s;
3514 return s;
3515}
3516
3517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 * Free the list of completions
3519 */
3520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003521ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003523 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003524 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaard23a8232018-02-10 18:45:26 +01003526 VIM_CLEAR(compl_pattern);
3527 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003529 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003531
3532 ins_compl_del_pum();
3533 pum_clear();
3534
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003535 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 do
3537 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003538 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003539 compl_curr_match = compl_curr_match->cp_next;
3540 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003542 if (match->cp_flags & FREE_FNAME)
3543 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003544 for (i = 0; i < CPT_COUNT; ++i)
3545 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003547 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3548 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003549 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003550 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551}
3552
3553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003554ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003556 compl_cont_status = 0;
3557 compl_started = FALSE;
3558 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003559 VIM_CLEAR(compl_pattern);
3560 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003562 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003563 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003564 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003565 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003569 * Return TRUE when Insert completion is active.
3570 */
3571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003572ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003573{
3574 return compl_started;
3575}
3576
3577/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003578 * Delete one character before the cursor and show the subset of the matches
3579 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003580 * Returns the character to be used, NUL if the work is done and another char
3581 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003582 */
3583 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003585{
3586 char_u *line;
3587 char_u *p;
3588
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003589 line = ml_get_curline();
3590 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003591 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003592
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003593 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003594 * allow the word to be deleted, we won't match everything.
3595 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003596 if ((int)(p - line) - (int)compl_col < 0
3597 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003598 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3599 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3600 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003601 return K_BS;
3602
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003603 /* Deleted more than what was used to find matches or didn't finish
3604 * finding all matches: need to look for matches all over again. */
3605 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003606 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003607 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003608
Bram Moolenaara6557602006-02-04 22:43:20 +00003609 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003610 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003611 if (compl_leader != NULL)
3612 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003613 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003614 if (compl_shown_match != NULL)
3615 /* Make sure current match is not a hidden item. */
3616 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003617 return NUL;
3618 }
3619 return K_BS;
3620}
Bram Moolenaara6557602006-02-04 22:43:20 +00003621
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003622/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003623 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3624 * be called.
3625 */
3626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003627ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003628{
3629 /* Return TRUE if we didn't complete finding matches or when the
3630 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3631 return compl_was_interrupted
3632 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3633 && compl_opt_refresh_always);
3634}
3635
3636/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003637 * Called after changing "compl_leader".
3638 * Show the popup menu with a different set of matches.
3639 * May also search for matches again if the previous search was interrupted.
3640 */
3641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003643{
3644 ins_compl_del_pum();
3645 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003646 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003647 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003648
3649 if (compl_started)
3650 ins_compl_set_original_text(compl_leader);
3651 else
3652 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003653#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003654 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003655#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003656 /*
3657 * Matches were cleared, need to search for them now. First display
3658 * the changed text before the cursor. Set "compl_restarting" to
3659 * avoid that the first match is inserted.
3660 */
3661 update_screen(0);
3662#ifdef FEAT_GUI
3663 if (gui.in_use)
3664 {
3665 /* Show the cursor after the match, not after the redrawn text. */
3666 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003667 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003668 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003669#endif
3670 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003671 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003672 compl_cont_status = 0;
3673 compl_restarting = FALSE;
3674 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003675
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003676 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003677
3678 /* Show the popup menu with a different set of matches. */
3679 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003680
3681 /* Don't let Enter select the original text when there is no popup menu. */
3682 if (compl_match_array == NULL)
3683 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003684}
3685
3686/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003687 * Return the length of the completion, from the completion start column to
3688 * the cursor column. Making sure it never goes below zero.
3689 */
3690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003691ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003692{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003693 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003694
3695 if (off < 0)
3696 return 0;
3697 return off;
3698}
3699
3700/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003701 * Append one character to the match leader. May reduce the number of
3702 * matches.
3703 */
3704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003706{
3707#ifdef FEAT_MBYTE
3708 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003709#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003710
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003711 if (stop_arrow() == FAIL)
3712 return;
3713#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003714 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3715 {
3716 char_u buf[MB_MAXBYTES + 1];
3717
3718 (*mb_char2bytes)(c, buf);
3719 buf[cc] = NUL;
3720 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003721 if (compl_opt_refresh_always)
3722 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003723 }
3724 else
3725#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003726 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003727 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003728 if (compl_opt_refresh_always)
3729 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003730 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003731
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003732 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003733 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003734 ins_compl_restart();
3735
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003736 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003737 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003738 * break redo. */
3739 if (!compl_opt_refresh_always)
3740 {
3741 vim_free(compl_leader);
3742 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003743 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003744 if (compl_leader != NULL)
3745 ins_compl_new_leader();
3746 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003747}
3748
3749/*
3750 * Setup for finding completions again without leaving CTRL-X mode. Used when
3751 * BS or a key was typed while still searching for matches.
3752 */
3753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003754ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003755{
3756 ins_compl_free();
3757 compl_started = FALSE;
3758 compl_matches = 0;
3759 compl_cont_status = 0;
3760 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003761}
3762
3763/*
3764 * Set the first match, the original text.
3765 */
3766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003767ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003768{
3769 char_u *p;
3770
Bram Moolenaare87edf32018-04-17 22:14:32 +02003771 /* Replace the original text entry.
3772 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3773 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003774 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3775 {
3776 p = vim_strsave(str);
3777 if (p != NULL)
3778 {
3779 vim_free(compl_first_match->cp_str);
3780 compl_first_match->cp_str = p;
3781 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003782 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003783 else if (compl_first_match->cp_prev != NULL
3784 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3785 {
3786 p = vim_strsave(str);
3787 if (p != NULL)
3788 {
3789 vim_free(compl_first_match->cp_prev->cp_str);
3790 compl_first_match->cp_prev->cp_str = p;
3791 }
3792 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003793}
3794
3795/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003796 * Append one character to the match leader. May reduce the number of
3797 * matches.
3798 */
3799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003800ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003801{
3802 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003803 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003804 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003805 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003806
3807 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003808 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003809 {
3810 /* When still at the original match use the first entry that matches
3811 * the leader. */
3812 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3813 {
3814 p = NULL;
3815 for (cp = compl_shown_match->cp_next; cp != NULL
3816 && cp != compl_first_match; cp = cp->cp_next)
3817 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003818 if (compl_leader == NULL
3819 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003820 (int)STRLEN(compl_leader)))
3821 {
3822 p = cp->cp_str;
3823 break;
3824 }
3825 }
3826 if (p == NULL || (int)STRLEN(p) <= len)
3827 return;
3828 }
3829 else
3830 return;
3831 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003832 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003833 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003834 ins_compl_addleader(c);
3835}
3836
3837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003839 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003840 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003843ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
3845 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003847 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 /* Forget any previous 'special' messages if this is actually
3850 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3851 */
3852 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3853 edit_submode_extra = NULL;
3854
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003855 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003856 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3857 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003858 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003860 /* Set "compl_get_longest" when finding the first matches. */
3861 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003862 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003863 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003864 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003865 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003866
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003867 }
3868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3870 {
3871 /*
3872 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3873 * it will be yet. Now we decide.
3874 */
3875 switch (c)
3876 {
3877 case Ctrl_E:
3878 case Ctrl_Y:
3879 ctrl_x_mode = CTRL_X_SCROLL;
3880 if (!(State & REPLACE_FLAG))
3881 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3882 else
3883 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3884 edit_submode_pre = NULL;
3885 showmode();
3886 break;
3887 case Ctrl_L:
3888 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3889 break;
3890 case Ctrl_F:
3891 ctrl_x_mode = CTRL_X_FILES;
3892 break;
3893 case Ctrl_K:
3894 ctrl_x_mode = CTRL_X_DICTIONARY;
3895 break;
3896 case Ctrl_R:
3897 /* Simply allow ^R to happen without affecting ^X mode */
3898 break;
3899 case Ctrl_T:
3900 ctrl_x_mode = CTRL_X_THESAURUS;
3901 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003902#ifdef FEAT_COMPL_FUNC
3903 case Ctrl_U:
3904 ctrl_x_mode = CTRL_X_FUNCTION;
3905 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003907 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003909#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003910 case 's':
3911 case Ctrl_S:
3912 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003913#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003914 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003915 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003916 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003917#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 case Ctrl_RSB:
3920 ctrl_x_mode = CTRL_X_TAGS;
3921 break;
3922#ifdef FEAT_FIND_ID
3923 case Ctrl_I:
3924 case K_S_TAB:
3925 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3926 break;
3927 case Ctrl_D:
3928 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3929 break;
3930#endif
3931 case Ctrl_V:
3932 case Ctrl_Q:
3933 ctrl_x_mode = CTRL_X_CMDLINE;
3934 break;
3935 case Ctrl_P:
3936 case Ctrl_N:
3937 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3938 * just started ^X mode, or there were enough ^X's to cancel
3939 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3940 * do normal expansion when interrupting a different mode (say
3941 * ^X^F^X^P or ^P^X^X^P, see below)
3942 * nothing changes if interrupting mode 0, (eg, the flag
3943 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003944 if (!(compl_cont_status & CONT_INTRPT))
3945 compl_cont_status |= CONT_LOCAL;
3946 else if (compl_cont_mode != 0)
3947 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 /* FALLTHROUGH */
3949 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003950 /* If we have typed at least 2 ^X's... for modes != 0, we set
3951 * compl_cont_status = 0 (eg, as if we had just started ^X
3952 * mode).
3953 * For mode 0, we set "compl_cont_mode" to an impossible
3954 * value, in both cases ^X^X can be used to restart the same
3955 * mode (avoiding ADDING mode).
3956 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3957 * 'complete' and local ^P expansions respectively.
3958 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3959 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (c == Ctrl_X)
3961 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 if (compl_cont_mode != 0)
3963 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003965 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003967 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 edit_submode = NULL;
3969 showmode();
3970 break;
3971 }
3972 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003973 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
3975 /* We're already in CTRL-X mode, do we stay in it? */
3976 if (!vim_is_ctrl_x_key(c))
3977 {
3978 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003979 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 else
3981 ctrl_x_mode = CTRL_X_FINISHED;
3982 edit_submode = NULL;
3983 }
3984 showmode();
3985 }
3986
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003987 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
3989 /* Show error message from attempted keyword completion (probably
3990 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003991 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003993 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
3994 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 || ctrl_x_mode == CTRL_X_FINISHED)
3996 {
3997 /* Get here when we have finished typing a sequence of ^N and
3998 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003999 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004000 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
4002 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004003 * If any of the original typed text has been changed, eg when
4004 * ignorecase is set, we must add back-spaces to the redo
4005 * buffer. We add as few as necessary to delete just the part
4006 * of the original text that has changed.
4007 * When using the longest match, edited the match or used
4008 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004010 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4011 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004012 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004013 ptr = NULL;
4014 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016
4017#ifdef FEAT_CINDENT
4018 want_cindent = (can_cindent && cindent_on());
4019#endif
4020 /*
4021 * When completing whole lines: fix indent for 'cindent'.
4022 * Otherwise, break line if it's too long.
4023 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026#ifdef FEAT_CINDENT
4027 /* re-indent the current line */
4028 if (want_cindent)
4029 {
4030 do_c_expr_indent();
4031 want_cindent = FALSE; /* don't do it again */
4032 }
4033#endif
4034 }
4035 else
4036 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004037 int prev_col = curwin->w_cursor.col;
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004040 if (prev_col > 0)
4041 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004042 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004043 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004045 if (prev_col > 0
4046 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4047 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004050 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004051 * the selection without inserting anything. When
4052 * compl_enter_selects is set the Enter key does the same. */
4053 if ((c == Ctrl_Y || (compl_enter_selects
4054 && (c == CAR || c == K_KENTER || c == NL)))
4055 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004056 retval = TRUE;
4057
Bram Moolenaar47247282016-08-02 22:36:02 +02004058 /* CTRL-E means completion is Ended, go back to the typed text.
4059 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004060 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004061 {
4062 ins_compl_delete();
4063 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004064 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004065 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004066 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004067 retval = TRUE;
4068 }
4069
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004070 auto_format(FALSE, TRUE);
4071
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004073 compl_started = FALSE;
4074 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004075 if (!shortmess(SHM_COMPLETIONMENU))
4076 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004077 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004078 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 if (edit_submode != NULL)
4080 {
4081 edit_submode = NULL;
4082 showmode();
4083 }
4084
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004085#ifdef FEAT_CMDWIN
4086 if (c == Ctrl_C && cmdwin_type != 0)
4087 /* Avoid the popup menu remains displayed when leaving the
4088 * command line window. */
4089 update_screen(0);
4090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091#ifdef FEAT_CINDENT
4092 /*
4093 * Indent now if a key was typed that is in 'cinkeys'.
4094 */
4095 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4096 do_c_expr_indent();
4097#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004098 /* Trigger the CompleteDone event to give scripts a chance to act
4099 * upon the completion. */
4100 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004103 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4104 /* Trigger the CompleteDone event to give scripts a chance to act
4105 * upon the (possibly failed) completion. */
4106 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 /* reset continue_* if we left expansion-mode, if we stay they'll be
4109 * (re)set properly in ins_complete() */
4110 if (!vim_is_ctrl_x_key(c))
4111 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004112 compl_cont_status = 0;
4113 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004115
4116 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117}
4118
4119/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004120 * Fix the redo buffer for the completion leader replacing some of the typed
4121 * text. This inserts backspaces and appends the changed text.
4122 * "ptr" is the known leader text or NUL.
4123 */
4124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004125ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004126{
4127 int len;
4128 char_u *p;
4129 char_u *ptr = ptr_arg;
4130
4131 if (ptr == NULL)
4132 {
4133 if (compl_leader != NULL)
4134 ptr = compl_leader;
4135 else
4136 return; /* nothing to do */
4137 }
4138 if (compl_orig_text != NULL)
4139 {
4140 p = compl_orig_text;
4141 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4142 ;
4143#ifdef FEAT_MBYTE
4144 if (len > 0)
4145 len -= (*mb_head_off)(p, p + len);
4146#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004147 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004148 AppendCharToRedobuff(K_BS);
4149 }
4150 else
4151 len = 0;
4152 if (ptr != NULL)
4153 AppendToRedobuffLit(ptr + len, -1);
4154}
4155
4156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4158 * (depending on flag) starting from buf and looking for a non-scanned
4159 * buffer (other than curbuf). curbuf is special, if it is called with
4160 * buf=curbuf then it has to be the first call for a given flag/expansion.
4161 *
4162 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4163 */
4164 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004165ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 if (flag == 'w') /* just windows */
4170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if (buf == curbuf) /* first call for this flag/expansion */
4172 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004173 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 && wp->w_buffer->b_scanned)
4175 ;
4176 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178 else
4179 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4180 * (unlisted buffers)
4181 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004182 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 && ((flag == 'U'
4184 ? buf->b_p_bl
4185 : (!buf->b_p_bl
4186 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004187 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 ;
4189 return buf;
4190}
4191
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004192#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004193/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004194 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004195 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004196 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004198expand_by_function(
4199 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4200 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004201{
Bram Moolenaar82139082011-09-14 16:52:09 +02004202 list_T *matchlist = NULL;
4203 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004204 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004205 char_u *funcname;
4206 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004207 win_T *curwin_save;
4208 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004209 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004210
Bram Moolenaare344bea2005-09-01 20:46:49 +00004211 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4212 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004213 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004214
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004215 /* Call 'completefunc' to obtain the list of matches. */
4216 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004217 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004218
Bram Moolenaare344bea2005-09-01 20:46:49 +00004219 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004220 curwin_save = curwin;
4221 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004222
4223 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004224 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004225 {
4226 switch (rettv.v_type)
4227 {
4228 case VAR_LIST:
4229 matchlist = rettv.vval.v_list;
4230 break;
4231 case VAR_DICT:
4232 matchdict = rettv.vval.v_dict;
4233 break;
4234 default:
4235 /* TODO: Give error message? */
4236 clear_tv(&rettv);
4237 break;
4238 }
4239 }
4240
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004241 if (curwin_save != curwin || curbuf_save != curbuf)
4242 {
4243 EMSG(_(e_complwin));
4244 goto theend;
4245 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004246 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004247 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004248 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004249 {
4250 EMSG(_(e_compldel));
4251 goto theend;
4252 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004253
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004254 if (matchlist != NULL)
4255 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004256 else if (matchdict != NULL)
4257 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004258
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004259theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004260 if (matchdict != NULL)
4261 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004262 if (matchlist != NULL)
4263 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004264}
4265#endif /* FEAT_COMPL_FUNC */
4266
Bram Moolenaar39f05632006-03-19 22:15:26 +00004267#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004268/*
4269 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004270 */
4271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004272ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004273{
4274 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004275 int dir = compl_direction;
4276
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004277 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004278 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004279 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004280 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4281 /* if dir was BACKWARD then honor it just once */
4282 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004283 else if (did_emsg)
4284 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004285 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004286}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004287
4288/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004289 * Add completions from a dict.
4290 */
4291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004292ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004293{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004294 dictitem_T *di_refresh;
4295 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004296
4297 /* Check for optional "refresh" item. */
4298 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004299 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4300 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004301 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004302 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004303
4304 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4305 compl_opt_refresh_always = TRUE;
4306 }
4307
4308 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004309 di_words = dict_find(dict, (char_u *)"words", 5);
4310 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4311 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004312}
4313
4314/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004315 * Add a match to the list of matches from a typeval_T.
4316 * If the given string is already in the list of completions, then return
4317 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4318 * maybe because alloc() returns NULL, then FAIL is returned.
4319 */
4320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004321ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004322{
4323 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004324 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004325 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004326 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004327 char_u *(cptext[CPT_COUNT]);
4328
4329 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4330 {
4331 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4332 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4333 (char_u *)"abbr", FALSE);
4334 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4335 (char_u *)"menu", FALSE);
4336 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4337 (char_u *)"kind", FALSE);
4338 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4339 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004340 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4341 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004342 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4343 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004344 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004345 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004346 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4347 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004348 }
4349 else
4350 {
4351 word = get_tv_string_chk(tv);
4352 vim_memset(cptext, 0, sizeof(cptext));
4353 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004354 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004355 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004356 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004357}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004358#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004360/*
4361 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004362 * The search starts at position "ini" in curbuf and in the direction
4363 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004364 * When "compl_started" is FALSE start at that position, otherwise continue
4365 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004366 * This may return before finding all the matches.
4367 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 */
4369 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004370ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
4372 static pos_T first_match_pos;
4373 static pos_T last_match_pos;
4374 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004375 static int found_all = FALSE; /* Found all matches of a
4376 certain type. */
4377 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar572cb562005-08-05 21:35:02 +00004379 pos_T *pos;
4380 char_u **matches;
4381 int save_p_scs;
4382 int save_p_ws;
4383 int save_p_ic;
4384 int i;
4385 int num_matches;
4386 int len;
4387 int found_new_match;
4388 int type = ctrl_x_mode;
4389 char_u *ptr;
4390 char_u *dict = NULL;
4391 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004392 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004396 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 ins_buf->b_scanned = 0;
4398 found_all = FALSE;
4399 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004400 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004401 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 last_match_pos = first_match_pos = *ini;
4403 }
4404
Bram Moolenaar4475b622017-05-01 20:46:52 +02004405 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004406 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4408 for (;;)
4409 {
4410 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004411 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 * or if found_all says this entry is done. For ^X^L only use the
4415 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004416 if ((ctrl_x_mode == CTRL_X_NORMAL
4417 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
4420 found_all = FALSE;
4421 while (*e_cpt == ',' || *e_cpt == ' ')
4422 e_cpt++;
4423 if (*e_cpt == '.' && !curbuf->b_scanned)
4424 {
4425 ins_buf = curbuf;
4426 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004427 /* Move the cursor back one character so that ^N can match the
4428 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004429 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004430 {
4431 /* Move the cursor to after the last character in the
4432 * buffer, so that word at start of buffer is found
4433 * correctly. */
4434 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4435 first_match_pos.col =
4436 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 last_match_pos = first_match_pos;
4439 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004440
4441 /* Remember the first match so that the loop stops when we
4442 * wrap and come back there a second time. */
4443 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4446 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4447 {
4448 /* Scan a buffer, but not the current one. */
4449 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4450 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 first_match_pos.col = last_match_pos.col = 0;
4453 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4454 last_match_pos.lnum = 0;
4455 type = 0;
4456 }
4457 else /* unloaded buffer, scan like dictionary */
4458 {
4459 found_all = TRUE;
4460 if (ins_buf->b_fname == NULL)
4461 continue;
4462 type = CTRL_X_DICTIONARY;
4463 dict = ins_buf->b_fname;
4464 dict_f = DICT_EXACT;
4465 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004466 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 ins_buf->b_fname == NULL
4468 ? buf_spname(ins_buf)
4469 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004470 ? ins_buf->b_fname
4471 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004472 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 else if (*e_cpt == NUL)
4475 break;
4476 else
4477 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004478 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 type = -1;
4480 else if (*e_cpt == 'k' || *e_cpt == 's')
4481 {
4482 if (*e_cpt == 'k')
4483 type = CTRL_X_DICTIONARY;
4484 else
4485 type = CTRL_X_THESAURUS;
4486 if (*++e_cpt != ',' && *e_cpt != NUL)
4487 {
4488 dict = e_cpt;
4489 dict_f = DICT_FIRST;
4490 }
4491 }
4492#ifdef FEAT_FIND_ID
4493 else if (*e_cpt == 'i')
4494 type = CTRL_X_PATH_PATTERNS;
4495 else if (*e_cpt == 'd')
4496 type = CTRL_X_PATH_DEFINES;
4497#endif
4498 else if (*e_cpt == ']' || *e_cpt == 't')
4499 {
4500 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004501 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004502 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504 else
4505 type = -1;
4506
4507 /* in any case e_cpt is advanced to the next entry */
4508 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4509
4510 found_all = TRUE;
4511 if (type == -1)
4512 continue;
4513 }
4514 }
4515
Bram Moolenaar4475b622017-05-01 20:46:52 +02004516 /* If complete() was called then compl_pattern has been reset. The
4517 * following won't work then, bail out. */
4518 if (compl_pattern == NULL)
4519 break;
4520
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 switch (type)
4522 {
4523 case -1:
4524 break;
4525#ifdef FEAT_FIND_ID
4526 case CTRL_X_PATH_PATTERNS:
4527 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004528 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004529 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004531 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4533 (linenr_T)1, (linenr_T)MAXLNUM);
4534 break;
4535#endif
4536
4537 case CTRL_X_DICTIONARY:
4538 case CTRL_X_THESAURUS:
4539 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004540 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 : (type == CTRL_X_THESAURUS
4542 ? (*curbuf->b_p_tsr == NUL
4543 ? p_tsr
4544 : curbuf->b_p_tsr)
4545 : (*curbuf->b_p_dict == NUL
4546 ? p_dict
4547 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004548 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004549 dict != NULL ? dict_f
4550 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 dict = NULL;
4552 break;
4553
4554 case CTRL_X_TAGS:
4555 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4556 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004559 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004560 * of matches is found when compl_pattern is empty */
4561 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004562 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4563 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4565 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004566 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
4568 p_ic = save_p_ic;
4569 break;
4570
4571 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004572 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4574 {
4575
4576 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004578 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580 break;
4581
4582 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583 if (expand_cmdline(&compl_xp, compl_pattern,
4584 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004586 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 break;
4588
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004589#ifdef FEAT_COMPL_FUNC
4590 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004591 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004592 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004593 break;
4594#endif
4595
Bram Moolenaar488c6512005-08-11 20:09:58 +00004596 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004597#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004598 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004599 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004600 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004601 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004602#endif
4603 break;
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 default: /* normal ^P/^N and ^X^L */
4606 /*
4607 * If 'infercase' is set, don't use 'smartcase' here
4608 */
4609 save_p_scs = p_scs;
4610 if (ins_buf->b_p_inf)
4611 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612
Bram Moolenaar361aa502014-01-23 22:45:58 +01004613 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 * end but never from the middle, thus setting nowrapscan in this
4615 * buffers is a good idea, on the other hand, we always set
4616 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4617 save_p_ws = p_ws;
4618 if (ins_buf != curbuf)
4619 p_ws = FALSE;
4620 else if (*e_cpt == '.')
4621 p_ws = TRUE;
4622 for (;;)
4623 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004624 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004626 ++msg_silent; /* Don't want messages for wrapscan. */
4627
Bram Moolenaare4214502015-03-05 18:08:43 +01004628 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4629 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004630 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004631 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004632 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004634 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004636 found_new_match = searchit(NULL, ins_buf, pos,
4637 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004639 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004640 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004641 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004643 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 first_match_pos = *pos;
4646 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004647 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004650 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 found_new_match = FAIL;
4652 if (found_new_match == FAIL)
4653 {
4654 if (ins_buf == curbuf)
4655 found_all = TRUE;
4656 break;
4657 }
4658
4659 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004660 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 && ini->lnum == pos->lnum
4662 && ini->col == pos->col)
4663 continue;
4664 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004665 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004667 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
4669 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4670 continue;
4671 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4672 if (!p_paste)
4673 ptr = skipwhite(ptr);
4674 }
4675 len = (int)STRLEN(ptr);
4676 }
4677 else
4678 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679 char_u *tmp_ptr = ptr;
4680
4681 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 /* Skip if already inside a word. */
4685 if (vim_iswordp(tmp_ptr))
4686 continue;
4687 /* Find start of next word. */
4688 tmp_ptr = find_word_start(tmp_ptr);
4689 }
4690 /* Find end of this word. */
4691 tmp_ptr = find_word_end(tmp_ptr);
4692 len = (int)(tmp_ptr - ptr);
4693
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004694 if ((compl_cont_status & CONT_ADDING)
4695 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
4697 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4698 {
4699 /* Try next line, if any. the new word will be
4700 * "join" as if the normal command "J" was used.
4701 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004702 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 * works -- Acevedo */
4704 STRNCPY(IObuff, ptr, len);
4705 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4706 tmp_ptr = ptr = skipwhite(ptr);
4707 /* Find start of next word. */
4708 tmp_ptr = find_word_start(tmp_ptr);
4709 /* Find end of next word. */
4710 tmp_ptr = find_word_end(tmp_ptr);
4711 if (tmp_ptr > ptr)
4712 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004713 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004715 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 IObuff[len++] = ' ';
4717 /* IObuf =~ "\k.* ", thus len >= 2 */
4718 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004719 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 || (vim_strchr(p_cpo, CPO_JOINSP)
4721 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004722 && (IObuff[len - 2] == '?'
4723 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 IObuff[len++] = ' ';
4725 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004726 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 if (tmp_ptr - ptr >= IOSIZE - len)
4728 tmp_ptr = ptr + IOSIZE - len - 1;
4729 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4730 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004731 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
4733 IObuff[len] = NUL;
4734 ptr = IObuff;
4735 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 continue;
4738 }
4739 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004740 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004741 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004742 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
4744 found_new_match = OK;
4745 break;
4746 }
4747 }
4748 p_scs = save_p_scs;
4749 p_ws = save_p_ws;
4750 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004751
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004752 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004753 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004754 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 found_new_match = OK;
4756
4757 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004758 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4759 * match */
4760 if ((ctrl_x_mode != CTRL_X_NORMAL
4761 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004762 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004763 {
4764 if (got_int)
4765 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004766 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004767 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004768 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004769
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004770 if ((ctrl_x_mode != CTRL_X_NORMAL
4771 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004772 || compl_interrupted)
4773 break;
4774 compl_started = TRUE;
4775 }
4776 else
4777 {
4778 /* Mark a buffer scanned when it has been scanned completely */
4779 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4780 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004782 compl_started = FALSE;
4783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004785 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004787 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 && *e_cpt == NUL) /* Got to end of 'complete' */
4789 found_new_match = FAIL;
4790
4791 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004792 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4793 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 i = ins_compl_make_cyclic();
4795
Bram Moolenaar4475b622017-05-01 20:46:52 +02004796 if (compl_old_match != NULL)
4797 {
4798 /* If several matches were added (FORWARD) or the search failed and has
4799 * just been made cyclic then we have to move compl_curr_match to the
4800 * next or previous entry (if any) -- Acevedo */
4801 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4802 : compl_old_match->cp_prev;
4803 if (compl_curr_match == NULL)
4804 compl_curr_match = compl_old_match;
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return i;
4807}
4808
4809/* Delete the old text being completed. */
4810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004811ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004813 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
4815 /*
4816 * In insert mode: Delete the typed part.
4817 * In replace mode: Put the old characters back, if any.
4818 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004819 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4820 if ((int)curwin->w_cursor.col > col)
4821 {
4822 if (stop_arrow() == FAIL)
4823 return;
4824 backspace_until_column(col);
4825 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004826
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004827 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4828 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004830 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004831 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832}
4833
Bram Moolenaar472e8592016-10-15 17:06:47 +02004834/*
4835 * Insert the new text being completed.
4836 * "in_compl_func" is TRUE when called from complete_check().
4837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004839ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004841 dict_T *dict;
4842
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004843 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004844 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4845 compl_used_match = FALSE;
4846 else
4847 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004848
4849 /* Set completed item. */
4850 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004851 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004852 if (dict != NULL)
4853 {
4854 dict_add_nr_str(dict, "word", 0L,
4855 EMPTY_IF_NULL(compl_shown_match->cp_str));
4856 dict_add_nr_str(dict, "abbr", 0L,
4857 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4858 dict_add_nr_str(dict, "menu", 0L,
4859 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4860 dict_add_nr_str(dict, "kind", 0L,
4861 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4862 dict_add_nr_str(dict, "info", 0L,
4863 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004864 dict_add_nr_str(dict, "user_data", 0L,
4865 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004866 }
4867 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004868 if (!in_compl_func)
4869 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870}
4871
4872/*
4873 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004874 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4875 * get more completions. If it is FALSE, then we just do nothing when there
4876 * are no more completions in a given direction. The latter case is used when
4877 * we are still in the middle of finding completions, to allow browsing
4878 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 * Return the total number of matches, or -1 if still unknown -- webb.
4880 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004881 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4882 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 *
4884 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004885 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4886 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 */
4888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004889ins_compl_next(
4890 int allow_get_expansion,
4891 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004892 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004893 int insert_match, /* Insert the newly selected match */
4894 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004897 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004898 compl_T *found_compl = NULL;
4899 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004900 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004901 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004903 /* When user complete function return -1 for findstart which is next
4904 * time of 'always', compl_shown_match become NULL. */
4905 if (compl_shown_match == NULL)
4906 return -1;
4907
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004908 if (compl_leader != NULL
4909 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004911 /* Set "compl_shown_match" to the actually shown match, it may differ
4912 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004913 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004914 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004915 && compl_shown_match->cp_next != NULL
4916 && compl_shown_match->cp_next != compl_first_match)
4917 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004918
4919 /* If we didn't find it searching forward, and compl_shows_dir is
4920 * backward, find the last match. */
4921 if (compl_shows_dir == BACKWARD
4922 && !ins_compl_equal(compl_shown_match,
4923 compl_leader, (int)STRLEN(compl_leader))
4924 && (compl_shown_match->cp_next == NULL
4925 || compl_shown_match->cp_next == compl_first_match))
4926 {
4927 while (!ins_compl_equal(compl_shown_match,
4928 compl_leader, (int)STRLEN(compl_leader))
4929 && compl_shown_match->cp_prev != NULL
4930 && compl_shown_match->cp_prev != compl_first_match)
4931 compl_shown_match = compl_shown_match->cp_prev;
4932 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004933 }
4934
4935 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004936 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 /* Delete old text to be replaced */
4938 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004939
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004940 /* When finding the longest common text we stick at the original text,
4941 * don't let CTRL-N or CTRL-P move to the first match. */
4942 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4943
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004944 /* When restarting the search don't insert the first match either. */
4945 if (compl_restarting)
4946 {
4947 advance = FALSE;
4948 compl_restarting = FALSE;
4949 }
4950
Bram Moolenaare3226be2005-12-18 22:10:00 +00004951 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4952 * around. */
4953 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004955 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004957 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004958 found_end = (compl_first_match != NULL
4959 && (compl_shown_match->cp_next == compl_first_match
4960 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004961 }
4962 else if (compl_shows_dir == BACKWARD
4963 && compl_shown_match->cp_prev != NULL)
4964 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004965 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004966 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004967 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
4969 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004970 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004971 if (!allow_get_expansion)
4972 {
4973 if (advance)
4974 {
4975 if (compl_shows_dir == BACKWARD)
4976 compl_pending -= todo + 1;
4977 else
4978 compl_pending += todo + 1;
4979 }
4980 return -1;
4981 }
4982
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004983 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004984 {
4985 if (compl_shows_dir == BACKWARD)
4986 --compl_pending;
4987 else
4988 ++compl_pending;
4989 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004990
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004991 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004992 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004993
4994 /* handle any pending completions */
4995 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004996 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004997 {
4998 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4999 {
5000 compl_shown_match = compl_shown_match->cp_next;
5001 --compl_pending;
5002 }
5003 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5004 {
5005 compl_shown_match = compl_shown_match->cp_prev;
5006 ++compl_pending;
5007 }
5008 else
5009 break;
5010 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005011 found_end = FALSE;
5012 }
5013 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5014 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005015 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005016 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005017 ++todo;
5018 else
5019 /* Remember a matching item. */
5020 found_compl = compl_shown_match;
5021
5022 /* Stop at the end of the list when we found a usable match. */
5023 if (found_end)
5024 {
5025 if (found_compl != NULL)
5026 {
5027 compl_shown_match = found_compl;
5028 break;
5029 }
5030 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005034 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005035 if (compl_no_insert && !started)
5036 {
5037 ins_bytes(compl_orig_text + ins_compl_len());
5038 compl_used_match = FALSE;
5039 }
5040 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005041 {
5042 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005043 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005044 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005045 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005046 }
5047 else
5048 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
5050 if (!allow_get_expansion)
5051 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005052 /* may undisplay the popup menu first */
5053 ins_compl_upd_pum();
5054
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005055 /* redraw to show the user what was inserted */
5056 update_screen(0);
5057
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005058 /* display the updated popup menu */
5059 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005060#ifdef FEAT_GUI
5061 if (gui.in_use)
5062 {
5063 /* Show the cursor after the match, not after the redrawn text. */
5064 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005065 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005066 }
5067#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005068
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 /* Delete old text to be replaced, since we're still searching and
5070 * don't want to match ourselves! */
5071 ins_compl_delete();
5072 }
5073
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005074 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005075 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005076 if (compl_no_insert && !started)
5077 compl_enter_selects = TRUE;
5078 else
5079 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 /*
5082 * Show the file name for the match (if any)
5083 * Truncate the file name to avoid a wait for return.
5084 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005085 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005087 char *lead = _("match in file");
5088 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5089 char_u *s;
5090 char_u *e;
5091
5092 if (space > 0)
5093 {
5094 /* We need the tail that fits. With double-byte encoding going
5095 * back from the end is very slow, thus go from the start and keep
5096 * the text that fits in "space" between "s" and "e". */
5097 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5098 {
5099 space -= ptr2cells(e);
5100 while (space < 0)
5101 {
5102 space += ptr2cells(s);
5103 MB_PTR_ADV(s);
5104 }
5105 }
5106 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5107 s > compl_shown_match->cp_fname ? "<" : "", s);
5108 msg(IObuff);
5109 redraw_cmdline = FALSE; /* don't overwrite! */
5110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
5112
5113 return num_matches;
5114}
5115
5116/*
5117 * Call this while finding completions, to check whether the user has hit a key
5118 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005119 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005121 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005122 * "in_compl_func" is TRUE when called from complete_check(), don't set
5123 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 */
5125 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005126ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
5128 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005129 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005131 /* Don't check when reading keys from a script, :normal or feedkeys().
5132 * That would break the test scripts. But do check for keys when called
5133 * from complete_check(). */
5134 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 return;
5136
5137 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005138 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 return;
5140 count = 0;
5141
Bram Moolenaara260a972006-06-23 19:36:29 +00005142 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5143 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (c != NUL)
5146 {
5147 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5148 {
5149 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005150 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005151 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005152 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005154 else
5155 {
5156 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005157 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005158 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005159 if (c != K_IGNORE)
5160 {
5161 /* Don't interrupt completion when the character wasn't typed,
5162 * e.g., when doing @q to replay keys. */
5163 if (c != Ctrl_R && KeyTyped)
5164 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005165
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005166 vungetc(c);
5167 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005170 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005171 {
5172 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5173
5174 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005175 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005176 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005177}
5178
5179/*
5180 * Decide the direction of Insert mode complete from the key typed.
5181 * Returns BACKWARD or FORWARD.
5182 */
5183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005184ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005185{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005186 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005187 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005188 return BACKWARD;
5189 return FORWARD;
5190}
5191
5192/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005193 * Return TRUE for keys that are used for completion only when the popup menu
5194 * is visible.
5195 */
5196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005197ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005198{
5199 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005200 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5201 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005202}
5203
5204/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005205 * Decide the number of completions to move forward.
5206 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5207 */
5208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005209ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005210{
5211 int h;
5212
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005213 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005214 {
5215 h = pum_get_height();
5216 if (h > 3)
5217 h -= 2; /* keep some context */
5218 return h;
5219 }
5220 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221}
5222
5223/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005224 * Return TRUE if completion with "c" should insert the match, FALSE if only
5225 * to change the currently selected completion.
5226 */
5227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005228ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005229{
5230 switch (c)
5231 {
5232 case K_UP:
5233 case K_DOWN:
5234 case K_PAGEDOWN:
5235 case K_KPAGEDOWN:
5236 case K_S_DOWN:
5237 case K_PAGEUP:
5238 case K_KPAGEUP:
5239 case K_S_UP:
5240 return FALSE;
5241 }
5242 return TRUE;
5243}
5244
5245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 * Do Insert mode completion.
5247 * Called when character "c" was typed, which has a meaning for completion.
5248 * Returns OK if completion was done, FAIL if something failed (out of mem).
5249 */
5250 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005251ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005253 char_u *line;
5254 int startcol = 0; /* column where searched text starts */
5255 colnr_T curs_col; /* cursor column */
5256 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005257 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005258 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005259 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005260 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
Bram Moolenaare3226be2005-12-18 22:10:00 +00005262 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005263 insert_match = ins_compl_use_match(c);
5264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
5267 /* First time we hit ^N or ^P (in a row, I mean) */
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 did_ai = FALSE;
5270#ifdef FEAT_SMARTINDENT
5271 did_si = FALSE;
5272 can_si = FALSE;
5273 can_si_back = FALSE;
5274#endif
5275 if (stop_arrow() == FAIL)
5276 return FAIL;
5277
5278 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005279 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005280 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005282 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 * "compl_startpos" to the cursor as a pattern to add a new word
5284 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005285 * "compl_startpos" is not in the same line as the cursor then fix it
5286 * (the line has been split because it was longer than 'tw'). if SOL
5287 * is set then skip the previous pattern, a word at the beginning of
5288 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005289 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5290 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 /*
5293 * it is a continued search
5294 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005295 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005296 if (ctrl_x_mode == CTRL_X_NORMAL
5297 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5298 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005300 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 /* line (probably) wrapped, set compl_startpos to the
5303 * first non_blank in the line, if it is not a wordchar
5304 * include it to get a better pattern, but then we don't
5305 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005306 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005307 compl_startpos.col = compl_col;
5308 compl_startpos.lnum = curwin->w_cursor.lnum;
5309 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
5311 else
5312 {
5313 /* S_IPOS was set when we inserted a word that was at the
5314 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 * mode but first we need to redefine compl_startpos */
5316 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005318 compl_cont_status |= CONT_SOL;
5319 compl_startpos.col = (colnr_T)(skipwhite(
5320 line + compl_length
5321 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005323 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005326 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005327 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005329 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005331 compl_cont_status &= ~CONT_SOL;
5332 compl_length = (IOSIZE - MIN_SPACE);
5333 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5336 if (compl_length < 1)
5337 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005339 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 }
5344 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005345 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005350 if (ctrl_x_mode != CTRL_X_NORMAL)
5351 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 compl_cont_status = 0;
5353 compl_cont_status |= CONT_N_ADDS;
5354 compl_startpos = curwin->w_cursor;
5355 startcol = (int)curs_col;
5356 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
5358
5359 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005360 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5364 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005367 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005369 compl_col += ++startcol;
5370 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
5372 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 compl_pattern = str_foldcase(line + compl_col,
5374 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005376 compl_pattern = vim_strnsave(line + compl_col,
5377 compl_length);
5378 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 return FAIL;
5380 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005381 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
5383 char_u *prefix = (char_u *)"\\<";
5384
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005385 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005386 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005387 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005388 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005390 if (!vim_iswordp(line + compl_col)
5391 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 && (
5393#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005396 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#endif
5398 )))
5399 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005400 STRCPY((char *)compl_pattern, prefix);
5401 (void)quote_meta(compl_pattern + STRLEN(prefix),
5402 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005404 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005406 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005408 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409#endif
5410 )
5411 {
5412 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5414 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005416 compl_col += curs_col;
5417 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
5419 else
5420 {
5421#ifdef FEAT_MBYTE
5422 /* Search the point of change class of multibyte character
5423 * or not a word single byte character backward. */
5424 if (has_mbyte)
5425 {
5426 int base_class;
5427 int head_off;
5428
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 startcol -= (*mb_head_off)(line, line + startcol);
5430 base_class = mb_get_class(line + startcol);
5431 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005433 head_off = (*mb_head_off)(line, line + startcol);
5434 if (base_class != mb_get_class(line + startcol
5435 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005437 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
5439 }
5440 else
5441#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005442 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005444 compl_col += ++startcol;
5445 compl_length = (int)curs_col - startcol;
5446 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
5448 /* Only match word with at least two chars -- webb
5449 * there's no need to call quote_meta,
5450 * alloc(7) is enough -- Acevedo
5451 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005452 compl_pattern = alloc(7);
5453 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 STRCPY((char *)compl_pattern, "\\<");
5456 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5457 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 else
5460 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005462 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005463 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005465 STRCPY((char *)compl_pattern, "\\<");
5466 (void)quote_meta(compl_pattern + 2, line + compl_col,
5467 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469 }
5470 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005471 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005473 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 compl_length = (int)curs_col - (int)compl_col;
5475 if (compl_length < 0) /* cursor in indent: empty pattern */
5476 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005478 compl_pattern = str_foldcase(line + compl_col, compl_length,
5479 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5482 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 return FAIL;
5484 }
5485 else if (ctrl_x_mode == CTRL_X_FILES)
5486 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005487 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005488 if (startcol > 0)
5489 {
5490 char_u *p = line + startcol;
5491
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005492 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005493 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005494 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005495 if (p == line && vim_isfilec(PTR2CHAR(p)))
5496 startcol = 0;
5497 else
5498 startcol = (int)(p - line) + 1;
5499 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005500
Bram Moolenaar0300e462013-09-08 16:03:45 +02005501 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005502 compl_length = (int)curs_col - startcol;
5503 compl_pattern = addstar(line + compl_col, compl_length,
5504 EXPAND_FILES);
5505 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 return FAIL;
5507 }
5508 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5509 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005510 compl_pattern = vim_strnsave(line, curs_col);
5511 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005513 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005514 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5516 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005517 /* No completion possible, use an empty pattern to get a
5518 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005519 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005520 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005521 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5522 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005524 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005525 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005526#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005527 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005528 * Call user defined function 'completefunc' with "a:findstart"
5529 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005530 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005531 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005532 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005533 char_u *funcname;
5534 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005535 win_T *curwin_save;
5536 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005537
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005538 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005539 * string */
5540 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5541 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5542 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005543 {
5544 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5545 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005546 /* restore did_ai, so that adding comment leader works */
5547 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005548 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005549 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005550
5551 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005552 args[1] = NULL;
5553 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005554 curwin_save = curwin;
5555 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005556 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005557 if (curwin_save != curwin || curbuf_save != curbuf)
5558 {
5559 EMSG(_(e_complwin));
5560 return FAIL;
5561 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005562 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005563 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005564 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005565 {
5566 EMSG(_(e_compldel));
5567 return FAIL;
5568 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005569
Bram Moolenaar6110a002012-01-26 18:58:38 +01005570 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005571 * cancel the complete without an error.
5572 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005573 if (col == -2)
5574 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005575 if (col == -3)
5576 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005577 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005578 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005579 if (!shortmess(SHM_COMPLETIONMENU))
5580 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005581 return FAIL;
5582 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005583
Bram Moolenaar82139082011-09-14 16:52:09 +02005584 /*
5585 * Reset extended parameters of completion, when start new
5586 * completion.
5587 */
5588 compl_opt_refresh_always = FALSE;
5589
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005590 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005591 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005592 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005593 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005594 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005595
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005596 /* Setup variables for completion. Need to obtain "line" again,
5597 * it may have become invalid. */
5598 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005599 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005600 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5601 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005602#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005603 return FAIL;
5604 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005605 else if (ctrl_x_mode == CTRL_X_SPELL)
5606 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005607#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005608 if (spell_bad_len > 0)
5609 compl_col = curs_col - spell_bad_len;
5610 else
5611 compl_col = spell_word_start(startcol);
5612 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005613 {
5614 compl_length = 0;
5615 compl_col = curs_col;
5616 }
5617 else
5618 {
5619 spell_expand_check_cap(compl_col);
5620 compl_length = (int)curs_col - compl_col;
5621 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005622 /* Need to obtain "line" again, it may have become invalid. */
5623 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005624 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5625 if (compl_pattern == NULL)
5626#endif
5627 return FAIL;
5628 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005629 else
5630 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005631 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005632 return FAIL;
5633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005635 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
5637 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005638 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
5640 /* Insert a new line, keep indentation but ignore 'comments' */
5641#ifdef FEAT_COMMENTS
5642 char_u *old = curbuf->b_p_com;
5643
5644 curbuf->b_p_com = (char_u *)"";
5645#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005646 compl_startpos.lnum = curwin->w_cursor.lnum;
5647 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 ins_eol('\r');
5649#ifdef FEAT_COMMENTS
5650 curbuf->b_p_com = old;
5651#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005652 compl_length = 0;
5653 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655 }
5656 else
5657 {
5658 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005659 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005662 if (compl_cont_status & CONT_LOCAL)
5663 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 else
5665 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5666
Bram Moolenaar62951b12011-09-21 18:23:05 +02005667 /* If any of the original typed text has been changed we need to fix
5668 * the redo buffer. */
5669 ins_compl_fixRedoBufForLeader(NULL);
5670
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005671 /* Always add completion for the original text. */
5672 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005673 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5674 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005675 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005677 VIM_CLEAR(compl_pattern);
5678 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 return FAIL;
5680 }
5681
5682 /* showmode might reset the internal line pointers, so it must
5683 * be called before line = ml_get(), or when this address is no
5684 * longer needed. -- Acevedo.
5685 */
5686 edit_submode_extra = (char_u *)_("-- Searching...");
5687 edit_submode_highl = HLF_COUNT;
5688 showmode();
5689 edit_submode_extra = NULL;
5690 out_flush();
5691 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005692 else if (insert_match && stop_arrow() == FAIL)
5693 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005695 compl_shown_match = compl_curr_match;
5696 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
5698 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005699 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005701 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005702 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005703 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005705 /* may undisplay the popup menu */
5706 ins_compl_upd_pum();
5707
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005708 if (n > 1) /* all matches have been found */
5709 compl_matches = n;
5710 compl_curr_match = compl_shown_match;
5711 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
Bram Moolenaard68071d2006-05-02 22:08:30 +00005713 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5714 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 if (got_int && !global_busy)
5716 {
5717 (void)vgetc();
5718 got_int = FALSE;
5719 }
5720
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005721 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005722 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005724 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5725 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5727 edit_submode_highl = HLF_E;
5728 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5729 * because we couldn't expand anything at first place, but if we used
5730 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5731 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005732 if ( compl_length > 1
5733 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005734 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5736 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005737 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739
Bram Moolenaar572cb562005-08-05 21:35:02 +00005740 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005741 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005743 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
5745 if (edit_submode_extra == NULL)
5746 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005747 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
5749 edit_submode_extra = (char_u *)_("Back at original");
5750 edit_submode_highl = HLF_W;
5751 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005752 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 edit_submode_extra = (char_u *)_("Word from other line");
5755 edit_submode_highl = HLF_COUNT;
5756 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005757 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {
5759 edit_submode_extra = (char_u *)_("The only match");
5760 edit_submode_highl = HLF_COUNT;
5761 }
5762 else
5763 {
5764 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005765 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005767 int number = 0;
5768 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005770 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
5772 /* search backwards for the first valid (!= -1) number.
5773 * This should normally succeed already at the first loop
5774 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005775 for (match = compl_curr_match->cp_prev; match != NULL
5776 && match != compl_first_match;
5777 match = match->cp_prev)
5778 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005780 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 break;
5782 }
5783 if (match != NULL)
5784 /* go up and assign all numbers which are not assigned
5785 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005786 for (match = match->cp_next;
5787 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005788 match = match->cp_next)
5789 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791 else /* BACKWARD */
5792 {
5793 /* search forwards (upwards) for the first valid (!= -1)
5794 * number. This should normally succeed already at the
5795 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005796 for (match = compl_curr_match->cp_next; match != NULL
5797 && match != compl_first_match;
5798 match = match->cp_next)
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 down and assign all numbers which are not
5806 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005807 for (match = match->cp_prev; match
5808 && match->cp_number == -1;
5809 match = match->cp_prev)
5810 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
5812 }
5813
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005814 /* The match should always have a sequence number now, this is
5815 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005816 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005818 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5819 * Translations may need more than twice that. */
5820 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005822 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005823 vim_snprintf((char *)match_ref, sizeof(match_ref),
5824 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005825 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005827 vim_snprintf((char *)match_ref, sizeof(match_ref),
5828 _("match %d"),
5829 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 edit_submode_extra = match_ref;
5831 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005832 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 curs_columns(FALSE);
5834 }
5835 }
5836 }
5837
5838 /* Show a message about what (completion) mode we're in. */
5839 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005840 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005842 if (edit_submode_extra != NULL)
5843 {
5844 if (!p_smd)
5845 msg_attr(edit_submode_extra,
5846 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005847 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005848 }
5849 else
5850 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
Bram Moolenaard68071d2006-05-02 22:08:30 +00005853 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005854 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005855 show_pum(save_w_wrow, save_w_leftcol);
5856
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005857 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005858 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 return OK;
5861}
5862
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005863 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005864show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005865{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005866 /* RedrawingDisabled may be set when invoked through complete(). */
5867 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005868
Bram Moolenaarcb036422017-03-01 12:29:10 +01005869 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005870
Bram Moolenaarcb036422017-03-01 12:29:10 +01005871 /* If the cursor moved or the display scrolled we need to remove the pum
5872 * first. */
5873 setcursor();
5874 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5875 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005876
Bram Moolenaarcb036422017-03-01 12:29:10 +01005877 ins_compl_show_pum();
5878 setcursor();
5879 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005880}
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882/*
5883 * Looks in the first "len" chars. of "src" for search-metachars.
5884 * If dest is not NULL the chars. are copied there quoting (with
5885 * a backslash) the metachars, and dest would be NUL terminated.
5886 * Returns the length (needed) of dest
5887 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005888 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005889quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005891 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005893 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {
5895 switch (*src)
5896 {
5897 case '.':
5898 case '*':
5899 case '[':
5900 if (ctrl_x_mode == CTRL_X_DICTIONARY
5901 || ctrl_x_mode == CTRL_X_THESAURUS)
5902 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005903 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 case '~':
5905 if (!p_magic) /* quote these only if magic is set */
5906 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005907 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 case '\\':
5909 if (ctrl_x_mode == CTRL_X_DICTIONARY
5910 || ctrl_x_mode == CTRL_X_THESAURUS)
5911 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005912 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 case '^': /* currently it's not needed. */
5914 case '$':
5915 m++;
5916 if (dest != NULL)
5917 *dest++ = '\\';
5918 break;
5919 }
5920 if (dest != NULL)
5921 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005922# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 /* Copy remaining bytes of a multibyte character. */
5924 if (has_mbyte)
5925 {
5926 int i, mb_len;
5927
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005928 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 if (mb_len > 0 && len >= mb_len)
5930 for (i = 0; i < mb_len; ++i)
5931 {
5932 --len;
5933 ++src;
5934 if (dest != NULL)
5935 *dest++ = *src;
5936 }
5937 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 }
5940 if (dest != NULL)
5941 *dest = NUL;
5942
5943 return m;
5944}
5945#endif /* FEAT_INS_EXPAND */
5946
5947/*
5948 * Next character is interpreted literally.
5949 * A one, two or three digit decimal number is interpreted as its byte value.
5950 * If one or two digits are entered, the next character is given to vungetc().
5951 * For Unicode a character > 255 may be returned.
5952 */
5953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005954get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 int cc;
5957 int nc;
5958 int i;
5959 int hex = FALSE;
5960 int octal = FALSE;
5961#ifdef FEAT_MBYTE
5962 int unicode = 0;
5963#endif
5964
5965 if (got_int)
5966 return Ctrl_C;
5967
5968#ifdef FEAT_GUI
5969 /*
5970 * In GUI there is no point inserting the internal code for a special key.
5971 * It is more useful to insert the string "<KEY>" instead. This would
5972 * probably be useful in a text window too, but it would not be
5973 * vi-compatible (maybe there should be an option for it?) -- webb
5974 */
5975 if (gui.in_use)
5976 ++allow_keys;
5977#endif
5978#ifdef USE_ON_FLY_SCROLL
5979 dont_scroll = TRUE; /* disallow scrolling here */
5980#endif
5981 ++no_mapping; /* don't map the next key hits */
5982 cc = 0;
5983 i = 0;
5984 for (;;)
5985 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005986 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987#ifdef FEAT_CMDL_INFO
5988 if (!(State & CMDLINE)
5989# ifdef FEAT_MBYTE
5990 && MB_BYTE2LEN_CHECK(nc) == 1
5991# endif
5992 )
5993 add_to_showcmd(nc);
5994#endif
5995 if (nc == 'x' || nc == 'X')
5996 hex = TRUE;
5997 else if (nc == 'o' || nc == 'O')
5998 octal = TRUE;
5999#ifdef FEAT_MBYTE
6000 else if (nc == 'u' || nc == 'U')
6001 unicode = nc;
6002#endif
6003 else
6004 {
6005 if (hex
6006#ifdef FEAT_MBYTE
6007 || unicode != 0
6008#endif
6009 )
6010 {
6011 if (!vim_isxdigit(nc))
6012 break;
6013 cc = cc * 16 + hex2nr(nc);
6014 }
6015 else if (octal)
6016 {
6017 if (nc < '0' || nc > '7')
6018 break;
6019 cc = cc * 8 + nc - '0';
6020 }
6021 else
6022 {
6023 if (!VIM_ISDIGIT(nc))
6024 break;
6025 cc = cc * 10 + nc - '0';
6026 }
6027
6028 ++i;
6029 }
6030
6031 if (cc > 255
6032#ifdef FEAT_MBYTE
6033 && unicode == 0
6034#endif
6035 )
6036 cc = 255; /* limit range to 0-255 */
6037 nc = 0;
6038
6039 if (hex) /* hex: up to two chars */
6040 {
6041 if (i >= 2)
6042 break;
6043 }
6044#ifdef FEAT_MBYTE
6045 else if (unicode) /* Unicode: up to four or eight chars */
6046 {
6047 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6048 break;
6049 }
6050#endif
6051 else if (i >= 3) /* decimal or octal: up to three chars */
6052 break;
6053 }
6054 if (i == 0) /* no number entered */
6055 {
6056 if (nc == K_ZERO) /* NUL is stored as NL */
6057 {
6058 cc = '\n';
6059 nc = 0;
6060 }
6061 else
6062 {
6063 cc = nc;
6064 nc = 0;
6065 }
6066 }
6067
6068 if (cc == 0) /* NUL is stored as NL */
6069 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006070#ifdef FEAT_MBYTE
6071 if (enc_dbcs && (cc & 0xff) == 0)
6072 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6073 second byte will cause trouble! */
6074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
6076 --no_mapping;
6077#ifdef FEAT_GUI
6078 if (gui.in_use)
6079 --allow_keys;
6080#endif
6081 if (nc)
6082 vungetc(nc);
6083 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6084 return cc;
6085}
6086
6087/*
6088 * Insert character, taking care of special keys and mod_mask
6089 */
6090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091insert_special(
6092 int c,
6093 int allow_modmask,
6094 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 char_u *p;
6097 int len;
6098
6099 /*
6100 * Special function key, translate into "<Key>". Up to the last '>' is
6101 * inserted with ins_str(), so as not to replace characters in replace
6102 * mode.
6103 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6104 * unless 'allow_modmask' is TRUE.
6105 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006106#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 /* Command-key never produces a normal key */
6108 if (mod_mask & MOD_MASK_CMD)
6109 allow_modmask = TRUE;
6110#endif
6111 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6112 {
6113 p = get_special_key_name(c, mod_mask);
6114 len = (int)STRLEN(p);
6115 c = p[len - 1];
6116 if (len > 2)
6117 {
6118 if (stop_arrow() == FAIL)
6119 return;
6120 p[len - 1] = NUL;
6121 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006122 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 ctrlv = FALSE;
6124 }
6125 }
6126 if (stop_arrow() == OK)
6127 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6128}
6129
6130/*
6131 * Special characters in this context are those that need processing other
6132 * than the simple insertion that can be performed here. This includes ESC
6133 * which terminates the insert, and CR/NL which need special processing to
6134 * open up a new line. This routine tries to optimize insertions performed by
6135 * the "redo", "undo" or "put" commands, so it needs to know when it should
6136 * stop and defer processing to the "normal" mechanism.
6137 * '0' and '^' are special, because they can be followed by CTRL-D.
6138 */
6139#ifdef EBCDIC
6140# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6141#else
6142# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6143#endif
6144
6145#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006146# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006148# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149#endif
6150
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006151/*
6152 * "flags": INSCHAR_FORMAT - force formatting
6153 * INSCHAR_CTRLV - char typed just after CTRL-V
6154 * INSCHAR_NO_FEX - don't use 'formatexpr'
6155 *
6156 * NOTE: passes the flags value straight through to internal_format() which,
6157 * beside INSCHAR_FORMAT (above), is also looking for these:
6158 * INSCHAR_DO_COM - format comments
6159 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006162insertchar(
6163 int c, /* character to insert or NUL */
6164 int flags, /* INSCHAR_FORMAT, etc. */
6165 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 int textwidth;
6168#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006172 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006174 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
6177 /*
6178 * Try to break the line in two or more pieces when:
6179 * - Always do this if we have been called to do formatting only.
6180 * - Always do this when 'formatoptions' has the 'a' flag and the line
6181 * ends in white space.
6182 * - Otherwise:
6183 * - Don't do this if inserting a blank
6184 * - Don't do this if an existing character is being replaced, unless
6185 * we're in VREPLACE mode.
6186 * - Do this if the cursor is not on the line where insert started
6187 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6188 * before the insert.
6189 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6190 * before 'textwidth'
6191 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006192 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006193 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006194 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 && !((State & REPLACE_FLAG)
6196#ifdef FEAT_VREPLACE
6197 && !(State & VREPLACE_FLAG)
6198#endif
6199 && *ml_get_cursor() != NUL)
6200 && (curwin->w_cursor.lnum != Insstart.lnum
6201 || ((!has_format_option(FO_INS_LONG)
6202 || Insstart_textlen <= (colnr_T)textwidth)
6203 && (!fo_ins_blank
6204 || Insstart_blank_vcol <= (colnr_T)textwidth
6205 ))))))
6206 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006207 /* Format with 'formatexpr' when it's set. Use internal formatting
6208 * when 'formatexpr' isn't set or it returns non-zero. */
6209#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006210 int do_internal = TRUE;
6211 colnr_T virtcol = get_nolist_virtcol()
6212 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006213
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006214 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6215 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006216 {
6217 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6218 /* It may be required to save for undo again, e.g. when setline()
6219 * was called. */
6220 ins_need_undo = TRUE;
6221 }
6222 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006224 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (c == NUL) /* only formatting was wanted */
6228 return;
6229
6230#ifdef FEAT_COMMENTS
6231 /* Check whether this character should end a comment. */
6232 if (did_ai && (int)c == end_comment_pending)
6233 {
6234 char_u *line;
6235 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6236 int middle_len, end_len;
6237 int i;
6238
6239 /*
6240 * Need to remove existing (middle) comment leader and insert end
6241 * comment leader. First, check what comment leader we can find.
6242 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006243 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6245 {
6246 /* Skip middle-comment string */
6247 while (*p && p[-1] != ':') /* find end of middle flags */
6248 ++p;
6249 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6250 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006251 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 --middle_len;
6253
6254 /* Find the end-comment string */
6255 while (*p && p[-1] != ':') /* find end of end flags */
6256 ++p;
6257 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6258
6259 /* Skip white space before the cursor */
6260 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006261 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 ;
6263 i++;
6264
6265 /* Skip to before the middle leader */
6266 i -= middle_len;
6267
6268 /* Check some expected things before we go on */
6269 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6270 {
6271 /* Backspace over all the stuff we want to replace */
6272 backspace_until_column(i);
6273
6274 /*
6275 * Insert the end-comment string, except for the last
6276 * character, which will get inserted as normal later.
6277 */
6278 ins_bytes_len(lead_end, end_len - 1);
6279 }
6280 }
6281 }
6282 end_comment_pending = NUL;
6283#endif
6284
6285 did_ai = FALSE;
6286#ifdef FEAT_SMARTINDENT
6287 did_si = FALSE;
6288 can_si = FALSE;
6289 can_si_back = FALSE;
6290#endif
6291
6292 /*
6293 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6294 * This speeds up normal text input considerably.
6295 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6296 * need to re-indent at a ':', or any other character (but not what
6297 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006298 * Don't do this when there an InsertCharPre autocommand is defined,
6299 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006300 * Do the check for InsertCharPre before the call to vpeekc() because the
6301 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 */
6303#ifdef USE_ON_FLY_SCROLL
6304 dont_scroll = FALSE; /* allow scrolling here */
6305#endif
6306
6307 if ( !ISSPECIAL(c)
6308#ifdef FEAT_MBYTE
6309 && (!has_mbyte || (*mb_char2len)(c) == 1)
6310#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006311 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 && vpeekc() != NUL
6313 && !(State & REPLACE_FLAG)
6314#ifdef FEAT_CINDENT
6315 && !cindent_on()
6316#endif
6317#ifdef FEAT_RIGHTLEFT
6318 && !p_ri
6319#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006320 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 {
6322#define INPUT_BUFLEN 100
6323 char_u buf[INPUT_BUFLEN + 1];
6324 int i;
6325 colnr_T virtcol = 0;
6326
6327 buf[0] = c;
6328 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006329 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 virtcol = get_nolist_virtcol();
6331 /*
6332 * Stop the string when:
6333 * - no more chars available
6334 * - finding a special character (command key)
6335 * - buffer is full
6336 * - running into the 'textwidth' boundary
6337 * - need to check for abbreviation: A non-word char after a word-char
6338 */
6339 while ( (c = vpeekc()) != NUL
6340 && !ISSPECIAL(c)
6341#ifdef FEAT_MBYTE
6342 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6343#endif
6344 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006345# ifdef FEAT_FKMAP
6346 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 && (textwidth == 0
6349 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6350 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6351 {
6352#ifdef FEAT_RIGHTLEFT
6353 c = vgetc();
6354 if (p_hkmap && KeyTyped)
6355 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 buf[i++] = c;
6357#else
6358 buf[i++] = vgetc();
6359#endif
6360 }
6361
6362#ifdef FEAT_DIGRAPHS
6363 do_digraph(-1); /* clear digraphs */
6364 do_digraph(buf[i-1]); /* may be the start of a digraph */
6365#endif
6366 buf[i] = NUL;
6367 ins_str(buf);
6368 if (flags & INSCHAR_CTRLV)
6369 {
6370 redo_literal(*buf);
6371 i = 1;
6372 }
6373 else
6374 i = 0;
6375 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006376 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 }
6378 else
6379 {
6380#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006381 int cc;
6382
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6384 {
6385 char_u buf[MB_MAXBYTES + 1];
6386
6387 (*mb_char2bytes)(c, buf);
6388 buf[cc] = NUL;
6389 ins_char_bytes(buf, cc);
6390 AppendCharToRedobuff(c);
6391 }
6392 else
6393#endif
6394 {
6395 ins_char(c);
6396 if (flags & INSCHAR_CTRLV)
6397 redo_literal(c);
6398 else
6399 AppendCharToRedobuff(c);
6400 }
6401 }
6402}
6403
6404/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006405 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006406 *
6407 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6408 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006409 */
6410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006411internal_format(
6412 int textwidth,
6413 int second_indent,
6414 int flags,
6415 int format_only,
6416 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006417{
6418 int cc;
6419 int save_char = NUL;
6420 int haveto_redraw = FALSE;
6421 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6422#ifdef FEAT_MBYTE
6423 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6424#endif
6425 int fo_white_par = has_format_option(FO_WHITE_PAR);
6426 int first_line = TRUE;
6427#ifdef FEAT_COMMENTS
6428 colnr_T leader_len;
6429 int no_leader = FALSE;
6430 int do_comments = (flags & INSCHAR_DO_COM);
6431#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006432#ifdef FEAT_LINEBREAK
6433 int has_lbr = curwin->w_p_lbr;
6434
6435 /* make sure win_lbr_chartabsize() counts correctly */
6436 curwin->w_p_lbr = FALSE;
6437#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006438
6439 /*
6440 * When 'ai' is off we don't want a space under the cursor to be
6441 * deleted. Replace it with an 'x' temporarily.
6442 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006443 if (!curbuf->b_p_ai
6444#ifdef FEAT_VREPLACE
6445 && !(State & VREPLACE_FLAG)
6446#endif
6447 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006448 {
6449 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006450 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006451 {
6452 save_char = cc;
6453 pchar_cursor('x');
6454 }
6455 }
6456
6457 /*
6458 * Repeat breaking lines, until the current line is not too long.
6459 */
6460 while (!got_int)
6461 {
6462 int startcol; /* Cursor column at entry */
6463 int wantcol; /* column at textwidth border */
6464 int foundcol; /* column for start of spaces */
6465 int end_foundcol = 0; /* column for start of word */
6466 colnr_T len;
6467 colnr_T virtcol;
6468#ifdef FEAT_VREPLACE
6469 int orig_col = 0;
6470 char_u *saved_text = NULL;
6471#endif
6472 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006473 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006474
Bram Moolenaar97b98102009-11-17 16:41:01 +00006475 virtcol = get_nolist_virtcol()
6476 + char2cells(c != NUL ? c : gchar_cursor());
6477 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006478 break;
6479
6480#ifdef FEAT_COMMENTS
6481 if (no_leader)
6482 do_comments = FALSE;
6483 else if (!(flags & INSCHAR_FORMAT)
6484 && has_format_option(FO_WRAP_COMS))
6485 do_comments = TRUE;
6486
6487 /* Don't break until after the comment leader */
6488 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006489 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006490 else
6491 leader_len = 0;
6492
6493 /* If the line doesn't start with a comment leader, then don't
6494 * start one in a following broken line. Avoids that a %word
6495 * moved to the start of the next line causes all following lines
6496 * to start with %. */
6497 if (leader_len == 0)
6498 no_leader = TRUE;
6499#endif
6500 if (!(flags & INSCHAR_FORMAT)
6501#ifdef FEAT_COMMENTS
6502 && leader_len == 0
6503#endif
6504 && !has_format_option(FO_WRAP))
6505
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006506 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006507 if ((startcol = curwin->w_cursor.col) == 0)
6508 break;
6509
6510 /* find column of textwidth border */
6511 coladvance((colnr_T)textwidth);
6512 wantcol = curwin->w_cursor.col;
6513
Bram Moolenaar97b98102009-11-17 16:41:01 +00006514 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515 foundcol = 0;
6516
6517 /*
6518 * Find position to break at.
6519 * Stop at first entered white when 'formatoptions' has 'v'
6520 */
6521 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006522 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523 || curwin->w_cursor.lnum != Insstart.lnum
6524 || curwin->w_cursor.col >= Insstart.col)
6525 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006526 if (curwin->w_cursor.col == startcol && c != NUL)
6527 cc = c;
6528 else
6529 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006530 if (WHITECHAR(cc))
6531 {
6532 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006533 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006534
6535 /* find start of sequence of blanks */
6536 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6537 {
6538 dec_cursor();
6539 cc = gchar_cursor();
6540 }
6541 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6542 break; /* only spaces in front of text */
6543#ifdef FEAT_COMMENTS
6544 /* Don't break until after the comment leader */
6545 if (curwin->w_cursor.col < leader_len)
6546 break;
6547#endif
6548 if (has_format_option(FO_ONE_LETTER))
6549 {
6550 /* do not break after one-letter words */
6551 if (curwin->w_cursor.col == 0)
6552 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006553#ifdef FEAT_COMMENTS
6554 /* do not break "#a b" when 'tw' is 2 */
6555 if (curwin->w_cursor.col <= leader_len)
6556 break;
6557#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006558 col = curwin->w_cursor.col;
6559 dec_cursor();
6560 cc = gchar_cursor();
6561
6562 if (WHITECHAR(cc))
6563 continue; /* one-letter, continue */
6564 curwin->w_cursor.col = col;
6565 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006566
6567 inc_cursor();
6568
6569 end_foundcol = end_col + 1;
6570 foundcol = curwin->w_cursor.col;
6571 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006572 break;
6573 }
6574#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006575 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006576 {
6577 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006578 if (curwin->w_cursor.col != startcol)
6579 {
6580#ifdef FEAT_COMMENTS
6581 /* Don't break until after the comment leader */
6582 if (curwin->w_cursor.col < leader_len)
6583 break;
6584#endif
6585 col = curwin->w_cursor.col;
6586 inc_cursor();
6587 /* Don't change end_foundcol if already set. */
6588 if (foundcol != curwin->w_cursor.col)
6589 {
6590 foundcol = curwin->w_cursor.col;
6591 end_foundcol = foundcol;
6592 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6593 break;
6594 }
6595 curwin->w_cursor.col = col;
6596 }
6597
6598 if (curwin->w_cursor.col == 0)
6599 break;
6600
6601 col = curwin->w_cursor.col;
6602
6603 dec_cursor();
6604 cc = gchar_cursor();
6605
6606 if (WHITECHAR(cc))
6607 continue; /* break with space */
6608#ifdef FEAT_COMMENTS
6609 /* Don't break until after the comment leader */
6610 if (curwin->w_cursor.col < leader_len)
6611 break;
6612#endif
6613
6614 curwin->w_cursor.col = col;
6615
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006616 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006617 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006618 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6619 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006620 }
6621#endif
6622 if (curwin->w_cursor.col == 0)
6623 break;
6624 dec_cursor();
6625 }
6626
6627 if (foundcol == 0) /* no spaces, cannot break line */
6628 {
6629 curwin->w_cursor.col = startcol;
6630 break;
6631 }
6632
6633 /* Going to break the line, remove any "$" now. */
6634 undisplay_dollar();
6635
6636 /*
6637 * Offset between cursor position and line break is used by replace
6638 * stack functions. VREPLACE does not use this, and backspaces
6639 * over the text instead.
6640 */
6641#ifdef FEAT_VREPLACE
6642 if (State & VREPLACE_FLAG)
6643 orig_col = startcol; /* Will start backspacing from here */
6644 else
6645#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006646 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006647
6648 /*
6649 * adjust startcol for spaces that will be deleted and
6650 * characters that will remain on top line
6651 */
6652 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006653 while ((cc = gchar_cursor(), WHITECHAR(cc))
6654 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006655 inc_cursor();
6656 startcol -= curwin->w_cursor.col;
6657 if (startcol < 0)
6658 startcol = 0;
6659
6660#ifdef FEAT_VREPLACE
6661 if (State & VREPLACE_FLAG)
6662 {
6663 /*
6664 * In VREPLACE mode, we will backspace over the text to be
6665 * wrapped, so save a copy now to put on the next line.
6666 */
6667 saved_text = vim_strsave(ml_get_cursor());
6668 curwin->w_cursor.col = orig_col;
6669 if (saved_text == NULL)
6670 break; /* Can't do it, out of memory */
6671 saved_text[startcol] = NUL;
6672
6673 /* Backspace over characters that will move to the next line */
6674 if (!fo_white_par)
6675 backspace_until_column(foundcol);
6676 }
6677 else
6678#endif
6679 {
6680 /* put cursor after pos. to break line */
6681 if (!fo_white_par)
6682 curwin->w_cursor.col = foundcol;
6683 }
6684
6685 /*
6686 * Split the line just before the margin.
6687 * Only insert/delete lines, but don't really redraw the window.
6688 */
6689 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6690 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6691#ifdef FEAT_COMMENTS
6692 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006693 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006694#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006695 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6696 if (!(flags & INSCHAR_COM_LIST))
6697 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698
6699 replace_offset = 0;
6700 if (first_line)
6701 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006702 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006703 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006704 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006705 * This section is for auto-wrap of numeric lists. When not
6706 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6707 * flag will be set and open_line() will handle it (as seen
6708 * above). The code here (and in get_number_indent()) will
6709 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006710 */
6711 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006712 second_indent =
6713 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006714 if (second_indent >= 0)
6715 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006716#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006717 if (State & VREPLACE_FLAG)
6718 change_indent(INDENT_SET, second_indent,
6719 FALSE, NUL, TRUE);
6720 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006721#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006722#ifdef FEAT_COMMENTS
6723 if (leader_len > 0 && second_indent - leader_len > 0)
6724 {
6725 int i;
6726 int padding = second_indent - leader_len;
6727
6728 /* We started at the first_line of a numbered list
6729 * that has a comment. the open_line() function has
6730 * inserted the proper comment leader and positioned
6731 * the cursor at the end of the split line. Now we
6732 * add the additional whitespace needed after the
6733 * comment leader for the numbered list. */
6734 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006735 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006736 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006737 }
6738 else
6739 {
6740#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006741 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006742#ifdef FEAT_COMMENTS
6743 }
6744#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006745 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006746 }
6747 first_line = FALSE;
6748 }
6749
6750#ifdef FEAT_VREPLACE
6751 if (State & VREPLACE_FLAG)
6752 {
6753 /*
6754 * In VREPLACE mode we have backspaced over the text to be
6755 * moved, now we re-insert it into the new line.
6756 */
6757 ins_bytes(saved_text);
6758 vim_free(saved_text);
6759 }
6760 else
6761#endif
6762 {
6763 /*
6764 * Check if cursor is not past the NUL off the line, cindent
6765 * may have added or removed indent.
6766 */
6767 curwin->w_cursor.col += startcol;
6768 len = (colnr_T)STRLEN(ml_get_curline());
6769 if (curwin->w_cursor.col > len)
6770 curwin->w_cursor.col = len;
6771 }
6772
6773 haveto_redraw = TRUE;
6774#ifdef FEAT_CINDENT
6775 can_cindent = TRUE;
6776#endif
6777 /* moved the cursor, don't autoindent or cindent now */
6778 did_ai = FALSE;
6779#ifdef FEAT_SMARTINDENT
6780 did_si = FALSE;
6781 can_si = FALSE;
6782 can_si_back = FALSE;
6783#endif
6784 line_breakcheck();
6785 }
6786
6787 if (save_char != NUL) /* put back space after cursor */
6788 pchar_cursor(save_char);
6789
Bram Moolenaar0026d472014-09-09 16:32:39 +02006790#ifdef FEAT_LINEBREAK
6791 curwin->w_p_lbr = has_lbr;
6792#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006793 if (!format_only && haveto_redraw)
6794 {
6795 update_topline();
6796 redraw_curbuf_later(VALID);
6797 }
6798}
6799
6800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 * Called after inserting or deleting text: When 'formatoptions' includes the
6802 * 'a' flag format from the current line until the end of the paragraph.
6803 * Keep the cursor at the same position relative to the text.
6804 * The caller must have saved the cursor line for undo, following ones will be
6805 * saved here.
6806 */
6807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006808auto_format(
6809 int trailblank, /* when TRUE also format with trailing blank */
6810 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
6812 pos_T pos;
6813 colnr_T len;
6814 char_u *old;
6815 char_u *new, *pnew;
6816 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006817 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819 if (!has_format_option(FO_AUTO))
6820 return;
6821
6822 pos = curwin->w_cursor;
6823 old = ml_get_curline();
6824
6825 /* may remove added space */
6826 check_auto_format(FALSE);
6827
6828 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6829 * user might insert normal text next. Also skip formatting when "1" is
6830 * in 'formatoptions' and there is a single character before the cursor.
6831 * Otherwise the line would be broken and when typing another non-white
6832 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006833 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (*old != NUL && !trailblank && wasatend)
6835 {
6836 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006837 cc = gchar_cursor();
6838 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6839 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006841 cc = gchar_cursor();
6842 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 {
6844 curwin->w_cursor = pos;
6845 return;
6846 }
6847 curwin->w_cursor = pos;
6848 }
6849
6850#ifdef FEAT_COMMENTS
6851 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6852 * comments. */
6853 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006854 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 return;
6856#endif
6857
6858 /*
6859 * May start formatting in a previous line, so that after "x" a word is
6860 * moved to the previous line if it fits there now. Only when this is not
6861 * the start of a paragraph.
6862 */
6863 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6864 {
6865 --curwin->w_cursor.lnum;
6866 if (u_save_cursor() == FAIL)
6867 return;
6868 }
6869
6870 /*
6871 * Do the formatting and restore the cursor position. "saved_cursor" will
6872 * be adjusted for the text formatting.
6873 */
6874 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006875 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 curwin->w_cursor = saved_cursor;
6877 saved_cursor.lnum = 0;
6878
6879 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6880 {
6881 /* "cannot happen" */
6882 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6883 coladvance((colnr_T)MAXCOL);
6884 }
6885 else
6886 check_cursor_col();
6887
6888 /* Insert mode: If the cursor is now after the end of the line while it
6889 * previously wasn't, the line was broken. Because of the rule above we
6890 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6891 * formatted. */
6892 if (!wasatend && has_format_option(FO_WHITE_PAR))
6893 {
6894 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006895 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 if (curwin->w_cursor.col == len)
6897 {
6898 pnew = vim_strnsave(new, len + 2);
6899 pnew[len] = ' ';
6900 pnew[len + 1] = NUL;
6901 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6902 /* remove the space later */
6903 did_add_space = TRUE;
6904 }
6905 else
6906 /* may remove added space */
6907 check_auto_format(FALSE);
6908 }
6909
6910 check_cursor();
6911}
6912
6913/*
6914 * When an extra space was added to continue a paragraph for auto-formatting,
6915 * delete it now. The space must be under the cursor, just after the insert
6916 * position.
6917 */
6918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006919check_auto_format(
6920 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
6922 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006923 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
6925 if (did_add_space)
6926 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006927 cc = gchar_cursor();
6928 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 /* Somehow the space was removed already. */
6930 did_add_space = FALSE;
6931 else
6932 {
6933 if (!end_insert)
6934 {
6935 inc_cursor();
6936 c = gchar_cursor();
6937 dec_cursor();
6938 }
6939 if (c != NUL)
6940 {
6941 /* The space is no longer at the end of the line, delete it. */
6942 del_char(FALSE);
6943 did_add_space = FALSE;
6944 }
6945 }
6946 }
6947}
6948
6949/*
6950 * Find out textwidth to be used for formatting:
6951 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006952 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 * if invalid value, use 0.
6954 * Set default to window width (maximum 79) for "gq" operator.
6955 */
6956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006957comp_textwidth(
6958 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 int textwidth;
6961
6962 textwidth = curbuf->b_p_tw;
6963 if (textwidth == 0 && curbuf->b_p_wm)
6964 {
6965 /* The width is the window width minus 'wrapmargin' minus all the
6966 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006967 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968#ifdef FEAT_CMDWIN
6969 if (cmdwin_type != 0)
6970 textwidth -= 1;
6971#endif
6972#ifdef FEAT_FOLDING
6973 textwidth -= curwin->w_p_fdc;
6974#endif
6975#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006976 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 textwidth -= 1;
6978#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006979 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 textwidth -= 8;
6981 }
6982 if (textwidth < 0)
6983 textwidth = 0;
6984 if (ff && textwidth == 0)
6985 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006986 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (textwidth > 79)
6988 textwidth = 79;
6989 }
6990 return textwidth;
6991}
6992
6993/*
6994 * Put a character in the redo buffer, for when just after a CTRL-V.
6995 */
6996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006997redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 char_u buf[10];
7000
7001 /* Only digits need special treatment. Translate them into a string of
7002 * three digits. */
7003 if (VIM_ISDIGIT(c))
7004 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007005 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 AppendToRedobuff(buf);
7007 }
7008 else
7009 AppendCharToRedobuff(c);
7010}
7011
7012/*
7013 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007014 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 */
7016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007017start_arrow(
7018 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007020 start_arrow_common(end_insert_pos, TRUE);
7021}
7022
7023/*
7024 * Like start_arrow() but with end_change argument.
7025 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7026 */
7027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007028start_arrow_with_change(
7029 pos_T *end_insert_pos, /* can be NULL */
7030 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007031{
7032 start_arrow_common(end_insert_pos, end_change);
7033 if (!end_change)
7034 {
7035 AppendCharToRedobuff(Ctrl_G);
7036 AppendCharToRedobuff('U');
7037 }
7038}
7039
7040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007041start_arrow_common(
7042 pos_T *end_insert_pos, /* can be NULL */
7043 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007044{
7045 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {
7047 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007048 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 arrow_used = TRUE; /* this means we stopped the current insert */
7050 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007051#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007052 check_spell_redraw();
7053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007056#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007057/*
7058 * If we skipped highlighting word at cursor, do it now.
7059 * It may be skipped again, thus reset spell_redraw_lnum first.
7060 */
7061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007062check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007063{
7064 if (spell_redraw_lnum != 0)
7065 {
7066 linenr_T lnum = spell_redraw_lnum;
7067
7068 spell_redraw_lnum = 0;
7069 redrawWinline(lnum, FALSE);
7070 }
7071}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007072
7073/*
7074 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7075 * spelled word, if there is one.
7076 */
7077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007079{
7080 pos_T tpos = curwin->w_cursor;
7081
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007082 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007083 if (curwin->w_cursor.col != tpos.col)
7084 start_arrow(&tpos);
7085}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007086#endif
7087
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088/*
7089 * stop_arrow() is called before a change is made in insert mode.
7090 * If an arrow key has been used, start a new insertion.
7091 * Returns FAIL if undo is impossible, shouldn't insert then.
7092 */
7093 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007094stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
7096 if (arrow_used)
7097 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007098 Insstart = curwin->w_cursor; /* new insertion starts here */
7099 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7100 /* Don't update the original insert position when moved to the
7101 * right, except when nothing was inserted yet. */
7102 update_Insstart_orig = FALSE;
7103 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (u_save_cursor() == OK)
7106 {
7107 arrow_used = FALSE;
7108 ins_need_undo = FALSE;
7109 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 ai_col = 0;
7112#ifdef FEAT_VREPLACE
7113 if (State & VREPLACE_FLAG)
7114 {
7115 orig_line_count = curbuf->b_ml.ml_line_count;
7116 vr_lines_changed = 1;
7117 }
7118#endif
7119 ResetRedobuff();
7120 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007121 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 }
7123 else if (ins_need_undo)
7124 {
7125 if (u_save_cursor() == OK)
7126 ins_need_undo = FALSE;
7127 }
7128
7129#ifdef FEAT_FOLDING
7130 /* Always open fold at the cursor line when inserting something. */
7131 foldOpenCursor();
7132#endif
7133
7134 return (arrow_used || ins_need_undo ? FAIL : OK);
7135}
7136
7137/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007138 * Do a few things to stop inserting.
7139 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7140 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 */
7142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007143stop_insert(
7144 pos_T *end_insert_pos,
7145 int esc, /* called by ins_esc() */
7146 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007148 int cc;
7149 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151 stop_redo_ins();
7152 replace_flush(); /* abandon replace stack */
7153
7154 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007155 * Save the inserted text for later redo with ^@ and CTRL-A.
7156 * Don't do it when "restart_edit" was set and nothing was inserted,
7157 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007159 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007160 if (did_restart_edit == 0 || (ptr != NULL
7161 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007162 {
7163 vim_free(last_insert);
7164 last_insert = ptr;
7165 last_insert_skip = new_insert_skip;
7166 }
7167 else
7168 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007170 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {
7172 /* Auto-format now. It may seem strange to do this when stopping an
7173 * insertion (or moving the cursor), but it's required when appending
7174 * a line and having it end in a space. But only do it when something
7175 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007176 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007178 pos_T tpos = curwin->w_cursor;
7179
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 /* When the cursor is at the end of the line after a space the
7181 * formatting will move it to the following word. Avoid that by
7182 * moving the cursor onto the space. */
7183 cc = 'x';
7184 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7185 {
7186 dec_cursor();
7187 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007188 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007189 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
7191
7192 auto_format(TRUE, FALSE);
7193
Bram Moolenaar1c465442017-03-12 20:10:05 +01007194 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007195 {
7196 if (gchar_cursor() != NUL)
7197 inc_cursor();
7198#ifdef FEAT_VIRTUALEDIT
7199 /* If the cursor is still at the same character, also keep
7200 * the "coladd". */
7201 if (gchar_cursor() == NUL
7202 && curwin->w_cursor.lnum == tpos.lnum
7203 && curwin->w_cursor.col == tpos.col)
7204 curwin->w_cursor.coladd = tpos.coladd;
7205#endif
7206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208
7209 /* If a space was inserted for auto-formatting, remove it now. */
7210 check_auto_format(TRUE);
7211
7212 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007213 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007214 * Do this when ESC was used or moving the cursor up/down.
7215 * Check for the old position still being valid, just in case the text
7216 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007217 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007218 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7219 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007221 pos_T tpos = curwin->w_cursor;
7222
7223 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007224 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007225 for (;;)
7226 {
7227 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7228 --curwin->w_cursor.col;
7229 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007230 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007231 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007232 if (del_char(TRUE) == FAIL)
7233 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007234 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007235 if (curwin->w_cursor.lnum != tpos.lnum)
7236 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007237 else
7238 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007239 /* reset tpos, could have been invalidated in the loop above */
7240 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007241 tpos.col++;
7242 if (cc != NUL && gchar_pos(&tpos) == NUL)
7243 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 /* <C-S-Right> may have started Visual mode, adjust the position for
7247 * deleted characters. */
7248 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7249 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007250 int len = (int)STRLEN(ml_get_curline());
7251
7252 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007254 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007255#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261 }
7262 did_ai = FALSE;
7263#ifdef FEAT_SMARTINDENT
7264 did_si = FALSE;
7265 can_si = FALSE;
7266 can_si_back = FALSE;
7267#endif
7268
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007269 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7270 * now in a different buffer. */
7271 if (end_insert_pos != NULL)
7272 {
7273 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007274 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007275 curbuf->b_op_end = *end_insert_pos;
7276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277}
7278
7279/*
7280 * Set the last inserted text to a single character.
7281 * Used for the replace command.
7282 */
7283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007284set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
7286 char_u *s;
7287
7288 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 if (last_insert != NULL)
7291 {
7292 s = last_insert;
7293 /* Use the CTRL-V only when entering a special char */
7294 if (c < ' ' || c == DEL)
7295 *s++ = Ctrl_V;
7296 s = add_char2buf(c, s);
7297 *s++ = ESC;
7298 *s++ = NUL;
7299 last_insert_skip = 0;
7300 }
7301}
7302
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007303#if defined(EXITFREE) || defined(PROTO)
7304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007305free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007306{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007307 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007308# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007309 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007310# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007311}
7312#endif
7313
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314/*
7315 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7316 * and CSI. Handle multi-byte characters.
7317 * Returns a pointer to after the added bytes.
7318 */
7319 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007320add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007323 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 int i;
7325 int len;
7326
7327 len = (*mb_char2bytes)(c, temp);
7328 for (i = 0; i < len; ++i)
7329 {
7330 c = temp[i];
7331#endif
7332 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7333 if (c == K_SPECIAL)
7334 {
7335 *s++ = K_SPECIAL;
7336 *s++ = KS_SPECIAL;
7337 *s++ = KE_FILLER;
7338 }
7339#ifdef FEAT_GUI
7340 else if (c == CSI)
7341 {
7342 *s++ = CSI;
7343 *s++ = KS_EXTRA;
7344 *s++ = (int)KE_CSI;
7345 }
7346#endif
7347 else
7348 *s++ = c;
7349#ifdef FEAT_MBYTE
7350 }
7351#endif
7352 return s;
7353}
7354
7355/*
7356 * move cursor to start of line
7357 * if flags & BL_WHITE move to first non-white
7358 * if flags & BL_SOL move to first non-white if startofline is set,
7359 * otherwise keep "curswant" column
7360 * if flags & BL_FIX don't leave the cursor on a NUL.
7361 */
7362 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007363beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
7365 if ((flags & BL_SOL) && !p_sol)
7366 coladvance(curwin->w_curswant);
7367 else
7368 {
7369 curwin->w_cursor.col = 0;
7370#ifdef FEAT_VIRTUALEDIT
7371 curwin->w_cursor.coladd = 0;
7372#endif
7373
7374 if (flags & (BL_WHITE | BL_SOL))
7375 {
7376 char_u *ptr;
7377
Bram Moolenaar1c465442017-03-12 20:10:05 +01007378 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7380 ++curwin->w_cursor.col;
7381 }
7382 curwin->w_set_curswant = TRUE;
7383 }
7384}
7385
7386/*
7387 * oneright oneleft cursor_down cursor_up
7388 *
7389 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007390 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 * Return OK when successful, FAIL when we hit a line of file boundary.
7392 */
7393
7394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007395oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
7400#ifdef FEAT_VIRTUALEDIT
7401 if (virtual_active())
7402 {
7403 pos_T prevpos = curwin->w_cursor;
7404
7405 /* Adjust for multi-wide char (excluding TAB) */
7406 ptr = ml_get_cursor();
7407 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007408# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007410# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 ))
7414 ? ptr2cells(ptr) : 1));
7415 curwin->w_set_curswant = TRUE;
7416 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7417 return (prevpos.col != curwin->w_cursor.col
7418 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7419 }
7420#endif
7421
7422 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007423 if (*ptr == NUL)
7424 return FAIL; /* already at the very end */
7425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007427 if (has_mbyte)
7428 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 else
7430#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007431 l = 1;
7432
7433 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7434 * contains "onemore". */
7435 if (ptr[l] == NUL
7436#ifdef FEAT_VIRTUALEDIT
7437 && (ve_flags & VE_ONEMORE) == 0
7438#endif
7439 )
7440 return FAIL;
7441 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
7443 curwin->w_set_curswant = TRUE;
7444 return OK;
7445}
7446
7447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007448oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
7450#ifdef FEAT_VIRTUALEDIT
7451 if (virtual_active())
7452 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007453# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 int v = getviscol();
7457
7458 if (v == 0)
7459 return FAIL;
7460
7461# ifdef FEAT_LINEBREAK
7462 /* We might get stuck on 'showbreak', skip over it. */
7463 width = 1;
7464 for (;;)
7465 {
7466 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007467 /* getviscol() is slow, skip it when 'showbreak' is empty,
7468 * 'breakindent' is not set and there are no multi-byte
7469 * characters */
7470 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471# ifdef FEAT_MBYTE
7472 && !has_mbyte
7473# endif
7474 ) || getviscol() < v)
7475 break;
7476 ++width;
7477 }
7478# else
7479 coladvance(v - 1);
7480# endif
7481
7482 if (curwin->w_cursor.coladd == 1)
7483 {
7484 char_u *ptr;
7485
7486 /* Adjust for multi-wide char (not a TAB) */
7487 ptr = ml_get_cursor();
7488 if (*ptr != TAB && vim_isprintc(
7489# ifdef FEAT_MBYTE
7490 (*mb_ptr2char)(ptr)
7491# else
7492 *ptr
7493# endif
7494 ) && ptr2cells(ptr) > 1)
7495 curwin->w_cursor.coladd = 0;
7496 }
7497
7498 curwin->w_set_curswant = TRUE;
7499 return OK;
7500 }
7501#endif
7502
7503 if (curwin->w_cursor.col == 0)
7504 return FAIL;
7505
7506 curwin->w_set_curswant = TRUE;
7507 --curwin->w_cursor.col;
7508
7509#ifdef FEAT_MBYTE
7510 /* if the character on the left of the current cursor is a multi-byte
7511 * character, move to its first byte */
7512 if (has_mbyte)
7513 mb_adjust_cursor();
7514#endif
7515 return OK;
7516}
7517
7518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007519cursor_up(
7520 long n,
7521 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 linenr_T lnum;
7524
7525 if (n > 0)
7526 {
7527 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007528 /* This fails if the cursor is already in the first line or the count
7529 * is larger than the line number and '-' is in 'cpoptions' */
7530 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 return FAIL;
7532 if (n >= lnum)
7533 lnum = 1;
7534 else
7535#ifdef FEAT_FOLDING
7536 if (hasAnyFolding(curwin))
7537 {
7538 /*
7539 * Count each sequence of folded lines as one logical line.
7540 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007541 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 (void)hasFolding(lnum, &lnum, NULL);
7543
7544 while (n--)
7545 {
7546 /* move up one line */
7547 --lnum;
7548 if (lnum <= 1)
7549 break;
7550 /* If we entered a fold, move to the beginning, unless in
7551 * Insert mode or when 'foldopen' contains "all": it will open
7552 * in a moment. */
7553 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7554 (void)hasFolding(lnum, &lnum, NULL);
7555 }
7556 if (lnum < 1)
7557 lnum = 1;
7558 }
7559 else
7560#endif
7561 lnum -= n;
7562 curwin->w_cursor.lnum = lnum;
7563 }
7564
7565 /* try to advance to the column we want to be at */
7566 coladvance(curwin->w_curswant);
7567
7568 if (upd_topline)
7569 update_topline(); /* make sure curwin->w_topline is valid */
7570
7571 return OK;
7572}
7573
7574/*
7575 * Cursor down a number of logical lines.
7576 */
7577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007578cursor_down(
7579 long n,
7580 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
7582 linenr_T lnum;
7583
7584 if (n > 0)
7585 {
7586 lnum = curwin->w_cursor.lnum;
7587#ifdef FEAT_FOLDING
7588 /* Move to last line of fold, will fail if it's the end-of-file. */
7589 (void)hasFolding(lnum, NULL, &lnum);
7590#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007591 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007592 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007593 if (lnum >= curbuf->b_ml.ml_line_count
7594 || (lnum + n > curbuf->b_ml.ml_line_count
7595 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 return FAIL;
7597 if (lnum + n >= curbuf->b_ml.ml_line_count)
7598 lnum = curbuf->b_ml.ml_line_count;
7599 else
7600#ifdef FEAT_FOLDING
7601 if (hasAnyFolding(curwin))
7602 {
7603 linenr_T last;
7604
7605 /* count each sequence of folded lines as one logical line */
7606 while (n--)
7607 {
7608 if (hasFolding(lnum, NULL, &last))
7609 lnum = last + 1;
7610 else
7611 ++lnum;
7612 if (lnum >= curbuf->b_ml.ml_line_count)
7613 break;
7614 }
7615 if (lnum > curbuf->b_ml.ml_line_count)
7616 lnum = curbuf->b_ml.ml_line_count;
7617 }
7618 else
7619#endif
7620 lnum += n;
7621 curwin->w_cursor.lnum = lnum;
7622 }
7623
7624 /* try to advance to the column we want to be at */
7625 coladvance(curwin->w_curswant);
7626
7627 if (upd_topline)
7628 update_topline(); /* make sure curwin->w_topline is valid */
7629
7630 return OK;
7631}
7632
7633/*
7634 * Stuff the last inserted text in the read buffer.
7635 * Last_insert actually is a copy of the redo buffer, so we
7636 * first have to remove the command.
7637 */
7638 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007639stuff_inserted(
7640 int c, /* Command character to be inserted */
7641 long count, /* Repeat this many times */
7642 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644 char_u *esc_ptr;
7645 char_u *ptr;
7646 char_u *last_ptr;
7647 char_u last = NUL;
7648
7649 ptr = get_last_insert();
7650 if (ptr == NULL)
7651 {
7652 EMSG(_(e_noinstext));
7653 return FAIL;
7654 }
7655
7656 /* may want to stuff the command character, to start Insert mode */
7657 if (c != NUL)
7658 stuffcharReadbuff(c);
7659 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7660 *esc_ptr = NUL; /* remove the ESC */
7661
7662 /* when the last char is either "0" or "^" it will be quoted if no ESC
7663 * comes after it OR if it will inserted more than once and "ptr"
7664 * starts with ^D. -- Acevedo
7665 */
7666 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7667 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7668 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7669 {
7670 last = *last_ptr;
7671 *last_ptr = NUL;
7672 }
7673
7674 do
7675 {
7676 stuffReadbuff(ptr);
7677 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7678 if (last)
7679 stuffReadbuff((char_u *)(last == '0'
7680 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7681 : IF_EB("\026^", CTRL_V_STR "^")));
7682 }
7683 while (--count > 0);
7684
7685 if (last)
7686 *last_ptr = last;
7687
7688 if (esc_ptr != NULL)
7689 *esc_ptr = ESC; /* put the ESC back */
7690
7691 /* may want to stuff a trailing ESC, to get out of Insert mode */
7692 if (!no_esc)
7693 stuffcharReadbuff(ESC);
7694
7695 return OK;
7696}
7697
7698 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007699get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 if (last_insert == NULL)
7702 return NULL;
7703 return last_insert + last_insert_skip;
7704}
7705
7706/*
7707 * Get last inserted string, and remove trailing <Esc>.
7708 * Returns pointer to allocated memory (must be freed) or NULL.
7709 */
7710 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007711get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 char_u *s;
7714 int len;
7715
7716 if (last_insert == NULL)
7717 return NULL;
7718 s = vim_strsave(last_insert + last_insert_skip);
7719 if (s != NULL)
7720 {
7721 len = (int)STRLEN(s);
7722 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7723 s[len - 1] = NUL;
7724 }
7725 return s;
7726}
7727
7728/*
7729 * Check the word in front of the cursor for an abbreviation.
7730 * Called when the non-id character "c" has been entered.
7731 * When an abbreviation is recognized it is removed from the text and
7732 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7733 */
7734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007735echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 /* Don't check for abbreviation in paste mode, when disabled and just
7738 * after moving around with cursor keys. */
7739 if (p_paste || no_abbr || arrow_used)
7740 return FALSE;
7741
7742 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7743 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7744}
7745
7746/*
7747 * replace-stack functions
7748 *
7749 * When replacing characters, the replaced characters are remembered for each
7750 * new character. This is used to re-insert the old text when backspacing.
7751 *
7752 * There is a NUL headed list of characters for each character that is
7753 * currently in the file after the insertion point. When BS is used, one NUL
7754 * headed list is put back for the deleted character.
7755 *
7756 * For a newline, there are two NUL headed lists. One contains the characters
7757 * that the NL replaced. The extra one stores the characters after the cursor
7758 * that were deleted (always white space).
7759 *
7760 * Replace_offset is normally 0, in which case replace_push will add a new
7761 * character at the end of the stack. If replace_offset is not 0, that many
7762 * characters will be left on the stack above the newly inserted character.
7763 */
7764
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007765static char_u *replace_stack = NULL;
7766static long replace_stack_nr = 0; /* next entry in replace stack */
7767static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768
7769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007770replace_push(
7771 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 char_u *p;
7774
7775 if (replace_stack_nr < replace_offset) /* nothing to do */
7776 return;
7777 if (replace_stack_len <= replace_stack_nr)
7778 {
7779 replace_stack_len += 50;
7780 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7781 if (p == NULL) /* out of memory */
7782 {
7783 replace_stack_len -= 50;
7784 return;
7785 }
7786 if (replace_stack != NULL)
7787 {
7788 mch_memmove(p, replace_stack,
7789 (size_t)(replace_stack_nr * sizeof(char_u)));
7790 vim_free(replace_stack);
7791 }
7792 replace_stack = p;
7793 }
7794 p = replace_stack + replace_stack_nr - replace_offset;
7795 if (replace_offset)
7796 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7797 *p = c;
7798 ++replace_stack_nr;
7799}
7800
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007801#if defined(FEAT_MBYTE) || defined(PROTO)
7802/*
7803 * Push a character onto the replace stack. Handles a multi-byte character in
7804 * reverse byte order, so that the first byte is popped off first.
7805 * Return the number of bytes done (includes composing characters).
7806 */
7807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007808replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007809{
7810 int l = (*mb_ptr2len)(p);
7811 int j;
7812
7813 for (j = l - 1; j >= 0; --j)
7814 replace_push(p[j]);
7815 return l;
7816}
7817#endif
7818
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819/*
7820 * Pop one item from the replace stack.
7821 * return -1 if stack empty
7822 * return replaced character or NUL otherwise
7823 */
7824 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007825replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827 if (replace_stack_nr == 0)
7828 return -1;
7829 return (int)replace_stack[--replace_stack_nr];
7830}
7831
7832/*
7833 * Join the top two items on the replace stack. This removes to "off"'th NUL
7834 * encountered.
7835 */
7836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007837replace_join(
7838 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840 int i;
7841
7842 for (i = replace_stack_nr; --i >= 0; )
7843 if (replace_stack[i] == NUL && off-- <= 0)
7844 {
7845 --replace_stack_nr;
7846 mch_memmove(replace_stack + i, replace_stack + i + 1,
7847 (size_t)(replace_stack_nr - i));
7848 return;
7849 }
7850}
7851
7852/*
7853 * Pop bytes from the replace stack until a NUL is found, and insert them
7854 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7855 */
7856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007857replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859 int cc;
7860 int oldState = State;
7861
7862 State = NORMAL; /* don't want REPLACE here */
7863 while ((cc = replace_pop()) > 0)
7864 {
7865#ifdef FEAT_MBYTE
7866 mb_replace_pop_ins(cc);
7867#else
7868 ins_char(cc);
7869#endif
7870 dec_cursor();
7871 }
7872 State = oldState;
7873}
7874
7875#ifdef FEAT_MBYTE
7876/*
7877 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7878 * indicates a multi-byte char, pop the other bytes too.
7879 */
7880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007881mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
7883 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007884 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 int i;
7886 int c;
7887
7888 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7889 {
7890 buf[0] = cc;
7891 for (i = 1; i < n; ++i)
7892 buf[i] = replace_pop();
7893 ins_bytes_len(buf, n);
7894 }
7895 else
7896 ins_char(cc);
7897
7898 if (enc_utf8)
7899 /* Handle composing chars. */
7900 for (;;)
7901 {
7902 c = replace_pop();
7903 if (c == -1) /* stack empty */
7904 break;
7905 if ((n = MB_BYTE2LEN(c)) == 1)
7906 {
7907 /* Not a multi-byte char, put it back. */
7908 replace_push(c);
7909 break;
7910 }
7911 else
7912 {
7913 buf[0] = c;
7914 for (i = 1; i < n; ++i)
7915 buf[i] = replace_pop();
7916 if (utf_iscomposing(utf_ptr2char(buf)))
7917 ins_bytes_len(buf, n);
7918 else
7919 {
7920 /* Not a composing char, put it back. */
7921 for (i = n - 1; i >= 0; --i)
7922 replace_push(buf[i]);
7923 break;
7924 }
7925 }
7926 }
7927}
7928#endif
7929
7930/*
7931 * make the replace stack empty
7932 * (called when exiting replace mode)
7933 */
7934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007935replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007937 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 replace_stack_len = 0;
7939 replace_stack_nr = 0;
7940}
7941
7942/*
7943 * Handle doing a BS for one character.
7944 * cc < 0: replace stack empty, just move cursor
7945 * cc == 0: character was inserted, delete it
7946 * cc > 0: character was replaced, put cc (first byte of original char) back
7947 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007948 * When "limit_col" is >= 0, don't delete before this column. Matters when
7949 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 */
7951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007952replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
7954 int cc;
7955#ifdef FEAT_VREPLACE
7956 int orig_len = 0;
7957 int ins_len;
7958 int orig_vcols = 0;
7959 colnr_T start_vcol;
7960 char_u *p;
7961 int i;
7962 int vcol;
7963#endif
7964
7965 cc = replace_pop();
7966 if (cc > 0)
7967 {
7968#ifdef FEAT_VREPLACE
7969 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 }
7976#endif
7977#ifdef FEAT_MBYTE
7978 if (has_mbyte)
7979 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007980 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981# ifdef FEAT_VREPLACE
7982 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007983 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984# endif
7985 replace_push(cc);
7986 }
7987 else
7988#endif
7989 {
7990 pchar_cursor(cc);
7991#ifdef FEAT_VREPLACE
7992 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007993 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994#endif
7995 }
7996 replace_pop_ins();
7997
7998#ifdef FEAT_VREPLACE
7999 if (State & VREPLACE_FLAG)
8000 {
8001 /* Get the number of screen cells used by the inserted characters */
8002 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008003 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 vcol = start_vcol;
8005 for (i = 0; i < ins_len; ++i)
8006 {
8007 vcol += chartabsize(p + i, vcol);
8008#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008009 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010#endif
8011 }
8012 vcol -= start_vcol;
8013
8014 /* Delete spaces that were inserted after the cursor to keep the
8015 * text aligned. */
8016 curwin->w_cursor.col += ins_len;
8017 while (vcol > orig_vcols && gchar_cursor() == ' ')
8018 {
8019 del_char(FALSE);
8020 ++orig_vcols;
8021 }
8022 curwin->w_cursor.col -= ins_len;
8023 }
8024#endif
8025
8026 /* mark the buffer as changed and prepare for displaying */
8027 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8028 }
8029 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008030 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031}
8032
8033#ifdef FEAT_CINDENT
8034/*
8035 * Return TRUE if C-indenting is on.
8036 */
8037 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008038cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039{
8040 return (!p_paste && (curbuf->b_p_cin
8041# ifdef FEAT_EVAL
8042 || *curbuf->b_p_inde != NUL
8043# endif
8044 ));
8045}
8046#endif
8047
8048#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8049/*
8050 * Re-indent the current line, based on the current contents of it and the
8051 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8052 * confused what all the part that handles Control-T is doing that I'm not.
8053 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8054 */
8055
8056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008057fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008059 int amount = get_the_indent();
8060
8061 if (amount >= 0)
8062 {
8063 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8064 if (linewhite(curwin->w_cursor.lnum))
8065 did_ai = TRUE; /* delete the indent if the line stays empty */
8066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068
8069 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008070fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072 if (p_paste)
8073 return;
8074# ifdef FEAT_LISP
8075 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8076 fixthisline(get_lisp_indent);
8077# endif
8078# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8079 else
8080# endif
8081# ifdef FEAT_CINDENT
8082 if (cindent_on())
8083 do_c_expr_indent();
8084# endif
8085}
8086
8087#endif
8088
8089#ifdef FEAT_CINDENT
8090/*
8091 * return TRUE if 'cinkeys' contains the key "keytyped",
8092 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008093 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8095 *
8096 * "keytyped" can have a few special values:
8097 * KEY_OPEN_FORW
8098 * KEY_OPEN_BACK
8099 * KEY_COMPLETE just finished completion.
8100 *
8101 * If line_is_empty is TRUE accept keys with '0' before them.
8102 */
8103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008104in_cinkeys(
8105 int keytyped,
8106 int when,
8107 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 char_u *look;
8110 int try_match;
8111 int try_match_word;
8112 char_u *p;
8113 char_u *line;
8114 int icase;
8115 int i;
8116
Bram Moolenaar30848942009-12-24 14:46:12 +00008117 if (keytyped == NUL)
8118 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8119 return FALSE;
8120
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121#ifdef FEAT_EVAL
8122 if (*curbuf->b_p_inde != NUL)
8123 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8124 else
8125#endif
8126 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8127 while (*look)
8128 {
8129 /*
8130 * Find out if we want to try a match with this key, depending on
8131 * 'when' and a '*' or '!' before the key.
8132 */
8133 switch (when)
8134 {
8135 case '*': try_match = (*look == '*'); break;
8136 case '!': try_match = (*look == '!'); break;
8137 default: try_match = (*look != '*'); break;
8138 }
8139 if (*look == '*' || *look == '!')
8140 ++look;
8141
8142 /*
8143 * If there is a '0', only accept a match if the line is empty.
8144 * But may still match when typing last char of a word.
8145 */
8146 if (*look == '0')
8147 {
8148 try_match_word = try_match;
8149 if (!line_is_empty)
8150 try_match = FALSE;
8151 ++look;
8152 }
8153 else
8154 try_match_word = FALSE;
8155
8156 /*
8157 * does it look like a control character?
8158 */
8159 if (*look == '^'
8160#ifdef EBCDIC
8161 && (Ctrl_chr(look[1]) != 0)
8162#else
8163 && look[1] >= '?' && look[1] <= '_'
8164#endif
8165 )
8166 {
8167 if (try_match && keytyped == Ctrl_chr(look[1]))
8168 return TRUE;
8169 look += 2;
8170 }
8171 /*
8172 * 'o' means "o" command, open forward.
8173 * 'O' means "O" command, open backward.
8174 */
8175 else if (*look == 'o')
8176 {
8177 if (try_match && keytyped == KEY_OPEN_FORW)
8178 return TRUE;
8179 ++look;
8180 }
8181 else if (*look == 'O')
8182 {
8183 if (try_match && keytyped == KEY_OPEN_BACK)
8184 return TRUE;
8185 ++look;
8186 }
8187
8188 /*
8189 * 'e' means to check for "else" at start of line and just before the
8190 * cursor.
8191 */
8192 else if (*look == 'e')
8193 {
8194 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8195 {
8196 p = ml_get_curline();
8197 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8198 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8199 return TRUE;
8200 }
8201 ++look;
8202 }
8203
8204 /*
8205 * ':' only causes an indent if it is at the end of a label or case
8206 * statement, or when it was before typing the ':' (to fix
8207 * class::method for C++).
8208 */
8209 else if (*look == ':')
8210 {
8211 if (try_match && keytyped == ':')
8212 {
8213 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008214 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008216 /* Need to get the line again after cin_islabel(). */
8217 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 if (curwin->w_cursor.col > 2
8219 && p[curwin->w_cursor.col - 1] == ':'
8220 && p[curwin->w_cursor.col - 2] == ':')
8221 {
8222 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008223 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008224 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 p = ml_get_curline();
8226 p[curwin->w_cursor.col - 1] = ':';
8227 if (i)
8228 return TRUE;
8229 }
8230 }
8231 ++look;
8232 }
8233
8234
8235 /*
8236 * Is it a key in <>, maybe?
8237 */
8238 else if (*look == '<')
8239 {
8240 if (try_match)
8241 {
8242 /*
8243 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8244 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8245 * >, *, : and ! keys if they really really want to.
8246 */
8247 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8248 && keytyped == look[1])
8249 return TRUE;
8250
8251 if (keytyped == get_special_key_code(look + 1))
8252 return TRUE;
8253 }
8254 while (*look && *look != '>')
8255 look++;
8256 while (*look == '>')
8257 look++;
8258 }
8259
8260 /*
8261 * Is it a word: "=word"?
8262 */
8263 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8264 {
8265 ++look;
8266 if (*look == '~')
8267 {
8268 icase = TRUE;
8269 ++look;
8270 }
8271 else
8272 icase = FALSE;
8273 p = vim_strchr(look, ',');
8274 if (p == NULL)
8275 p = look + STRLEN(look);
8276 if ((try_match || try_match_word)
8277 && curwin->w_cursor.col >= (colnr_T)(p - look))
8278 {
8279 int match = FALSE;
8280
8281#ifdef FEAT_INS_EXPAND
8282 if (keytyped == KEY_COMPLETE)
8283 {
8284 char_u *s;
8285
8286 /* Just completed a word, check if it starts with "look".
8287 * search back for the start of a word. */
8288 line = ml_get_curline();
8289# ifdef FEAT_MBYTE
8290 if (has_mbyte)
8291 {
8292 char_u *n;
8293
8294 for (s = line + curwin->w_cursor.col; s > line; s = n)
8295 {
8296 n = mb_prevptr(line, s);
8297 if (!vim_iswordp(n))
8298 break;
8299 }
8300 }
8301 else
8302# endif
8303 for (s = line + curwin->w_cursor.col; s > line; --s)
8304 if (!vim_iswordc(s[-1]))
8305 break;
8306 if (s + (p - look) <= line + curwin->w_cursor.col
8307 && (icase
8308 ? MB_STRNICMP(s, look, p - look)
8309 : STRNCMP(s, look, p - look)) == 0)
8310 match = TRUE;
8311 }
8312 else
8313#endif
8314 /* TODO: multi-byte */
8315 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8316 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8317 {
8318 line = ml_get_cursor();
8319 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8320 || !vim_iswordc(line[-(p - look) - 1]))
8321 && (icase
8322 ? MB_STRNICMP(line - (p - look), look, p - look)
8323 : STRNCMP(line - (p - look), look, p - look))
8324 == 0)
8325 match = TRUE;
8326 }
8327 if (match && try_match_word && !try_match)
8328 {
8329 /* "0=word": Check if there are only blanks before the
8330 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008331 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 (int)(curwin->w_cursor.col - (p - look)))
8333 match = FALSE;
8334 }
8335 if (match)
8336 return TRUE;
8337 }
8338 look = p;
8339 }
8340
8341 /*
8342 * ok, it's a boring generic character.
8343 */
8344 else
8345 {
8346 if (try_match && *look == keytyped)
8347 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008348 if (*look != NUL)
8349 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 }
8351
8352 /*
8353 * Skip over ", ".
8354 */
8355 look = skip_to_option_part(look);
8356 }
8357 return FALSE;
8358}
8359#endif /* FEAT_CINDENT */
8360
8361#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8362/*
8363 * Map Hebrew keyboard when in hkmap mode.
8364 */
8365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008366hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8369 {
8370 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8371 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8372 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8373 static char_u map[26] =
8374 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8375 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8376 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8377 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8378 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8379 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8380 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8381 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8382 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8383
8384 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8385 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8386 /* '-1'='sofit' */
8387 else if (c == 'x')
8388 return 'X';
8389 else if (c == 'q')
8390 return '\''; /* {geresh}={'} */
8391 else if (c == 246)
8392 return ' '; /* \"o --> ' ' for a german keyboard */
8393 else if (c == 228)
8394 return ' '; /* \"a --> ' ' -- / -- */
8395 else if (c == 252)
8396 return ' '; /* \"u --> ' ' -- / -- */
8397#ifdef EBCDIC
8398 else if (islower(c))
8399#else
8400 /* NOTE: islower() does not do the right thing for us on Linux so we
8401 * do this the same was as 5.7 and previous, so it works correctly on
8402 * all systems. Specifically, the e.g. Delete and Arrow keys are
8403 * munged and won't work if e.g. searching for Hebrew text.
8404 */
8405 else if (c >= 'a' && c <= 'z')
8406#endif
8407 return (int)(map[CharOrdLow(c)] + p_aleph);
8408 else
8409 return c;
8410 }
8411 else
8412 {
8413 switch (c)
8414 {
8415 case '`': return ';';
8416 case '/': return '.';
8417 case '\'': return ',';
8418 case 'q': return '/';
8419 case 'w': return '\'';
8420
8421 /* Hebrew letters - set offset from 'a' */
8422 case ',': c = '{'; break;
8423 case '.': c = 'v'; break;
8424 case ';': c = 't'; break;
8425 default: {
8426 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8427
8428#ifdef EBCDIC
8429 /* see note about islower() above */
8430 if (!islower(c))
8431#else
8432 if (c < 'a' || c > 'z')
8433#endif
8434 return c;
8435 c = str[CharOrdLow(c)];
8436 break;
8437 }
8438 }
8439
8440 return (int)(CharOrdLow(c) + p_aleph);
8441 }
8442}
8443#endif
8444
8445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008446ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 int need_redraw = FALSE;
8449 int regname;
8450 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008451 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452
8453 /*
8454 * If we are going to wait for a character, show a '"'.
8455 */
8456 pc_status = PC_STATUS_UNSET;
8457 if (redrawing() && !char_avail())
8458 {
8459 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008460 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461
8462 edit_putchar('"', TRUE);
8463#ifdef FEAT_CMDL_INFO
8464 add_to_showcmd_c(Ctrl_R);
8465#endif
8466 }
8467
8468#ifdef USE_ON_FLY_SCROLL
8469 dont_scroll = TRUE; /* disallow scrolling here */
8470#endif
8471
8472 /*
8473 * Don't map the register name. This also prevents the mode message to be
8474 * deleted when ESC is hit.
8475 */
8476 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008477 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8480 {
8481 /* Get a third key for literal register insertion */
8482 literally = regname;
8483#ifdef FEAT_CMDL_INFO
8484 add_to_showcmd_c(literally);
8485#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008486 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 }
8489 --no_mapping;
8490
8491#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008492 /* Don't call u_sync() while typing the expression or giving an error
8493 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 ++no_u_sync;
8495 if (regname == '=')
8496 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008497# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008499# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008500 /* Sync undo when evaluating the expression calls setline() or
8501 * append(), so that it can be undone separately. */
8502 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008505# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 /* Restore the Input Method. */
8507 if (im_on)
8508 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008511 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8512 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008513 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 else
8517 {
8518#endif
8519 if (literally == Ctrl_O || literally == Ctrl_P)
8520 {
8521 /* Append the command to the redo buffer. */
8522 AppendCharToRedobuff(Ctrl_R);
8523 AppendCharToRedobuff(literally);
8524 AppendCharToRedobuff(regname);
8525
8526 do_put(regname, BACKWARD, 1L,
8527 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8528 }
8529 else if (insert_reg(regname, literally) == FAIL)
8530 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008531 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 need_redraw = TRUE; /* remove the '"' */
8533 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008534 else if (stop_insert_mode)
8535 /* When the '=' register was used and a function was invoked that
8536 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8537 * insert anything, need to remove the '"' */
8538 need_redraw = TRUE;
8539
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540#ifdef FEAT_EVAL
8541 }
8542 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008543 if (u_sync_once == 1)
8544 ins_need_undo = TRUE;
8545 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#endif
8547#ifdef FEAT_CMDL_INFO
8548 clear_showcmd();
8549#endif
8550
8551 /* If the inserted register is empty, we need to remove the '"' */
8552 if (need_redraw || stuff_empty())
8553 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008554
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008555 /* Disallow starting Visual mode here, would get a weird mode. */
8556 if (!vis_active && VIsual_active)
8557 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558}
8559
8560/*
8561 * CTRL-G commands in Insert mode.
8562 */
8563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008564ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 int c;
8567
8568#ifdef FEAT_INS_EXPAND
8569 /* Right after CTRL-X the cursor will be after the ruler. */
8570 setcursor();
8571#endif
8572
8573 /*
8574 * Don't map the second key. This also prevents the mode message to be
8575 * deleted when ESC is hit.
8576 */
8577 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008578 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 --no_mapping;
8580 switch (c)
8581 {
8582 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8583 case K_UP:
8584 case Ctrl_K:
8585 case 'k': ins_up(TRUE);
8586 break;
8587
8588 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8589 case K_DOWN:
8590 case Ctrl_J:
8591 case 'j': ins_down(TRUE);
8592 break;
8593
8594 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008595 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008597
8598 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008599 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008600 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008601 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 break;
8603
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008604 /* CTRL-G U: do not break undo with the next char */
8605 case 'U':
8606 /* Allow one left/right cursor movement with the next char,
8607 * without breaking undo. */
8608 dont_sync_undo = MAYBE;
8609 break;
8610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008612 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 }
8614}
8615
8616/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008617 * CTRL-^ in Insert mode.
8618 */
8619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008620ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008621{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008622 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008623 {
8624 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8625 if (State & LANGMAP)
8626 {
8627 curbuf->b_p_iminsert = B_IMODE_NONE;
8628 State &= ~LANGMAP;
8629 }
8630 else
8631 {
8632 curbuf->b_p_iminsert = B_IMODE_LMAP;
8633 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008634#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008635 im_set_active(FALSE);
8636#endif
8637 }
8638 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008639#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008640 else
8641 {
8642 /* There are no ":lmap" mappings, toggle IM */
8643 if (im_get_status())
8644 {
8645 curbuf->b_p_iminsert = B_IMODE_NONE;
8646 im_set_active(FALSE);
8647 }
8648 else
8649 {
8650 curbuf->b_p_iminsert = B_IMODE_IM;
8651 State &= ~LANGMAP;
8652 im_set_active(TRUE);
8653 }
8654 }
8655#endif
8656 set_iminsert_global();
8657 showmode();
8658#ifdef FEAT_GUI
8659 /* may show different cursor shape or color */
8660 if (gui.in_use)
8661 gui_update_cursor(TRUE, FALSE);
8662#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008663#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008664 /* Show/unshow value of 'keymap' in status lines. */
8665 status_redraw_curbuf();
8666#endif
8667}
8668
8669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 * Handle ESC in insert mode.
8671 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8672 * insert.
8673 */
8674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008675ins_esc(
8676 long *count,
8677 int cmdchar,
8678 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
8680 int temp;
8681 static int disabled_redraw = FALSE;
8682
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008683#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008684 check_spell_redraw();
8685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686#if defined(FEAT_HANGULIN)
8687# if defined(ESC_CHG_TO_ENG_MODE)
8688 hangul_input_state_set(0);
8689# endif
8690 if (composing_hangul)
8691 {
8692 push_raw_key(composing_hangul_buffer, 2);
8693 composing_hangul = 0;
8694 }
8695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
8697 temp = curwin->w_cursor.col;
8698 if (disabled_redraw)
8699 {
8700 --RedrawingDisabled;
8701 disabled_redraw = FALSE;
8702 }
8703 if (!arrow_used)
8704 {
8705 /*
8706 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008707 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8708 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 */
8710 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008711 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712
8713 /*
8714 * Repeating insert may take a long time. Check for
8715 * interrupt now and then.
8716 */
8717 if (*count > 0)
8718 {
8719 line_breakcheck();
8720 if (got_int)
8721 *count = 0;
8722 }
8723
8724 if (--*count > 0) /* repeat what was typed */
8725 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008726 /* Vi repeats the insert without replacing characters. */
8727 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8728 State &= ~REPLACE_FLAG;
8729
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 (void)start_redo_ins();
8731 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008732 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 ++RedrawingDisabled;
8734 disabled_redraw = TRUE;
8735 return FALSE; /* repeat the insert */
8736 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008737 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 undisplay_dollar();
8739 }
8740
8741 /* When an autoindent was removed, curswant stays after the
8742 * indent */
8743 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8744 curwin->w_set_curswant = TRUE;
8745
8746 /* Remember the last Insert position in the '^ mark. */
8747 if (!cmdmod.keepjumps)
8748 curbuf->b_last_insert = curwin->w_cursor;
8749
8750 /*
8751 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008752 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008754 if (!nomove
8755 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756#ifdef FEAT_VIRTUALEDIT
8757 || curwin->w_cursor.coladd > 0
8758#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008759 )
8760 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008761 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762#ifdef FEAT_RIGHTLEFT
8763 && !revins_on
8764#endif
8765 )
8766 {
8767#ifdef FEAT_VIRTUALEDIT
8768 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8769 {
8770 oneleft();
8771 if (restart_edit != NUL)
8772 ++curwin->w_cursor.coladd;
8773 }
8774 else
8775#endif
8776 {
8777 --curwin->w_cursor.col;
8778#ifdef FEAT_MBYTE
8779 /* Correct cursor for multi-byte character. */
8780 if (has_mbyte)
8781 mb_adjust_cursor();
8782#endif
8783 }
8784 }
8785
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008786#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 /* Disable IM to allow typing English directly for Normal mode commands.
8788 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8789 * well). */
8790 if (!(State & LANGMAP))
8791 im_save_status(&curbuf->b_p_iminsert);
8792 im_set_active(FALSE);
8793#endif
8794
8795 State = NORMAL;
8796 /* need to position cursor again (e.g. when on a TAB ) */
8797 changed_cline_bef_curs();
8798
8799#ifdef FEAT_MOUSE
8800 setmouse();
8801#endif
8802#ifdef CURSOR_SHAPE
8803 ui_cursor_shape(); /* may show different cursor shape */
8804#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008805 if (!p_ek)
8806 /* Re-enable bracketed paste mode. */
8807 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
8809 /*
8810 * When recording or for CTRL-O, need to display the new mode.
8811 * Otherwise remove the mode message.
8812 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008813 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 showmode();
8815 else if (p_smd)
8816 MSG("");
8817
8818 return TRUE; /* exit Insert mode */
8819}
8820
8821#ifdef FEAT_RIGHTLEFT
8822/*
8823 * Toggle language: hkmap and revins_on.
8824 * Move to end of reverse inserted text.
8825 */
8826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008827ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829 if (revins_on && revins_chars && revins_scol >= 0)
8830 {
8831 while (gchar_cursor() != NUL && revins_chars--)
8832 ++curwin->w_cursor.col;
8833 }
8834 p_ri = !p_ri;
8835 revins_on = (State == INSERT && p_ri);
8836 if (revins_on)
8837 {
8838 revins_scol = curwin->w_cursor.col;
8839 revins_legal++;
8840 revins_chars = 0;
8841 undisplay_dollar();
8842 }
8843 else
8844 revins_scol = -1;
8845#ifdef FEAT_FKMAP
8846 if (p_altkeymap)
8847 {
8848 /*
8849 * to be consistent also for redo command, using '.'
8850 * set arrow_used to true and stop it - causing to redo
8851 * characters entered in one mode (normal/reverse insert).
8852 */
8853 arrow_used = TRUE;
8854 (void)stop_arrow();
8855 p_fkmap = curwin->w_p_rl ^ p_ri;
8856 if (p_fkmap && p_ri)
8857 State = INSERT;
8858 }
8859 else
8860#endif
8861 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8862 showmode();
8863}
8864#endif
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866/*
8867 * If 'keymodel' contains "startsel", may start selection.
8868 * Returns TRUE when a CTRL-O and other keys stuffed.
8869 */
8870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008871ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
8873 if (km_startsel)
8874 switch (c)
8875 {
8876 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 case K_PAGEUP:
8879 case K_KPAGEUP:
8880 case K_PAGEDOWN:
8881 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008882# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 case K_LEFT:
8884 case K_RIGHT:
8885 case K_UP:
8886 case K_DOWN:
8887 case K_END:
8888 case K_HOME:
8889# endif
8890 if (!(mod_mask & MOD_MASK_SHIFT))
8891 break;
8892 /* FALLTHROUGH */
8893 case K_S_LEFT:
8894 case K_S_RIGHT:
8895 case K_S_UP:
8896 case K_S_DOWN:
8897 case K_S_END:
8898 case K_S_HOME:
8899 /* Start selection right away, the cursor can move with
8900 * CTRL-O when beyond the end of the line. */
8901 start_selection();
8902
8903 /* Execute the key in (insert) Select mode. */
8904 stuffcharReadbuff(Ctrl_O);
8905 if (mod_mask)
8906 {
8907 char_u buf[4];
8908
8909 buf[0] = K_SPECIAL;
8910 buf[1] = KS_MODIFIER;
8911 buf[2] = mod_mask;
8912 buf[3] = NUL;
8913 stuffReadbuff(buf);
8914 }
8915 stuffcharReadbuff(c);
8916 return TRUE;
8917 }
8918 return FALSE;
8919}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
8921/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008922 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008923 */
8924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008925ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008926{
8927#ifdef FEAT_FKMAP
8928 if (p_fkmap && p_ri)
8929 {
8930 beep_flush();
8931 EMSG(farsi_text_3); /* encoded in Farsi */
8932 return;
8933 }
8934#endif
8935
Bram Moolenaar1e015462005-09-25 22:16:38 +00008936# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008937 set_vim_var_string(VV_INSERTMODE,
8938 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008939# ifdef FEAT_VREPLACE
8940 replaceState == VREPLACE ? "v" :
8941# endif
8942 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008943# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008944 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008945 if (State & REPLACE_FLAG)
8946 State = INSERT | (State & LANGMAP);
8947 else
8948 State = replaceState | (State & LANGMAP);
8949 AppendCharToRedobuff(K_INS);
8950 showmode();
8951#ifdef CURSOR_SHAPE
8952 ui_cursor_shape(); /* may show different cursor shape */
8953#endif
8954}
8955
8956/*
8957 * Pressed CTRL-O in Insert mode.
8958 */
8959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008960ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008961{
8962#ifdef FEAT_VREPLACE
8963 if (State & VREPLACE_FLAG)
8964 restart_edit = 'V';
8965 else
8966#endif
8967 if (State & REPLACE_FLAG)
8968 restart_edit = 'R';
8969 else
8970 restart_edit = 'I';
8971#ifdef FEAT_VIRTUALEDIT
8972 if (virtual_active())
8973 ins_at_eol = FALSE; /* cursor always keeps its column */
8974 else
8975#endif
8976 ins_at_eol = (gchar_cursor() == NUL);
8977}
8978
8979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 * If the cursor is on an indent, ^T/^D insert/delete one
8981 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008982 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 * with vi. But vi only supports ^T and ^D after an
8984 * autoindent, we support it everywhere.
8985 */
8986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008987ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
8989 if (stop_arrow() == FAIL)
8990 return;
8991 AppendCharToRedobuff(c);
8992
8993 /*
8994 * 0^D and ^^D: remove all indent.
8995 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008996 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8997 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 {
8999 --curwin->w_cursor.col;
9000 (void)del_char(FALSE); /* delete the '^' or '0' */
9001 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9002 if (State & REPLACE_FLAG)
9003 replace_pop_ins();
9004 if (lastc == '^')
9005 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009006 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 }
9008 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009009 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010
9011 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9012 did_ai = FALSE;
9013#ifdef FEAT_SMARTINDENT
9014 did_si = FALSE;
9015 can_si = FALSE;
9016 can_si_back = FALSE;
9017#endif
9018#ifdef FEAT_CINDENT
9019 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9020#endif
9021}
9022
9023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009024ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
9026 int temp;
9027
9028 if (stop_arrow() == FAIL)
9029 return;
9030 if (gchar_cursor() == NUL) /* delete newline */
9031 {
9032 temp = curwin->w_cursor.col;
9033 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009034 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009035 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009039#ifdef FEAT_VREPLACE
9040 /* Adjust orig_line_count in case more lines have been deleted than
9041 * have been added. That makes sure, that open_line() later
9042 * can access all buffer lines correctly */
9043 if (State & VREPLACE_FLAG &&
9044 orig_line_count > curbuf->b_ml.ml_line_count)
9045 orig_line_count = curbuf->b_ml.ml_line_count;
9046#endif
9047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009049 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9050 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 did_ai = FALSE;
9052#ifdef FEAT_SMARTINDENT
9053 did_si = FALSE;
9054 can_si = FALSE;
9055 can_si_back = FALSE;
9056#endif
9057 AppendCharToRedobuff(K_DEL);
9058}
9059
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009060static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009061
9062/*
9063 * Delete one character for ins_bs().
9064 */
9065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009066ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009067{
9068 dec_cursor();
9069 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9070 if (State & REPLACE_FLAG)
9071 {
9072 /* Don't delete characters before the insert point when in
9073 * Replace mode */
9074 if (curwin->w_cursor.lnum != Insstart.lnum
9075 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009076 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009077 }
9078 else
9079 (void)del_char(FALSE);
9080}
9081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082/*
9083 * Handle Backspace, delete-word and delete-line in Insert mode.
9084 * Return TRUE when backspace was actually used.
9085 */
9086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009087ins_bs(
9088 int c,
9089 int mode,
9090 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091{
9092 linenr_T lnum;
9093 int cc;
9094 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009095 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 colnr_T mincol;
9097 int did_backspace = FALSE;
9098 int in_indent;
9099 int oldState;
9100#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009101 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102#endif
9103
9104 /*
9105 * can't delete anything in an empty file
9106 * can't backup past first character in buffer
9107 * can't backup past starting point unless 'backspace' > 1
9108 * can backup to a previous line if 'backspace' == 0
9109 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009110 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 || (
9112#ifdef FEAT_RIGHTLEFT
9113 !revins_on &&
9114#endif
9115 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9116 || (!can_bs(BS_START)
9117 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009118 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9119 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9121 && curwin->w_cursor.col <= ai_col)
9122 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9123 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009124 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 return FALSE;
9126 }
9127
9128 if (stop_arrow() == FAIL)
9129 return FALSE;
9130 in_indent = inindent(0);
9131#ifdef FEAT_CINDENT
9132 if (in_indent)
9133 can_cindent = FALSE;
9134#endif
9135#ifdef FEAT_COMMENTS
9136 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9137#endif
9138#ifdef FEAT_RIGHTLEFT
9139 if (revins_on) /* put cursor after last inserted char */
9140 inc_cursor();
9141#endif
9142
9143#ifdef FEAT_VIRTUALEDIT
9144 /* Virtualedit:
9145 * BACKSPACE_CHAR eats a virtual space
9146 * BACKSPACE_WORD eats all coladd
9147 * BACKSPACE_LINE eats all coladd and keeps going
9148 */
9149 if (curwin->w_cursor.coladd > 0)
9150 {
9151 if (mode == BACKSPACE_CHAR)
9152 {
9153 --curwin->w_cursor.coladd;
9154 return TRUE;
9155 }
9156 if (mode == BACKSPACE_WORD)
9157 {
9158 curwin->w_cursor.coladd = 0;
9159 return TRUE;
9160 }
9161 curwin->w_cursor.coladd = 0;
9162 }
9163#endif
9164
9165 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009166 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 */
9168 if (curwin->w_cursor.col == 0)
9169 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009170 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009171 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172#ifdef FEAT_RIGHTLEFT
9173 || revins_on
9174#endif
9175 )
9176 {
9177 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9178 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9179 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009180 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009181 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 }
9183 /*
9184 * In replace mode:
9185 * cc < 0: NL was inserted, delete it
9186 * cc >= 0: NL was replaced, put original characters back
9187 */
9188 cc = -1;
9189 if (State & REPLACE_FLAG)
9190 cc = replace_pop(); /* returns -1 if NL was inserted */
9191 /*
9192 * In replace mode, in the line we started replacing, we only move the
9193 * cursor.
9194 */
9195 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9196 {
9197 dec_cursor();
9198 }
9199 else
9200 {
9201#ifdef FEAT_VREPLACE
9202 if (!(State & VREPLACE_FLAG)
9203 || curwin->w_cursor.lnum > orig_line_count)
9204#endif
9205 {
9206 temp = gchar_cursor(); /* remember current char */
9207 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009208
9209 /* When "aw" is in 'formatoptions' we must delete the space at
9210 * the end of the line, otherwise the line will be broken
9211 * again when auto-formatting. */
9212 if (has_format_option(FO_AUTO)
9213 && has_format_option(FO_WHITE_PAR))
9214 {
9215 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9216 TRUE);
9217 int len;
9218
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009219 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009220 if (len > 0 && ptr[len - 1] == ' ')
9221 ptr[len - 1] = NUL;
9222 }
9223
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009224 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 if (temp == NUL && gchar_cursor() != NUL)
9226 inc_cursor();
9227 }
9228#ifdef FEAT_VREPLACE
9229 else
9230 dec_cursor();
9231#endif
9232
9233 /*
9234 * In REPLACE mode we have to put back the text that was replaced
9235 * by the NL. On the replace stack is first a NUL-terminated
9236 * sequence of characters that were deleted and then the
9237 * characters that NL replaced.
9238 */
9239 if (State & REPLACE_FLAG)
9240 {
9241 /*
9242 * Do the next ins_char() in NORMAL state, to
9243 * prevent ins_char() from replacing characters and
9244 * avoiding showmatch().
9245 */
9246 oldState = State;
9247 State = NORMAL;
9248 /*
9249 * restore characters (blanks) deleted after cursor
9250 */
9251 while (cc > 0)
9252 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009253 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254#ifdef FEAT_MBYTE
9255 mb_replace_pop_ins(cc);
9256#else
9257 ins_char(cc);
9258#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009259 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 cc = replace_pop();
9261 }
9262 /* restore the characters that NL replaced */
9263 replace_pop_ins();
9264 State = oldState;
9265 }
9266 }
9267 did_ai = FALSE;
9268 }
9269 else
9270 {
9271 /*
9272 * Delete character(s) before the cursor.
9273 */
9274#ifdef FEAT_RIGHTLEFT
9275 if (revins_on) /* put cursor on last inserted char */
9276 dec_cursor();
9277#endif
9278 mincol = 0;
9279 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009280 if (mode == BACKSPACE_LINE
9281 && (curbuf->b_p_ai
9282#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009283 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009284#endif
9285 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286#ifdef FEAT_RIGHTLEFT
9287 && !revins_on
9288#endif
9289 )
9290 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009291 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009293 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009295 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 }
9297
9298 /*
9299 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9300 */
9301 if ( mode == BACKSPACE_CHAR
9302 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009303 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009304 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 && (*(ml_get_cursor() - 1) == TAB
9306 || (*(ml_get_cursor() - 1) == ' '
9307 && (!*inserted_space_p
9308 || arrow_used))))))
9309 {
9310 int ts;
9311 colnr_T vcol;
9312 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009313 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314
9315 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009316 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009317 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009319 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 /* Compute the virtual column where we want to be. Since
9321 * 'showbreak' may get in the way, need to get the last column of
9322 * the previous character. */
9323 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009324 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 dec_cursor();
9326 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9327 inc_cursor();
9328 want_vcol = (want_vcol / ts) * ts;
9329
9330 /* delete characters until we are at or before want_vcol */
9331 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009332 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009333 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
9335 /* insert extra spaces until we are at want_vcol */
9336 while (vcol < want_vcol)
9337 {
9338 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009339 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9340 && curwin->w_cursor.col < Insstart_orig.col)
9341 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342
9343#ifdef FEAT_VREPLACE
9344 if (State & VREPLACE_FLAG)
9345 ins_char(' ');
9346 else
9347#endif
9348 {
9349 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009350 if ((State & REPLACE_FLAG))
9351 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 }
9353 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9354 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009355
9356 /* If we are now back where we started delete one character. Can
9357 * happen when using 'sts' and 'linebreak'. */
9358 if (vcol >= start_vcol)
9359 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 }
9361
9362 /*
9363 * Delete upto starting point, start of line or previous word.
9364 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009365 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009367#ifdef FEAT_MBYTE
9368 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009370 if (has_mbyte)
9371 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009373 do
9374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009376 if (!revins_on) /* put cursor on char to be deleted */
9377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009379
9380 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009382 /* look multi-byte character class */
9383 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009385 prev_cclass = cclass;
9386 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009389
9390 /* start of word? */
9391 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9392 {
9393 mode = BACKSPACE_WORD_NOT_SPACE;
9394 temp = vim_iswordc(cc);
9395 }
9396 /* end of word? */
9397 else if (mode == BACKSPACE_WORD_NOT_SPACE
9398 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9399#ifdef FEAT_MBYTE
9400 || prev_cclass != cclass
9401#endif
9402 ))
9403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009405 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009407 inc_cursor();
9408#ifdef FEAT_RIGHTLEFT
9409 else if (State & REPLACE_FLAG)
9410 dec_cursor();
9411#endif
9412 break;
9413 }
9414 if (State & REPLACE_FLAG)
9415 replace_do_bs(-1);
9416 else
9417 {
9418#ifdef FEAT_MBYTE
9419 if (enc_utf8 && p_deco)
9420 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9421#endif
9422 (void)del_char(FALSE);
9423#ifdef FEAT_MBYTE
9424 /*
9425 * If there are combining characters and 'delcombine' is set
9426 * move the cursor back. Don't back up before the base
9427 * character.
9428 */
9429 if (enc_utf8 && p_deco && cpc[0] != NUL)
9430 inc_cursor();
9431#endif
9432#ifdef FEAT_RIGHTLEFT
9433 if (revins_chars)
9434 {
9435 revins_chars--;
9436 revins_legal++;
9437 }
9438 if (revins_on && gchar_cursor() == NUL)
9439 break;
9440#endif
9441 }
9442 /* Just a single backspace?: */
9443 if (mode == BACKSPACE_CHAR)
9444 break;
9445 } while (
9446#ifdef FEAT_RIGHTLEFT
9447 revins_on ||
9448#endif
9449 (curwin->w_cursor.col > mincol
9450 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9451 || curwin->w_cursor.col != Insstart_orig.col)));
9452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 did_backspace = TRUE;
9454 }
9455#ifdef FEAT_SMARTINDENT
9456 did_si = FALSE;
9457 can_si = FALSE;
9458 can_si_back = FALSE;
9459#endif
9460 if (curwin->w_cursor.col <= 1)
9461 did_ai = FALSE;
9462 /*
9463 * It's a little strange to put backspaces into the redo
9464 * buffer, but it makes auto-indent a lot easier to deal
9465 * with.
9466 */
9467 AppendCharToRedobuff(c);
9468
9469 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009470 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009471 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009472 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
9474 /* vi behaviour: the cursor moves backward but the character that
9475 * was there remains visible
9476 * Vim behaviour: the cursor moves backward and the character that
9477 * was there is erased from the screen.
9478 * We can emulate the vi behaviour by pretending there is a dollar
9479 * displayed even when there isn't.
9480 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009481 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 dollar_vcol = curwin->w_virtcol;
9483
Bram Moolenaarce3be472008-01-14 19:12:28 +00009484#ifdef FEAT_FOLDING
9485 /* When deleting a char the cursor line must never be in a closed fold.
9486 * E.g., when 'foldmethod' is indent and deleting the first non-white
9487 * char before a Tab. */
9488 if (did_backspace)
9489 foldOpenCursor();
9490#endif
9491
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 return did_backspace;
9493}
9494
9495#ifdef FEAT_MOUSE
9496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009497ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009500 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502# ifdef FEAT_GUI
9503 /* When GUI is active, also move/paste when 'mouse' is empty */
9504 if (!gui.in_use)
9505# endif
9506 if (!mouse_has(MOUSE_INSERT))
9507 return;
9508
9509 undisplay_dollar();
9510 tpos = curwin->w_cursor;
9511 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9512 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009513 win_T *new_curwin = curwin;
9514
9515 if (curwin != old_curwin && win_valid(old_curwin))
9516 {
9517 /* Mouse took us to another window. We need to go back to the
9518 * previous one to stop insert there properly. */
9519 curwin = old_curwin;
9520 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009521#ifdef FEAT_JOB_CHANNEL
9522 if (bt_prompt(curbuf))
9523 // Restart Insert mode when re-entering the prompt buffer.
9524 curbuf->b_prompt_insert = 'A';
9525#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009526 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009527 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009528 if (curwin != new_curwin && win_valid(new_curwin))
9529 {
9530 curwin = new_curwin;
9531 curbuf = curwin->w_buffer;
9532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533# ifdef FEAT_CINDENT
9534 can_cindent = TRUE;
9535# endif
9536 }
9537
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 /* redraw status lines (in case another window became active) */
9539 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540}
9541
9542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009543ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
9545 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009546 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009547# ifdef FEAT_INS_EXPAND
9548 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549# endif
9550
9551 tpos = curwin->w_cursor;
9552
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009553 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 {
9555 int row, col;
9556
9557 row = mouse_row;
9558 col = mouse_col;
9559
9560 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009561 wp = mouse_find_win(&row, &col);
9562 if (wp == NULL)
9563 return;
9564 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 curbuf = curwin->w_buffer;
9566 }
9567 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 undisplay_dollar();
9569
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009570# ifdef FEAT_INS_EXPAND
9571 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009572 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009573# endif
9574 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009575 if (dir == MSCR_DOWN || dir == MSCR_UP)
9576 {
9577 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9578 scroll_redraw(dir,
9579 (long)(curwin->w_botline - curwin->w_topline));
9580 else
9581 scroll_redraw(dir, 3L);
9582 }
9583#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009584 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009585 {
9586 int val, step = 6;
9587
9588 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009589 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009590 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9591 if (val < 0)
9592 val = 0;
9593 gui_do_horiz_scroll(val, TRUE);
9594 }
9595#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009596# ifdef FEAT_INS_EXPAND
9597 did_scroll = TRUE;
9598# endif
9599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 curwin->w_redr_status = TRUE;
9602
9603 curwin = old_curwin;
9604 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009606# ifdef FEAT_INS_EXPAND
9607 /* The popup menu may overlay the window, need to redraw it.
9608 * TODO: Would be more efficient to only redraw the windows that are
9609 * overlapped by the popup menu. */
9610 if (pum_visible() && did_scroll)
9611 {
9612 redraw_all_later(NOT_VALID);
9613 ins_compl_show_pum();
9614 }
9615# endif
9616
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009617 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 {
9619 start_arrow(&tpos);
9620# ifdef FEAT_CINDENT
9621 can_cindent = TRUE;
9622# endif
9623 }
9624}
9625#endif
9626
Bram Moolenaarec2da362017-01-21 20:04:22 +01009627/*
9628 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9629 * P_PE literally.
9630 * When "drop" is TRUE then consume the text and drop it.
9631 */
9632 int
9633bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9634{
9635 int c;
9636 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9637 int idx = 0;
9638 char_u *end = find_termcode((char_u *)"PE");
9639 int ret_char = -1;
9640 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009641 int save_paste = p_paste;
9642 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009643
9644 /* If the end code is too long we can't detect it, read everything. */
9645 if (STRLEN(end) >= NUMBUFLEN)
9646 end = NULL;
9647 ++no_mapping;
9648 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009649 p_paste = TRUE;
9650 curbuf->b_p_ai = FALSE;
9651
Bram Moolenaarec2da362017-01-21 20:04:22 +01009652 for (;;)
9653 {
9654 /* When the end is not defined read everything. */
9655 if (end == NULL && vpeekc() == NUL)
9656 break;
9657 c = plain_vgetc();
9658#ifdef FEAT_MBYTE
9659 if (has_mbyte)
9660 idx += (*mb_char2bytes)(c, buf + idx);
9661 else
9662#endif
9663 buf[idx++] = c;
9664 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009665 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009666 {
9667 if (end[idx] == NUL)
9668 break; /* Found the end of paste code. */
9669 continue;
9670 }
9671 if (!drop)
9672 {
9673 switch (mode)
9674 {
9675 case PASTE_CMDLINE:
9676 put_on_cmdline(buf, idx, TRUE);
9677 break;
9678
9679 case PASTE_EX:
9680 if (gap != NULL && ga_grow(gap, idx) == OK)
9681 {
9682 mch_memmove((char *)gap->ga_data + gap->ga_len,
9683 buf, (size_t)idx);
9684 gap->ga_len += idx;
9685 }
9686 break;
9687
9688 case PASTE_INSERT:
9689 if (stop_arrow() == OK)
9690 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009691 c = buf[0];
9692 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9693 ins_eol(c);
9694 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009695 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009696 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009697 AppendToRedobuffLit(buf, idx);
9698 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009699 }
9700 break;
9701
9702 case PASTE_ONE_CHAR:
9703 if (ret_char == -1)
9704 {
9705#ifdef FEAT_MBYTE
9706 if (has_mbyte)
9707 ret_char = (*mb_ptr2char)(buf);
9708 else
9709#endif
9710 ret_char = buf[0];
9711 }
9712 break;
9713 }
9714 }
9715 idx = 0;
9716 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009717
Bram Moolenaarec2da362017-01-21 20:04:22 +01009718 --no_mapping;
9719 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009720 p_paste = save_paste;
9721 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009722
9723 return ret_char;
9724}
9725
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009726#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009728ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009729{
9730 /* We will be leaving the current window, unless closing another tab. */
9731 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9732 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9733 {
9734 undisplay_dollar();
9735 start_arrow(&curwin->w_cursor);
9736# ifdef FEAT_CINDENT
9737 can_cindent = TRUE;
9738# endif
9739 }
9740
9741 if (c == K_TABLINE)
9742 goto_tabpage(current_tab);
9743 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009744 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009745 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009746 redraw_statuslines(); /* will redraw the tabline when needed */
9747 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009748}
9749#endif
9750
9751#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009753ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754{
9755 pos_T tpos;
9756
9757 undisplay_dollar();
9758 tpos = curwin->w_cursor;
9759 if (gui_do_scroll())
9760 {
9761 start_arrow(&tpos);
9762# ifdef FEAT_CINDENT
9763 can_cindent = TRUE;
9764# endif
9765 }
9766}
9767
9768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009769ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 pos_T tpos;
9772
9773 undisplay_dollar();
9774 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009775 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 {
9777 start_arrow(&tpos);
9778# ifdef FEAT_CINDENT
9779 can_cindent = TRUE;
9780# endif
9781 }
9782}
9783#endif
9784
9785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009786ins_left(
9787 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788{
9789 pos_T tpos;
9790
9791#ifdef FEAT_FOLDING
9792 if ((fdo_flags & FDO_HOR) && KeyTyped)
9793 foldOpenCursor();
9794#endif
9795 undisplay_dollar();
9796 tpos = curwin->w_cursor;
9797 if (oneleft() == OK)
9798 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009799#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9800 /* Only call start_arrow() when not busy with preediting, it will
9801 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009802 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009803#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009804 {
9805 start_arrow_with_change(&tpos, end_change);
9806 if (!end_change)
9807 AppendCharToRedobuff(K_LEFT);
9808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809#ifdef FEAT_RIGHTLEFT
9810 /* If exit reversed string, position is fixed */
9811 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9812 revins_legal++;
9813 revins_chars++;
9814#endif
9815 }
9816
9817 /*
9818 * if 'whichwrap' set for cursor in insert mode may go to
9819 * previous line
9820 */
9821 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9822 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009823 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 start_arrow(&tpos);
9825 --(curwin->w_cursor.lnum);
9826 coladvance((colnr_T)MAXCOL);
9827 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9828 }
9829 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009830 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009831 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832}
9833
9834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009835ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 pos_T tpos;
9838
9839#ifdef FEAT_FOLDING
9840 if ((fdo_flags & FDO_HOR) && KeyTyped)
9841 foldOpenCursor();
9842#endif
9843 undisplay_dollar();
9844 tpos = curwin->w_cursor;
9845 if (c == K_C_HOME)
9846 curwin->w_cursor.lnum = 1;
9847 curwin->w_cursor.col = 0;
9848#ifdef FEAT_VIRTUALEDIT
9849 curwin->w_cursor.coladd = 0;
9850#endif
9851 curwin->w_curswant = 0;
9852 start_arrow(&tpos);
9853}
9854
9855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009856ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857{
9858 pos_T tpos;
9859
9860#ifdef FEAT_FOLDING
9861 if ((fdo_flags & FDO_HOR) && KeyTyped)
9862 foldOpenCursor();
9863#endif
9864 undisplay_dollar();
9865 tpos = curwin->w_cursor;
9866 if (c == K_C_END)
9867 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9868 coladvance((colnr_T)MAXCOL);
9869 curwin->w_curswant = MAXCOL;
9870
9871 start_arrow(&tpos);
9872}
9873
9874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009875ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
9877#ifdef FEAT_FOLDING
9878 if ((fdo_flags & FDO_HOR) && KeyTyped)
9879 foldOpenCursor();
9880#endif
9881 undisplay_dollar();
9882 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9883 {
9884 start_arrow(&curwin->w_cursor);
9885 (void)bck_word(1L, FALSE, FALSE);
9886 curwin->w_set_curswant = TRUE;
9887 }
9888 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009889 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
9891
9892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009893ins_right(
9894 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896#ifdef FEAT_FOLDING
9897 if ((fdo_flags & FDO_HOR) && KeyTyped)
9898 foldOpenCursor();
9899#endif
9900 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009901 if (gchar_cursor() != NUL
9902#ifdef FEAT_VIRTUALEDIT
9903 || virtual_active()
9904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 )
9906 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009907 start_arrow_with_change(&curwin->w_cursor, end_change);
9908 if (!end_change)
9909 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 curwin->w_set_curswant = TRUE;
9911#ifdef FEAT_VIRTUALEDIT
9912 if (virtual_active())
9913 oneright();
9914 else
9915#endif
9916 {
9917#ifdef FEAT_MBYTE
9918 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009919 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 else
9921#endif
9922 ++curwin->w_cursor.col;
9923 }
9924
9925#ifdef FEAT_RIGHTLEFT
9926 revins_legal++;
9927 if (revins_chars)
9928 revins_chars--;
9929#endif
9930 }
9931 /* if 'whichwrap' set for cursor in insert mode, may move the
9932 * cursor to the next line */
9933 else if (vim_strchr(p_ww, ']') != NULL
9934 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9935 {
9936 start_arrow(&curwin->w_cursor);
9937 curwin->w_set_curswant = TRUE;
9938 ++curwin->w_cursor.lnum;
9939 curwin->w_cursor.col = 0;
9940 }
9941 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009942 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009943 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944}
9945
9946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948{
9949#ifdef FEAT_FOLDING
9950 if ((fdo_flags & FDO_HOR) && KeyTyped)
9951 foldOpenCursor();
9952#endif
9953 undisplay_dollar();
9954 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9955 || gchar_cursor() != NUL)
9956 {
9957 start_arrow(&curwin->w_cursor);
9958 (void)fwd_word(1L, FALSE, 0);
9959 curwin->w_set_curswant = TRUE;
9960 }
9961 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009962 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963}
9964
9965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009966ins_up(
9967 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968{
9969 pos_T tpos;
9970 linenr_T old_topline = curwin->w_topline;
9971#ifdef FEAT_DIFF
9972 int old_topfill = curwin->w_topfill;
9973#endif
9974
9975 undisplay_dollar();
9976 tpos = curwin->w_cursor;
9977 if (cursor_up(1L, TRUE) == OK)
9978 {
9979 if (startcol)
9980 coladvance(getvcol_nolist(&Insstart));
9981 if (old_topline != curwin->w_topline
9982#ifdef FEAT_DIFF
9983 || old_topfill != curwin->w_topfill
9984#endif
9985 )
9986 redraw_later(VALID);
9987 start_arrow(&tpos);
9988#ifdef FEAT_CINDENT
9989 can_cindent = TRUE;
9990#endif
9991 }
9992 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009993 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994}
9995
9996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009997ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998{
9999 pos_T tpos;
10000
10001 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010002
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010003 if (mod_mask & MOD_MASK_CTRL)
10004 {
10005 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010006 if (first_tabpage->tp_next != NULL)
10007 {
10008 start_arrow(&curwin->w_cursor);
10009 goto_tabpage(-1);
10010 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010011 return;
10012 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010013
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 tpos = curwin->w_cursor;
10015 if (onepage(BACKWARD, 1L) == OK)
10016 {
10017 start_arrow(&tpos);
10018#ifdef FEAT_CINDENT
10019 can_cindent = TRUE;
10020#endif
10021 }
10022 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010023 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024}
10025
10026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010027ins_down(
10028 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
10030 pos_T tpos;
10031 linenr_T old_topline = curwin->w_topline;
10032#ifdef FEAT_DIFF
10033 int old_topfill = curwin->w_topfill;
10034#endif
10035
10036 undisplay_dollar();
10037 tpos = curwin->w_cursor;
10038 if (cursor_down(1L, TRUE) == OK)
10039 {
10040 if (startcol)
10041 coladvance(getvcol_nolist(&Insstart));
10042 if (old_topline != curwin->w_topline
10043#ifdef FEAT_DIFF
10044 || old_topfill != curwin->w_topfill
10045#endif
10046 )
10047 redraw_later(VALID);
10048 start_arrow(&tpos);
10049#ifdef FEAT_CINDENT
10050 can_cindent = TRUE;
10051#endif
10052 }
10053 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010054 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055}
10056
10057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010058ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059{
10060 pos_T tpos;
10061
10062 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010063
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010064 if (mod_mask & MOD_MASK_CTRL)
10065 {
10066 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010067 if (first_tabpage->tp_next != NULL)
10068 {
10069 start_arrow(&curwin->w_cursor);
10070 goto_tabpage(0);
10071 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010072 return;
10073 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010074
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 tpos = curwin->w_cursor;
10076 if (onepage(FORWARD, 1L) == OK)
10077 {
10078 start_arrow(&tpos);
10079#ifdef FEAT_CINDENT
10080 can_cindent = TRUE;
10081#endif
10082 }
10083 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010084 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085}
10086
10087#ifdef FEAT_DND
10088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010089ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
10091 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10092}
10093#endif
10094
10095/*
10096 * Handle TAB in Insert or Replace mode.
10097 * Return TRUE when the TAB needs to be inserted like a normal character.
10098 */
10099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010100ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101{
10102 int ind;
10103 int i;
10104 int temp;
10105
10106 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10107 Insstart_blank_vcol = get_nolist_virtcol();
10108 if (echeck_abbr(TAB + ABBR_OFF))
10109 return FALSE;
10110
10111 ind = inindent(0);
10112#ifdef FEAT_CINDENT
10113 if (ind)
10114 can_cindent = FALSE;
10115#endif
10116
10117 /*
10118 * When nothing special, insert TAB like a normal character
10119 */
10120 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010121 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010122 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 return TRUE;
10124
10125 if (stop_arrow() == FAIL)
10126 return TRUE;
10127
10128 did_ai = FALSE;
10129#ifdef FEAT_SMARTINDENT
10130 did_si = FALSE;
10131 can_si = FALSE;
10132 can_si_back = FALSE;
10133#endif
10134 AppendToRedobuff((char_u *)"\t");
10135
10136 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010137 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010138 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10139 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 else /* otherwise use 'tabstop' */
10141 temp = (int)curbuf->b_p_ts;
10142 temp -= get_nolist_virtcol() % temp;
10143
10144 /*
10145 * Insert the first space with ins_char(). It will delete one char in
10146 * replace mode. Insert the rest with ins_str(); it will not delete any
10147 * chars. For VREPLACE mode, we use ins_char() for all characters.
10148 */
10149 ins_char(' ');
10150 while (--temp > 0)
10151 {
10152#ifdef FEAT_VREPLACE
10153 if (State & VREPLACE_FLAG)
10154 ins_char(' ');
10155 else
10156#endif
10157 {
10158 ins_str((char_u *)" ");
10159 if (State & REPLACE_FLAG) /* no char replaced */
10160 replace_push(NUL);
10161 }
10162 }
10163
10164 /*
10165 * When 'expandtab' not set: Replace spaces by TABs where possible.
10166 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010167 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 {
10169 char_u *ptr;
10170#ifdef FEAT_VREPLACE
10171 char_u *saved_line = NULL; /* init for GCC */
10172 pos_T pos;
10173#endif
10174 pos_T fpos;
10175 pos_T *cursor;
10176 colnr_T want_vcol, vcol;
10177 int change_col = -1;
10178 int save_list = curwin->w_p_list;
10179
10180 /*
10181 * Get the current line. For VREPLACE mode, don't make real changes
10182 * yet, just work on a copy of the line.
10183 */
10184#ifdef FEAT_VREPLACE
10185 if (State & VREPLACE_FLAG)
10186 {
10187 pos = curwin->w_cursor;
10188 cursor = &pos;
10189 saved_line = vim_strsave(ml_get_curline());
10190 if (saved_line == NULL)
10191 return FALSE;
10192 ptr = saved_line + pos.col;
10193 }
10194 else
10195#endif
10196 {
10197 ptr = ml_get_cursor();
10198 cursor = &curwin->w_cursor;
10199 }
10200
10201 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10202 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10203 curwin->w_p_list = FALSE;
10204
10205 /* Find first white before the cursor */
10206 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010207 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 {
10209 --fpos.col;
10210 --ptr;
10211 }
10212
10213 /* In Replace mode, don't change characters before the insert point. */
10214 if ((State & REPLACE_FLAG)
10215 && fpos.lnum == Insstart.lnum
10216 && fpos.col < Insstart.col)
10217 {
10218 ptr += Insstart.col - fpos.col;
10219 fpos.col = Insstart.col;
10220 }
10221
10222 /* compute virtual column numbers of first white and cursor */
10223 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10224 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10225
Bram Moolenaar597a4222014-06-25 14:39:50 +020010226 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10227 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010228 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010230 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 if (vcol + i > want_vcol)
10232 break;
10233 if (*ptr != TAB)
10234 {
10235 *ptr = TAB;
10236 if (change_col < 0)
10237 {
10238 change_col = fpos.col; /* Column of first change */
10239 /* May have to adjust Insstart */
10240 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10241 Insstart.col = fpos.col;
10242 }
10243 }
10244 ++fpos.col;
10245 ++ptr;
10246 vcol += i;
10247 }
10248
10249 if (change_col >= 0)
10250 {
10251 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010252 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253
10254 /* Skip over the spaces we need. */
10255 while (vcol < want_vcol && *ptr == ' ')
10256 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010257 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 ++ptr;
10259 ++repl_off;
10260 }
10261 if (vcol > want_vcol)
10262 {
10263 /* Must have a char with 'showbreak' just before it. */
10264 --ptr;
10265 --repl_off;
10266 }
10267 fpos.col += repl_off;
10268
10269 /* Delete following spaces. */
10270 i = cursor->col - fpos.col;
10271 if (i > 0)
10272 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010273 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 /* correct replace stack. */
10275 if ((State & REPLACE_FLAG)
10276#ifdef FEAT_VREPLACE
10277 && !(State & VREPLACE_FLAG)
10278#endif
10279 )
10280 for (temp = i; --temp >= 0; )
10281 replace_join(repl_off);
10282 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010283#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010284 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010285 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010286 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010287 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10288 (char_u *)"\t", 1);
10289 }
10290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 cursor->col -= i;
10292
10293#ifdef FEAT_VREPLACE
10294 /*
10295 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10296 * backspacing over the changed spacing and then inserting the new
10297 * spacing.
10298 */
10299 if (State & VREPLACE_FLAG)
10300 {
10301 /* Backspace from real cursor to change_col */
10302 backspace_until_column(change_col);
10303
10304 /* Insert each char in saved_line from changed_col to
10305 * ptr-cursor */
10306 ins_bytes_len(saved_line + change_col,
10307 cursor->col - change_col);
10308 }
10309#endif
10310 }
10311
10312#ifdef FEAT_VREPLACE
10313 if (State & VREPLACE_FLAG)
10314 vim_free(saved_line);
10315#endif
10316 curwin->w_p_list = save_list;
10317 }
10318
10319 return FALSE;
10320}
10321
10322/*
10323 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010324 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 */
10326 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010327ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328{
10329 int i;
10330
10331 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010332 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010334 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 undisplay_dollar();
10336
10337 /*
10338 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10339 * character under the cursor. Only push a NUL on the replace stack,
10340 * nothing to put back when the NL is deleted.
10341 */
10342 if ((State & REPLACE_FLAG)
10343#ifdef FEAT_VREPLACE
10344 && !(State & VREPLACE_FLAG)
10345#endif
10346 )
10347 replace_push(NUL);
10348
10349 /*
10350 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10351 * replacing the next line, so we push all of the characters left on the
10352 * line onto the replace stack. This is not done here though, it is done
10353 * in open_line().
10354 */
10355
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010356#ifdef FEAT_VIRTUALEDIT
10357 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10358 * CTRL-O). */
10359 if (virtual_active() && curwin->w_cursor.coladd > 0)
10360 coladvance(getviscol());
10361#endif
10362
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363#ifdef FEAT_RIGHTLEFT
10364# ifdef FEAT_FKMAP
10365 if (p_altkeymap && p_fkmap)
10366 fkmap(NL);
10367# endif
10368 /* NL in reverse insert will always start in the end of
10369 * current line. */
10370 if (revins_on)
10371 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10372#endif
10373
10374 AppendToRedobuff(NL_STR);
10375 i = open_line(FORWARD,
10376#ifdef FEAT_COMMENTS
10377 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10378#endif
10379 0, old_indent);
10380 old_indent = 0;
10381#ifdef FEAT_CINDENT
10382 can_cindent = TRUE;
10383#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010384#ifdef FEAT_FOLDING
10385 /* When inserting a line the cursor line must never be in a closed fold. */
10386 foldOpenCursor();
10387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010389 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390}
10391
10392#ifdef FEAT_DIGRAPHS
10393/*
10394 * Handle digraph in insert mode.
10395 * Returns character still to be inserted, or NUL when nothing remaining to be
10396 * done.
10397 */
10398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010399ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
10401 int c;
10402 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010403 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404
10405 pc_status = PC_STATUS_UNSET;
10406 if (redrawing() && !char_avail())
10407 {
10408 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010409 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410
10411 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010412 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413#ifdef FEAT_CMDL_INFO
10414 add_to_showcmd_c(Ctrl_K);
10415#endif
10416 }
10417
10418#ifdef USE_ON_FLY_SCROLL
10419 dont_scroll = TRUE; /* disallow scrolling here */
10420#endif
10421
10422 /* don't map the digraph chars. This also prevents the
10423 * mode message to be deleted when ESC is hit */
10424 ++no_mapping;
10425 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010426 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 --no_mapping;
10428 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010429 if (did_putchar)
10430 /* when the line fits in 'columns' the '?' is at the start of the next
10431 * line and will not be removed by the redraw */
10432 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010433
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 if (IS_SPECIAL(c) || mod_mask) /* special key */
10435 {
10436#ifdef FEAT_CMDL_INFO
10437 clear_showcmd();
10438#endif
10439 insert_special(c, TRUE, FALSE);
10440 return NUL;
10441 }
10442 if (c != ESC)
10443 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010444 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 if (redrawing() && !char_avail())
10446 {
10447 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010448 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449
10450 if (char2cells(c) == 1)
10451 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010452 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010454 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 }
10456#ifdef FEAT_CMDL_INFO
10457 add_to_showcmd_c(c);
10458#endif
10459 }
10460 ++no_mapping;
10461 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010462 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 --no_mapping;
10464 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010465 if (did_putchar)
10466 /* when the line fits in 'columns' the '?' is at the start of the
10467 * next line and will not be removed by a redraw */
10468 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 if (cc != ESC)
10470 {
10471 AppendToRedobuff((char_u *)CTRL_V_STR);
10472 c = getdigraph(c, cc, TRUE);
10473#ifdef FEAT_CMDL_INFO
10474 clear_showcmd();
10475#endif
10476 return c;
10477 }
10478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479#ifdef FEAT_CMDL_INFO
10480 clear_showcmd();
10481#endif
10482 return NUL;
10483}
10484#endif
10485
10486/*
10487 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10488 * Returns the char to be inserted, or NUL if none found.
10489 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010491ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492{
10493 int c;
10494 int temp;
10495 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010496 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497
10498 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10499 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010500 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 return NUL;
10502 }
10503
10504 /* try to advance to the cursor column */
10505 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010506 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 prev_ptr = ptr;
10508 validate_virtcol();
10509 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10510 {
10511 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010512 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 }
10514 if ((colnr_T)temp > curwin->w_virtcol)
10515 ptr = prev_ptr;
10516
10517#ifdef FEAT_MBYTE
10518 c = (*mb_ptr2char)(ptr);
10519#else
10520 c = *ptr;
10521#endif
10522 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010523 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 return c;
10525}
10526
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010527/*
10528 * CTRL-Y or CTRL-E typed in Insert mode.
10529 */
10530 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010531ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010532{
10533 int c = tc;
10534
10535#ifdef FEAT_INS_EXPAND
10536 if (ctrl_x_mode == CTRL_X_SCROLL)
10537 {
10538 if (c == Ctrl_Y)
10539 scrolldown_clamp();
10540 else
10541 scrollup_clamp();
10542 redraw_later(VALID);
10543 }
10544 else
10545#endif
10546 {
10547 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10548 if (c != NUL)
10549 {
10550 long tw_save;
10551
10552 /* The character must be taken literally, insert like it
10553 * was typed after a CTRL-V, and pretend 'textwidth'
10554 * wasn't set. Digits, 'o' and 'x' are special after a
10555 * CTRL-V, don't use it for these. */
10556 if (c < 256 && !isalnum(c))
10557 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10558 tw_save = curbuf->b_p_tw;
10559 curbuf->b_p_tw = -1;
10560 insert_special(c, TRUE, FALSE);
10561 curbuf->b_p_tw = tw_save;
10562#ifdef FEAT_RIGHTLEFT
10563 revins_chars++;
10564 revins_legal++;
10565#endif
10566 c = Ctrl_V; /* pretend CTRL-V is last character */
10567 auto_format(FALSE, TRUE);
10568 }
10569 }
10570 return c;
10571}
10572
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573#ifdef FEAT_SMARTINDENT
10574/*
10575 * Try to do some very smart auto-indenting.
10576 * Used when inserting a "normal" character.
10577 */
10578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010579ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580{
10581 pos_T *pos, old_pos;
10582 char_u *ptr;
10583 int i;
10584 int temp;
10585
10586 /*
10587 * do some very smart indenting when entering '{' or '}'
10588 */
10589 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10590 {
10591 /*
10592 * for '}' set indent equal to indent of line containing matching '{'
10593 */
10594 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10595 {
10596 old_pos = curwin->w_cursor;
10597 /*
10598 * If the matching '{' has a ')' immediately before it (ignoring
10599 * white-space), then line up with the start of the line
10600 * containing the matching '(' if there is one. This handles the
10601 * case where an "if (..\n..) {" statement continues over multiple
10602 * lines -- webb
10603 */
10604 ptr = ml_get(pos->lnum);
10605 i = pos->col;
10606 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010607 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 ;
10609 curwin->w_cursor.lnum = pos->lnum;
10610 curwin->w_cursor.col = i;
10611 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10612 curwin->w_cursor = *pos;
10613 i = get_indent();
10614 curwin->w_cursor = old_pos;
10615#ifdef FEAT_VREPLACE
10616 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010617 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 else
10619#endif
10620 (void)set_indent(i, SIN_CHANGED);
10621 }
10622 else if (curwin->w_cursor.col > 0)
10623 {
10624 /*
10625 * when inserting '{' after "O" reduce indent, but not
10626 * more than indent of previous line
10627 */
10628 temp = TRUE;
10629 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10630 {
10631 old_pos = curwin->w_cursor;
10632 i = get_indent();
10633 while (curwin->w_cursor.lnum > 1)
10634 {
10635 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10636
10637 /* ignore empty lines and lines starting with '#'. */
10638 if (*ptr != '#' && *ptr != NUL)
10639 break;
10640 }
10641 if (get_indent() >= i)
10642 temp = FALSE;
10643 curwin->w_cursor = old_pos;
10644 }
10645 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010646 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 }
10648 }
10649
10650 /*
10651 * set indent of '#' always to 0
10652 */
10653 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10654 {
10655 /* remember current indent for next line */
10656 old_indent = get_indent();
10657 (void)set_indent(0, SIN_CHANGED);
10658 }
10659
10660 /* Adjust ai_col, the char at this position can be deleted. */
10661 if (ai_col > curwin->w_cursor.col)
10662 ai_col = curwin->w_cursor.col;
10663}
10664#endif
10665
10666/*
10667 * Get the value that w_virtcol would have when 'list' is off.
10668 * Unless 'cpo' contains the 'L' flag.
10669 */
10670 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010671get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672{
10673 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10674 return getvcol_nolist(&curwin->w_cursor);
10675 validate_virtcol();
10676 return curwin->w_virtcol;
10677}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010678
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010679#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010680/*
10681 * Handle the InsertCharPre autocommand.
10682 * "c" is the character that was typed.
10683 * Return a pointer to allocated memory with the replacement string.
10684 * Return NULL to continue inserting "c".
10685 */
10686 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010687do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010688{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010689 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010690 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010691
10692 /* Return quickly when there is nothing to do. */
10693 if (!has_insertcharpre())
10694 return NULL;
10695
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010696# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010697 if (has_mbyte)
10698 buf[(*mb_char2bytes)(c, buf)] = NUL;
10699 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010700# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010701 {
10702 buf[0] = c;
10703 buf[1] = NUL;
10704 }
10705
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010706 /* Lock the text to avoid weird things from happening. */
10707 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010708 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010709
Bram Moolenaar704984a2012-06-01 14:57:51 +020010710 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010711 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010712 {
10713 /* Get the value of v:char. It may be empty or more than one
10714 * character. Only use it when changed, otherwise continue with the
10715 * original character to avoid breaking autoindent. */
10716 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10717 res = vim_strsave(get_vim_var_str(VV_CHAR));
10718 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010719
10720 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10721 --textlock;
10722
10723 return res;
10724}
10725#endif