blob: 3204ec31a7c2cf9921240e29a15a828501ee2d18 [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);
Bram Moolenaar942b4542018-06-17 16:23:34 +02001182 restart_edit = 'A';
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001183 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 Moolenaar6ba3ec12018-06-16 15:32:38 +02001707 aco_save_T aco;
1708
1709 // save and restore curwin and curbuf, in case the autocmd changes them
1710 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001711 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001712 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001713 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
Bram Moolenaar5a093432018-02-10 18:15:19 +01001715
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001716#ifdef FEAT_INS_EXPAND
Bram Moolenaar5a093432018-02-10 18:15:19 +01001717 /* Trigger TextChangedP if b_changedtick differs. When the popupmenu closes
1718 * TextChangedI will need to trigger for backwards compatibility, thus use
1719 * different b_last_changedtick* variables. */
1720 if (ready && has_textchangedP()
1721 && curbuf->b_last_changedtick_pum != CHANGEDTICK(curbuf)
1722 && pum_visible())
1723 {
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001724 aco_save_T aco;
1725
1726 // save and restore curwin and curbuf, in case the autocmd changes them
1727 aucmd_prepbuf(&aco, curbuf);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001728 apply_autocmds(EVENT_TEXTCHANGEDP, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001729 aucmd_restbuf(&aco);
Bram Moolenaar5a093432018-02-10 18:15:19 +01001730 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
1731 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001732#endif
1733
1734 if (must_redraw)
1735 update_screen(0);
1736 else if (clear_cmdline || redraw_cmdline)
1737 showmode(); /* clear cmdline and show mode */
1738# if defined(FEAT_CONCEAL)
1739 if ((conceal_update_lines
1740 && (conceal_old_cursor_line != conceal_new_cursor_line
1741 || conceal_cursor_line(curwin)))
1742 || need_cursor_line_redraw)
1743 {
1744 if (conceal_old_cursor_line != conceal_new_cursor_line)
1745 update_single_line(curwin, conceal_old_cursor_line);
1746 update_single_line(curwin, conceal_new_cursor_line == 0
1747 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1748 curwin->w_valid &= ~VALID_CROW;
1749 }
1750# endif
1751 showruler(FALSE);
1752 setcursor();
1753 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754}
1755
1756/*
1757 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1758 */
1759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001760ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001763 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001766 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
1768 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001771 did_putchar = TRUE;
1772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1774
1775#ifdef FEAT_CMDL_INFO
1776 add_to_showcmd_c(Ctrl_V);
1777#endif
1778
1779 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001780 if (did_putchar)
1781 /* when the line fits in 'columns' the '^' is at the start of the next
1782 * line and will not removed by the redraw */
1783 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#ifdef FEAT_CMDL_INFO
1785 clear_showcmd();
1786#endif
1787 insert_special(c, FALSE, TRUE);
1788#ifdef FEAT_RIGHTLEFT
1789 revins_chars++;
1790 revins_legal++;
1791#endif
1792}
1793
1794/*
1795 * Put a character directly onto the screen. It's not stored in a buffer.
1796 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1797 */
1798static int pc_status;
1799#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1800#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1801#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1802#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804static int pc_attr;
1805static int pc_row;
1806static int pc_col;
1807
1808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001809edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 int attr;
1812
1813 if (ScreenLines != NULL)
1814 {
1815 update_topline(); /* just in case w_topline isn't valid */
1816 validate_cursor();
1817 if (highlight)
Bram Moolenaar8820b482017-03-16 17:23:31 +01001818 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 else
1820 attr = 0;
1821 pc_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02001822 pc_col = curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1824 pc_status = PC_STATUS_UNSET;
1825#endif
1826#ifdef FEAT_RIGHTLEFT
1827 if (curwin->w_p_rl)
1828 {
Bram Moolenaar02631462017-09-22 15:20:32 +02001829 pc_col += curwin->w_width - 1 - curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830# ifdef FEAT_MBYTE
1831 if (has_mbyte)
1832 {
1833 int fix_col = mb_fix_col(pc_col, pc_row);
1834
1835 if (fix_col != pc_col)
1836 {
1837 screen_putchar(' ', pc_row, fix_col, attr);
1838 --curwin->w_wcol;
1839 pc_status = PC_STATUS_RIGHT;
1840 }
1841 }
1842# endif
1843 }
1844 else
1845#endif
1846 {
1847 pc_col += curwin->w_wcol;
1848#ifdef FEAT_MBYTE
1849 if (mb_lefthalve(pc_row, pc_col))
1850 pc_status = PC_STATUS_LEFT;
1851#endif
1852 }
1853
1854 /* save the character to be able to put it back */
1855#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1856 if (pc_status == PC_STATUS_UNSET)
1857#endif
1858 {
1859 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1860 pc_status = PC_STATUS_SET;
1861 }
1862 screen_putchar(c, pc_row, pc_col, attr);
1863 }
1864}
1865
Bram Moolenaarf2732452018-06-03 14:47:35 +02001866#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
1867/*
1868 * Return the effective prompt for the current buffer.
1869 */
1870 char_u *
1871prompt_text(void)
1872{
1873 if (curbuf->b_prompt_text == NULL)
1874 return (char_u *)"% ";
1875 return curbuf->b_prompt_text;
1876}
1877
1878/*
1879 * Prepare for prompt mode: Make sure the last line has the prompt text.
1880 * Move the cursor to this line.
1881 */
1882 static void
1883init_prompt(int cmdchar_todo)
1884{
1885 char_u *prompt = prompt_text();
1886 char_u *text;
1887
1888 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1889 text = ml_get_curline();
1890 if (STRNCMP(text, prompt, STRLEN(prompt)) != 0)
1891 {
1892 // prompt is missing, insert it or append a line with it
1893 if (*text == NUL)
1894 ml_replace(curbuf->b_ml.ml_line_count, prompt, TRUE);
1895 else
1896 ml_append(curbuf->b_ml.ml_line_count, prompt, 0, FALSE);
1897 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1898 coladvance((colnr_T)MAXCOL);
1899 changed_bytes(curbuf->b_ml.ml_line_count, 0);
1900 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001901
1902 // Insert always starts after the prompt, allow editing text after it.
1903 if (Insstart_orig.lnum != curwin->w_cursor.lnum
1904 || Insstart_orig.col != (int)STRLEN(prompt))
1905 {
1906 Insstart.lnum = curwin->w_cursor.lnum;
Bram Moolenaare31e2562018-06-10 13:12:55 +02001907 Insstart.col = (int)STRLEN(prompt);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02001908 Insstart_orig = Insstart;
1909 Insstart_textlen = Insstart.col;
1910 Insstart_blank_vcol = MAXCOL;
1911 arrow_used = FALSE;
1912 }
1913
Bram Moolenaarf2732452018-06-03 14:47:35 +02001914 if (cmdchar_todo == 'A')
1915 coladvance((colnr_T)MAXCOL);
1916 if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt))
Bram Moolenaare31e2562018-06-10 13:12:55 +02001917 curwin->w_cursor.col = (int)STRLEN(prompt);
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02001918 /* Make sure the cursor is in a valid position. */
1919 check_cursor();
Bram Moolenaarf2732452018-06-03 14:47:35 +02001920}
1921
1922/*
1923 * Return TRUE if the cursor is in the editable position of the prompt line.
1924 */
1925 int
1926prompt_curpos_editable()
1927{
1928 return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
1929 && curwin->w_cursor.col >= (int)STRLEN(prompt_text());
1930}
1931#endif
1932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933/*
1934 * Undo the previous edit_putchar().
1935 */
1936 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001937edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938{
1939 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1940 {
1941#if defined(FEAT_MBYTE)
1942 if (pc_status == PC_STATUS_RIGHT)
1943 ++curwin->w_wcol;
1944 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1945 redrawWinline(curwin->w_cursor.lnum, FALSE);
1946 else
1947#endif
1948 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1949 }
1950}
1951
1952/*
1953 * Called when p_dollar is set: display a '$' at the end of the changed text
1954 * Only works when cursor is in the line that changes.
1955 */
1956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001957display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 colnr_T save_col;
1960
1961 if (!redrawing())
1962 return;
1963
1964 cursor_off();
1965 save_col = curwin->w_cursor.col;
1966 curwin->w_cursor.col = col;
1967#ifdef FEAT_MBYTE
1968 if (has_mbyte)
1969 {
1970 char_u *p;
1971
1972 /* If on the last byte of a multi-byte move to the first byte. */
1973 p = ml_get_curline();
1974 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1975 }
1976#endif
1977 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
Bram Moolenaar02631462017-09-22 15:20:32 +02001978 if (curwin->w_wcol < curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 edit_putchar('$', FALSE);
1981 dollar_vcol = curwin->w_virtcol;
1982 }
1983 curwin->w_cursor.col = save_col;
1984}
1985
1986/*
1987 * Call this function before moving the cursor from the normal insert position
1988 * in insert mode.
1989 */
1990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001991undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001993 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001995 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 redrawWinline(curwin->w_cursor.lnum, FALSE);
1997 }
1998}
1999
2000/*
2001 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
2002 * Keep the cursor on the same character.
2003 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
2004 * type == INDENT_DEC decrease indent (for CTRL-D)
2005 * type == INDENT_SET set indent to "amount"
2006 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
2007 */
2008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002009change_indent(
2010 int type,
2011 int amount,
2012 int round,
2013 int replaced, /* replaced character, put on replace stack */
2014 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 int vcol;
2017 int last_vcol;
2018 int insstart_less; /* reduction for Insstart.col */
2019 int new_cursor_col;
2020 int i;
2021 char_u *ptr;
2022 int save_p_list;
2023 int start_col;
2024 colnr_T vc;
2025#ifdef FEAT_VREPLACE
2026 colnr_T orig_col = 0; /* init for GCC */
2027 char_u *new_line, *orig_line = NULL; /* init for GCC */
2028
2029 /* VREPLACE mode needs to know what the line was like before changing */
2030 if (State & VREPLACE_FLAG)
2031 {
2032 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
2033 orig_col = curwin->w_cursor.col;
2034 }
2035#endif
2036
2037 /* for the following tricks we don't want list mode */
2038 save_p_list = curwin->w_p_list;
2039 curwin->w_p_list = FALSE;
2040 vc = getvcol_nolist(&curwin->w_cursor);
2041 vcol = vc;
2042
2043 /*
2044 * For Replace mode we need to fix the replace stack later, which is only
2045 * possible when the cursor is in the indent. Remember the number of
2046 * characters before the cursor if it's possible.
2047 */
2048 start_col = curwin->w_cursor.col;
2049
2050 /* determine offset from first non-blank */
2051 new_cursor_col = curwin->w_cursor.col;
2052 beginline(BL_WHITE);
2053 new_cursor_col -= curwin->w_cursor.col;
2054
2055 insstart_less = curwin->w_cursor.col;
2056
2057 /*
2058 * If the cursor is in the indent, compute how many screen columns the
2059 * cursor is to the left of the first non-blank.
2060 */
2061 if (new_cursor_col < 0)
2062 vcol = get_indent() - vcol;
2063
2064 if (new_cursor_col > 0) /* can't fix replace stack */
2065 start_col = -1;
2066
2067 /*
2068 * Set the new indent. The cursor will be put on the first non-blank.
2069 */
2070 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002071 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 else
2073 {
2074#ifdef FEAT_VREPLACE
2075 int save_State = State;
2076
2077 /* Avoid being called recursively. */
2078 if (State & VREPLACE_FLAG)
2079 State = INSERT;
2080#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00002081 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082#ifdef FEAT_VREPLACE
2083 State = save_State;
2084#endif
2085 }
2086 insstart_less -= curwin->w_cursor.col;
2087
2088 /*
2089 * Try to put cursor on same character.
2090 * If the cursor is at or after the first non-blank in the line,
2091 * compute the cursor column relative to the column of the first
2092 * non-blank character.
2093 * If we are not in insert mode, leave the cursor on the first non-blank.
2094 * If the cursor is before the first non-blank, position it relative
2095 * to the first non-blank, counted in screen columns.
2096 */
2097 if (new_cursor_col >= 0)
2098 {
2099 /*
2100 * When changing the indent while the cursor is touching it, reset
2101 * Insstart_col to 0.
2102 */
2103 if (new_cursor_col == 0)
2104 insstart_less = MAXCOL;
2105 new_cursor_col += curwin->w_cursor.col;
2106 }
2107 else if (!(State & INSERT))
2108 new_cursor_col = curwin->w_cursor.col;
2109 else
2110 {
2111 /*
2112 * Compute the screen column where the cursor should be.
2113 */
2114 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002115 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 /*
2118 * Advance the cursor until we reach the right screen column.
2119 */
2120 vcol = last_vcol = 0;
2121 new_cursor_col = -1;
2122 ptr = ml_get_curline();
2123 while (vcol <= (int)curwin->w_virtcol)
2124 {
2125 last_vcol = vcol;
2126#ifdef FEAT_MBYTE
2127 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002128 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 else
2130#endif
2131 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002132 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
2134 vcol = last_vcol;
2135
2136 /*
2137 * May need to insert spaces to be able to position the cursor on
2138 * the right screen column.
2139 */
2140 if (vcol != (int)curwin->w_virtcol)
2141 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002142 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002144 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (ptr != NULL)
2146 {
2147 new_cursor_col += i;
2148 ptr[i] = NUL;
2149 while (--i >= 0)
2150 ptr[i] = ' ';
2151 ins_str(ptr);
2152 vim_free(ptr);
2153 }
2154 }
2155
2156 /*
2157 * When changing the indent while the cursor is in it, reset
2158 * Insstart_col to 0.
2159 */
2160 insstart_less = MAXCOL;
2161 }
2162
2163 curwin->w_p_list = save_p_list;
2164
2165 if (new_cursor_col <= 0)
2166 curwin->w_cursor.col = 0;
2167 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002168 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 curwin->w_set_curswant = TRUE;
2170 changed_cline_bef_curs();
2171
2172 /*
2173 * May have to adjust the start of the insert.
2174 */
2175 if (State & INSERT)
2176 {
2177 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2178 {
2179 if ((int)Insstart.col <= insstart_less)
2180 Insstart.col = 0;
2181 else
2182 Insstart.col -= insstart_less;
2183 }
2184 if ((int)ai_col <= insstart_less)
2185 ai_col = 0;
2186 else
2187 ai_col -= insstart_less;
2188 }
2189
2190 /*
2191 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2192 * If the number of characters before the cursor decreased, need to pop a
2193 * few characters from the replace stack.
2194 * If the number of characters before the cursor increased, need to push a
2195 * few NULs onto the replace stack.
2196 */
2197 if (REPLACE_NORMAL(State) && start_col >= 0)
2198 {
2199 while (start_col > (int)curwin->w_cursor.col)
2200 {
2201 replace_join(0); /* remove a NUL from the replace stack */
2202 --start_col;
2203 }
2204 while (start_col < (int)curwin->w_cursor.col || replaced)
2205 {
2206 replace_push(NUL);
2207 if (replaced)
2208 {
2209 replace_push(replaced);
2210 replaced = NUL;
2211 }
2212 ++start_col;
2213 }
2214 }
2215
2216#ifdef FEAT_VREPLACE
2217 /*
2218 * For VREPLACE mode, we also have to fix the replace stack. In this case
2219 * it is always possible because we backspace over the whole line and then
2220 * put it back again the way we wanted it.
2221 */
2222 if (State & VREPLACE_FLAG)
2223 {
2224 /* If orig_line didn't allocate, just return. At least we did the job,
2225 * even if you can't backspace. */
2226 if (orig_line == NULL)
2227 return;
2228
2229 /* Save new line */
2230 new_line = vim_strsave(ml_get_curline());
2231 if (new_line == NULL)
2232 return;
2233
2234 /* We only put back the new line up to the cursor */
2235 new_line[curwin->w_cursor.col] = NUL;
2236
2237 /* Put back original line */
2238 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2239 curwin->w_cursor.col = orig_col;
2240
2241 /* Backspace from cursor to start of line */
2242 backspace_until_column(0);
2243
2244 /* Insert new stuff into line again */
2245 ins_bytes(new_line);
2246
2247 vim_free(new_line);
2248 }
2249#endif
2250}
2251
2252/*
2253 * Truncate the space at the end of a line. This is to be used only in an
2254 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2255 * modes.
2256 */
2257 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002258truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 int i;
2261
2262 /* find start of trailing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002263 for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 if (State & REPLACE_FLAG)
2266 replace_join(0); /* remove a NUL from the replace stack */
2267 }
2268 line[i + 1] = NUL;
2269}
2270
2271#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2272 || defined(FEAT_COMMENTS) || defined(PROTO)
2273/*
2274 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2275 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002276 * Will attempt not to go before "col" even when there is a composing
2277 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 */
2279 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002280backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281{
2282 while ((int)curwin->w_cursor.col > col)
2283 {
2284 curwin->w_cursor.col--;
2285 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002286 replace_do_bs(col);
2287 else if (!del_char_after_col(col))
2288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290}
2291#endif
2292
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002293/*
2294 * Like del_char(), but make sure not to go before column "limit_col".
2295 * Only matters when there are composing characters.
2296 * Return TRUE when something was deleted.
2297 */
2298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002299del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002300{
2301#ifdef FEAT_MBYTE
2302 if (enc_utf8 && limit_col >= 0)
2303 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002304 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002305
2306 /* Make sure the cursor is at the start of a character, but
2307 * skip forward again when going too far back because of a
2308 * composing character. */
2309 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002310 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002311 {
2312 int l = utf_ptr2len(ml_get_cursor());
2313
2314 if (l == 0) /* end of line */
2315 break;
2316 curwin->w_cursor.col += l;
2317 }
2318 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2319 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002320 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002321 }
2322 else
2323#endif
2324 (void)del_char(FALSE);
2325 return TRUE;
2326}
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2329/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002330 * CTRL-X pressed in Insert mode.
2331 */
2332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002333ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002334{
2335 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2336 * CTRL-V works like CTRL-N */
2337 if (ctrl_x_mode != CTRL_X_CMDLINE)
2338 {
2339 /* if the next ^X<> won't ADD nothing, then reset
2340 * compl_cont_status */
2341 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002342 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002343 else
2344 compl_cont_status = 0;
2345 /* We're not sure which CTRL-X mode it will be yet */
2346 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2347 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2348 edit_submode_pre = NULL;
2349 showmode();
2350 }
2351}
2352
2353/*
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002354 * Whether other than default completion has been selected.
2355 */
2356 int
2357ctrl_x_mode_not_default(void)
2358{
2359 return ctrl_x_mode != CTRL_X_NORMAL;
2360}
2361
2362/*
2363 * Whether CTRL-X was typed without a following character.
2364 */
2365 int
2366ctrl_x_mode_not_defined_yet(void)
2367{
2368 return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
2369}
2370
2371/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002372 * Return TRUE if the 'dict' or 'tsr' option can be used.
2373 */
2374 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002375has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002376{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002377 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002378# ifdef FEAT_SPELL
2379 && !curwin->w_p_spell
2380# endif
2381 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002382 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2383 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002384 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002385 edit_submode = NULL;
2386 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2387 : (char_u *)_("'thesaurus' option is empty"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002388 HL_ATTR(HLF_E));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002389 if (emsg_silent == 0)
2390 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002391 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002392 setcursor();
2393 out_flush();
Bram Moolenaareb992cb2017-03-09 18:20:16 +01002394#ifdef FEAT_EVAL
2395 if (!get_vim_var_nr(VV_TESTING))
2396#endif
2397 ui_delay(2000L, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002398 }
2399 return FALSE;
2400 }
2401 return TRUE;
2402}
2403
2404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2406 * This depends on the current mode.
2407 */
2408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002409vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
2411 /* Always allow ^R - let it's results then be checked */
2412 if (c == Ctrl_R)
2413 return TRUE;
2414
Bram Moolenaare3226be2005-12-18 22:10:00 +00002415 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002416 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002417 return TRUE;
2418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 switch (ctrl_x_mode)
2420 {
2421 case 0: /* Not in any CTRL-X mode */
2422 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2423 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002424 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2426 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2427 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002428 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002429 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 case CTRL_X_SCROLL:
2431 return (c == Ctrl_Y || c == Ctrl_E);
2432 case CTRL_X_WHOLE_LINE:
2433 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2434 case CTRL_X_FILES:
2435 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2436 case CTRL_X_DICTIONARY:
2437 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2438 case CTRL_X_THESAURUS:
2439 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2440 case CTRL_X_TAGS:
2441 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2442#ifdef FEAT_FIND_ID
2443 case CTRL_X_PATH_PATTERNS:
2444 return (c == Ctrl_P || c == Ctrl_N);
2445 case CTRL_X_PATH_DEFINES:
2446 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2447#endif
2448 case CTRL_X_CMDLINE:
2449 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2450 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002451#ifdef FEAT_COMPL_FUNC
2452 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002453 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002454 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002455 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002456#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002457 case CTRL_X_SPELL:
2458 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002459 case CTRL_X_EVAL:
2460 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002462 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 return FALSE;
2464}
2465
2466/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002467 * Return TRUE when character "c" is part of the item currently being
2468 * completed. Used to decide whether to abandon complete mode when the menu
2469 * is visible.
2470 */
2471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002472ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002473{
2474 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2475 /* When expanding an identifier only accept identifier chars. */
2476 return vim_isIDc(c);
2477
2478 switch (ctrl_x_mode)
2479 {
2480 case CTRL_X_FILES:
2481 /* When expanding file name only accept file name chars. But not
2482 * path separators, so that "proto/<Tab>" expands files in
2483 * "proto", not "proto/" as a whole */
2484 return vim_isfilec(c) && !vim_ispathsep(c);
2485
2486 case CTRL_X_CMDLINE:
2487 case CTRL_X_OMNI:
2488 /* Command line and Omni completion can work with just about any
2489 * printable character, but do stop at white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002490 return vim_isprintc(c) && !VIM_ISWHITE(c);
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002491
2492 case CTRL_X_WHOLE_LINE:
2493 /* For while line completion a space can be part of the line. */
2494 return vim_isprintc(c);
2495 }
2496 return vim_iswordc(c);
2497}
2498
2499/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002500 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002502 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * the rest of the word to be in -- webb
2504 */
2505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002506ins_compl_add_infercase(
2507 char_u *str,
2508 int len,
2509 int icase,
2510 char_u *fname,
2511 int dir,
2512 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002514 char_u *p;
2515 int i, c;
2516 int actual_len; /* Take multi-byte characters */
2517 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002518 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002519 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 int has_lower = FALSE;
2521 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002523 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002525 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaara2993e12007-08-12 14:38:46 +00002527 /* Find actual length of completion. */
2528#ifdef FEAT_MBYTE
2529 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002531 p = str;
2532 actual_len = 0;
2533 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002535 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002536 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
2538 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002539 else
2540#endif
2541 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaara2993e12007-08-12 14:38:46 +00002543 /* Find actual length of original text. */
2544#ifdef FEAT_MBYTE
2545 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002547 p = compl_orig_text;
2548 actual_compl_length = 0;
2549 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002551 MB_PTR_ADV(p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002552 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002555 else
2556#endif
2557 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002559 /* "actual_len" may be smaller than "actual_compl_length" when using
2560 * thesaurus, only use the minimum when comparing. */
2561 min_len = actual_len < actual_compl_length
2562 ? actual_len : actual_compl_length;
2563
Bram Moolenaara2993e12007-08-12 14:38:46 +00002564 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002565 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002566 if (wca != NULL)
2567 {
2568 p = str;
2569 for (i = 0; i < actual_len; ++i)
2570#ifdef FEAT_MBYTE
2571 if (has_mbyte)
2572 wca[i] = mb_ptr2char_adv(&p);
2573 else
2574#endif
2575 wca[i] = *(p++);
2576
2577 /* Rule 1: Were any chars converted to lower? */
2578 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002579 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002580 {
2581#ifdef FEAT_MBYTE
2582 if (has_mbyte)
2583 c = mb_ptr2char_adv(&p);
2584 else
2585#endif
2586 c = *(p++);
2587 if (MB_ISLOWER(c))
2588 {
2589 has_lower = TRUE;
2590 if (MB_ISUPPER(wca[i]))
2591 {
2592 /* Rule 1 is satisfied. */
2593 for (i = actual_compl_length; i < actual_len; ++i)
2594 wca[i] = MB_TOLOWER(wca[i]);
2595 break;
2596 }
2597 }
2598 }
2599
2600 /*
2601 * Rule 2: No lower case, 2nd consecutive letter converted to
2602 * upper case.
2603 */
2604 if (!has_lower)
2605 {
2606 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002607 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002608 {
2609#ifdef FEAT_MBYTE
2610 if (has_mbyte)
2611 c = mb_ptr2char_adv(&p);
2612 else
2613#endif
2614 c = *(p++);
2615 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2616 {
2617 /* Rule 2 is satisfied. */
2618 for (i = actual_compl_length; i < actual_len; ++i)
2619 wca[i] = MB_TOUPPER(wca[i]);
2620 break;
2621 }
2622 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2623 }
2624 }
2625
2626 /* Copy the original case of the part we typed. */
2627 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002628 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002629 {
2630#ifdef FEAT_MBYTE
2631 if (has_mbyte)
2632 c = mb_ptr2char_adv(&p);
2633 else
2634#endif
2635 c = *(p++);
2636 if (MB_ISLOWER(c))
2637 wca[i] = MB_TOLOWER(wca[i]);
2638 else if (MB_ISUPPER(c))
2639 wca[i] = MB_TOUPPER(wca[i]);
2640 }
2641
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002642 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002643 * Generate encoding specific output from wide character array.
2644 * Multi-byte characters can occupy up to five bytes more than
2645 * ASCII characters, and we also need one byte for NUL, so stay
2646 * six bytes away from the edge of IObuff.
2647 */
2648 p = IObuff;
2649 i = 0;
2650 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2651#ifdef FEAT_MBYTE
2652 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002653 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002654 else
2655#endif
2656 *(p++) = wca[i++];
2657 *p = NUL;
2658
2659 vim_free(wca);
2660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002662 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2663 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002665 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666}
2667
2668/*
2669 * Add a match to the list of matches.
2670 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002671 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002672 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002675ins_compl_add(
2676 char_u *str,
2677 int len,
2678 int icase,
2679 char_u *fname,
2680 char_u **cptext, /* extra text for popup menu or NULL */
2681 int cdir,
2682 int flags,
2683 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002685 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002686 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 ui_breakcheck();
2689 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002690 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (len < 0)
2692 len = (int)STRLEN(str);
2693
2694 /*
2695 * If the same match is already present, don't add it.
2696 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002697 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002699 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 do
2701 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002702 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002703 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002704 && match->cp_str[len] == NUL)
2705 return NOTDONE;
2706 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002707 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002710 /* Remove any popup menu before changing the list of matches. */
2711 ins_compl_del_pum();
2712
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 /*
2714 * Allocate a new match structure.
2715 * Copy the values to the new match structure.
2716 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002717 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002719 return FAIL;
2720 match->cp_number = -1;
2721 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002722 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002723 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
2725 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002726 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002728 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002731 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2733 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002734 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002735 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002736 && compl_curr_match->cp_fname != NULL
2737 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002738 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002739 else if (fname != NULL)
2740 {
2741 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002742 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002745 match->cp_fname = NULL;
2746 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002747
2748 if (cptext != NULL)
2749 {
2750 int i;
2751
2752 for (i = 0; i < CPT_COUNT; ++i)
2753 if (cptext[i] != NULL && *cptext[i] != NUL)
2754 match->cp_text[i] = vim_strsave(cptext[i]);
2755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 /*
2758 * Link the new match structure in the list of matches.
2759 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002760 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002761 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 else if (dir == FORWARD)
2763 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002764 match->cp_next = compl_curr_match->cp_next;
2765 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767 else /* BACKWARD */
2768 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002769 match->cp_next = compl_curr_match;
2770 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002772 if (match->cp_next)
2773 match->cp_next->cp_prev = match;
2774 if (match->cp_prev)
2775 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002777 compl_first_match = match;
2778 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002780 /*
2781 * Find the longest common string if still doing that.
2782 */
2783 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2784 ins_compl_longest_match(match);
2785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 return OK;
2787}
2788
2789/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002790 * Return TRUE if "str[len]" matches with match->cp_str, considering
2791 * match->cp_icase.
2792 */
2793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002795{
2796 if (match->cp_icase)
2797 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2798 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2799}
2800
2801/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002802 * Reduce the longest common string for match "match".
2803 */
2804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002805ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002806{
2807 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002808 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002809 int had_match;
2810
2811 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002812 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002813 /* First match, use it as a whole. */
2814 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002815 if (compl_leader != NULL)
2816 {
2817 had_match = (curwin->w_cursor.col > compl_col);
2818 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002819 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002820 ins_redraw(FALSE);
2821
2822 /* When the match isn't there (to avoid matching itself) remove it
2823 * again after redrawing. */
2824 if (!had_match)
2825 ins_compl_delete();
2826 compl_used_match = FALSE;
2827 }
2828 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002829 else
2830 {
2831 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002832 p = compl_leader;
2833 s = match->cp_str;
2834 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002835 {
2836#ifdef FEAT_MBYTE
2837 if (has_mbyte)
2838 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002839 c1 = mb_ptr2char(p);
2840 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002841 }
2842 else
2843#endif
2844 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002845 c1 = *p;
2846 c2 = *s;
2847 }
2848 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2849 : (c1 != c2))
2850 break;
2851#ifdef FEAT_MBYTE
2852 if (has_mbyte)
2853 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002854 MB_PTR_ADV(p);
2855 MB_PTR_ADV(s);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002856 }
2857 else
2858#endif
2859 {
2860 ++p;
2861 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002862 }
2863 }
2864
2865 if (*p != NUL)
2866 {
2867 /* Leader was shortened, need to change the inserted text. */
2868 *p = NUL;
2869 had_match = (curwin->w_cursor.col > compl_col);
2870 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002871 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002872 ins_redraw(FALSE);
2873
2874 /* When the match isn't there (to avoid matching itself) remove it
2875 * again after redrawing. */
2876 if (!had_match)
2877 ins_compl_delete();
2878 }
2879
2880 compl_used_match = FALSE;
2881 }
2882}
2883
2884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 * Add an array of matches to the list of matches.
2886 * Frees matches[].
2887 */
2888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002889ins_compl_add_matches(
2890 int num_matches,
2891 char_u **matches,
2892 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893{
2894 int i;
2895 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002896 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
Bram Moolenaar572cb562005-08-05 21:35:02 +00002898 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002899 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002900 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002902 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 FreeWild(num_matches, matches);
2904}
2905
2906/* Make the completion list cyclic.
2907 * Return the number of matches (excluding the original).
2908 */
2909 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002910ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002912 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 int count = 0;
2914
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002915 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
2917 /*
2918 * Find the end of the list.
2919 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002920 match = compl_first_match;
2921 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002922 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002924 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 ++count;
2926 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002927 match->cp_next = compl_first_match;
2928 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
2930 return count;
2931}
2932
Bram Moolenaara94bc432006-03-10 21:42:59 +00002933/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002934 * Set variables that store noselect and noinsert behavior from the
2935 * 'completeopt' value.
2936 */
2937 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002938completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002939{
2940 compl_no_insert = FALSE;
2941 compl_no_select = FALSE;
2942 if (strstr((char *)p_cot, "noselect") != NULL)
2943 compl_no_select = TRUE;
2944 if (strstr((char *)p_cot, "noinsert") != NULL)
2945 compl_no_insert = TRUE;
2946}
2947
2948/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002949 * Start completion for the complete() function.
2950 * "startcol" is where the matched text starts (1 is first column).
2951 * "list" is the list of matches.
2952 */
2953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002955{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002956 int save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01002957 int save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002958
Bram Moolenaara94bc432006-03-10 21:42:59 +00002959 /* If already doing completions stop it. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01002960 if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002961 ins_compl_prep(' ');
2962 ins_compl_clear();
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +01002963 ins_compl_free();
Bram Moolenaara94bc432006-03-10 21:42:59 +00002964
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002965 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002966 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002967 startcol = curwin->w_cursor.col;
2968 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002969 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002970 /* compl_pattern doesn't need to be set */
2971 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2972 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002973 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002974 return;
2975
Bram Moolenaare4214502015-03-05 18:08:43 +01002976 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002977
2978 ins_compl_add_list(list);
2979 compl_matches = ins_compl_make_cyclic();
2980 compl_started = TRUE;
2981 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002982 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002983
2984 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002985 if (compl_no_insert || compl_no_select)
2986 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002987 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002988 if (compl_no_select)
2989 /* Down/Up has no real effect. */
2990 ins_complete(K_UP, FALSE);
2991 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002992 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002993 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002994 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002995
2996 /* Lazily show the popup menu, unless we got interrupted. */
2997 if (!compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01002998 show_pum(save_w_wrow, save_w_leftcol);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002999 out_flush();
3000}
3001
3002
Bram Moolenaar9372a112005-12-06 19:59:18 +00003003/* "compl_match_array" points the currently displayed list of entries in the
3004 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003005static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003006static int compl_match_arraysize;
3007
3008/*
3009 * Update the screen and when there is any scrolling remove the popup menu.
3010 */
3011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003012ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003013{
3014 int h;
3015
3016 if (compl_match_array != NULL)
3017 {
3018 h = curwin->w_cline_height;
3019 update_screen(0);
3020 if (h != curwin->w_cline_height)
3021 ins_compl_del_pum();
3022 }
3023}
3024
3025/*
3026 * Remove any popup menu.
3027 */
3028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003029ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003030{
3031 if (compl_match_array != NULL)
3032 {
3033 pum_undisplay();
Bram Moolenaard23a8232018-02-10 18:45:26 +01003034 VIM_CLEAR(compl_match_array);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003035 }
3036}
3037
3038/*
3039 * Return TRUE if the popup menu should be displayed.
3040 */
3041 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003042pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003043{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003044 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003045 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003046 return FALSE;
3047
3048 /* The display looks bad on a B&W display. */
3049 if (t_colors < 8
3050#ifdef FEAT_GUI
3051 && !gui.in_use
3052#endif
3053 )
3054 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003055 return TRUE;
3056}
3057
3058/*
3059 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003060 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00003061 */
3062 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003063pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003064{
3065 compl_T *compl;
3066 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003067
3068 /* Don't display the popup menu if there are no matches or there is only
3069 * one (ignoring the original text). */
3070 compl = compl_first_match;
3071 i = 0;
3072 do
3073 {
3074 if (compl == NULL
3075 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
3076 break;
3077 compl = compl->cp_next;
3078 } while (compl != compl_first_match);
3079
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003080 if (strstr((char *)p_cot, "menuone") != NULL)
3081 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003082 return (i >= 2);
3083}
3084
3085/*
3086 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003087 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003088 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003090ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003091{
3092 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003093 compl_T *shown_compl = NULL;
3094 int did_find_shown_match = FALSE;
3095 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003096 int i;
3097 int cur = -1;
3098 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00003099 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003100
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003101 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003102 return;
3103
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003104#if defined(FEAT_EVAL)
3105 /* Dirty hard-coded hack: remove any matchparen highlighting. */
3106 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
3107#endif
3108
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003109 /* Update the screen before drawing the popup menu over it. */
3110 update_screen(0);
3111
3112 if (compl_match_array == NULL)
3113 {
3114 /* Need to build the popup menu list. */
3115 compl_match_arraysize = 0;
3116 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00003117 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003118 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003119 do
3120 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003121 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3122 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003123 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003124 ++compl_match_arraysize;
3125 compl = compl->cp_next;
3126 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00003127 if (compl_match_arraysize == 0)
3128 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003129 compl_match_array = (pumitem_T *)alloc_clear(
3130 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003131 * compl_match_arraysize));
3132 if (compl_match_array != NULL)
3133 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003134 /* If the current match is the original text don't find the first
3135 * match after it, don't highlight anything. */
3136 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3137 shown_match_ok = TRUE;
3138
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003139 i = 0;
3140 compl = compl_first_match;
3141 do
3142 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003143 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3144 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003145 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003146 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003147 if (!shown_match_ok)
3148 {
3149 if (compl == compl_shown_match || did_find_shown_match)
3150 {
3151 /* This item is the shown match or this is the
3152 * first displayed item after the shown match. */
3153 compl_shown_match = compl;
3154 did_find_shown_match = TRUE;
3155 shown_match_ok = TRUE;
3156 }
3157 else
3158 /* Remember this displayed match for when the
3159 * shown match is just below it. */
3160 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003161 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003162 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003163
3164 if (compl->cp_text[CPT_ABBR] != NULL)
3165 compl_match_array[i].pum_text =
3166 compl->cp_text[CPT_ABBR];
3167 else
3168 compl_match_array[i].pum_text = compl->cp_str;
3169 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3170 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3171 if (compl->cp_text[CPT_MENU] != NULL)
3172 compl_match_array[i++].pum_extra =
3173 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003174 else
3175 compl_match_array[i++].pum_extra = compl->cp_fname;
3176 }
3177
3178 if (compl == compl_shown_match)
3179 {
3180 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003181
3182 /* When the original text is the shown match don't set
3183 * compl_shown_match. */
3184 if (compl->cp_flags & ORIGINAL_TEXT)
3185 shown_match_ok = TRUE;
3186
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003187 if (!shown_match_ok && shown_compl != NULL)
3188 {
3189 /* The shown match isn't displayed, set it to the
3190 * previously displayed match. */
3191 compl_shown_match = shown_compl;
3192 shown_match_ok = TRUE;
3193 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003194 }
3195 compl = compl->cp_next;
3196 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003197
3198 if (!shown_match_ok) /* no displayed match at all */
3199 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003200 }
3201 }
3202 else
3203 {
3204 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003205 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003206 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3207 || compl_match_array[i].pum_text
3208 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003209 {
3210 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003211 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003212 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003213 }
3214
3215 if (compl_match_array != NULL)
3216 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003217 /* In Replace mode when a $ is displayed at the end of the line only
3218 * part of the screen would be updated. We do need to redraw here. */
3219 dollar_vcol = -1;
3220
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003221 /* Compute the screen column of the start of the completed text.
3222 * Use the cursor to get all wrapping and other settings right. */
3223 col = curwin->w_cursor.col;
3224 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003225 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003226 curwin->w_cursor.col = col;
3227 }
3228}
3229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230#define DICT_FIRST (1) /* use just first element in "dict" */
3231#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003234 * Add any identifiers that match the given pattern in the list of dictionary
3235 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 */
3237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238ins_compl_dictionaries(
3239 char_u *dict_start,
3240 char_u *pat,
3241 int flags, /* DICT_FIRST and/or DICT_EXACT */
3242 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003244 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 char_u *ptr;
3246 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 char_u **files;
3249 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003251 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaar0b238792006-03-02 22:49:12 +00003253 if (*dict == NUL)
3254 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003255#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003256 /* When 'dictionary' is empty and spell checking is enabled use
3257 * "spell". */
3258 if (!thesaurus && curwin->w_p_spell)
3259 dict = (char_u *)"spell";
3260 else
3261#endif
3262 return;
3263 }
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003266 if (buf == NULL)
3267 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003268 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 /* If 'infercase' is set, don't use 'smartcase' here */
3271 save_p_scs = p_scs;
3272 if (curbuf->b_p_inf)
3273 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003274
3275 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3276 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003277 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003278 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003279 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003280 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003281 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003282
3283 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003284 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003285 len = STRLEN(pat_esc) + 10;
3286 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003287 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003288 {
3289 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003290 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003291 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003292 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003293 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3294 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003295 vim_free(ptr);
3296 }
3297 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003298 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003299 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003300 if (regmatch.regprog == NULL)
3301 goto theend;
3302 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003303
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3305 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003306 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 {
3308 /* copy one dictionary file name into buf */
3309 if (flags == DICT_EXACT)
3310 {
3311 count = 1;
3312 files = &dict;
3313 }
3314 else
3315 {
3316 /* Expand wildcards in the dictionary name, but do not allow
3317 * backticks (for security, the 'dict' option may have been set in
3318 * a modeline). */
3319 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003320# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003321 if (!thesaurus && STRCMP(buf, "spell") == 0)
3322 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003323 else
3324# endif
3325 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 || expand_wildcards(1, &buf, &count, &files,
3327 EW_FILE|EW_SILENT) != OK)
3328 count = 0;
3329 }
3330
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003331# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003332 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003334 /* Complete from active spelling. Skip "\<" in the pattern, we
3335 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003336 if (pat[0] == '\\' && pat[1] == '<')
3337 ptr = pat + 2;
3338 else
3339 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003340 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003342 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003343# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003344 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003345 {
3346 ins_compl_files(count, files, thesaurus, flags,
3347 &regmatch, buf, &dir);
3348 if (flags != DICT_EXACT)
3349 FreeWild(count, files);
3350 }
3351 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 break;
3353 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003354
3355theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003357 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 vim_free(buf);
3359}
3360
Bram Moolenaar0b238792006-03-02 22:49:12 +00003361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003362ins_compl_files(
3363 int count,
3364 char_u **files,
3365 int thesaurus,
3366 int flags,
3367 regmatch_T *regmatch,
3368 char_u *buf,
3369 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003370{
3371 char_u *ptr;
3372 int i;
3373 FILE *fp;
3374 int add_r;
3375
3376 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3377 {
3378 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3379 if (flags != DICT_EXACT)
3380 {
3381 vim_snprintf((char *)IObuff, IOSIZE,
3382 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003383 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003384 }
3385
3386 if (fp != NULL)
3387 {
3388 /*
3389 * Read dictionary file line by line.
3390 * Check each line for a match.
3391 */
3392 while (!got_int && !compl_interrupted
3393 && !vim_fgets(buf, LSIZE, fp))
3394 {
3395 ptr = buf;
3396 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3397 {
3398 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003399 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003400 ptr = find_line_end(ptr);
3401 else
3402 ptr = find_word_end(ptr);
3403 add_r = ins_compl_add_infercase(regmatch->startp[0],
3404 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003405 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003406 if (thesaurus)
3407 {
3408 char_u *wstart;
3409
3410 /*
3411 * Add the other matches on the line
3412 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003413 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003414 while (!got_int)
3415 {
3416 /* Find start of the next word. Skip white
3417 * space and punctuation. */
3418 ptr = find_word_start(ptr);
3419 if (*ptr == NUL || *ptr == NL)
3420 break;
3421 wstart = ptr;
3422
Bram Moolenaara2993e12007-08-12 14:38:46 +00003423 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003424#ifdef FEAT_MBYTE
3425 if (has_mbyte)
3426 /* Japanese words may have characters in
3427 * different classes, only separate words
3428 * with single-byte non-word characters. */
3429 while (*ptr != NUL)
3430 {
3431 int l = (*mb_ptr2len)(ptr);
3432
3433 if (l < 2 && !vim_iswordc(*ptr))
3434 break;
3435 ptr += l;
3436 }
3437 else
3438#endif
3439 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003440
3441 /* Add the word. Skip the regexp match. */
3442 if (wstart != regmatch->startp[0])
3443 add_r = ins_compl_add_infercase(wstart,
3444 (int)(ptr - wstart),
3445 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003446 }
3447 }
3448 if (add_r == OK)
3449 /* if dir was BACKWARD then honor it just once */
3450 *dir = FORWARD;
3451 else if (add_r == FAIL)
3452 break;
3453 /* avoid expensive call to vim_regexec() when at end
3454 * of line */
3455 if (*ptr == '\n' || got_int)
3456 break;
3457 }
3458 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003459 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003460 }
3461 fclose(fp);
3462 }
3463 }
3464}
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466/*
3467 * Find the start of the next word.
3468 * Returns a pointer to the first char of the word. Also stops at a NUL.
3469 */
3470 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003471find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473#ifdef FEAT_MBYTE
3474 if (has_mbyte)
3475 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003476 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 else
3478#endif
3479 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3480 ++ptr;
3481 return ptr;
3482}
3483
3484/*
3485 * Find the end of the word. Assumes it starts inside a word.
3486 * Returns a pointer to just after the word.
3487 */
3488 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003489find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
3491#ifdef FEAT_MBYTE
3492 int start_class;
3493
3494 if (has_mbyte)
3495 {
3496 start_class = mb_get_class(ptr);
3497 if (start_class > 1)
3498 while (*ptr != NUL)
3499 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003500 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (mb_get_class(ptr) != start_class)
3502 break;
3503 }
3504 }
3505 else
3506#endif
3507 while (vim_iswordc(*ptr))
3508 ++ptr;
3509 return ptr;
3510}
3511
3512/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003513 * Find the end of the line, omitting CR and NL at the end.
3514 * Returns a pointer to just after the line.
3515 */
3516 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003517find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003518{
3519 char_u *s;
3520
3521 s = ptr + STRLEN(ptr);
3522 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3523 --s;
3524 return s;
3525}
3526
3527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 * Free the list of completions
3529 */
3530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003531ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003533 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003534 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
Bram Moolenaard23a8232018-02-10 18:45:26 +01003536 VIM_CLEAR(compl_pattern);
3537 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003539 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003541
3542 ins_compl_del_pum();
3543 pum_clear();
3544
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003545 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 do
3547 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003548 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003549 compl_curr_match = compl_curr_match->cp_next;
3550 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003552 if (match->cp_flags & FREE_FNAME)
3553 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003554 for (i = 0; i < CPT_COUNT; ++i)
3555 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003557 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3558 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003559 compl_shown_match = NULL;
Bram Moolenaar4475b622017-05-01 20:46:52 +02003560 compl_old_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561}
3562
3563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003564ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003566 compl_cont_status = 0;
3567 compl_started = FALSE;
3568 compl_matches = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003569 VIM_CLEAR(compl_pattern);
3570 VIM_CLEAR(compl_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 edit_submode_extra = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003572 VIM_CLEAR(compl_orig_text);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003573 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003574 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003575 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576}
3577
3578/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003579 * Return TRUE when Insert completion is active.
3580 */
3581 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003582ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003583{
3584 return compl_started;
3585}
3586
3587/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003588 * Delete one character before the cursor and show the subset of the matches
3589 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003590 * Returns the character to be used, NUL if the work is done and another char
3591 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003592 */
3593 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003594ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003595{
3596 char_u *line;
3597 char_u *p;
3598
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003599 line = ml_get_curline();
3600 p = line + curwin->w_cursor.col;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003601 MB_PTR_BACK(line, p);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003602
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003603 /* Stop completion when the whole word was deleted. For Omni completion
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003604 * allow the word to be deleted, we won't match everything.
3605 * Respect the 'backspace' option. */
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003606 if ((int)(p - line) - (int)compl_col < 0
3607 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaar190b04c2017-02-09 17:37:03 +01003608 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
3609 || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
3610 - compl_length < 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003611 return K_BS;
3612
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003613 /* Deleted more than what was used to find matches or didn't finish
3614 * finding all matches: need to look for matches all over again. */
3615 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003616 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003617 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003618
Bram Moolenaara6557602006-02-04 22:43:20 +00003619 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003620 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003621 if (compl_leader != NULL)
3622 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003623 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003624 if (compl_shown_match != NULL)
3625 /* Make sure current match is not a hidden item. */
3626 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003627 return NUL;
3628 }
3629 return K_BS;
3630}
Bram Moolenaara6557602006-02-04 22:43:20 +00003631
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003632/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003633 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3634 * be called.
3635 */
3636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003637ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003638{
3639 /* Return TRUE if we didn't complete finding matches or when the
3640 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3641 return compl_was_interrupted
3642 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3643 && compl_opt_refresh_always);
3644}
3645
3646/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003647 * Called after changing "compl_leader".
3648 * Show the popup menu with a different set of matches.
3649 * May also search for matches again if the previous search was interrupted.
3650 */
3651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003652ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003653{
3654 ins_compl_del_pum();
3655 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003656 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003657 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003658
3659 if (compl_started)
3660 ins_compl_set_original_text(compl_leader);
3661 else
3662 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003663#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003664 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003665#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003666 /*
3667 * Matches were cleared, need to search for them now. First display
3668 * the changed text before the cursor. Set "compl_restarting" to
3669 * avoid that the first match is inserted.
3670 */
3671 update_screen(0);
3672#ifdef FEAT_GUI
3673 if (gui.in_use)
3674 {
3675 /* Show the cursor after the match, not after the redrawn text. */
3676 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003677 out_flush_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003678 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003679#endif
3680 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003681 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003682 compl_cont_status = 0;
3683 compl_restarting = FALSE;
3684 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003685
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003686 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003687
3688 /* Show the popup menu with a different set of matches. */
3689 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003690
3691 /* Don't let Enter select the original text when there is no popup menu. */
3692 if (compl_match_array == NULL)
3693 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003694}
3695
3696/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003697 * Return the length of the completion, from the completion start column to
3698 * the cursor column. Making sure it never goes below zero.
3699 */
3700 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003702{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003703 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003704
3705 if (off < 0)
3706 return 0;
3707 return off;
3708}
3709
3710/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003711 * Append one character to the match leader. May reduce the number of
3712 * matches.
3713 */
3714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003715ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003716{
3717#ifdef FEAT_MBYTE
3718 int cc;
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003719#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00003720
Bram Moolenaard56a79d2017-02-19 15:26:18 +01003721 if (stop_arrow() == FAIL)
3722 return;
3723#ifdef FEAT_MBYTE
Bram Moolenaara6557602006-02-04 22:43:20 +00003724 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3725 {
3726 char_u buf[MB_MAXBYTES + 1];
3727
3728 (*mb_char2bytes)(c, buf);
3729 buf[cc] = NUL;
3730 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003731 if (compl_opt_refresh_always)
3732 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003733 }
3734 else
3735#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003736 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003737 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003738 if (compl_opt_refresh_always)
3739 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003740 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003741
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003742 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003743 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003744 ins_compl_restart();
3745
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003746 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003747 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003748 * break redo. */
3749 if (!compl_opt_refresh_always)
3750 {
3751 vim_free(compl_leader);
3752 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003753 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003754 if (compl_leader != NULL)
3755 ins_compl_new_leader();
3756 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003757}
3758
3759/*
3760 * Setup for finding completions again without leaving CTRL-X mode. Used when
3761 * BS or a key was typed while still searching for matches.
3762 */
3763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003764ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003765{
3766 ins_compl_free();
3767 compl_started = FALSE;
3768 compl_matches = 0;
3769 compl_cont_status = 0;
3770 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003771}
3772
3773/*
3774 * Set the first match, the original text.
3775 */
3776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003778{
3779 char_u *p;
3780
Bram Moolenaare87edf32018-04-17 22:14:32 +02003781 /* Replace the original text entry.
3782 * The ORIGINAL_TEXT flag is either at the first item or might possibly be
3783 * at the last item for backward completion */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003784 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3785 {
3786 p = vim_strsave(str);
3787 if (p != NULL)
3788 {
3789 vim_free(compl_first_match->cp_str);
3790 compl_first_match->cp_str = p;
3791 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003792 }
Bram Moolenaare87edf32018-04-17 22:14:32 +02003793 else if (compl_first_match->cp_prev != NULL
3794 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3795 {
3796 p = vim_strsave(str);
3797 if (p != NULL)
3798 {
3799 vim_free(compl_first_match->cp_prev->cp_str);
3800 compl_first_match->cp_prev->cp_str = p;
3801 }
3802 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003803}
3804
3805/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003806 * Append one character to the match leader. May reduce the number of
3807 * matches.
3808 */
3809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003810ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003811{
3812 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003813 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003814 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003815 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003816
3817 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003818 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003819 {
3820 /* When still at the original match use the first entry that matches
3821 * the leader. */
3822 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3823 {
3824 p = NULL;
3825 for (cp = compl_shown_match->cp_next; cp != NULL
3826 && cp != compl_first_match; cp = cp->cp_next)
3827 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003828 if (compl_leader == NULL
3829 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003830 (int)STRLEN(compl_leader)))
3831 {
3832 p = cp->cp_str;
3833 break;
3834 }
3835 }
3836 if (p == NULL || (int)STRLEN(p) <= len)
3837 return;
3838 }
3839 else
3840 return;
3841 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003842 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003843 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003844 ins_compl_addleader(c);
3845}
3846
3847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003849 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003850 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003852 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003853ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003857 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
3859 /* Forget any previous 'special' messages if this is actually
3860 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3861 */
3862 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3863 edit_submode_extra = NULL;
3864
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003865 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003866 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3867 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003868 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003870 /* Set "compl_get_longest" when finding the first matches. */
3871 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003872 || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003873 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003874 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003875 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003876
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003877 }
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3880 {
3881 /*
3882 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3883 * it will be yet. Now we decide.
3884 */
3885 switch (c)
3886 {
3887 case Ctrl_E:
3888 case Ctrl_Y:
3889 ctrl_x_mode = CTRL_X_SCROLL;
3890 if (!(State & REPLACE_FLAG))
3891 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3892 else
3893 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3894 edit_submode_pre = NULL;
3895 showmode();
3896 break;
3897 case Ctrl_L:
3898 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3899 break;
3900 case Ctrl_F:
3901 ctrl_x_mode = CTRL_X_FILES;
3902 break;
3903 case Ctrl_K:
3904 ctrl_x_mode = CTRL_X_DICTIONARY;
3905 break;
3906 case Ctrl_R:
3907 /* Simply allow ^R to happen without affecting ^X mode */
3908 break;
3909 case Ctrl_T:
3910 ctrl_x_mode = CTRL_X_THESAURUS;
3911 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003912#ifdef FEAT_COMPL_FUNC
3913 case Ctrl_U:
3914 ctrl_x_mode = CTRL_X_FUNCTION;
3915 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003916 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003917 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003918 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003919#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003920 case 's':
3921 case Ctrl_S:
3922 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003923#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003924 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003925 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003926 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003927#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 case Ctrl_RSB:
3930 ctrl_x_mode = CTRL_X_TAGS;
3931 break;
3932#ifdef FEAT_FIND_ID
3933 case Ctrl_I:
3934 case K_S_TAB:
3935 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3936 break;
3937 case Ctrl_D:
3938 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3939 break;
3940#endif
3941 case Ctrl_V:
3942 case Ctrl_Q:
3943 ctrl_x_mode = CTRL_X_CMDLINE;
3944 break;
3945 case Ctrl_P:
3946 case Ctrl_N:
3947 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3948 * just started ^X mode, or there were enough ^X's to cancel
3949 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3950 * do normal expansion when interrupting a different mode (say
3951 * ^X^F^X^P or ^P^X^X^P, see below)
3952 * nothing changes if interrupting mode 0, (eg, the flag
3953 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003954 if (!(compl_cont_status & CONT_INTRPT))
3955 compl_cont_status |= CONT_LOCAL;
3956 else if (compl_cont_mode != 0)
3957 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 /* FALLTHROUGH */
3959 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003960 /* If we have typed at least 2 ^X's... for modes != 0, we set
3961 * compl_cont_status = 0 (eg, as if we had just started ^X
3962 * mode).
3963 * For mode 0, we set "compl_cont_mode" to an impossible
3964 * value, in both cases ^X^X can be used to restart the same
3965 * mode (avoiding ADDING mode).
3966 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3967 * 'complete' and local ^P expansions respectively.
3968 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3969 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (c == Ctrl_X)
3971 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003972 if (compl_cont_mode != 0)
3973 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003975 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003977 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 edit_submode = NULL;
3979 showmode();
3980 break;
3981 }
3982 }
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003983 else if (ctrl_x_mode != CTRL_X_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 {
3985 /* We're already in CTRL-X mode, do we stay in it? */
3986 if (!vim_is_ctrl_x_key(c))
3987 {
3988 if (ctrl_x_mode == CTRL_X_SCROLL)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01003989 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 else
3991 ctrl_x_mode = CTRL_X_FINISHED;
3992 edit_submode = NULL;
3993 }
3994 showmode();
3995 }
3996
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003997 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
3999 /* Show error message from attempted keyword completion (probably
4000 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004001 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 showmode();
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004003 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
4004 && c != Ctrl_R && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 || ctrl_x_mode == CTRL_X_FINISHED)
4006 {
4007 /* Get here when we have finished typing a sequence of ^N and
4008 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004009 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004010 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
4012 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004013 * If any of the original typed text has been changed, eg when
4014 * ignorecase is set, we must add back-spaces to the redo
4015 * buffer. We add as few as necessary to delete just the part
4016 * of the original text that has changed.
4017 * When using the longest match, edited the match or used
4018 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004020 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
4021 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004022 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02004023 ptr = NULL;
4024 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026
4027#ifdef FEAT_CINDENT
4028 want_cindent = (can_cindent && cindent_on());
4029#endif
4030 /*
4031 * When completing whole lines: fix indent for 'cindent'.
4032 * Otherwise, break line if it's too long.
4033 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004034 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
4036#ifdef FEAT_CINDENT
4037 /* re-indent the current line */
4038 if (want_cindent)
4039 {
4040 do_c_expr_indent();
4041 want_cindent = FALSE; /* don't do it again */
4042 }
4043#endif
4044 }
4045 else
4046 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004047 int prev_col = curwin->w_cursor.col;
4048
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004050 if (prev_col > 0)
4051 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02004052 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01004053 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00004055 if (prev_col > 0
4056 && ml_get_curline()[curwin->w_cursor.col] != NUL)
4057 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004060 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004061 * the selection without inserting anything. When
4062 * compl_enter_selects is set the Enter key does the same. */
4063 if ((c == Ctrl_Y || (compl_enter_selects
4064 && (c == CAR || c == K_KENTER || c == NL)))
4065 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004066 retval = TRUE;
4067
Bram Moolenaar47247282016-08-02 22:36:02 +02004068 /* CTRL-E means completion is Ended, go back to the typed text.
4069 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02004070 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004071 {
4072 ins_compl_delete();
4073 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004074 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004075 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004076 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004077 retval = TRUE;
4078 }
4079
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004080 auto_format(FALSE, TRUE);
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004083 compl_started = FALSE;
4084 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004085 if (!shortmess(SHM_COMPLETIONMENU))
4086 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004087 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004088 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 if (edit_submode != NULL)
4090 {
4091 edit_submode = NULL;
4092 showmode();
4093 }
4094
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02004095#ifdef FEAT_CMDWIN
4096 if (c == Ctrl_C && cmdwin_type != 0)
4097 /* Avoid the popup menu remains displayed when leaving the
4098 * command line window. */
4099 update_screen(0);
4100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101#ifdef FEAT_CINDENT
4102 /*
4103 * Indent now if a key was typed that is in 'cinkeys'.
4104 */
4105 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
4106 do_c_expr_indent();
4107#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02004108 /* Trigger the CompleteDone event to give scripts a chance to act
4109 * upon the completion. */
4110 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 }
Bram Moolenaara3914322013-02-13 16:30:21 +01004113 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
4114 /* Trigger the CompleteDone event to give scripts a chance to act
4115 * upon the (possibly failed) completion. */
4116 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
4118 /* reset continue_* if we left expansion-mode, if we stay they'll be
4119 * (re)set properly in ins_complete() */
4120 if (!vim_is_ctrl_x_key(c))
4121 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 compl_cont_status = 0;
4123 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004125
4126 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127}
4128
4129/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02004130 * Fix the redo buffer for the completion leader replacing some of the typed
4131 * text. This inserts backspaces and appends the changed text.
4132 * "ptr" is the known leader text or NUL.
4133 */
4134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004135ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02004136{
4137 int len;
4138 char_u *p;
4139 char_u *ptr = ptr_arg;
4140
4141 if (ptr == NULL)
4142 {
4143 if (compl_leader != NULL)
4144 ptr = compl_leader;
4145 else
4146 return; /* nothing to do */
4147 }
4148 if (compl_orig_text != NULL)
4149 {
4150 p = compl_orig_text;
4151 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
4152 ;
4153#ifdef FEAT_MBYTE
4154 if (len > 0)
4155 len -= (*mb_head_off)(p, p + len);
4156#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004157 for (p += len; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar62951b12011-09-21 18:23:05 +02004158 AppendCharToRedobuff(K_BS);
4159 }
4160 else
4161 len = 0;
4162 if (ptr != NULL)
4163 AppendToRedobuffLit(ptr + len, -1);
4164}
4165
4166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4168 * (depending on flag) starting from buf and looking for a non-scanned
4169 * buffer (other than curbuf). curbuf is special, if it is called with
4170 * buf=curbuf then it has to be the first call for a given flag/expansion.
4171 *
4172 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4173 */
4174 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004175ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 static win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
4179 if (flag == 'w') /* just windows */
4180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (buf == curbuf) /* first call for this flag/expansion */
4182 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004183 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 && wp->w_buffer->b_scanned)
4185 ;
4186 buf = wp->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188 else
4189 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4190 * (unlisted buffers)
4191 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004192 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 && ((flag == 'U'
4194 ? buf->b_p_bl
4195 : (!buf->b_p_bl
4196 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004197 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 ;
4199 return buf;
4200}
4201
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004202#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004203/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004204 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004205 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004206 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004208expand_by_function(
4209 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4210 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004211{
Bram Moolenaar82139082011-09-14 16:52:09 +02004212 list_T *matchlist = NULL;
4213 dict_T *matchdict = NULL;
Bram Moolenaarffa96842018-06-12 22:05:14 +02004214 typval_T args[3];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004215 char_u *funcname;
4216 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004217 win_T *curwin_save;
4218 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004219 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004220
Bram Moolenaare344bea2005-09-01 20:46:49 +00004221 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4222 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004223 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004224
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004225 /* Call 'completefunc' to obtain the list of matches. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004226 args[0].v_type = VAR_NUMBER;
4227 args[0].vval.v_number = 0;
4228 args[1].v_type = VAR_STRING;
4229 args[1].vval.v_string = base != NULL ? base : (char_u *)"";
4230 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004231
Bram Moolenaare344bea2005-09-01 20:46:49 +00004232 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004233 curwin_save = curwin;
4234 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004235
4236 /* Call a function, which returns a list or dict. */
Bram Moolenaarffa96842018-06-12 22:05:14 +02004237 if (call_vim_function(funcname, 2, args, &rettv, FALSE) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004238 {
4239 switch (rettv.v_type)
4240 {
4241 case VAR_LIST:
4242 matchlist = rettv.vval.v_list;
4243 break;
4244 case VAR_DICT:
4245 matchdict = rettv.vval.v_dict;
4246 break;
4247 default:
4248 /* TODO: Give error message? */
4249 clear_tv(&rettv);
4250 break;
4251 }
4252 }
4253
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004254 if (curwin_save != curwin || curbuf_save != curbuf)
4255 {
4256 EMSG(_(e_complwin));
4257 goto theend;
4258 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004259 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004260 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004261 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004262 {
4263 EMSG(_(e_compldel));
4264 goto theend;
4265 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004266
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004267 if (matchlist != NULL)
4268 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004269 else if (matchdict != NULL)
4270 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004271
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004272theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004273 if (matchdict != NULL)
4274 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004275 if (matchlist != NULL)
4276 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004277}
4278#endif /* FEAT_COMPL_FUNC */
4279
Bram Moolenaar39f05632006-03-19 22:15:26 +00004280#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004281/*
4282 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004283 */
4284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004285ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004286{
4287 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004288 int dir = compl_direction;
4289
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004290 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004291 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004292 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004293 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4294 /* if dir was BACKWARD then honor it just once */
4295 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004296 else if (did_emsg)
4297 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004298 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004299}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004300
4301/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004302 * Add completions from a dict.
4303 */
4304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004305ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004306{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004307 dictitem_T *di_refresh;
4308 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004309
4310 /* Check for optional "refresh" item. */
4311 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004312 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4313 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004314 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004315 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004316
4317 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4318 compl_opt_refresh_always = TRUE;
4319 }
4320
4321 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004322 di_words = dict_find(dict, (char_u *)"words", 5);
4323 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4324 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004325}
4326
4327/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004328 * Add a match to the list of matches from a typeval_T.
4329 * If the given string is already in the list of completions, then return
4330 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4331 * maybe because alloc() returns NULL, then FAIL is returned.
4332 */
4333 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004334ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004335{
4336 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004337 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004338 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004339 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004340 char_u *(cptext[CPT_COUNT]);
4341
4342 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4343 {
4344 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4345 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4346 (char_u *)"abbr", FALSE);
4347 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4348 (char_u *)"menu", FALSE);
4349 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4350 (char_u *)"kind", FALSE);
4351 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4352 (char_u *)"info", FALSE);
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004353 cptext[CPT_USER_DATA] = get_dict_string(tv->vval.v_dict,
4354 (char_u *)"user_data", FALSE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004355 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4356 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004357 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004358 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004359 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4360 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004361 }
4362 else
4363 {
4364 word = get_tv_string_chk(tv);
4365 vim_memset(cptext, 0, sizeof(cptext));
4366 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004367 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004368 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004369 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004370}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004371#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004372
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004373/*
4374 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004375 * The search starts at position "ini" in curbuf and in the direction
4376 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004377 * When "compl_started" is FALSE start at that position, otherwise continue
4378 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379 * This may return before finding all the matches.
4380 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 */
4382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004383ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 static pos_T first_match_pos;
4386 static pos_T last_match_pos;
4387 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388 static int found_all = FALSE; /* Found all matches of a
4389 certain type. */
4390 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
Bram Moolenaar572cb562005-08-05 21:35:02 +00004392 pos_T *pos;
4393 char_u **matches;
4394 int save_p_scs;
4395 int save_p_ws;
4396 int save_p_ic;
4397 int i;
4398 int num_matches;
4399 int len;
4400 int found_new_match;
4401 int type = ctrl_x_mode;
4402 char_u *ptr;
4403 char_u *dict = NULL;
4404 int dict_f = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004405 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004407 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004409 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 ins_buf->b_scanned = 0;
4411 found_all = FALSE;
4412 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004414 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 last_match_pos = first_match_pos = *ini;
4416 }
4417
Bram Moolenaar4475b622017-05-01 20:46:52 +02004418 compl_old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004419 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4421 for (;;)
4422 {
4423 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004424 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 * or if found_all says this entry is done. For ^X^L only use the
4428 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004429 if ((ctrl_x_mode == CTRL_X_NORMAL
4430 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
4433 found_all = FALSE;
4434 while (*e_cpt == ',' || *e_cpt == ' ')
4435 e_cpt++;
4436 if (*e_cpt == '.' && !curbuf->b_scanned)
4437 {
4438 ins_buf = curbuf;
4439 first_match_pos = *ini;
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004440 /* Move the cursor back one character so that ^N can match the
4441 * word immediately after the cursor. */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004442 if (ctrl_x_mode == CTRL_X_NORMAL && dec(&first_match_pos) < 0)
Bram Moolenaar24a9e342017-06-24 15:39:07 +02004443 {
4444 /* Move the cursor to after the last character in the
4445 * buffer, so that word at start of buffer is found
4446 * correctly. */
4447 first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
4448 first_match_pos.col =
4449 (colnr_T)STRLEN(ml_get(first_match_pos.lnum));
4450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 last_match_pos = first_match_pos;
4452 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004453
4454 /* Remember the first match so that the loop stops when we
4455 * wrap and come back there a second time. */
4456 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4459 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4460 {
4461 /* Scan a buffer, but not the current one. */
4462 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4463 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 first_match_pos.col = last_match_pos.col = 0;
4466 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4467 last_match_pos.lnum = 0;
4468 type = 0;
4469 }
4470 else /* unloaded buffer, scan like dictionary */
4471 {
4472 found_all = TRUE;
4473 if (ins_buf->b_fname == NULL)
4474 continue;
4475 type = CTRL_X_DICTIONARY;
4476 dict = ins_buf->b_fname;
4477 dict_f = DICT_EXACT;
4478 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004479 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 ins_buf->b_fname == NULL
4481 ? buf_spname(ins_buf)
4482 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004483 ? ins_buf->b_fname
4484 : ins_buf->b_sfname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004485 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 else if (*e_cpt == NUL)
4488 break;
4489 else
4490 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004491 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 type = -1;
4493 else if (*e_cpt == 'k' || *e_cpt == 's')
4494 {
4495 if (*e_cpt == 'k')
4496 type = CTRL_X_DICTIONARY;
4497 else
4498 type = CTRL_X_THESAURUS;
4499 if (*++e_cpt != ',' && *e_cpt != NUL)
4500 {
4501 dict = e_cpt;
4502 dict_f = DICT_FIRST;
4503 }
4504 }
4505#ifdef FEAT_FIND_ID
4506 else if (*e_cpt == 'i')
4507 type = CTRL_X_PATH_PATTERNS;
4508 else if (*e_cpt == 'd')
4509 type = CTRL_X_PATH_DEFINES;
4510#endif
4511 else if (*e_cpt == ']' || *e_cpt == 't')
4512 {
4513 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004514 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar8820b482017-03-16 17:23:31 +01004515 (void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517 else
4518 type = -1;
4519
4520 /* in any case e_cpt is advanced to the next entry */
4521 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4522
4523 found_all = TRUE;
4524 if (type == -1)
4525 continue;
4526 }
4527 }
4528
Bram Moolenaar4475b622017-05-01 20:46:52 +02004529 /* If complete() was called then compl_pattern has been reset. The
4530 * following won't work then, bail out. */
4531 if (compl_pattern == NULL)
4532 break;
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 switch (type)
4535 {
4536 case -1:
4537 break;
4538#ifdef FEAT_FIND_ID
4539 case CTRL_X_PATH_PATTERNS:
4540 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004541 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004542 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004544 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4546 (linenr_T)1, (linenr_T)MAXLNUM);
4547 break;
4548#endif
4549
4550 case CTRL_X_DICTIONARY:
4551 case CTRL_X_THESAURUS:
4552 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004553 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 : (type == CTRL_X_THESAURUS
4555 ? (*curbuf->b_p_tsr == NUL
4556 ? p_tsr
4557 : curbuf->b_p_tsr)
4558 : (*curbuf->b_p_dict == NUL
4559 ? p_dict
4560 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004561 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004562 dict != NULL ? dict_f
4563 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 dict = NULL;
4565 break;
4566
4567 case CTRL_X_TAGS:
4568 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4569 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004572 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004573 * of matches is found when compl_pattern is empty */
4574 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004575 TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
4576 | (ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4578 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004579 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 p_ic = save_p_ic;
4582 break;
4583
4584 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004585 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4587 {
4588
4589 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004591 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 break;
4594
4595 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 if (expand_cmdline(&compl_xp, compl_pattern,
4597 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004599 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 break;
4601
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004602#ifdef FEAT_COMPL_FUNC
4603 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004604 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004605 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004606 break;
4607#endif
4608
Bram Moolenaar488c6512005-08-11 20:09:58 +00004609 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004610#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004611 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004612 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004613 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004614 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004615#endif
4616 break;
4617
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 default: /* normal ^P/^N and ^X^L */
4619 /*
4620 * If 'infercase' is set, don't use 'smartcase' here
4621 */
4622 save_p_scs = p_scs;
4623 if (ins_buf->b_p_inf)
4624 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004625
Bram Moolenaar361aa502014-01-23 22:45:58 +01004626 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 * end but never from the middle, thus setting nowrapscan in this
4628 * buffers is a good idea, on the other hand, we always set
4629 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4630 save_p_ws = p_ws;
4631 if (ins_buf != curbuf)
4632 p_ws = FALSE;
4633 else if (*e_cpt == '.')
4634 p_ws = TRUE;
4635 for (;;)
4636 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004637 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004639 ++msg_silent; /* Don't want messages for wrapscan. */
4640
Bram Moolenaare4214502015-03-05 18:08:43 +01004641 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4642 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004643 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004644 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004647 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004649 found_new_match = searchit(NULL, ins_buf, pos,
4650 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004652 RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004653 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004654 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004656 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004657 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 first_match_pos = *pos;
4659 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004660 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 }
4662 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004663 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 found_new_match = FAIL;
4665 if (found_new_match == FAIL)
4666 {
4667 if (ins_buf == curbuf)
4668 found_all = TRUE;
4669 break;
4670 }
4671
4672 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004673 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 && ini->lnum == pos->lnum
4675 && ini->col == pos->col)
4676 continue;
4677 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004678 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004680 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
4682 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4683 continue;
4684 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4685 if (!p_paste)
4686 ptr = skipwhite(ptr);
4687 }
4688 len = (int)STRLEN(ptr);
4689 }
4690 else
4691 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004692 char_u *tmp_ptr = ptr;
4693
4694 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004696 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 /* Skip if already inside a word. */
4698 if (vim_iswordp(tmp_ptr))
4699 continue;
4700 /* Find start of next word. */
4701 tmp_ptr = find_word_start(tmp_ptr);
4702 }
4703 /* Find end of this word. */
4704 tmp_ptr = find_word_end(tmp_ptr);
4705 len = (int)(tmp_ptr - ptr);
4706
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004707 if ((compl_cont_status & CONT_ADDING)
4708 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
4710 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4711 {
4712 /* Try next line, if any. the new word will be
4713 * "join" as if the normal command "J" was used.
4714 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004715 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 * works -- Acevedo */
4717 STRNCPY(IObuff, ptr, len);
4718 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4719 tmp_ptr = ptr = skipwhite(ptr);
4720 /* Find start of next word. */
4721 tmp_ptr = find_word_start(tmp_ptr);
4722 /* Find end of next word. */
4723 tmp_ptr = find_word_end(tmp_ptr);
4724 if (tmp_ptr > ptr)
4725 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004726 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004728 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 IObuff[len++] = ' ';
4730 /* IObuf =~ "\k.* ", thus len >= 2 */
4731 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004732 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 || (vim_strchr(p_cpo, CPO_JOINSP)
4734 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004735 && (IObuff[len - 2] == '?'
4736 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 IObuff[len++] = ' ';
4738 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004739 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (tmp_ptr - ptr >= IOSIZE - len)
4741 tmp_ptr = ptr + IOSIZE - len - 1;
4742 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4743 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004744 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
4746 IObuff[len] = NUL;
4747 ptr = IObuff;
4748 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004749 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 continue;
4751 }
4752 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004753 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004754 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004755 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 found_new_match = OK;
4758 break;
4759 }
4760 }
4761 p_scs = save_p_scs;
4762 p_ws = save_p_ws;
4763 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004764
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004765 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004766 * expansion added something) */
Bram Moolenaar4475b622017-05-01 20:46:52 +02004767 if (type != 0 && compl_curr_match != compl_old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 found_new_match = OK;
4769
4770 /* break the loop for specialized modes (use 'complete' just for the
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004771 * generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new
4772 * match */
4773 if ((ctrl_x_mode != CTRL_X_NORMAL
4774 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004775 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004776 {
4777 if (got_int)
4778 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004779 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004780 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004781 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004782
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004783 if ((ctrl_x_mode != CTRL_X_NORMAL
4784 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004785 || compl_interrupted)
4786 break;
4787 compl_started = TRUE;
4788 }
4789 else
4790 {
4791 /* Mark a buffer scanned when it has been scanned completely */
4792 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4793 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004795 compl_started = FALSE;
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004798 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004800 if ((ctrl_x_mode == CTRL_X_NORMAL || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 && *e_cpt == NUL) /* Got to end of 'complete' */
4802 found_new_match = FAIL;
4803
4804 i = -1; /* total of matches, unknown */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01004805 if (found_new_match == FAIL || (ctrl_x_mode != CTRL_X_NORMAL
4806 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 i = ins_compl_make_cyclic();
4808
Bram Moolenaar4475b622017-05-01 20:46:52 +02004809 if (compl_old_match != NULL)
4810 {
4811 /* If several matches were added (FORWARD) or the search failed and has
4812 * just been made cyclic then we have to move compl_curr_match to the
4813 * next or previous entry (if any) -- Acevedo */
4814 compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
4815 : compl_old_match->cp_prev;
4816 if (compl_curr_match == NULL)
4817 compl_curr_match = compl_old_match;
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return i;
4820}
4821
4822/* Delete the old text being completed. */
4823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004824ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004826 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
4828 /*
4829 * In insert mode: Delete the typed part.
4830 * In replace mode: Put the old characters back, if any.
4831 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004832 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4833 if ((int)curwin->w_cursor.col > col)
4834 {
4835 if (stop_arrow() == FAIL)
4836 return;
4837 backspace_until_column(col);
4838 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004839
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004840 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4841 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004843 /* clear v:completed_item */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004844 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845}
4846
Bram Moolenaar472e8592016-10-15 17:06:47 +02004847/*
4848 * Insert the new text being completed.
4849 * "in_compl_func" is TRUE when called from complete_check().
4850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004852ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004854 dict_T *dict;
4855
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004856 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004857 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4858 compl_used_match = FALSE;
4859 else
4860 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004861
4862 /* Set completed item. */
4863 /* { word, abbr, menu, kind, info } */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004864 dict = dict_alloc_lock(VAR_FIXED);
Bram Moolenaar42a45122015-07-10 17:56:23 +02004865 if (dict != NULL)
4866 {
4867 dict_add_nr_str(dict, "word", 0L,
4868 EMPTY_IF_NULL(compl_shown_match->cp_str));
4869 dict_add_nr_str(dict, "abbr", 0L,
4870 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4871 dict_add_nr_str(dict, "menu", 0L,
4872 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4873 dict_add_nr_str(dict, "kind", 0L,
4874 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4875 dict_add_nr_str(dict, "info", 0L,
4876 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
Bram Moolenaar9b56a572018-02-10 16:19:32 +01004877 dict_add_nr_str(dict, "user_data", 0L,
4878 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
Bram Moolenaar42a45122015-07-10 17:56:23 +02004879 }
4880 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004881 if (!in_compl_func)
4882 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883}
4884
4885/*
4886 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004887 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4888 * get more completions. If it is FALSE, then we just do nothing when there
4889 * are no more completions in a given direction. The latter case is used when
4890 * we are still in the middle of finding completions, to allow browsing
4891 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 * Return the total number of matches, or -1 if still unknown -- webb.
4893 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004894 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4895 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 *
4897 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004898 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4899 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
4901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004902ins_compl_next(
4903 int allow_get_expansion,
4904 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004905 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004906 int insert_match, /* Insert the newly selected match */
4907 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908{
4909 int num_matches = -1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004910 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004911 compl_T *found_compl = NULL;
4912 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004913 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004914 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004916 /* When user complete function return -1 for findstart which is next
4917 * time of 'always', compl_shown_match become NULL. */
4918 if (compl_shown_match == NULL)
4919 return -1;
4920
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004921 if (compl_leader != NULL
4922 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004924 /* Set "compl_shown_match" to the actually shown match, it may differ
4925 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004926 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004927 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004928 && compl_shown_match->cp_next != NULL
4929 && compl_shown_match->cp_next != compl_first_match)
4930 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004931
4932 /* If we didn't find it searching forward, and compl_shows_dir is
4933 * backward, find the last match. */
4934 if (compl_shows_dir == BACKWARD
4935 && !ins_compl_equal(compl_shown_match,
4936 compl_leader, (int)STRLEN(compl_leader))
4937 && (compl_shown_match->cp_next == NULL
4938 || compl_shown_match->cp_next == compl_first_match))
4939 {
4940 while (!ins_compl_equal(compl_shown_match,
4941 compl_leader, (int)STRLEN(compl_leader))
4942 && compl_shown_match->cp_prev != NULL
4943 && compl_shown_match->cp_prev != compl_first_match)
4944 compl_shown_match = compl_shown_match->cp_prev;
4945 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004946 }
4947
4948 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004949 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 /* Delete old text to be replaced */
4951 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004952
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004953 /* When finding the longest common text we stick at the original text,
4954 * don't let CTRL-N or CTRL-P move to the first match. */
4955 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4956
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004957 /* When restarting the search don't insert the first match either. */
4958 if (compl_restarting)
4959 {
4960 advance = FALSE;
4961 compl_restarting = FALSE;
4962 }
4963
Bram Moolenaare3226be2005-12-18 22:10:00 +00004964 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4965 * around. */
4966 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004968 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004970 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004971 found_end = (compl_first_match != NULL
4972 && (compl_shown_match->cp_next == compl_first_match
4973 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004974 }
4975 else if (compl_shows_dir == BACKWARD
4976 && compl_shown_match->cp_prev != NULL)
4977 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004978 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004979 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004980 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004983 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004984 if (!allow_get_expansion)
4985 {
4986 if (advance)
4987 {
4988 if (compl_shows_dir == BACKWARD)
4989 compl_pending -= todo + 1;
4990 else
4991 compl_pending += todo + 1;
4992 }
4993 return -1;
4994 }
4995
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004996 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004997 {
4998 if (compl_shows_dir == BACKWARD)
4999 --compl_pending;
5000 else
5001 ++compl_pending;
5002 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005003
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005004 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005005 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00005006
5007 /* handle any pending completions */
5008 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00005009 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00005010 {
5011 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
5012 {
5013 compl_shown_match = compl_shown_match->cp_next;
5014 --compl_pending;
5015 }
5016 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
5017 {
5018 compl_shown_match = compl_shown_match->cp_prev;
5019 ++compl_pending;
5020 }
5021 else
5022 break;
5023 }
Bram Moolenaara6557602006-02-04 22:43:20 +00005024 found_end = FALSE;
5025 }
5026 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
5027 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005028 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005029 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00005030 ++todo;
5031 else
5032 /* Remember a matching item. */
5033 found_compl = compl_shown_match;
5034
5035 /* Stop at the end of the list when we found a usable match. */
5036 if (found_end)
5037 {
5038 if (found_compl != NULL)
5039 {
5040 compl_shown_match = found_compl;
5041 break;
5042 }
5043 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005047 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005048 if (compl_no_insert && !started)
5049 {
5050 ins_bytes(compl_orig_text + ins_compl_len());
5051 compl_used_match = FALSE;
5052 }
5053 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005054 {
5055 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005056 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005057 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00005058 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005059 }
5060 else
5061 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 if (!allow_get_expansion)
5064 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005065 /* may undisplay the popup menu first */
5066 ins_compl_upd_pum();
5067
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005068 /* redraw to show the user what was inserted */
5069 update_screen(0);
5070
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005071 /* display the updated popup menu */
5072 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00005073#ifdef FEAT_GUI
5074 if (gui.in_use)
5075 {
5076 /* Show the cursor after the match, not after the redrawn text. */
5077 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005078 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar14716812006-05-04 21:54:08 +00005079 }
5080#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 /* Delete old text to be replaced, since we're still searching and
5083 * don't want to match ourselves! */
5084 ins_compl_delete();
5085 }
5086
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005087 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00005088 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005089 if (compl_no_insert && !started)
5090 compl_enter_selects = TRUE;
5091 else
5092 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 /*
5095 * Show the file name for the match (if any)
5096 * Truncate the file name to avoid a wait for return.
5097 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005098 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar658a3a22017-03-31 22:27:12 +02005100 char *lead = _("match in file");
5101 int space = sc_col - vim_strsize((char_u *)lead) - 2;
5102 char_u *s;
5103 char_u *e;
5104
5105 if (space > 0)
5106 {
5107 /* We need the tail that fits. With double-byte encoding going
5108 * back from the end is very slow, thus go from the start and keep
5109 * the text that fits in "space" between "s" and "e". */
5110 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
5111 {
5112 space -= ptr2cells(e);
5113 while (space < 0)
5114 {
5115 space += ptr2cells(s);
5116 MB_PTR_ADV(s);
5117 }
5118 }
5119 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
5120 s > compl_shown_match->cp_fname ? "<" : "", s);
5121 msg(IObuff);
5122 redraw_cmdline = FALSE; /* don't overwrite! */
5123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125
5126 return num_matches;
5127}
5128
5129/*
5130 * Call this while finding completions, to check whether the user has hit a key
5131 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005132 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00005134 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02005135 * "in_compl_func" is TRUE when called from complete_check(), don't set
5136 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 */
5138 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02005139ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 static int count = 0;
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005142 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
Bram Moolenaar02ae9b42018-02-09 15:06:02 +01005144 /* Don't check when reading keys from a script, :normal or feedkeys().
5145 * That would break the test scripts. But do check for keys when called
5146 * from complete_check(). */
5147 if (!in_compl_func && (using_script() || ex_normal_busy))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 return;
5149
5150 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005151 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 return;
5153 count = 0;
5154
Bram Moolenaara260a972006-06-23 19:36:29 +00005155 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
5156 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (c != NUL)
5159 {
5160 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
5161 {
5162 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00005163 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005164 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02005165 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005167 else
5168 {
5169 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005170 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00005171 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005172 if (c != K_IGNORE)
5173 {
5174 /* Don't interrupt completion when the character wasn't typed,
5175 * e.g., when doing @q to replay keys. */
5176 if (c != Ctrl_R && KeyTyped)
5177 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00005178
Bram Moolenaard4e20a72008-01-22 16:50:03 +00005179 vungetc(c);
5180 }
Bram Moolenaara260a972006-06-23 19:36:29 +00005181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02005183 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00005184 {
5185 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5186
5187 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005188 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005189 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005190}
5191
5192/*
5193 * Decide the direction of Insert mode complete from the key typed.
5194 * Returns BACKWARD or FORWARD.
5195 */
5196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005197ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005198{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005199 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005200 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005201 return BACKWARD;
5202 return FORWARD;
5203}
5204
5205/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005206 * Return TRUE for keys that are used for completion only when the popup menu
5207 * is visible.
5208 */
5209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005210ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005211{
5212 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005213 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5214 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005215}
5216
5217/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005218 * Decide the number of completions to move forward.
5219 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5220 */
5221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005222ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005223{
5224 int h;
5225
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005226 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005227 {
5228 h = pum_get_height();
5229 if (h > 3)
5230 h -= 2; /* keep some context */
5231 return h;
5232 }
5233 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234}
5235
5236/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005237 * Return TRUE if completion with "c" should insert the match, FALSE if only
5238 * to change the currently selected completion.
5239 */
5240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005241ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005242{
5243 switch (c)
5244 {
5245 case K_UP:
5246 case K_DOWN:
5247 case K_PAGEDOWN:
5248 case K_KPAGEDOWN:
5249 case K_S_DOWN:
5250 case K_PAGEUP:
5251 case K_KPAGEUP:
5252 case K_S_UP:
5253 return FALSE;
5254 }
5255 return TRUE;
5256}
5257
5258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 * Do Insert mode completion.
5260 * Called when character "c" was typed, which has a meaning for completion.
5261 * Returns OK if completion was done, FAIL if something failed (out of mem).
5262 */
5263 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005264ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005266 char_u *line;
5267 int startcol = 0; /* column where searched text starts */
5268 colnr_T curs_col; /* cursor column */
5269 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005270 int save_w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005271 int save_w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005272 int insert_match;
Bram Moolenaard099e032017-02-21 23:00:36 +01005273 int save_did_ai = did_ai;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
Bram Moolenaare3226be2005-12-18 22:10:00 +00005275 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005276 insert_match = ins_compl_use_match(c);
5277
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 /* First time we hit ^N or ^P (in a row, I mean) */
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 did_ai = FALSE;
5283#ifdef FEAT_SMARTINDENT
5284 did_si = FALSE;
5285 can_si = FALSE;
5286 can_si_back = FALSE;
5287#endif
5288 if (stop_arrow() == FAIL)
5289 return FAIL;
5290
5291 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005292 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005293 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005295 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 * "compl_startpos" to the cursor as a pattern to add a new word
5297 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005298 * "compl_startpos" is not in the same line as the cursor then fix it
5299 * (the line has been split because it was longer than 'tw'). if SOL
5300 * is set then skip the previous pattern, a word at the beginning of
5301 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005302 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5303 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
5305 /*
5306 * it is a continued search
5307 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005309 if (ctrl_x_mode == CTRL_X_NORMAL
5310 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5311 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 /* line (probably) wrapped, set compl_startpos to the
5316 * first non_blank in the line, if it is not a wordchar
5317 * include it to get a better pattern, but then we don't
5318 * want the "\\<" prefix, check it bellow */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005319 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005320 compl_startpos.col = compl_col;
5321 compl_startpos.lnum = curwin->w_cursor.lnum;
5322 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324 else
5325 {
5326 /* S_IPOS was set when we inserted a word that was at the
5327 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 * mode but first we need to redefine compl_startpos */
5329 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005331 compl_cont_status |= CONT_SOL;
5332 compl_startpos.col = (colnr_T)(skipwhite(
5333 line + compl_length
5334 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005339 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005340 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 compl_cont_status &= ~CONT_SOL;
5345 compl_length = (IOSIZE - MIN_SPACE);
5346 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5349 if (compl_length < 1)
5350 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005352 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 compl_cont_mode = ctrl_x_mode;
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005363 if (ctrl_x_mode != CTRL_X_NORMAL)
5364 /* Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 compl_cont_status = 0;
5366 compl_cont_status |= CONT_N_ADDS;
5367 compl_startpos = curwin->w_cursor;
5368 startcol = (int)curs_col;
5369 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371
5372 /* Work out completion pattern and original text -- webb */
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005373 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5377 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005378 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 compl_col += ++startcol;
5383 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
5385 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005386 compl_pattern = str_foldcase(line + compl_col,
5387 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 compl_pattern = vim_strnsave(line + compl_col,
5390 compl_length);
5391 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 return FAIL;
5393 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 char_u *prefix = (char_u *)"\\<";
5397
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005398 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005399 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005400 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005401 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 if (!vim_iswordp(line + compl_col)
5404 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 && (
5406#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#endif
5411 )))
5412 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 STRCPY((char *)compl_pattern, prefix);
5414 (void)quote_meta(compl_pattern + STRLEN(prefix),
5415 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005417 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#endif
5423 )
5424 {
5425 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5427 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 compl_col += curs_col;
5430 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432 else
5433 {
5434#ifdef FEAT_MBYTE
5435 /* Search the point of change class of multibyte character
5436 * or not a word single byte character backward. */
5437 if (has_mbyte)
5438 {
5439 int base_class;
5440 int head_off;
5441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005442 startcol -= (*mb_head_off)(line, line + startcol);
5443 base_class = mb_get_class(line + startcol);
5444 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005446 head_off = (*mb_head_off)(line, line + startcol);
5447 if (base_class != mb_get_class(line + startcol
5448 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
5452 }
5453 else
5454#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005457 compl_col += ++startcol;
5458 compl_length = (int)curs_col - startcol;
5459 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
5461 /* Only match word with at least two chars -- webb
5462 * there's no need to call quote_meta,
5463 * alloc(7) is enough -- Acevedo
5464 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005465 compl_pattern = alloc(7);
5466 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005468 STRCPY((char *)compl_pattern, "\\<");
5469 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5470 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 else
5473 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005475 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005478 STRCPY((char *)compl_pattern, "\\<");
5479 (void)quote_meta(compl_pattern + 2, line + compl_col,
5480 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
5482 }
5483 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005484 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005486 compl_col = (colnr_T)getwhitecols(line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005487 compl_length = (int)curs_col - (int)compl_col;
5488 if (compl_length < 0) /* cursor in indent: empty pattern */
5489 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005491 compl_pattern = str_foldcase(line + compl_col, compl_length,
5492 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005494 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5495 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 return FAIL;
5497 }
5498 else if (ctrl_x_mode == CTRL_X_FILES)
5499 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005500 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005501 if (startcol > 0)
5502 {
5503 char_u *p = line + startcol;
5504
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005505 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005506 while (p > line && vim_isfilec(PTR2CHAR(p)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005507 MB_PTR_BACK(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005508 if (p == line && vim_isfilec(PTR2CHAR(p)))
5509 startcol = 0;
5510 else
5511 startcol = (int)(p - line) + 1;
5512 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005513
Bram Moolenaar0300e462013-09-08 16:03:45 +02005514 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 compl_length = (int)curs_col - startcol;
5516 compl_pattern = addstar(line + compl_col, compl_length,
5517 EXPAND_FILES);
5518 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 return FAIL;
5520 }
5521 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5522 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005523 compl_pattern = vim_strnsave(line, curs_col);
5524 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005526 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005527 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005528 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5529 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005530 /* No completion possible, use an empty pattern to get a
5531 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005532 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005533 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005534 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5535 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005537 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005538 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005539#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005540 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005541 * Call user defined function 'completefunc' with "a:findstart"
5542 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005543 */
Bram Moolenaarffa96842018-06-12 22:05:14 +02005544 typval_T args[3];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005545 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005546 char_u *funcname;
5547 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005548 win_T *curwin_save;
5549 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005550
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005551 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005552 * string */
5553 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5554 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5555 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005556 {
5557 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5558 ? "completefunc" : "omnifunc");
Bram Moolenaard099e032017-02-21 23:00:36 +01005559 /* restore did_ai, so that adding comment leader works */
5560 did_ai = save_did_ai;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005561 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005562 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005563
Bram Moolenaarffa96842018-06-12 22:05:14 +02005564 args[0].v_type = VAR_NUMBER;
5565 args[0].vval.v_number = 1;
5566 args[1].v_type = VAR_STRING;
5567 args[1].vval.v_string = (char_u *)"";
5568 args[2].v_type = VAR_UNKNOWN;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005569 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005570 curwin_save = curwin;
5571 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005572 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005573 if (curwin_save != curwin || curbuf_save != curbuf)
5574 {
5575 EMSG(_(e_complwin));
5576 return FAIL;
5577 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005578 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005579 validate_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005580 if (!EQUAL_POS(curwin->w_cursor, pos))
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005581 {
5582 EMSG(_(e_compldel));
5583 return FAIL;
5584 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005585
Bram Moolenaar6110a002012-01-26 18:58:38 +01005586 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005587 * cancel the complete without an error.
5588 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005589 if (col == -2)
5590 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005591 if (col == -3)
5592 {
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005593 ctrl_x_mode = CTRL_X_NORMAL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005594 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005595 if (!shortmess(SHM_COMPLETIONMENU))
5596 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005597 return FAIL;
5598 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005599
Bram Moolenaar82139082011-09-14 16:52:09 +02005600 /*
5601 * Reset extended parameters of completion, when start new
5602 * completion.
5603 */
5604 compl_opt_refresh_always = FALSE;
5605
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005606 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005607 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005608 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005609 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005610 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005611
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005612 /* Setup variables for completion. Need to obtain "line" again,
5613 * it may have become invalid. */
5614 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005615 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005616 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5617 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005618#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005619 return FAIL;
5620 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005621 else if (ctrl_x_mode == CTRL_X_SPELL)
5622 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005623#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005624 if (spell_bad_len > 0)
5625 compl_col = curs_col - spell_bad_len;
5626 else
5627 compl_col = spell_word_start(startcol);
5628 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005629 {
5630 compl_length = 0;
5631 compl_col = curs_col;
5632 }
5633 else
5634 {
5635 spell_expand_check_cap(compl_col);
5636 compl_length = (int)curs_col - compl_col;
5637 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005638 /* Need to obtain "line" again, it may have become invalid. */
5639 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005640 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5641 if (compl_pattern == NULL)
5642#endif
5643 return FAIL;
5644 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005645 else
5646 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005647 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005648 return FAIL;
5649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005651 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005654 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 /* Insert a new line, keep indentation but ignore 'comments' */
5657#ifdef FEAT_COMMENTS
5658 char_u *old = curbuf->b_p_com;
5659
5660 curbuf->b_p_com = (char_u *)"";
5661#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005662 compl_startpos.lnum = curwin->w_cursor.lnum;
5663 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 ins_eol('\r');
5665#ifdef FEAT_COMMENTS
5666 curbuf->b_p_com = old;
5667#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005668 compl_length = 0;
5669 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 }
5671 }
5672 else
5673 {
5674 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005675 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005678 if (compl_cont_status & CONT_LOCAL)
5679 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 else
5681 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5682
Bram Moolenaar62951b12011-09-21 18:23:05 +02005683 /* If any of the original typed text has been changed we need to fix
5684 * the redo buffer. */
5685 ins_compl_fixRedoBufForLeader(NULL);
5686
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005687 /* Always add completion for the original text. */
5688 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005689 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5690 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005691 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005693 VIM_CLEAR(compl_pattern);
5694 VIM_CLEAR(compl_orig_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 return FAIL;
5696 }
5697
5698 /* showmode might reset the internal line pointers, so it must
5699 * be called before line = ml_get(), or when this address is no
5700 * longer needed. -- Acevedo.
5701 */
5702 edit_submode_extra = (char_u *)_("-- Searching...");
5703 edit_submode_highl = HLF_COUNT;
5704 showmode();
5705 edit_submode_extra = NULL;
5706 out_flush();
5707 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005708 else if (insert_match && stop_arrow() == FAIL)
5709 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005711 compl_shown_match = compl_curr_match;
5712 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
5714 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005715 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005717 save_w_wrow = curwin->w_wrow;
Bram Moolenaarcb036422017-03-01 12:29:10 +01005718 save_w_leftcol = curwin->w_leftcol;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005719 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005721 /* may undisplay the popup menu */
5722 ins_compl_upd_pum();
5723
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005724 if (n > 1) /* all matches have been found */
5725 compl_matches = n;
5726 compl_curr_match = compl_shown_match;
5727 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
Bram Moolenaard68071d2006-05-02 22:08:30 +00005729 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5730 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (got_int && !global_busy)
5732 {
5733 (void)vgetc();
5734 got_int = FALSE;
5735 }
5736
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005737 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005738 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005740 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5741 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5743 edit_submode_highl = HLF_E;
5744 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5745 * because we couldn't expand anything at first place, but if we used
5746 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5747 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005748 if ( compl_length > 1
5749 || (compl_cont_status & CONT_ADDING)
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +01005750 || (ctrl_x_mode != CTRL_X_NORMAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5752 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005753 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755
Bram Moolenaar572cb562005-08-05 21:35:02 +00005756 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005757 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005759 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 if (edit_submode_extra == NULL)
5762 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005763 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
5765 edit_submode_extra = (char_u *)_("Back at original");
5766 edit_submode_highl = HLF_W;
5767 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005768 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
5770 edit_submode_extra = (char_u *)_("Word from other line");
5771 edit_submode_highl = HLF_COUNT;
5772 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005773 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 edit_submode_extra = (char_u *)_("The only match");
5776 edit_submode_highl = HLF_COUNT;
5777 }
5778 else
5779 {
5780 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005781 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005783 int number = 0;
5784 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005786 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 /* search backwards for the first valid (!= -1) number.
5789 * This should normally succeed already at the first loop
5790 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005791 for (match = compl_curr_match->cp_prev; match != NULL
5792 && match != compl_first_match;
5793 match = match->cp_prev)
5794 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005796 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 break;
5798 }
5799 if (match != NULL)
5800 /* go up and assign all numbers which are not assigned
5801 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005802 for (match = match->cp_next;
5803 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005804 match = match->cp_next)
5805 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807 else /* BACKWARD */
5808 {
5809 /* search forwards (upwards) for the first valid (!= -1)
5810 * number. This should normally succeed already at the
5811 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005812 for (match = compl_curr_match->cp_next; match != NULL
5813 && match != compl_first_match;
5814 match = match->cp_next)
5815 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005817 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819 }
5820 if (match != NULL)
5821 /* go down and assign all numbers which are not
5822 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005823 for (match = match->cp_prev; match
5824 && match->cp_number == -1;
5825 match = match->cp_prev)
5826 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 }
5828 }
5829
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005830 /* The match should always have a sequence number now, this is
5831 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005832 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005834 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5835 * Translations may need more than twice that. */
5836 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005838 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005839 vim_snprintf((char *)match_ref, sizeof(match_ref),
5840 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005841 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005843 vim_snprintf((char *)match_ref, sizeof(match_ref),
5844 _("match %d"),
5845 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 edit_submode_extra = match_ref;
5847 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005848 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 curs_columns(FALSE);
5850 }
5851 }
5852 }
5853
5854 /* Show a message about what (completion) mode we're in. */
5855 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005856 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005858 if (edit_submode_extra != NULL)
5859 {
5860 if (!p_smd)
5861 msg_attr(edit_submode_extra,
5862 edit_submode_highl < HLF_COUNT
Bram Moolenaar8820b482017-03-16 17:23:31 +01005863 ? HL_ATTR(edit_submode_highl) : 0);
Bram Moolenaarea389e92014-05-28 21:40:52 +02005864 }
5865 else
5866 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
Bram Moolenaard68071d2006-05-02 22:08:30 +00005869 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005870 if (enable_pum && !compl_interrupted)
Bram Moolenaarcb036422017-03-01 12:29:10 +01005871 show_pum(save_w_wrow, save_w_leftcol);
5872
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005873 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005874 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005875
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 return OK;
5877}
5878
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005879 static void
Bram Moolenaarcb036422017-03-01 12:29:10 +01005880show_pum(int prev_w_wrow, int prev_w_leftcol)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005881{
Bram Moolenaarcb036422017-03-01 12:29:10 +01005882 /* RedrawingDisabled may be set when invoked through complete(). */
5883 int n = RedrawingDisabled;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005884
Bram Moolenaarcb036422017-03-01 12:29:10 +01005885 RedrawingDisabled = 0;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005886
Bram Moolenaarcb036422017-03-01 12:29:10 +01005887 /* If the cursor moved or the display scrolled we need to remove the pum
5888 * first. */
5889 setcursor();
5890 if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
5891 ins_compl_del_pum();
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005892
Bram Moolenaarcb036422017-03-01 12:29:10 +01005893 ins_compl_show_pum();
5894 setcursor();
5895 RedrawingDisabled = n;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005896}
5897
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898/*
5899 * Looks in the first "len" chars. of "src" for search-metachars.
5900 * If dest is not NULL the chars. are copied there quoting (with
5901 * a backslash) the metachars, and dest would be NUL terminated.
5902 * Returns the length (needed) of dest
5903 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005904 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005905quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005907 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005909 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
5911 switch (*src)
5912 {
5913 case '.':
5914 case '*':
5915 case '[':
5916 if (ctrl_x_mode == CTRL_X_DICTIONARY
5917 || ctrl_x_mode == CTRL_X_THESAURUS)
5918 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005919 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 case '~':
5921 if (!p_magic) /* quote these only if magic is set */
5922 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005923 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 case '\\':
5925 if (ctrl_x_mode == CTRL_X_DICTIONARY
5926 || ctrl_x_mode == CTRL_X_THESAURUS)
5927 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005928 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 case '^': /* currently it's not needed. */
5930 case '$':
5931 m++;
5932 if (dest != NULL)
5933 *dest++ = '\\';
5934 break;
5935 }
5936 if (dest != NULL)
5937 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005938# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 /* Copy remaining bytes of a multibyte character. */
5940 if (has_mbyte)
5941 {
5942 int i, mb_len;
5943
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005944 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 if (mb_len > 0 && len >= mb_len)
5946 for (i = 0; i < mb_len; ++i)
5947 {
5948 --len;
5949 ++src;
5950 if (dest != NULL)
5951 *dest++ = *src;
5952 }
5953 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 }
5956 if (dest != NULL)
5957 *dest = NUL;
5958
5959 return m;
5960}
5961#endif /* FEAT_INS_EXPAND */
5962
5963/*
5964 * Next character is interpreted literally.
5965 * A one, two or three digit decimal number is interpreted as its byte value.
5966 * If one or two digits are entered, the next character is given to vungetc().
5967 * For Unicode a character > 255 may be returned.
5968 */
5969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005970get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
5972 int cc;
5973 int nc;
5974 int i;
5975 int hex = FALSE;
5976 int octal = FALSE;
5977#ifdef FEAT_MBYTE
5978 int unicode = 0;
5979#endif
5980
5981 if (got_int)
5982 return Ctrl_C;
5983
5984#ifdef FEAT_GUI
5985 /*
5986 * In GUI there is no point inserting the internal code for a special key.
5987 * It is more useful to insert the string "<KEY>" instead. This would
5988 * probably be useful in a text window too, but it would not be
5989 * vi-compatible (maybe there should be an option for it?) -- webb
5990 */
5991 if (gui.in_use)
5992 ++allow_keys;
5993#endif
5994#ifdef USE_ON_FLY_SCROLL
5995 dont_scroll = TRUE; /* disallow scrolling here */
5996#endif
5997 ++no_mapping; /* don't map the next key hits */
5998 cc = 0;
5999 i = 0;
6000 for (;;)
6001 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00006002 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003#ifdef FEAT_CMDL_INFO
6004 if (!(State & CMDLINE)
6005# ifdef FEAT_MBYTE
6006 && MB_BYTE2LEN_CHECK(nc) == 1
6007# endif
6008 )
6009 add_to_showcmd(nc);
6010#endif
6011 if (nc == 'x' || nc == 'X')
6012 hex = TRUE;
6013 else if (nc == 'o' || nc == 'O')
6014 octal = TRUE;
6015#ifdef FEAT_MBYTE
6016 else if (nc == 'u' || nc == 'U')
6017 unicode = nc;
6018#endif
6019 else
6020 {
6021 if (hex
6022#ifdef FEAT_MBYTE
6023 || unicode != 0
6024#endif
6025 )
6026 {
6027 if (!vim_isxdigit(nc))
6028 break;
6029 cc = cc * 16 + hex2nr(nc);
6030 }
6031 else if (octal)
6032 {
6033 if (nc < '0' || nc > '7')
6034 break;
6035 cc = cc * 8 + nc - '0';
6036 }
6037 else
6038 {
6039 if (!VIM_ISDIGIT(nc))
6040 break;
6041 cc = cc * 10 + nc - '0';
6042 }
6043
6044 ++i;
6045 }
6046
6047 if (cc > 255
6048#ifdef FEAT_MBYTE
6049 && unicode == 0
6050#endif
6051 )
6052 cc = 255; /* limit range to 0-255 */
6053 nc = 0;
6054
6055 if (hex) /* hex: up to two chars */
6056 {
6057 if (i >= 2)
6058 break;
6059 }
6060#ifdef FEAT_MBYTE
6061 else if (unicode) /* Unicode: up to four or eight chars */
6062 {
6063 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
6064 break;
6065 }
6066#endif
6067 else if (i >= 3) /* decimal or octal: up to three chars */
6068 break;
6069 }
6070 if (i == 0) /* no number entered */
6071 {
6072 if (nc == K_ZERO) /* NUL is stored as NL */
6073 {
6074 cc = '\n';
6075 nc = 0;
6076 }
6077 else
6078 {
6079 cc = nc;
6080 nc = 0;
6081 }
6082 }
6083
6084 if (cc == 0) /* NUL is stored as NL */
6085 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00006086#ifdef FEAT_MBYTE
6087 if (enc_dbcs && (cc & 0xff) == 0)
6088 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
6089 second byte will cause trouble! */
6090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092 --no_mapping;
6093#ifdef FEAT_GUI
6094 if (gui.in_use)
6095 --allow_keys;
6096#endif
6097 if (nc)
6098 vungetc(nc);
6099 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
6100 return cc;
6101}
6102
6103/*
6104 * Insert character, taking care of special keys and mod_mask
6105 */
6106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107insert_special(
6108 int c,
6109 int allow_modmask,
6110 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
6112 char_u *p;
6113 int len;
6114
6115 /*
6116 * Special function key, translate into "<Key>". Up to the last '>' is
6117 * inserted with ins_str(), so as not to replace characters in replace
6118 * mode.
6119 * Only use mod_mask for special keys, to avoid things like <S-Space>,
6120 * unless 'allow_modmask' is TRUE.
6121 */
Bram Moolenaard0573012017-10-28 21:11:06 +02006122#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 /* Command-key never produces a normal key */
6124 if (mod_mask & MOD_MASK_CMD)
6125 allow_modmask = TRUE;
6126#endif
6127 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
6128 {
6129 p = get_special_key_name(c, mod_mask);
6130 len = (int)STRLEN(p);
6131 c = p[len - 1];
6132 if (len > 2)
6133 {
6134 if (stop_arrow() == FAIL)
6135 return;
6136 p[len - 1] = NUL;
6137 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00006138 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 ctrlv = FALSE;
6140 }
6141 }
6142 if (stop_arrow() == OK)
6143 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
6144}
6145
6146/*
6147 * Special characters in this context are those that need processing other
6148 * than the simple insertion that can be performed here. This includes ESC
6149 * which terminates the insert, and CR/NL which need special processing to
6150 * open up a new line. This routine tries to optimize insertions performed by
6151 * the "redo", "undo" or "put" commands, so it needs to know when it should
6152 * stop and defer processing to the "normal" mechanism.
6153 * '0' and '^' are special, because they can be followed by CTRL-D.
6154 */
6155#ifdef EBCDIC
6156# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
6157#else
6158# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
6159#endif
6160
6161#ifdef FEAT_MBYTE
Bram Moolenaar1c465442017-03-12 20:10:05 +01006162# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163#else
Bram Moolenaar1c465442017-03-12 20:10:05 +01006164# define WHITECHAR(cc) VIM_ISWHITE(cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165#endif
6166
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006167/*
6168 * "flags": INSCHAR_FORMAT - force formatting
6169 * INSCHAR_CTRLV - char typed just after CTRL-V
6170 * INSCHAR_NO_FEX - don't use 'formatexpr'
6171 *
6172 * NOTE: passes the flags value straight through to internal_format() which,
6173 * beside INSCHAR_FORMAT (above), is also looking for these:
6174 * INSCHAR_DO_COM - format comments
6175 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
6176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178insertchar(
6179 int c, /* character to insert or NUL */
6180 int flags, /* INSCHAR_FORMAT, etc. */
6181 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 int textwidth;
6184#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006188 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006190 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192
6193 /*
6194 * Try to break the line in two or more pieces when:
6195 * - Always do this if we have been called to do formatting only.
6196 * - Always do this when 'formatoptions' has the 'a' flag and the line
6197 * ends in white space.
6198 * - Otherwise:
6199 * - Don't do this if inserting a blank
6200 * - Don't do this if an existing character is being replaced, unless
6201 * we're in VREPLACE mode.
6202 * - Do this if the cursor is not on the line where insert started
6203 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6204 * before the insert.
6205 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6206 * before 'textwidth'
6207 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006208 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006209 && (force_format
Bram Moolenaar1c465442017-03-12 20:10:05 +01006210 || (!VIM_ISWHITE(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 && !((State & REPLACE_FLAG)
6212#ifdef FEAT_VREPLACE
6213 && !(State & VREPLACE_FLAG)
6214#endif
6215 && *ml_get_cursor() != NUL)
6216 && (curwin->w_cursor.lnum != Insstart.lnum
6217 || ((!has_format_option(FO_INS_LONG)
6218 || Insstart_textlen <= (colnr_T)textwidth)
6219 && (!fo_ins_blank
6220 || Insstart_blank_vcol <= (colnr_T)textwidth
6221 ))))))
6222 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006223 /* Format with 'formatexpr' when it's set. Use internal formatting
6224 * when 'formatexpr' isn't set or it returns non-zero. */
6225#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006226 int do_internal = TRUE;
6227 colnr_T virtcol = get_nolist_virtcol()
6228 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006229
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006230 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6231 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006232 {
6233 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6234 /* It may be required to save for undo again, e.g. when setline()
6235 * was called. */
6236 ins_need_undo = TRUE;
6237 }
6238 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006240 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (c == NUL) /* only formatting was wanted */
6244 return;
6245
6246#ifdef FEAT_COMMENTS
6247 /* Check whether this character should end a comment. */
6248 if (did_ai && (int)c == end_comment_pending)
6249 {
6250 char_u *line;
6251 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6252 int middle_len, end_len;
6253 int i;
6254
6255 /*
6256 * Need to remove existing (middle) comment leader and insert end
6257 * comment leader. First, check what comment leader we can find.
6258 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006259 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6261 {
6262 /* Skip middle-comment string */
6263 while (*p && p[-1] != ':') /* find end of middle flags */
6264 ++p;
6265 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6266 /* Don't count trailing white space for middle_len */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006267 while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 --middle_len;
6269
6270 /* Find the end-comment string */
6271 while (*p && p[-1] != ':') /* find end of end flags */
6272 ++p;
6273 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6274
6275 /* Skip white space before the cursor */
6276 i = curwin->w_cursor.col;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006277 while (--i >= 0 && VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 ;
6279 i++;
6280
6281 /* Skip to before the middle leader */
6282 i -= middle_len;
6283
6284 /* Check some expected things before we go on */
6285 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6286 {
6287 /* Backspace over all the stuff we want to replace */
6288 backspace_until_column(i);
6289
6290 /*
6291 * Insert the end-comment string, except for the last
6292 * character, which will get inserted as normal later.
6293 */
6294 ins_bytes_len(lead_end, end_len - 1);
6295 }
6296 }
6297 }
6298 end_comment_pending = NUL;
6299#endif
6300
6301 did_ai = FALSE;
6302#ifdef FEAT_SMARTINDENT
6303 did_si = FALSE;
6304 can_si = FALSE;
6305 can_si_back = FALSE;
6306#endif
6307
6308 /*
6309 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6310 * This speeds up normal text input considerably.
6311 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6312 * need to re-indent at a ':', or any other character (but not what
6313 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006314 * Don't do this when there an InsertCharPre autocommand is defined,
6315 * because we need to fire the event for every character.
Bram Moolenaar39de9522018-05-08 22:48:00 +02006316 * Do the check for InsertCharPre before the call to vpeekc() because the
6317 * InsertCharPre autocommand could change the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 */
6319#ifdef USE_ON_FLY_SCROLL
6320 dont_scroll = FALSE; /* allow scrolling here */
6321#endif
6322
6323 if ( !ISSPECIAL(c)
6324#ifdef FEAT_MBYTE
6325 && (!has_mbyte || (*mb_char2len)(c) == 1)
6326#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006327 && !has_insertcharpre()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 && vpeekc() != NUL
6329 && !(State & REPLACE_FLAG)
6330#ifdef FEAT_CINDENT
6331 && !cindent_on()
6332#endif
6333#ifdef FEAT_RIGHTLEFT
6334 && !p_ri
6335#endif
Bram Moolenaar39de9522018-05-08 22:48:00 +02006336 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
6338#define INPUT_BUFLEN 100
6339 char_u buf[INPUT_BUFLEN + 1];
6340 int i;
6341 colnr_T virtcol = 0;
6342
6343 buf[0] = c;
6344 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006345 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 virtcol = get_nolist_virtcol();
6347 /*
6348 * Stop the string when:
6349 * - no more chars available
6350 * - finding a special character (command key)
6351 * - buffer is full
6352 * - running into the 'textwidth' boundary
6353 * - need to check for abbreviation: A non-word char after a word-char
6354 */
6355 while ( (c = vpeekc()) != NUL
6356 && !ISSPECIAL(c)
6357#ifdef FEAT_MBYTE
6358 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6359#endif
6360 && i < INPUT_BUFLEN
Bram Moolenaarddf662a2017-01-29 17:59:12 +01006361# ifdef FEAT_FKMAP
6362 && !(p_fkmap && KeyTyped) /* Farsi mode mapping moves cursor */
6363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 && (textwidth == 0
6365 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6366 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6367 {
6368#ifdef FEAT_RIGHTLEFT
6369 c = vgetc();
6370 if (p_hkmap && KeyTyped)
6371 c = hkmap(c); /* Hebrew mode mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 buf[i++] = c;
6373#else
6374 buf[i++] = vgetc();
6375#endif
6376 }
6377
6378#ifdef FEAT_DIGRAPHS
6379 do_digraph(-1); /* clear digraphs */
6380 do_digraph(buf[i-1]); /* may be the start of a digraph */
6381#endif
6382 buf[i] = NUL;
6383 ins_str(buf);
6384 if (flags & INSCHAR_CTRLV)
6385 {
6386 redo_literal(*buf);
6387 i = 1;
6388 }
6389 else
6390 i = 0;
6391 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006392 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394 else
6395 {
6396#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006397 int cc;
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6400 {
6401 char_u buf[MB_MAXBYTES + 1];
6402
6403 (*mb_char2bytes)(c, buf);
6404 buf[cc] = NUL;
6405 ins_char_bytes(buf, cc);
6406 AppendCharToRedobuff(c);
6407 }
6408 else
6409#endif
6410 {
6411 ins_char(c);
6412 if (flags & INSCHAR_CTRLV)
6413 redo_literal(c);
6414 else
6415 AppendCharToRedobuff(c);
6416 }
6417 }
6418}
6419
6420/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006421 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006422 *
6423 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6424 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006425 */
6426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006427internal_format(
6428 int textwidth,
6429 int second_indent,
6430 int flags,
6431 int format_only,
6432 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433{
6434 int cc;
6435 int save_char = NUL;
6436 int haveto_redraw = FALSE;
6437 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6438#ifdef FEAT_MBYTE
6439 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6440#endif
6441 int fo_white_par = has_format_option(FO_WHITE_PAR);
6442 int first_line = TRUE;
6443#ifdef FEAT_COMMENTS
6444 colnr_T leader_len;
6445 int no_leader = FALSE;
6446 int do_comments = (flags & INSCHAR_DO_COM);
6447#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006448#ifdef FEAT_LINEBREAK
6449 int has_lbr = curwin->w_p_lbr;
6450
6451 /* make sure win_lbr_chartabsize() counts correctly */
6452 curwin->w_p_lbr = FALSE;
6453#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006454
6455 /*
6456 * When 'ai' is off we don't want a space under the cursor to be
6457 * deleted. Replace it with an 'x' temporarily.
6458 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006459 if (!curbuf->b_p_ai
6460#ifdef FEAT_VREPLACE
6461 && !(State & VREPLACE_FLAG)
6462#endif
6463 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006464 {
6465 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006466 if (VIM_ISWHITE(cc))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006467 {
6468 save_char = cc;
6469 pchar_cursor('x');
6470 }
6471 }
6472
6473 /*
6474 * Repeat breaking lines, until the current line is not too long.
6475 */
6476 while (!got_int)
6477 {
6478 int startcol; /* Cursor column at entry */
6479 int wantcol; /* column at textwidth border */
6480 int foundcol; /* column for start of spaces */
6481 int end_foundcol = 0; /* column for start of word */
6482 colnr_T len;
6483 colnr_T virtcol;
6484#ifdef FEAT_VREPLACE
6485 int orig_col = 0;
6486 char_u *saved_text = NULL;
6487#endif
6488 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006489 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006490
Bram Moolenaar97b98102009-11-17 16:41:01 +00006491 virtcol = get_nolist_virtcol()
6492 + char2cells(c != NUL ? c : gchar_cursor());
6493 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006494 break;
6495
6496#ifdef FEAT_COMMENTS
6497 if (no_leader)
6498 do_comments = FALSE;
6499 else if (!(flags & INSCHAR_FORMAT)
6500 && has_format_option(FO_WRAP_COMS))
6501 do_comments = TRUE;
6502
6503 /* Don't break until after the comment leader */
6504 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006505 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006506 else
6507 leader_len = 0;
6508
6509 /* If the line doesn't start with a comment leader, then don't
6510 * start one in a following broken line. Avoids that a %word
6511 * moved to the start of the next line causes all following lines
6512 * to start with %. */
6513 if (leader_len == 0)
6514 no_leader = TRUE;
6515#endif
6516 if (!(flags & INSCHAR_FORMAT)
6517#ifdef FEAT_COMMENTS
6518 && leader_len == 0
6519#endif
6520 && !has_format_option(FO_WRAP))
6521
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006522 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523 if ((startcol = curwin->w_cursor.col) == 0)
6524 break;
6525
6526 /* find column of textwidth border */
6527 coladvance((colnr_T)textwidth);
6528 wantcol = curwin->w_cursor.col;
6529
Bram Moolenaar97b98102009-11-17 16:41:01 +00006530 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006531 foundcol = 0;
6532
6533 /*
6534 * Find position to break at.
6535 * Stop at first entered white when 'formatoptions' has 'v'
6536 */
6537 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006538 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006539 || curwin->w_cursor.lnum != Insstart.lnum
6540 || curwin->w_cursor.col >= Insstart.col)
6541 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006542 if (curwin->w_cursor.col == startcol && c != NUL)
6543 cc = c;
6544 else
6545 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006546 if (WHITECHAR(cc))
6547 {
6548 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006549 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006550
6551 /* find start of sequence of blanks */
6552 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6553 {
6554 dec_cursor();
6555 cc = gchar_cursor();
6556 }
6557 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6558 break; /* only spaces in front of text */
6559#ifdef FEAT_COMMENTS
6560 /* Don't break until after the comment leader */
6561 if (curwin->w_cursor.col < leader_len)
6562 break;
6563#endif
6564 if (has_format_option(FO_ONE_LETTER))
6565 {
6566 /* do not break after one-letter words */
6567 if (curwin->w_cursor.col == 0)
6568 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006569#ifdef FEAT_COMMENTS
6570 /* do not break "#a b" when 'tw' is 2 */
6571 if (curwin->w_cursor.col <= leader_len)
6572 break;
6573#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006574 col = curwin->w_cursor.col;
6575 dec_cursor();
6576 cc = gchar_cursor();
6577
6578 if (WHITECHAR(cc))
6579 continue; /* one-letter, continue */
6580 curwin->w_cursor.col = col;
6581 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006582
6583 inc_cursor();
6584
6585 end_foundcol = end_col + 1;
6586 foundcol = curwin->w_cursor.col;
6587 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006588 break;
6589 }
6590#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006591 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006592 {
6593 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006594 if (curwin->w_cursor.col != startcol)
6595 {
6596#ifdef FEAT_COMMENTS
6597 /* Don't break until after the comment leader */
6598 if (curwin->w_cursor.col < leader_len)
6599 break;
6600#endif
6601 col = curwin->w_cursor.col;
6602 inc_cursor();
6603 /* Don't change end_foundcol if already set. */
6604 if (foundcol != curwin->w_cursor.col)
6605 {
6606 foundcol = curwin->w_cursor.col;
6607 end_foundcol = foundcol;
6608 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6609 break;
6610 }
6611 curwin->w_cursor.col = col;
6612 }
6613
6614 if (curwin->w_cursor.col == 0)
6615 break;
6616
6617 col = curwin->w_cursor.col;
6618
6619 dec_cursor();
6620 cc = gchar_cursor();
6621
6622 if (WHITECHAR(cc))
6623 continue; /* break with space */
6624#ifdef FEAT_COMMENTS
6625 /* Don't break until after the comment leader */
6626 if (curwin->w_cursor.col < leader_len)
6627 break;
6628#endif
6629
6630 curwin->w_cursor.col = col;
6631
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006632 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006633 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006634 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6635 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006636 }
6637#endif
6638 if (curwin->w_cursor.col == 0)
6639 break;
6640 dec_cursor();
6641 }
6642
6643 if (foundcol == 0) /* no spaces, cannot break line */
6644 {
6645 curwin->w_cursor.col = startcol;
6646 break;
6647 }
6648
6649 /* Going to break the line, remove any "$" now. */
6650 undisplay_dollar();
6651
6652 /*
6653 * Offset between cursor position and line break is used by replace
6654 * stack functions. VREPLACE does not use this, and backspaces
6655 * over the text instead.
6656 */
6657#ifdef FEAT_VREPLACE
6658 if (State & VREPLACE_FLAG)
6659 orig_col = startcol; /* Will start backspacing from here */
6660 else
6661#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006662 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006663
6664 /*
6665 * adjust startcol for spaces that will be deleted and
6666 * characters that will remain on top line
6667 */
6668 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006669 while ((cc = gchar_cursor(), WHITECHAR(cc))
6670 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006671 inc_cursor();
6672 startcol -= curwin->w_cursor.col;
6673 if (startcol < 0)
6674 startcol = 0;
6675
6676#ifdef FEAT_VREPLACE
6677 if (State & VREPLACE_FLAG)
6678 {
6679 /*
6680 * In VREPLACE mode, we will backspace over the text to be
6681 * wrapped, so save a copy now to put on the next line.
6682 */
6683 saved_text = vim_strsave(ml_get_cursor());
6684 curwin->w_cursor.col = orig_col;
6685 if (saved_text == NULL)
6686 break; /* Can't do it, out of memory */
6687 saved_text[startcol] = NUL;
6688
6689 /* Backspace over characters that will move to the next line */
6690 if (!fo_white_par)
6691 backspace_until_column(foundcol);
6692 }
6693 else
6694#endif
6695 {
6696 /* put cursor after pos. to break line */
6697 if (!fo_white_par)
6698 curwin->w_cursor.col = foundcol;
6699 }
6700
6701 /*
6702 * Split the line just before the margin.
6703 * Only insert/delete lines, but don't really redraw the window.
6704 */
6705 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6706 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6707#ifdef FEAT_COMMENTS
6708 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006709 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006710#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006711 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6712 if (!(flags & INSCHAR_COM_LIST))
6713 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006714
6715 replace_offset = 0;
6716 if (first_line)
6717 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006718 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006719 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006720 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006721 * This section is for auto-wrap of numeric lists. When not
6722 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6723 * flag will be set and open_line() will handle it (as seen
6724 * above). The code here (and in get_number_indent()) will
6725 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006726 */
6727 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006728 second_indent =
6729 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006730 if (second_indent >= 0)
6731 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006732#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006733 if (State & VREPLACE_FLAG)
6734 change_indent(INDENT_SET, second_indent,
6735 FALSE, NUL, TRUE);
6736 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006737#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006738#ifdef FEAT_COMMENTS
6739 if (leader_len > 0 && second_indent - leader_len > 0)
6740 {
6741 int i;
6742 int padding = second_indent - leader_len;
6743
6744 /* We started at the first_line of a numbered list
6745 * that has a comment. the open_line() function has
6746 * inserted the proper comment leader and positioned
6747 * the cursor at the end of the split line. Now we
6748 * add the additional whitespace needed after the
6749 * comment leader for the numbered list. */
6750 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006751 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006752 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006753 }
6754 else
6755 {
6756#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006757 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006758#ifdef FEAT_COMMENTS
6759 }
6760#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006761 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006762 }
6763 first_line = FALSE;
6764 }
6765
6766#ifdef FEAT_VREPLACE
6767 if (State & VREPLACE_FLAG)
6768 {
6769 /*
6770 * In VREPLACE mode we have backspaced over the text to be
6771 * moved, now we re-insert it into the new line.
6772 */
6773 ins_bytes(saved_text);
6774 vim_free(saved_text);
6775 }
6776 else
6777#endif
6778 {
6779 /*
6780 * Check if cursor is not past the NUL off the line, cindent
6781 * may have added or removed indent.
6782 */
6783 curwin->w_cursor.col += startcol;
6784 len = (colnr_T)STRLEN(ml_get_curline());
6785 if (curwin->w_cursor.col > len)
6786 curwin->w_cursor.col = len;
6787 }
6788
6789 haveto_redraw = TRUE;
6790#ifdef FEAT_CINDENT
6791 can_cindent = TRUE;
6792#endif
6793 /* moved the cursor, don't autoindent or cindent now */
6794 did_ai = FALSE;
6795#ifdef FEAT_SMARTINDENT
6796 did_si = FALSE;
6797 can_si = FALSE;
6798 can_si_back = FALSE;
6799#endif
6800 line_breakcheck();
6801 }
6802
6803 if (save_char != NUL) /* put back space after cursor */
6804 pchar_cursor(save_char);
6805
Bram Moolenaar0026d472014-09-09 16:32:39 +02006806#ifdef FEAT_LINEBREAK
6807 curwin->w_p_lbr = has_lbr;
6808#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006809 if (!format_only && haveto_redraw)
6810 {
6811 update_topline();
6812 redraw_curbuf_later(VALID);
6813 }
6814}
6815
6816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 * Called after inserting or deleting text: When 'formatoptions' includes the
6818 * 'a' flag format from the current line until the end of the paragraph.
6819 * Keep the cursor at the same position relative to the text.
6820 * The caller must have saved the cursor line for undo, following ones will be
6821 * saved here.
6822 */
6823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006824auto_format(
6825 int trailblank, /* when TRUE also format with trailing blank */
6826 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
6828 pos_T pos;
6829 colnr_T len;
6830 char_u *old;
6831 char_u *new, *pnew;
6832 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006833 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
6835 if (!has_format_option(FO_AUTO))
6836 return;
6837
6838 pos = curwin->w_cursor;
6839 old = ml_get_curline();
6840
6841 /* may remove added space */
6842 check_auto_format(FALSE);
6843
6844 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6845 * user might insert normal text next. Also skip formatting when "1" is
6846 * in 'formatoptions' and there is a single character before the cursor.
6847 * Otherwise the line would be broken and when typing another non-white
6848 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006849 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 if (*old != NUL && !trailblank && wasatend)
6851 {
6852 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006853 cc = gchar_cursor();
6854 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6855 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006857 cc = gchar_cursor();
6858 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
6860 curwin->w_cursor = pos;
6861 return;
6862 }
6863 curwin->w_cursor = pos;
6864 }
6865
6866#ifdef FEAT_COMMENTS
6867 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6868 * comments. */
6869 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006870 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 return;
6872#endif
6873
6874 /*
6875 * May start formatting in a previous line, so that after "x" a word is
6876 * moved to the previous line if it fits there now. Only when this is not
6877 * the start of a paragraph.
6878 */
6879 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6880 {
6881 --curwin->w_cursor.lnum;
6882 if (u_save_cursor() == FAIL)
6883 return;
6884 }
6885
6886 /*
6887 * Do the formatting and restore the cursor position. "saved_cursor" will
6888 * be adjusted for the text formatting.
6889 */
6890 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006891 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 curwin->w_cursor = saved_cursor;
6893 saved_cursor.lnum = 0;
6894
6895 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6896 {
6897 /* "cannot happen" */
6898 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6899 coladvance((colnr_T)MAXCOL);
6900 }
6901 else
6902 check_cursor_col();
6903
6904 /* Insert mode: If the cursor is now after the end of the line while it
6905 * previously wasn't, the line was broken. Because of the rule above we
6906 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6907 * formatted. */
6908 if (!wasatend && has_format_option(FO_WHITE_PAR))
6909 {
6910 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006911 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 if (curwin->w_cursor.col == len)
6913 {
6914 pnew = vim_strnsave(new, len + 2);
6915 pnew[len] = ' ';
6916 pnew[len + 1] = NUL;
6917 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6918 /* remove the space later */
6919 did_add_space = TRUE;
6920 }
6921 else
6922 /* may remove added space */
6923 check_auto_format(FALSE);
6924 }
6925
6926 check_cursor();
6927}
6928
6929/*
6930 * When an extra space was added to continue a paragraph for auto-formatting,
6931 * delete it now. The space must be under the cursor, just after the insert
6932 * position.
6933 */
6934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006935check_auto_format(
6936 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006939 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941 if (did_add_space)
6942 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006943 cc = gchar_cursor();
6944 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 /* Somehow the space was removed already. */
6946 did_add_space = FALSE;
6947 else
6948 {
6949 if (!end_insert)
6950 {
6951 inc_cursor();
6952 c = gchar_cursor();
6953 dec_cursor();
6954 }
6955 if (c != NUL)
6956 {
6957 /* The space is no longer at the end of the line, delete it. */
6958 del_char(FALSE);
6959 did_add_space = FALSE;
6960 }
6961 }
6962 }
6963}
6964
6965/*
6966 * Find out textwidth to be used for formatting:
6967 * if 'textwidth' option is set, use it
Bram Moolenaar02631462017-09-22 15:20:32 +02006968 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 * if invalid value, use 0.
6970 * Set default to window width (maximum 79) for "gq" operator.
6971 */
6972 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006973comp_textwidth(
6974 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 int textwidth;
6977
6978 textwidth = curbuf->b_p_tw;
6979 if (textwidth == 0 && curbuf->b_p_wm)
6980 {
6981 /* The width is the window width minus 'wrapmargin' minus all the
6982 * things that add to the margin. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006983 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#ifdef FEAT_CMDWIN
6985 if (cmdwin_type != 0)
6986 textwidth -= 1;
6987#endif
6988#ifdef FEAT_FOLDING
6989 textwidth -= curwin->w_p_fdc;
6990#endif
6991#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006992 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 textwidth -= 1;
6994#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006995 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 textwidth -= 8;
6997 }
6998 if (textwidth < 0)
6999 textwidth = 0;
7000 if (ff && textwidth == 0)
7001 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007002 textwidth = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 if (textwidth > 79)
7004 textwidth = 79;
7005 }
7006 return textwidth;
7007}
7008
7009/*
7010 * Put a character in the redo buffer, for when just after a CTRL-V.
7011 */
7012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007013redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 char_u buf[10];
7016
7017 /* Only digits need special treatment. Translate them into a string of
7018 * three digits. */
7019 if (VIM_ISDIGIT(c))
7020 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007021 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 AppendToRedobuff(buf);
7023 }
7024 else
7025 AppendCharToRedobuff(c);
7026}
7027
7028/*
7029 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007030 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 */
7032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007033start_arrow(
7034 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007036 start_arrow_common(end_insert_pos, TRUE);
7037}
7038
7039/*
7040 * Like start_arrow() but with end_change argument.
7041 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
7042 */
7043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007044start_arrow_with_change(
7045 pos_T *end_insert_pos, /* can be NULL */
7046 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007047{
7048 start_arrow_common(end_insert_pos, end_change);
7049 if (!end_change)
7050 {
7051 AppendCharToRedobuff(Ctrl_G);
7052 AppendCharToRedobuff('U');
7053 }
7054}
7055
7056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007057start_arrow_common(
7058 pos_T *end_insert_pos, /* can be NULL */
7059 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02007060{
7061 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
7063 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01007064 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 arrow_used = TRUE; /* this means we stopped the current insert */
7066 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007067#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007068 check_spell_redraw();
7069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070}
7071
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007072#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007073/*
7074 * If we skipped highlighting word at cursor, do it now.
7075 * It may be skipped again, thus reset spell_redraw_lnum first.
7076 */
7077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00007079{
7080 if (spell_redraw_lnum != 0)
7081 {
7082 linenr_T lnum = spell_redraw_lnum;
7083
7084 spell_redraw_lnum = 0;
7085 redrawWinline(lnum, FALSE);
7086 }
7087}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007088
7089/*
7090 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
7091 * spelled word, if there is one.
7092 */
7093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007094spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007095{
7096 pos_T tpos = curwin->w_cursor;
7097
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00007098 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00007099 if (curwin->w_cursor.col != tpos.col)
7100 start_arrow(&tpos);
7101}
Bram Moolenaar217ad922005-03-20 22:37:15 +00007102#endif
7103
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104/*
7105 * stop_arrow() is called before a change is made in insert mode.
7106 * If an arrow key has been used, start a new insertion.
7107 * Returns FAIL if undo is impossible, shouldn't insert then.
7108 */
7109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111{
7112 if (arrow_used)
7113 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007114 Insstart = curwin->w_cursor; /* new insertion starts here */
7115 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
7116 /* Don't update the original insert position when moved to the
7117 * right, except when nothing was inserted yet. */
7118 update_Insstart_orig = FALSE;
7119 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
7120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 if (u_save_cursor() == OK)
7122 {
7123 arrow_used = FALSE;
7124 ins_need_undo = FALSE;
7125 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02007126
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 ai_col = 0;
7128#ifdef FEAT_VREPLACE
7129 if (State & VREPLACE_FLAG)
7130 {
7131 orig_line_count = curbuf->b_ml.ml_line_count;
7132 vr_lines_changed = 1;
7133 }
7134#endif
7135 ResetRedobuff();
7136 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00007137 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 }
7139 else if (ins_need_undo)
7140 {
7141 if (u_save_cursor() == OK)
7142 ins_need_undo = FALSE;
7143 }
7144
7145#ifdef FEAT_FOLDING
7146 /* Always open fold at the cursor line when inserting something. */
7147 foldOpenCursor();
7148#endif
7149
7150 return (arrow_used || ins_need_undo ? FAIL : OK);
7151}
7152
7153/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007154 * Do a few things to stop inserting.
7155 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
7156 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 */
7158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007159stop_insert(
7160 pos_T *end_insert_pos,
7161 int esc, /* called by ins_esc() */
7162 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007164 int cc;
7165 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
7167 stop_redo_ins();
7168 replace_flush(); /* abandon replace stack */
7169
7170 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007171 * Save the inserted text for later redo with ^@ and CTRL-A.
7172 * Don't do it when "restart_edit" was set and nothing was inserted,
7173 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007175 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007176 if (did_restart_edit == 0 || (ptr != NULL
7177 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00007178 {
7179 vim_free(last_insert);
7180 last_insert = ptr;
7181 last_insert_skip = new_insert_skip;
7182 }
7183 else
7184 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007186 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 /* Auto-format now. It may seem strange to do this when stopping an
7189 * insertion (or moving the cursor), but it's required when appending
7190 * a line and having it end in a space. But only do it when something
7191 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007192 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007194 pos_T tpos = curwin->w_cursor;
7195
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 /* When the cursor is at the end of the line after a space the
7197 * formatting will move it to the following word. Avoid that by
7198 * moving the cursor onto the space. */
7199 cc = 'x';
7200 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7201 {
7202 dec_cursor();
7203 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007204 if (!VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007205 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
7207
7208 auto_format(TRUE, FALSE);
7209
Bram Moolenaar1c465442017-03-12 20:10:05 +01007210 if (VIM_ISWHITE(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007211 {
7212 if (gchar_cursor() != NUL)
7213 inc_cursor();
7214#ifdef FEAT_VIRTUALEDIT
7215 /* If the cursor is still at the same character, also keep
7216 * the "coladd". */
7217 if (gchar_cursor() == NUL
7218 && curwin->w_cursor.lnum == tpos.lnum
7219 && curwin->w_cursor.col == tpos.col)
7220 curwin->w_cursor.coladd = tpos.coladd;
7221#endif
7222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 }
7224
7225 /* If a space was inserted for auto-formatting, remove it now. */
7226 check_auto_format(TRUE);
7227
7228 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007229 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007230 * Do this when ESC was used or moving the cursor up/down.
7231 * Check for the old position still being valid, just in case the text
7232 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007233 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007234 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7235 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007237 pos_T tpos = curwin->w_cursor;
7238
7239 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007240 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007241 for (;;)
7242 {
7243 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7244 --curwin->w_cursor.col;
7245 cc = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007246 if (!VIM_ISWHITE(cc))
Bram Moolenaar39f05632006-03-19 22:15:26 +00007247 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007248 if (del_char(TRUE) == FAIL)
7249 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007250 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007251 if (curwin->w_cursor.lnum != tpos.lnum)
7252 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007253 else
7254 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007255 /* reset tpos, could have been invalidated in the loop above */
7256 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007257 tpos.col++;
7258 if (cc != NUL && gchar_pos(&tpos) == NUL)
7259 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 /* <C-S-Right> may have started Visual mode, adjust the position for
7263 * deleted characters. */
7264 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7265 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007266 int len = (int)STRLEN(ml_get_curline());
7267
7268 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007270 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007271#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 }
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277 }
7278 did_ai = FALSE;
7279#ifdef FEAT_SMARTINDENT
7280 did_si = FALSE;
7281 can_si = FALSE;
7282 can_si_back = FALSE;
7283#endif
7284
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007285 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7286 * now in a different buffer. */
7287 if (end_insert_pos != NULL)
7288 {
7289 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007290 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007291 curbuf->b_op_end = *end_insert_pos;
7292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293}
7294
7295/*
7296 * Set the last inserted text to a single character.
7297 * Used for the replace command.
7298 */
7299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
7302 char_u *s;
7303
7304 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 if (last_insert != NULL)
7307 {
7308 s = last_insert;
7309 /* Use the CTRL-V only when entering a special char */
7310 if (c < ' ' || c == DEL)
7311 *s++ = Ctrl_V;
7312 s = add_char2buf(c, s);
7313 *s++ = ESC;
7314 *s++ = NUL;
7315 last_insert_skip = 0;
7316 }
7317}
7318
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007319#if defined(EXITFREE) || defined(PROTO)
7320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007321free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007322{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007323 VIM_CLEAR(last_insert);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007324# ifdef FEAT_INS_EXPAND
Bram Moolenaard23a8232018-02-10 18:45:26 +01007325 VIM_CLEAR(compl_orig_text);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007326# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007327}
7328#endif
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330/*
7331 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7332 * and CSI. Handle multi-byte characters.
7333 * Returns a pointer to after the added bytes.
7334 */
7335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007336add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
7338#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007339 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 int i;
7341 int len;
7342
7343 len = (*mb_char2bytes)(c, temp);
7344 for (i = 0; i < len; ++i)
7345 {
7346 c = temp[i];
7347#endif
7348 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7349 if (c == K_SPECIAL)
7350 {
7351 *s++ = K_SPECIAL;
7352 *s++ = KS_SPECIAL;
7353 *s++ = KE_FILLER;
7354 }
7355#ifdef FEAT_GUI
7356 else if (c == CSI)
7357 {
7358 *s++ = CSI;
7359 *s++ = KS_EXTRA;
7360 *s++ = (int)KE_CSI;
7361 }
7362#endif
7363 else
7364 *s++ = c;
7365#ifdef FEAT_MBYTE
7366 }
7367#endif
7368 return s;
7369}
7370
7371/*
7372 * move cursor to start of line
7373 * if flags & BL_WHITE move to first non-white
7374 * if flags & BL_SOL move to first non-white if startofline is set,
7375 * otherwise keep "curswant" column
7376 * if flags & BL_FIX don't leave the cursor on a NUL.
7377 */
7378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007379beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 if ((flags & BL_SOL) && !p_sol)
7382 coladvance(curwin->w_curswant);
7383 else
7384 {
7385 curwin->w_cursor.col = 0;
7386#ifdef FEAT_VIRTUALEDIT
7387 curwin->w_cursor.coladd = 0;
7388#endif
7389
7390 if (flags & (BL_WHITE | BL_SOL))
7391 {
7392 char_u *ptr;
7393
Bram Moolenaar1c465442017-03-12 20:10:05 +01007394 for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7396 ++curwin->w_cursor.col;
7397 }
7398 curwin->w_set_curswant = TRUE;
7399 }
7400}
7401
7402/*
7403 * oneright oneleft cursor_down cursor_up
7404 *
7405 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007406 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * Return OK when successful, FAIL when we hit a line of file boundary.
7408 */
7409
7410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
7416#ifdef FEAT_VIRTUALEDIT
7417 if (virtual_active())
7418 {
7419 pos_T prevpos = curwin->w_cursor;
7420
7421 /* Adjust for multi-wide char (excluding TAB) */
7422 ptr = ml_get_cursor();
7423 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007424# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007426# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 ))
7430 ? ptr2cells(ptr) : 1));
7431 curwin->w_set_curswant = TRUE;
7432 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7433 return (prevpos.col != curwin->w_cursor.col
7434 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7435 }
7436#endif
7437
7438 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007439 if (*ptr == NUL)
7440 return FAIL; /* already at the very end */
7441
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007443 if (has_mbyte)
7444 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 else
7446#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007447 l = 1;
7448
7449 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7450 * contains "onemore". */
7451 if (ptr[l] == NUL
7452#ifdef FEAT_VIRTUALEDIT
7453 && (ve_flags & VE_ONEMORE) == 0
7454#endif
7455 )
7456 return FAIL;
7457 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 curwin->w_set_curswant = TRUE;
7460 return OK;
7461}
7462
7463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007464oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465{
7466#ifdef FEAT_VIRTUALEDIT
7467 if (virtual_active())
7468 {
Bram Moolenaara83fe752017-06-29 22:33:13 +02007469# ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 int width;
Bram Moolenaara83fe752017-06-29 22:33:13 +02007471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 int v = getviscol();
7473
7474 if (v == 0)
7475 return FAIL;
7476
7477# ifdef FEAT_LINEBREAK
7478 /* We might get stuck on 'showbreak', skip over it. */
7479 width = 1;
7480 for (;;)
7481 {
7482 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007483 /* getviscol() is slow, skip it when 'showbreak' is empty,
7484 * 'breakindent' is not set and there are no multi-byte
7485 * characters */
7486 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487# ifdef FEAT_MBYTE
7488 && !has_mbyte
7489# endif
7490 ) || getviscol() < v)
7491 break;
7492 ++width;
7493 }
7494# else
7495 coladvance(v - 1);
7496# endif
7497
7498 if (curwin->w_cursor.coladd == 1)
7499 {
7500 char_u *ptr;
7501
7502 /* Adjust for multi-wide char (not a TAB) */
7503 ptr = ml_get_cursor();
7504 if (*ptr != TAB && vim_isprintc(
7505# ifdef FEAT_MBYTE
7506 (*mb_ptr2char)(ptr)
7507# else
7508 *ptr
7509# endif
7510 ) && ptr2cells(ptr) > 1)
7511 curwin->w_cursor.coladd = 0;
7512 }
7513
7514 curwin->w_set_curswant = TRUE;
7515 return OK;
7516 }
7517#endif
7518
7519 if (curwin->w_cursor.col == 0)
7520 return FAIL;
7521
7522 curwin->w_set_curswant = TRUE;
7523 --curwin->w_cursor.col;
7524
7525#ifdef FEAT_MBYTE
7526 /* if the character on the left of the current cursor is a multi-byte
7527 * character, move to its first byte */
7528 if (has_mbyte)
7529 mb_adjust_cursor();
7530#endif
7531 return OK;
7532}
7533
7534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535cursor_up(
7536 long n,
7537 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
7539 linenr_T lnum;
7540
7541 if (n > 0)
7542 {
7543 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007544 /* This fails if the cursor is already in the first line or the count
7545 * is larger than the line number and '-' is in 'cpoptions' */
7546 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 return FAIL;
7548 if (n >= lnum)
7549 lnum = 1;
7550 else
7551#ifdef FEAT_FOLDING
7552 if (hasAnyFolding(curwin))
7553 {
7554 /*
7555 * Count each sequence of folded lines as one logical line.
7556 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007557 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 (void)hasFolding(lnum, &lnum, NULL);
7559
7560 while (n--)
7561 {
7562 /* move up one line */
7563 --lnum;
7564 if (lnum <= 1)
7565 break;
7566 /* If we entered a fold, move to the beginning, unless in
7567 * Insert mode or when 'foldopen' contains "all": it will open
7568 * in a moment. */
7569 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7570 (void)hasFolding(lnum, &lnum, NULL);
7571 }
7572 if (lnum < 1)
7573 lnum = 1;
7574 }
7575 else
7576#endif
7577 lnum -= n;
7578 curwin->w_cursor.lnum = lnum;
7579 }
7580
7581 /* try to advance to the column we want to be at */
7582 coladvance(curwin->w_curswant);
7583
7584 if (upd_topline)
7585 update_topline(); /* make sure curwin->w_topline is valid */
7586
7587 return OK;
7588}
7589
7590/*
7591 * Cursor down a number of logical lines.
7592 */
7593 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007594cursor_down(
7595 long n,
7596 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598 linenr_T lnum;
7599
7600 if (n > 0)
7601 {
7602 lnum = curwin->w_cursor.lnum;
7603#ifdef FEAT_FOLDING
7604 /* Move to last line of fold, will fail if it's the end-of-file. */
7605 (void)hasFolding(lnum, NULL, &lnum);
7606#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007607 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007608 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007609 if (lnum >= curbuf->b_ml.ml_line_count
7610 || (lnum + n > curbuf->b_ml.ml_line_count
7611 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 return FAIL;
7613 if (lnum + n >= curbuf->b_ml.ml_line_count)
7614 lnum = curbuf->b_ml.ml_line_count;
7615 else
7616#ifdef FEAT_FOLDING
7617 if (hasAnyFolding(curwin))
7618 {
7619 linenr_T last;
7620
7621 /* count each sequence of folded lines as one logical line */
7622 while (n--)
7623 {
7624 if (hasFolding(lnum, NULL, &last))
7625 lnum = last + 1;
7626 else
7627 ++lnum;
7628 if (lnum >= curbuf->b_ml.ml_line_count)
7629 break;
7630 }
7631 if (lnum > curbuf->b_ml.ml_line_count)
7632 lnum = curbuf->b_ml.ml_line_count;
7633 }
7634 else
7635#endif
7636 lnum += n;
7637 curwin->w_cursor.lnum = lnum;
7638 }
7639
7640 /* try to advance to the column we want to be at */
7641 coladvance(curwin->w_curswant);
7642
7643 if (upd_topline)
7644 update_topline(); /* make sure curwin->w_topline is valid */
7645
7646 return OK;
7647}
7648
7649/*
7650 * Stuff the last inserted text in the read buffer.
7651 * Last_insert actually is a copy of the redo buffer, so we
7652 * first have to remove the command.
7653 */
7654 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007655stuff_inserted(
7656 int c, /* Command character to be inserted */
7657 long count, /* Repeat this many times */
7658 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
7660 char_u *esc_ptr;
7661 char_u *ptr;
7662 char_u *last_ptr;
7663 char_u last = NUL;
7664
7665 ptr = get_last_insert();
7666 if (ptr == NULL)
7667 {
7668 EMSG(_(e_noinstext));
7669 return FAIL;
7670 }
7671
7672 /* may want to stuff the command character, to start Insert mode */
7673 if (c != NUL)
7674 stuffcharReadbuff(c);
7675 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7676 *esc_ptr = NUL; /* remove the ESC */
7677
7678 /* when the last char is either "0" or "^" it will be quoted if no ESC
7679 * comes after it OR if it will inserted more than once and "ptr"
7680 * starts with ^D. -- Acevedo
7681 */
7682 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7683 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7684 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7685 {
7686 last = *last_ptr;
7687 *last_ptr = NUL;
7688 }
7689
7690 do
7691 {
7692 stuffReadbuff(ptr);
7693 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7694 if (last)
7695 stuffReadbuff((char_u *)(last == '0'
7696 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7697 : IF_EB("\026^", CTRL_V_STR "^")));
7698 }
7699 while (--count > 0);
7700
7701 if (last)
7702 *last_ptr = last;
7703
7704 if (esc_ptr != NULL)
7705 *esc_ptr = ESC; /* put the ESC back */
7706
7707 /* may want to stuff a trailing ESC, to get out of Insert mode */
7708 if (!no_esc)
7709 stuffcharReadbuff(ESC);
7710
7711 return OK;
7712}
7713
7714 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007715get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 if (last_insert == NULL)
7718 return NULL;
7719 return last_insert + last_insert_skip;
7720}
7721
7722/*
7723 * Get last inserted string, and remove trailing <Esc>.
7724 * Returns pointer to allocated memory (must be freed) or NULL.
7725 */
7726 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007727get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
7729 char_u *s;
7730 int len;
7731
7732 if (last_insert == NULL)
7733 return NULL;
7734 s = vim_strsave(last_insert + last_insert_skip);
7735 if (s != NULL)
7736 {
7737 len = (int)STRLEN(s);
7738 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7739 s[len - 1] = NUL;
7740 }
7741 return s;
7742}
7743
7744/*
7745 * Check the word in front of the cursor for an abbreviation.
7746 * Called when the non-id character "c" has been entered.
7747 * When an abbreviation is recognized it is removed from the text and
7748 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7749 */
7750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007751echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 /* Don't check for abbreviation in paste mode, when disabled and just
7754 * after moving around with cursor keys. */
7755 if (p_paste || no_abbr || arrow_used)
7756 return FALSE;
7757
7758 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7759 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7760}
7761
7762/*
7763 * replace-stack functions
7764 *
7765 * When replacing characters, the replaced characters are remembered for each
7766 * new character. This is used to re-insert the old text when backspacing.
7767 *
7768 * There is a NUL headed list of characters for each character that is
7769 * currently in the file after the insertion point. When BS is used, one NUL
7770 * headed list is put back for the deleted character.
7771 *
7772 * For a newline, there are two NUL headed lists. One contains the characters
7773 * that the NL replaced. The extra one stores the characters after the cursor
7774 * that were deleted (always white space).
7775 *
7776 * Replace_offset is normally 0, in which case replace_push will add a new
7777 * character at the end of the stack. If replace_offset is not 0, that many
7778 * characters will be left on the stack above the newly inserted character.
7779 */
7780
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007781static char_u *replace_stack = NULL;
7782static long replace_stack_nr = 0; /* next entry in replace stack */
7783static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
7785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007786replace_push(
7787 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 char_u *p;
7790
7791 if (replace_stack_nr < replace_offset) /* nothing to do */
7792 return;
7793 if (replace_stack_len <= replace_stack_nr)
7794 {
7795 replace_stack_len += 50;
7796 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7797 if (p == NULL) /* out of memory */
7798 {
7799 replace_stack_len -= 50;
7800 return;
7801 }
7802 if (replace_stack != NULL)
7803 {
7804 mch_memmove(p, replace_stack,
7805 (size_t)(replace_stack_nr * sizeof(char_u)));
7806 vim_free(replace_stack);
7807 }
7808 replace_stack = p;
7809 }
7810 p = replace_stack + replace_stack_nr - replace_offset;
7811 if (replace_offset)
7812 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7813 *p = c;
7814 ++replace_stack_nr;
7815}
7816
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007817#if defined(FEAT_MBYTE) || defined(PROTO)
7818/*
7819 * Push a character onto the replace stack. Handles a multi-byte character in
7820 * reverse byte order, so that the first byte is popped off first.
7821 * Return the number of bytes done (includes composing characters).
7822 */
7823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007824replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007825{
7826 int l = (*mb_ptr2len)(p);
7827 int j;
7828
7829 for (j = l - 1; j >= 0; --j)
7830 replace_push(p[j]);
7831 return l;
7832}
7833#endif
7834
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835/*
7836 * Pop one item from the replace stack.
7837 * return -1 if stack empty
7838 * return replaced character or NUL otherwise
7839 */
7840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007841replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 if (replace_stack_nr == 0)
7844 return -1;
7845 return (int)replace_stack[--replace_stack_nr];
7846}
7847
7848/*
7849 * Join the top two items on the replace stack. This removes to "off"'th NUL
7850 * encountered.
7851 */
7852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007853replace_join(
7854 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 int i;
7857
7858 for (i = replace_stack_nr; --i >= 0; )
7859 if (replace_stack[i] == NUL && off-- <= 0)
7860 {
7861 --replace_stack_nr;
7862 mch_memmove(replace_stack + i, replace_stack + i + 1,
7863 (size_t)(replace_stack_nr - i));
7864 return;
7865 }
7866}
7867
7868/*
7869 * Pop bytes from the replace stack until a NUL is found, and insert them
7870 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7871 */
7872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007873replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 int cc;
7876 int oldState = State;
7877
7878 State = NORMAL; /* don't want REPLACE here */
7879 while ((cc = replace_pop()) > 0)
7880 {
7881#ifdef FEAT_MBYTE
7882 mb_replace_pop_ins(cc);
7883#else
7884 ins_char(cc);
7885#endif
7886 dec_cursor();
7887 }
7888 State = oldState;
7889}
7890
7891#ifdef FEAT_MBYTE
7892/*
7893 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7894 * indicates a multi-byte char, pop the other bytes too.
7895 */
7896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007897mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007900 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 int i;
7902 int c;
7903
7904 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7905 {
7906 buf[0] = cc;
7907 for (i = 1; i < n; ++i)
7908 buf[i] = replace_pop();
7909 ins_bytes_len(buf, n);
7910 }
7911 else
7912 ins_char(cc);
7913
7914 if (enc_utf8)
7915 /* Handle composing chars. */
7916 for (;;)
7917 {
7918 c = replace_pop();
7919 if (c == -1) /* stack empty */
7920 break;
7921 if ((n = MB_BYTE2LEN(c)) == 1)
7922 {
7923 /* Not a multi-byte char, put it back. */
7924 replace_push(c);
7925 break;
7926 }
7927 else
7928 {
7929 buf[0] = c;
7930 for (i = 1; i < n; ++i)
7931 buf[i] = replace_pop();
7932 if (utf_iscomposing(utf_ptr2char(buf)))
7933 ins_bytes_len(buf, n);
7934 else
7935 {
7936 /* Not a composing char, put it back. */
7937 for (i = n - 1; i >= 0; --i)
7938 replace_push(buf[i]);
7939 break;
7940 }
7941 }
7942 }
7943}
7944#endif
7945
7946/*
7947 * make the replace stack empty
7948 * (called when exiting replace mode)
7949 */
7950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007951replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007953 VIM_CLEAR(replace_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 replace_stack_len = 0;
7955 replace_stack_nr = 0;
7956}
7957
7958/*
7959 * Handle doing a BS for one character.
7960 * cc < 0: replace stack empty, just move cursor
7961 * cc == 0: character was inserted, delete it
7962 * cc > 0: character was replaced, put cc (first byte of original char) back
7963 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007964 * When "limit_col" is >= 0, don't delete before this column. Matters when
7965 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 */
7967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007968replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 int cc;
7971#ifdef FEAT_VREPLACE
7972 int orig_len = 0;
7973 int ins_len;
7974 int orig_vcols = 0;
7975 colnr_T start_vcol;
7976 char_u *p;
7977 int i;
7978 int vcol;
7979#endif
7980
7981 cc = replace_pop();
7982 if (cc > 0)
7983 {
7984#ifdef FEAT_VREPLACE
7985 if (State & VREPLACE_FLAG)
7986 {
7987 /* Get the number of screen cells used by the character we are
7988 * going to delete. */
7989 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7990 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7991 }
7992#endif
7993#ifdef FEAT_MBYTE
7994 if (has_mbyte)
7995 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007996 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997# ifdef FEAT_VREPLACE
7998 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007999 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000# endif
8001 replace_push(cc);
8002 }
8003 else
8004#endif
8005 {
8006 pchar_cursor(cc);
8007#ifdef FEAT_VREPLACE
8008 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008009 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010#endif
8011 }
8012 replace_pop_ins();
8013
8014#ifdef FEAT_VREPLACE
8015 if (State & VREPLACE_FLAG)
8016 {
8017 /* Get the number of screen cells used by the inserted characters */
8018 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008019 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 vcol = start_vcol;
8021 for (i = 0; i < ins_len; ++i)
8022 {
8023 vcol += chartabsize(p + i, vcol);
8024#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008025 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026#endif
8027 }
8028 vcol -= start_vcol;
8029
8030 /* Delete spaces that were inserted after the cursor to keep the
8031 * text aligned. */
8032 curwin->w_cursor.col += ins_len;
8033 while (vcol > orig_vcols && gchar_cursor() == ' ')
8034 {
8035 del_char(FALSE);
8036 ++orig_vcols;
8037 }
8038 curwin->w_cursor.col -= ins_len;
8039 }
8040#endif
8041
8042 /* mark the buffer as changed and prepare for displaying */
8043 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
8044 }
8045 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008046 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047}
8048
8049#ifdef FEAT_CINDENT
8050/*
8051 * Return TRUE if C-indenting is on.
8052 */
8053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008054cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055{
8056 return (!p_paste && (curbuf->b_p_cin
8057# ifdef FEAT_EVAL
8058 || *curbuf->b_p_inde != NUL
8059# endif
8060 ));
8061}
8062#endif
8063
8064#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
8065/*
8066 * Re-indent the current line, based on the current contents of it and the
8067 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
8068 * confused what all the part that handles Control-T is doing that I'm not.
8069 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
8070 */
8071
8072 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008073fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008075 int amount = get_the_indent();
8076
8077 if (amount >= 0)
8078 {
8079 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
8080 if (linewhite(curwin->w_cursor.lnum))
8081 did_ai = TRUE; /* delete the indent if the line stays empty */
8082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083}
8084
8085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008086fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
8088 if (p_paste)
8089 return;
8090# ifdef FEAT_LISP
8091 if (curbuf->b_p_lisp && curbuf->b_p_ai)
8092 fixthisline(get_lisp_indent);
8093# endif
8094# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
8095 else
8096# endif
8097# ifdef FEAT_CINDENT
8098 if (cindent_on())
8099 do_c_expr_indent();
8100# endif
8101}
8102
8103#endif
8104
8105#ifdef FEAT_CINDENT
8106/*
8107 * return TRUE if 'cinkeys' contains the key "keytyped",
8108 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008109 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
8111 *
8112 * "keytyped" can have a few special values:
8113 * KEY_OPEN_FORW
8114 * KEY_OPEN_BACK
8115 * KEY_COMPLETE just finished completion.
8116 *
8117 * If line_is_empty is TRUE accept keys with '0' before them.
8118 */
8119 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008120in_cinkeys(
8121 int keytyped,
8122 int when,
8123 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
8125 char_u *look;
8126 int try_match;
8127 int try_match_word;
8128 char_u *p;
8129 char_u *line;
8130 int icase;
8131 int i;
8132
Bram Moolenaar30848942009-12-24 14:46:12 +00008133 if (keytyped == NUL)
8134 /* Can happen with CTRL-Y and CTRL-E on a short line. */
8135 return FALSE;
8136
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137#ifdef FEAT_EVAL
8138 if (*curbuf->b_p_inde != NUL)
8139 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
8140 else
8141#endif
8142 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
8143 while (*look)
8144 {
8145 /*
8146 * Find out if we want to try a match with this key, depending on
8147 * 'when' and a '*' or '!' before the key.
8148 */
8149 switch (when)
8150 {
8151 case '*': try_match = (*look == '*'); break;
8152 case '!': try_match = (*look == '!'); break;
8153 default: try_match = (*look != '*'); break;
8154 }
8155 if (*look == '*' || *look == '!')
8156 ++look;
8157
8158 /*
8159 * If there is a '0', only accept a match if the line is empty.
8160 * But may still match when typing last char of a word.
8161 */
8162 if (*look == '0')
8163 {
8164 try_match_word = try_match;
8165 if (!line_is_empty)
8166 try_match = FALSE;
8167 ++look;
8168 }
8169 else
8170 try_match_word = FALSE;
8171
8172 /*
8173 * does it look like a control character?
8174 */
8175 if (*look == '^'
8176#ifdef EBCDIC
8177 && (Ctrl_chr(look[1]) != 0)
8178#else
8179 && look[1] >= '?' && look[1] <= '_'
8180#endif
8181 )
8182 {
8183 if (try_match && keytyped == Ctrl_chr(look[1]))
8184 return TRUE;
8185 look += 2;
8186 }
8187 /*
8188 * 'o' means "o" command, open forward.
8189 * 'O' means "O" command, open backward.
8190 */
8191 else if (*look == 'o')
8192 {
8193 if (try_match && keytyped == KEY_OPEN_FORW)
8194 return TRUE;
8195 ++look;
8196 }
8197 else if (*look == 'O')
8198 {
8199 if (try_match && keytyped == KEY_OPEN_BACK)
8200 return TRUE;
8201 ++look;
8202 }
8203
8204 /*
8205 * 'e' means to check for "else" at start of line and just before the
8206 * cursor.
8207 */
8208 else if (*look == 'e')
8209 {
8210 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8211 {
8212 p = ml_get_curline();
8213 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8214 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8215 return TRUE;
8216 }
8217 ++look;
8218 }
8219
8220 /*
8221 * ':' only causes an indent if it is at the end of a label or case
8222 * statement, or when it was before typing the ':' (to fix
8223 * class::method for C++).
8224 */
8225 else if (*look == ':')
8226 {
8227 if (try_match && keytyped == ':')
8228 {
8229 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008230 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008232 /* Need to get the line again after cin_islabel(). */
8233 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 if (curwin->w_cursor.col > 2
8235 && p[curwin->w_cursor.col - 1] == ':'
8236 && p[curwin->w_cursor.col - 2] == ':')
8237 {
8238 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008239 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008240 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 p = ml_get_curline();
8242 p[curwin->w_cursor.col - 1] = ':';
8243 if (i)
8244 return TRUE;
8245 }
8246 }
8247 ++look;
8248 }
8249
8250
8251 /*
8252 * Is it a key in <>, maybe?
8253 */
8254 else if (*look == '<')
8255 {
8256 if (try_match)
8257 {
8258 /*
8259 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8260 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8261 * >, *, : and ! keys if they really really want to.
8262 */
8263 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8264 && keytyped == look[1])
8265 return TRUE;
8266
8267 if (keytyped == get_special_key_code(look + 1))
8268 return TRUE;
8269 }
8270 while (*look && *look != '>')
8271 look++;
8272 while (*look == '>')
8273 look++;
8274 }
8275
8276 /*
8277 * Is it a word: "=word"?
8278 */
8279 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8280 {
8281 ++look;
8282 if (*look == '~')
8283 {
8284 icase = TRUE;
8285 ++look;
8286 }
8287 else
8288 icase = FALSE;
8289 p = vim_strchr(look, ',');
8290 if (p == NULL)
8291 p = look + STRLEN(look);
8292 if ((try_match || try_match_word)
8293 && curwin->w_cursor.col >= (colnr_T)(p - look))
8294 {
8295 int match = FALSE;
8296
8297#ifdef FEAT_INS_EXPAND
8298 if (keytyped == KEY_COMPLETE)
8299 {
8300 char_u *s;
8301
8302 /* Just completed a word, check if it starts with "look".
8303 * search back for the start of a word. */
8304 line = ml_get_curline();
8305# ifdef FEAT_MBYTE
8306 if (has_mbyte)
8307 {
8308 char_u *n;
8309
8310 for (s = line + curwin->w_cursor.col; s > line; s = n)
8311 {
8312 n = mb_prevptr(line, s);
8313 if (!vim_iswordp(n))
8314 break;
8315 }
8316 }
8317 else
8318# endif
8319 for (s = line + curwin->w_cursor.col; s > line; --s)
8320 if (!vim_iswordc(s[-1]))
8321 break;
8322 if (s + (p - look) <= line + curwin->w_cursor.col
8323 && (icase
8324 ? MB_STRNICMP(s, look, p - look)
8325 : STRNCMP(s, look, p - look)) == 0)
8326 match = TRUE;
8327 }
8328 else
8329#endif
8330 /* TODO: multi-byte */
8331 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8332 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8333 {
8334 line = ml_get_cursor();
8335 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8336 || !vim_iswordc(line[-(p - look) - 1]))
8337 && (icase
8338 ? MB_STRNICMP(line - (p - look), look, p - look)
8339 : STRNCMP(line - (p - look), look, p - look))
8340 == 0)
8341 match = TRUE;
8342 }
8343 if (match && try_match_word && !try_match)
8344 {
8345 /* "0=word": Check if there are only blanks before the
8346 * word. */
Bram Moolenaar1b383442017-09-26 20:04:54 +02008347 if (getwhitecols_curline() !=
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 (int)(curwin->w_cursor.col - (p - look)))
8349 match = FALSE;
8350 }
8351 if (match)
8352 return TRUE;
8353 }
8354 look = p;
8355 }
8356
8357 /*
8358 * ok, it's a boring generic character.
8359 */
8360 else
8361 {
8362 if (try_match && *look == keytyped)
8363 return TRUE;
Bram Moolenaar60629d62017-02-23 18:08:56 +01008364 if (*look != NUL)
8365 ++look;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 }
8367
8368 /*
8369 * Skip over ", ".
8370 */
8371 look = skip_to_option_part(look);
8372 }
8373 return FALSE;
8374}
8375#endif /* FEAT_CINDENT */
8376
8377#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8378/*
8379 * Map Hebrew keyboard when in hkmap mode.
8380 */
8381 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008382hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
8384 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8385 {
8386 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8387 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8388 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8389 static char_u map[26] =
8390 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8391 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8392 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8393 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8394 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8395 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8396 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8397 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8398 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8399
8400 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8401 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8402 /* '-1'='sofit' */
8403 else if (c == 'x')
8404 return 'X';
8405 else if (c == 'q')
8406 return '\''; /* {geresh}={'} */
8407 else if (c == 246)
8408 return ' '; /* \"o --> ' ' for a german keyboard */
8409 else if (c == 228)
8410 return ' '; /* \"a --> ' ' -- / -- */
8411 else if (c == 252)
8412 return ' '; /* \"u --> ' ' -- / -- */
8413#ifdef EBCDIC
8414 else if (islower(c))
8415#else
8416 /* NOTE: islower() does not do the right thing for us on Linux so we
8417 * do this the same was as 5.7 and previous, so it works correctly on
8418 * all systems. Specifically, the e.g. Delete and Arrow keys are
8419 * munged and won't work if e.g. searching for Hebrew text.
8420 */
8421 else if (c >= 'a' && c <= 'z')
8422#endif
8423 return (int)(map[CharOrdLow(c)] + p_aleph);
8424 else
8425 return c;
8426 }
8427 else
8428 {
8429 switch (c)
8430 {
8431 case '`': return ';';
8432 case '/': return '.';
8433 case '\'': return ',';
8434 case 'q': return '/';
8435 case 'w': return '\'';
8436
8437 /* Hebrew letters - set offset from 'a' */
8438 case ',': c = '{'; break;
8439 case '.': c = 'v'; break;
8440 case ';': c = 't'; break;
8441 default: {
8442 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8443
8444#ifdef EBCDIC
8445 /* see note about islower() above */
8446 if (!islower(c))
8447#else
8448 if (c < 'a' || c > 'z')
8449#endif
8450 return c;
8451 c = str[CharOrdLow(c)];
8452 break;
8453 }
8454 }
8455
8456 return (int)(CharOrdLow(c) + p_aleph);
8457 }
8458}
8459#endif
8460
8461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008462ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
8464 int need_redraw = FALSE;
8465 int regname;
8466 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008467 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468
8469 /*
8470 * If we are going to wait for a character, show a '"'.
8471 */
8472 pc_status = PC_STATUS_UNSET;
8473 if (redrawing() && !char_avail())
8474 {
8475 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008476 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477
8478 edit_putchar('"', TRUE);
8479#ifdef FEAT_CMDL_INFO
8480 add_to_showcmd_c(Ctrl_R);
8481#endif
8482 }
8483
8484#ifdef USE_ON_FLY_SCROLL
8485 dont_scroll = TRUE; /* disallow scrolling here */
8486#endif
8487
8488 /*
8489 * Don't map the register name. This also prevents the mode message to be
8490 * deleted when ESC is hit.
8491 */
8492 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008493 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8496 {
8497 /* Get a third key for literal register insertion */
8498 literally = regname;
8499#ifdef FEAT_CMDL_INFO
8500 add_to_showcmd_c(literally);
8501#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008502 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 }
8505 --no_mapping;
8506
8507#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008508 /* Don't call u_sync() while typing the expression or giving an error
8509 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 ++no_u_sync;
8511 if (regname == '=')
8512 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008513# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008515# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008516 /* Sync undo when evaluating the expression calls setline() or
8517 * append(), so that it can be undone separately. */
8518 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008519
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 regname = get_expr_register();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008521# ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 /* Restore the Input Method. */
8523 if (im_on)
8524 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008527 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8528 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008529 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 else
8533 {
8534#endif
8535 if (literally == Ctrl_O || literally == Ctrl_P)
8536 {
8537 /* Append the command to the redo buffer. */
8538 AppendCharToRedobuff(Ctrl_R);
8539 AppendCharToRedobuff(literally);
8540 AppendCharToRedobuff(regname);
8541
8542 do_put(regname, BACKWARD, 1L,
8543 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8544 }
8545 else if (insert_reg(regname, literally) == FAIL)
8546 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008547 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 need_redraw = TRUE; /* remove the '"' */
8549 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008550 else if (stop_insert_mode)
8551 /* When the '=' register was used and a function was invoked that
8552 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8553 * insert anything, need to remove the '"' */
8554 need_redraw = TRUE;
8555
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556#ifdef FEAT_EVAL
8557 }
8558 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008559 if (u_sync_once == 1)
8560 ins_need_undo = TRUE;
8561 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562#endif
8563#ifdef FEAT_CMDL_INFO
8564 clear_showcmd();
8565#endif
8566
8567 /* If the inserted register is empty, we need to remove the '"' */
8568 if (need_redraw || stuff_empty())
8569 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008570
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008571 /* Disallow starting Visual mode here, would get a weird mode. */
8572 if (!vis_active && VIsual_active)
8573 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574}
8575
8576/*
8577 * CTRL-G commands in Insert mode.
8578 */
8579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008580ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
8582 int c;
8583
8584#ifdef FEAT_INS_EXPAND
8585 /* Right after CTRL-X the cursor will be after the ruler. */
8586 setcursor();
8587#endif
8588
8589 /*
8590 * Don't map the second key. This also prevents the mode message to be
8591 * deleted when ESC is hit.
8592 */
8593 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008594 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 --no_mapping;
8596 switch (c)
8597 {
8598 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8599 case K_UP:
8600 case Ctrl_K:
8601 case 'k': ins_up(TRUE);
8602 break;
8603
8604 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8605 case K_DOWN:
8606 case Ctrl_J:
8607 case 'j': ins_down(TRUE);
8608 break;
8609
8610 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008611 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008613
8614 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008615 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008616 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008617 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 break;
8619
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008620 /* CTRL-G U: do not break undo with the next char */
8621 case 'U':
8622 /* Allow one left/right cursor movement with the next char,
8623 * without breaking undo. */
8624 dont_sync_undo = MAYBE;
8625 break;
8626
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008628 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 }
8630}
8631
8632/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008633 * CTRL-^ in Insert mode.
8634 */
8635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008636ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008637{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008638 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008639 {
8640 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8641 if (State & LANGMAP)
8642 {
8643 curbuf->b_p_iminsert = B_IMODE_NONE;
8644 State &= ~LANGMAP;
8645 }
8646 else
8647 {
8648 curbuf->b_p_iminsert = B_IMODE_LMAP;
8649 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008650#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008651 im_set_active(FALSE);
8652#endif
8653 }
8654 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008655#ifdef HAVE_INPUT_METHOD
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008656 else
8657 {
8658 /* There are no ":lmap" mappings, toggle IM */
8659 if (im_get_status())
8660 {
8661 curbuf->b_p_iminsert = B_IMODE_NONE;
8662 im_set_active(FALSE);
8663 }
8664 else
8665 {
8666 curbuf->b_p_iminsert = B_IMODE_IM;
8667 State &= ~LANGMAP;
8668 im_set_active(TRUE);
8669 }
8670 }
8671#endif
8672 set_iminsert_global();
8673 showmode();
8674#ifdef FEAT_GUI
8675 /* may show different cursor shape or color */
8676 if (gui.in_use)
8677 gui_update_cursor(TRUE, FALSE);
8678#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02008679#if defined(FEAT_KEYMAP)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008680 /* Show/unshow value of 'keymap' in status lines. */
8681 status_redraw_curbuf();
8682#endif
8683}
8684
8685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 * Handle ESC in insert mode.
8687 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8688 * insert.
8689 */
8690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008691ins_esc(
8692 long *count,
8693 int cmdchar,
8694 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
8696 int temp;
8697 static int disabled_redraw = FALSE;
8698
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008699#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008700 check_spell_redraw();
8701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702#if defined(FEAT_HANGULIN)
8703# if defined(ESC_CHG_TO_ENG_MODE)
8704 hangul_input_state_set(0);
8705# endif
8706 if (composing_hangul)
8707 {
8708 push_raw_key(composing_hangul_buffer, 2);
8709 composing_hangul = 0;
8710 }
8711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712
8713 temp = curwin->w_cursor.col;
8714 if (disabled_redraw)
8715 {
8716 --RedrawingDisabled;
8717 disabled_redraw = FALSE;
8718 }
8719 if (!arrow_used)
8720 {
8721 /*
8722 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008723 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8724 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 */
8726 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008727 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728
8729 /*
8730 * Repeating insert may take a long time. Check for
8731 * interrupt now and then.
8732 */
8733 if (*count > 0)
8734 {
8735 line_breakcheck();
8736 if (got_int)
8737 *count = 0;
8738 }
8739
8740 if (--*count > 0) /* repeat what was typed */
8741 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008742 /* Vi repeats the insert without replacing characters. */
8743 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8744 State &= ~REPLACE_FLAG;
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 (void)start_redo_ins();
8747 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008748 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 ++RedrawingDisabled;
8750 disabled_redraw = TRUE;
8751 return FALSE; /* repeat the insert */
8752 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008753 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 undisplay_dollar();
8755 }
8756
8757 /* When an autoindent was removed, curswant stays after the
8758 * indent */
8759 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8760 curwin->w_set_curswant = TRUE;
8761
8762 /* Remember the last Insert position in the '^ mark. */
8763 if (!cmdmod.keepjumps)
8764 curbuf->b_last_insert = curwin->w_cursor;
8765
8766 /*
8767 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008768 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008770 if (!nomove
8771 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772#ifdef FEAT_VIRTUALEDIT
8773 || curwin->w_cursor.coladd > 0
8774#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008775 )
8776 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008777 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778#ifdef FEAT_RIGHTLEFT
8779 && !revins_on
8780#endif
8781 )
8782 {
8783#ifdef FEAT_VIRTUALEDIT
8784 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8785 {
8786 oneleft();
8787 if (restart_edit != NUL)
8788 ++curwin->w_cursor.coladd;
8789 }
8790 else
8791#endif
8792 {
8793 --curwin->w_cursor.col;
8794#ifdef FEAT_MBYTE
8795 /* Correct cursor for multi-byte character. */
8796 if (has_mbyte)
8797 mb_adjust_cursor();
8798#endif
8799 }
8800 }
8801
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008802#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 /* Disable IM to allow typing English directly for Normal mode commands.
8804 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8805 * well). */
8806 if (!(State & LANGMAP))
8807 im_save_status(&curbuf->b_p_iminsert);
8808 im_set_active(FALSE);
8809#endif
8810
8811 State = NORMAL;
8812 /* need to position cursor again (e.g. when on a TAB ) */
8813 changed_cline_bef_curs();
8814
8815#ifdef FEAT_MOUSE
8816 setmouse();
8817#endif
8818#ifdef CURSOR_SHAPE
8819 ui_cursor_shape(); /* may show different cursor shape */
8820#endif
Bram Moolenaar48c9f3b2017-01-24 19:08:15 +01008821 if (!p_ek)
8822 /* Re-enable bracketed paste mode. */
8823 out_str(T_BE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824
8825 /*
8826 * When recording or for CTRL-O, need to display the new mode.
8827 * Otherwise remove the mode message.
8828 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008829 if (reg_recording != 0 || restart_edit != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 showmode();
8831 else if (p_smd)
8832 MSG("");
8833
8834 return TRUE; /* exit Insert mode */
8835}
8836
8837#ifdef FEAT_RIGHTLEFT
8838/*
8839 * Toggle language: hkmap and revins_on.
8840 * Move to end of reverse inserted text.
8841 */
8842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008843ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
8845 if (revins_on && revins_chars && revins_scol >= 0)
8846 {
8847 while (gchar_cursor() != NUL && revins_chars--)
8848 ++curwin->w_cursor.col;
8849 }
8850 p_ri = !p_ri;
8851 revins_on = (State == INSERT && p_ri);
8852 if (revins_on)
8853 {
8854 revins_scol = curwin->w_cursor.col;
8855 revins_legal++;
8856 revins_chars = 0;
8857 undisplay_dollar();
8858 }
8859 else
8860 revins_scol = -1;
8861#ifdef FEAT_FKMAP
8862 if (p_altkeymap)
8863 {
8864 /*
8865 * to be consistent also for redo command, using '.'
8866 * set arrow_used to true and stop it - causing to redo
8867 * characters entered in one mode (normal/reverse insert).
8868 */
8869 arrow_used = TRUE;
8870 (void)stop_arrow();
8871 p_fkmap = curwin->w_p_rl ^ p_ri;
8872 if (p_fkmap && p_ri)
8873 State = INSERT;
8874 }
8875 else
8876#endif
8877 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8878 showmode();
8879}
8880#endif
8881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882/*
8883 * If 'keymodel' contains "startsel", may start selection.
8884 * Returns TRUE when a CTRL-O and other keys stuffed.
8885 */
8886 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008887ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 if (km_startsel)
8890 switch (c)
8891 {
8892 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 case K_PAGEUP:
8895 case K_KPAGEUP:
8896 case K_PAGEDOWN:
8897 case K_KPAGEDOWN:
Bram Moolenaard0573012017-10-28 21:11:06 +02008898# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 case K_LEFT:
8900 case K_RIGHT:
8901 case K_UP:
8902 case K_DOWN:
8903 case K_END:
8904 case K_HOME:
8905# endif
8906 if (!(mod_mask & MOD_MASK_SHIFT))
8907 break;
8908 /* FALLTHROUGH */
8909 case K_S_LEFT:
8910 case K_S_RIGHT:
8911 case K_S_UP:
8912 case K_S_DOWN:
8913 case K_S_END:
8914 case K_S_HOME:
8915 /* Start selection right away, the cursor can move with
8916 * CTRL-O when beyond the end of the line. */
8917 start_selection();
8918
8919 /* Execute the key in (insert) Select mode. */
8920 stuffcharReadbuff(Ctrl_O);
8921 if (mod_mask)
8922 {
8923 char_u buf[4];
8924
8925 buf[0] = K_SPECIAL;
8926 buf[1] = KS_MODIFIER;
8927 buf[2] = mod_mask;
8928 buf[3] = NUL;
8929 stuffReadbuff(buf);
8930 }
8931 stuffcharReadbuff(c);
8932 return TRUE;
8933 }
8934 return FALSE;
8935}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936
8937/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008938 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008939 */
8940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008941ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008942{
8943#ifdef FEAT_FKMAP
8944 if (p_fkmap && p_ri)
8945 {
8946 beep_flush();
8947 EMSG(farsi_text_3); /* encoded in Farsi */
8948 return;
8949 }
8950#endif
8951
Bram Moolenaar1e015462005-09-25 22:16:38 +00008952# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008953 set_vim_var_string(VV_INSERTMODE,
8954 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008955# ifdef FEAT_VREPLACE
8956 replaceState == VREPLACE ? "v" :
8957# endif
8958 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008959# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008960 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008961 if (State & REPLACE_FLAG)
8962 State = INSERT | (State & LANGMAP);
8963 else
8964 State = replaceState | (State & LANGMAP);
8965 AppendCharToRedobuff(K_INS);
8966 showmode();
8967#ifdef CURSOR_SHAPE
8968 ui_cursor_shape(); /* may show different cursor shape */
8969#endif
8970}
8971
8972/*
8973 * Pressed CTRL-O in Insert mode.
8974 */
8975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008976ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008977{
8978#ifdef FEAT_VREPLACE
8979 if (State & VREPLACE_FLAG)
8980 restart_edit = 'V';
8981 else
8982#endif
8983 if (State & REPLACE_FLAG)
8984 restart_edit = 'R';
8985 else
8986 restart_edit = 'I';
8987#ifdef FEAT_VIRTUALEDIT
8988 if (virtual_active())
8989 ins_at_eol = FALSE; /* cursor always keeps its column */
8990 else
8991#endif
8992 ins_at_eol = (gchar_cursor() == NUL);
8993}
8994
8995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 * If the cursor is on an indent, ^T/^D insert/delete one
8997 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008998 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 * with vi. But vi only supports ^T and ^D after an
9000 * autoindent, we support it everywhere.
9001 */
9002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009003ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
9005 if (stop_arrow() == FAIL)
9006 return;
9007 AppendCharToRedobuff(c);
9008
9009 /*
9010 * 0^D and ^^D: remove all indent.
9011 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00009012 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
9013 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 {
9015 --curwin->w_cursor.col;
9016 (void)del_char(FALSE); /* delete the '^' or '0' */
9017 /* In Replace mode, restore the characters that '^' or '0' replaced. */
9018 if (State & REPLACE_FLAG)
9019 replace_pop_ins();
9020 if (lastc == '^')
9021 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009022 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 }
9024 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009025 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026
9027 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
9028 did_ai = FALSE;
9029#ifdef FEAT_SMARTINDENT
9030 did_si = FALSE;
9031 can_si = FALSE;
9032 can_si_back = FALSE;
9033#endif
9034#ifdef FEAT_CINDENT
9035 can_cindent = FALSE; /* no cindenting after ^D or ^T */
9036#endif
9037}
9038
9039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009040ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041{
9042 int temp;
9043
9044 if (stop_arrow() == FAIL)
9045 return;
9046 if (gchar_cursor() == NUL) /* delete newline */
9047 {
9048 temp = curwin->w_cursor.col;
9049 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009050 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009051 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 else
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 curwin->w_cursor.col = temp;
Bram Moolenaar63e82db2018-03-06 12:10:48 +01009055#ifdef FEAT_VREPLACE
9056 /* Adjust orig_line_count in case more lines have been deleted than
9057 * have been added. That makes sure, that open_line() later
9058 * can access all buffer lines correctly */
9059 if (State & VREPLACE_FLAG &&
9060 orig_line_count > curbuf->b_ml.ml_line_count)
9061 orig_line_count = curbuf->b_ml.ml_line_count;
9062#endif
9063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02009065 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
9066 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 did_ai = FALSE;
9068#ifdef FEAT_SMARTINDENT
9069 did_si = FALSE;
9070 can_si = FALSE;
9071 can_si_back = FALSE;
9072#endif
9073 AppendCharToRedobuff(K_DEL);
9074}
9075
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009076static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009077
9078/*
9079 * Delete one character for ins_bs().
9080 */
9081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009082ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009083{
9084 dec_cursor();
9085 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
9086 if (State & REPLACE_FLAG)
9087 {
9088 /* Don't delete characters before the insert point when in
9089 * Replace mode */
9090 if (curwin->w_cursor.lnum != Insstart.lnum
9091 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009092 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009093 }
9094 else
9095 (void)del_char(FALSE);
9096}
9097
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098/*
9099 * Handle Backspace, delete-word and delete-line in Insert mode.
9100 * Return TRUE when backspace was actually used.
9101 */
9102 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009103ins_bs(
9104 int c,
9105 int mode,
9106 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108 linenr_T lnum;
9109 int cc;
9110 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009111 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 colnr_T mincol;
9113 int did_backspace = FALSE;
9114 int in_indent;
9115 int oldState;
9116#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009117 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118#endif
9119
9120 /*
9121 * can't delete anything in an empty file
9122 * can't backup past first character in buffer
9123 * can't backup past starting point unless 'backspace' > 1
9124 * can backup to a previous line if 'backspace' == 0
9125 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009126 if ( BUFEMPTY()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 || (
9128#ifdef FEAT_RIGHTLEFT
9129 !revins_on &&
9130#endif
9131 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
9132 || (!can_bs(BS_START)
9133 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009134 || (curwin->w_cursor.lnum == Insstart_orig.lnum
9135 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
9137 && curwin->w_cursor.col <= ai_col)
9138 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
9139 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02009140 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 return FALSE;
9142 }
9143
9144 if (stop_arrow() == FAIL)
9145 return FALSE;
9146 in_indent = inindent(0);
9147#ifdef FEAT_CINDENT
9148 if (in_indent)
9149 can_cindent = FALSE;
9150#endif
9151#ifdef FEAT_COMMENTS
9152 end_comment_pending = NUL; /* After BS, don't auto-end comment */
9153#endif
9154#ifdef FEAT_RIGHTLEFT
9155 if (revins_on) /* put cursor after last inserted char */
9156 inc_cursor();
9157#endif
9158
9159#ifdef FEAT_VIRTUALEDIT
9160 /* Virtualedit:
9161 * BACKSPACE_CHAR eats a virtual space
9162 * BACKSPACE_WORD eats all coladd
9163 * BACKSPACE_LINE eats all coladd and keeps going
9164 */
9165 if (curwin->w_cursor.coladd > 0)
9166 {
9167 if (mode == BACKSPACE_CHAR)
9168 {
9169 --curwin->w_cursor.coladd;
9170 return TRUE;
9171 }
9172 if (mode == BACKSPACE_WORD)
9173 {
9174 curwin->w_cursor.coladd = 0;
9175 return TRUE;
9176 }
9177 curwin->w_cursor.coladd = 0;
9178 }
9179#endif
9180
9181 /*
Bram Moolenaar878c2632017-04-01 15:15:52 +02009182 * Delete newline!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 */
9184 if (curwin->w_cursor.col == 0)
9185 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009186 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009187 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#ifdef FEAT_RIGHTLEFT
9189 || revins_on
9190#endif
9191 )
9192 {
9193 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
9194 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
9195 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01009196 --Insstart.lnum;
Bram Moolenaar04000562017-04-03 21:35:42 +02009197 Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 }
9199 /*
9200 * In replace mode:
9201 * cc < 0: NL was inserted, delete it
9202 * cc >= 0: NL was replaced, put original characters back
9203 */
9204 cc = -1;
9205 if (State & REPLACE_FLAG)
9206 cc = replace_pop(); /* returns -1 if NL was inserted */
9207 /*
9208 * In replace mode, in the line we started replacing, we only move the
9209 * cursor.
9210 */
9211 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9212 {
9213 dec_cursor();
9214 }
9215 else
9216 {
9217#ifdef FEAT_VREPLACE
9218 if (!(State & VREPLACE_FLAG)
9219 || curwin->w_cursor.lnum > orig_line_count)
9220#endif
9221 {
9222 temp = gchar_cursor(); /* remember current char */
9223 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009224
9225 /* When "aw" is in 'formatoptions' we must delete the space at
9226 * the end of the line, otherwise the line will be broken
9227 * again when auto-formatting. */
9228 if (has_format_option(FO_AUTO)
9229 && has_format_option(FO_WHITE_PAR))
9230 {
9231 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9232 TRUE);
9233 int len;
9234
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009235 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009236 if (len > 0 && ptr[len - 1] == ' ')
9237 ptr[len - 1] = NUL;
9238 }
9239
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009240 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 if (temp == NUL && gchar_cursor() != NUL)
9242 inc_cursor();
9243 }
9244#ifdef FEAT_VREPLACE
9245 else
9246 dec_cursor();
9247#endif
9248
9249 /*
9250 * In REPLACE mode we have to put back the text that was replaced
9251 * by the NL. On the replace stack is first a NUL-terminated
9252 * sequence of characters that were deleted and then the
9253 * characters that NL replaced.
9254 */
9255 if (State & REPLACE_FLAG)
9256 {
9257 /*
9258 * Do the next ins_char() in NORMAL state, to
9259 * prevent ins_char() from replacing characters and
9260 * avoiding showmatch().
9261 */
9262 oldState = State;
9263 State = NORMAL;
9264 /*
9265 * restore characters (blanks) deleted after cursor
9266 */
9267 while (cc > 0)
9268 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009269 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270#ifdef FEAT_MBYTE
9271 mb_replace_pop_ins(cc);
9272#else
9273 ins_char(cc);
9274#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009275 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 cc = replace_pop();
9277 }
9278 /* restore the characters that NL replaced */
9279 replace_pop_ins();
9280 State = oldState;
9281 }
9282 }
9283 did_ai = FALSE;
9284 }
9285 else
9286 {
9287 /*
9288 * Delete character(s) before the cursor.
9289 */
9290#ifdef FEAT_RIGHTLEFT
9291 if (revins_on) /* put cursor on last inserted char */
9292 dec_cursor();
9293#endif
9294 mincol = 0;
9295 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009296 if (mode == BACKSPACE_LINE
9297 && (curbuf->b_p_ai
9298#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009299 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009300#endif
9301 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302#ifdef FEAT_RIGHTLEFT
9303 && !revins_on
9304#endif
9305 )
9306 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009307 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009309 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009311 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 }
9313
9314 /*
9315 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9316 */
9317 if ( mode == BACKSPACE_CHAR
9318 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009319 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009320 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 && (*(ml_get_cursor() - 1) == TAB
9322 || (*(ml_get_cursor() - 1) == ' '
9323 && (!*inserted_space_p
9324 || arrow_used))))))
9325 {
9326 int ts;
9327 colnr_T vcol;
9328 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009329 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330
9331 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009332 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009333 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009335 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 /* Compute the virtual column where we want to be. Since
9337 * 'showbreak' may get in the way, need to get the last column of
9338 * the previous character. */
9339 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009340 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 dec_cursor();
9342 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9343 inc_cursor();
9344 want_vcol = (want_vcol / ts) * ts;
9345
9346 /* delete characters until we are at or before want_vcol */
9347 while (vcol > want_vcol
Bram Moolenaar1c465442017-03-12 20:10:05 +01009348 && (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009349 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350
9351 /* insert extra spaces until we are at want_vcol */
9352 while (vcol < want_vcol)
9353 {
9354 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009355 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9356 && curwin->w_cursor.col < Insstart_orig.col)
9357 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358
9359#ifdef FEAT_VREPLACE
9360 if (State & VREPLACE_FLAG)
9361 ins_char(' ');
9362 else
9363#endif
9364 {
9365 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009366 if ((State & REPLACE_FLAG))
9367 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 }
9369 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9370 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009371
9372 /* If we are now back where we started delete one character. Can
9373 * happen when using 'sts' and 'linebreak'. */
9374 if (vcol >= start_vcol)
9375 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 }
9377
9378 /*
9379 * Delete upto starting point, start of line or previous word.
9380 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009381 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009383#ifdef FEAT_MBYTE
9384 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009386 if (has_mbyte)
9387 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009389 do
9390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009392 if (!revins_on) /* put cursor on char to be deleted */
9393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009395
9396 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009398 /* look multi-byte character class */
9399 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009401 prev_cclass = cclass;
9402 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009405
9406 /* start of word? */
9407 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9408 {
9409 mode = BACKSPACE_WORD_NOT_SPACE;
9410 temp = vim_iswordc(cc);
9411 }
9412 /* end of word? */
9413 else if (mode == BACKSPACE_WORD_NOT_SPACE
9414 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9415#ifdef FEAT_MBYTE
9416 || prev_cclass != cclass
9417#endif
9418 ))
9419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009421 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009423 inc_cursor();
9424#ifdef FEAT_RIGHTLEFT
9425 else if (State & REPLACE_FLAG)
9426 dec_cursor();
9427#endif
9428 break;
9429 }
9430 if (State & REPLACE_FLAG)
9431 replace_do_bs(-1);
9432 else
9433 {
9434#ifdef FEAT_MBYTE
9435 if (enc_utf8 && p_deco)
9436 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9437#endif
9438 (void)del_char(FALSE);
9439#ifdef FEAT_MBYTE
9440 /*
9441 * If there are combining characters and 'delcombine' is set
9442 * move the cursor back. Don't back up before the base
9443 * character.
9444 */
9445 if (enc_utf8 && p_deco && cpc[0] != NUL)
9446 inc_cursor();
9447#endif
9448#ifdef FEAT_RIGHTLEFT
9449 if (revins_chars)
9450 {
9451 revins_chars--;
9452 revins_legal++;
9453 }
9454 if (revins_on && gchar_cursor() == NUL)
9455 break;
9456#endif
9457 }
9458 /* Just a single backspace?: */
9459 if (mode == BACKSPACE_CHAR)
9460 break;
9461 } while (
9462#ifdef FEAT_RIGHTLEFT
9463 revins_on ||
9464#endif
9465 (curwin->w_cursor.col > mincol
9466 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9467 || curwin->w_cursor.col != Insstart_orig.col)));
9468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 did_backspace = TRUE;
9470 }
9471#ifdef FEAT_SMARTINDENT
9472 did_si = FALSE;
9473 can_si = FALSE;
9474 can_si_back = FALSE;
9475#endif
9476 if (curwin->w_cursor.col <= 1)
9477 did_ai = FALSE;
9478 /*
9479 * It's a little strange to put backspaces into the redo
9480 * buffer, but it makes auto-indent a lot easier to deal
9481 * with.
9482 */
9483 AppendCharToRedobuff(c);
9484
9485 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009486 if (curwin->w_cursor.lnum == Insstart_orig.lnum
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009487 && curwin->w_cursor.col < Insstart_orig.col)
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009488 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489
9490 /* vi behaviour: the cursor moves backward but the character that
9491 * was there remains visible
9492 * Vim behaviour: the cursor moves backward and the character that
9493 * was there is erased from the screen.
9494 * We can emulate the vi behaviour by pretending there is a dollar
9495 * displayed even when there isn't.
9496 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009497 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 dollar_vcol = curwin->w_virtcol;
9499
Bram Moolenaarce3be472008-01-14 19:12:28 +00009500#ifdef FEAT_FOLDING
9501 /* When deleting a char the cursor line must never be in a closed fold.
9502 * E.g., when 'foldmethod' is indent and deleting the first non-white
9503 * char before a Tab. */
9504 if (did_backspace)
9505 foldOpenCursor();
9506#endif
9507
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 return did_backspace;
9509}
9510
9511#ifdef FEAT_MOUSE
9512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009513ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009516 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517
9518# ifdef FEAT_GUI
9519 /* When GUI is active, also move/paste when 'mouse' is empty */
9520 if (!gui.in_use)
9521# endif
9522 if (!mouse_has(MOUSE_INSERT))
9523 return;
9524
9525 undisplay_dollar();
9526 tpos = curwin->w_cursor;
9527 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9528 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009529 win_T *new_curwin = curwin;
9530
9531 if (curwin != old_curwin && win_valid(old_curwin))
9532 {
9533 /* Mouse took us to another window. We need to go back to the
9534 * previous one to stop insert there properly. */
9535 curwin = old_curwin;
9536 curbuf = curwin->w_buffer;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02009537#ifdef FEAT_JOB_CHANNEL
9538 if (bt_prompt(curbuf))
9539 // Restart Insert mode when re-entering the prompt buffer.
9540 curbuf->b_prompt_insert = 'A';
9541#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009542 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009543 start_arrow(curwin == old_curwin ? &tpos : NULL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009544 if (curwin != new_curwin && win_valid(new_curwin))
9545 {
9546 curwin = new_curwin;
9547 curbuf = curwin->w_buffer;
9548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549# ifdef FEAT_CINDENT
9550 can_cindent = TRUE;
9551# endif
9552 }
9553
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 /* redraw status lines (in case another window became active) */
9555 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556}
9557
9558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009559ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560{
9561 pos_T tpos;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009562 win_T *old_curwin = curwin, *wp;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009563# ifdef FEAT_INS_EXPAND
9564 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565# endif
9566
9567 tpos = curwin->w_cursor;
9568
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009569 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 {
9571 int row, col;
9572
9573 row = mouse_row;
9574 col = mouse_col;
9575
9576 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02009577 wp = mouse_find_win(&row, &col);
9578 if (wp == NULL)
9579 return;
9580 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 curbuf = curwin->w_buffer;
9582 }
9583 if (curwin == old_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 undisplay_dollar();
9585
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009586# ifdef FEAT_INS_EXPAND
9587 /* Don't scroll the window in which completion is being done. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009588 if (!pum_visible() || curwin != old_curwin)
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009589# endif
9590 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009591 if (dir == MSCR_DOWN || dir == MSCR_UP)
9592 {
9593 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9594 scroll_redraw(dir,
9595 (long)(curwin->w_botline - curwin->w_topline));
9596 else
9597 scroll_redraw(dir, 3L);
9598 }
9599#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009600 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009601 {
9602 int val, step = 6;
9603
9604 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02009605 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009606 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9607 if (val < 0)
9608 val = 0;
9609 gui_do_horiz_scroll(val, TRUE);
9610 }
9611#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009612# ifdef FEAT_INS_EXPAND
9613 did_scroll = TRUE;
9614# endif
9615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 curwin->w_redr_status = TRUE;
9618
9619 curwin = old_curwin;
9620 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009622# ifdef FEAT_INS_EXPAND
9623 /* The popup menu may overlay the window, need to redraw it.
9624 * TODO: Would be more efficient to only redraw the windows that are
9625 * overlapped by the popup menu. */
9626 if (pum_visible() && did_scroll)
9627 {
9628 redraw_all_later(NOT_VALID);
9629 ins_compl_show_pum();
9630 }
9631# endif
9632
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009633 if (!EQUAL_POS(curwin->w_cursor, tpos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 {
9635 start_arrow(&tpos);
9636# ifdef FEAT_CINDENT
9637 can_cindent = TRUE;
9638# endif
9639 }
9640}
9641#endif
9642
Bram Moolenaarec2da362017-01-21 20:04:22 +01009643/*
9644 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9645 * P_PE literally.
9646 * When "drop" is TRUE then consume the text and drop it.
9647 */
9648 int
9649bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9650{
9651 int c;
9652 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9653 int idx = 0;
9654 char_u *end = find_termcode((char_u *)"PE");
9655 int ret_char = -1;
9656 int save_allow_keys = allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009657 int save_paste = p_paste;
9658 int save_ai = curbuf->b_p_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009659
9660 /* If the end code is too long we can't detect it, read everything. */
9661 if (STRLEN(end) >= NUMBUFLEN)
9662 end = NULL;
9663 ++no_mapping;
9664 allow_keys = 0;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009665 p_paste = TRUE;
9666 curbuf->b_p_ai = FALSE;
9667
Bram Moolenaarec2da362017-01-21 20:04:22 +01009668 for (;;)
9669 {
9670 /* When the end is not defined read everything. */
9671 if (end == NULL && vpeekc() == NUL)
9672 break;
9673 c = plain_vgetc();
9674#ifdef FEAT_MBYTE
9675 if (has_mbyte)
9676 idx += (*mb_char2bytes)(c, buf + idx);
9677 else
9678#endif
9679 buf[idx++] = c;
9680 buf[idx] = NUL;
Bram Moolenaar866c6882017-04-07 14:02:01 +02009681 if (end != NULL && STRNCMP(buf, end, idx) == 0)
Bram Moolenaarec2da362017-01-21 20:04:22 +01009682 {
9683 if (end[idx] == NUL)
9684 break; /* Found the end of paste code. */
9685 continue;
9686 }
9687 if (!drop)
9688 {
9689 switch (mode)
9690 {
9691 case PASTE_CMDLINE:
9692 put_on_cmdline(buf, idx, TRUE);
9693 break;
9694
9695 case PASTE_EX:
9696 if (gap != NULL && ga_grow(gap, idx) == OK)
9697 {
9698 mch_memmove((char *)gap->ga_data + gap->ga_len,
9699 buf, (size_t)idx);
9700 gap->ga_len += idx;
9701 }
9702 break;
9703
9704 case PASTE_INSERT:
9705 if (stop_arrow() == OK)
9706 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009707 c = buf[0];
9708 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9709 ins_eol(c);
9710 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009711 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009712 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009713 AppendToRedobuffLit(buf, idx);
9714 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009715 }
9716 break;
9717
9718 case PASTE_ONE_CHAR:
9719 if (ret_char == -1)
9720 {
9721#ifdef FEAT_MBYTE
9722 if (has_mbyte)
9723 ret_char = (*mb_ptr2char)(buf);
9724 else
9725#endif
9726 ret_char = buf[0];
9727 }
9728 break;
9729 }
9730 }
9731 idx = 0;
9732 }
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009733
Bram Moolenaarec2da362017-01-21 20:04:22 +01009734 --no_mapping;
9735 allow_keys = save_allow_keys;
Bram Moolenaar9e817c82017-01-25 21:36:17 +01009736 p_paste = save_paste;
9737 curbuf->b_p_ai = save_ai;
Bram Moolenaarec2da362017-01-21 20:04:22 +01009738
9739 return ret_char;
9740}
9741
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009742#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009744ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009745{
9746 /* We will be leaving the current window, unless closing another tab. */
9747 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9748 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9749 {
9750 undisplay_dollar();
9751 start_arrow(&curwin->w_cursor);
9752# ifdef FEAT_CINDENT
9753 can_cindent = TRUE;
9754# endif
9755 }
9756
9757 if (c == K_TABLINE)
9758 goto_tabpage(current_tab);
9759 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009760 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009761 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009762 redraw_statuslines(); /* will redraw the tabline when needed */
9763 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009764}
9765#endif
9766
9767#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009769ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 pos_T tpos;
9772
9773 undisplay_dollar();
9774 tpos = curwin->w_cursor;
9775 if (gui_do_scroll())
9776 {
9777 start_arrow(&tpos);
9778# ifdef FEAT_CINDENT
9779 can_cindent = TRUE;
9780# endif
9781 }
9782}
9783
9784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009785ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
9787 pos_T tpos;
9788
9789 undisplay_dollar();
9790 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009791 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 {
9793 start_arrow(&tpos);
9794# ifdef FEAT_CINDENT
9795 can_cindent = TRUE;
9796# endif
9797 }
9798}
9799#endif
9800
9801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009802ins_left(
9803 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804{
9805 pos_T tpos;
9806
9807#ifdef FEAT_FOLDING
9808 if ((fdo_flags & FDO_HOR) && KeyTyped)
9809 foldOpenCursor();
9810#endif
9811 undisplay_dollar();
9812 tpos = curwin->w_cursor;
9813 if (oneleft() == OK)
9814 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009815#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9816 /* Only call start_arrow() when not busy with preediting, it will
9817 * break undo. K_LEFT is inserted in im_correct_cursor(). */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02009818 if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009819#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009820 {
9821 start_arrow_with_change(&tpos, end_change);
9822 if (!end_change)
9823 AppendCharToRedobuff(K_LEFT);
9824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825#ifdef FEAT_RIGHTLEFT
9826 /* If exit reversed string, position is fixed */
9827 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9828 revins_legal++;
9829 revins_chars++;
9830#endif
9831 }
9832
9833 /*
9834 * if 'whichwrap' set for cursor in insert mode may go to
9835 * previous line
9836 */
9837 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9838 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009839 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 start_arrow(&tpos);
9841 --(curwin->w_cursor.lnum);
9842 coladvance((colnr_T)MAXCOL);
9843 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9844 }
9845 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009846 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009847 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848}
9849
9850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009851ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
9853 pos_T tpos;
9854
9855#ifdef FEAT_FOLDING
9856 if ((fdo_flags & FDO_HOR) && KeyTyped)
9857 foldOpenCursor();
9858#endif
9859 undisplay_dollar();
9860 tpos = curwin->w_cursor;
9861 if (c == K_C_HOME)
9862 curwin->w_cursor.lnum = 1;
9863 curwin->w_cursor.col = 0;
9864#ifdef FEAT_VIRTUALEDIT
9865 curwin->w_cursor.coladd = 0;
9866#endif
9867 curwin->w_curswant = 0;
9868 start_arrow(&tpos);
9869}
9870
9871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009872ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873{
9874 pos_T tpos;
9875
9876#ifdef FEAT_FOLDING
9877 if ((fdo_flags & FDO_HOR) && KeyTyped)
9878 foldOpenCursor();
9879#endif
9880 undisplay_dollar();
9881 tpos = curwin->w_cursor;
9882 if (c == K_C_END)
9883 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9884 coladvance((colnr_T)MAXCOL);
9885 curwin->w_curswant = MAXCOL;
9886
9887 start_arrow(&tpos);
9888}
9889
9890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009891ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892{
9893#ifdef FEAT_FOLDING
9894 if ((fdo_flags & FDO_HOR) && KeyTyped)
9895 foldOpenCursor();
9896#endif
9897 undisplay_dollar();
9898 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9899 {
9900 start_arrow(&curwin->w_cursor);
9901 (void)bck_word(1L, FALSE, FALSE);
9902 curwin->w_set_curswant = TRUE;
9903 }
9904 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009905 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906}
9907
9908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009909ins_right(
9910 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912#ifdef FEAT_FOLDING
9913 if ((fdo_flags & FDO_HOR) && KeyTyped)
9914 foldOpenCursor();
9915#endif
9916 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009917 if (gchar_cursor() != NUL
9918#ifdef FEAT_VIRTUALEDIT
9919 || virtual_active()
9920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 )
9922 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009923 start_arrow_with_change(&curwin->w_cursor, end_change);
9924 if (!end_change)
9925 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 curwin->w_set_curswant = TRUE;
9927#ifdef FEAT_VIRTUALEDIT
9928 if (virtual_active())
9929 oneright();
9930 else
9931#endif
9932 {
9933#ifdef FEAT_MBYTE
9934 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009935 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 else
9937#endif
9938 ++curwin->w_cursor.col;
9939 }
9940
9941#ifdef FEAT_RIGHTLEFT
9942 revins_legal++;
9943 if (revins_chars)
9944 revins_chars--;
9945#endif
9946 }
9947 /* if 'whichwrap' set for cursor in insert mode, may move the
9948 * cursor to the next line */
9949 else if (vim_strchr(p_ww, ']') != NULL
9950 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9951 {
9952 start_arrow(&curwin->w_cursor);
9953 curwin->w_set_curswant = TRUE;
9954 ++curwin->w_cursor.lnum;
9955 curwin->w_cursor.col = 0;
9956 }
9957 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009958 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009959 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960}
9961
9962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009963ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965#ifdef FEAT_FOLDING
9966 if ((fdo_flags & FDO_HOR) && KeyTyped)
9967 foldOpenCursor();
9968#endif
9969 undisplay_dollar();
9970 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9971 || gchar_cursor() != NUL)
9972 {
9973 start_arrow(&curwin->w_cursor);
9974 (void)fwd_word(1L, FALSE, 0);
9975 curwin->w_set_curswant = TRUE;
9976 }
9977 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009978 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
9981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009982ins_up(
9983 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984{
9985 pos_T tpos;
9986 linenr_T old_topline = curwin->w_topline;
9987#ifdef FEAT_DIFF
9988 int old_topfill = curwin->w_topfill;
9989#endif
9990
9991 undisplay_dollar();
9992 tpos = curwin->w_cursor;
9993 if (cursor_up(1L, TRUE) == OK)
9994 {
9995 if (startcol)
9996 coladvance(getvcol_nolist(&Insstart));
9997 if (old_topline != curwin->w_topline
9998#ifdef FEAT_DIFF
9999 || old_topfill != curwin->w_topfill
10000#endif
10001 )
10002 redraw_later(VALID);
10003 start_arrow(&tpos);
10004#ifdef FEAT_CINDENT
10005 can_cindent = TRUE;
10006#endif
10007 }
10008 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010009 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010}
10011
10012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010013ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014{
10015 pos_T tpos;
10016
10017 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010018
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010019 if (mod_mask & MOD_MASK_CTRL)
10020 {
10021 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010022 if (first_tabpage->tp_next != NULL)
10023 {
10024 start_arrow(&curwin->w_cursor);
10025 goto_tabpage(-1);
10026 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010027 return;
10028 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010029
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 tpos = curwin->w_cursor;
10031 if (onepage(BACKWARD, 1L) == OK)
10032 {
10033 start_arrow(&tpos);
10034#ifdef FEAT_CINDENT
10035 can_cindent = TRUE;
10036#endif
10037 }
10038 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010039 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040}
10041
10042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010043ins_down(
10044 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045{
10046 pos_T tpos;
10047 linenr_T old_topline = curwin->w_topline;
10048#ifdef FEAT_DIFF
10049 int old_topfill = curwin->w_topfill;
10050#endif
10051
10052 undisplay_dollar();
10053 tpos = curwin->w_cursor;
10054 if (cursor_down(1L, TRUE) == OK)
10055 {
10056 if (startcol)
10057 coladvance(getvcol_nolist(&Insstart));
10058 if (old_topline != curwin->w_topline
10059#ifdef FEAT_DIFF
10060 || old_topfill != curwin->w_topfill
10061#endif
10062 )
10063 redraw_later(VALID);
10064 start_arrow(&tpos);
10065#ifdef FEAT_CINDENT
10066 can_cindent = TRUE;
10067#endif
10068 }
10069 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010070 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071}
10072
10073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010074ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075{
10076 pos_T tpos;
10077
10078 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010079
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010080 if (mod_mask & MOD_MASK_CTRL)
10081 {
10082 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +000010083 if (first_tabpage->tp_next != NULL)
10084 {
10085 start_arrow(&curwin->w_cursor);
10086 goto_tabpage(0);
10087 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010088 return;
10089 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +000010090
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 tpos = curwin->w_cursor;
10092 if (onepage(FORWARD, 1L) == OK)
10093 {
10094 start_arrow(&tpos);
10095#ifdef FEAT_CINDENT
10096 can_cindent = TRUE;
10097#endif
10098 }
10099 else
Bram Moolenaar165bc692015-07-21 17:53:25 +020010100 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101}
10102
10103#ifdef FEAT_DND
10104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010105ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
10107 do_put('~', BACKWARD, 1L, PUT_CURSEND);
10108}
10109#endif
10110
10111/*
10112 * Handle TAB in Insert or Replace mode.
10113 * Return TRUE when the TAB needs to be inserted like a normal character.
10114 */
10115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010116ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118 int ind;
10119 int i;
10120 int temp;
10121
10122 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
10123 Insstart_blank_vcol = get_nolist_virtcol();
10124 if (echeck_abbr(TAB + ABBR_OFF))
10125 return FALSE;
10126
10127 ind = inindent(0);
10128#ifdef FEAT_CINDENT
10129 if (ind)
10130 can_cindent = FALSE;
10131#endif
10132
10133 /*
10134 * When nothing special, insert TAB like a normal character
10135 */
10136 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010137 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010138 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 return TRUE;
10140
10141 if (stop_arrow() == FAIL)
10142 return TRUE;
10143
10144 did_ai = FALSE;
10145#ifdef FEAT_SMARTINDENT
10146 did_si = FALSE;
10147 can_si = FALSE;
10148 can_si_back = FALSE;
10149#endif
10150 AppendToRedobuff((char_u *)"\t");
10151
10152 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010010153 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010154 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
10155 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 else /* otherwise use 'tabstop' */
10157 temp = (int)curbuf->b_p_ts;
10158 temp -= get_nolist_virtcol() % temp;
10159
10160 /*
10161 * Insert the first space with ins_char(). It will delete one char in
10162 * replace mode. Insert the rest with ins_str(); it will not delete any
10163 * chars. For VREPLACE mode, we use ins_char() for all characters.
10164 */
10165 ins_char(' ');
10166 while (--temp > 0)
10167 {
10168#ifdef FEAT_VREPLACE
10169 if (State & VREPLACE_FLAG)
10170 ins_char(' ');
10171 else
10172#endif
10173 {
10174 ins_str((char_u *)" ");
10175 if (State & REPLACE_FLAG) /* no char replaced */
10176 replace_push(NUL);
10177 }
10178 }
10179
10180 /*
10181 * When 'expandtab' not set: Replace spaces by TABs where possible.
10182 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +020010183 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 {
10185 char_u *ptr;
10186#ifdef FEAT_VREPLACE
10187 char_u *saved_line = NULL; /* init for GCC */
10188 pos_T pos;
10189#endif
10190 pos_T fpos;
10191 pos_T *cursor;
10192 colnr_T want_vcol, vcol;
10193 int change_col = -1;
10194 int save_list = curwin->w_p_list;
10195
10196 /*
10197 * Get the current line. For VREPLACE mode, don't make real changes
10198 * yet, just work on a copy of the line.
10199 */
10200#ifdef FEAT_VREPLACE
10201 if (State & VREPLACE_FLAG)
10202 {
10203 pos = curwin->w_cursor;
10204 cursor = &pos;
10205 saved_line = vim_strsave(ml_get_curline());
10206 if (saved_line == NULL)
10207 return FALSE;
10208 ptr = saved_line + pos.col;
10209 }
10210 else
10211#endif
10212 {
10213 ptr = ml_get_cursor();
10214 cursor = &curwin->w_cursor;
10215 }
10216
10217 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10218 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10219 curwin->w_p_list = FALSE;
10220
10221 /* Find first white before the cursor */
10222 fpos = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +010010223 while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 {
10225 --fpos.col;
10226 --ptr;
10227 }
10228
10229 /* In Replace mode, don't change characters before the insert point. */
10230 if ((State & REPLACE_FLAG)
10231 && fpos.lnum == Insstart.lnum
10232 && fpos.col < Insstart.col)
10233 {
10234 ptr += Insstart.col - fpos.col;
10235 fpos.col = Insstart.col;
10236 }
10237
10238 /* compute virtual column numbers of first white and cursor */
10239 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10240 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10241
Bram Moolenaar597a4222014-06-25 14:39:50 +020010242 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10243 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010244 while (VIM_ISWHITE(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010246 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 if (vcol + i > want_vcol)
10248 break;
10249 if (*ptr != TAB)
10250 {
10251 *ptr = TAB;
10252 if (change_col < 0)
10253 {
10254 change_col = fpos.col; /* Column of first change */
10255 /* May have to adjust Insstart */
10256 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10257 Insstart.col = fpos.col;
10258 }
10259 }
10260 ++fpos.col;
10261 ++ptr;
10262 vcol += i;
10263 }
10264
10265 if (change_col >= 0)
10266 {
10267 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010268 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269
10270 /* Skip over the spaces we need. */
10271 while (vcol < want_vcol && *ptr == ' ')
10272 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010273 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 ++ptr;
10275 ++repl_off;
10276 }
10277 if (vcol > want_vcol)
10278 {
10279 /* Must have a char with 'showbreak' just before it. */
10280 --ptr;
10281 --repl_off;
10282 }
10283 fpos.col += repl_off;
10284
10285 /* Delete following spaces. */
10286 i = cursor->col - fpos.col;
10287 if (i > 0)
10288 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010289 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 /* correct replace stack. */
10291 if ((State & REPLACE_FLAG)
10292#ifdef FEAT_VREPLACE
10293 && !(State & VREPLACE_FLAG)
10294#endif
10295 )
10296 for (temp = i; --temp >= 0; )
10297 replace_join(repl_off);
10298 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010299#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010300 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010301 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010302 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010303 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10304 (char_u *)"\t", 1);
10305 }
10306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 cursor->col -= i;
10308
10309#ifdef FEAT_VREPLACE
10310 /*
10311 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10312 * backspacing over the changed spacing and then inserting the new
10313 * spacing.
10314 */
10315 if (State & VREPLACE_FLAG)
10316 {
10317 /* Backspace from real cursor to change_col */
10318 backspace_until_column(change_col);
10319
10320 /* Insert each char in saved_line from changed_col to
10321 * ptr-cursor */
10322 ins_bytes_len(saved_line + change_col,
10323 cursor->col - change_col);
10324 }
10325#endif
10326 }
10327
10328#ifdef FEAT_VREPLACE
10329 if (State & VREPLACE_FLAG)
10330 vim_free(saved_line);
10331#endif
10332 curwin->w_p_list = save_list;
10333 }
10334
10335 return FALSE;
10336}
10337
10338/*
10339 * Handle CR or NL in insert mode.
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010340 * Return FAIL when out of memory or can't undo.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 */
10342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010343ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344{
10345 int i;
10346
10347 if (echeck_abbr(c + ABBR_OFF))
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010348 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 if (stop_arrow() == FAIL)
Bram Moolenaarc3c3e692018-04-26 22:30:33 +020010350 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 undisplay_dollar();
10352
10353 /*
10354 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10355 * character under the cursor. Only push a NUL on the replace stack,
10356 * nothing to put back when the NL is deleted.
10357 */
10358 if ((State & REPLACE_FLAG)
10359#ifdef FEAT_VREPLACE
10360 && !(State & VREPLACE_FLAG)
10361#endif
10362 )
10363 replace_push(NUL);
10364
10365 /*
10366 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10367 * replacing the next line, so we push all of the characters left on the
10368 * line onto the replace stack. This is not done here though, it is done
10369 * in open_line().
10370 */
10371
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010372#ifdef FEAT_VIRTUALEDIT
10373 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10374 * CTRL-O). */
10375 if (virtual_active() && curwin->w_cursor.coladd > 0)
10376 coladvance(getviscol());
10377#endif
10378
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379#ifdef FEAT_RIGHTLEFT
10380# ifdef FEAT_FKMAP
10381 if (p_altkeymap && p_fkmap)
10382 fkmap(NL);
10383# endif
10384 /* NL in reverse insert will always start in the end of
10385 * current line. */
10386 if (revins_on)
10387 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10388#endif
10389
10390 AppendToRedobuff(NL_STR);
10391 i = open_line(FORWARD,
10392#ifdef FEAT_COMMENTS
10393 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10394#endif
10395 0, old_indent);
10396 old_indent = 0;
10397#ifdef FEAT_CINDENT
10398 can_cindent = TRUE;
10399#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010400#ifdef FEAT_FOLDING
10401 /* When inserting a line the cursor line must never be in a closed fold. */
10402 foldOpenCursor();
10403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404
Bram Moolenaar24a2d722018-04-24 19:36:43 +020010405 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406}
10407
10408#ifdef FEAT_DIGRAPHS
10409/*
10410 * Handle digraph in insert mode.
10411 * Returns character still to be inserted, or NUL when nothing remaining to be
10412 * done.
10413 */
10414 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010415ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416{
10417 int c;
10418 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010419 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420
10421 pc_status = PC_STATUS_UNSET;
10422 if (redrawing() && !char_avail())
10423 {
10424 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010425 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426
10427 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010428 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#ifdef FEAT_CMDL_INFO
10430 add_to_showcmd_c(Ctrl_K);
10431#endif
10432 }
10433
10434#ifdef USE_ON_FLY_SCROLL
10435 dont_scroll = TRUE; /* disallow scrolling here */
10436#endif
10437
10438 /* don't map the digraph chars. This also prevents the
10439 * mode message to be deleted when ESC is hit */
10440 ++no_mapping;
10441 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010442 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 --no_mapping;
10444 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010445 if (did_putchar)
10446 /* when the line fits in 'columns' the '?' is at the start of the next
10447 * line and will not be removed by the redraw */
10448 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010449
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 if (IS_SPECIAL(c) || mod_mask) /* special key */
10451 {
10452#ifdef FEAT_CMDL_INFO
10453 clear_showcmd();
10454#endif
10455 insert_special(c, TRUE, FALSE);
10456 return NUL;
10457 }
10458 if (c != ESC)
10459 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010460 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 if (redrawing() && !char_avail())
10462 {
10463 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010464 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465
10466 if (char2cells(c) == 1)
10467 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010468 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010470 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 }
10472#ifdef FEAT_CMDL_INFO
10473 add_to_showcmd_c(c);
10474#endif
10475 }
10476 ++no_mapping;
10477 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010478 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 --no_mapping;
10480 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010481 if (did_putchar)
10482 /* when the line fits in 'columns' the '?' is at the start of the
10483 * next line and will not be removed by a redraw */
10484 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 if (cc != ESC)
10486 {
10487 AppendToRedobuff((char_u *)CTRL_V_STR);
10488 c = getdigraph(c, cc, TRUE);
10489#ifdef FEAT_CMDL_INFO
10490 clear_showcmd();
10491#endif
10492 return c;
10493 }
10494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495#ifdef FEAT_CMDL_INFO
10496 clear_showcmd();
10497#endif
10498 return NUL;
10499}
10500#endif
10501
10502/*
10503 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10504 * Returns the char to be inserted, or NUL if none found.
10505 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010507ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 int c;
10510 int temp;
10511 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010512 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513
10514 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10515 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010516 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 return NUL;
10518 }
10519
10520 /* try to advance to the cursor column */
10521 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010522 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 prev_ptr = ptr;
10524 validate_virtcol();
10525 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10526 {
10527 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010528 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 }
10530 if ((colnr_T)temp > curwin->w_virtcol)
10531 ptr = prev_ptr;
10532
10533#ifdef FEAT_MBYTE
10534 c = (*mb_ptr2char)(ptr);
10535#else
10536 c = *ptr;
10537#endif
10538 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010539 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 return c;
10541}
10542
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010543/*
10544 * CTRL-Y or CTRL-E typed in Insert mode.
10545 */
10546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010547ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010548{
10549 int c = tc;
10550
10551#ifdef FEAT_INS_EXPAND
10552 if (ctrl_x_mode == CTRL_X_SCROLL)
10553 {
10554 if (c == Ctrl_Y)
10555 scrolldown_clamp();
10556 else
10557 scrollup_clamp();
10558 redraw_later(VALID);
10559 }
10560 else
10561#endif
10562 {
10563 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10564 if (c != NUL)
10565 {
10566 long tw_save;
10567
10568 /* The character must be taken literally, insert like it
10569 * was typed after a CTRL-V, and pretend 'textwidth'
10570 * wasn't set. Digits, 'o' and 'x' are special after a
10571 * CTRL-V, don't use it for these. */
10572 if (c < 256 && !isalnum(c))
10573 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10574 tw_save = curbuf->b_p_tw;
10575 curbuf->b_p_tw = -1;
10576 insert_special(c, TRUE, FALSE);
10577 curbuf->b_p_tw = tw_save;
10578#ifdef FEAT_RIGHTLEFT
10579 revins_chars++;
10580 revins_legal++;
10581#endif
10582 c = Ctrl_V; /* pretend CTRL-V is last character */
10583 auto_format(FALSE, TRUE);
10584 }
10585 }
10586 return c;
10587}
10588
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589#ifdef FEAT_SMARTINDENT
10590/*
10591 * Try to do some very smart auto-indenting.
10592 * Used when inserting a "normal" character.
10593 */
10594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010595ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596{
10597 pos_T *pos, old_pos;
10598 char_u *ptr;
10599 int i;
10600 int temp;
10601
10602 /*
10603 * do some very smart indenting when entering '{' or '}'
10604 */
10605 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10606 {
10607 /*
10608 * for '}' set indent equal to indent of line containing matching '{'
10609 */
10610 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10611 {
10612 old_pos = curwin->w_cursor;
10613 /*
10614 * If the matching '{' has a ')' immediately before it (ignoring
10615 * white-space), then line up with the start of the line
10616 * containing the matching '(' if there is one. This handles the
10617 * case where an "if (..\n..) {" statement continues over multiple
10618 * lines -- webb
10619 */
10620 ptr = ml_get(pos->lnum);
10621 i = pos->col;
10622 if (i > 0) /* skip blanks before '{' */
Bram Moolenaar1c465442017-03-12 20:10:05 +010010623 while (--i > 0 && VIM_ISWHITE(ptr[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 ;
10625 curwin->w_cursor.lnum = pos->lnum;
10626 curwin->w_cursor.col = i;
10627 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10628 curwin->w_cursor = *pos;
10629 i = get_indent();
10630 curwin->w_cursor = old_pos;
10631#ifdef FEAT_VREPLACE
10632 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010633 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 else
10635#endif
10636 (void)set_indent(i, SIN_CHANGED);
10637 }
10638 else if (curwin->w_cursor.col > 0)
10639 {
10640 /*
10641 * when inserting '{' after "O" reduce indent, but not
10642 * more than indent of previous line
10643 */
10644 temp = TRUE;
10645 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10646 {
10647 old_pos = curwin->w_cursor;
10648 i = get_indent();
10649 while (curwin->w_cursor.lnum > 1)
10650 {
10651 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10652
10653 /* ignore empty lines and lines starting with '#'. */
10654 if (*ptr != '#' && *ptr != NUL)
10655 break;
10656 }
10657 if (get_indent() >= i)
10658 temp = FALSE;
10659 curwin->w_cursor = old_pos;
10660 }
10661 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010662 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 }
10664 }
10665
10666 /*
10667 * set indent of '#' always to 0
10668 */
10669 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10670 {
10671 /* remember current indent for next line */
10672 old_indent = get_indent();
10673 (void)set_indent(0, SIN_CHANGED);
10674 }
10675
10676 /* Adjust ai_col, the char at this position can be deleted. */
10677 if (ai_col > curwin->w_cursor.col)
10678 ai_col = curwin->w_cursor.col;
10679}
10680#endif
10681
10682/*
10683 * Get the value that w_virtcol would have when 'list' is off.
10684 * Unless 'cpo' contains the 'L' flag.
10685 */
10686 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010687get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
10689 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10690 return getvcol_nolist(&curwin->w_cursor);
10691 validate_virtcol();
10692 return curwin->w_virtcol;
10693}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010694
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010695#if defined(FEAT_EVAL)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010696/*
10697 * Handle the InsertCharPre autocommand.
10698 * "c" is the character that was typed.
10699 * Return a pointer to allocated memory with the replacement string.
10700 * Return NULL to continue inserting "c".
10701 */
10702 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010703do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010704{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010705 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010706 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010707
10708 /* Return quickly when there is nothing to do. */
10709 if (!has_insertcharpre())
10710 return NULL;
10711
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010712# ifdef FEAT_MBYTE
Bram Moolenaar704984a2012-06-01 14:57:51 +020010713 if (has_mbyte)
10714 buf[(*mb_char2bytes)(c, buf)] = NUL;
10715 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010716# endif
Bram Moolenaar704984a2012-06-01 14:57:51 +020010717 {
10718 buf[0] = c;
10719 buf[1] = NUL;
10720 }
10721
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010722 /* Lock the text to avoid weird things from happening. */
10723 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010724 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010725
Bram Moolenaar704984a2012-06-01 14:57:51 +020010726 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010727 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010728 {
10729 /* Get the value of v:char. It may be empty or more than one
10730 * character. Only use it when changed, otherwise continue with the
10731 * original character to avoid breaking autoindent. */
10732 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10733 res = vim_strsave(get_vim_var_str(VV_CHAR));
10734 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010735
10736 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10737 --textlock;
10738
10739 return res;
10740}
10741#endif